-
Notifications
You must be signed in to change notification settings - Fork 123
/
build-jim-ext.in
239 lines (221 loc) · 4.72 KB
/
build-jim-ext.in
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/usr/bin/env jimsh
# Separate command line arguments into options and source files
set opts {}
set sources {}
proc usage {{msg {}}} {
puts stderr "Usage: build-jim-ext ?--notest? ?--cross? ?--install? ?--static? ?cc-options? ?-o modname? sources..."
if {$msg ne ""} {
puts stderr \n$msg
}
exit 1
}
set linker "@CC@"
set testmod 1
set cross @cross_compiling@
set install 0
set static 0
set verbose 0
set keep 0
set includepaths {}
set libpaths {}
set libs {}
for {set i 0} {$i < [llength $argv]} {incr i} {
set arg [lindex $argv $i]
switch -glob -- $arg {
*.c {
lappend sources $arg
}
*.cpp {
lappend sources $arg
set linker "@CXX@"
}
--notest {
# Don't test to see if the module can be loaded
set testmod 0
}
--cross {
# Don't use standard include/lib paths if cross compiling
set cross 1
}
--install {
# Install to $DESTDIR/@libdir@/jim
set install 1
}
--static {
# Build a static extension that can be linked
set static 1
}
--verbose {
set verbose 1
}
--keep {
# Don't remove intermediate files
set keep 1
}
--help {
usage "Easily builds dynamic (loadable) modules for jim"
}
-o {
incr i
set modname [file rootname [lindex $argv $i]]
if {$modname eq ""} {
usage "Option -o requires an argument"
}
}
-I* {
lappend includepaths $arg
if {$arg eq "-I"} {
lappend includepaths [lindex $argv $i]
}
}
-L* {
lappend libpaths $arg
if {$arg eq "-L"} {
lappend libpaths [lindex $argv $i]
}
}
-l* {
lappend libs $arg
}
-* {
lappend opts $arg
}
default {
usage "Unexpected '$arg'"
}
}
}
if {$sources eq ""} {
usage "No sources provided"
}
if {![info exists modname]} {
set modname [file rootname [file tail [lindex $sources 0]]]
# Remove jim- prefix if one exists
regsub "^jim-" $modname "" modname
}
if {$static} {
set target libjim-$modname.a
} else {
set target $modname.so
}
puts "Building $target from $sources\n"
if {!$cross} {
# If not cross compiling, add the standard location after any user include paths
lappend includepaths -I@prefix@/include
}
# Work around Tcl's strange behaviour of exec failing if stderr is produced
#
proc exec-catch {verbose cmdlist} {
if {$verbose} {
puts [join $cmdlist]
}
flush stdout
set rc [catch {
exec {*}$cmdlist
} msg errinfo]
# Handle failed case.
# Note that Tcl returns rc=1 if there is any stderr,
# even if the exit code is 0
if {$rc} {
if {[dict get $errinfo -errorcode] ne "NONE"} {
if {!$verbose} {
puts stderr [join $cmdlist]
}
puts stderr $msg
return 1
}
}
if {$msg ne ""} {
puts stderr $msg
}
return 0
}
set CPPFLAGS "-D_GNU_SOURCE"
set ljim ""
set shobj_cflags ""
set shobj_ldflags ""
if {!$static} {
set shobj_cflags "@SHOBJ_CFLAGS@"
if {"@JIM_STATICLIB@" eq "1"} {
puts stderr "Warning: libjim is static. Dynamic module may not work on some platforms.\n"
set shobj_ldflags "@SHOBJ_LDFLAGS@"
} else {
# If shared, link against the shared libjim to resolve symbols
set ljim -ljim
set shobj_ldflags "@SHOBJ_LDFLAGS_R@"
}
}
set objs {}
foreach source $sources {
set obj [file rootname [file tail $source]].o
if {[string match *.c $source]} {
set compiler "@CC@"
} else {
set compiler "@CXX@"
}
set compile "$compiler @CFLAGS@ $CPPFLAGS $shobj_cflags $includepaths $opts -c -o $obj $source"
puts "Compile: $obj"
lappend objs $obj
set rc [exec-catch $verbose $compile]
if {$rc} {
file delete {*}$objs
exit $rc
}
}
if {$static} {
set ar "@AR@ cq $target $objs"
set ranlib "@RANLIB@ $target"
puts "Ar: $target"
set rc [exec-catch $verbose $ar]
if {$rc == 0} {
set rc [exec-catch $verbose $ranlib]
}
file delete {*}$objs
if {$rc} {
file delete $target
exit $rc
}
} else {
if {!$cross} {
# If not cross compiling, add the standard location after any user lib paths
lappend libpaths -L@prefix@/lib
}
set link "$linker @CFLAGS@ @LDFLAGS@ $shobj_ldflags $libpaths $opts -o $target $objs $ljim @LIBS@ $libs"
puts "Link: $target"
set rc [exec-catch $verbose $link]
if {!$keep} {
file delete {*}$objs
}
if {$rc} {
file delete $target
exit $rc
}
if {$testmod && !$cross} {
# Now, is testing even possible?
# We must be running a compatible jimsh with the load command at least
set testmod 0
set rc [catch {
# This will avoid attempting on Tcl and on jimsh without load
if {[info version] > 0.73 && [exists -command load]} {
set testmod 1
}
} msg]
}
set rc [catch {
if {$testmod && !$cross} {
puts "Test: load $target"
load ./$target
}
if {$install} {
set dest [env DESTDIR ""]@libdir@/jim
puts "Install: $target => $dest"
file mkdir $dest
file copy $target $dest/$target
}
puts "\nSuccess!"
} msg]
if {$rc} {
puts stderr $msg
exit 1
}
}