blob: ce95156208967a6f9c187e426cd98ef30c7fdc1f [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
swissChilid98781b2021-07-25 21:04:17 -070012define(status_log, ` @printf " \e[1;34m%8s\e[m %s\n" "$1" "$2" > /dev/stderr')
swissChilie8491742021-04-08 20:38:06 -070013define(DO_MAKE, `$(MAKE) --no-print-directory MAKEFILE_DEPTH="$$(( $(MAKEFILE_DEPTH) + 1))"')
swissChili2b5acc82021-03-13 17:06:42 -080014
swissChilid98781b2021-07-25 21:04:17 -070015define(gtags_path, $(ROOT)/GTAGS)
16define(gen_gtags, ifelse(disable_gtags,true,,cd $(ROOT) && gtags))
17
swissChili2b5acc82021-03-13 17:06:42 -080018define(init,
19`jmk_project := $1
20jmk_target = ifelse(`$#', 2, $2, $1)
21ROOT := jmk_root
22ASM ?= nasm
23CC ?= gcc
24LD ?= ld
25CFLAGS += -I$(ROOT)/include
swissChili52a03d82021-07-18 15:22:14 -070026ASMFLAGS += -felf
swissChilie8491742021-04-08 20:38:06 -070027jmk_clean_libs =
28
29MAKEFILE_DEPTH ?= 1
swissChili2b5acc82021-03-13 17:06:42 -080030
swissChili8cfb7c42021-04-18 21:17:58 -070031ifneq (,$(wildcard ./Jmk.options))
32 include Jmk.options
33endif
34
swissChili2b5acc82021-03-13 17:06:42 -080035all: $(jmk_target)')
36
37dnl preset applies a certain configuration preset to the project
38define(preset,
39 `ifelse($1, `freestanding',
40 `CFLAGS += -nostdlib -nostdinc -fno-builtin -fno-stack-protector -ffreestanding',
41 $1, `optimize', `CFLAGS += -O2',
swissChili52a03d82021-07-18 15:22:14 -070042 $1, `debug', `CFLAGS += -g
43ASMFLAGS += -Fdwarf',
swissChili2b5acc82021-03-13 17:06:42 -080044 $1, `32', `CFLAGS += -m32',
swissChilic6d97102021-06-07 22:00:05 -070045 $1, `warn', `CFLAGS += -Wall -Wno-unused-function -Wno-unused-variable -Wno-incompatible-pointer-types',
swissChilie8491742021-04-08 20:38:06 -070046 $1, `nasm', `ASM = nasm')')
swissChili2b5acc82021-03-13 17:06:42 -080047
48dnl this is really, really terrible, but to my knowledge there is no
49dnl other way to escape a $. The manual says nothing about this.
50define(ident, $1)
51define(dollar_at, `ident($)ident(@)')
52
53dnl archetype enables a language archetype
54define(archetype,
swissChilid98781b2021-07-25 21:04:17 -070055 `ifelse($1, c, `.c.o: gtags_path
swissChilie8491742021-04-08 20:38:06 -070056status_log(CC, $<)
57 @$(CC) -c $< -o dollar_at $(CFLAGS)',
swissChilid98781b2021-07-25 21:04:17 -070058 $1, asm, `.s.o: gtags_path
swissChilie8491742021-04-08 20:38:06 -070059status_log(AS, $<)
60 @$(ASM) $(ASMFLAGS) $< -o dollar_at')')
swissChili2b5acc82021-03-13 17:06:42 -080061
62dnl depends declares an external dependency
63define(`depends', `
swissChilie8491742021-04-08 20:38:06 -070064jmk_clean_libs += jmk_lib_clean_$1
swissChili2b5acc82021-03-13 17:06:42 -080065jmk_lib_path_$1 = $2
66jmk_lib_target_$1 = ifelse($3, `', $1.a, $3)
67jmk_libs_phony += $(jmk_lib_path_$1)/$(jmk_lib_target_$1)
68
69$(jmk_lib_path_$1)/$(jmk_lib_target_$1):
swissChilie8491742021-04-08 20:38:06 -070070status_log(MAKE[$(MAKEFILE_DEPTH)], Entering $2)
71 @DO_MAKE -C $(jmk_lib_path_$1) $(jmk_lib_target_$1)
72status_log(MAKE[$(MAKEFILE_DEPTH)], Leaving $2)
73
74jmk_lib_clean_$1:
75 @DO_MAKE -C $2 clean')
swissChili2b5acc82021-03-13 17:06:42 -080076
77dnl lib is used to list an external dependency declared with depends()
swissChili1b839222021-06-03 13:54:40 -070078define(`lib', `ifelse($1,,``$0'',$(jmk_lib_path_$1)/$(jmk_lib_target_$1))')
swissChili2b5acc82021-03-13 17:06:42 -080079
80define(`phony', `jmk_custom_phony += $1')
81
82dnl type is used to specify the target type
83define(type,
84 `ifelse($1, executable,
85`$(jmk_target): $(OBJECTS)
swissChili7a6f5eb2021-04-13 16:46:02 -070086status_log(LD, dollar_at)
swissChilid98781b2021-07-25 21:04:17 -070087 @gen_gtags
swissChilie8491742021-04-08 20:38:06 -070088 @$(CC) -o dollar_at $^ $(CFLAGS)',
swissChili2b5acc82021-03-13 17:06:42 -080089 $1, static_lib,
90`$(jmk_target): $(OBJECTS)
swissChilie8491742021-04-08 20:38:06 -070091status_log(AR, dollar_at)
swissChilid98781b2021-07-25 21:04:17 -070092 @gen_gtags
swissChilie8491742021-04-08 20:38:06 -070093 @ar rcs dollar_at $^',
swissChili2b5acc82021-03-13 17:06:42 -080094 $1, custom_link,
95`$(jmk_target): $(OBJECTS)
swissChilie8491742021-04-08 20:38:06 -070096status_log(LD, dollar_at)
swissChilid98781b2021-07-25 21:04:17 -070097 @gen_gtags
swissChilie8491742021-04-08 20:38:06 -070098 @$(LD) $(LDFLAGS) -o dollar_at $^')')
swissChili2b5acc82021-03-13 17:06:42 -080099
swissChili8cfb7c42021-04-18 21:17:58 -0700100define(option,
101`$1 ?= $3
102jmk_options += $1
103jmk_option_help_$1 = $2 (default: $3)')
104
swissChili2b5acc82021-03-13 17:06:42 -0800105dnl finish is required at the end of the Jmk file to generate some
106dnl final declarations
107
108define(finish,
swissChilie8491742021-04-08 20:38:06 -0700109`clean: $(jmk_clean_libs)
swissChili2999dd12021-07-02 14:19:53 -0700110 @rm -f **/*.o **/*.a *.so $(jmk_target) $(OBJECTS)
swissChili2b5acc82021-03-13 17:06:42 -0800111
112Makefile: Jmk
swissChilie8491742021-04-08 20:38:06 -0700113status_log(JMK, jmk_build_dir)
114 @cd "jmk_build_dir" && jmk_build_cmd
swissChili2b5acc82021-03-13 17:06:42 -0800115
swissChilid98781b2021-07-25 21:04:17 -0700116gtags_path:
117status_log(GTAGS,)
118 gen_gtags
119
120show_cflags:
121 @echo $(CFLAGS)
122
123.PHONY: $(jmk_libs_phony) $(jmk_custom_phony) $(jmk_clean_libs) clean all show_cflags')
swissChili2b5acc82021-03-13 17:06:42 -0800124
125divert(0)dnl