blob: e251a8b8e09e1f197c5e2b5de23b990c1f3c7e15 [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
swissChili52a03d82021-07-18 15:22:14 -070023ASMFLAGS += -felf
swissChilie8491742021-04-08 20:38:06 -070024jmk_clean_libs =
25
26MAKEFILE_DEPTH ?= 1
swissChili2b5acc82021-03-13 17:06:42 -080027
swissChili8cfb7c42021-04-18 21:17:58 -070028ifneq (,$(wildcard ./Jmk.options))
29 include Jmk.options
30endif
31
swissChili2b5acc82021-03-13 17:06:42 -080032all: $(jmk_target)')
33
34dnl preset applies a certain configuration preset to the project
35define(preset,
36 `ifelse($1, `freestanding',
37 `CFLAGS += -nostdlib -nostdinc -fno-builtin -fno-stack-protector -ffreestanding',
38 $1, `optimize', `CFLAGS += -O2',
swissChili52a03d82021-07-18 15:22:14 -070039 $1, `debug', `CFLAGS += -g
40ASMFLAGS += -Fdwarf',
swissChili2b5acc82021-03-13 17:06:42 -080041 $1, `32', `CFLAGS += -m32',
swissChilic6d97102021-06-07 22:00:05 -070042 $1, `warn', `CFLAGS += -Wall -Wno-unused-function -Wno-unused-variable -Wno-incompatible-pointer-types',
swissChilie8491742021-04-08 20:38:06 -070043 $1, `nasm', `ASM = nasm')')
swissChili2b5acc82021-03-13 17:06:42 -080044
45dnl this is really, really terrible, but to my knowledge there is no
46dnl other way to escape a $. The manual says nothing about this.
47define(ident, $1)
48define(dollar_at, `ident($)ident(@)')
49
50dnl archetype enables a language archetype
51define(archetype,
52 `ifelse($1, c, `.c.o:
swissChilie8491742021-04-08 20:38:06 -070053status_log(CC, $<)
54 @$(CC) -c $< -o dollar_at $(CFLAGS)',
swissChili2b5acc82021-03-13 17:06:42 -080055 $1, asm, `.s.o:
swissChilie8491742021-04-08 20:38:06 -070056status_log(AS, $<)
57 @$(ASM) $(ASMFLAGS) $< -o dollar_at')')
swissChili2b5acc82021-03-13 17:06:42 -080058
59dnl depends declares an external dependency
60define(`depends', `
swissChilie8491742021-04-08 20:38:06 -070061jmk_clean_libs += jmk_lib_clean_$1
swissChili2b5acc82021-03-13 17:06:42 -080062jmk_lib_path_$1 = $2
63jmk_lib_target_$1 = ifelse($3, `', $1.a, $3)
64jmk_libs_phony += $(jmk_lib_path_$1)/$(jmk_lib_target_$1)
65
66$(jmk_lib_path_$1)/$(jmk_lib_target_$1):
swissChilie8491742021-04-08 20:38:06 -070067status_log(MAKE[$(MAKEFILE_DEPTH)], Entering $2)
68 @DO_MAKE -C $(jmk_lib_path_$1) $(jmk_lib_target_$1)
69status_log(MAKE[$(MAKEFILE_DEPTH)], Leaving $2)
70
71jmk_lib_clean_$1:
72 @DO_MAKE -C $2 clean')
swissChili2b5acc82021-03-13 17:06:42 -080073
74dnl lib is used to list an external dependency declared with depends()
swissChili1b839222021-06-03 13:54:40 -070075define(`lib', `ifelse($1,,``$0'',$(jmk_lib_path_$1)/$(jmk_lib_target_$1))')
swissChili2b5acc82021-03-13 17:06:42 -080076
77define(`phony', `jmk_custom_phony += $1')
78
79dnl type is used to specify the target type
80define(type,
81 `ifelse($1, executable,
82`$(jmk_target): $(OBJECTS)
swissChili7a6f5eb2021-04-13 16:46:02 -070083status_log(LD, dollar_at)
swissChilie8491742021-04-08 20:38:06 -070084 @$(CC) -o dollar_at $^ $(CFLAGS)',
swissChili2b5acc82021-03-13 17:06:42 -080085 $1, static_lib,
86`$(jmk_target): $(OBJECTS)
swissChilie8491742021-04-08 20:38:06 -070087status_log(AR, dollar_at)
88 @ar rcs dollar_at $^',
swissChili2b5acc82021-03-13 17:06:42 -080089 $1, custom_link,
90`$(jmk_target): $(OBJECTS)
swissChilie8491742021-04-08 20:38:06 -070091status_log(LD, dollar_at)
92 @$(LD) $(LDFLAGS) -o dollar_at $^')')
swissChili2b5acc82021-03-13 17:06:42 -080093
swissChili8cfb7c42021-04-18 21:17:58 -070094define(option,
95`$1 ?= $3
96jmk_options += $1
97jmk_option_help_$1 = $2 (default: $3)')
98
swissChili2b5acc82021-03-13 17:06:42 -080099dnl finish is required at the end of the Jmk file to generate some
100dnl final declarations
101
102define(finish,
swissChilie8491742021-04-08 20:38:06 -0700103`clean: $(jmk_clean_libs)
swissChili2999dd12021-07-02 14:19:53 -0700104 @rm -f **/*.o **/*.a *.so $(jmk_target) $(OBJECTS)
swissChili2b5acc82021-03-13 17:06:42 -0800105
106Makefile: Jmk
swissChilie8491742021-04-08 20:38:06 -0700107status_log(JMK, jmk_build_dir)
108 @cd "jmk_build_dir" && jmk_build_cmd
swissChili2b5acc82021-03-13 17:06:42 -0800109
swissChilie8491742021-04-08 20:38:06 -0700110.PHONY: $(jmk_libs_phony) $(jmk_custom_phony) $(jmk_clean_libs) clean all')
swissChili2b5acc82021-03-13 17:06:42 -0800111
112divert(0)dnl