diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 84c6df1..d235452 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -25,9 +25,6 @@ jobs: - name: Unit tests # same reason as integration tests: nimble exits with code 0 even on failure. run: | - cmake -B vendor/libplum/build -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF vendor/libplum - cmake --build vendor/libplum/build - cp vendor/libplum/build/libplum.a vendor/libplum/libplum.a nim c -o:tests/test_plum tests/test_plum.nim ./tests/test_plum diff --git a/libplum.nimble b/libplum.nimble index ab798d8..6744764 100644 --- a/libplum.nimble +++ b/libplum.nimble @@ -13,20 +13,10 @@ requires "nim >= 1.6.0", "chronos >= 4.2.0 & < 5.0.0", "unittest2" -proc compileStaticLibraries() = - withDir "vendor/libplum": - exec("cmake -B build -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF") - exec("cmake --build build") - cpFile("build/libplum.a", "libplum.a") - task format, "format Nim code using nph": exec "nph libplum/ tests/" -task buildBundledLibs, "build bundled libraries": - compileStaticLibraries() - task test, "run tests": - compileStaticLibraries() exec("nimble setup") exec("nim c -o:tests/test_plum tests/test_plum.nim") exec("./tests/test_plum") @@ -40,6 +30,3 @@ task testIntegration, "run miniupnpd integration tests in Docker / Podman": exec(docker & " run --rm --cap-add=NET_ADMIN -e TEST_MINIUPNP_PCP=1" & flags & " " & packageName) exec(docker & " run --rm --cap-add=NET_ADMIN -e TEST_MINIUPNP_UPNP=1" & flags & " " & packageName) exec(docker & " run --rm --cap-add=NET_ADMIN -e TEST_MINIUPNP_NATPMP=1" & flags & " " & packageName) - -before install: - compileStaticLibraries() diff --git a/libplum/libplum.nim b/libplum/libplum.nim index 9c3457e..d0ce609 100644 --- a/libplum/libplum.nim +++ b/libplum/libplum.nim @@ -6,21 +6,37 @@ # This file may not be copied, modified, or distributed except according to # those terms. -import std/[os, strutils] +import std/[os, sequtils, strutils] const rootPath = currentSourcePath.parentDir().parentDir().replace('\\', '/') libplumPath = rootPath & "/vendor/libplum" includePath = libplumPath & "/include/plum" - libraryPath = libplumPath & "/libplum.a" -{.passc: "-I" & includePath & " -DPLUM_STATIC".} -{.passl: libraryPath.} + srcPath = libplumPath & "/src" + + includes = @["-I" & includePath, "-I" & srcPath] + + defs = @["-DPLUM_STATIC", "-DPLUM_EXPORTS", "-DRELEASE=1", "-D_GNU_SOURCE"] + + pdefs = + when defined(windows): + @["-DWIN32_LEAN_AND_MEAN"] + else: + @[] + + flags* = (includes & defs & pdefs).mapIt(it.quoteShell()).join(" ") + +{.localPassC: flags.} when defined(windows): {.passl: "-lws2_32 -liphlpapi -lbcrypt".} else: {.passl: "-lpthread".} +{.compile(rootPath & "/libplum_units.c", flags).} +{.compile(srcPath & "/http.c", flags).} +{.compile(srcPath & "/upnp.c", flags).} + const PLUM_ERR_SUCCESS* = cint(0) PLUM_ERR_INVALID* = cint(-1) diff --git a/libplum/plum.nim b/libplum/plum.nim index ca14cf7..65b3aad 100644 --- a/libplum/plum.nim +++ b/libplum/plum.nim @@ -10,7 +10,9 @@ import std/atomics import chronos import chronos/threadsync import results -import ./libplum +import libplum + +{.localPassC: libplum.flags.} # libplum declares some parameters as `const T*` in C (read-only pointer). # Nim has no equivalent, so the generated C code drops the `const`, causing diff --git a/libplum_units.c b/libplum_units.c new file mode 100644 index 0000000..0099e7d --- /dev/null +++ b/libplum_units.c @@ -0,0 +1,22 @@ +// Copyright (c) 2026 Status Research & Development GmbH +// Licensed under either of +// * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +// * MIT license ([LICENSE-MIT](LICENSE-MIT)) +// at your option. +// This file may not be copied, modified, or distributed except according to +// those terms. + +#include "./vendor/libplum/src/addr.c" +#include "./vendor/libplum/src/client.c" +#include "./vendor/libplum/src/dummytls.c" +#include "./vendor/libplum/src/log.c" +#include "./vendor/libplum/src/natpmp.c" +#include "./vendor/libplum/src/net.c" +#include "./vendor/libplum/src/noprotocol.c" +#include "./vendor/libplum/src/pcp.c" +#include "./vendor/libplum/src/plum.c" +#include "./vendor/libplum/src/random.c" +#include "./vendor/libplum/src/tcp.c" +#include "./vendor/libplum/src/timestamp.c" +#include "./vendor/libplum/src/udp.c" +#include "./vendor/libplum/src/util.c" diff --git a/tests/Dockerfile b/tests/Dockerfile index e4b21ef..9180a6c 100644 --- a/tests/Dockerfile +++ b/tests/Dockerfile @@ -1,7 +1,7 @@ FROM ubuntu:24.04 RUN apt-get update && apt-get install -y --no-install-recommends \ - cmake gcc make git curl ca-certificates xz-utils \ + gcc make git curl ca-certificates xz-utils \ libc-dev \ iproute2 \ && rm -rf /var/lib/apt/lists/* @@ -34,19 +34,11 @@ RUN curl -fsSL "https://nim-lang.org/download/nim-${NIM_VERSION}-linux_x64.tar.x # Install nim deps (cached layer) WORKDIR /app COPY libplum.nimble . +COPY libplum_units.c . COPY libplum/ libplum/ COPY vendor/ vendor/ RUN nimble setup -y -# Build libplum static library -RUN rm -rf vendor/libplum/build && \ - cmake -B vendor/libplum/build \ - -DCMAKE_BUILD_TYPE=Release \ - -DBUILD_SHARED_LIBS=OFF \ - vendor/libplum && \ - cmake --build vendor/libplum/build && \ - cp vendor/libplum/build/libplum.a vendor/libplum/libplum.a - # Compile test binaries (protocol x memory manager) COPY tests/ tests/ RUN nim c -d:miniupnp_protocol=pcp --mm:orc --threads:on -o:tests/test_pcp_orc tests/test_plum.nim && \