static library

This commit is contained in:
dzaima 2023-05-21 23:47:40 +03:00
parent fd72ea79c8
commit de5bcfe211
6 changed files with 69 additions and 39 deletions

5
.gitignore vendored
View File

@ -6,6 +6,8 @@
*.cwasm *.cwasm
libcbqn.so libcbqn.so
libcbqn.dylib libcbqn.dylib
cbqn.dll
libcbqn.a
*.exe *.exe
*.dll *.dll
@ -30,6 +32,9 @@ cbqn-jit.bqn
/test/ffi/test.got /test/ffi/test.got
/test/ffi/shared.got /test/ffi/shared.got
/test/ffi/sharedTest /test/ffi/sharedTest
/test/ffi/staticTest
/test/ffi/sharedLibFolder
/test/ffi/staticLibFolder
/test/ffi/lib.so /test/ffi/lib.so
/test/testDir/ /test/testDir/

View File

@ -43,7 +43,8 @@ opts ← ⟨
2, "rm_lf", , "forcibly remove linker flag(s)" 2, "rm_lf", , "forcibly remove linker flag(s)"
0, "v", "", "Version string to report by --version; v=0 to disable"@+10 0, "v", "", "Version string to report by --version; v=0 to disable"@+10
1, "shared", 0, "Build a shared library; default output depends on 'os' setting" 1, "shared", 0, "Build a shared library"
1, "static-lib", 0, "Build a static library"
1, "wasi", 0, "Build with WASI; default output name is BQN.wasm" 1, "wasi", 0, "Build with WASI; default output name is BQN.wasm"
1, "emcc", 0, "Build with emscripten; outputs two files - BQN.wasm and BQN.js" 1, "emcc", 0, "Build with emscripten; outputs two files - BQN.wasm and BQN.js"
1, "clangd", 0, "Don't build, instead generate a compile_commands.json for clangd"@+10 1, "clangd", 0, "Don't build, instead generate a compile_commands.json for clangd"@+10
@ -150,8 +151,8 @@ AtRoot ← rootDir⊸•file.At
po { # parsed options po { # parsed options
verbose, J, rebuildAll, rebuildSingeli, tui, loud, output, clangd, versionName, verbose, J, rebuildAll, rebuildSingeli, tui, loud, output, clangd, versionName,
os, arch, native, has, avx2 os, arch, native, has, avx2,
emcc, wasm, replxx, singeli, emcc, wasm, replxx, singeli, staticLib,
usz, ffi, o3, debug, rtverify, heapverify, usz, ffi, o3, debug, rtverify, heapverify,
bytecodeDir, replxxDir, singeliDir, bytecodeDir, replxxDir, singeliDir,
CBQNc, ccColor, singeliFlags, REPLXXc, Linker CBQNc, ccColor, singeliFlags, REPLXXc, Linker
@ -171,19 +172,22 @@ po ← { # parsed options
ErrBoth {ab: "Error: '",a,"' and '",b,"' cannot both be enabled"} ErrBoth {ab: "Error: '",a,"' and '",b,"' cannot both be enabled"}
Max1 {𝕩 ErrBoth(2/˜) _assert_(1+´) GetOpt¨ 𝕩} Max1 {𝕩 ErrBoth(2/˜) _assert_(1+´) GetOpt¨ 𝕩}
NotWith {¬GetOpt 𝕨?@; ErrBoth(𝕨0/𝕩) _assert_(¬´) GetOpt¨ 𝕩} NotWith {𝕩 {ErrBoth ¨ 𝕩/¨𝕨}_assert_(¬·´´¨) GetOpt¨¨ 𝕩}
Max1 "REPLXX""wasi""emcc" Max1 "REPLXX""wasi""emcc"
"shared" NotWith "REPLXX""emcc" Max1 "shared""static-lib"
"static-lib""shared" NotWith "REPLXX""emcc"
shared GetOpt "shared" sharedLib GetOpt "shared"
staticLib GetOpt "static-lib"
lib staticLib sharedLib
native 0 DOpt "native" native 0 DOpt "native"
emcc GetOpt "emcc" emcc GetOpt "emcc"
wasi GetOpt "wasi" wasi GetOpt "wasi"
usz •ParseFloat GetOpt "usz" usz •ParseFloat GetOpt "usz"
{𝕊: "Error: usz can only be 32 or 64"}_assert_ usz3264 {𝕊: "Error: usz can only be 32 or 64"}_assert_ usz3264
versionName {shared? @; "0"@ GetOpt "v"} versionName {sharedLib? @; "0"@ GetOpt "v"}
wasm wasiemcc wasm wasiemcc
cc {emcc? "emcc"; "clang"} DOpt "CC" cc {emcc? "emcc"; "clang"} DOpt "CC"
@ -223,19 +227,20 @@ po ← { # parsed options
} (arch"x86-64") singeli ¬avx2 } (arch"x86-64") singeli ¬avx2
pie {(¬shared)linuxarch"x86-64"? 0; 1} DOpt "pie" pie {(¬sharedLibstaticLib)linuxarch"x86-64"? 0; 1} DOpt "pie"
{𝕊: pie 0} wasm {𝕊: pie 0} wasm
output GetOpt "OUTPUT" output GetOpt "OUTPUT"
output •wdpath•file.At {𝕊: output •wdpath•file.At {𝕊:
wasi? "BQN.wasm"; wasi? "BQN.wasm";
emcc? "."; emcc? ".";
shared? {macos? "libcbqn.dylib"; windows? "cbqn.dll"; "libcbqn.so"}; staticLib? "libcbqn.a";
sharedLib? {macos? "libcbqn.dylib"; windows? "cbqn.dll"; "libcbqn.so"};
windows? "BQN.exe"; windows? "BQN.exe";
"BQN" "BQN"
}("") output }("") output
exportSymbols ffishared exportSymbols ffisharedLib
defLibs @GetOpt "LD_LIBS" defLibs @GetOpt "LD_LIBS"
GetLibs { 𝕊: GetLibs { 𝕊:
@ -293,8 +298,9 @@ po ← { # parsed options
args ( heapverify) / "-DHEAP_VERIFY" args ( heapverify) / "-DHEAP_VERIFY"
args ( exportSymbols) / "-DCBQN_EXPORT" args ( exportSymbols) / "-DCBQN_EXPORT"
args ( ¬pie) / "-fno-pie" args ( ¬pie) / "-fno-pie"
args ( pie ¬shared) / "-fPIE" args (pie ¬sharedLib) / "-fPIE"
args ( shared) / "-DCBQN_SHARED" (¬GetOpt"no_fPIC")/"-fPIC" args ( lib) / "-DCBQN_LIB"
args ( sharedLib) / (¬GetOpt"no_fPIC")/"-fPIC"
args ( windows) / "-DNO_MMAP" args ( windows) / "-DNO_MMAP"
args (replxx windows) / "-DUSE_REPLXX_IO" args (replxx windows) / "-DUSE_REPLXX_IO"
args ( @versionName) / "-DHAS_VERSION" args ( @versionName) / "-DHAS_VERSION"
@ -309,10 +315,10 @@ po ← { # parsed options
args cxx, "-DREPLXX_STATIC=1", "-I"replxxDir"/include" args cxx, "-DREPLXX_STATIC=1", "-I"replxxDir"/include"
args GetOpt "REPLXX_FLAGS" args GetOpt "REPLXX_FLAGS"
args GetOpt "CXXFLAGS" args GetOpt "CXXFLAGS"
args ( ¬pie) / "-fno-pie" args ( ¬pie) / "-fno-pie"
args GetOpt "CCFLAGS" args GetOpt "CCFLAGS"
args (pie ¬shared) / "-fPIE" args (pie ¬sharedLib) / "-fPIE"
args ( shared) / (¬GetOpt"no_fPIC")/"-fPIC" args ( sharedLib) / (¬GetOpt"no_fPIC")/"-fPIC"
{"REPLXX C++ compiler: "•Repr 𝕩} _verboseLog args {"REPLXX C++ compiler: "•Repr 𝕩} _verboseLog args
replxxc args replxxc args
} }
@ -320,26 +326,35 @@ po ← { # parsed options
singeliFlags GetOpt "sf" singeliFlags GetOpt "sf"
Linker { 𝕊: Linker { 𝕊:
rdynamic (¬wasi) exportSymbols ¬windows args
args {replxx? cxx; cc} DOpt "LD" {
args defLibs / "-lm" staticLib?
args DOpt "LD_LIBS" args "ar" DOpt "LD"
args ((¬GetOpt "NO_LDL") defLibs ffi ¬bsd) / "-ldl" args GetOpt "lf"
args 1GetLibs@ args GetOpt "LDFLAGS"
args GetOpt "lf" args "-rcs"
args GetOpt "CCFLAGS" ;
args GetOpt "LDFLAGS" rdynamic (¬wasi) exportSymbols ¬windows
args ( emcc) / "-s", "EXPORTED_FUNCTIONS=_main,_cbqn_runLine,_cbqn_evalSrc", "-s", "EXPORTED_RUNTIME_METHODS=ccall,cwrap", "-s", "ALLOW_MEMORY_GROWTH=1" args {replxx? cxx; cc} DOpt "LD"
args ( wasi) / "-lwasi-emulated-mman", "--target=wasm32-wasi", "-Wl,-z,stack-size=8388608", "-Wl,--initial-memory=67108864" args defLibs / "-lm"
args (shared wasi) / "-mexec-model=reactor", "-Wl,--export-dynamic", "-Wl,--export=malloc", "-Wl,--export=free" args DOpt "LD_LIBS"
args (shared ¬wasi) / "-shared" args ((¬GetOpt "NO_LDL") defLibs ffi ¬bsd) / "-ldl"
args (shared macos) / "-install_name", "@rpath/libcbqn.dylib" args 1GetLibs@
args ( rdynamic) / "-rdynamic" args GetOpt "lf"
args ((¬wasm) ¬pie) / "-no-pie" args GetOpt "CCFLAGS"
args (pie ¬shared) / "-fPIE", "-pie" args GetOpt "LDFLAGS"
args ( windows) / "-lpthread" args ( emcc) / "-s", "EXPORTED_FUNCTIONS=_main,_cbqn_runLine,_cbqn_evalSrc", "-s", "EXPORTED_RUNTIME_METHODS=ccall,cwrap", "-s", "ALLOW_MEMORY_GROWTH=1"
args args (¬/) GetOpt "rm_lf" args ( wasi) / "-lwasi-emulated-mman", "--target=wasm32-wasi", "-Wl,-z,stack-size=8388608", "-Wl,--initial-memory=67108864"
{"linker: "•Repr 𝕩} _verboseLog args args (sharedLib wasi) / "-mexec-model=reactor", "-Wl,--export-dynamic", "-Wl,--export=malloc", "-Wl,--export=free"
args (sharedLib¬wasi) / "-shared"
args (sharedLibmacos) / "-install_name", "@rpath/libcbqn.dylib"
args ( rdynamic) / "-rdynamic"
args ((¬wasm) ¬pie) / "-no-pie"
args (pie ¬sharedLib) / "-fPIE", "-pie"
args ( windows) / "-lpthread"
args args (¬/) GetOpt "rm_lf"
{"linker: "•Repr 𝕩} _verboseLog args
}
linker args linker args
} }
@ -590,7 +605,8 @@ MakeLinkerInv ← { 𝕊 GetArgs‿cache‿name‿srcs:
dst cache.File name dst cache.File name
GetCMD { 𝕊: GetCMD { 𝕊:
args GetArgs@ args GetArgs@
"sh", rootDir, args, "-o", dst({𝕩.dst}¨ srcs)1args, @ cmd "sh", rootDir, args, "-o", dst({𝕩.dst}¨ srcs)1args, @
{po.staticLib? "staticlib", AtRoot dst, cmd; cmd}
} }
AddRule cache, name, dst, GetCMD, "link", srcs AddRule cache, name, dst, GetCMD, "link", srcs
} }

View File

@ -44,6 +44,10 @@ _runSingeli_ ← { runArgs GetDeps _𝕣_ dir runFile:
# c≠0: # c≠0:
0, oe 0, oe
} c } c
;
"staticlib"dstcmd:
•file.Remove•file.Exists dst
𝕊 cmd
; ;
"singeli"dirdstsingeliDirargssrcdep: "singeli"dirdstsingeliDirargssrcdep:
prefix "si_" (`'.')/ •file.Name src prefix "si_" (`'.')/ •file.Name src

View File

@ -899,7 +899,7 @@ void cbqn_evalSrc(char* src, i64 len) {
int main() { int main() {
repl_init(); repl_init();
} }
#elif !CBQN_SHARED #elif !CBQN_LIB
#if HAS_VERSION #if HAS_VERSION
extern char* cbqn_versionString; extern char* cbqn_versionString;
#endif #endif

View File

@ -5,9 +5,14 @@ test: testLib testShared
testShared: buildShared testShared: buildShared
@LD_LIBRARY_PATH=../../ ./sharedTest > shared.got @LD_LIBRARY_PATH=../../ ./sharedTest > shared.got
@diff --color -su shared.expected shared.got @diff --color -su shared.expected shared.got
buildShared: buildShared:
$(CC) -g sharedTest.c -o sharedTest -L../.. -lcbqn mkdir -p sharedLibFolder && cp ../../libcbqn.so sharedLibFolder
$(CC) -g libTest.c -o sharedTest -LsharedLibFolder -lcbqn
buildStatic:
mkdir -p staticLibFolder && cp ../../libcbqn.a staticLibFolder
$(CC) -g libTest.c -o staticTest -LstaticLibFolder -lcbqn -lm -lffi