From f8c68520d2beb7f8d5cfaf8063526bb484398042 Mon Sep 17 00:00:00 2001 From: Levi Morrison Date: Sat, 1 Jan 2022 19:03:09 -0700 Subject: [PATCH 1/2] Adjust per-platform link libraries While investigating a sigsegv due to strange linking, I realized that the link libraries that Rust suggests are per platform. This commit adjusts the pkg-config and cmake helpers in a per-platform way. Note that this is brittle. Any change in these flags should be investigated by a human. --- ...ProfConfig.cmake => DDProfConfig.cmake.in} | 2 +- ddprof_ffi.pc.in | 4 +- ffi-build.sh | 52 ++++++++++++++++--- 3 files changed, 49 insertions(+), 9 deletions(-) rename cmake/{DDProfConfig.cmake => DDProfConfig.cmake.in} (94%) diff --git a/cmake/DDProfConfig.cmake b/cmake/DDProfConfig.cmake.in similarity index 94% rename from cmake/DDProfConfig.cmake rename to cmake/DDProfConfig.cmake.in index a3b258a..bd5c597 100644 --- a/cmake/DDProfConfig.cmake +++ b/cmake/DDProfConfig.cmake.in @@ -24,7 +24,7 @@ find_package_handle_standard_args(DDProf DEFAULT_MSG if (DDProf_FOUND) set(DDProf_INCLUDE_DIRS ${DDProf_INCLUDE_DIR}) - set(DDProf_LIBRARIES ${DDProf_FFI_LIBRARY}) + set(DDProf_LIBRARIES ${DDProf_FFI_LIBRARY} @DDProf_FFI_LIBRARIES@) mark_as_advanced( DDProf_ROOT DDProf_FFI_LIBRARY diff --git a/ddprof_ffi.pc.in b/ddprof_ffi.pc.in index fdd23ba..24c5d44 100644 --- a/ddprof_ffi.pc.in +++ b/ddprof_ffi.pc.in @@ -10,8 +10,8 @@ includedir=${prefix}/include Name: ddprof_ffi Description: Contains common code used to implement Datadog's Continuous Profilers. -Version: @version@ +Version: @DDProf_FFI_VERSION@ Requires: -Libs: -L${libdir} -lddprof_ffi -lpthread -ldl -lm +Libs: -L${libdir} -lddprof_ffi @DDProf_FFI_LIBRARIES@ Libs.private: Cflags: -I${includedir} diff --git a/ffi-build.sh b/ffi-build.sh index 398e8d1..6369cd3 100644 --- a/ffi-build.sh +++ b/ffi-build.sh @@ -1,7 +1,8 @@ #/bin/bash -# Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. -# This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021-Present Datadog, Inc. +# Unless explicitly stated otherwise all files in this repository are licensed +# under the Apache License Version 2.0. This product includes software developed +# at Datadog (https://www.datadoghq.com/). Copyright 2021-Present Datadog, Inc. set -eu @@ -10,15 +11,54 @@ destdir="$1" mkdir -v -p "$destdir/include/ddprof" "$destdir/lib/pkgconfig" "$destdir/cmake" version=$(awk -F\" '$1 ~ /^version/ { print $2 }' < ddprof-ffi/Cargo.toml) -sed "s/@version@/${version}/g" < ddprof_ffi.pc.in > "$destdir/lib/pkgconfig/ddprof_ffi.pc" -cp -v cmake/DDProfConfig.cmake "$destdir/cmake/" +target="$(rustc -vV | awk '/^host:/ { print $2 }')" + +case "$target" in + "x86_64-alpine-linux-musl") + native_static_libs=" -lssp_nonshared -lgcc_s -lc" + ;; + "x86_64-apple-darwin") + native_static_libs=" -framework Security -liconv -lSystem -lresolv -lc -lm -liconv" + ;; + "x86_64-unknown-linux-gnu") + native_static_libs=" -ldl -lrt -lpthread -lgcc_s -lc -lm -lrt -lpthread -lutil -ldl -lutil" + ;; + *) + >&2 echo "Unknown platform '${target}'" + exit 1 + ;; +esac + +echo "Recognized platform '${target}'. Adding libs: ${native_static_libs}" +sed < ddprof_ffi.pc.in "s/@DDProf_FFI_VERSION@/${version}/g" \ + | sed "s/@DDProf_FFI_LIBRARIES@/${native_static_libs}/g" \ + > "$destdir/lib/pkgconfig/ddprof_ffi.pc" + +sed < cmake/DDProfConfig.cmake.in \ + > $destdir/cmake/DDProfConfig.cmake \ + "s/@DDProf_FFI_LIBRARIES@/${native_static_libs}/g" + cp -v LICENSE LICENSE-3rdparty.yml NOTICE "$destdir/" -RUSTFLAGS="${RUSTFLAGS:- -C relocation-model=pic}" cargo build --release -cp -v target/release/libddprof_ffi.a "$destdir/lib/" +export RUSTFLAGS="${RUSTFLAGS:- -C relocation-model=pic}" + +echo "Building the libddprof_ffi.a library (may take some time)..." +cargo build --release --target ${target} +cp -v target/${target}/release/libddprof_ffi.a "$destdir/lib/" + +echo "Checking that native-static-libs are as expected for this platform..." +cd ddprof-ffi +actual_native_static_libs="$(cargo rustc --release --target ${target} -- --print=native-static-libs 2>&1 | awk -F ':' '/note: native-static-libs:/ { print $3 }')" +echo "Actual native-static-libs:${actual_native_static_libs}" +echo "Expected native-static-libs:${native_static_libs}" + +[ "${native_static_libs}" = "${actual_native_static_libs}" ] +cd - +echo "Generating the ddprof/ffi.h header..." cbindgen --crate ddprof-ffi --config ddprof-ffi/cbindgen.toml --output "$destdir/include/ddprof/ffi.h" # CI doesn't have any clang tooling # clang-format -i "$destdir/include/ddprof/ffi.h" +echo "Done." From af8f5bff58f212738d546e913ff807c853169b31 Mon Sep 17 00:00:00 2001 From: Levi Morrison Date: Mon, 3 Jan 2022 10:37:09 -0700 Subject: [PATCH 2/2] Remove -lgcc_s for Linux platforms This is because if it is present, even if -static-libgcc is provided, then it will be present in the final library runtime dependencies. This is undesirable at least on Alpine Linux, where it may not even be present due to the platform's minimalism. --- ffi-build.sh | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/ffi-build.sh b/ffi-build.sh index 6369cd3..9946dd3 100644 --- a/ffi-build.sh +++ b/ffi-build.sh @@ -13,15 +13,27 @@ mkdir -v -p "$destdir/include/ddprof" "$destdir/lib/pkgconfig" "$destdir/cmake" version=$(awk -F\" '$1 ~ /^version/ { print $2 }' < ddprof-ffi/Cargo.toml) target="$(rustc -vV | awk '/^host:/ { print $2 }')" +# Rust provides this note about the link libraries: +# note: Link against the following native artifacts when linking against this +# static library. The order and any duplication can be significant on some +# platforms. +# +# We've decided to strip out -lgcc_s because if it's provided then it will +# always make it into the final runtime dependencies, even if -static-libgcc is +# provided. At least on Alpine, libgcc_s may not even exist in the users' +# images, so -static-libgcc is recommended there. case "$target" in "x86_64-alpine-linux-musl") - native_static_libs=" -lssp_nonshared -lgcc_s -lc" + expected_native_static_libs=" -lssp_nonshared -lgcc_s -lc" + native_static_libs=" -lssp_nonshared -lc" ;; "x86_64-apple-darwin") - native_static_libs=" -framework Security -liconv -lSystem -lresolv -lc -lm -liconv" + expected_native_static_libs=" -framework Security -liconv -lSystem -lresolv -lc -lm -liconv" + native_static_libs="${expected_native_static_libs}" ;; "x86_64-unknown-linux-gnu") - native_static_libs=" -ldl -lrt -lpthread -lgcc_s -lc -lm -lrt -lpthread -lutil -ldl -lutil" + expected_native_static_libs=" -ldl -lrt -lpthread -lgcc_s -lc -lm -lrt -lpthread -lutil -ldl -lutil" + native_static_libs=" -ldl -lrt -lpthread -lc -lm -lrt -lpthread -lutil -ldl -lutil" ;; *) >&2 echo "Unknown platform '${target}'" @@ -50,9 +62,9 @@ echo "Checking that native-static-libs are as expected for this platform..." cd ddprof-ffi actual_native_static_libs="$(cargo rustc --release --target ${target} -- --print=native-static-libs 2>&1 | awk -F ':' '/note: native-static-libs:/ { print $3 }')" echo "Actual native-static-libs:${actual_native_static_libs}" -echo "Expected native-static-libs:${native_static_libs}" +echo "Expected native-static-libs:${expected_native_static_libs}" -[ "${native_static_libs}" = "${actual_native_static_libs}" ] +[ "${expected_native_static_libs}" = "${actual_native_static_libs}" ] cd - echo "Generating the ddprof/ffi.h header..."