diff options
| -rw-r--r-- | Makefile | 10 | ||||
| -rw-r--r-- | README.md | 63 | ||||
| -rw-r--r-- | mkproj.1 | 248 | ||||
| -rw-r--r-- | mkproj.c | 1120 | ||||
| -rwxr-xr-x | tests.sh | 127 |
5 files changed, 1301 insertions, 267 deletions
@@ -31,6 +31,9 @@ SRC := mkproj.c PREFIX ?= /usr/local bindir := $(DESTDIR)$(PREFIX)/bin mandir := $(DESTDIR)$(PREFIX)/share/man/man1 +bashdir := $(DESTDIR)$(PREFIX)/share/bash-completion/completions +zshdir := $(DESTDIR)$(PREFIX)/share/zsh/site-functions +fishdir := $(DESTDIR)$(PREFIX)/share/fish/vendor_completions.d all: $(TARGET) @@ -55,9 +58,16 @@ install: mkproj.1 install -m 755 $(TARGET) $(bindir)/$(TARGET) install -d $(mandir) install -m 644 mkproj.1 $(mandir)/mkproj.1 + install -d $(bashdir) $(zshdir) $(fishdir) + ./$(TARGET) --completions bash > $(bashdir)/mkproj + ./$(TARGET) --completions zsh > $(zshdir)/_mkproj + ./$(TARGET) --completions fish > $(fishdir)/mkproj.fish uninstall: rm -f $(bindir)/$(TARGET) rm -f $(mandir)/mkproj.1 + rm -f $(bashdir)/mkproj + rm -f $(zshdir)/_mkproj + rm -f $(fishdir)/mkproj.fish .PHONY: all check test clean install uninstall @@ -3,37 +3,53 @@ just a dumb bullshit utility that generates build system boilerplate and gets th like, imagine you want to start a new project. maybe it's something scalable, maybe it's just some stupid scratch program to test an idea. either way, before you've written a single line of the thing you actually want to make, you have to pick a build system, because project management in c is fucking cancer. then you have to go out of your way to write a `Makefile`, or a `CMakeLists.txt`, or whatever other configuration file your build system decided it needed, instead of just actually writing whatever the fuck it is you wanted to write. -`mkproj` fixes that. it makes a directory, generates the boilerplate for the build system you picked — currently `make`, `cmake`, `meson`, and `premake` — and that's it. you get a project you can immediately build, and you never use `mkproj` again. +`mkproj` fixes that. it makes a directory (or uses the one you're in, with `mkproj .`), generates the boilerplate for the build system you picked — currently `make`, `cmake`, `meson`, `premake`, and `xmake` — and that's it. you get a project you can immediately build, and you never use `mkproj` again. -there are a couple of things you can customize: whether it initializes a git repository, the language (`c`, `cpp`, or, god forbid, `objc`), the language standard, where your build output goes, and whether you want a separate intermediate directory for object files. but most of the time, you shouldn't have to think about any of that. +by default you get a flat c project built with `make`: the source sits at the root, the binary builds to the root, the object files go under `bin/`, and it uses the newest language standard. there are a couple of things you can customize: whether it initializes a git repository, the language (`c`, `cpp`, `objc`, `nasm`, `swift`, or `crust` — rust through `rustc`, no cargo), the language standard, where your sources live, where your build output goes, and whether you want the object files in their own directory. but most of the time, you shouldn't have to think about any of that. ```sh $ mkproj hello ``` -that's a c project using `make`. `cd hello && make` and you're off. +that's a c project using `make`. `cd hello && make run` and you're off — the generated makefiles have a `run` target that builds and runs the thing. ## usage ```text $ mkproj --help -mkproj 0.3.0 - dumb project generator +mkproj 0.4.0 - dumb project generator generates boilerplate for make, cmake, meson or premake so you can stop writing the same fucking build files every time you start a project. -usage: mkproj [-l lang] [-s std] [-b system] [--no-git] [--intermediate] - [--bin dir] [--bin-int dir] [--no-clangd] <name> +usage: mkproj [-l lang] [-s std] [-b system] [-k kind] [--cc cc] [--license name] + [--git] [--no-root] [--single] [--no-validation] [--docker] + [--style] [--ci] [--src dir] [--bin dir] [--bin-int dir] <name> + mkproj --completions bash|zsh|fish mkproj [-v | -h] +<name> is the project directory to create, or . to generate into the +current directory (the project name is taken from its basename). + +defaults: a flat C project built with make, binary at the project root, +the latest language standard, and objects under bin/. + options: - -l, --lang LANG language: c, cpp, objc (default c) - -s, --std STD language standard (default per language) - -b, --build SYSTEM build system: make, cmake, meson, premake (default make) - --no-git don't initialize a git repository - --intermediate use a separate intermediate object directory + -l, --lang LANG language: c, cpp, objc, nasm, swift, crust (default c) + -s, --std STD language standard (default: latest for the language) + -b, --build SYSTEM build system: make, cmake, meson, premake, xmake (default make) + -k, --kind KIND exe, lib (static) or shared (default exe) + --cc CC compiler (default clang/clang++); make, premake, xmake + --license NAME add a LICENSE: mit, isc, bsd-2, bsd-3, unlicense + --git initialize a git repository and write a .gitignore + --src DIR put sources in DIR/ instead of at the project root + --no-root put the binary in the build dir, not the project root + --single single-file project: compile directly, no object dir (make, c family) + --no-validation drop the make check target and the sanitizer flags + --docker add a Dockerfile to build in a container (c, cpp, nasm) + --style add .clang-format + .editorconfig from the style guide + --ci add a GitHub Actions workflow that builds the project --bin DIR build directory (default per build system) - --bin-int DIR separate object dir for make/premake; implies --intermediate - --no-clangd don't ignore clangd files in .gitignore + --bin-int DIR put objects in a separate dir (make/premake) -v, --version print version and bail -h, --help print this and bail @@ -42,10 +58,14 @@ by hachem <im@hachem.wtf> ```sh $ mkproj hello # C + make, the default +$ mkproj . # generate into the current directory $ mkproj -l cpp -s c++20 -b cmake app # C++20 + cmake $ mkproj -l objc -b meson gui # Objective-C + meson -$ mkproj -b make --bin-int obj tool # objects in obj/, binary in bin/ -$ mkproj --no-git scratch # don't bother with git +$ mkproj -l crust hello # Rust through rustc, no cargo +$ mkproj -k shared -b cmake libbar # a shared library +$ mkproj --src source tool # sources under source/ +$ mkproj -b make --bin-int obj tool # objects in obj/, binary at the root +$ mkproj --git tool # also init a git repo + .gitignore ``` ## build @@ -63,6 +83,19 @@ it really is one file, so if you don't want the makefile: $ cc -O2 mkproj.c -o mkproj ``` +## completions + +`make install` already drops bash, zsh, and fish completions into the standard +directories under `$PREFIX/share`, so a normal `sudo make install` just works. + +if you installed somewhere non-standard (e.g. `PREFIX=~/.local`), zsh may need that dir on its `fpath` — add `fpath=(~/.local/share/zsh/site-functions $fpath)` to your `.zshrc` before `compinit`. you can also generate a script by hand anytime: + +```sh +$ mkproj --completions bash > /etc/bash_completion.d/mkproj +$ mkproj --completions zsh > ~/.zsh/completions/_mkproj +$ mkproj --completions fish > ~/.config/fish/completions/mkproj.fish +``` + ## license `mkproj` is licensed under the mit license. see [license](LICENSE) for more. @@ -1,4 +1,4 @@ -.TH MKPROJ 1 "2026-09-18" "mkproj 0.3.0" "User Commands" +.TH MKPROJ 1 "2026-09-20" "mkproj 0.4.0" "User Commands" .SH NAME mkproj \- dumb project generator .SH SYNOPSIS @@ -6,6 +6,9 @@ mkproj \- dumb project generator .RI [ options ] .I name .br +.B mkproj \-\-completions +.BR bash | zsh | fish +.br .B mkproj .BR \-v | \-h .SH DESCRIPTION @@ -21,28 +24,61 @@ so you do not have to keep writing build files for small projects. .PP It creates a directory .I name -containing a -.B src -directory with a stub -.IR main -(or the stub at the root, with -.BR \-\-flat ), -a build file for the chosen build system, a +containing a stub +.I main +at the root and a build file for the chosen build system. With +.B \-\-git +it also writes a .B .gitignore -and, unless disabled, an initialized git repository. The stub compiles +and initializes a git repository. The stub compiles cleanly under a strict set of warnings with address and undefined sanitizers enabled by default. +.PP +If +.I name +is +.BR . , +the project is generated into the current directory instead of a new one, +and the project name is taken from that directory's basename. In this mode +.B mkproj +refuses to run if a build file already exists, so it will not clobber an +existing project. +.PP +By default the layout is flat (sources at the root), the executable is +built at the project root, object files go under +.BR bin , +and the newest language standard is used. Use +.BR \-\-src ", " \-\-no\-root +and +.B \-s +to change these. .SH OPTIONS .TP .BR \-l ", " \-\-lang " " \fIlang\fR Source language: -.BR c ", " cpp ", " objc " or " nasm . +.BR c ", " cpp ", " objc ", " nasm ", " swift " or " crust . Default .BR c . .B nasm -projects are x86\-64 assembly and only support the +projects are x86\-64 assembly. +.B swift +is compiled with +.BR swiftc , +and +.B crust +is Rust driven straight through +.B rustc +(no cargo), from the crate root +.RB ( main.rs ). +.B nasm ", " swift " and " crust +only support the .B make -build system (executables only, no language standard). +build system, build executables only, and take no language standard; +.B swift +and +.B crust +also have no +.BR \-\-style . .TP .BR \-s ", " \-\-std " " \fIstd\fR Language standard passed to the compiler as @@ -51,16 +87,16 @@ for example .B c17 or .BR c++23 . -Must be valid for the chosen language. Defaults to -.B c17 +Must be valid for the chosen language. Defaults to the newest standard for +the language +.RB ( c2y for .B c and .BR objc , -and -.B c++23 +.B c++26 for -.BR cpp . +.BR cpp ). .TP .BR \-b ", " \-\-build " " \fIsystem\fR Build system to generate: @@ -71,15 +107,24 @@ Default .BR \-k ", " \-\-kind " " \fIkind\fR What to build: .B exe -for an executable or +for an executable, .B lib -for a static library. A library gets a +for a static library +.RI ( lib < name >.a ), +or +.B shared +for a shared library +.RI ( lib < name >.so +on Linux, +.RI lib < name >.dylib +on macOS). Either library kind gets a .RI < name >.h / .RI < name >.c pair with a stub function instead of a -.IR main . -Default +.IR main , +and is built without sanitizers \(em those belong in the executable that +links the library. Default .BR exe . .TP .BR \-\-cc " " \fIcc\fR @@ -115,23 +160,18 @@ The copyright holder is taken from .BR $USER ). No license is written by default. .TP -.B \-\-no\-git -Do not initialize a git repository. -.TP -.B \-\-intermediate -Compile objects into a separate directory (see -.BR \-\-bin\-int ). -Only meaningful for -.B make -and -.BR premake . +.B \-\-git +Initialize a git repository and write a +.BR .gitignore . +By default neither is created. .TP -.B \-\-flat -Flat layout: put the sources at the project root instead of in a -.B src -directory. Objects still build into the +.BR \-\-src " " \fIdir\fR +Put the sources in +.I dir +instead of at the project root. By default the layout is flat, with sources +at the root; objects always build into the .B \-\-bin -directory, so the tree stays a single level deep. +directory. .TP .B \-\-docker Add a @@ -153,6 +193,67 @@ the container targets Linux so you write against Linux syscalls instead of the macOS assembly conventions the native build uses. .TP +.B \-\-no\-root +Put the built executable inside +.B \-\-bin +instead of at the project root. By default the executable is built at the +project root +.RI ( ./ name ), +with objects under +.BR \-\-bin . +Applies to +.B make +and +.BR premake ; +.BR cmake ", " meson " and " xmake +always keep the executable in their build directory. +.TP +.B \-\-single +Generate a simplified +.B Makefile +for a project whose sources compile in one step: the sources are built straight +into the executable with no object directory, and +.B clean +removes only the binary. There is no +.B \-\-bin +or +.BR \-\-bin\-int . +Only for +.B make +with +.BR c ", " cpp " or " objc , +and only for an executable. +.TP +.B \-\-no\-validation +Leave out the +.B check +target (which runs +.BR bear " and " cppcheck ) +and the address and undefined sanitizer flags from the generated +.BR Makefile . +.TP +.B \-\-style +Add a +.B .clang-format +and +.B .editorconfig +matching the house style guide +.RB ( hachem.wtf/blabbing/style-guide ). +Only for +.BR c ", " cpp " and " objc . +.TP +.B \-\-ci +Add a +.B .github/workflows/ci.yml +GitHub Actions workflow that builds the project on every push and pull +request. Runs on +.B macOS +for +.BR objc ", " swift " and " nasm , +and on +.B Linux +otherwise, installing whatever the runner does not already provide. +.TP .BR \-\-bin " " \fIdir\fR Build directory. The default depends on the build system: .B bin @@ -170,51 +271,58 @@ for The final executable is written here. .TP .BR \-\-bin\-int " " \fIdir\fR -Separate directory for intermediate object files. Implies -.BR \-\-intermediate , -and applies only to +Compile object files into their own directory +.I dir +instead of alongside the executable in +.BR \-\-bin . +Applies only to .B make and .BR premake . -Default -.BR bin\-int . -.TP -.B \-\-no\-clangd -Do not generate a -.B compile_flags.txt -for clangd, and do not add the clangd artifacts -.RB ( .clangd ", " compile_commands.json ) -to -.BR .gitignore . -By default a -.B compile_flags.txt -with the project's flags is written so an editor's language server works before -the first build. +By default objects and the executable share the +.B \-\-bin +directory. .TP .BR \-v ", " \-\-version Print version and exit. .TP .BR \-h ", " \-\-help Print a usage summary and exit. +.TP +.BR \-\-completions " " \fIshell\fR +Print a shell completion script for +.BR bash ", " zsh " or " fish +to standard output, then exit. Source it from the shell's startup file, or +install it where that shell looks for completions. .SH "BUILD SYSTEMS" .B make and .B premake -build objects and the executable into +build objects into .BR \-\-bin , -or split objects into +or into .B \-\-bin\-int -when -.B \-\-intermediate -is given. +when it is given, and place the executable at the project root unless +.B \-\-no\-root +is set. .BR cmake ", " meson " and " xmake use a single out\-of\-source build directory named by .BR \-\-bin , so -.B \-\-intermediate -and .B \-\-bin\-int -do not apply to them and are rejected if combined with either. +does not apply to them and is rejected if combined with either. +.PP +The generated +.B make +build files have a +.B run +target for executables, so +.B make run +builds and runs the program; +.B xmake +projects use +.B xmake run +instead. .SH EXAMPLES .TP .B mkproj hello @@ -227,14 +335,26 @@ A C++20 project built with cmake. A make project with objects compiled into .BR obj/ . .TP -.B mkproj \-\-flat quick -A flat project: the source sits at the root, no -.B src +.B mkproj \-\-src source quick +A project with its source under +.BR source/ , +rather than the default flat layout. +.TP +.B mkproj . +Generate the project into the current directory, naming it after that directory. .TP +.B mkproj \-l crust hello +A Rust project built straight through +.B rustc +with make, no cargo. +.TP .B mkproj \-k lib \-\-license mit libfoo A static-library project with an MIT .BR LICENSE . +.TP +.B mkproj \-k shared \-b cmake libbar +A shared-library project built with cmake. .SH ENVIRONMENT These set defaults that command-line options override: .BR MKPROJ_LANG ", " MKPROJ_STD ", " MKPROJ_BUILD ", " MKPROJ_CC ", " MKPROJ_KIND @@ -5,7 +5,7 @@ * stop writing the same fucking build files every time you start a project. * * Author: hachem <im@hachem.wtf> - * Version: 0.3.0 + * Version: 0.4.0 */ #include <ctype.h> @@ -21,7 +21,7 @@ #include <unistd.h> #define mkproj_program "mkproj" -#define mkproj_version "0.3.0" +#define mkproj_version "0.4.0" #define mkproj_tagline "dumb project generator" enum Language @@ -30,6 +30,8 @@ enum Language language_cpp, language_objc, language_nasm, + language_swift, + language_crust, language_count }; @@ -46,7 +48,8 @@ enum Build_System enum Kind { kind_exe, - kind_lib + kind_lib, + kind_shared }; enum License @@ -82,6 +85,8 @@ struct Build_Info struct Config { const char* name; + const char* dir; + const char* src_dir; enum Language language; const char* standard; const char* compiler; @@ -92,9 +97,13 @@ struct Config const char* bin_int_dir; int use_git; int use_intermediate; - int use_clangd; int use_flat; int use_docker; + int use_root; + int use_style; + int use_ci; + int use_single; + int use_validation; }; static const char* const c_standards[] = @@ -129,7 +138,7 @@ static const struct Language_Info languages[language_count] = .extension = "c", .standards = c_standards, .standard_count = sizeof c_standards / sizeof c_standards[0], - .default_standard = "c17", + .default_standard = "c2y", }, [language_cpp] = { @@ -139,7 +148,7 @@ static const struct Language_Info languages[language_count] = .extension = "cpp", .standards = cpp_standards, .standard_count = sizeof cpp_standards / sizeof cpp_standards[0], - .default_standard = "c++23", + .default_standard = "c++26", }, [language_objc] = { @@ -149,7 +158,7 @@ static const struct Language_Info languages[language_count] = .extension = "m", .standards = c_standards, .standard_count = sizeof c_standards / sizeof c_standards[0], - .default_standard = "c17", + .default_standard = "c2y", }, [language_nasm] = { @@ -161,8 +170,36 @@ static const struct Language_Info languages[language_count] = .standard_count = 0, .default_standard = NULL, }, + [language_swift] = + { + .key = "swift", + .name = "Swift", + .compiler = "swiftc", + .extension = "swift", + .standards = NULL, + .standard_count = 0, + .default_standard = NULL, + }, + [language_crust] = + { + .key = "crust", + .name = "Rust", + .compiler = "rustc", + .extension = "rs", + .standards = NULL, + .standard_count = 0, + .default_standard = NULL, + }, }; +/* the clang-driven languages: they share -std, the warning set, per-object + * make rules and clang-format. nasm, swift and crust each build their own way + * instead. */ +static int is_c_family(enum Language language) +{ + return language == language_c || language == language_cpp || language == language_objc; +} + static const struct Build_Info build_systems[build_system_count] = { [build_make] = @@ -223,30 +260,172 @@ static void print_usage(FILE* stream) "stop writing the same fucking build files every time you start a project.\n" "\n" "usage: %s [-l lang] [-s std] [-b system] [-k kind] [--cc cc] [--license name]\n" - " [--no-git] [--intermediate] [--flat] [--docker] [--bin dir]\n" - " [--bin-int dir] [--no-clangd] <name>\n" + " [--git] [--no-root] [--single] [--no-validation] [--docker]\n" + " [--style] [--ci] [--src dir] [--bin dir] [--bin-int dir] <name>\n" + " %s --completions bash|zsh|fish\n" " %s [-v | -h]\n" "\n" + "<name> is the project directory to create, or . to generate into the\n" + "current directory (the project name is taken from its basename).\n" + "\n" + "defaults: a flat C project built with make, binary at the project root,\n" + "the latest language standard, and objects under bin/.\n" + "\n" "options:\n" - " -l, --lang LANG language: c, cpp, objc, nasm (default c)\n" - " -s, --std STD language standard (default per language)\n" + " -l, --lang LANG language: c, cpp, objc, nasm, swift, crust (default c)\n" + " -s, --std STD language standard (default: latest for the language)\n" " -b, --build SYSTEM build system: make, cmake, meson, premake, xmake (default make)\n" " -k, --kind KIND exe or lib (default exe)\n" " --cc CC compiler (default clang/clang++); make, premake, xmake\n" " --license NAME add a LICENSE: mit, isc, bsd-2, bsd-3, unlicense\n" - " --no-git don't initialize a git repository\n" - " --intermediate use a separate intermediate object directory\n" - " --flat flat layout: sources at the root, no src/ directory\n" + " --git initialize a git repository and write a .gitignore\n" + " --src DIR put sources in DIR/ instead of at the project root\n" + " --no-root put the binary in the build dir, not the project root\n" + " --single single-file project: compile directly, no object dir (make, c family)\n" + " --no-validation drop the make check target and the sanitizer flags\n" " --docker add a Dockerfile to build in a container (c, cpp, nasm)\n" + " --style add .clang-format + .editorconfig from the style guide\n" + " --ci add a GitHub Actions workflow that builds the project\n" " --bin DIR build directory (default per build system)\n" - " --bin-int DIR separate object dir for make/premake; implies --intermediate\n" - " --no-clangd don't generate compile_flags.txt for clangd\n" + " --bin-int DIR put objects in a separate dir (make/premake)\n" " -v, --version print version and bail\n" " -h, --help print this and bail\n" "\n" "by hachem <im@hachem.wtf>\n", mkproj_program, mkproj_version, mkproj_tagline, - mkproj_program, mkproj_program); + mkproj_program, mkproj_program, mkproj_program); +} + +/* space-separated language / build-system / license keys, straight from the + * tables so the completions never drift from what the parser accepts. */ +static void collect_langs(char* buf, size_t size) +{ + size_t off = 0; + + for (enum Language l = 0; l < language_count; l++) + off += (size_t) snprintf(buf + off, size - off, "%s%s", off != 0 ? " " : "", languages[l].key); +} + +static void collect_builds(char* buf, size_t size) +{ + size_t off = 0; + + for (enum Build_System b = 0; b < build_system_count; b++) + off += (size_t) snprintf(buf + off, size - off, "%s%s", off != 0 ? " " : "", build_systems[b].key); +} + +static void collect_licenses(char* buf, size_t size) +{ + size_t off = 0; + + for (enum License l = license_mit; l < license_count; l++) + off += (size_t) snprintf(buf + off, size - off, "%s%s", off != 0 ? " " : "", licenses[l].key); +} + +/* the plain (no-argument) flags, shared by every shell's option list. */ +#define mkproj_flags \ + "-l --lang -s --std -b --build -k --kind --cc --license " \ + "--git --src --no-root --single --no-validation --docker --style --ci " \ + "--bin --bin-int -v --version -h --help" + +static void print_completions_bash(void) +{ + char langs[128], builds[128], lics[128]; + collect_langs(langs, sizeof langs); + collect_builds(builds, sizeof builds); + collect_licenses(lics, sizeof lics); + + (void) printf( + "_mkproj()\n" + "{\n" + " local cur prev\n" + " cur=\"${COMP_WORDS[COMP_CWORD]}\"\n" + " prev=\"${COMP_WORDS[COMP_CWORD-1]}\"\n" + "\n" + " case \"$prev\" in\n" + " -l|--lang) COMPREPLY=( $(compgen -W \"%s\" -- \"$cur\") ); return ;;\n" + " -b|--build) COMPREPLY=( $(compgen -W \"%s\" -- \"$cur\") ); return ;;\n" + " -k|--kind) COMPREPLY=( $(compgen -W \"exe lib\" -- \"$cur\") ); return ;;\n" + " --license) COMPREPLY=( $(compgen -W \"%s\" -- \"$cur\") ); return ;;\n" + " --completions) COMPREPLY=( $(compgen -W \"bash zsh fish\" -- \"$cur\") ); return ;;\n" + " --cc|--bin|--bin-int|--src|-s|--std) return ;;\n" + " esac\n" + "\n" + " if [[ \"$cur\" == -* ]]; then\n" + " COMPREPLY=( $(compgen -W \"%s\" -- \"$cur\") )\n" + " else\n" + " COMPREPLY=( $(compgen -f -- \"$cur\") )\n" + " fi\n" + "}\n" + "complete -F _mkproj mkproj\n", + langs, builds, lics, mkproj_flags); +} + +static void print_completions_zsh(void) +{ + char langs[128], builds[128], lics[128]; + collect_langs(langs, sizeof langs); + collect_builds(builds, sizeof builds); + collect_licenses(lics, sizeof lics); + + (void) printf( + "#compdef mkproj\n" + "\n" + "_mkproj()\n" + "{\n" + " _arguments -s \\\n" + " '(-l --lang)'{-l,--lang}'[source language]:language:(%s)' \\\n" + " '(-s --std)'{-s,--std}'[language standard]:standard:' \\\n" + " '(-b --build)'{-b,--build}'[build system]:system:(%s)' \\\n" + " '(-k --kind)'{-k,--kind}'[exe or lib]:kind:(exe lib)' \\\n" + " '--cc[compiler]:compiler:' \\\n" + " '--license[add a LICENSE]:license:(%s)' \\\n" + " '--src[source directory]:dir:_files -/' \\\n" + " '--bin[build directory]:dir:_files -/' \\\n" + " '--bin-int[object directory]:dir:_files -/' \\\n" + " '--git[init a git repo and write .gitignore]' \\\n" + " '--no-root[binary in the build dir]' \\\n" + " '--single[single-file project, no object dir]' \\\n" + " '--no-validation[no check target or sanitizers]' \\\n" + " '--docker[add a Dockerfile]' \\\n" + " '--style[add clang-format + editorconfig]' \\\n" + " '--ci[add a GitHub Actions workflow]' \\\n" + " '(-v --version)'{-v,--version}'[print version]' \\\n" + " '(-h --help)'{-h,--help}'[print help]' \\\n" + " '1:name:'\n" + "}\n" + "\n" + "_mkproj \"$@\"\n", + langs, builds, lics); +} + +static void print_completions_fish(void) +{ + char langs[128], builds[128], lics[128]; + collect_langs(langs, sizeof langs); + collect_builds(builds, sizeof builds); + collect_licenses(lics, sizeof lics); + + (void) printf( + "complete -c mkproj -s l -l lang -x -a '%s' -d 'source language'\n" + "complete -c mkproj -s s -l std -x -d 'language standard'\n" + "complete -c mkproj -s b -l build -x -a '%s' -d 'build system'\n" + "complete -c mkproj -s k -l kind -x -a 'exe lib' -d 'exe or lib'\n" + "complete -c mkproj -l cc -x -d 'compiler'\n" + "complete -c mkproj -l license -x -a '%s' -d 'add a LICENSE'\n" + "complete -c mkproj -l src -x -a '(__fish_complete_directories)' -d 'source directory'\n" + "complete -c mkproj -l bin -x -a '(__fish_complete_directories)' -d 'build directory'\n" + "complete -c mkproj -l bin-int -x -a '(__fish_complete_directories)' -d 'object directory'\n" + "complete -c mkproj -l git -d 'init a git repo and write .gitignore'\n" + "complete -c mkproj -l no-root -d 'binary in the build dir'\n" + "complete -c mkproj -l single -d 'single-file project, no object dir'\n" + "complete -c mkproj -l no-validation -d 'no check target or sanitizers'\n" + "complete -c mkproj -l docker -d 'add a Dockerfile'\n" + "complete -c mkproj -l style -d 'add clang-format + editorconfig'\n" + "complete -c mkproj -l ci -d 'add a GitHub Actions workflow'\n" + "complete -c mkproj -s v -l version -d 'print version'\n" + "complete -c mkproj -s h -l help -d 'print help'\n", + langs, builds, lics); } static int find_language(const char* key, enum Language* out) @@ -373,6 +552,36 @@ static const char* toolset_of(const char* compiler) return "clang"; } +/* the build dir ends up in a "rm -rf" clean rule, so it must stay inside the + * project: reject empty, absolute and ~ paths, any .. component, and paths + * that resolve to the project root itself (., ./, ./., ...). */ +static int bin_dir_safe(const char* dir) +{ + if (dir[0] == '\0' || dir[0] == '/' || dir[0] == '~') + return 0; + + int has_real = 0; + + for (const char* p = dir; *p != '\0'; ) + { + const char* start = p; + while (*p != '\0' && *p != '/') + p++; + + size_t len = (size_t) (p - start); + + if (len == 2 && start[0] == '.' && start[1] == '.') + return 0; + if (len != 0 && !(len == 1 && start[0] == '.')) + has_real = 1; + + if (*p == '/') + p++; + } + + return has_real; +} + /* * parse argv into config. returns 0 on success, a negative value when the * program should exit successfully (help/version), or a positive value on a @@ -388,6 +597,7 @@ static int parse_args(int argc, char** argv, struct Config* config) const char* license_arg = getenv("MKPROJ_LICENSE"); const char* bin_arg = NULL; const char* bin_int_arg = NULL; + const char* src_arg = NULL; const char* name = NULL; for (int i = 1; i < argc; i++) @@ -406,33 +616,70 @@ static int parse_args(int argc, char** argv, struct Config* config) return -1; } - if (strcmp(arg, "--no-git") == 0) + if (strcmp(arg, "--completions") == 0) { - config->use_git = 0; + if (i + 1 >= argc) + { + error("missing argument for", arg); + return 1; + } + + const char* shell = argv[++i]; + + if (strcmp(shell, "bash") == 0) + print_completions_bash(); + else if (strcmp(shell, "zsh") == 0) + print_completions_zsh(); + else if (strcmp(shell, "fish") == 0) + print_completions_fish(); + else + { + error("unknown shell (want bash, zsh or fish)", shell); + return 1; + } + + return -1; + } + + if (strcmp(arg, "--git") == 0) + { + config->use_git = 1; continue; } - if (strcmp(arg, "--intermediate") == 0) + if (strcmp(arg, "--no-root") == 0) { - config->use_intermediate = 1; + config->use_root = 0; continue; } - if (strcmp(arg, "--no-clangd") == 0) + if (strcmp(arg, "--docker") == 0) { - config->use_clangd = 0; + config->use_docker = 1; continue; } - if (strcmp(arg, "--flat") == 0) + if (strcmp(arg, "--style") == 0) { - config->use_flat = 1; + config->use_style = 1; continue; } - if (strcmp(arg, "--docker") == 0) + if (strcmp(arg, "--ci") == 0) { - config->use_docker = 1; + config->use_ci = 1; + continue; + } + + if (strcmp(arg, "--single") == 0) + { + config->use_single = 1; + continue; + } + + if (strcmp(arg, "--no-validation") == 0) + { + config->use_validation = 0; continue; } @@ -495,6 +742,14 @@ static int parse_args(int argc, char** argv, struct Config* config) continue; } + matched = opt_value(arg, "--src", "--src", argc, argv, &i, &src_arg); + if (matched != 0) + { + if (matched < 0) { error("missing argument for", arg); return 1; } + config->use_flat = 0; + continue; + } + if (arg[0] == '-' && arg[1] != '\0') { error("unknown option", arg); @@ -516,6 +771,30 @@ static int parse_args(int argc, char** argv, struct Config* config) return 1; } + /* "." means generate into the current directory, taking the project name + * from its basename instead of creating a subdirectory. */ + static char here_name[PATH_MAX]; + + if (strcmp(name, ".") == 0) + { + char cwd[PATH_MAX]; + + if (getcwd(cwd, sizeof cwd) == NULL) + { + error("cannot determine the current directory", NULL); + return 1; + } + + const char* base = strrchr(cwd, '/'); + (void) snprintf(here_name, sizeof here_name, "%s", base != NULL ? base + 1 : cwd); + name = here_name; + config->dir = "."; + } + else + { + config->dir = name; + } + const char* name_error = validate_name(name); if (name_error != NULL) @@ -525,6 +804,7 @@ static int parse_args(int argc, char** argv, struct Config* config) } config->name = name; + config->src_dir = src_arg != NULL ? src_arg : "src"; if (lang_arg != NULL && !find_language(lang_arg, &config->language)) { @@ -538,11 +818,11 @@ static int parse_args(int argc, char** argv, struct Config* config) return 1; } - if (config->language == language_nasm) + if (!is_c_family(config->language)) { if (std_arg != NULL) { - error("nasm has no language standard", std_arg); + error("this language has no language standard", languages[config->language].name); return 1; } @@ -571,9 +851,11 @@ static int parse_args(int argc, char** argv, struct Config* config) config->kind = kind_exe; else if (strcmp(kind_arg, "lib") == 0) config->kind = kind_lib; + else if (strcmp(kind_arg, "shared") == 0) + config->kind = kind_shared; else { - error("unknown kind (want exe or lib)", kind_arg); + error("unknown kind (want exe, lib or shared)", kind_arg); return 1; } } @@ -588,19 +870,26 @@ static int parse_args(int argc, char** argv, struct Config* config) && config->build != build_make && config->build != build_premake) { - error("--intermediate/--bin-int only apply to make and premake", build_systems[config->build].key); + error("--bin-int only applies to make and premake", build_systems[config->build].key); return 1; } - if (config->language == language_nasm && config->build != build_make) + if (config->use_intermediate + && (config->language == language_swift || config->language == language_crust)) + { + error("--bin-int does not apply to", languages[config->language].name); + return 1; + } + + if (!is_c_family(config->language) && config->build != build_make) { - error("nasm projects only support make", build_systems[config->build].key); + error("this language only supports make", languages[config->language].name); return 1; } - if (config->language == language_nasm && config->kind == kind_lib) + if (!is_c_family(config->language) && config->kind != kind_exe) { - error("nasm projects only support executables", NULL); + error("this language only supports executables", languages[config->language].name); return 1; } @@ -622,6 +911,48 @@ static int parse_args(int argc, char** argv, struct Config* config) return 1; } + if (config->use_docker && config->kind != kind_exe) + { + error("--docker needs an executable, not a library", NULL); + return 1; + } + + if (config->use_style && !is_c_family(config->language)) + { + error("--style (clang-format) supports only c, cpp and objc", languages[config->language].name); + return 1; + } + + if (config->use_single && config->build != build_make) + { + error("--single only applies to make", build_systems[config->build].key); + return 1; + } + + if (config->use_single && !is_c_family(config->language)) + { + error("--single supports only c, cpp and objc", languages[config->language].name); + return 1; + } + + if (config->use_single && config->kind != kind_exe) + { + error("--single needs an executable, not a library", NULL); + return 1; + } + + if (bin_arg != NULL && !bin_dir_safe(bin_arg)) + { + error("--bin must be a path inside the project, not '.', '..' or absolute", bin_arg); + return 1; + } + + if (bin_int_arg != NULL && !bin_dir_safe(bin_int_arg)) + { + error("--bin-int must be a path inside the project, not '.', '..' or absolute", bin_int_arg); + return 1; + } + config->bin_dir = bin_arg != NULL ? bin_arg : build_systems[config->build].default_bin; config->bin_int_dir = bin_int_arg != NULL ? bin_int_arg : build_systems[config->build].default_bin_int; @@ -718,14 +1049,25 @@ static const char* main_source(const struct Config* config) "msg: db \"type shi\", 10, 0\n"; } + if (config->language == language_swift) + return "print(\"type shi\")\n"; + + if (config->language == language_crust) + return + "fn main()\n" + "{\n" + " println!(\"type shi\");\n" + "}\n"; + if (config->language == language_objc) return "#import <Foundation/Foundation.h>\n" "\n" "int main(void)\n" "{\n" - " @autoreleasepool {\n" - " (void) NSLog(@\"type shi\");\n" + " @autoreleasepool\n" + " {\n" + " (void)NSLog(@\"type shi\");\n" " }\n" "}\n"; @@ -765,7 +1107,7 @@ static const char* main_source(const struct Config* config) "\n" "int main(void)\n" "{\n" - " (void) puts(\"type shi\");\n" + " (void)puts(\"type shi\");\n" "}\n"; } @@ -835,21 +1177,6 @@ static size_t base_flags(const struct Config* config, const char* flags[]) return count; } -/* clangd reads compile_flags.txt (one flag per line) to know how to parse the - * project, so the editor works before any build has run. */ -static void write_compile_flags(FILE* out, const struct Config* config) -{ - const char* warnings[10]; - size_t count = base_warnings(config, warnings); - - (void) fprintf(out, "-std=%s\n", config->standard); - - for (size_t i = 0; i < count; i++) - (void) fprintf(out, "%s\n", warnings[i]); - - (void) fprintf(out, "%s\n", config->use_flat ? "-I." : "-Isrc"); -} - static const char* cmake_language(enum Language language) { if (language == language_cpp) @@ -867,13 +1194,25 @@ static void write_makefile_nasm(FILE* out, const struct Config* config) { const char* obj_dir = config->use_intermediate ? config->bin_int_dir : config->bin_dir; - char clean[2 * PATH_MAX + 16]; + char clean[3 * PATH_MAX + 16]; if (config->use_intermediate) (void) snprintf(clean, sizeof clean, "rm -rf %s %s", config->bin_dir, config->bin_int_dir); else (void) snprintf(clean, sizeof clean, "rm -rf %s", config->bin_dir); + if (config->use_root) + { + size_t n = strlen(clean); + (void) snprintf(clean + n, sizeof clean - n, " %s", config->name); + } + + char target[2 * PATH_MAX]; + if (config->use_root) + (void) snprintf(target, sizeof target, "%s", config->name); + else + (void) snprintf(target, sizeof target, "%s/%s", config->bin_dir, config->name); + const char* format = config->use_docker ? "elf64" : "macho64"; const char* ld = config->use_docker ? "ld" : "cc"; const char* ldflags = config->use_docker ? "" : "-arch x86_64"; @@ -892,13 +1231,14 @@ static void write_makefile_nasm(FILE* out, const struct Config* config) "ASFLAGS += -g -F dwarf\n" "endif\n" "\n" - "TARGET := %s/%s\n", - ld, format, ldflags, config->bin_dir, config->name); + "TARGET := %s\n", + ld, format, ldflags, target); if (config->use_flat) (void) fprintf(out, "SRC := $(wildcard *.asm)\nOBJ := $(SRC:%%=%s/%%.o)\n", obj_dir); else - (void) fprintf(out, "SRC := $(shell find src -name '*.asm')\nOBJ := $(SRC:src/%%=%s/%%.o)\n", obj_dir); + (void) fprintf(out, "SRC := $(shell find %s -name '*.asm')\nOBJ := $(SRC:%s/%%=%s/%%.o)\n", + config->src_dir, config->src_dir, obj_dir); (void) fprintf( out, @@ -913,17 +1253,151 @@ static void write_makefile_nasm(FILE* out, const struct Config* config) if (config->use_flat) (void) fprintf(out, "%s/%%.asm.o: %%.asm\n", obj_dir); else - (void) fprintf(out, "%s/%%.asm.o: src/%%.asm\n", obj_dir); + (void) fprintf(out, "%s/%%.asm.o: %s/%%.asm\n", obj_dir, config->src_dir); (void) fprintf( out, "\t@mkdir -p $(@D)\n" "\t$(AS) $(ASFLAGS) $< -o $@\n" "\n" + "run: $(TARGET)\n" + "\t./$(TARGET)\n" + "\n" + "clean:\n" + "\t%s\n" + "\n" + ".PHONY: all run clean\n", + clean); +} + +/* swift compiles the whole module in one shot: no per-file objects, no -std, + * no warning flags. asan is available through swiftc's own -sanitize. */ +static void write_makefile_swift(FILE* out, const struct Config* config) +{ + char target[2 * PATH_MAX]; + if (config->use_root) + (void) snprintf(target, sizeof target, "%s", config->name); + else + (void) snprintf(target, sizeof target, "%s/%s", config->bin_dir, config->name); + + char clean[2 * PATH_MAX + 16]; + if (config->use_root) + (void) snprintf(clean, sizeof clean, "rm -rf %s %s", config->bin_dir, config->name); + else + (void) snprintf(clean, sizeof clean, "rm -rf %s", config->bin_dir); + + (void) fprintf(out, "SWIFTC := %s\n\nDEBUG ?= 1\n", config->compiler); + + if (config->use_validation) + (void) fprintf(out, "SANITIZE ?= 1\n"); + + (void) fprintf( + out, + "\n" + "SWIFTFLAGS :=\n" + "\n" + "ifeq ($(DEBUG),1)\n" + "SWIFTFLAGS += -g -Onone\n" + "else\n" + "SWIFTFLAGS += -O\n" + "endif\n"); + + if (config->use_validation) + (void) fprintf( + out, + "\n" + "ifeq ($(SANITIZE),1)\n" + "SWIFTFLAGS += -sanitize=address\n" + "endif\n"); + + (void) fprintf(out, "\nTARGET := %s\n", target); + + if (config->use_flat) + (void) fprintf(out, "SRC := $(wildcard *.swift)\n"); + else + (void) fprintf(out, "SRC := $(shell find %s -name '*.swift')\n", config->src_dir); + + (void) fprintf( + out, + "\n" + "all: $(TARGET)\n" + "\n" + "$(TARGET): $(SRC)\n" + "\t@mkdir -p $(@D)\n" + "\t$(SWIFTC) $(SWIFTFLAGS) $^ -o $@\n" + "\n" + "run: $(TARGET)\n" + "\t./$(TARGET)\n" + "\n" "clean:\n" "\t%s\n" "\n" - ".PHONY: all clean\n", + ".PHONY: all run clean\n", + clean); +} + +/* crust is rust driven straight through rustc from the crate root, no cargo. + * rustc pulls in the other modules itself, so only the crate root is compiled; + * $(SRC) exists just to retrigger the build when any source changes. */ +static void write_makefile_crust(FILE* out, const struct Config* config) +{ + char target[2 * PATH_MAX]; + if (config->use_root) + (void) snprintf(target, sizeof target, "%s", config->name); + else + (void) snprintf(target, sizeof target, "%s/%s", config->bin_dir, config->name); + + char clean[2 * PATH_MAX + 16]; + if (config->use_root) + (void) snprintf(clean, sizeof clean, "rm -rf %s %s", config->bin_dir, config->name); + else + (void) snprintf(clean, sizeof clean, "rm -rf %s", config->bin_dir); + + char crate[PATH_MAX]; + if (config->use_flat) + (void) snprintf(crate, sizeof crate, "main.rs"); + else + (void) snprintf(crate, sizeof crate, "%s/main.rs", config->src_dir); + + (void) fprintf( + out, + "RUSTC := %s\n" + "\n" + "DEBUG ?= 1\n" + "\n" + "RUSTFLAGS :=\n" + "\n" + "ifeq ($(DEBUG),1)\n" + "RUSTFLAGS += -g\n" + "else\n" + "RUSTFLAGS += -O\n" + "endif\n" + "\n" + "TARGET := %s\n" + "CRATE := %s\n", + config->compiler, target, crate); + + if (config->use_flat) + (void) fprintf(out, "SRC := $(wildcard *.rs)\n"); + else + (void) fprintf(out, "SRC := $(shell find %s -name '*.rs')\n", config->src_dir); + + (void) fprintf( + out, + "\n" + "all: $(TARGET)\n" + "\n" + "$(TARGET): $(SRC)\n" + "\t@mkdir -p $(@D)\n" + "\t$(RUSTC) $(RUSTFLAGS) $(CRATE) -o $@\n" + "\n" + "run: $(TARGET)\n" + "\t./$(TARGET)\n" + "\n" + "clean:\n" + "\t%s\n" + "\n" + ".PHONY: all run clean\n", clean); } @@ -935,23 +1409,60 @@ static void write_makefile(FILE* out, const struct Config* config) return; } + if (config->language == language_swift) + { + write_makefile_swift(out, config); + return; + } + + if (config->language == language_crust) + { + write_makefile_crust(out, config); + return; + } + const struct Language_Info* lang = &languages[config->language]; const char* ext = lang->extension; const char* obj_dir = config->use_intermediate ? config->bin_int_dir : config->bin_dir; - char clean[2 * PATH_MAX + 16]; + char target_name[PATH_MAX]; + if (config->kind == kind_lib) + (void) snprintf(target_name, sizeof target_name, "lib%s.a", config->name); + else if (config->kind == kind_shared) + (void) snprintf(target_name, sizeof target_name, "lib%s.$(SOEXT)", config->name); + else + (void) snprintf(target_name, sizeof target_name, "%s", config->name); - if (config->use_intermediate) + char target[2 * PATH_MAX]; + if (config->use_single || config->use_root) + (void) snprintf(target, sizeof target, "%s", target_name); + else + (void) snprintf(target, sizeof target, "%s/%s", config->bin_dir, target_name); + + char clean[3 * PATH_MAX + 16]; + + if (config->use_single) + (void) snprintf(clean, sizeof clean, "rm -f %s", target_name); + else if (config->use_intermediate) (void) snprintf(clean, sizeof clean, "rm -rf %s %s", config->bin_dir, config->bin_int_dir); else (void) snprintf(clean, sizeof clean, "rm -rf %s", config->bin_dir); + if (!config->use_single && config->use_root) + { + size_t n = strlen(clean); + (void) snprintf(clean + n, sizeof clean - n, " %s", target_name); + } + const char* flags[16]; size_t count = 0; + char include[PATH_MAX + 2]; + (void) snprintf(include, sizeof include, "-I%s", config->use_flat ? "." : config->src_dir); + flags[count++] = "-std=$(STD)"; count += base_warnings(config, flags + count); - flags[count++] = config->use_flat ? "-I." : "-Isrc"; + flags[count++] = include; int width = 0; @@ -959,7 +1470,14 @@ static void write_makefile(FILE* out, const struct Config* config) if ((int) strlen(flags[i]) > width) width = (int) strlen(flags[i]); - (void) fprintf(out, "CC := %s\nSTD := %s\n\nDEBUG ?= 1\nSANITIZE ?= 1\n\nCFLAGS := ", config->compiler, config->standard); + int sanitize = config->use_validation && config->kind == kind_exe; + + (void) fprintf(out, "CC := %s\nSTD := %s\n\nDEBUG ?= 1\n", config->compiler, config->standard); + + if (sanitize) + (void) fprintf(out, "SANITIZE ?= 1\n"); + + (void) fprintf(out, "\nCFLAGS := "); for (size_t i = 0; i < count; i++) { @@ -979,60 +1497,110 @@ static void write_makefile(FILE* out, const struct Config* config) "CFLAGS += -g -fno-omit-frame-pointer\n" "else\n" "CFLAGS += -O2\n" - "endif\n" - "\n" - "ifeq ($(SANITIZE),1)\n" - "CFLAGS += -fsanitize=address,undefined\n" - "LDFLAGS += -fsanitize=address,undefined\n" "endif\n"); + if (sanitize) + (void) fprintf( + out, + "\n" + "ifeq ($(SANITIZE),1)\n" + "CFLAGS += -fsanitize=address,undefined\n" + "LDFLAGS += -fsanitize=address,undefined\n" + "endif\n"); + if (config->language == language_objc && config->kind == kind_exe) (void) fprintf(out, "\nLDFLAGS += -framework Foundation\n"); - if (config->kind == kind_lib) - (void) fprintf(out, "\nTARGET := %s/lib%s.a\n", config->bin_dir, config->name); - else - (void) fprintf(out, "\nTARGET := %s/%s\n", config->bin_dir, config->name); + if (config->kind == kind_shared) + (void) fprintf( + out, + "\n" + "CFLAGS += -fPIC\n" + "\n" + "UNAME := $(shell uname)\n" + "ifeq ($(UNAME),Darwin)\n" + "SOEXT := dylib\n" + "SOFLAGS := -dynamiclib\n" + "else\n" + "SOEXT := so\n" + "SOFLAGS := -shared\n" + "endif\n"); - if (config->use_flat) - (void) fprintf(out, "SRC := $(wildcard *.%s)\nOBJ := $(SRC:%%=%s/%%.o)\n", ext, obj_dir); - else - (void) fprintf(out, "SRC := $(shell find src -name '*.%s')\nOBJ := $(SRC:src/%%=%s/%%.o)\n", ext, obj_dir); + (void) fprintf(out, "\nTARGET := %s\n", target); - (void) fprintf( - out, - "\n" - "all: $(TARGET)\n" - "\n" - "$(TARGET): $(OBJ)\n" - "\t@mkdir -p $(@D)\n"); + if (config->use_single) + { + if (config->use_flat) + (void) fprintf(out, "SRC := $(wildcard *.%s)\n", ext); + else + (void) fprintf(out, "SRC := $(shell find %s -name '*.%s')\n", config->src_dir, ext); - if (config->kind == kind_lib) - (void) fprintf(out, "\t$(AR) rcs $@ $^\n\n"); + (void) fprintf( + out, + "\n" + "all: $(TARGET)\n" + "\n" + "$(TARGET): $(SRC)\n" + "\t$(CC) $(CFLAGS) $(SRC) -o $@ $(LDFLAGS)\n" + "\n"); + } else - (void) fprintf(out, "\t$(CC) $^ -o $@ $(LDFLAGS)\n\n"); + { + if (config->use_flat) + (void) fprintf(out, "SRC := $(wildcard *.%s)\nOBJ := $(SRC:%%=%s/%%.o)\n", ext, obj_dir); + else + (void) fprintf(out, "SRC := $(shell find %s -name '*.%s')\nOBJ := $(SRC:%s/%%=%s/%%.o)\n", + config->src_dir, ext, config->src_dir, obj_dir); - if (config->use_flat) - (void) fprintf(out, "%s/%%.%s.o: %%.%s\n", obj_dir, ext, ext); - else - (void) fprintf(out, "%s/%%.%s.o: src/%%.%s\n", obj_dir, ext, ext); + (void) fprintf( + out, + "\n" + "all: $(TARGET)\n" + "\n" + "$(TARGET): $(OBJ)\n" + "\t@mkdir -p $(@D)\n"); + + if (config->kind == kind_lib) + (void) fprintf(out, "\t$(AR) rcs $@ $^\n\n"); + else if (config->kind == kind_shared) + (void) fprintf(out, "\t$(CC) $(SOFLAGS) $^ -o $@ $(LDFLAGS)\n\n"); + else + (void) fprintf(out, "\t$(CC) $^ -o $@ $(LDFLAGS)\n\n"); + + if (config->use_flat) + (void) fprintf(out, "%s/%%.%s.o: %%.%s\n", obj_dir, ext, ext); + else + (void) fprintf(out, "%s/%%.%s.o: %s/%%.%s\n", obj_dir, ext, config->src_dir, ext); + + (void) fprintf( + out, + "\t@mkdir -p $(@D)\n" + "\t$(CC) $(CFLAGS) -c $< -o $@\n" + "\n"); + } + + if (config->use_validation) + (void) fprintf( + out, + "check:\n" + "\tbear -- make clean all\n" + "\tcppcheck --enable=warning,style,performance,portability \\\n" + "\t\t --project=compile_commands.json \\\n" + "\t\t --error-exitcode=1\n" + "\n"); + + if (config->kind == kind_exe) + (void) fprintf(out, "run: $(TARGET)\n\t./$(TARGET)\n\n"); (void) fprintf( out, - "\t@mkdir -p $(@D)\n" - "\t$(CC) $(CFLAGS) -c $< -o $@\n" - "\n" - "check:\n" - "\tbear -- make clean all\n" - "\tcppcheck --enable=warning,style,performance,portability \\\n" - "\t\t --project=compile_commands.json \\\n" - "\t\t --error-exitcode=1\n" - "\n" "clean:\n" "\t%s\n" "\n" - ".PHONY: all check clean\n", - clean); + ".PHONY: all%s clean%s\n", + clean, + config->use_validation ? " check" : "", + config->kind == kind_exe ? " run" : ""); } static void write_cmake(FILE* out, const struct Config* config) @@ -1042,23 +1610,29 @@ static void write_cmake(FILE* out, const struct Config* config) const char* flags[10]; size_t count = base_flags(config, flags); + int sanitize = config->kind == kind_exe; + (void) fprintf( out, "cmake_minimum_required(VERSION 3.16)\n" "\n" "project(%s %s)\n" - "\n" - "option(SANITIZE \"build with asan + ubsan\" ON)\n" "\n", name, cmake_language(config->language)); + if (sanitize) + (void) fprintf(out, "option(SANITIZE \"build with asan + ubsan\" ON)\n\n"); + if (config->use_flat) (void) fprintf(out, "file(GLOB SOURCES CONFIGURE_DEPENDS *.%s)\n", ext); else - (void) fprintf(out, "file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS src/*.%s)\n", ext); + (void) fprintf(out, "file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS %s/*.%s)\n", + config->src_dir, ext); if (config->kind == kind_lib) (void) fprintf(out, "\nadd_library(%s STATIC ${SOURCES})\n", name); + else if (config->kind == kind_shared) + (void) fprintf(out, "\nadd_library(%s SHARED ${SOURCES})\n", name); else (void) fprintf(out, "\nadd_executable(%s ${SOURCES})\n", name); @@ -1068,7 +1642,7 @@ static void write_cmake(FILE* out, const struct Config* config) "target_include_directories(%s PRIVATE %s)\n" "\n" "target_compile_options(%s PRIVATE\n", - name, config->use_flat ? "." : "src", name); + name, config->use_flat ? "." : config->src_dir, name); for (size_t i = 0; i < count; i++) (void) fprintf(out, " %s\n", flags[i]); @@ -1082,14 +1656,15 @@ static void write_cmake(FILE* out, const struct Config* config) "target_link_libraries(%s PRIVATE \"-framework Foundation\")\n", name); - (void) fprintf( - out, - "\n" - "if(SANITIZE)\n" - " target_compile_options(%s PRIVATE -fsanitize=address,undefined)\n" - " target_link_options(%s PRIVATE -fsanitize=address,undefined)\n" - "endif()\n", - name, name); + if (sanitize) + (void) fprintf( + out, + "\n" + "if(SANITIZE)\n" + " target_compile_options(%s PRIVATE -fsanitize=address,undefined)\n" + " target_link_options(%s PRIVATE -fsanitize=address,undefined)\n" + "endif()\n", + name, name); } static const char* meson_language(enum Language language) @@ -1111,15 +1686,18 @@ static void write_meson(FILE* out, const struct Config* config) const char* flags[10]; size_t count = base_flags(config, flags); - (void) fprintf( - out, - "project('%s', '%s',\n" - " default_options: [\n" - " 'b_sanitize=address,undefined',\n" - " 'b_lundef=false',\n" - " ])\n" - "\n", - name, lang); + if (config->kind == kind_exe) + (void) fprintf( + out, + "project('%s', '%s',\n" + " default_options: [\n" + " 'b_sanitize=address,undefined',\n" + " 'b_lundef=false',\n" + " ])\n" + "\n", + name, lang); + else + (void) fprintf(out, "project('%s', '%s')\n\n", name, lang); if (config->use_flat) (void) fprintf( @@ -1133,10 +1711,10 @@ static void write_meson(FILE* out, const struct Config* config) (void) fprintf( out, "sources = run_command(\n" - " 'find', 'src', '-name', '*.%s',\n" + " 'find', '%s', '-name', '*.%s',\n" " check: true).stdout().strip().split('\\n')\n" "\n", - ext); + config->src_dir, ext); (void) fprintf(out, "add_project_arguments(\n"); @@ -1153,13 +1731,15 @@ static void write_meson(FILE* out, const struct Config* config) int link_foundation = config->language == language_objc && config->kind == kind_exe; if (!config->use_flat) - (void) fprintf(out, "inc = include_directories('src')\n\n"); + (void) fprintf(out, "inc = include_directories('%s')\n\n", config->src_dir); if (link_foundation) (void) fprintf(out, "foundation = dependency('appleframeworks', modules: ['Foundation'])\n\n"); if (config->kind == kind_lib) (void) fprintf(out, "static_library('%s', sources", name); + else if (config->kind == kind_shared) + (void) fprintf(out, "shared_library('%s', sources", name); else (void) fprintf(out, "executable('%s', sources", name); @@ -1188,6 +1768,10 @@ static void write_premake(FILE* out, const struct Config* config) const char* flags[10]; size_t count = base_flags(config, flags); + const char* pkind = config->kind == kind_lib ? "StaticLib" + : config->kind == kind_shared ? "SharedLib" + : "ConsoleApp"; + (void) fprintf( out, "workspace \"%s\"\n" @@ -1202,14 +1786,15 @@ static void write_premake(FILE* out, const struct Config* config) " targetname \"%s\"\n" " objdir \"%s\"\n" "\n", - name, name, config->kind == kind_lib ? "StaticLib" : "ConsoleApp", + name, name, pkind, premake_language(config->language), - toolset_of(config->compiler), config->bin_dir, name, obj_dir); + toolset_of(config->compiler), config->use_root ? "." : config->bin_dir, name, obj_dir); if (config->use_flat) (void) fprintf(out, " files { \"*.%s\" }\n includedirs { \".\" }\n", ext); else - (void) fprintf(out, " files { \"src/**.%s\" }\n includedirs { \"src\" }\n", ext); + (void) fprintf(out, " files { \"%s/**.%s\" }\n includedirs { \"%s\" }\n", + config->src_dir, ext, config->src_dir); (void) fprintf(out, "\n buildoptions {\n"); @@ -1260,25 +1845,29 @@ static void write_xmake(FILE* out, const struct Config* config) "target(\"%s\")\n" " set_kind(\"%s\")\n", config->bin_dir, toolset_of(config->compiler), name, - config->kind == kind_lib ? "static" : "binary"); + config->kind == kind_lib ? "static" : config->kind == kind_shared ? "shared" : "binary"); if (config->use_flat) (void) fprintf(out, " add_files(\"*.%s\")\n add_includedirs(\".\")\n", ext); else - (void) fprintf(out, " add_files(\"src/**.%s\")\n add_includedirs(\"src\")\n", ext); + (void) fprintf(out, " add_files(\"%s/**.%s\")\n add_includedirs(\"%s\")\n", + config->src_dir, ext, config->src_dir); (void) fprintf(out, "\n %s(\n", flag_fn); for (size_t i = 0; i < count; i++) (void) fprintf(out, " \"%s\",\n", flags[i]); - (void) fprintf( - out, - " \"-std=%s\",\n" - " \"-fsanitize=address,undefined\")\n" - "\n" - " add_ldflags(\"-fsanitize=address,undefined\")\n", - config->standard); + if (config->kind == kind_exe) + (void) fprintf( + out, + " \"-std=%s\",\n" + " \"-fsanitize=address,undefined\")\n" + "\n" + " add_ldflags(\"-fsanitize=address,undefined\")\n", + config->standard); + else + (void) fprintf(out, " \"-std=%s\")\n", config->standard); if (config->language == language_objc && config->kind == kind_exe) (void) fputs("\n add_frameworks(\"Foundation\")\n", out); @@ -1430,10 +2019,97 @@ static void write_license(FILE* out, const struct Config* config) } } +/* a minimal GitHub Actions workflow that builds the project. objc, swift and + * nasm run on macOS; everything else on Linux. build only: the C 'make check' + * needs bear and cppcheck, which we do not drag into CI. */ +static void write_ci(FILE* out, const struct Config* config) +{ + int apple = config->language == language_objc + || config->language == language_swift + || config->language == language_nasm; + const char* runner = apple ? "macos-latest" : "ubuntu-latest"; + + (void) fprintf( + out, + "name: ci\n" + "\n" + "on:\n" + " push:\n" + " pull_request:\n" + "\n" + "jobs:\n" + " build:\n" + " runs-on: %s\n" + " steps:\n" + " - uses: actions/checkout@v4\n", + runner); + + /* extra toolchain the runner does not already ship. */ + if (config->language == language_nasm) + (void) fputs(" - run: brew install nasm\n", out); + + switch (config->build) + { + case build_meson: + if (apple) + (void) fputs(" - run: brew install meson ninja\n", out); + else + (void) fputs(" - run: sudo apt-get update && sudo apt-get install -y meson ninja-build\n", out); + break; + + case build_premake: + (void) fputs(" - uses: abel0b/setup-premake@v2.4\n", out); + break; + + case build_xmake: + (void) fputs(" - uses: xmake-io/github-action-setup-xmake@v1\n", out); + break; + + default: + break; + } + + switch (config->build) + { + case build_cmake: + (void) fprintf(out, " - run: cmake -B %s && cmake --build %s\n", + config->bin_dir, config->bin_dir); + break; + + case build_meson: + (void) fprintf(out, " - run: meson setup %s && meson compile -C %s\n", + config->bin_dir, config->bin_dir); + break; + + case build_premake: + (void) fputs(" - run: premake5 gmake2 && make\n", out); + break; + + case build_xmake: + (void) fputs(" - run: xmake -y\n", out); + break; + + default: + (void) fputs(" - run: make\n", out); + break; + } +} + /* a single-stage Dockerfile that installs the toolchain, builds a release * (no sanitizers), and runs the binary. c/cpp with make, cmake or meson. */ static void write_dockerfile(FILE* out, const struct Config* config) { + /* make and premake honour --root and drop the binary at the project root; + * cmake and meson always keep it inside their build directory. */ + int at_root = config->use_root + && (config->build == build_make || config->build == build_premake); + char run_path[2 * PATH_MAX]; + + if (at_root) + (void) snprintf(run_path, sizeof run_path, "./%s", config->name); + else + (void) snprintf(run_path, sizeof run_path, "./%s/%s", config->bin_dir, config->name); + if (config->language == language_nasm) { (void) fprintf( @@ -1449,8 +2125,8 @@ static void write_dockerfile(FILE* out, const struct Config* config) "\n" "RUN make\n" "\n" - "CMD [\"./%s/%s\"]\n", - config->bin_dir, config->name); + "CMD [\"%s\"]\n", + run_path); return; } @@ -1498,8 +2174,8 @@ static void write_dockerfile(FILE* out, const struct Config* config) "\n" "RUN %s\n" "\n" - "CMD [\"./%s/%s\"]\n", - packages, build, config->bin_dir, config->name); + "CMD [\"%s\"]\n", + packages, build, run_path); } static void write_dockerignore(FILE* out, const struct Config* config) @@ -1510,11 +2186,63 @@ static void write_dockerignore(FILE* out, const struct Config* config) (void) fprintf(out, "%s/\n", config->bin_int_dir); } +/* clang-format matching the house style guide (hachem.wtf/blabbing/style-guide). */ +static void write_clang_format(FILE* out, const struct Config* config) +{ + (void) fprintf(out, "Language: %s\n", config->language == language_objc ? "ObjC" : "Cpp"); + + (void) fputs( + "BreakBeforeBraces: Allman\n" + "PointerAlignment: Left\n" + "AllowShortFunctionsOnASingleLine: None\n" + "AllowShortIfStatementsOnASingleLine: Never\n" + "AllowShortLoopsOnASingleLine: false\n" + "AllowShortBlocksOnASingleLine: Never\n" + "SpaceBeforeParens: ControlStatements\n" + "IndentCaseLabels: false\n" + "IndentWidth: 4\n" + "ContinuationIndentWidth: 8\n" + "ColumnLimit: 100\n" + "UseTab: Never\n" + "AlignConsecutiveDeclarations: false\n", + out); +} + +static void write_editorconfig(FILE* out, const struct Config* config) +{ + (void) config; + + (void) fputs( + "root = true\n" + "\n" + "[*]\n" + "charset = utf-8\n" + "end_of_line = lf\n" + "insert_final_newline = true\n" + "trim_trailing_whitespace = true\n" + "\n" + "[*.{c,h,cpp,hpp,cc,m}]\n" + "indent_style = space\n" + "indent_size = 4\n" + "max_line_length = 100\n", + out); +} + static void write_gitignore(FILE* out, const struct Config* config) { (void) fprintf(out, "%s/\n", config->bin_dir); - if (config->use_intermediate && + if (config->use_root) + { + if (config->kind == kind_lib) + (void) fprintf(out, "/lib%s.a\n", config->name); + else if (config->kind == kind_shared) + (void) fprintf(out, "/lib%s.so\n/lib%s.dylib\n", config->name, config->name); + else + (void) fprintf(out, "/%s\n", config->name); + } + + if (config->use_intermediate && (config->build == build_make || config->build == build_premake) && strcmp(config->bin_int_dir, config->bin_dir) != 0) @@ -1526,11 +2254,8 @@ static void write_gitignore(FILE* out, const struct Config* config) if (config->build == build_xmake) (void) fputs(".xmake/\n", out); - if (config->use_clangd && config->language != language_nasm) - (void) fputs("\n" - ".clangd\n" - "compile_commands.json\n", - out); + if (is_c_family(config->language)) + (void) fputs("\ncompile_commands.json\n", out); } /* open path, hand the stream to writer, close. returns 0 on success. */ @@ -1568,6 +2293,17 @@ static int make_dir(const char* path) return -1; } +/* like make_dir but fine with the directory already being there; for nested + * output dirs such as .github/workflows that we do not own outright. */ +static int ensure_dir(const char* path) +{ + if (mkdir(path, 0755) == 0 || errno == EEXIST) + return 0; + + error("cannot create directory", path); + return -1; +} + static void print_next_steps(const struct Config* config) { if (config->standard != NULL) @@ -1582,7 +2318,8 @@ static void print_next_steps(const struct Config* config) languages[config->language].name, build_systems[config->build].name); - (void) printf(" cd %s\n", config->name); + if (strcmp(config->dir, ".") != 0) + (void) printf(" cd %s\n", config->name); if (config->use_docker) { @@ -1596,7 +2333,9 @@ static void print_next_steps(const struct Config* config) { case build_make: (void) printf(" make\n"); - if (config->language != language_nasm) + if (config->kind == kind_exe) + (void) printf(" make run\n"); + else if (is_c_family(config->language)) (void) printf(" make check\n"); break; @@ -1645,24 +2384,33 @@ static int generate_project(const struct Config* config) } const char* ext = languages[config->language].extension; + const char* dir = config->dir; + int in_place = strcmp(dir, ".") == 0; char srcdir[PATH_MAX]; - if (make_dir(config->name) != 0) + /* generating in place: refuse to clobber an existing build file. */ + if (in_place && access(build_file, F_OK) == 0) + { + error("refusing to overwrite existing", build_file); + return -1; + } + + if (!in_place && make_dir(dir) != 0) return -1; if (config->use_flat) { - (void) snprintf(srcdir, sizeof srcdir, "%s", config->name); + (void) snprintf(srcdir, sizeof srcdir, "%s", dir); } else { - (void) snprintf(srcdir, sizeof srcdir, "%s/src", config->name); + (void) snprintf(srcdir, sizeof srcdir, "%s/%s", dir, config->src_dir); if (make_dir(srcdir) != 0) return -1; } - if (config->kind == kind_lib) + if (config->kind != kind_exe) { (void) snprintf(path, sizeof path, "%s/%s.h", srcdir, config->name); @@ -1682,27 +2430,22 @@ static int generate_project(const struct Config* config) return -1; } - (void) snprintf(path, sizeof path, "%s/%s", config->name, build_file); + (void) snprintf(path, sizeof path, "%s/%s", dir, build_file); if (emit(path, config, write_build) != 0) return -1; - (void) snprintf(path, sizeof path, "%s/.gitignore", config->name); - - if (emit(path, config, write_gitignore) != 0) - return -1; - - if (config->use_clangd && config->language != language_nasm) + if (config->use_git) { - (void) snprintf(path, sizeof path, "%s/compile_flags.txt", config->name); + (void) snprintf(path, sizeof path, "%s/.gitignore", dir); - if (emit(path, config, write_compile_flags) != 0) + if (emit(path, config, write_gitignore) != 0) return -1; } if (config->license != license_none) { - (void) snprintf(path, sizeof path, "%s/LICENSE", config->name); + (void) snprintf(path, sizeof path, "%s/LICENSE", dir); if (emit(path, config, write_license) != 0) return -1; @@ -1710,24 +2453,55 @@ static int generate_project(const struct Config* config) if (config->use_docker) { - (void) snprintf(path, sizeof path, "%s/Dockerfile", config->name); + (void) snprintf(path, sizeof path, "%s/Dockerfile", dir); if (emit(path, config, write_dockerfile) != 0) return -1; - (void) snprintf(path, sizeof path, "%s/.dockerignore", config->name); + (void) snprintf(path, sizeof path, "%s/.dockerignore", dir); if (emit(path, config, write_dockerignore) != 0) return -1; } - if (config->use_git && git_init(config->name) != 0) + if (config->use_style) + { + (void) snprintf(path, sizeof path, "%s/.clang-format", dir); + + if (emit(path, config, write_clang_format) != 0) + return -1; + + (void) snprintf(path, sizeof path, "%s/.editorconfig", dir); + + if (emit(path, config, write_editorconfig) != 0) + return -1; + } + + if (config->use_ci) + { + (void) snprintf(path, sizeof path, "%s/.github", dir); + + if (ensure_dir(path) != 0) + return -1; + + (void) snprintf(path, sizeof path, "%s/.github/workflows", dir); + + if (ensure_dir(path) != 0) + return -1; + + (void) snprintf(path, sizeof path, "%s/.github/workflows/ci.yml", dir); + + if (emit(path, config, write_ci) != 0) + return -1; + } + + if (config->use_git && git_init(dir) != 0) (void) fprintf(stderr, "%s: warning: git is not installed\n", mkproj_program); print_next_steps(config); { - char* const argv[] = { "tree", "-a", "--dirsfirst", "-I", ".git", (char*) config->name, NULL }; + char* const argv[] = { "tree", "-a", "--dirsfirst", "-I", ".git", (char*) dir, NULL }; if (run(argv, 0) == 127) (void) puts(" (tree is not installed)"); @@ -1741,6 +2515,8 @@ int main(int argc, char** argv) struct Config config = { .name = NULL, + .dir = NULL, + .src_dir = NULL, .language = language_c, .standard = NULL, .compiler = NULL, @@ -1749,11 +2525,15 @@ int main(int argc, char** argv) .license = license_none, .bin_dir = NULL, .bin_int_dir = NULL, - .use_git = 1, + .use_git = 0, .use_intermediate = 0, - .use_clangd = 1, - .use_flat = 0, + .use_flat = 1, + .use_single = 0, + .use_validation = 1, .use_docker = 0, + .use_root = 1, + .use_style = 0, + .use_ci = 0, }; int status = parse_args(argc, argv, &config); @@ -25,10 +25,10 @@ build_run() case $bs in make) ( cd "$dir" && make ) >/dev/null 2>&1 - "$dir/bin/$name" 2>&1 ;; + "$dir/$name" 2>&1 ;; premake) ( cd "$dir" && premake5 gmake2 && make ) >/dev/null 2>&1 - "$dir/bin/$name" 2>&1 ;; + "$dir/$name" 2>&1 ;; cmake) ( cd "$dir" && cmake -B build && cmake --build build ) >/dev/null 2>&1 "$dir/build/$name" 2>&1 ;; @@ -46,7 +46,7 @@ check() label=$1 bs=$2 name=$3 shift 3 - if ! ( cd "$work" && "$mkproj" --no-git -b "$bs" "$@" "$name" ) >/dev/null 2>&1 + if ! ( cd "$work" && "$mkproj" -b "$bs" "$@" "$name" ) >/dev/null 2>&1 then bad "$label (generate)" return @@ -77,7 +77,7 @@ check_lib() { bs=$1 name=$2 - if ! ( cd "$work" && "$mkproj" --no-git -b "$bs" -k lib "$name" ) >/dev/null 2>&1 + if ! ( cd "$work" && "$mkproj" -b "$bs" -k lib "$name" ) >/dev/null 2>&1 then bad "$bs / lib (generate)" return @@ -89,19 +89,41 @@ check_lib() fi } +check_shared() +{ + bs=$1 name=$2 + + if ! ( cd "$work" && "$mkproj" -b "$bs" -k shared "$name" ) >/dev/null 2>&1 + then + bad "$bs / shared (generate)" + return + fi + + lib_build "$bs" "$work/$name" + + if find "$work/$name" \( -name "lib$name.so" -o -name "lib$name.dylib" \) | grep -q . + then ok "$bs / shared" + else bad "$bs / shared (build)" + fi +} + echo "mkproj test harness ($mkproj)" echo echo "cli:" "$mkproj" --version >/dev/null 2>&1 && ok "--version" || bad "--version" "$mkproj" --help >/dev/null 2>&1 && ok "--help" || bad "--help" -( cd "$work" && "$mkproj" --no-git --lang=cpp eqform ) >/dev/null 2>&1 && - [ -f "$work/eqform/src/main.cpp" ] && ok "--lang=cpp (=form)" || bad "--lang=cpp (=form)" +"$mkproj" --completions bash | bash -n 2>/dev/null && ok "--completions bash" || bad "--completions bash" +"$mkproj" --completions zsh | zsh -n 2>/dev/null && ok "--completions zsh" || bad "--completions zsh" +"$mkproj" --completions fish >/dev/null 2>&1 && ok "--completions fish" || bad "--completions fish" +"$mkproj" --completions tcsh >/dev/null 2>&1 && bad "--completions rejects unknown" || ok "--completions rejects unknown" +( cd "$work" && "$mkproj" --lang=cpp eqform ) >/dev/null 2>&1 && + [ -f "$work/eqform/main.cpp" ] && ok "--lang=cpp (=form)" || bad "--lang=cpp (=form)" echo echo "name validation (these must be rejected):" for badname in "bad name" "a/b" ".." "" 'x") evil('; do - if ( cd "$work" && "$mkproj" --no-git "$badname" ) >/dev/null 2>&1 + if ( cd "$work" && "$mkproj" "$badname" ) >/dev/null 2>&1 then bad "rejects [$badname]" else ok "rejects [$badname]" fi @@ -118,32 +140,101 @@ for bs in make cmake meson premake xmake; do check "$bs / c" "$bs" "t_${bs}_c" check "$bs / cpp" "$bs" "t_${bs}_cpp" -l cpp - check "$bs / flat" "$bs" "t_${bs}_flat" --flat + check "$bs / src" "$bs" "t_${bs}_src" --src src [ "$(uname)" = Darwin ] && check "$bs / objc" "$bs" "t_${bs}_objc" -l objc check_lib "$bs" "t_${bs}_lib" + check_shared "$bs" "t${bs}sh" done if [ "$(uname)" = Darwin ] && have nasm; then - check "nasm / make" make t_nasm -l nasm + # native nasm is x86-64; it links here but only runs under Rosetta, so + # check the binary builds rather than requiring it to run. + ( cd "$work" && "$mkproj" -l nasm t_nasm ) >/dev/null 2>&1 && + ( cd "$work/t_nasm" && make ) >/dev/null 2>&1 && + [ -x "$work/t_nasm/t_nasm" ] && ok "nasm / make (build)" || bad "nasm / make (build)" else skip "nasm (needs macOS + nasm)" fi +if have swiftc; then + ( cd "$work" && "$mkproj" -l swift t_swift ) >/dev/null 2>&1 && + build_run make "$work/t_swift" t_swift | grep -q 'type shi' && + ok "swift / make" || bad "swift / make" +else + skip "swift (no swiftc)" +fi + +if have rustc; then + ( cd "$work" && "$mkproj" -l crust t_crust ) >/dev/null 2>&1 && + build_run make "$work/t_crust" t_crust | grep -q 'type shi' && + ok "crust / make" || bad "crust / make" +else + skip "crust (no rustc)" +fi + +# swift and crust are make-only, executables-only, no -std / clang-format +( cd "$work" && "$mkproj" -l swift -b cmake sc ) >/dev/null 2>&1 && bad "swift rejects cmake" || ok "swift rejects cmake" +( cd "$work" && "$mkproj" -l crust -k lib cl ) >/dev/null 2>&1 && bad "crust rejects lib" || ok "crust rejects lib" +( cd "$work" && "$mkproj" -l swift -k shared ssh ) >/dev/null 2>&1 && bad "swift rejects shared" || ok "swift rejects shared" +( cd "$work" && "$mkproj" --docker -k lib dl ) >/dev/null 2>&1 && bad "--docker rejects lib" || ok "--docker rejects lib" +( cd "$work" && "$mkproj" -l swift --style ss ) >/dev/null 2>&1 && bad "swift rejects --style" || ok "swift rejects --style" +( cd "$work" && "$mkproj" --bin . dotbin ) >/dev/null 2>&1 && bad "--bin . rejected" || ok "--bin . rejected" +( cd "$work" && "$mkproj" --bin .. dotdot ) >/dev/null 2>&1 && bad "--bin .. rejected" || ok "--bin .. rejected" +( cd "$work" && "$mkproj" --bin-int / slashint ) >/dev/null 2>&1 && bad "--bin-int / rejected" || ok "--bin-int / rejected" +( cd "$work" && "$mkproj" --single -b cmake scm ) >/dev/null 2>&1 && bad "--single rejects cmake" || ok "--single rejects cmake" +( cd "$work" && "$mkproj" --single -l swift sws ) >/dev/null 2>&1 && bad "--single rejects swift" || ok "--single rejects swift" +( cd "$work" && "$mkproj" --single -k lib slib ) >/dev/null 2>&1 && bad "--single rejects lib" || ok "--single rejects lib" + echo echo "generated files:" -( cd "$work" && "$mkproj" --no-git clangd_on ) >/dev/null 2>&1 -[ -f "$work/clangd_on/compile_flags.txt" ] && ok "compile_flags.txt generated" || bad "compile_flags.txt generated" -( cd "$work" && "$mkproj" --no-git --no-clangd clangd_off ) >/dev/null 2>&1 -[ -f "$work/clangd_off/compile_flags.txt" ] && bad "--no-clangd skips compile_flags.txt" || ok "--no-clangd skips compile_flags.txt" -( cd "$work" && "$mkproj" --no-git --license mit lic_mit ) >/dev/null 2>&1 +( cd "$work" && "$mkproj" --license mit lic_mit ) >/dev/null 2>&1 grep -q 'MIT License' "$work/lic_mit/LICENSE" 2>/dev/null && ok "--license mit" || bad "--license mit" -( cd "$work" && MKPROJ_BUILD=cmake "$mkproj" --no-git envdef ) >/dev/null 2>&1 +( cd "$work" && MKPROJ_BUILD=cmake "$mkproj" envdef ) >/dev/null 2>&1 [ -f "$work/envdef/CMakeLists.txt" ] && ok "MKPROJ_BUILD env default" || bad "MKPROJ_BUILD env default" -( cd "$work" && "$mkproj" --no-git --docker dok ) >/dev/null 2>&1 +( cd "$work" && "$mkproj" --docker dok ) >/dev/null 2>&1 [ -f "$work/dok/Dockerfile" ] && [ -f "$work/dok/.dockerignore" ] && ok "--docker files" || bad "--docker files" -( cd "$work" && "$mkproj" --no-git --docker -l nasm dok_nasm ) >/dev/null 2>&1 +( cd "$work" && "$mkproj" --docker -l nasm dok_nasm ) >/dev/null 2>&1 [ -f "$work/dok_nasm/Dockerfile" ] && grep -q 'elf64' "$work/dok_nasm/Makefile" && ok "--docker nasm targets elf64" || bad "--docker nasm targets elf64" -( cd "$work" && "$mkproj" --no-git --docker -l objc dok_objc ) >/dev/null 2>&1 && bad "--docker rejects objc" || ok "--docker rejects objc" +( cd "$work" && "$mkproj" --docker -l objc dok_objc ) >/dev/null 2>&1 && bad "--docker rejects objc" || ok "--docker rejects objc" +( cd "$work" && "$mkproj" rootbin ) >/dev/null 2>&1 && + ( cd "$work/rootbin" && make ) >/dev/null 2>&1 && + [ -x "$work/rootbin/rootbin" ] && ok "binary at root (default)" || bad "binary at root (default)" +( cd "$work" && "$mkproj" --no-root norootbin ) >/dev/null 2>&1 && + ( cd "$work/norootbin" && make ) >/dev/null 2>&1 && + [ -x "$work/norootbin/bin/norootbin" ] && ok "--no-root binary in bin/" || bad "--no-root binary in bin/" +( cd "$work" && "$mkproj" --git gitted ) >/dev/null 2>&1 +[ -d "$work/gitted/.git" ] && [ -f "$work/gitted/.gitignore" ] && ok "--git repo + .gitignore" || bad "--git repo + .gitignore" +[ -f "$work/rootbin/.gitignore" ] && bad "no git/.gitignore by default" || ok "no git/.gitignore by default" +( cd "$work" && "$mkproj" --single singlep ) >/dev/null 2>&1 && + ( cd "$work/singlep" && make ) >/dev/null 2>&1 && + [ -x "$work/singlep/singlep" ] && ok "--single builds" || bad "--single builds" +grep -q 'OBJ :=' "$work/singlep/Makefile" 2>/dev/null && bad "--single has no object dir" || ok "--single has no object dir" +grep -q 'rm -f singlep' "$work/singlep/Makefile" 2>/dev/null && ok "--single clean is rm -f binary" || bad "--single clean is rm -f binary" +( cd "$work" && "$mkproj" --no-validation noval ) >/dev/null 2>&1 +{ ! grep -q 'check:' "$work/noval/Makefile" && ! grep -q 'fsanitize' "$work/noval/Makefile"; } 2>/dev/null && ok "--no-validation drops check + sanitizers" || bad "--no-validation drops check + sanitizers" +( cd "$work" && "$mkproj" --style styled ) >/dev/null 2>&1 +[ -f "$work/styled/.clang-format" ] && [ -f "$work/styled/.editorconfig" ] && ok "--style files" || bad "--style files" +if have clang-format; then + ( cd "$work/styled" && clang-format --style=file --dry-run --Werror main.c ) >/dev/null 2>&1 && ok "--style stub conforms" || bad "--style stub conforms" +fi +( cd "$work" && "$mkproj" --style -l nasm sn ) >/dev/null 2>&1 && bad "--style rejects nasm" || ok "--style rejects nasm" +( cd "$work" && "$mkproj" runt ) >/dev/null 2>&1 && + ( cd "$work/runt" && make run ) 2>/dev/null | grep -q 'type shi' && ok "make run" || bad "make run" +( cd "$work" && "$mkproj" --ci cit ) >/dev/null 2>&1 +[ -f "$work/cit/.github/workflows/ci.yml" ] && ok "--ci workflow" || bad "--ci workflow" +grep -q 'ubuntu-latest' "$work/cit/.github/workflows/ci.yml" 2>/dev/null && ok "--ci c runs on ubuntu" || bad "--ci c runs on ubuntu" +( cd "$work" && "$mkproj" --ci -l swift cis ) >/dev/null 2>&1 +grep -q 'macos-latest' "$work/cis/.github/workflows/ci.yml" 2>/dev/null && ok "--ci swift runs on macos" || bad "--ci swift runs on macos" +( cd "$work" && "$mkproj" --src source srcdir ) >/dev/null 2>&1 && + [ -f "$work/srcdir/source/main.c" ] && + ( cd "$work/srcdir" && make ) >/dev/null 2>&1 && + [ -x "$work/srcdir/srcdir" ] && ok "--src custom source dir" || bad "--src custom source dir" +mkdir -p "$work/inplace" +( cd "$work/inplace" && "$mkproj" . ) >/dev/null 2>&1 && + [ -f "$work/inplace/main.c" ] && [ -f "$work/inplace/Makefile" ] && + ( cd "$work/inplace" && make ) >/dev/null 2>&1 && + [ -x "$work/inplace/inplace" ] && ok "mkproj . (in place)" || bad "mkproj . (in place)" +( cd "$work/inplace" && "$mkproj" . ) >/dev/null 2>&1 && bad "in place refuses to clobber" || ok "in place refuses to clobber" echo printf '%d passed, %d failed\n' "$pass" "$fail" |
