Building code, applying recipes? - Programming On Unix

Users browsing this thread: 1 Guest(s)
z3bra
Grey Hair Nixers
I mostly (only?) write C, for which I will always write a makefile. It's almost always the same (see safe/makefile as an example), and uses a "config.mk" file to change the build system, with well established macros, so the "users" can customize it according to what they're used to (LDFLAGS, DESTDIR, PREFIX, …).
I write strictly POSIX makefiles, which isn't too hard, and IMO only lacks a way to auto discover source files (for which shell assignement, "SRC != find . -name *.c", works with both GNU and BSD make).

Edit:You can take a look at this link for a good way to write portable makefiles (gopher).

What I like about make is the convenience of its implicit rules. So convenient, that you might not even need a makefile at all !

Code:
$ ls -l
total 4
-rw-r--r-- 1 z3bra users 121 Aug 14 09:27 sqrt.c
$ cat sqrt.c
#include <stdlib.h>
#include <math.h>
int
main(int argc, char *argv[])
{
        return argc > 1 ? sqrt(atoi(argv[1])) : -1;
}
$ make LDFLAGS=-lm sqrt
cc   -lm  sqrt.c   -o sqrt
$ ./sqrt 16; echo $?
4

Thanks to implicit rules, and builtin macros, make knows how to build target "sqrt" without me having to specify a makefile. This is pretty handy, and I use it very often !

Now make is my "marry one" choice. The (IMO) saner alternative that only lacks a wider adoption to rule the world, is plan9's mk. It is make, but simpler. There are not macros (with weird expansion times), no implicit rules (easier to debug), and better shell integration (eg, auto discover source files).

I usually provide an mkfile along with my programs, so I can be the "change I want to see in the world" :)
It can even reuse the makefile's config.mk (if it's strictly POSIX ofc). Here is for example the mkfile for the same project I shared above: safe/mkfile. It ressemble the makefile quite a lot !

Coming from plan9, mk integrates better with the system than make. When make defines "Macros", mk will export them as variables in the environment when building the recipes. This means that mk will NOT expand the variables before passing them to the shell, and it will pass the recipes "as-is", without modifying them. This is boring, predictable, awesome to use. No need to learn a new "language" atop of your shell, and pre-process macros in your head to debug recipes.

I definitely prefer mk to make.


Another option to consider is djb's redo, through apenwarr's implementation.
It's more similar to ninja, and looks appealing, but I must say that it seems to clutter the project with lots of files. I never tried that though, so I don't want to elaborate on the subject. Looks interresting :)


Messages In This Thread
Building code, applying recipes? - by freem - 13-08-2020, 10:16 PM
RE: Building code, applying recipes? - by venam - 14-08-2020, 03:20 AM
RE: Building code, applying recipes? - by z3bra - 14-08-2020, 04:48 AM
RE: Building code, applying recipes? - by jkl - 14-08-2020, 04:58 PM
RE: Building code, applying recipes? - by freem - 17-08-2020, 02:15 PM
RE: Building code, applying recipes? - by jkl - 18-08-2020, 02:50 AM
RE: Building code, applying recipes? - by z3bra - 23-08-2020, 04:37 PM
RE: Building code, applying recipes? - by venam - 24-07-2021, 06:18 AM