3 # Separate command line arguments into options and source files
7 proc usage {{msg {}}} {
8 puts stderr "Usage: build-jim-ext ?--notest? ?--cross? ?--install? ?--static? ?cc-options? ?-o modname? sources..."
17 set cross @cross_compiling@
25 for {set i 0} {$i < [llength $argv]} {incr i} {
26 set arg [lindex $argv $i]
27 switch -glob -- $arg {
36 # Don't test to see if the module can be loaded
40 # Don't use standard include/lib paths if cross compiling
44 # Install to $DESTDIR/@prefix@/lib/jim
48 # Build a static extension that can be linked
55 # Don't remove intermediate files
59 usage "Easily builds dynamic (loadable) modules for jim"
63 set modname [file rootname [lindex $argv $i]]
65 usage "Option -o requires an argument"
69 lappend includepaths $arg
71 lappend includepaths [lindex $argv $i]
77 lappend libpaths [lindex $argv $i]
87 usage "Unexpected '$arg'"
93 usage "No sources provided"
95 if {![info exists modname]} {
96 set modname [file rootname [file tail [lindex $sources 0]]]
97 # Remove jim- prefix if one exists
98 regsub "^jim-" $modname "" modname
102 set target libjim-$modname.a
104 set target $modname.so
106 puts "Building $target from $sources\n"
109 # If not cross compiling, add the standard location after any user include paths
110 lappend includepaths -I@prefix@/include
113 # Work around Tcl's strange behaviour of exec failing if stderr is produced
115 proc exec-catch {verbose cmdlist} {
124 # Handle failed case.
125 # Note that Tcl returns rc=1 if there is any stderr,
126 # even if the exit code is 0
128 if {[dict get $errinfo -errorcode] ne "NONE"} {
130 puts stderr [join $cmdlist]
142 set CPPFLAGS "-D_GNU_SOURCE"
148 set shobj_cflags "@SHOBJ_CFLAGS@"
149 if {"@JIM_STATICLIB@" eq "1"} {
150 puts stderr "Warning: libjim is static. Dynamic module may not work on some platforms.\n"
151 set shobj_ldflags "@SHOBJ_LDFLAGS@"
153 # If shared, link against the shared libjim to resolve symbols
155 set shobj_ldflags "@SHOBJ_LDFLAGS_R@"
160 foreach source $sources {
161 set obj [file rootname [file tail $source]].o
162 if {[string match *.c $source]} {
167 set compile "$compiler @CFLAGS@ $CPPFLAGS $shobj_cflags $includepaths $opts -c -o $obj $source"
171 set rc [exec-catch $verbose $compile]
179 set ar "@AR@ cq $target $objs"
180 set ranlib "@RANLIB@ $target"
183 set rc [exec-catch $verbose $ar]
185 set rc [exec-catch $verbose $ranlib]
194 # If not cross compiling, add the standard location after any user lib paths
195 lappend libpaths -L@prefix@/lib
198 set link "$linker @CFLAGS@ @LDFLAGS@ $shobj_ldflags $libpaths $opts -o $target $objs $ljim @LIBS@ $libs"
201 set rc [exec-catch $verbose $link]
210 if {$testmod && !$cross} {
211 # Now, is testing even possible?
212 # We must be running a compatible jimsh with the load command at least
215 # This will avoid attempting on Tcl and on jimsh without load
216 if {[info version] > 0.73 && [exists -command load]} {
223 if {$testmod && !$cross} {
224 puts "Test: load $target"
228 set dest [env DESTDIR ""]@prefix@/lib/jim
229 puts "Install: $target => $dest"
231 file copy $target $dest/$target