3 # Separate command line arguments into options and source files
7 proc usage {{msg {}}} {
8 puts stderr "Usage: build-jim-ext ?--notest? ?--install? ?--static? ?cc-options? ?-o modname? sources..."
15 proc readfile {filename {default_value ""}} {
16 set result $default_value
18 set f [open $filename]
19 set result [$f read -nonewline]
34 for {set i 0} {$i < [llength $argv]} {incr i} {
35 set arg [lindex $argv $i]
36 switch -glob -- $arg {
60 usage "Easily builds dynamic (loadable) modules for jim"
64 set modname [file rootname [lindex $argv $i]]
66 usage "Option -o requires an argument"
70 lappend includepaths $arg
72 lappend includepaths [lindex $argv $i]
78 lappend libpaths [lindex $argv $i]
88 usage "Unexpected '$arg'"
94 usage "No sources provided"
96 if {![info exists modname]} {
97 set modname [file rootname [file tail [lindex $sources 0]]]
98 # Remove jim- prefix if one exists
99 regsub "^jim-" $modname "" modname
103 set target libjim-$modname.a
105 set target $modname.so
107 puts "Building $target from $sources\n"
109 # Now add the standard location after any user include paths
110 lappend includepaths -I@prefix@/include
112 set CPPFLAGS "-D_GNU_SOURCE"
118 set shobj_cflags "@SHOBJ_CFLAGS@"
119 if {"@JIM_STATICLIB@" eq "1"} {
120 puts stderr "Warning: libjim is static. Dynamic module may not work on some platforms.\n"
121 set shobj_ldflags "@SHOBJ_LDFLAGS@"
123 # If shared, link against the shared libjim to resolve symbols
125 set shobj_ldflags "@SHOBJ_LDFLAGS_R@"
130 foreach source $sources {
131 set obj [file rootname [file tail $source]].o
132 if {[string match *.c $source]} {
137 set compile "$compiler @CFLAGS@ $CPPFLAGS $shobj_cflags $includepaths $opts -c -o $obj $source"
145 exec 2>jimerr.out {*}$compile
148 set errmsg [readfile jimerr.out]
149 file delete jimerr.out
169 set ar "@AR@ cq $target $objs"
170 set ranlib "@RANLIB@ $target"
192 # Add the standard location after any user lib paths
193 lappend libpaths -L@prefix@/lib
195 set link "$linker @CFLAGS@ @LDFLAGS@ $shobj_ldflags $libpaths $opts -o $target $objs $ljim @LIBS@ $libs"
202 exec 2>jimerr.out {*}$link
205 set errmsg [readfile jimerr.out]
206 file delete jimerr.out
226 # Now, is testing even possible?
227 # We must be running a compatible jimsh with the load command at least
230 # This will avoid attempting on Tcl and on jimsh without load
231 # How to tell if we are cross compiling?
232 if {[info version] > 0.73 && [exists -command load]} {
240 puts "Test: load $target"
244 set dest [env DESTDIR ""]@prefix@/lib/jim
245 puts "Install: $target => $dest"
247 file copy $target $dest/$target