Jim Tcl
Check-in [7c5bcde648]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:build-jim-ext: Improvements when cross compiling, Tcl

Detect when cross compiling (build != host) Tcl exec loses stderr if redirected and an error code is returned, so redo the approach to exec so that it works on both Tcl and Jim

Signed-off-by: Steve Bennett <steveb@workware.net.au>

Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 7c5bcde64805f65b5d338799c680cda5b7e71003
User & Date: steveb@workware.net.au 2018-07-08 01:49:53
Context
2018-07-08
11:47
remove some unused variable assignments

Signed-off-by: Steve Bennett <steveb@workware.net.au> check-in: 6220fc5fc0 user: steveb@workware.net.au tags: trunk

01:49
build-jim-ext: Improvements when cross compiling, Tcl

Detect when cross compiling (build != host) Tcl exec loses stderr if redirected and an error code is returned, so redo the approach to exec so that it works on both Tcl and Jim

Signed-off-by: Steve Bennett <steveb@workware.net.au> check-in: 7c5bcde648 user: steveb@workware.net.au tags: trunk

01:17
linenoise.c: Minor update to fix compiler warning

Signed-off-by: Steve Bennett <steveb@workware.net.au> check-in: 81c9a5ae01 user: steveb@workware.net.au tags: trunk

Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to auto.def.

451
452
453
454
455
456
457







458
459
460
461
462
463
464
    lappend extra_objs jim-nosignal.o
}

if {[ext-get-status load] eq "n"} {
    # If we don't have load, no need to support shared objects
    define SH_LINKFLAGS ""
}








msg-result "Jim static extensions: [lsort [concat $extinfo(static-tcl) $extinfo(static-c)]]"
if {[llength $extinfo(module-tcl)]} {
    msg-result "Jim Tcl extensions: [lsort $extinfo(module-tcl)]"
}
if {[llength $extinfo(module-c)]} {
    msg-result "Jim dynamic extensions: [lsort $extinfo(module-c)]"







>
>
>
>
>
>
>







451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
    lappend extra_objs jim-nosignal.o
}

if {[ext-get-status load] eq "n"} {
    # If we don't have load, no need to support shared objects
    define SH_LINKFLAGS ""
}

# Are we cross compiling?
if {[get-define host] ne [get-define build]} {
	define cross_compiling 1
} else {
	define cross_compiling 0
}

msg-result "Jim static extensions: [lsort [concat $extinfo(static-tcl) $extinfo(static-c)]]"
if {[llength $extinfo(module-tcl)]} {
    msg-result "Jim Tcl extensions: [lsort $extinfo(module-tcl)]"
}
if {[llength $extinfo(module-c)]} {
    msg-result "Jim dynamic extensions: [lsort $extinfo(module-c)]"

Changes to 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
..
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
...
102
103
104
105
106
107
108

109
110






























111
112
113
114
115
116
117
...
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
#!/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 {}
................................................................................
			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
................................................................................
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 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







|






<
<
<
<
<
<
<
<
<
<


>







 







>


>
>
>
>

>



>






>







 







>
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







<
<
<
<
|
<
<
<
<
<
<
>

<
<
<
<
<
<
<

|
<
<
<
<








|
|
|
<
<
<
|
<
<

<

<
<
<

|


>
|
|
>




|
<
<
<
<
<
<
<
<
<



<


<
<
<
<
<
|

<
<
|
<
|





<







|







1
2
3
4
5
6
7
8
9
10
11
12
13
14










15
16
17
18
19
20
21
22
23
24
..
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
...
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
...
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
#!/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 {}
................................................................................
			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/@prefix@/lib/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
................................................................................
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 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 ""]@prefix@/lib/jim
			puts "Install: $target => $dest"
			file mkdir $dest