2025-06-17 09:59:46 +02:00
|
|
|
mode = ScriptMode.Verbose
|
|
|
|
|
|
2025-12-23 16:41:41 +04:00
|
|
|
import strutils, os
|
2025-12-09 10:09:01 +01:00
|
|
|
|
2025-05-29 16:48:53 +05:30
|
|
|
# Package
|
2025-07-11 16:31:22 +05:30
|
|
|
version = "0.1.0"
|
|
|
|
|
author = "Waku Team"
|
|
|
|
|
description = "E2E Reliability Protocol API"
|
|
|
|
|
license = "MIT"
|
|
|
|
|
srcDir = "src"
|
2025-05-29 16:48:53 +05:30
|
|
|
|
|
|
|
|
# Dependencies
|
2025-06-17 09:59:46 +02:00
|
|
|
requires "nim >= 2.2.4",
|
2025-07-11 16:31:22 +05:30
|
|
|
"chronicles", "chronos", "stew", "stint", "metrics", "libp2p", "results"
|
2025-05-29 16:48:53 +05:30
|
|
|
|
2025-11-07 11:20:17 +01:00
|
|
|
proc buildLibrary(
|
|
|
|
|
outLibNameAndExt: string,
|
|
|
|
|
name: string,
|
|
|
|
|
srcDir = "./",
|
|
|
|
|
params = "",
|
|
|
|
|
`type` = "static",
|
|
|
|
|
) =
|
2025-05-29 16:48:53 +05:30
|
|
|
if not dirExists "build":
|
|
|
|
|
mkDir "build"
|
|
|
|
|
# allow something like "nim nimbus --verbosity:0 --hints:off nimbus.nims"
|
|
|
|
|
var extra_params = params
|
|
|
|
|
for i in 2 ..< paramCount():
|
|
|
|
|
extra_params &= " " & paramStr(i)
|
|
|
|
|
if `type` == "static":
|
2025-11-07 11:20:17 +01:00
|
|
|
exec "nim c" & " --out:build/" & outLibNameAndExt &
|
|
|
|
|
" --threads:on --app:staticlib --opt:size --noMain --mm:refc --header --nimMainPrefix:libsds --skipParentCfg:on " &
|
2025-05-29 16:48:53 +05:30
|
|
|
extra_params & " " & srcDir & name & ".nim"
|
|
|
|
|
else:
|
2025-09-11 14:30:08 +02:00
|
|
|
when defined(windows):
|
2025-11-07 11:20:17 +01:00
|
|
|
exec "nim c" & " --out:build/" & outLibNameAndExt &
|
2025-09-11 14:30:08 +02:00
|
|
|
" --threads:on --app:lib --opt:size --noMain --mm:refc --header --nimMainPrefix:libsds --skipParentCfg:off " &
|
|
|
|
|
extra_params & " " & srcDir & name & ".nim"
|
|
|
|
|
else:
|
2025-11-07 11:20:17 +01:00
|
|
|
exec "nim c" & " --out:build/" & outLibNameAndExt &
|
2025-09-11 14:30:08 +02:00
|
|
|
" --threads:on --app:lib --opt:size --noMain --mm:refc --header --nimMainPrefix:libsds --skipParentCfg:on " &
|
|
|
|
|
extra_params & " " & srcDir & name & ".nim"
|
2025-05-29 16:48:53 +05:30
|
|
|
|
2025-12-17 16:02:53 +01:00
|
|
|
proc getArch(): string =
|
|
|
|
|
let arch = getEnv("ARCH")
|
|
|
|
|
if arch != "": return $arch
|
|
|
|
|
let (archFromUname, _) = gorgeEx("uname -m")
|
|
|
|
|
return $archFromUname
|
|
|
|
|
|
2025-05-29 16:48:53 +05:30
|
|
|
# Tasks
|
|
|
|
|
task test, "Run the test suite":
|
|
|
|
|
exec "nim c -r tests/test_bloom.nim"
|
|
|
|
|
exec "nim c -r tests/test_reliability.nim"
|
|
|
|
|
|
2025-11-07 11:20:17 +01:00
|
|
|
task libsdsDynamicWindows, "Generate bindings":
|
|
|
|
|
let outLibNameAndExt = "libsds.dll"
|
2025-05-29 16:48:53 +05:30
|
|
|
let name = "libsds"
|
2025-11-07 11:20:17 +01:00
|
|
|
buildLibrary outLibNameAndExt,
|
|
|
|
|
name, "library/",
|
2025-09-11 14:30:08 +02:00
|
|
|
"""-d:chronicles_line_numbers --warning:Deprecated:off --warning:UnusedImport:on -d:chronicles_log_level=TRACE """,
|
2025-07-11 16:31:22 +05:30
|
|
|
"dynamic"
|
2025-08-24 23:56:14 +02:00
|
|
|
|
2025-11-07 11:20:17 +01:00
|
|
|
task libsdsDynamicLinux, "Generate bindings":
|
|
|
|
|
let outLibNameAndExt = "libsds.so"
|
|
|
|
|
let name = "libsds"
|
|
|
|
|
buildLibrary outLibNameAndExt,
|
|
|
|
|
name, "library/",
|
|
|
|
|
"""-d:chronicles_line_numbers --warning:Deprecated:off --warning:UnusedImport:on -d:chronicles_log_level=TRACE """,
|
|
|
|
|
"dynamic"
|
|
|
|
|
|
|
|
|
|
task libsdsDynamicMac, "Generate bindings":
|
|
|
|
|
let outLibNameAndExt = "libsds.dylib"
|
|
|
|
|
let name = "libsds"
|
2025-12-09 10:09:01 +01:00
|
|
|
|
2025-12-17 16:02:53 +01:00
|
|
|
let arch = getArch()
|
2025-12-09 10:09:01 +01:00
|
|
|
let sdkPath = staticExec("xcrun --show-sdk-path").strip()
|
|
|
|
|
let archFlags = (if arch == "arm64": "--cpu:arm64 --passC:\"-arch arm64\" --passL:\"-arch arm64\" --passC:\"-isysroot " & sdkPath & "\" --passL:\"-isysroot " & sdkPath & "\""
|
|
|
|
|
else: "--cpu:amd64 --passC:\"-arch x86_64\" --passL:\"-arch x86_64\" --passC:\"-isysroot " & sdkPath & "\" --passL:\"-isysroot " & sdkPath & "\"")
|
2025-11-07 11:20:17 +01:00
|
|
|
buildLibrary outLibNameAndExt,
|
|
|
|
|
name, "library/",
|
2025-12-09 10:09:01 +01:00
|
|
|
archFlags & " -d:chronicles_line_numbers --warning:Deprecated:off --warning:UnusedImport:on -d:chronicles_log_level=TRACE",
|
2025-11-07 11:20:17 +01:00
|
|
|
"dynamic"
|
|
|
|
|
|
|
|
|
|
task libsdsStaticWindows, "Generate bindings":
|
|
|
|
|
let outLibNameAndExt = "libsds.lib"
|
|
|
|
|
let name = "libsds"
|
|
|
|
|
buildLibrary outLibNameAndExt,
|
|
|
|
|
name, "library/",
|
|
|
|
|
"""-d:chronicles_line_numbers --warning:Deprecated:off --warning:UnusedImport:on -d:chronicles_log_level=TRACE """,
|
|
|
|
|
"static"
|
|
|
|
|
|
|
|
|
|
task libsdsStaticLinux, "Generate bindings":
|
|
|
|
|
let outLibNameAndExt = "libsds.a"
|
|
|
|
|
let name = "libsds"
|
|
|
|
|
buildLibrary outLibNameAndExt,
|
|
|
|
|
name, "library/",
|
|
|
|
|
"""-d:chronicles_line_numbers --warning:Deprecated:off --warning:UnusedImport:on -d:chronicles_log_level=TRACE """,
|
|
|
|
|
"static"
|
|
|
|
|
|
|
|
|
|
task libsdsStaticMac, "Generate bindings":
|
|
|
|
|
let outLibNameAndExt = "libsds.a"
|
|
|
|
|
let name = "libsds"
|
2025-12-09 10:09:01 +01:00
|
|
|
|
2025-12-17 16:02:53 +01:00
|
|
|
let arch = getArch()
|
2025-12-09 10:09:01 +01:00
|
|
|
let sdkPath = staticExec("xcrun --show-sdk-path").strip()
|
|
|
|
|
let archFlags = (if arch == "arm64": "--cpu:arm64 --passC:\"-arch arm64\" --passL:\"-arch arm64\" --passC:\"-isysroot " & sdkPath & "\" --passL:\"-isysroot " & sdkPath & "\""
|
|
|
|
|
else: "--cpu:amd64 --passC:\"-arch x86_64\" --passL:\"-arch x86_64\" --passC:\"-isysroot " & sdkPath & "\" --passL:\"-isysroot " & sdkPath & "\"")
|
2025-11-07 11:20:17 +01:00
|
|
|
buildLibrary outLibNameAndExt,
|
|
|
|
|
name, "library/",
|
2025-12-09 10:09:01 +01:00
|
|
|
archFlags & " -d:chronicles_line_numbers --warning:Deprecated:off --warning:UnusedImport:on -d:chronicles_log_level=TRACE",
|
2025-11-07 11:20:17 +01:00
|
|
|
"static"
|
|
|
|
|
|
2025-11-27 23:01:39 +01:00
|
|
|
# Build Mobile iOS
|
|
|
|
|
proc buildMobileIOS(srcDir = ".", sdkPath = "") =
|
|
|
|
|
echo "Building iOS libsds library"
|
|
|
|
|
|
|
|
|
|
let outDir = "build"
|
2025-12-23 16:41:41 +04:00
|
|
|
let nimcacheDir = outDir & "/nimcache"
|
2025-11-27 23:01:39 +01:00
|
|
|
if not dirExists outDir:
|
|
|
|
|
mkDir outDir
|
|
|
|
|
|
|
|
|
|
if sdkPath.len == 0:
|
|
|
|
|
quit "Error: Xcode/iOS SDK not found"
|
|
|
|
|
|
|
|
|
|
let aFile = outDir & "/libsds.a"
|
2025-12-23 16:41:41 +04:00
|
|
|
let aFileTmp = outDir & "/libsds_tmp.a"
|
|
|
|
|
let arch = getArch()
|
2025-11-27 23:01:39 +01:00
|
|
|
|
|
|
|
|
# 1) Generate C sources from Nim (no linking)
|
2025-12-23 16:41:41 +04:00
|
|
|
# Use unique symbol prefix to avoid conflicts with other Nim libraries
|
2025-11-27 23:01:39 +01:00
|
|
|
exec "nim c" &
|
2025-12-23 16:41:41 +04:00
|
|
|
" --nimcache:" & nimcacheDir & " --os:ios --cpu:" & arch &
|
2025-11-27 23:01:39 +01:00
|
|
|
" --compileOnly:on" &
|
2025-12-23 16:41:41 +04:00
|
|
|
" --noMain --mm:orc" &
|
2025-11-27 23:01:39 +01:00
|
|
|
" --threads:on --opt:size --header" &
|
|
|
|
|
" --nimMainPrefix:libsds --skipParentCfg:on" &
|
|
|
|
|
" --cc:clang" &
|
2025-12-23 16:41:41 +04:00
|
|
|
" -d:useMalloc" &
|
|
|
|
|
" " & srcDir & "/libsds.nim"
|
2025-11-27 23:01:39 +01:00
|
|
|
|
2025-12-23 16:41:41 +04:00
|
|
|
# 2) Compile all generated C files to object files with hidden visibility
|
|
|
|
|
# This prevents symbol conflicts with other Nim libraries (e.g., libnim_status_client)
|
|
|
|
|
let clangFlags = "-arch " & arch & " -isysroot " & sdkPath &
|
2025-11-27 23:01:39 +01:00
|
|
|
" -I./vendor/nimbus-build-system/vendor/Nim/lib/" &
|
2025-12-23 16:41:41 +04:00
|
|
|
" -fembed-bitcode -miphoneos-version-min=16.0 -O2" &
|
|
|
|
|
" -fvisibility=hidden"
|
|
|
|
|
|
|
|
|
|
var objectFiles: seq[string] = @[]
|
|
|
|
|
for cFile in listFiles(nimcacheDir):
|
|
|
|
|
if cFile.endsWith(".c"):
|
|
|
|
|
let oFile = cFile.changeFileExt("o")
|
|
|
|
|
exec "clang " & clangFlags & " -c " & cFile & " -o " & oFile
|
|
|
|
|
objectFiles.add(oFile)
|
|
|
|
|
|
|
|
|
|
# 3) Create static library from all object files
|
|
|
|
|
exec "ar rcs " & aFileTmp & " " & objectFiles.join(" ")
|
|
|
|
|
|
|
|
|
|
# 4) Use libtool to localize all non-public symbols
|
|
|
|
|
# Keep only Sds* functions as global, hide everything else to prevent conflicts
|
|
|
|
|
# with nim runtime symbols from libnim_status_client
|
|
|
|
|
let keepSymbols = "_Sds*:_libsdsNimMain:_libsdsDatInit*:_libsdsInit*:_NimMainModule__libsds*"
|
|
|
|
|
exec "xcrun libtool -static -o " & aFile & " " & aFileTmp &
|
|
|
|
|
" -exported_symbols_list /dev/stdin <<< '" & keepSymbols & "' 2>/dev/null || cp " & aFileTmp & " " & aFile
|
2025-11-27 23:01:39 +01:00
|
|
|
|
|
|
|
|
echo "✔ iOS library created: " & aFile
|
|
|
|
|
|
|
|
|
|
task libsdsIOS, "Build the mobile bindings for iOS":
|
|
|
|
|
let srcDir = "./library"
|
|
|
|
|
let sdkPath = getEnv("IOS_SDK_PATH")
|
|
|
|
|
buildMobileIOS srcDir, sdkPath
|
|
|
|
|
|
2025-08-24 23:56:14 +02:00
|
|
|
### Mobile Android
|
|
|
|
|
proc buildMobileAndroid(srcDir = ".", params = "") =
|
2025-12-17 16:02:53 +01:00
|
|
|
let cpu = getArch()
|
2025-08-24 23:56:14 +02:00
|
|
|
|
2025-08-28 18:53:19 +02:00
|
|
|
let outDir = "build/"
|
2025-08-24 23:56:14 +02:00
|
|
|
if not dirExists outDir:
|
|
|
|
|
mkDir outDir
|
|
|
|
|
|
|
|
|
|
var extra_params = params
|
|
|
|
|
for i in 2 ..< paramCount():
|
|
|
|
|
extra_params &= " " & paramStr(i)
|
|
|
|
|
|
|
|
|
|
exec "nim c" & " --out:" & outDir &
|
2025-08-29 20:08:16 +02:00
|
|
|
"/libsds.so --threads:on --app:lib --opt:size --noMain --mm:refc --nimMainPrefix:libsds " &
|
2025-08-27 19:55:08 +02:00
|
|
|
"-d:chronicles_sinks=textlines[dynamic] --header --passL:-L" & outdir &
|
2025-08-28 18:53:19 +02:00
|
|
|
" --passL:-llog --cpu:" & cpu & " --os:android -d:androidNDK " & extra_params & " " &
|
|
|
|
|
srcDir & "/libsds.nim"
|
2025-08-24 23:56:14 +02:00
|
|
|
|
|
|
|
|
task libsdsAndroid, "Build the mobile bindings for Android":
|
|
|
|
|
let srcDir = "./library"
|
|
|
|
|
let extraParams = "-d:chronicles_log_level=ERROR"
|
|
|
|
|
buildMobileAndroid srcDir, extraParams
|