blob: 06ca08a1a4f365854fe2bc45378404ed85168b2b [file] [log] [blame]
swissChili2b5acc82021-03-13 17:06:42 -08001divert(-1)
2
3dnl
4dnl JMK (Jay MaKe) build system
5dnl
6
7define(`foreach', `pushdef(`$1')_foreach($@)popdef(`$1')')
8define(`_arg1', `$1')
9define(`_foreach', `ifelse(`$2', `()', `',
10 `define(`$1', _arg1$2)$3`'$0(`$1', (shift$2), `$3')')')
11
swissChilie8491742021-04-08 20:38:06 -070012define(status_log, ` @printf " \e[1;34m%8s\e[m %s\n" "$1" "$2"')
13define(DO_MAKE, `$(MAKE) --no-print-directory MAKEFILE_DEPTH="$$(( $(MAKEFILE_DEPTH) + 1))"')
swissChili2b5acc82021-03-13 17:06:42 -080014
15define(init,
16`jmk_project := $1
17jmk_target = ifelse(`$#', 2, $2, $1)
18ROOT := jmk_root
19ASM ?= nasm
20CC ?= gcc
21LD ?= ld
22CFLAGS += -I$(ROOT)/include
swissChilie8491742021-04-08 20:38:06 -070023jmk_clean_libs =
24
25MAKEFILE_DEPTH ?= 1
swissChili2b5acc82021-03-13 17:06:42 -080026
27all: $(jmk_target)')
28
29dnl preset applies a certain configuration preset to the project
30define(preset,
31 `ifelse($1, `freestanding',
32 `CFLAGS += -nostdlib -nostdinc -fno-builtin -fno-stack-protector -ffreestanding',
33 $1, `optimize', `CFLAGS += -O2',
34 $1, `debug', `CFLAGS += -g',
35 $1, `32', `CFLAGS += -m32',
36 $1, `warn', `CFLAGS += -Wall -Wno-unused-function -Wno-unused-variable',
swissChilie8491742021-04-08 20:38:06 -070037 $1, `nasm', `ASM = nasm')')
swissChili2b5acc82021-03-13 17:06:42 -080038
39dnl this is really, really terrible, but to my knowledge there is no
40dnl other way to escape a $. The manual says nothing about this.
41define(ident, $1)
42define(dollar_at, `ident($)ident(@)')
43
44dnl archetype enables a language archetype
45define(archetype,
46 `ifelse($1, c, `.c.o:
swissChilie8491742021-04-08 20:38:06 -070047status_log(CC, $<)
48 @$(CC) -c $< -o dollar_at $(CFLAGS)',
swissChili2b5acc82021-03-13 17:06:42 -080049 $1, asm, `.s.o:
swissChilie8491742021-04-08 20:38:06 -070050status_log(AS, $<)
51 @$(ASM) $(ASMFLAGS) $< -o dollar_at')')
swissChili2b5acc82021-03-13 17:06:42 -080052
53dnl depends declares an external dependency
54define(`depends', `
swissChilie8491742021-04-08 20:38:06 -070055jmk_clean_libs += jmk_lib_clean_$1
swissChili2b5acc82021-03-13 17:06:42 -080056jmk_lib_path_$1 = $2
57jmk_lib_target_$1 = ifelse($3, `', $1.a, $3)
58jmk_libs_phony += $(jmk_lib_path_$1)/$(jmk_lib_target_$1)
59
60$(jmk_lib_path_$1)/$(jmk_lib_target_$1):
swissChilie8491742021-04-08 20:38:06 -070061status_log(MAKE[$(MAKEFILE_DEPTH)], Entering $2)
62 @DO_MAKE -C $(jmk_lib_path_$1) $(jmk_lib_target_$1)
63status_log(MAKE[$(MAKEFILE_DEPTH)], Leaving $2)
64
65jmk_lib_clean_$1:
66 @DO_MAKE -C $2 clean')
swissChili2b5acc82021-03-13 17:06:42 -080067
68dnl lib is used to list an external dependency declared with depends()
69define(`lib', `$(jmk_lib_path_$1)/$(jmk_lib_target_$1)')
70
71define(`phony', `jmk_custom_phony += $1')
72
73dnl type is used to specify the target type
74define(type,
75 `ifelse($1, executable,
76`$(jmk_target): $(OBJECTS)
swissChilie8491742021-04-08 20:38:06 -070077status_log(CC, dollar_at)
78 @$(CC) -o dollar_at $^ $(CFLAGS)',
swissChili2b5acc82021-03-13 17:06:42 -080079 $1, static_lib,
80`$(jmk_target): $(OBJECTS)
swissChilie8491742021-04-08 20:38:06 -070081status_log(AR, dollar_at)
82 @ar rcs dollar_at $^',
swissChili2b5acc82021-03-13 17:06:42 -080083 $1, custom_link,
84`$(jmk_target): $(OBJECTS)
swissChilie8491742021-04-08 20:38:06 -070085status_log(LD, dollar_at)
86 @$(LD) $(LDFLAGS) -o dollar_at $^')')
swissChili2b5acc82021-03-13 17:06:42 -080087
88dnl finish is required at the end of the Jmk file to generate some
89dnl final declarations
90
91define(finish,
swissChilie8491742021-04-08 20:38:06 -070092`clean: $(jmk_clean_libs)
93 @rm -f *.o *.a *.so $(jmk_target)
swissChili2b5acc82021-03-13 17:06:42 -080094
95Makefile: Jmk
swissChilie8491742021-04-08 20:38:06 -070096status_log(JMK, jmk_build_dir)
97 @cd "jmk_build_dir" && jmk_build_cmd
swissChili2b5acc82021-03-13 17:06:42 -080098
swissChilie8491742021-04-08 20:38:06 -070099.PHONY: $(jmk_libs_phony) $(jmk_custom_phony) $(jmk_clean_libs) clean all')
swissChili2b5acc82021-03-13 17:06:42 -0800100
101divert(0)dnl