fmake

make any project just by typing `fmake`
git clone git@getsh.org:fmake.git
Log | Files | Refs | README | LICENSE

commit a3a7be4f1dcf5e5dec804abb2e54eeb05aa7ddf8
parent 25f0c3f420d1df1bc71228ba96bcf0dc7ff5eed9
Author: Bharatvaj Hemanth <bharatvaj@yahoo.com>
Date:   Tue, 27 Aug 2024 08:09:48 +0530

Add support for windows for file, ext and dir methods

Add incomplete fmake.1 man page

Update LICENSE to MIT/X

Fix Makefile for Windows

Update README with simple graphics that describes usage

Simplify build system addition in config.h using __VA_ARGS__ and macros

Diffstat:
MLICENSE | 22++++++++++++++++++----
MMakefile | 16+++++++++-------
MREADME | 44++++++++++++++++++++++++++++----------------
Mconfig.h | 72+++++++++++++++++++++++++++++++++++-------------------------------------
Mconfig.mk | 8+++++---
Afmake.1 | 17+++++++++++++++++
Mfmake.c | 188++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------
7 files changed, 251 insertions(+), 116 deletions(-)

diff --git a/LICENSE b/LICENSE @@ -1,7 +1,21 @@ -Copyright © 2022 Bharatvaj Hemanth <bvaj@getsh.org> +MIT/X Consortium License -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +© 2024 Bharatvaj Hemanth <bharatvaj@getsh.org> -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: -THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/Makefile b/Makefile @@ -1,14 +1,16 @@ -VERSION = 0.1.3 +include config.mk -CFLAGS += -DFMAKE_VERSION=$(VERSION) -Wall -Wextra -g -PREFIX ?= /usr/local +SOURCE = fmake.c config.h -all: fmake +fmake.exe: $(SOURCE) + cl /nologo $(cl_CFLAGS) $(CFLAGS) fmake.c -fmake: fmake.c config.h +fmake: $(SOURCE) + $(CC) -o $@ $(_CFLAGS) $(CFLAGS) $(LDFLAGS) fmake.c clean: - rm -f fmake + -rm fmake + -del fmake.exe install: fmake mkdir -p $(DESTDIR)$(PREFIX)/bin @@ -18,4 +20,4 @@ install: fmake test: @./fmake ENABLE_DEBUG=1 -.PHONY: all test clean +.PHONY: test clean diff --git a/README b/README @@ -1,27 +1,30 @@ fmake ===== -An attempt at saving developers time. + ______________________________ +| > C:\AndroidApp\ - o x| +|------------------------------| +| $ fmake | +| ++ gradle buildDebug | +!______________________________! -fmake is a program that brings `make`s interface to almost any build system. +A tool for the repo nomad. -See below for the build systems currently supported +fmake is a tool that brings `make`s interface to +almost any build system. -fmake refrains from reading the source files. +fmake "intelligently" knows what targets to build which can be +configured in the config.h file. -fmake "intelligently" knows what targets to build. These can be configured in the config.h file. +Opinionates build directory as 'out' in case of no clear build out +directory standard. Avoids in-source builds. -Opinionates build directory as 'out' in case of no clear build 'out' directory standard. Avoids in-source builds. - -USAGE +Usage ----- -Just type fmake, in your project directory. - - alias m=fmake -a - -to quickly run fmake commands. +Just type `fmake`, in your project directory to quickly run +fmake commands. -When none is found, it just defaults to the `make` in $PATH +When none is found, it just defaults to running `make`. Supported build files --------------------- @@ -31,7 +34,16 @@ Supported build files * configure * gradle * ninja -* nobuild, -* and much more! +* nobuild + +and much more! see the config.h for the complete list Find the complete list in config.h + +Building fmake +-------------- +For *NIX, + make + +For MSVC, + nmake diff --git a/config.h b/config.h @@ -1,45 +1,43 @@ -typedef enum { - FMAKE_FILE, - FMAKE_DIR, - FMAKE_EXT -} file_type_t; - +/* level filetype lookup name cmd cmd args */ #define BUILD_SYSTEMS \ - X(0 , "Makefile" , "make") \ - X(0 , "makefile" , "make") \ - X(0 , "GNUMakefile" , "gmake") \ - X(0 , "BSDMakefile" , "bmake") \ - X(2 , "pro" , "qmake") \ - X(0 , "make" , "sh" , "make") \ - X(0 , "build.sh" , "sh" , "build.sh") \ - X(0 , "build.ninja" , "ninja") \ - X(0 , "OMakefile" , "omake") \ - X(0 , "configure" , "sh" , "configure") \ - X(0 , "configure.ac" , "autoreconf" , "-fiv") \ - X(0 , "CMakeLists.txt" , "cmake" , "-B" , "out/") \ - X(0 , "BUILD.gn" , "gn" , "gen" , "out/") \ - X(0 , "nob" , "./nob") \ - X(0 , "nob.c" , "cc" , "./nob.c" , "-o" , "nob") \ - X(0 , "nobuild" , "./nobuild" , ) \ - X(0 , "nobuild.c" , "cc" , "./nobuild.c" , "-o" , "nobuild") \ - X(0 , "package.json" , "npm" , "install") \ - X(1 , "node_modules" , "npm" , "run") \ - X(0 , "Cargo.toml" , "cargo" , "build") \ - X(0 , "setup.py" , "pip" , "install" , ".") \ - X(0 , "gradlew.bat" , "./gradlew.bat") \ - X(0 , "gradlew" , "sh" , "gradlew") \ - X(0 , "PKGBUILD" , "makepkg" , "-i") + X( 1, FMAKE_FIL, "Makefile" , "make") \ + X( 1, FMAKE_FIL, "makefile" , "make") \ + X( 1, FMAKE_FIL, "GNUMakefile" , "gmake") \ + X( 1, FMAKE_FIL, "BSDMakefile" , "bmake") \ + X( 1, FMAKE_FIL, "Justfile" , "just") \ + X( 1, FMAKE_EXT, "*.pro" , "qmake") \ + X( 1, FMAKE_EXT, "*.sln" , "msbuild") \ + X( 1, FMAKE_EXT, "*.vcxproj" , "msbuild") \ + X( 1, FMAKE_FIL, "make" , "sh" , "make") \ + X( 1, FMAKE_FIL, "Gruntfile" , "grunt") \ + X( 1, FMAKE_FIL, "build.sh" , "sh" , "build.sh") \ + X( 1, FMAKE_FIL, "build.ninja" , "ninja") \ + X( 1, FMAKE_FIL, "OMakefile" , "omake") \ + X( 2, FMAKE_FIL, "configure" , "sh" , "configure") \ + X( 1, FMAKE_FIL, "configure.ac" , "autoreconf" , "-fiv") \ + X( 2, FMAKE_FIL, "CMakeLists.txt" , "cmake" , "-B" , "out/") \ + X( 2, FMAKE_FIL, "BUILD.gn" , "gn" , "gen" , "out/") \ + X( 1, FMAKE_FIL, "BUILD" , "bazel" , "build") \ + X( 1, FMAKE_FIL, "nob" , "./nob") \ + X( 1, FMAKE_FIL, "nob.c" , "cc" , "./nob.c" , "-o" , "nob") \ + X( 1, FMAKE_FIL, "nobuild" , "./nobuild" , ) \ + X( 1, FMAKE_FIL, "nobuild.c" , "cc" , "./nobuild.c" , "-o" , "nobuild") \ + X( 1, FMAKE_FIL, "packages.json" , "npm" , "install") \ + X( 1, FMAKE_DIR, "node_modules" , "npm" , "run") \ + X( 1, FMAKE_FIL, "Cargo.toml" , "cargo" , "build") \ + X( 1, FMAKE_FIL, "setup.py" , "pip" , "install" , ".") \ + X( 1, FMAKE_FIL, "gradlew.bat" , "./gradlew.bat") \ + X( 1, FMAKE_FIL, "gradlew" , "sh" , "gradlew") \ + X( 3, FMAKE_FIL, "APKBUILD" , "abuild" , "-r") \ + X( 3, FMAKE_FIL, "PKGBUILD" , "makepkg" , "-i") -typedef struct { -file_type_t file_type; -const char* filename; -const char* cmd; -const char* args[256]; -} maker_config_t; static const maker_config_t makers[] = { -#define X(ISEXT, LOOKFOR, CMD, ...) {ISEXT, LOOKFOR, CMD, { CMD, __VA_ARGS__} }, +#define X(MLEVEL, ISEXT, LOOKFOR, CMD, ...) {MLEVEL, ISEXT, LOOKFOR, \ + CMD, { CMD, __VA_ARGS__}, (sizeof((char*[]){"" __VA_ARGS__}) / sizeof(char**)) }, BUILD_SYSTEMS #undef X +#undef NUMARGS }; +static const size_t makers_len = sizeof(makers) / sizeof(maker_config_t); diff --git a/config.mk b/config.mk @@ -1,4 +1,6 @@ -VERSION = 0.1.1 +VERSION = 0.1.6 -CPPFLAGS += -DFMAKE_VERSION=$(VERSION) -PREFIX ?= /usr/local +$(CC)_CFLAGS = -DFMAKE_VERSION=\"$(VERSION)\" -Wall -Wextra -g +cl_CFLAGS = /DFMAKE_VERSION=\"$(VERSION)\" /Zi + +PREFIX = /usr/local diff --git a/fmake.1 b/fmake.1 @@ -0,0 +1,17 @@ +.DD August 26, 2024 +.DT FMAKE 1 +.OS +.SH NAME +.NM fmake +.ND Build command wrapper +.SH SYNOPSIS +.NM +.OP -a +.NM +.OP -l +The +.NM +is a program that brings `make`s interface to almost any build system. +.EL +.SH AUTHORS +.AN Bharatvaj AQ MT bharatvaj@getsh.org diff --git a/fmake.c b/fmake.c @@ -1,29 +1,57 @@ +/* See LICENSE file for copyright and license details. */ #include <stdio.h> #include <stdlib.h> #include <string.h> +#ifdef _WIN32 +#include <windows.h> +#include <process.h> +#else #include <unistd.h> #include <dirent.h> +#endif + +typedef enum { + FMAKE_FIL = 0, + FMAKE_DIR = 1, + FMAKE_EXT = 2 +} filetype_t; + +typedef struct { + short level; + filetype_t filetype; + const char* filename; + const char* cmd; + const char* args[256]; + size_t args_len; +} maker_config_t; #include "config.h" static maker_config_t maker; -// TODO assigning NULL can be avoided -static DIR *dir = NULL; -static struct dirent *entry; -static short should_execute_commands = 0; + +static short should_execute_commands = 1; static short is_accepting_cmd_args = 0; +static short is_verbose = 0; +#ifdef _WIN32 +static HANDLE dir; +static WIN32_FIND_DATA entry; +#else +static DIR *dir; +static struct dirent *entry; +#endif #define info(...) \ if (should_execute_commands) { \ fprintf(stderr, __VA_ARGS__); \ } -int process_build(char* argv[]) { +int +process_build(int argc, char* argv[]) { int status = -1; info("++"); - for(size_t i = 0; i < sizeof(maker.args) && maker.args[i] != NULL; i++) { - fprintf(should_execute_commands? stderr : stdout , " %s", maker.args[i]); + for(size_t bs_i = 0; bs_i < maker.args_len && maker.args[bs_i] != NULL; bs_i++) { + fprintf(should_execute_commands? stderr : stdout, " %s", maker.args[bs_i]); } info("\n"); if (should_execute_commands) { @@ -35,23 +63,84 @@ int process_build(char* argv[]) { status = execvp(maker.cmd, (char* const*)maker.args); } if (status == -1) { - perror(maker.cmd); - //printf("Error: %d\n", status); + perror(maker.cmd); } } return status; } +inline int +is_file_present(short filetype, const char* name, size_t name_len) { + switch(filetype) { +#ifdef _WIN32 + case FMAKE_FIL: + case FMAKE_EXT: + dir = FindFirstFile(name, &entry); + if (dir == INVALID_HANDLE_VALUE) { + return -1; + } + goto FMAKE_FOUND_MATCH; + break; +#else + case FMAKE_FIL: + while ((entry = readdir(dir)) != NULL) { + if (entry->d_type == DT_REG || entry->d_type == DT_LNK) { + if (strcmp(entry->d_name, name) == 0) { + goto FMAKE_FOUND_MATCH; + } + } + } + break; + case FMAKE_EXT: + while ((entry = readdir(dir)) != NULL) { + if (entry->d_type == DT_REG || entry->d_type == DT_LNK) { + char* dot = strrchr(entry->d_name, '.'); + if (dot && strcmp(dot + 1, name) == 0) { + goto FMAKE_FOUND_MATCH; + } + } + } + break; +#endif + case FMAKE_DIR: + if (access(name, 0) == 0) { + goto FMAKE_FOUND_MATCH; + } + break; + } + return -1; +FMAKE_FOUND_MATCH: +#ifndef _WIN32 + if (dir) closedir(dir); +#endif + return 0; +} + +void +fmake_usage(int status) { + printf("Usage: fmake [options] [target] ...\n"); + printf("Options:\n"); + char* opt_array[] = { + "-?", "Prints fmake usage", + "-D", "Print various types of debugging information.", + "-N", "Don't actually run any build commands; just print them.", + "-V", "Print the version number of make and exit." + }; + for(int i = 0; i < sizeof(opt_array) / sizeof(char**); i += 2) { + printf(" %-27s %s\n", opt_array[i], opt_array[i + 1]); + } + exit(status); +} -int main(int argc, char* argv[]) { - int i = 0; - int makers_size = sizeof(makers) / sizeof(maker_config_t); - for(i = 1; i < argc; i++) { - if (!(argv[i][0] == '-' && argv[i][1] != '\0')) { - printf("Usage: fmake [-l]\n"); +int +main(int argc, char* argv[]) { + int bs_i = 0; + int makers_len = sizeof(makers) / sizeof(maker_config_t); + for(bs_i = 1; bs_i < argc; bs_i++) { + if (!(argv[bs_i][0] == '-' && argv[bs_i][1] != '\0')) { exit(-1); } - switch(argv[i][1]) { + switch(argv[bs_i][1]) { case '-': is_accepting_cmd_args = 1; goto FMAKE_AFTER_ARG_CHECK; @@ -59,45 +148,46 @@ int main(int argc, char* argv[]) { case 'l': // TODO show better listing /* list supported build systems */ - for (size_t i = 0; i < (sizeof(makers) / sizeof(maker_config_t)); i++) { - printf("%s\n", makers[i].cmd); + for (size_t bs_i = 0; bs_i < makers_len; bs_i++) { + maker = makers[bs_i]; + printf("%-5d %-5d %-25s %-20s", maker.level, maker.filetype, maker.filename, maker.cmd); + for(size_t i = 1; i < maker.args_len; i++) printf("%s ", maker.args[i]); + printf("\n"); } break; - case 'a': - should_execute_commands = 1; + case 'N': + should_execute_commands = 0; + break; + case 'V': + printf("fmake " FMAKE_VERSION); + return 0; + break; + case 'D': + is_verbose=1; + break; + case '?': + fmake_usage(0); break; } } FMAKE_AFTER_ARG_CHECK: - argc = i; - argv = argv + i; - for (i = 0; i < makers_size; i++) { - if (makers[i].file_type == FMAKE_EXT) { - // TODO optimize this code - // FIXME it's not sure if the '.' is found from first or last - // last should be preferred - dir = opendir("./"); - if (dir == NULL) { - perror("fmake"); - return -1; - } - while ((entry = readdir(dir)) != NULL) { - if (entry->d_type == DT_REG) { - char* dot = strrchr(entry->d_name, '.'); - if (dot && strcmp(dot + 1, makers[i].filename) == 0) { - goto FMAKE_FOUND_MATCH; - } - } - } - } else { - const char* filename = makers[i].filename; - if (access(filename, F_OK) == 0) { - goto FMAKE_FOUND_MATCH; - } + argc = argc - bs_i; + argv = argv + bs_i; +#ifndef _WIN32 + dir = opendir("./"); + if (dir == NULL) { + perror("fmake"); + return -1; + } +#endif + for (bs_i = 0; bs_i < makers_len; bs_i++) { + const char *str = makers[bs_i].filename; + if (is_file_present(makers[bs_i].filetype, str, sizeof(makers[bs_i].filename)) == 0) { + break; + } else if (is_verbose) { + fprintf(stderr, "fmake: %s (%d)\n", makers[bs_i].filename, makers[bs_i].filetype); } } -FMAKE_FOUND_MATCH: - if (dir) closedir(dir); - maker = makers[i == makers_size? 0 : i]; - return process_build(argv); + maker = makers[bs_i == makers_len? 0 : bs_i]; + return process_build(argc, argv); }