Skip to content

Commit

Permalink
build: Don't assume that libc.so is always in /usr/lib or /usr/lib64
Browse files Browse the repository at this point in the history
The location for public shared libraries can change from one operating
system distribution to another. eg., while Fedora uses /usr/lib and
/usr/lib64, depending on the hardware architecture, Debian uses paths
like /usr/lib/x86_64-linux-gnu. Therefore, it's best not to assume
anything and ask the toolchain.

#923
  • Loading branch information
debarshiray committed Nov 13, 2021
1 parent b80ffec commit c8aaed5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
3 changes: 3 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
project(
'toolbox',
'c',
version: '0.0.99.2',
license: 'ASL 2.0',
meson_version: '>= 0.53.0',
)

cc = meson.get_compiler('c')

go = find_program('go')
go_md2man = find_program('go-md2man')
patchelf = find_program('patchelf')
Expand Down
21 changes: 18 additions & 3 deletions src/go-build-wrapper
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
#


if [ "$#" -ne 3 ]; then
if [ "$#" -ne 4 ]; then
echo "go-build-wrapper: wrong arguments" >&2
echo "Usage: go-build-wrapper [SOURCE DIR] [OUTPUT DIR] [VERSION]" >&2
echo "Usage: go-build-wrapper [SOURCE DIR] [OUTPUT DIR] [VERSION] [C COMPILER]" >&2
exit 1
fi

Expand All @@ -27,7 +27,22 @@ if ! cd "$1"; then
exit 1
fi

go build -trimpath -ldflags "-extldflags '-Wl,-rpath,/run/host/usr/lib -Wl,-rpath,/run/host/usr/lib64' -linkmode external -X github.com/containers/toolbox/pkg/version.currentVersion=$3" -o "$2/toolbox"
if ! libc_dir=$("$4" --print-file-name=libc.so); then
echo "go-build-wrapper: failed to read the path to libc.so" >&2
exit 1
fi

if ! libc_dir_canonical=$(readlink --canonicalize "$libc_dir"); then
echo "go-build-wrapper: failed to canonicalize the path to libc.so" >&2
exit 1
fi

if ! libc_dir_canonical_dirname=$(dirname "$libc_dir_canonical"); then
echo "go-build-wrapper: failed to read the dirname of the canonicalized path to libc.so" >&2
exit 1
fi

go build -trimpath -ldflags "-extldflags '-Wl,-rpath,/run/host$libc_dir_canonical_dirname' -linkmode external -X github.com/containers/toolbox/pkg/version.currentVersion=$3" -o "$2/toolbox"

if ! interpreter=$(patchelf --print-interpreter "$2/toolbox"); then
echo "go-build-wrapper: failed to read PT_INTERP from $2/toolbox" >&2
Expand Down
1 change: 1 addition & 0 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ custom_target(
meson.current_source_dir(),
meson.current_build_dir(),
meson.project_version(),
cc.cmd_array().get(-1),
],
input: sources,
install: true,
Expand Down

0 comments on commit c8aaed5

Please sign in to comment.