summaryrefslogtreecommitdiff
path: root/build-jim-ext.in
blob: 087ec23a593888051717bd4f4d43fe72bbcb0420 (plain)
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#!/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? ?--install? ?--static? ?cc-options? ?-o modname? sources..."
	if {$msg ne ""} {
		puts stderr \n$msg
	}
	exit 1
}

proc readfile {filename {default_value ""}} {
	set result $default_value
	catch {
		set f [open $filename]
		set result [$f read -nonewline]
		$f close
	}
	return $result
}

set linker "@CC@"
set testmod 1
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 {
			set testmod 0
		}
		--install {
			set install 1
		}
		--static {
			set static 1
		}
		--verbose {
			set verbose 1
		}
		--keep {
			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"

# Now add the standard location after any user include paths
lappend includepaths -I@prefix@/include

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
	flush stdout
	set rc [catch {
		if {$verbose} {
			puts $compile
		}
		exec 2>jimerr.out {*}$compile
	} msg]

	set errmsg [readfile jimerr.out]
	file delete jimerr.out

	if {$rc} {
		if {!$verbose} {
			puts stderr $compile
		}
		puts stderr $msg
		if {$errmsg ne ""} {
			puts stderr $errmsg
		}
		file delete {*}$objs
		exit 1
	} else {
		if {$errmsg ne ""} {
			puts $errmsg
		}
	}
}

if {$static} {
	set ar "@AR@ cq $target $objs"
	set ranlib "@RANLIB@ $target"

	puts "Ar:      $target"
	set rc [catch {
		file delete $target
		exec {*}$ar
		exec {*}$ranlib
		if {$verbose} {
			puts stderr $ar
		}
	} msg]

	file delete {*}$objs

	if {$rc} {
		puts stderr $ar
		puts stderr $ranlib
		puts stderr $msg
		file delete $target
		exit 1
	}
} else {
	# 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 [catch {
		if {$verbose} {
			puts stderr $link
		}
		exec 2>jimerr.out {*}$link
	} msg]

	set errmsg [readfile jimerr.out]
	file delete jimerr.out

	if {!$keep} {
		file delete {*}$objs
	}

	if {$rc} {
		file delete $target
		puts stderr $link
		puts stderr $msg
		if {$errmsg ne ""} {
			puts stderr $errmsg
		}
		exit 1
	}
	if {$errmsg ne ""} {
		puts $errmsg
	}

	if {$testmod} {
		# 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
			# How to tell if we are cross compiling?
			if {[info version] > 0.73 && [exists -command load]} {
				set testmod 1
			}
		} msg]
	}

	set rc [catch {
		if {$testmod} {
			puts "Test:    load $target"
			load ./$target
		}
		if {$install} {
			set dest [env DESTDIR ""]@prefix@/lib/jim
			puts "Install: $target => $dest"
			file mkdir $dest
			file copy $target $dest/$target
		}
		puts "\nSuccess!"
	} msg]
	if {$rc} {
		puts stderr $msg
		exit 1
	}
}