blob: 0b2c96c590340451111260bd42879c483a08b018 [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
swissChili8cfb7c42021-04-18 21:17:58 -070027ifneq (,$(wildcard ./Jmk.options))
28 include Jmk.options
29endif
30
swissChili2b5acc82021-03-13 17:06:42 -080031all: $(jmk_target)')
32
33dnl preset applies a certain configuration preset to the project
34define(preset,
35 `ifelse($1, `freestanding',
36 `CFLAGS += -nostdlib -nostdinc -fno-builtin -fno-stack-protector -ffreestanding',
37 $1, `optimize', `CFLAGS += -O2',
38 $1, `debug', `CFLAGS += -g',
39 $1, `32', `CFLAGS += -m32',
swissChilic6d97102021-06-07 22:00:05 -070040 $1, `warn', `CFLAGS += -Wall -Wno-unused-function -Wno-unused-variable -Wno-incompatible-pointer-types',
swissChilie8491742021-04-08 20:38:06 -070041 $1, `nasm', `ASM = nasm')')
swissChili2b5acc82021-03-13 17:06:42 -080042
43dnl this is really, really terrible, but to my knowledge there is no
44dnl other way to escape a $. The manual says nothing about this.
45define(ident, $1)
46define(dollar_at, `ident($)ident(@)')
47
48dnl archetype enables a language archetype
49define(archetype,
50 `ifelse($1, c, `.c.o:
swissChilie8491742021-04-08 20:38:06 -070051status_log(CC, $<)
52 @$(CC) -c $< -o dollar_at $(CFLAGS)',
swissChili2b5acc82021-03-13 17:06:42 -080053 $1, asm, `.s.o:
swissChilie8491742021-04-08 20:38:06 -070054status_log(AS, $<)
55 @$(ASM) $(ASMFLAGS) $< -o dollar_at')')
swissChili2b5acc82021-03-13 17:06:42 -080056
57dnl depends declares an external dependency
58define(`depends', `
swissChilie8491742021-04-08 20:38:06 -070059jmk_clean_libs += jmk_lib_clean_$1
swissChili2b5acc82021-03-13 17:06:42 -080060jmk_lib_path_$1 = $2
61jmk_lib_target_$1 = ifelse($3, `', $1.a, $3)
62jmk_libs_phony += $(jmk_lib_path_$1)/$(jmk_lib_target_$1)
63
64$(jmk_lib_path_$1)/$(jmk_lib_target_$1):
swissChilie8491742021-04-08 20:38:06 -070065status_log(MAKE[$(MAKEFILE_DEPTH)], Entering $2)
66 @DO_MAKE -C $(jmk_lib_path_$1) $(jmk_lib_target_$1)
67status_log(MAKE[$(MAKEFILE_DEPTH)], Leaving $2)
68
69jmk_lib_clean_$1:
70 @DO_MAKE -C $2 clean')
swissChili2b5acc82021-03-13 17:06:42 -080071
72dnl lib is used to list an external dependency declared with depends()
swissChili1b839222021-06-03 13:54:40 -070073define(`lib', `ifelse($1,,``$0'',$(jmk_lib_path_$1)/$(jmk_lib_target_$1))')
swissChili2b5acc82021-03-13 17:06:42 -080074
75define(`phony', `jmk_custom_phony += $1')
76
77dnl type is used to specify the target type
78define(type,
79 `ifelse($1, executable,
80`$(jmk_target): $(OBJECTS)
swissChili7a6f5eb2021-04-13 16:46:02 -070081status_log(LD, dollar_at)
swissChilie8491742021-04-08 20:38:06 -070082 @$(CC) -o dollar_at $^ $(CFLAGS)',
swissChili2b5acc82021-03-13 17:06:42 -080083 $1, static_lib,
84`$(jmk_target): $(OBJECTS)
swissChilie8491742021-04-08 20:38:06 -070085status_log(AR, dollar_at)
86 @ar rcs dollar_at $^',
swissChili2b5acc82021-03-13 17:06:42 -080087 $1, custom_link,
88`$(jmk_target): $(OBJECTS)
swissChilie8491742021-04-08 20:38:06 -070089status_log(LD, dollar_at)
90 @$(LD) $(LDFLAGS) -o dollar_at $^')')
swissChili2b5acc82021-03-13 17:06:42 -080091
swissChili8cfb7c42021-04-18 21:17:58 -070092define(option,
93`$1 ?= $3
94jmk_options += $1
95jmk_option_help_$1 = $2 (default: $3)')
96
swissChili2b5acc82021-03-13 17:06:42 -080097dnl finish is required at the end of the Jmk file to generate some
98dnl final declarations
99
100define(finish,
swissChilie8491742021-04-08 20:38:06 -0700101`clean: $(jmk_clean_libs)
swissChili2999dd12021-07-02 14:19:53 -0700102 @rm -f **/*.o **/*.a *.so $(jmk_target) $(OBJECTS)
swissChili2b5acc82021-03-13 17:06:42 -0800103
104Makefile: Jmk
swissChilie8491742021-04-08 20:38:06 -0700105status_log(JMK, jmk_build_dir)
106 @cd "jmk_build_dir" && jmk_build_cmd
swissChili2b5acc82021-03-13 17:06:42 -0800107
swissChilie8491742021-04-08 20:38:06 -0700108.PHONY: $(jmk_libs_phony) $(jmk_custom_phony) $(jmk_clean_libs) clean all')
swissChili2b5acc82021-03-13 17:06:42 -0800109
110divert(0)dnl