diff options
-rw-r--r-- | .gitignore | 7 | ||||
-rw-r--r-- | Makefile.in | 5 | ||||
-rw-r--r-- | auto.def | 3 | ||||
-rw-r--r-- | build-jim-ext.in | 219 | ||||
-rw-r--r-- | examples.ext/Makefile | 29 | ||||
-rw-r--r-- | examples.ext/README | 7 | ||||
-rw-r--r-- | examples.ext/helloworld.c | 24 | ||||
-rw-r--r-- | jim-array.c | 4 | ||||
-rw-r--r-- | jim-clock.c | 3 | ||||
-rw-r--r-- | jim-eventloop.c | 4 | ||||
-rw-r--r-- | jim-exec.c | 2 | ||||
-rw-r--r-- | jim-file.c | 5 | ||||
-rw-r--r-- | jim-format.c | 3 | ||||
-rw-r--r-- | jim-interactive.c | 3 | ||||
-rw-r--r-- | jim-load.c | 5 | ||||
-rw-r--r-- | jim-package.c | 3 | ||||
-rw-r--r-- | jim-posix.c | 2 | ||||
-rw-r--r-- | jim-readdir.c | 4 | ||||
-rw-r--r-- | jim-readline.c | 3 | ||||
-rw-r--r-- | jim-regexp.c | 2 | ||||
-rw-r--r-- | jim-sdl.c | 3 | ||||
-rw-r--r-- | jim-signal.c | 5 | ||||
-rw-r--r-- | jim-sqlite.c | 3 | ||||
-rw-r--r-- | jim-sqlite3.c | 3 | ||||
-rw-r--r-- | jim-subcmd.c | 3 | ||||
-rw-r--r-- | jim-syslog.c | 3 | ||||
-rw-r--r-- | jim-win32.c | 3 |
27 files changed, 315 insertions, 45 deletions
@@ -1,9 +1,5 @@ -autom4te.cache config.log -config.status -config.h tags -.clean /Makefile Tcl.html jimautoconf.h @@ -17,7 +13,8 @@ jim-oo.c jimsh *.exe libjim.a -libjim.so +*.so *.o configure.gnu jimsh0 +build-jim-ext diff --git a/Makefile.in b/Makefile.in index 458236a..f810c45 100644 --- a/Makefile.in +++ b/Makefile.in @@ -47,7 +47,7 @@ OBJS := _load-static-exts.o jim-subcmd.o jim-interactive.o jim-format.o jim.o ut JIMSH := jimsh@EXEEXT@ -all: $(JIMSH) @C_EXT_SHOBJS@ +all: $(JIMSH) @C_EXT_SHOBJS@ build-jim-ext # Create C extensions from pure Tcl extensions .SUFFIXES: .tcl @@ -72,6 +72,8 @@ install: all docs @TCL_EXTS@ install-exec cp jim-config.h $(DESTDIR)$(prefix)/include mkdir -p $(DESTDIR)$(prefix)/doc/jim cp Tcl.html $(DESTDIR)$(prefix)/doc/jim + mkdir -p $(DESTDIR)$(prefix)/bin + cp build-jim-ext $(DESTDIR)$(prefix)/bin install-exec: all mkdir -p $(DESTDIR)$(prefix)/bin @@ -79,6 +81,7 @@ install-exec: all uninstall: rm -f $(DESTDIR)$(prefix)/bin/$(JIMSH) + rm -f $(DESTDIR)$(prefix)/bin/build-jim-ext rm -f $(DESTDIR)$(prefix)/lib/$(LIBJIM) for i in README.extensions @C_EXT_SHOBJS@ @TCL_EXTS@; do rm -f $(DESTDIR)$(prefix)/lib/jim/$$i; done rm -f $(DESTDIR)$(prefix)/include/jim*.h @@ -434,3 +434,6 @@ define EXTRA_OBJS $extra_objs make-config-header jim-config.h -auto {HAVE_LONG_LONG* JIM_UTF8} -none * make-config-header jimautoconf.h -auto {jim_ext_* TCL_PLATFORM_* TCL_LIBRARY USE_* JIM_*} make-template Makefile.in +make-template build-jim-ext.in + +catch {exec chmod +x build-jim-ext} diff --git a/build-jim-ext.in b/build-jim-ext.in new file mode 100644 index 0000000..8eb42fa --- /dev/null +++ b/build-jim-ext.in @@ -0,0 +1,219 @@ +#!/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 +} + +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" + +if {"@JIM_STATICLIB@" eq "1" && !$static} { + puts stderr "Warning: libjim is static. Dynamic module may not work on some platforms.\n" + set ljim "" +} else { + set ljim -ljim +} + +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@" + } + if {$static} { + set shobj_cflags "" + } else { + set shobj_cflags "@SHOBJ_CFLAGS@" + } + set compile "$compiler @CFLAGS@ $CPPFLAGS $shobj_cflags $includepaths $opts -c -o $obj $source" + puts "Compile: $obj" + lappend objs $obj + set rc [catch { + exec {*}$compile + if {$verbose} { + puts stderr $compile + } + } msg] + if {$rc} { + puts stderr $compile + puts stderr $msg + file delete {*}$objs + exit 1 + } +} + +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 { + exec {*}$link + if {$verbose} { + puts stderr $link + } + } msg] + + if {!$keep} { + file delete {*}$objs + } + + if {$rc} { + file delete $target + puts stderr $link + puts stderr $msg + exit 1 + } + + 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 + } +} diff --git a/examples.ext/Makefile b/examples.ext/Makefile new file mode 100644 index 0000000..fa5cfc6 --- /dev/null +++ b/examples.ext/Makefile @@ -0,0 +1,29 @@ +# Note that if cross compiling, build with: +# +# make NOTEST=1 +# +# to avoid trying to load the resulting module. +# Also note that you will need a build-host version of jimsh in the +# PATH in order to build the extension. + +# Prefer jimsh in the PATH because it is more likely to be built +# for the build-host rather than the target. + +ifdef NOTEST +BUILDOPTS := --notest +endif + +export PATH := $(PATH):.. + +all: helloworld.so + +helloworld.so: helloworld.c + ../build-jim-ext -I.. -L.. $(BUILDOPTS) $^ + +# Note: Currently we don't attempt to set LD_LIBRARY_PATH or equivalent + +test: + JIMLIB=. ../jimsh -e 'package require helloworld; hello' + +clean: + rm -f *.o *.so diff --git a/examples.ext/README b/examples.ext/README new file mode 100644 index 0000000..2317481 --- /dev/null +++ b/examples.ext/README @@ -0,0 +1,7 @@ +This directory contains examples of C extensions for Jim. + +In general, do: + + build-jim-ext extsource.c + +See the Makefile diff --git a/examples.ext/helloworld.c b/examples.ext/helloworld.c new file mode 100644 index 0000000..371a23d --- /dev/null +++ b/examples.ext/helloworld.c @@ -0,0 +1,24 @@ +/* + * hello.c -- A minimal Jim C extension. + */ +#include <jim.h> + +static int +Hello_Cmd(Jim_Interp *interp, int objc, Jim_Obj *const objv[]) +{ + Jim_SetResultString(interp, "Hello, World!", -1); + return JIM_OK; +} + +/* + * Jim_helloworldInit -- Called when Jim loads your extension. + * + * Note that the name *must* correspond exactly to the name of the extension: + * Jim_<extname>Init + */ +int +Jim_helloworldInit(Jim_Interp *interp) +{ + Jim_CreateCommand(interp, "hello", Hello_Cmd, NULL, NULL); + return JIM_OK; +} diff --git a/jim-array.c b/jim-array.c index 112106a..f364ec8 100644 --- a/jim-array.c +++ b/jim-array.c @@ -50,9 +50,7 @@ #include <stdio.h> #include <errno.h> -#include "jim.h" -#include "jimautoconf.h" -#include "jim-subcmd.h" +#include <jim-subcmd.h> static int array_cmd_exists(Jim_Interp *interp, int argc, Jim_Obj *const *argv) { diff --git a/jim-clock.c b/jim-clock.c index fe957b2..f791106 100644 --- a/jim-clock.c +++ b/jim-clock.c @@ -15,9 +15,8 @@ #include <stdio.h> #include <time.h> -#include "jim.h" #include "jimautoconf.h" -#include "jim-subcmd.h" +#include <jim-subcmd.h> #ifdef HAVE_SYS_TIME_H #include <sys/time.h> diff --git a/jim-eventloop.c b/jim-eventloop.c index a7efdc2..ef62284 100644 --- a/jim-eventloop.c +++ b/jim-eventloop.c @@ -38,9 +38,9 @@ * official policies, either expressed or implied, of the Jim Tcl Project. **/ -#include "jim.h" #include "jimautoconf.h" -#include "jim-eventloop.h" +#include <jim.h> +#include <jim-eventloop.h> /* POSIX includes */ #include <sys/time.h> @@ -23,8 +23,8 @@ #include <string.h> #include <ctype.h> -#include "jim.h" #include "jimautoconf.h" +#include <jim.h> #if (!defined(HAVE_VFORK) || !defined(HAVE_WAITPID)) && !defined(__MINGW32__) /* Poor man's implementation of exec with system() @@ -50,9 +50,8 @@ #include <errno.h> #include <sys/stat.h> -#include "jim.h" -#include "jimautoconf.h" -#include "jim-subcmd.h" +#include <jimautoconf.h> +#include <jim-subcmd.h> #ifdef HAVE_UTIMES #include <sys/time.h> diff --git a/jim-format.c b/jim-format.c index c9cde89..bf149a2 100644 --- a/jim-format.c +++ b/jim-format.c @@ -42,8 +42,7 @@ #include <ctype.h> #include <string.h> -#include "jim.h" -#include "jimautoconf.h" +#include <jim.h> #include "utf8.h" #define JIM_UTF_MAX 3 diff --git a/jim-interactive.c b/jim-interactive.c index 658e8d8..88ae63e 100644 --- a/jim-interactive.c +++ b/jim-interactive.c @@ -1,7 +1,8 @@ #include <errno.h> #include <string.h> -#include "jim.h" + #include "jimautoconf.h" +#include <jim.h> #ifdef USE_LINENOISE #include <unistd.h> @@ -1,7 +1,8 @@ -#include "jim.h" -#include "jimautoconf.h" #include <string.h> +#include "jimautoconf.h" +#include <jim.h> + /* ----------------------------------------------------------------------------- * Dynamic libraries support (WIN32 not supported) * ---------------------------------------------------------------------------*/ diff --git a/jim-package.c b/jim-package.c index 8a2fc49..d9f50a9 100644 --- a/jim-package.c +++ b/jim-package.c @@ -1,8 +1,7 @@ #include <string.h> -#include "jim.h" #include "jimautoconf.h" -#include "jim-subcmd.h" +#include <jim-subcmd.h> #ifdef HAVE_UNISTD_H #include <unistd.h> diff --git a/jim-posix.c b/jim-posix.c index 0d17c49..5c1ae7f 100644 --- a/jim-posix.c +++ b/jim-posix.c @@ -40,8 +40,8 @@ #include <signal.h> #include <errno.h> -#include "jim.h" #include "jimautoconf.h" +#include <jim.h> #ifdef HAVE_SYS_SYSINFO_H #include <sys/sysinfo.h> diff --git a/jim-readdir.c b/jim-readdir.c index c5a65ae..a405692 100644 --- a/jim-readdir.c +++ b/jim-readdir.c @@ -49,8 +49,8 @@ #include <stdio.h> #include <string.h> -#include "jim.h" -#include "jimautoconf.h" +#include <jim.h> +#include <jimautoconf.h> #ifdef HAVE_DIRENT_H #include <dirent.h> diff --git a/jim-readline.c b/jim-readline.c index 9b1f410..39051b0 100644 --- a/jim-readline.c +++ b/jim-readline.c @@ -32,8 +32,7 @@ * official policies, either expressed or implied, of the Jim Tcl Project. */ -#include "jim.h" -#include "jimautoconf.h" +#include <jim.h> #include <readline/readline.h> #include <readline/history.h> diff --git a/jim-regexp.c b/jim-regexp.c index 2ccd996..de28b93 100644 --- a/jim-regexp.c +++ b/jim-regexp.c @@ -48,8 +48,8 @@ #include <stdlib.h> #include <string.h> -#include "jim.h" #include "jimautoconf.h" +#include "jim.h" #include "jimregexp.h" static void FreeRegexpInternalRep(Jim_Interp *interp, Jim_Obj *objPtr) @@ -39,8 +39,7 @@ #include <SDL/SDL.h> #include <SDL/SDL_gfxPrimitives.h> -#include "jim.h" -#include "jimautoconf.h" +#include <jim.h> #define AIO_CMD_LEN 128 diff --git a/jim-signal.c b/jim-signal.c index b248698..065c0f9 100644 --- a/jim-signal.c +++ b/jim-signal.c @@ -9,10 +9,9 @@ #include <ctype.h> #include <unistd.h> -#include "jim.h" #include "jimautoconf.h" -#include "jim-subcmd.h" -#include "jim-signal.h" +#include <jim-subcmd.h> +#include <jim-signal.h> #define MAX_SIGNALS (sizeof(jim_wide) * 8) diff --git a/jim-sqlite.c b/jim-sqlite.c index 019d671..f37e943 100644 --- a/jim-sqlite.c +++ b/jim-sqlite.c @@ -36,8 +36,7 @@ #include <string.h> #include <sqlite.h> -#include "jim.h" -#include "jimautoconf.h" +#include <jim.h> typedef struct JimSqliteHandle { diff --git a/jim-sqlite3.c b/jim-sqlite3.c index 8d4d8b8..be20d47 100644 --- a/jim-sqlite3.c +++ b/jim-sqlite3.c @@ -36,8 +36,7 @@ #include <string.h> #include <sqlite3.h> -#include "jim.h" -#include "jimautoconf.h" +#include <jim.h> typedef struct JimSqliteHandle { diff --git a/jim-subcmd.c b/jim-subcmd.c index f417c1e..446ed19 100644 --- a/jim-subcmd.c +++ b/jim-subcmd.c @@ -1,8 +1,7 @@ #include <stdio.h> #include <string.h> -#include "jim-subcmd.h" -#include "jimautoconf.h" +#include <jim-subcmd.h> /** * Implements the common 'commands' subcommand diff --git a/jim-syslog.c b/jim-syslog.c index 4e14910..bd373b8 100644 --- a/jim-syslog.c +++ b/jim-syslog.c @@ -9,8 +9,7 @@ #include <syslog.h> #include <string.h> -#include "jim.h" -#include "jimautoconf.h" +#include <jim.h> typedef struct { diff --git a/jim-win32.c b/jim-win32.c index 93ae0ef..89cedd4 100644 --- a/jim-win32.c +++ b/jim-win32.c @@ -32,8 +32,7 @@ * official policies, either expressed or implied, of the Jim Tcl Project. */ -#include "jim.h" -#include "jimautoconf.h" +#include <jim.h> /* Apparently windows.h and cygwin don't mix, but we seem to get * away with it here. Use at your own risk under cygwin |