diff --git a/.mgit/bundle.sh b/.mgit/bundle.sh index 5ffe340..433dca8 100755 --- a/.mgit/bundle.sh +++ b/.mgit/bundle.sh @@ -10,6 +10,7 @@ describe() { } say() { [ "$VERBOSE" ] && echo "$@"; } +print() { [[ "$AS_STRING" = "string" ]] && echo -n "$@ " || echo "$@"; } verbose() { say "$@"; "$@"; } die() { echo "$@" >&2; exit 1; } @@ -19,17 +20,26 @@ BLUA_PREFIX=Blua_ BBIN_PREFIX=Bbin_ # note: only the mingw linker is smart to ommit dlibs that are not used. -DLIBS_mingw="gdi32 msimg32 opengl32 winmm ws2_32 ole32" +DLIBS_mingw="crypt32 gdi32 msimg32 opengl32 winmm ws2_32 ole32 psapi mpr" DLIBS_linux="m dl" DLIBS_osx= -FRAMEWORKS="ApplicationServices" # for OSX +FRAMEWORKS="ApplicationServices CoreFoundation Security" # for OSX APREFIX_mingw= APREFIX_linux=lib APREFIX_osx=lib -ALIBS="luajit" +AR=ar +CC=gcc +CXX=g++ +STRIP=strip +CROSS= +ALIBS="" MODULES="bundle_loader" +LUAJIT_LIB="luajit" +OUT_LIBRARY="libluab.a" +LUA_STATIC_LIBRARY="libluasrc.a" +BUNDLE_STATIC_LIBRARY="libbundle.a" BIN_MODULES= DIR_MODULES= ICON_mingw=media/icons/luapower.ico @@ -39,7 +49,9 @@ OSX_ICON_SIZES="16 32 128" # you can add 256 and 512 but the icns will be 0.5M IGNORE_ODIR= COMPRESS_EXE= NOCONSOLE= +TODLL= VERBOSE= +AS_STRING= # list modules and libs ------------------------------------------------------ @@ -51,8 +63,9 @@ lua_module() { [ "$ext" != lua -a "$ext" != dasl ] && return [ "${f%_test.lua}" != $f ] && return [ "${f%_demo.lua}" != $f ] && return - [ "${f#bin/}" != $f -a "${f#bin/$P/}" = $f ] && return - echo $f + [ "${f#bin/}" != $f ] && return + [ "bundle_loader.lua" == $f ] && return + print $f } # usage: P= $0 [dir] -> module1.lua|.dasl ... @@ -76,8 +89,11 @@ lua_modules() { alibs() { (cd bin/$P && for f in *.a; do - local m=${f%*.*} # libz.* -> libz - echo ${m#$APREFIX} # libz -> z + local m=${f%*.*} # libz.* -> libz + local l=${m#$APREFIX} # libz -> z + [ "luajit" == $l ] && continue + [ "stdc++" == $l ] && continue + print $l done) } @@ -100,17 +116,17 @@ compile_bin_file() { .incbin \"$f\" label_2: $shim - " | gcc -c -xassembler - -o $o $CFLAGS "$@" + " | ${CC} -fPIC -c -xassembler - -o $o ${CFLAGS} "$@" } # usage: CFLAGS=... f=file.c o=file.o $0 CFLAGS... -> file.o compile_c_module() { - gcc -c -xc $f -o $o $CFLAGS "$@" + ${CC} -fPIC -c -xc $f -o $o $CFLAGS "$@" } # usage: [ filename=file.lua ] f=file.lua|- o=file.o $0 CFLAGS... -> file.o compile_lua_module() { - ./luajit -b -t raw -g $f $o.luac + ./bin/$PO/luajit -b -t raw -g $f $o.luac local sym=$filename [ "$sym" ] || sym=$f sym=${sym#bin/$P/lua/} # bin//lua/a.lua -> a.lua @@ -122,7 +138,7 @@ compile_lua_module() { # usage: f=file.dasl o=file.o $0 CFLAGS... -> file.o compile_dasl_module() { - ./luajit dynasm.lua $f | filename=$f f=- compile_lua_module "$@" + ./bin/$PO/luajit dynasm.lua $f | filename=$f f=- compile_lua_module "$@" } # usage: f=file.* [name=file.*] o=file.o $0 CFLAGS... -> file.o @@ -173,6 +189,14 @@ compile_module() { local o=$ODIR/$f$osuffix.o # a.ext -> $ODIR/a.ext.o OFILES="$OFILES $o" # add the .o file to the list of files to be linked + # add the .o file to the list of files to be linked + [[ "$x" == "lua" || "$x" == "dasl" ]] && + OLFILES="$OLFILES $o" + # exclude the bundle.c file because it'll be linked to single library + [[ "$x" != "lua" && "$x" != "dasl" && "$(basename "$f")" != "bundle.c" ]] && + OFILES="$OFILES $o" + [[ "$(basename "$f")" == "bundle.c" ]] && + OBFILES="$OBFILES $o" # use the cached .o file if the source file hasn't changed, make-style. [ -z "$IGNORE_ODIR" -a -f $o -a $o -nt $f ] && return @@ -198,7 +222,7 @@ compile_resource() { sayt res $o mkdir -p `dirname $o` - echo "$s" | windres -o $o + echo "$s" | ${CROSS}windres -o $o } # add an icon file for the exe file and main window (Windows only) @@ -235,7 +259,7 @@ compile_version_info() { [ $OS = mingw ] || return sayt versioninfo "$VERSIONINFO" s="$(echo ' - 1 VERSIONINFO' + 2 VERSIONINFO' if [ "${#FILEVERSION}" = 7 ]; then echo " FILEVERSION ${FILEVERSION//./,} @@ -264,7 +288,7 @@ compile_version_info() { compile_virtual_lua_module() { local o=$ODIR/$1.lua.o sayt vlua $1 - OFILES="$OFILES $o" + OLFILES="$OLFILES $o" mkdir -p `dirname $o` echo "$s" | o=$o filename=$1.lua f=- compile_lua_module } @@ -279,6 +303,18 @@ compile_bundle_appversion() { s="return '$APPVERSION'" compile_virtual_lua_module bundle_appversion } +compile_lua_static_lib() { + [ "$OLFILES" ] || return + [ "$ODIR/$LUA_STATIC_LIBRARY" ] && rm $ODIR/$LUA_STATIC_LIBRARY + [ "$ODIR" ] && verbose ${AR} rcs $ODIR/$LUA_STATIC_LIBRARY $OLFILES +} + +compile_bundle_static_lib() { + [ "$OBFILES" ] || return + [ "$EXE_DIR/$BUNDLE_STATIC_LIBRARY" ] && rm $EXE_DIR/$BUNDLE_STATIC_LIBRARY + [ "$ODIR" ] && verbose ${AR} rcs $EXE_DIR/$BUNDLE_STATIC_LIBRARY $OBFILES +} + # usage: MODULES='mod1 ...' $0 -> $ODIR/*.o compile_all() { say "Compiling modules..." @@ -290,6 +326,12 @@ compile_all() { # the compile_*() functions will add the names of all .o files to this var OFILES= + # the compile_virtual_lua_module() function will add the names of all .o files to this var + OLFILES= + + # the compile_bundle_module() function will add the names of all .o files to this var + OBFILES= + # the icon has to be linked first, believe it! # so we compile it first so that it's added to $OFILES first. compile_icon "$ICON" @@ -331,14 +373,59 @@ compile_all() { # generate a VERSIONINFO resource (Windows) compile_version_info "$VERSIONINFO" + # generate a static LIB with Lua code + compile_lua_static_lib + + # generate a static LIB with Bundle code + compile_bundle_static_lib + } # linking -------------------------------------------------------------------- -aopt() { for f in $1; do echo "bin/$P/$APREFIX$f.a"; done; } +aopt() { for f in $1; do echo "addlib bin/$P/$APREFIX$f.a"; done; } +sopt() { for f in $1; do echo -ne "bin/$P/$APREFIX$f.a "; done; } lopt() { for f in $1; do echo "-l$f"; done; } fopt() { for f in $1; do echo "-framework $f"; done; } +make_union_static_lib() { + rm "$EXE_DIR/$OUT_LIBRARY" + if [ $OS = osx ]; then + mkdir -p .bundle-tmp/$P/luab + cp `sopt "$ALIBS"` "$ODIR/$LUA_STATIC_LIBRARY" .bundle-tmp/$P/luab/ + pushd .bundle-tmp/$P/luab/ + for libname in *.a; do + local libdir=${libname%.*} + mkdir ${libdir} + pushd ${libdir} + ${AR} x ../${libname} + for objname in *.o; do + cp ${objname} "../${libdir}_${objname}" + done + popd + rm -r ${libdir} + done + rm *.a + say "Build union static library:" + ${AR} r "../../../$EXE_DIR/$OUT_LIBRARY" *.o + verbose ${CROSS}ranlib "../../../$EXE_DIR/$OUT_LIBRARY" + popd + rm -r .bundle-tmp/$P/luab + else + AR_MANIFEST=` + echo "create $EXE_DIR/$OUT_LIBRARY" && + aopt "$ALIBS" && + echo "addlib $ODIR/$LUA_STATIC_LIBRARY" && + echo "save" && + echo "end"` + say "Manifest for union static library:" + say "$AR_MANIFEST" + ${AR} -M <