blob: aa3ad7936c973c45fc43030035f033c6c33ea8f4 [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
swissChilifbf525f2021-08-04 21:28:07 -070025LATEXC ?= pdflatex
26LATEX_MAKE_GLOSSARIES = 0
27LATEX_EXTRAS = *.aux *.glg *.glo *.gls *.idx *.ilg *ind *.ist *.log *.toc
swissChili2b5acc82021-03-13 17:06:42 -080028CFLAGS += -I$(ROOT)/include
swissChili52a03d82021-07-18 15:22:14 -070029ASMFLAGS += -felf
swissChilie8491742021-04-08 20:38:06 -070030jmk_clean_libs =
31
32MAKEFILE_DEPTH ?= 1
swissChili2b5acc82021-03-13 17:06:42 -080033
swissChili8cfb7c42021-04-18 21:17:58 -070034ifneq (,$(wildcard ./Jmk.options))
35 include Jmk.options
36endif
37
swissChili2b5acc82021-03-13 17:06:42 -080038all: $(jmk_target)')
39
40dnl preset applies a certain configuration preset to the project
41define(preset,
42 `ifelse($1, `freestanding',
43 `CFLAGS += -nostdlib -nostdinc -fno-builtin -fno-stack-protector -ffreestanding',
44 $1, `optimize', `CFLAGS += -O2',
swissChili52a03d82021-07-18 15:22:14 -070045 $1, `debug', `CFLAGS += -g
46ASMFLAGS += -Fdwarf',
swissChili2b5acc82021-03-13 17:06:42 -080047 $1, `32', `CFLAGS += -m32',
swissChili36f2c692021-08-08 14:31:44 -070048 $1, `warn', `CFLAGS += -Wall -Wextra -Wno-unused-function -Wno-unused-variable -Wno-incompatible-pointer-types',
swissChilifbf525f2021-08-04 21:28:07 -070049 $1, `nasm', `ASM = nasm',
50 $1, `glossaries', `LATEX_MAKE_GLOSSARIES = 1',
51 $1, xelatex, `LATEXC = xelatex')')
swissChili2b5acc82021-03-13 17:06:42 -080052
53dnl this is really, really terrible, but to my knowledge there is no
54dnl other way to escape a $. The manual says nothing about this.
55define(ident, $1)
56define(dollar_at, `ident($)ident(@)')
57
58dnl archetype enables a language archetype
59define(archetype,
swissChilib51552c2021-08-03 10:23:37 -070060 `ifelse($1, c, `.c.o:
swissChilie8491742021-04-08 20:38:06 -070061status_log(CC, $<)
62 @$(CC) -c $< -o dollar_at $(CFLAGS)',
swissChilib51552c2021-08-03 10:23:37 -070063 $1, asm, `.s.o:
swissChilie8491742021-04-08 20:38:06 -070064status_log(AS, $<)
65 @$(ASM) $(ASMFLAGS) $< -o dollar_at')')
swissChili2b5acc82021-03-13 17:06:42 -080066
67dnl depends declares an external dependency
68define(`depends', `
swissChilie8491742021-04-08 20:38:06 -070069jmk_clean_libs += jmk_lib_clean_$1
swissChili2b5acc82021-03-13 17:06:42 -080070jmk_lib_path_$1 = $2
71jmk_lib_target_$1 = ifelse($3, `', $1.a, $3)
72jmk_libs_phony += $(jmk_lib_path_$1)/$(jmk_lib_target_$1)
73
74$(jmk_lib_path_$1)/$(jmk_lib_target_$1):
swissChilie8491742021-04-08 20:38:06 -070075status_log(MAKE[$(MAKEFILE_DEPTH)], Entering $2)
76 @DO_MAKE -C $(jmk_lib_path_$1) $(jmk_lib_target_$1)
77status_log(MAKE[$(MAKEFILE_DEPTH)], Leaving $2)
78
79jmk_lib_clean_$1:
80 @DO_MAKE -C $2 clean')
swissChili2b5acc82021-03-13 17:06:42 -080081
82dnl lib is used to list an external dependency declared with depends()
swissChili1b839222021-06-03 13:54:40 -070083define(`lib', `ifelse($1,,``$0'',$(jmk_lib_path_$1)/$(jmk_lib_target_$1))')
swissChili2b5acc82021-03-13 17:06:42 -080084
85define(`phony', `jmk_custom_phony += $1')
86
87dnl type is used to specify the target type
88define(type,
89 `ifelse($1, executable,
90`$(jmk_target): $(OBJECTS)
swissChili7a6f5eb2021-04-13 16:46:02 -070091status_log(LD, dollar_at)
swissChilid98781b2021-07-25 21:04:17 -070092 @gen_gtags
swissChilie8491742021-04-08 20:38:06 -070093 @$(CC) -o dollar_at $^ $(CFLAGS)',
swissChili2b5acc82021-03-13 17:06:42 -080094 $1, static_lib,
95`$(jmk_target): $(OBJECTS)
swissChilie8491742021-04-08 20:38:06 -070096status_log(AR, dollar_at)
swissChilid98781b2021-07-25 21:04:17 -070097 @gen_gtags
swissChilie8491742021-04-08 20:38:06 -070098 @ar rcs dollar_at $^',
swissChili2b5acc82021-03-13 17:06:42 -080099 $1, custom_link,
100`$(jmk_target): $(OBJECTS)
swissChilie8491742021-04-08 20:38:06 -0700101status_log(LD, dollar_at)
swissChilid98781b2021-07-25 21:04:17 -0700102 @gen_gtags
swissChilifbf525f2021-08-04 21:28:07 -0700103 @$(LD) $(LDFLAGS) -o dollar_at $^',
104 $1, latex,
105`$(jmk_target): $(LATEX_SOURCES)
106status_log(LATEX, dollar_at)
107 @$(LATEXC) $<
108ifeq ($(LATEX_MAKE_GLOSSARIES),1)
109status_log(GLOSSARY, dollar_at)
110 @makeglossaries $(patsubst %.tex,%,$<)
111 @$(LATEXC) $<
112endif')')
swissChili2b5acc82021-03-13 17:06:42 -0800113
swissChili8cfb7c42021-04-18 21:17:58 -0700114define(option,
115`$1 ?= $3
116jmk_options += $1
117jmk_option_help_$1 = $2 (default: $3)')
118
swissChili2b5acc82021-03-13 17:06:42 -0800119dnl finish is required at the end of the Jmk file to generate some
120dnl final declarations
121
122define(finish,
swissChilie8491742021-04-08 20:38:06 -0700123`clean: $(jmk_clean_libs)
swissChilifbf525f2021-08-04 21:28:07 -0700124 @rm -f **/*.o **/*.a *.so $(jmk_target) $(OBJECTS) $(LATEX_EXTRAS)
swissChili2b5acc82021-03-13 17:06:42 -0800125
126Makefile: Jmk
swissChilie8491742021-04-08 20:38:06 -0700127status_log(JMK, jmk_build_dir)
128 @cd "jmk_build_dir" && jmk_build_cmd
swissChili2b5acc82021-03-13 17:06:42 -0800129
swissChilid98781b2021-07-25 21:04:17 -0700130gtags_path:
131status_log(GTAGS,)
132 gen_gtags
133
134show_cflags:
135 @echo $(CFLAGS)
136
137.PHONY: $(jmk_libs_phony) $(jmk_custom_phony) $(jmk_clean_libs) clean all show_cflags')
swissChili2b5acc82021-03-13 17:06:42 -0800138
139divert(0)dnl