Differences From
Artifact [3c50b488d5]:
1 1 #- modmake
2 2 # this is the master makefile that controls the building of each
3 3 # libk module. it is included from each k*/makefile.
4 4 # vim: ft=make
5 5
6 6 mod = $(notdir $(PWD))
7 -src = $(wildcard *.c) $(wildcard *.s)
7 +src = $(wildcard *.c) $(wildcard *.s) $(filter-out %.h,$(patsubst %.m,%,$(wildcard *.m)))
8 8 bare = $(mod:k%=%)
9 -headers = $(wildcard *.h) $(gen-headers)
9 +headers = $(wildcard *.h) $(gen-headers) $(patsubst %.m,%,$(wildcard *.h.m))
10 10
11 11 tools = $(filter %.exe.c, $(src))
12 12 nontools = $(filter-out %.exe.c, $(src))
13 13 cobjects = $(filter %.c, $(nontools))
14 14 sobjects = $(filter %.${TARGET}.s, $(nontools))
15 15
16 -cflags = -isystem ${OUT} -fPIC -nostdlib ${COMPLIB} -L${OUT} -lk
16 +gpp = gpp
17 +cflags = -std=c11 -isystem ${OUT} -fPIC -nostdlib ${COMPLIB} -L${OUT}
18 +
19 +m-env = atom_target_arch=${ARCH}
20 +m-env += atom_target_os=${OS}
21 +ifneq (${BITS},) #!!! ifdef does NOT work with environment variables
22 + m-env += atom_target_bits=${BITS}
23 +endif
24 +m-env += target_posix=${POSIX}
25 +m-env += target_unix=${UNIX}
26 +
27 +m-grammar = $(file < ${TMP}/precomp.g)
28 +m-comp = $(gpp) $(m-grammar) $(m-env:%=-D%)
17 29
18 30 obj: $(cobjects:%.c=${OUT}/$(mod).%.o) \
19 31 $(sobjects:%.s=${OUT}/$(mod).%.o)
20 32 tool: $(tools:%.exe.c=${OUT}/$(mod).%) \
21 33 ${OUT}/libk.a
22 34
23 35 def: $(headers:%=${OUT}/k/%)
24 36
25 37 dbg:
38 + @echo src = $(src)
26 39 @echo tools = $(tools)
27 40 @echo TARGET = ${TARGET}
28 41 @echo cobjects = $(cobjects)
29 42 @echo sobjects = $(sobjects)
30 43 @echo headers = $(headers)
44 + @echo m-comp = $(m-comp)
45 + @echo m-grammar = $(m-grammar)
46 + @echo m-env = $(m-env) "$(m-env:%=-D%)"
31 47 @echo mod = $(mod)
32 48
33 -${OUT}/$(mod).%.o: %.c
49 +${OUT}/k/%.h: %.h.m
50 + $(m-comp) $< > $@
51 +
52 +.PRECIOUS: ${TMP}/$(mod).%
53 +${TMP}/$(mod).%: %.m ${TMP}
54 + $(m-comp) $< > $@
55 +
56 +${OUT}/$(mod).%.o: ${TMP}/$(mod).%.c
57 + $(CC) $(cflags) -c $< -o $@
58 +
59 +${OUT}/$(mod).%.o: %.c $(bare).h
34 60 $(CC) $(cflags) -c $< -o $@
35 61
36 62 ${OUT}/k/%.h: %.h
37 63 cp $< $@
38 64
39 65 ${OUT}/$(mod).%: %.exe.c
40 - $(CC) $(cflags) $< -o $@
66 + $(CC) $(cflags) $< ${OUT}/libk.a -o $@
41 67
42 68 ${TMP}:
43 69 mkdir -p ${TMP}
44 70
45 71 #- assembly
46 72 # compiling the assembly code will be faster but a lot more
47 73 # complex, given the nature of assembly and the large number of
................................................................................
50 76 # that requires ugly make rules, we're just going to use a
51 77 # function to generate these.
52 78
53 79 # ${OUT} = ultimate build directory
54 80 # $(mod) = module name
55 81 # % = function name
56 82 # $(1) = arch tuple
57 -arch = ${OUT}/$(mod).%.$(1).o: %.$(1).s
83 +arch = ${OUT}/$(mod).%.$(1).o: $2%.$(1).s
58 84 # invoke with $(call arch,tuple). do not
59 85 # put spaces between either term though!
60 86
61 87 ifeq ($(debug),yes)
62 88 yasm-flags = -gdwarf2
63 89 endif
64 90
91 +yasm = yasm $(yasm-flags) -f$1 -i${TMP} $< -o $@
92 +
65 93 #-- linux
66 94 # linux uses the ELF{32,64} binary format, and generating these
67 95 # from yasm is trivial. linux only supports one ABI per format,
68 96 # at least with ELF, so that's all we need to do.
69 97
70 -#${OUT}/$(mod).%.x86.lin.32.o: %.x86.lin.32.s
71 -$(call arch,x86.lin.32)
72 - yasm $(yasm-flags) -felf32 -i${TMP} $< -o $@
98 +
99 +$(call arch,x86.lin.32,)
100 + $(call yasm,elf32)
101 +
102 +$(call arch,x86.lin.64,)
103 + $(call yasm,elf64)
104 +
105 +$(call arch,x86.lin.32,${TMP}/$(mod).)
106 + $(call yasm,elf32)
73 107
74 -#${OUT}/$(mod).%.x86.lin.64.o: %.x86.lin.64.s
75 -$(call arch,x86.lin.64)
76 - yasm $(yasm-flags) -felf64 -i${TMP} $< -o $@
108 +$(call arch,x86.lin.64,${TMP}/$(mod).)
109 + $(call yasm,elf64)
77 110
78 111 #-- freebsd
79 112 # the freebsd ABI is different, so it will require different code
80 113 # (though there might be ways to minimize that). freebsd uses the
81 114 # same binary format as Linux (though it also supports a.out and
82 115 # COFF) but because freebsd can interpret multiple different ABIs
83 116 # the object files need to be "branded" with the correct one
84 117 # using the tool brandelf (`brandelf -t [ABI]`)
85 118
86 -$(call arch,x86.fbsd.32)
87 - yasm -felf32 $< -o $@
119 +$(call arch,x86.fbsd.32,)
120 + $(call yasm,elf32)
121 + brandelf -t FreeBSD $@
122 +
123 +$(call arch,x86.fbsd.64,)
124 + $(call yasm,elf64)
88 125 brandelf -t FreeBSD $@
89 126
90 -$(call arch,x86.fbsd.64)
91 - yasm -felf64 $< -o $@
127 +$(call arch,x86.fbsd.32,${TMP}/$(mod).)
128 + $(call yasm,elf32)
92 129 brandelf -t FreeBSD $@
93 130
131 +$(call arch,x86.fbsd.64,${TMP}/$(mod).)
132 + $(call yasm,elf64)
133 + brandelf -t FreeBSD $@