blob: b3b860c1fe06ca4e4f330f3d34263bb7775440f2 [file] [log] [blame]
swissChili0fc3f262021-08-09 22:05:17 -07001# Rewrite of JMK in TCL
2
3variable jmk_name {}
4variable jmk_target {}
swissChilif3d65762022-07-03 22:34:54 -07005variable jmk_clean_libs {}
6variable jmk_phony_libs {}
7variable jmk_lib_paths
8variable jmk_lib_targets
9
swissChili0fc3f262021-08-09 22:05:17 -070010variable cflags {}
11variable asmflags {}
swissChilif3d65762022-07-03 22:34:54 -070012variable ldflags {}
swissChili0fc3f262021-08-09 22:05:17 -070013
14variable asm as
swissChilif3d65762022-07-03 22:34:54 -070015variable cc gcc
16variable ld ld
17
18variable objs {}
swissChili0fc3f262021-08-09 22:05:17 -070019
swissChili35558852022-07-02 18:15:45 -070020# variable options
21
22variable src \$^
23variable first_src \$<
24variable target \$@
swissChili0fc3f262021-08-09 22:05:17 -070025
26proc init {name {target {DEFAULT_TARGET}}} {
27 if {$target eq {DEFAULT_TARGET}} {
28 set target $name
29 }
30
31 global jmk_name
32 global jmk_target
33
34 set jmk_name $name
35 set jmk_target $target
swissChili35558852022-07-02 18:15:45 -070036
37 puts {MAKEFILE_DEPTH ?= 1}
38
39 rule all $target {}
swissChilif3d65762022-07-03 22:34:54 -070040
41 rule Makefile Jmk2 {
42 log JMK2 ""
43 shell "cd $::jmk_build_dir && $::jmk_build_cmd"
44 }
swissChili0fc3f262021-08-09 22:05:17 -070045}
46
47proc preset {p} {
48 ::preset::$p
49}
50
51proc presets {args} {
52 foreach arg $args {
53 preset $arg
54 }
55}
56
57proc cflag {arg} {
58 global cflags
59 set cflags "$cflags $arg"
60}
61
62proc cflags {args} {
63 foreach arg $args {
64 cflag $arg
65 }
66}
67
68proc asmflag {arg} {
69 global asmflags
70 set asmflags "$asmflags $arg"
71}
72
73proc asmflags {args} {
74 foreach arg $args {
75 asmflag $arg
76 }
77}
78
swissChilif3d65762022-07-03 22:34:54 -070079proc ldflag {arg} {
80 global ldflags
81 set ldflags "$ldflags $arg"
82}
83
84proc ldflags {args} {
85 foreach arg $args {
86 ldflag $arg
87 }
88}
89
swissChili0fc3f262021-08-09 22:05:17 -070090proc option {name val} {
91 global options
92 if {![info exists options($name)]} {
93 set options($name) $val
94 }
95}
96
swissChili35558852022-07-02 18:15:45 -070097proc log {category message} {
swissChilif3d65762022-07-03 22:34:54 -070098 puts "\t@printf ' \\e\[1;34m%8s\\e\[m %s\\n' '$category' '$message' > /dev/stderr"
swissChili35558852022-07-02 18:15:45 -070099}
100
101proc cc {command} {
102 puts "\t@$::cc $command $::cflags"
103}
104
105proc asm {command} {
106 puts "\t@$::asm $command $::asmflags"
107}
108
109proc shell {command} {
110 puts "\t@$command"
111}
112
swissChilif3d65762022-07-03 22:34:54 -0700113proc make {command} {
114 shell "\$(MAKE) --no-print-directory MAKEFILE_DEPTH=\$\$((\$(MAKEFILE_DEPTH)+1)) $command"
115}
116
swissChili35558852022-07-02 18:15:45 -0700117proc rule {target deps does} {
118 puts ""
119 puts "$target: $deps"
swissChilif3d65762022-07-03 22:34:54 -0700120 uplevel 1 $does
swissChili35558852022-07-02 18:15:45 -0700121}
122
123proc type {type} {
124 ::type::$type
125}
126
swissChilif3d65762022-07-03 22:34:54 -0700127proc objs {args} {
128 foreach obj $args {
129 set ::objs "$::objs $obj"
130 }
131}
132
swissChili35558852022-07-02 18:15:45 -0700133proc srcs {args} {
134 puts ""
135 variable objs ""
136
137 foreach src $args {
138 variable obj [regsub -- {(.+)\.\w+} $src {\1.o}]
swissChilif3d65762022-07-03 22:34:54 -0700139 set ::objs "$::objs $obj"
140 }
141}
142
143proc depends {name path {target DEFAULT_TARGET}} {
144 if {$target eq {DEFAULT_TARGET}} {
145 variable target "lib${name}.a"
swissChili35558852022-07-02 18:15:45 -0700146 }
147
swissChilif3d65762022-07-03 22:34:54 -0700148 set ::jmk_clean_libs "$::jmk_clean_libs $path"
149 set ::jmk_lib_paths($name) $path
150 set ::jmk_lib_target($name) $target
151 set ::jmk_phony_libs "$::jmk_phony_libs $path"
152
153 rule "$path/$target" {} {
154 log "MAKE\[\$(MAKEFILE_DEPTH)\]" "Entering $name"
155 make "-C $path $target"
156 log "MAKE\[\$(MAKEFILE_DEPTH)\]" "Leaving $name"
157 }
158}
159
160proc lib {name} {
161 return "$::jmk_lib_paths($name)/$::jmk_lib_target($name)"
swissChili35558852022-07-02 18:15:45 -0700162}
163
164namespace eval type {
165 proc executable {} {
166 global jmk_target
167
swissChilif3d65762022-07-03 22:34:54 -0700168 rule $jmk_target $::objs {
swissChili35558852022-07-02 18:15:45 -0700169 log LD $::target
170 cc "-o $::target $::src"
171 }
172
173 helpers
174 }
175
swissChilif3d65762022-07-03 22:34:54 -0700176proc custom_link {} {
177 global jmk_target
178
179 rule $jmk_target $::objs {
180 log LD $::target
181 shell "$::ld $::ldflags -o $::target $::src"
182 }
183
184 helpers
185}
186
swissChili35558852022-07-02 18:15:45 -0700187proc helpers {} {
188 rule .c.o {} {
189 log CC $::first_src
190 cc "-c $::first_src -o $::target"
191 }
192
193 rule .s.o {} {
194 log ASM $::first_src
195 asm "\$(ASMFLAGS) $::first_src -o $::target"
196 }
197
198 rule clean {} {
swissChilif3d65762022-07-03 22:34:54 -0700199 shell "rm -f **/*.o **/*.a *.so $::jmk_target $::objs"
200
201 foreach lib $::jmk_clean_libs {
202 make "-C $lib clean"
203 }
swissChili35558852022-07-02 18:15:45 -0700204 }
swissChilif3d65762022-07-03 22:34:54 -0700205}
swissChili35558852022-07-02 18:15:45 -0700206}
207
swissChili0fc3f262021-08-09 22:05:17 -0700208namespace eval preset {
swissChili35558852022-07-02 18:15:45 -0700209 proc freestanding {} {
210 cflags -nostdlib -nostdinc -fno-builtin -fno-stack-protector -ffreestanding
211 }
212
213 proc optimize {} {
214 cflags -O2
215 }
216
swissChili0fc3f262021-08-09 22:05:17 -0700217 proc 32 {} {
218 cflag -m32
219 asmflag -felf32
220 }
221
222 proc debug {} {
223 cflag -g
224 asmflag -Fdwarf
225 }
226
227 proc warn {} {
swissChilif3d65762022-07-03 22:34:54 -0700228 cflags -Wall -Wno-unused-function -Wno-unused-variable -Wno-incompatible-pointer-types -Wno-sign-compare
swissChili0fc3f262021-08-09 22:05:17 -0700229 }
230
231 proc nasm {} {
232 global asm
233 set asm nasm
234 }
235}