indoorvivants / sn-bindgen-examples

Examples of using Scala 3 Native to interop with various C libraries using sn-bindgen
22 stars 2 forks source link

Question: packaging into Docker #125

Open susliko opened 2 months ago

susliko commented 2 months ago

Hey 🖖🏻

I'm still trying to wrap my mind around the linking process.

Let's say we're packaging the libuv example into Docker.

If we try the following Dockerfile

FROM keynmol/sn-vcpkg:latest as dev

WORKDIR /workdir

COPY . .

RUN sbt libuv/vcpkgInstall
RUN sbt libuv/nativeLink

FROM my-runtime-image

COPY --from=dev /lib/x86_64-linux-gnu/libuv.so.1 /lib/x86_64-linux-gnu/libuv.so.1

COPY --from=dev /workdir/example-libuv/target/scala-3.3.3/libuv /usr/bin/libuv-demo

ENTRYPOINT ["libuv-demo"]

it would fail with

120.7 [info] Generating intermediate code (2244 ms)
120.8 [error] /workdir/example-libuv/target/scala-3.3.3/native/dependencies/classes
-0/scala-native/generated/libuv.c:2:10: fatal error: 'uv.h' file not found
120.8 [error] #include "uv.h"

Installing libuv1-dev globally via apt helps:

RUN apt update && apt install -y libuv1-dev

but why vcpkg didn't download these header files and tuck them in wherever they should be?

keynmol commented 2 months ago

So to use vcpkg with SN, one thing you need to be aware of is using the correct plugin.

In the examples, I use VcpkgNativePlugin: https://github.com/indoorvivants/sn-bindgen-examples/blob/main/build.sbt#L389 - it differs from vanilla VcpkgPlugin in the fact that it automatically calls the vcpkg configurator to update the NativeConfig flags: https://github.com/indoorvivants/sn-vcpkg?tab=readme-ov-file#sbt

So far this has been the only necessary change to make sure libuv example compiles: https://github.com/indoorvivants/sn-bindgen-examples/blob/main/example-libuv/src/main/resources/scala-native/generated/libuv.c#L2

Also by default static binaries are produced, so you won't need libuv.so copied into the runtime image