blob: d1de80589e322d1d52e380e64038456dedf83576 [file] [log] [blame]
swissChili0fc3f262021-08-09 22:05:17 -07001# Rewrite of JMK in TCL
2
3variable jmk_name {}
4variable jmk_target {}
5variable cflags {}
6variable asmflags {}
7
8variable asm as
9variable cc cc
10
swissChili35558852022-07-02 18:15:45 -070011# variable options
12
13variable src \$^
14variable first_src \$<
15variable target \$@
swissChili0fc3f262021-08-09 22:05:17 -070016
17proc init {name {target {DEFAULT_TARGET}}} {
18 if {$target eq {DEFAULT_TARGET}} {
19 set target $name
20 }
21
22 global jmk_name
23 global jmk_target
24
25 set jmk_name $name
26 set jmk_target $target
swissChili35558852022-07-02 18:15:45 -070027
28 puts {MAKEFILE_DEPTH ?= 1}
29
30 rule all $target {}
swissChili0fc3f262021-08-09 22:05:17 -070031}
32
33proc preset {p} {
34 ::preset::$p
35}
36
37proc presets {args} {
38 foreach arg $args {
39 preset $arg
40 }
41}
42
43proc cflag {arg} {
44 global cflags
45 set cflags "$cflags $arg"
46}
47
48proc cflags {args} {
49 foreach arg $args {
50 cflag $arg
51 }
52}
53
54proc asmflag {arg} {
55 global asmflags
56 set asmflags "$asmflags $arg"
57}
58
59proc asmflags {args} {
60 foreach arg $args {
61 asmflag $arg
62 }
63}
64
65proc option {name val} {
66 global options
67 if {![info exists options($name)]} {
68 set options($name) $val
69 }
70}
71
swissChili35558852022-07-02 18:15:45 -070072proc log {category message} {
73 puts "\t@printf '\\e\[1;34m%8s\\e\[m %s\\n' '$category' '$message' > /dev/stderr"
74}
75
76proc cc {command} {
77 puts "\t@$::cc $command $::cflags"
78}
79
80proc asm {command} {
81 puts "\t@$::asm $command $::asmflags"
82}
83
84proc shell {command} {
85 puts "\t@$command"
86}
87
88proc rule {target deps does} {
89 puts ""
90 puts "$target: $deps"
91 eval $does
92}
93
94proc type {type} {
95 ::type::$type
96}
97
98proc srcs {args} {
99 puts ""
100 variable objs ""
101
102 foreach src $args {
103 variable obj [regsub -- {(.+)\.\w+} $src {\1.o}]
104 variable objs "$objs $obj"
105 }
106
107 puts "OBJECTS += $objs"
108}
109
110namespace eval type {
111 proc executable {} {
112 global jmk_target
113
114 rule $jmk_target "\$(OBJECTS)" {
115 log LD $::target
116 cc "-o $::target $::src"
117 }
118
119 helpers
120 }
121
122proc helpers {} {
123 rule .c.o {} {
124 log CC $::first_src
125 cc "-c $::first_src -o $::target"
126 }
127
128 rule .s.o {} {
129 log ASM $::first_src
130 asm "\$(ASMFLAGS) $::first_src -o $::target"
131 }
132
133 rule clean {} {
134 shell "rm -f **/*.o **/*.a *.so $::target \$(OBJECTS)"
135 }
136}
137}
138
swissChili0fc3f262021-08-09 22:05:17 -0700139namespace eval preset {
swissChili35558852022-07-02 18:15:45 -0700140 proc freestanding {} {
141 cflags -nostdlib -nostdinc -fno-builtin -fno-stack-protector -ffreestanding
142 }
143
144 proc optimize {} {
145 cflags -O2
146 }
147
swissChili0fc3f262021-08-09 22:05:17 -0700148 proc 32 {} {
149 cflag -m32
150 asmflag -felf32
151 }
152
153 proc debug {} {
154 cflag -g
155 asmflag -Fdwarf
156 }
157
158 proc warn {} {
swissChili35558852022-07-02 18:15:45 -0700159 cflags -Wall -Wextra -Wno-unused-function -Wno-unused-variable -Wno-incompatible-pointer-types -Werror
swissChili0fc3f262021-08-09 22:05:17 -0700160 }
161
162 proc nasm {} {
163 global asm
164 set asm nasm
165 }
166}