Skip to content

Commit 0ec93d0

Browse files
committed
wip: fix tests
1 parent 320803f commit 0ec93d0

File tree

4 files changed

+101
-18
lines changed

4 files changed

+101
-18
lines changed

.github/workflows/ci.yml

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,27 @@ jobs:
2626
- name: Build with Nix
2727
run: nix build
2828

29+
- name: Copy outputs outside nix store for portability checks
30+
run: |
31+
mkdir -p ${{ runner.temp }}/result
32+
cp -rL ./result/* ${{ runner.temp }}/result/
33+
2934
- name: Run core tests
3035
run: |
31-
export LGX_BINARY="$(pwd)/result/bin/lgx"
32-
./result/bin/lgx_tests
36+
export LGX_BINARY="${{ runner.temp }}/result/bin/lgx"
37+
"${{ runner.temp }}/result/bin/lgx_tests"
3338
3439
- name: Run library tests
35-
run: ./result/bin/lgx_lib_tests
40+
run: "${{ runner.temp }}/result/bin/lgx_lib_tests"
3641

3742
- name: Check nix refs in binaries
38-
run: ./scripts/check-nix-refs.sh ./result/bin/*
39-
43+
run: ./scripts/check-nix-refs.sh ${{ runner.temp }}/result/bin/*
44+
4045
- name: Check nix refs in libraries
41-
run: ./scripts/check-nix-refs.sh ./result/lib/*
42-
46+
run: ./scripts/check-nix-refs.sh ${{ runner.temp }}/result/lib/*
47+
4348
- name: Check bundle refs in binaries
44-
run: ./scripts/check-bundle-refs.sh ./result/bin/*
45-
49+
run: ./scripts/check-bundle-refs.sh ${{ runner.temp }}/result/bin/*
50+
4651
- name: Check bundle refs in libraries
47-
run: ./scripts/check-bundle-refs.sh ./result/lib/*
52+
run: ./scripts/check-bundle-refs.sh ${{ runner.temp }}/result/lib/*

nix/all.nix

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ pkgs.stdenv.mkDerivation {
7474
mkdir -p $out/bin
7575
cp build/tests/lgx_tests $out/bin/
7676
cp build/tests/lgx_lib_tests $out/bin/
77+
78+
# lgx_lib_tests links against liblgx.so; CMake embeds the build
79+
# directory in its RPATH. Fix it now so the standard Nix fixupPhase
80+
# check for /build/ references does not fail (postFixup runs too late).
81+
if command -v patchelf &>/dev/null; then
82+
patchelf --set-rpath '$ORIGIN:$ORIGIN/lib:$ORIGIN/../lib' $out/bin/lgx_lib_tests
83+
fi
7784
7885
runHook postInstall
7986
'';
@@ -237,20 +244,32 @@ pkgs.stdenv.mkDerivation {
237244
else
238245
echo "=== Making portable (Linux) ==="
239246
247+
# Glibc libraries that must come from the system. They contain
248+
# nix store paths in their data sections (locale/gconv paths) and
249+
# the dynamic linker must live at a fixed absolute path.
250+
is_system_lib() {
251+
case "$1" in
252+
ld-linux*|libc.so*|libpthread.so*|libdl.so*|libm.so*|librt.so*) return 0 ;;
253+
*) return 1 ;;
254+
esac
255+
}
256+
240257
# Bundle /nix/store/ deps from all binaries and libraries
241258
bundle_nix_deps() {
242259
local target="$1"
243-
ldd "$target" 2>/dev/null | grep '/nix/store/' | awk '{print $3}' | while IFS= read -r dep_path; do
260+
ldd "$target" 2>/dev/null | grep -o '/nix/store/[^ )]*' | while IFS= read -r dep_path; do
244261
[ -z "$dep_path" ] && continue
262+
[ -f "$dep_path" ] || continue
245263
local dep_name
246264
dep_name=$(basename "$dep_path")
265+
is_system_lib "$dep_name" && continue
247266
if [ ! -f "$out/lib/$dep_name" ]; then
248267
echo " bundling $dep_name"
249268
mkdir -p "$out/lib"
250269
cp "$dep_path" "$out/lib/$dep_name"
251270
chmod u+w "$out/lib/$dep_name"
252271
fi
253-
done
272+
done || true
254273
}
255274
256275
# Bundle deps from the shared library
@@ -292,6 +311,29 @@ pkgs.stdenv.mkDerivation {
292311
done
293312
fi
294313
314+
# Fix ELF interpreter: replace /nix/store/ interpreter with
315+
# the standard system path so binaries work without Nix.
316+
if [ -d "$out/bin" ]; then
317+
for exe in "$out/bin/"*; do
318+
[ -f "$exe" ] || continue
319+
if file "$exe" 2>/dev/null | grep -q 'ELF'; then
320+
local current_interp
321+
current_interp=$(patchelf --print-interpreter "$exe" 2>/dev/null) || continue
322+
case "$current_interp" in
323+
/nix/store/*)
324+
local interp_name
325+
interp_name=$(basename "$current_interp")
326+
local new_interp="/lib/$interp_name"
327+
# x86_64 conventionally uses /lib64
328+
[[ "$interp_name" == *x86-64* ]] && new_interp="/lib64/$interp_name"
329+
echo " setting interpreter on $(basename "$exe"): $new_interp"
330+
patchelf --set-interpreter "$new_interp" "$exe"
331+
;;
332+
esac
333+
fi
334+
done
335+
fi
336+
295337
echo "=== Linux portability fixup complete ==="
296338
fi
297339
'';

nix/bin.nix

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,20 +174,30 @@ pkgs.stdenv.mkDerivation {
174174
else
175175
echo "=== Making binary portable (Linux) ==="
176176
177+
# Glibc libraries that must come from the system.
178+
is_system_lib() {
179+
case "$1" in
180+
ld-linux*|libc.so*|libpthread.so*|libdl.so*|libm.so*|librt.so*) return 0 ;;
181+
*) return 1 ;;
182+
esac
183+
}
184+
177185
# Bundle /nix/store/ deps
178186
bundle_nix_deps() {
179187
local target="$1"
180-
ldd "$target" 2>/dev/null | grep '/nix/store/' | awk '{print $3}' | while IFS= read -r dep_path; do
188+
ldd "$target" 2>/dev/null | grep -o '/nix/store/[^ )]*' | while IFS= read -r dep_path; do
181189
[ -z "$dep_path" ] && continue
190+
[ -f "$dep_path" ] || continue
182191
local dep_name
183192
dep_name=$(basename "$dep_path")
193+
is_system_lib "$dep_name" && continue
184194
if [ ! -f "$out/lib/$dep_name" ]; then
185195
echo " bundling $dep_name"
186196
mkdir -p "$out/lib"
187197
cp "$dep_path" "$out/lib/$dep_name"
188198
chmod u+w "$out/lib/$dep_name"
189199
fi
190-
done
200+
done || true
191201
}
192202
193203
bundle_nix_deps "$out/bin/lgx"
@@ -209,6 +219,20 @@ pkgs.stdenv.mkDerivation {
209219
# Set RPATH on binary: search ./, ./lib/, ../lib/
210220
patchelf --set-rpath '$ORIGIN:$ORIGIN/lib:$ORIGIN/../lib' "$out/bin/lgx" 2>/dev/null || true
211221
222+
# Fix ELF interpreter
223+
local current_interp
224+
current_interp=$(patchelf --print-interpreter "$out/bin/lgx" 2>/dev/null) || true
225+
case "$current_interp" in
226+
/nix/store/*)
227+
local interp_name
228+
interp_name=$(basename "$current_interp")
229+
local new_interp="/lib/$interp_name"
230+
[[ "$interp_name" == *x86-64* ]] && new_interp="/lib64/$interp_name"
231+
echo " setting interpreter: $new_interp"
232+
patchelf --set-interpreter "$new_interp" "$out/bin/lgx"
233+
;;
234+
esac
235+
212236
echo "=== Linux portability fixup complete ==="
213237
fi
214238
'';

nix/lib.nix

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,32 +160,44 @@ pkgs.stdenv.mkDerivation {
160160
main_lib="$out/lib/liblgx.so"
161161
fi
162162
163+
# Glibc libraries that must come from the system.
164+
is_system_lib() {
165+
case "$1" in
166+
ld-linux*|libc.so*|libpthread.so*|libdl.so*|libm.so*|librt.so*) return 0 ;;
167+
*) return 1 ;;
168+
esac
169+
}
170+
163171
if [ -n "$main_lib" ]; then
164172
# Bundle all /nix/store/ dependencies
165-
ldd "$main_lib" 2>/dev/null | grep '/nix/store/' | awk '{print $3}' | while IFS= read -r dep_path; do
173+
ldd "$main_lib" 2>/dev/null | grep -o '/nix/store/[^ )]*' | while IFS= read -r dep_path; do
166174
[ -z "$dep_path" ] && continue
175+
[ -f "$dep_path" ] || continue
167176
local dep_name
168177
dep_name=$(basename "$dep_path")
178+
is_system_lib "$dep_name" && continue
169179
if [ ! -f "$out/lib/$dep_name" ]; then
170180
echo " bundling $dep_name"
171181
cp "$dep_path" "$out/lib/$dep_name"
172182
chmod u+w "$out/lib/$dep_name"
173183
fi
174-
done
184+
done || true
175185
176186
# Also bundle transitive deps from any copied libraries
177187
for pass in 1 2 3; do
178188
for bundled in "$out/lib/"*.so*; do
179-
ldd "$bundled" 2>/dev/null | grep '/nix/store/' | awk '{print $3}' | while IFS= read -r dep_path; do
189+
ldd "$bundled" 2>/dev/null | grep -o '/nix/store/[^ )]*' | while IFS= read -r dep_path; do
180190
[ -z "$dep_path" ] && continue
191+
[ -f "$dep_path" ] || continue
181192
local dep_name
182193
dep_name=$(basename "$dep_path")
194+
is_system_lib "$dep_name" && continue
183195
if [ ! -f "$out/lib/$dep_name" ]; then
184196
echo " bundling transitive dep $dep_name"
185197
cp "$dep_path" "$out/lib/$dep_name"
186198
chmod u+w "$out/lib/$dep_name"
187199
fi
188-
done
200+
done || true
189201
done
190202
done
191203

0 commit comments

Comments
 (0)