swissChili | 2b5acc8 | 2021-03-13 17:06:42 -0800 | [diff] [blame] | 1 | divert(-1) |
| 2 | |
| 3 | dnl |
| 4 | dnl JMK (Jay MaKe) build system |
| 5 | dnl |
| 6 | |
| 7 | define(`foreach', `pushdef(`$1')_foreach($@)popdef(`$1')') |
| 8 | define(`_arg1', `$1') |
| 9 | define(`_foreach', `ifelse(`$2', `()', `', |
| 10 | `define(`$1', _arg1$2)$3`'$0(`$1', (shift$2), `$3')')') |
| 11 | |
swissChili | d98781b | 2021-07-25 21:04:17 -0700 | [diff] [blame] | 12 | define(status_log, ` @printf " \e[1;34m%8s\e[m %s\n" "$1" "$2" > /dev/stderr') |
swissChili | e849174 | 2021-04-08 20:38:06 -0700 | [diff] [blame] | 13 | define(DO_MAKE, `$(MAKE) --no-print-directory MAKEFILE_DEPTH="$$(( $(MAKEFILE_DEPTH) + 1))"') |
swissChili | 2b5acc8 | 2021-03-13 17:06:42 -0800 | [diff] [blame] | 14 | |
swissChili | d98781b | 2021-07-25 21:04:17 -0700 | [diff] [blame] | 15 | define(gtags_path, $(ROOT)/GTAGS) |
| 16 | define(gen_gtags, ifelse(disable_gtags,true,,cd $(ROOT) && gtags)) |
| 17 | |
swissChili | 2b5acc8 | 2021-03-13 17:06:42 -0800 | [diff] [blame] | 18 | define(init, |
| 19 | `jmk_project := $1 |
| 20 | jmk_target = ifelse(`$#', 2, $2, $1) |
| 21 | ROOT := jmk_root |
| 22 | ASM ?= nasm |
| 23 | CC ?= gcc |
| 24 | LD ?= ld |
swissChili | fbf525f | 2021-08-04 21:28:07 -0700 | [diff] [blame] | 25 | LATEXC ?= pdflatex |
| 26 | LATEX_MAKE_GLOSSARIES = 0 |
| 27 | LATEX_EXTRAS = *.aux *.glg *.glo *.gls *.idx *.ilg *ind *.ist *.log *.toc |
swissChili | 2b5acc8 | 2021-03-13 17:06:42 -0800 | [diff] [blame] | 28 | CFLAGS += -I$(ROOT)/include |
swissChili | 52a03d8 | 2021-07-18 15:22:14 -0700 | [diff] [blame] | 29 | ASMFLAGS += -felf |
swissChili | e849174 | 2021-04-08 20:38:06 -0700 | [diff] [blame] | 30 | jmk_clean_libs = |
| 31 | |
| 32 | MAKEFILE_DEPTH ?= 1 |
swissChili | 2b5acc8 | 2021-03-13 17:06:42 -0800 | [diff] [blame] | 33 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 34 | ifneq (,$(wildcard ./Jmk.options)) |
| 35 | include Jmk.options |
| 36 | endif |
| 37 | |
swissChili | 2b5acc8 | 2021-03-13 17:06:42 -0800 | [diff] [blame] | 38 | all: $(jmk_target)') |
| 39 | |
| 40 | dnl preset applies a certain configuration preset to the project |
| 41 | define(preset, |
| 42 | `ifelse($1, `freestanding', |
| 43 | `CFLAGS += -nostdlib -nostdinc -fno-builtin -fno-stack-protector -ffreestanding', |
| 44 | $1, `optimize', `CFLAGS += -O2', |
swissChili | 52a03d8 | 2021-07-18 15:22:14 -0700 | [diff] [blame] | 45 | $1, `debug', `CFLAGS += -g |
| 46 | ASMFLAGS += -Fdwarf', |
swissChili | 2b5acc8 | 2021-03-13 17:06:42 -0800 | [diff] [blame] | 47 | $1, `32', `CFLAGS += -m32', |
swissChili | c6d9710 | 2021-06-07 22:00:05 -0700 | [diff] [blame] | 48 | $1, `warn', `CFLAGS += -Wall -Wno-unused-function -Wno-unused-variable -Wno-incompatible-pointer-types', |
swissChili | fbf525f | 2021-08-04 21:28:07 -0700 | [diff] [blame] | 49 | $1, `nasm', `ASM = nasm', |
| 50 | $1, `glossaries', `LATEX_MAKE_GLOSSARIES = 1', |
| 51 | $1, xelatex, `LATEXC = xelatex')') |
swissChili | 2b5acc8 | 2021-03-13 17:06:42 -0800 | [diff] [blame] | 52 | |
| 53 | dnl this is really, really terrible, but to my knowledge there is no |
| 54 | dnl other way to escape a $. The manual says nothing about this. |
| 55 | define(ident, $1) |
| 56 | define(dollar_at, `ident($)ident(@)') |
| 57 | |
| 58 | dnl archetype enables a language archetype |
| 59 | define(archetype, |
swissChili | b51552c | 2021-08-03 10:23:37 -0700 | [diff] [blame] | 60 | `ifelse($1, c, `.c.o: |
swissChili | e849174 | 2021-04-08 20:38:06 -0700 | [diff] [blame] | 61 | status_log(CC, $<) |
| 62 | @$(CC) -c $< -o dollar_at $(CFLAGS)', |
swissChili | b51552c | 2021-08-03 10:23:37 -0700 | [diff] [blame] | 63 | $1, asm, `.s.o: |
swissChili | e849174 | 2021-04-08 20:38:06 -0700 | [diff] [blame] | 64 | status_log(AS, $<) |
| 65 | @$(ASM) $(ASMFLAGS) $< -o dollar_at')') |
swissChili | 2b5acc8 | 2021-03-13 17:06:42 -0800 | [diff] [blame] | 66 | |
| 67 | dnl depends declares an external dependency |
| 68 | define(`depends', ` |
swissChili | e849174 | 2021-04-08 20:38:06 -0700 | [diff] [blame] | 69 | jmk_clean_libs += jmk_lib_clean_$1 |
swissChili | 2b5acc8 | 2021-03-13 17:06:42 -0800 | [diff] [blame] | 70 | jmk_lib_path_$1 = $2 |
| 71 | jmk_lib_target_$1 = ifelse($3, `', $1.a, $3) |
| 72 | jmk_libs_phony += $(jmk_lib_path_$1)/$(jmk_lib_target_$1) |
| 73 | |
| 74 | $(jmk_lib_path_$1)/$(jmk_lib_target_$1): |
swissChili | e849174 | 2021-04-08 20:38:06 -0700 | [diff] [blame] | 75 | status_log(MAKE[$(MAKEFILE_DEPTH)], Entering $2) |
| 76 | @DO_MAKE -C $(jmk_lib_path_$1) $(jmk_lib_target_$1) |
| 77 | status_log(MAKE[$(MAKEFILE_DEPTH)], Leaving $2) |
| 78 | |
| 79 | jmk_lib_clean_$1: |
| 80 | @DO_MAKE -C $2 clean') |
swissChili | 2b5acc8 | 2021-03-13 17:06:42 -0800 | [diff] [blame] | 81 | |
| 82 | dnl lib is used to list an external dependency declared with depends() |
swissChili | 1b83922 | 2021-06-03 13:54:40 -0700 | [diff] [blame] | 83 | define(`lib', `ifelse($1,,``$0'',$(jmk_lib_path_$1)/$(jmk_lib_target_$1))') |
swissChili | 2b5acc8 | 2021-03-13 17:06:42 -0800 | [diff] [blame] | 84 | |
| 85 | define(`phony', `jmk_custom_phony += $1') |
| 86 | |
| 87 | dnl type is used to specify the target type |
| 88 | define(type, |
| 89 | `ifelse($1, executable, |
| 90 | `$(jmk_target): $(OBJECTS) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 91 | status_log(LD, dollar_at) |
swissChili | d98781b | 2021-07-25 21:04:17 -0700 | [diff] [blame] | 92 | @gen_gtags |
swissChili | e849174 | 2021-04-08 20:38:06 -0700 | [diff] [blame] | 93 | @$(CC) -o dollar_at $^ $(CFLAGS)', |
swissChili | 2b5acc8 | 2021-03-13 17:06:42 -0800 | [diff] [blame] | 94 | $1, static_lib, |
| 95 | `$(jmk_target): $(OBJECTS) |
swissChili | e849174 | 2021-04-08 20:38:06 -0700 | [diff] [blame] | 96 | status_log(AR, dollar_at) |
swissChili | d98781b | 2021-07-25 21:04:17 -0700 | [diff] [blame] | 97 | @gen_gtags |
swissChili | e849174 | 2021-04-08 20:38:06 -0700 | [diff] [blame] | 98 | @ar rcs dollar_at $^', |
swissChili | 2b5acc8 | 2021-03-13 17:06:42 -0800 | [diff] [blame] | 99 | $1, custom_link, |
| 100 | `$(jmk_target): $(OBJECTS) |
swissChili | e849174 | 2021-04-08 20:38:06 -0700 | [diff] [blame] | 101 | status_log(LD, dollar_at) |
swissChili | d98781b | 2021-07-25 21:04:17 -0700 | [diff] [blame] | 102 | @gen_gtags |
swissChili | fbf525f | 2021-08-04 21:28:07 -0700 | [diff] [blame] | 103 | @$(LD) $(LDFLAGS) -o dollar_at $^', |
| 104 | $1, latex, |
| 105 | `$(jmk_target): $(LATEX_SOURCES) |
| 106 | status_log(LATEX, dollar_at) |
| 107 | @$(LATEXC) $< |
| 108 | ifeq ($(LATEX_MAKE_GLOSSARIES),1) |
| 109 | status_log(GLOSSARY, dollar_at) |
| 110 | @makeglossaries $(patsubst %.tex,%,$<) |
| 111 | @$(LATEXC) $< |
| 112 | endif')') |
swissChili | 2b5acc8 | 2021-03-13 17:06:42 -0800 | [diff] [blame] | 113 | |
swissChili | 8cfb7c4 | 2021-04-18 21:17:58 -0700 | [diff] [blame] | 114 | define(option, |
| 115 | `$1 ?= $3 |
| 116 | jmk_options += $1 |
| 117 | jmk_option_help_$1 = $2 (default: $3)') |
| 118 | |
swissChili | 2b5acc8 | 2021-03-13 17:06:42 -0800 | [diff] [blame] | 119 | dnl finish is required at the end of the Jmk file to generate some |
| 120 | dnl final declarations |
| 121 | |
| 122 | define(finish, |
swissChili | e849174 | 2021-04-08 20:38:06 -0700 | [diff] [blame] | 123 | `clean: $(jmk_clean_libs) |
swissChili | fbf525f | 2021-08-04 21:28:07 -0700 | [diff] [blame] | 124 | @rm -f **/*.o **/*.a *.so $(jmk_target) $(OBJECTS) $(LATEX_EXTRAS) |
swissChili | 2b5acc8 | 2021-03-13 17:06:42 -0800 | [diff] [blame] | 125 | |
| 126 | Makefile: Jmk |
swissChili | e849174 | 2021-04-08 20:38:06 -0700 | [diff] [blame] | 127 | status_log(JMK, jmk_build_dir) |
| 128 | @cd "jmk_build_dir" && jmk_build_cmd |
swissChili | 2b5acc8 | 2021-03-13 17:06:42 -0800 | [diff] [blame] | 129 | |
swissChili | d98781b | 2021-07-25 21:04:17 -0700 | [diff] [blame] | 130 | gtags_path: |
| 131 | status_log(GTAGS,) |
| 132 | gen_gtags |
| 133 | |
| 134 | show_cflags: |
| 135 | @echo $(CFLAGS) |
| 136 | |
| 137 | .PHONY: $(jmk_libs_phony) $(jmk_custom_phony) $(jmk_clean_libs) clean all show_cflags') |
swissChili | 2b5acc8 | 2021-03-13 17:06:42 -0800 | [diff] [blame] | 138 | |
| 139 | divert(0)dnl |