Skip to content

Building statically for x86_64-unknown-linux-musl #827

Description

@anti-social

I struggled building for x86_64-unknown-linux-musl. I used cross tool for this. What I found out is:

  • there is a bug in librdkafka which leads to an errors when compiling without curl support: WITH_CURL off still tries to include curl.h confluentinc/librdkafka#5204 . So I had to fork librdkafka and apply a patch (there are PRs linked from the issue on the way, hope it will be fixed soon)
  • then I tried to activate libz-static feature and got a lot of errors like:
 ...
 /usr/local/bin/../lib/gcc/x86_64-linux-musl/9.2.0/../../../../x86_64-linux-musl/bin/ld: /usr/include/x86_64-linux-gnu/bits/stdio2.h:67: undefined reference to `__snprintf_chk'
          collect2: error: ld returned 1 exit status

It turned out I should use cmake to cross-compile the library

  • after that I got another error:
  CMake Error at /usr/local/share/cmake-3.23/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
    Could NOT find ZLIB (missing: ZLIB_LIBRARY ZLIB_INCLUDE_DIR)
  Call Stack (most recent call first):
    /usr/local/share/cmake-3.23/Modules/FindPackageHandleStandardArgs.cmake:594 (_FPHSA_FAILURE_MESSAGE)
    /usr/local/share/cmake-3.23/Modules/FindZLIB.cmake:120 (FIND_PACKAGE_HANDLE_STANDARD_ARGS)
    src/CMakeLists.txt:221 (find_package)

I fixed it with a following change to build.rs:

@@ -238,7 +238,8 @@ fn build_librdkafka() {
         config.define("WITH_ZLIB", "1");
         config.register_dep("z");
         if let Ok(z_root) = env::var("DEP_Z_ROOT") {
-            cmake_library_paths.push(format!("{}/lib", z_root));
+            config.define("ZLIB_LIBRARY", format!("{}/include/lib/libz.a", z_root));
+            config.define("ZLIB_INCLUDE_DIR", format!("{}/include", z_root));
         }
     } else {
         config.define("WITH_ZLIB", "0");
  • then I activated zstd, it failed with an error:
  CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
  Please set them or make sure they are set and tested correctly in the CMake files:
  /home/alexk/projects/rust-rdkafka/rdkafka-sys/librdkafka/src/ZSTD_INCLUDE_DIR
     used as include directory in directory /home/alexk/projects/rust-rdkafka/rdkafka-sys/librdkafka/src
  ZSTD_LIBRARY (ADVANCED)
      linked by target "rdkafka" in directory /home/alexk/projects/rust-rdkafka/rdkafka-sys/librdkafka/src

Finally after patching I managed to statically cross-compile my program for musl with all the features I needed:

@@ -302,6 +303,10 @@ fn build_librdkafka() {
     if env::var("CARGO_FEATURE_ZSTD").is_ok() {
         config.define("WITH_ZSTD", "1");
         config.register_dep("zstd");
+        if let Ok(zstd_root) = env::var("DEP_ZSTD_ROOT") {
+            config.define("ZSTD_LIBRARY_RELEASE", format!("{}/libzstd.a", zstd_root));
+            config.define("ZSTD_INCLUDE_DIR", format!("{}/include", zstd_root));
+        }
     } else {
         config.define("WITH_ZSTD", "0");
     }

I could try to make a PR with patches and readme section how to cross compile the library.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions