-
Notifications
You must be signed in to change notification settings - Fork 0
/
tm_ext_cmds.tcl
186 lines (157 loc) · 3.53 KB
/
tm_ext_cmds.tcl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
set TM_PLATFORM "$TM_OPSYS-$TM_MACHINE_ARCH"
# Check if a variable var is defined
proc defined {var} {
if {[uplevel "info exists $var"]} {
return 1
} else {
return 0
}
}
# True if target is the current goal
proc make {target} {
global TM_CURRENT_GOAL
if {"$target" eq "$TM_CURRENT_GOAL"} {
return 1
} else {
return 0
}
}
# True if operand is the empty string
proc empty {operand} {
if {[string length $operand]} {
return 0
} else {
return 1
}
}
# Check if an array a contains a value for key
proc array_has {a key} {
if {[string length [uplevel "array get $a $key"]]} {
return 1
} else {
return 0
}
}
# Set a global parameter var to val using mode mode
proc param {var val} {
global TM_PARAM
global TM_ENV_LOOKUP
global env
if {[array_has TM_PARAM $var]} {
uplevel "set $var {$TM_PARAM($var)}"
} elseif {[info exists TM_ENV_LOOKUP]} {
uplevel "set $var {$env($var)}"
} else {
uplevel "set $var {$val}"
}
}
# Clean up a list of options to make it suitable for exec
proc options {str} {
set OPTIONS [split $str "\n"]
set TRIMOPTS {}
foreach OPT $OPTIONS {
set trim [string trim $OPT]
if {[string length $trim]} {
lappend TRIMOPTS $trim
}
}
return $TRIMOPTS
}
rename exec tcl::exec
proc exec args {
global TM_NO_EXECUTE
global TM_SILENT_MODE
set flags ""
set echo 1
set errexit 1
set noexec 0
set start 0
if {[defined TM_NO_EXECUTE]} {
set noexec $TM_NO_EXECUTE
}
if {[llength args] == 0} {
return "";
}
if {"[lindex $args 0]" eq "-flags"} {
if {[llength $args] < 2} {
error "No flags provided to -flags"
}
set flags [lindex $args 1]
set start 2
}
for {set i 0} {$i < [string length $flags]} {incr i} {
switch [string index $flags $i] {
"@" {set echo 0}
"-" {set errexit 0}
"+" {set noexec 0}
default {error "Unknown flag given to exec: [string index $i]"}
}
}
set rest [lrange $args $start end]
if {$echo && ![defined TM_SILENT_MODE]} {
# Do it this way to avoid having Tcl escape quotes and such
# in the output.
foreach arg [lrange $rest 0 end-1] {
puts -nonewline "$arg "
}
puts "[lrange $rest end end]"
flush stdout
}
if {$noexec} {
return ""
}
if {![defined TM_SILENT_MODE]} {
set childpid [tcl::exec {*}$rest &]
set status [os.wait $childpid] ;# TODO: This might not work on Windows...
if {"[lindex $status 1]" eq "exit" && [lindex $status 2] != 0} {
if {$errexit} {
error "exec returned non-zero return code: $status"
}
} elseif {"[lindex $status 1]" ne "exit"} {
if {$errexit} {
error "exec terminated abnormally: $status"
}
}
} else {
try {
tcl::exec {*}$rest
} on CHILDSTATUS {pid code} {
error "exec returned non-zero return code: $code"
} on CHILDKILLED {pid sig msg} {
error "exec terminated abnormally: $msg"
} on CHILDSUSP {pid sig msg} {
error "exec suspended: $msg"
}
}
return ""
}
# Take a list of filenames and replace the extensions that match x with y
proc replace-ext {files x y} {
set ys {}
foreach f $files {
if {"[file extension $f]" eq "$x"} {
lappend ys "[file rootname $f]$y"
} else {
lappend ys $f
}
}
return $ys
}
# Define a substitution rule. Returns a list of all the targets created.
proc sub {from to recipe} {
set OUT {}
foreach in [glob *$from] {
set out [replace-ext $in $from $to]
rule $out $in $recipe
lappend OUT $out
}
return $OUT
}
# Create a list of filenames with dir prepended to a given list of filenames
proc in-dir {dir files} {
set newfiles {}
foreach f $files {
lappend newfiles [file join $dir $f]
}
return $newfiles
}