Skip to content
This repository has been archived by the owner on Jun 28, 2022. It is now read-only.

Commit

Permalink
Adjust per-platform link libraries (#12)
Browse files Browse the repository at this point in the history
* 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.

* 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.
  • Loading branch information
morrisonlevi committed Jan 3, 2022
1 parent e9b9b9c commit daab60b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 9 deletions.
2 changes: 1 addition & 1 deletion cmake/DDProfConfig.cmake → cmake/DDProfConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions ddprof_ffi.pc.in
Original file line number Diff line number Diff line change
Expand Up @@ -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}
64 changes: 58 additions & 6 deletions ffi-build.sh
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -10,15 +11,66 @@ 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 }')"

# 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")
expected_native_static_libs=" -lssp_nonshared -lgcc_s -lc"
native_static_libs=" -lssp_nonshared -lc"
;;
"x86_64-apple-darwin")
expected_native_static_libs=" -framework Security -liconv -lSystem -lresolv -lc -lm -liconv"
native_static_libs="${expected_native_static_libs}"
;;
"x86_64-unknown-linux-gnu")
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}'"
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:${expected_native_static_libs}"

[ "${expected_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."

0 comments on commit daab60b

Please sign in to comment.