Skip to content

Commit 88c63ca

Browse files
authored
Merge pull request #182513 from trofi/strip-for-host-and-target
gcc: enable stripping for cross-compilers
2 parents 0049ace + eece5d0 commit 88c63ca

File tree

14 files changed

+151
-107
lines changed

14 files changed

+151
-107
lines changed

doc/stdenv/stdenv.chapter.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,10 @@ If set, files in `$out/sbin` are not moved to `$out/bin`. By default, they are.
731731

732732
List of directories to search for libraries and executables from which *all* symbols should be stripped. By default, it’s empty. Stripping all symbols is risky, since it may remove not just debug symbols but also ELF information necessary for normal execution.
733733

734+
##### `stripAllListTarget` {#var-stdenv-stripAllListTarget}
735+
736+
Like `stripAllList`, but only applies to packages’ target platform. By default, it’s empty. Useful when supporting cross compilation.
737+
734738
##### `stripAllFlags` {#var-stdenv-stripAllFlags}
735739

736740
Flags passed to the `strip` command applied to the files in the directories listed in `stripAllList`. Defaults to `-s` (i.e. `--strip-all`).
@@ -739,6 +743,10 @@ Flags passed to the `strip` command applied to the files in the directories list
739743

740744
List of directories to search for libraries and executables from which only debugging-related symbols should be stripped. It defaults to `lib lib32 lib64 libexec bin sbin`.
741745

746+
##### `stripDebugListTarget` {#var-stdenv-stripDebugListTarget}
747+
748+
Like `stripDebugList`, but only applies to packages’ target platform. By default, it’s empty. Useful when supporting cross compilation.
749+
742750
##### `stripDebugFlags` {#var-stdenv-stripDebugFlags}
743751

744752
Flags passed to the `strip` command applied to the files in the directories listed in `stripDebugList`. Defaults to `-S` (i.e. `--strip-debug`).

pkgs/build-support/setup-hooks/strip.sh

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,39 @@ _doStrip() {
77
# to $out anyways---if it does, that's a bigger problem that a lack of
88
# stripping will help catch.
99
local -ra flags=(dontStripHost dontStripTarget)
10-
local -ra stripCmds=(STRIP TARGET_STRIP)
10+
local -ra debugDirs=(stripDebugList stripDebugListTarget)
11+
local -ra allDirs=(stripAllList stripAllListTarget)
12+
local -ra stripCmds=(STRIP STRIP_FOR_TARGET)
13+
local -ra ranlibCmds=(RANLIB RANLIB_FOR_TARGET)
1114

12-
# Optimization
13-
if [[ "${STRIP-}" == "${TARGET_STRIP-}" ]]; then
14-
dontStripTarget+=1
15-
fi
15+
# Strip only host paths by default. Leave targets as is.
16+
stripDebugList=${stripDebugList:-lib lib32 lib64 libexec bin sbin}
17+
stripDebugListTarget=${stripDebugListTarget:-}
18+
stripAllList=${stripAllList:-}
19+
stripAllListTarget=${stripAllListTarget:-}
1620

1721
local i
1822
for i in ${!stripCmds[@]}; do
1923
local -n flag="${flags[$i]}"
24+
local -n debugDirList="${debugDirs[$i]}"
25+
local -n allDirList="${allDirs[$i]}"
2026
local -n stripCmd="${stripCmds[$i]}"
27+
local -n ranlibCmd="${ranlibCmds[$i]}"
2128

2229
# `dontStrip` disables them all
2330
if [[ "${dontStrip-}" || "${flag-}" ]] || ! type -f "${stripCmd-}" 2>/dev/null
2431
then continue; fi
2532

26-
stripDebugList=${stripDebugList:-lib lib32 lib64 libexec bin sbin}
27-
if [ -n "$stripDebugList" ]; then
28-
stripDirs "$stripCmd" "$stripDebugList" "${stripDebugFlags:--S}"
29-
fi
30-
31-
stripAllList=${stripAllList:-}
32-
if [ -n "$stripAllList" ]; then
33-
stripDirs "$stripCmd" "$stripAllList" "${stripAllFlags:--s}"
34-
fi
33+
stripDirs "$stripCmd" "$ranlibCmd" "$debugDirList" "${stripDebugFlags:--S}"
34+
stripDirs "$stripCmd" "$ranlibCmd" "$allDirList" "${stripAllFlags:--s}"
3535
done
3636
}
3737

3838
stripDirs() {
3939
local cmd="$1"
40-
local dirs="$2"
41-
local stripFlags="$3"
40+
local ranlibCmd="$2"
41+
local dirs="$3"
42+
local stripFlags="$4"
4243
local dirsNew=
4344

4445
local d
@@ -50,8 +51,13 @@ stripDirs() {
5051
dirs=${dirsNew}
5152

5253
if [ -n "${dirs}" ]; then
53-
header "stripping (with command $cmd and flags $stripFlags) in$dirs"
54+
echo "stripping (with command $cmd and flags $stripFlags) in$dirs"
5455
find $dirs -type f -exec $cmd $stripFlags '{}' \; 2>/dev/null
55-
stopNest
56+
# 'strip' does not normally preserve archive index in .a files.
57+
# This usually causes linking failures against static libs like:
58+
# ld: ...-i686-w64-mingw32-stage-final-gcc-13.0.0-lib/i686-w64-mingw32/lib/libstdc++.dll.a:
59+
# error adding symbols: archive has no index; run ranlib to add one
60+
# Restore the index by running 'ranlib'.
61+
find $dirs -name '*.a' -type f -exec $ranlibCmd '{}' \; 2>/dev/null
5662
fi
5763
}

pkgs/development/compilers/gcc/10/default.nix

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@
2424
, libcCross ? null
2525
, threadsCross ? null # for MinGW
2626
, crossStageStatic ? false
27-
, # Strip kills static libs of other archs (hence no cross)
28-
stripped ? stdenv.hostPlatform.system == stdenv.buildPlatform.system
29-
&& stdenv.targetPlatform.system == stdenv.hostPlatform.system
3027
, gnused ? null
3128
, cloog # unused; just for compat with gcc4, as we override the parameter on some places
3229
, buildPackages
@@ -86,7 +83,7 @@ let majorVersion = "10";
8683
in
8784

8885
stdenv.mkDerivation ({
89-
pname = "${crossNameAddon}${name}${if stripped then "" else "-debug"}";
86+
pname = "${crossNameAddon}${name}";
9087
inherit version;
9188

9289
builder = ../builder.sh;
@@ -231,9 +228,11 @@ stdenv.mkDerivation ({
231228
(targetPlatform == hostPlatform && hostPlatform == buildPlatform)
232229
(if profiledCompiler then "profiledbootstrap" else "bootstrap");
233230

234-
dontStrip = !stripped;
235-
236-
installTargets = optional stripped "install-strip";
231+
inherit
232+
(import ../common/strip-attributes.nix { inherit stdenv; })
233+
stripDebugList
234+
stripDebugListTarget
235+
preFixup;
237236

238237
# https://gcc.gnu.org/install/specific.html#x86-64-x-solaris210
239238
${if hostPlatform.system == "x86_64-solaris" then "CC" else null} = "gcc -m64";
@@ -274,8 +273,7 @@ stdenv.mkDerivation ({
274273
meta = {
275274
homepage = "https://gcc.gnu.org/";
276275
license = lib.licenses.gpl3Plus; # runtime support libraries are typically LGPLv3+
277-
description = "GNU Compiler Collection, version ${version}"
278-
+ (if stripped then "" else " (with debugging info)");
276+
description = "GNU Compiler Collection, version ${version}";
279277

280278
longDescription = ''
281279
The GNU Compiler Collection includes compiler front ends for C, C++,

pkgs/development/compilers/gcc/11/default.nix

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@
2424
, libcCross ? null
2525
, threadsCross ? null # for MinGW
2626
, crossStageStatic ? false
27-
, # Strip kills static libs of other archs (hence no cross)
28-
stripped ? stdenv.hostPlatform.system == stdenv.buildPlatform.system
29-
&& stdenv.targetPlatform.system == stdenv.hostPlatform.system
3027
, gnused ? null
3128
, cloog # unused; just for compat with gcc4, as we override the parameter on some places
3229
, buildPackages
@@ -92,7 +89,7 @@ let majorVersion = "11";
9289
in
9390

9491
stdenv.mkDerivation ({
95-
pname = "${crossNameAddon}${name}${if stripped then "" else "-debug"}";
92+
pname = "${crossNameAddon}${name}";
9693
inherit version;
9794

9895
builder = ../builder.sh;
@@ -239,9 +236,11 @@ stdenv.mkDerivation ({
239236
(targetPlatform == hostPlatform && hostPlatform == buildPlatform)
240237
(if profiledCompiler then "profiledbootstrap" else "bootstrap");
241238

242-
dontStrip = !stripped;
243-
244-
installTargets = optional stripped "install-strip";
239+
inherit
240+
(import ../common/strip-attributes.nix { inherit stdenv; })
241+
stripDebugList
242+
stripDebugListTarget
243+
preFixup;
245244

246245
# https://gcc.gnu.org/install/specific.html#x86-64-x-solaris210
247246
${if hostPlatform.system == "x86_64-solaris" then "CC" else null} = "gcc -m64";
@@ -282,8 +281,7 @@ stdenv.mkDerivation ({
282281
meta = {
283282
homepage = "https://gcc.gnu.org/";
284283
license = lib.licenses.gpl3Plus; # runtime support libraries are typically LGPLv3+
285-
description = "GNU Compiler Collection, version ${version}"
286-
+ (if stripped then "" else " (with debugging info)");
284+
description = "GNU Compiler Collection, version ${version}";
287285

288286
longDescription = ''
289287
The GNU Compiler Collection includes compiler front ends for C, C++,

pkgs/development/compilers/gcc/12/default.nix

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@
2424
, libcCross ? null
2525
, threadsCross ? null # for MinGW
2626
, crossStageStatic ? false
27-
, # Strip kills static libs of other archs (hence no cross)
28-
stripped ? stdenv.hostPlatform.system == stdenv.buildPlatform.system
29-
&& stdenv.targetPlatform.system == stdenv.hostPlatform.system
3027
, gnused ? null
3128
, cloog # unused; just for compat with gcc4, as we override the parameter on some places
3229
, buildPackages
@@ -89,7 +86,7 @@ let majorVersion = "12";
8986
in
9087

9188
stdenv.mkDerivation ({
92-
pname = "${crossNameAddon}${name}${if stripped then "" else "-debug"}";
89+
pname = "${crossNameAddon}${name}";
9390
inherit version;
9491

9592
builder = ../builder.sh;
@@ -234,9 +231,11 @@ stdenv.mkDerivation ({
234231
(targetPlatform == hostPlatform && hostPlatform == buildPlatform)
235232
(if profiledCompiler then "profiledbootstrap" else "bootstrap");
236233

237-
dontStrip = !stripped;
238-
239-
installTargets = optional stripped "install-strip";
234+
inherit
235+
(import ../common/strip-attributes.nix { inherit stdenv; })
236+
stripDebugList
237+
stripDebugListTarget
238+
preFixup;
240239

241240
# https://gcc.gnu.org/install/specific.html#x86-64-x-solaris210
242241
${if hostPlatform.system == "x86_64-solaris" then "CC" else null} = "gcc -m64";
@@ -277,8 +276,7 @@ stdenv.mkDerivation ({
277276
meta = {
278277
homepage = "https://gcc.gnu.org/";
279278
license = lib.licenses.gpl3Plus; # runtime support libraries are typically LGPLv3+
280-
description = "GNU Compiler Collection, version ${version}"
281-
+ (if stripped then "" else " (with debugging info)");
279+
description = "GNU Compiler Collection, version ${version}";
282280

283281
longDescription = ''
284282
The GNU Compiler Collection includes compiler front ends for C, C++,

pkgs/development/compilers/gcc/4.8/default.nix

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@
2828
, libcCross ? null
2929
, threadsCross ? null # for MinGW
3030
, crossStageStatic ? false
31-
, # Strip kills static libs of other archs (hence no cross)
32-
stripped ? stdenv.hostPlatform == stdenv.buildPlatform
33-
&& stdenv.targetPlatform == stdenv.hostPlatform
3431
, gnused ? null
3532
, buildPackages
3633
}:
@@ -124,7 +121,7 @@ in
124121
assert x11Support -> (filter (x: x == null) ([ gtk2 libart_lgpl ] ++ xlibs)) == [];
125122

126123
stdenv.mkDerivation ({
127-
pname = "${crossNameAddon}${name}${if stripped then "" else "-debug"}";
124+
pname = "${crossNameAddon}${name}";
128125
inherit version;
129126

130127
builder = ../builder.sh;
@@ -238,12 +235,14 @@ stdenv.mkDerivation ({
238235
(targetPlatform == hostPlatform && hostPlatform == buildPlatform)
239236
(if profiledCompiler then "profiledbootstrap" else "bootstrap");
240237

241-
dontStrip = !stripped;
238+
inherit
239+
(import ../common/strip-attributes.nix { inherit stdenv; })
240+
stripDebugList
241+
stripDebugListTarget
242+
preFixup;
242243

243244
doCheck = false; # requires a lot of tools, causes a dependency cycle for stdenv
244245

245-
installTargets = optional stripped "install-strip";
246-
247246
# https://gcc.gnu.org/install/specific.html#x86-64-x-solaris210
248247
${if hostPlatform.system == "x86_64-solaris" then "CC" else null} = "gcc -m64";
249248

@@ -297,8 +296,7 @@ stdenv.mkDerivation ({
297296
meta = {
298297
homepage = "https://gcc.gnu.org/";
299298
license = lib.licenses.gpl3Plus; # runtime support libraries are typically LGPLv3+
300-
description = "GNU Compiler Collection, version ${version}"
301-
+ (if stripped then "" else " (with debugging info)");
299+
description = "GNU Compiler Collection, version ${version}";
302300

303301
longDescription = ''
304302
The GNU Compiler Collection includes compiler front ends for C, C++,

pkgs/development/compilers/gcc/4.9/default.nix

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@
2828
, libcCross ? null
2929
, threadsCross ? null # for MinGW
3030
, crossStageStatic ? false
31-
, # Strip kills static libs of other archs (hence no cross)
32-
stripped ? stdenv.hostPlatform == stdenv.buildPlatform
33-
&& stdenv.targetPlatform == stdenv.hostPlatform
3431
, gnused ? null
3532
, buildPackages
3633
}:
@@ -140,7 +137,7 @@ in
140137
assert x11Support -> (filter (x: x == null) ([ gtk2 libart_lgpl ] ++ xlibs)) == [];
141138

142139
stdenv.mkDerivation ({
143-
pname = "${crossNameAddon}${name}${if stripped then "" else "-debug"}";
140+
pname = "${crossNameAddon}${name}";
144141
inherit version;
145142

146143
builder = ../builder.sh;
@@ -258,12 +255,14 @@ stdenv.mkDerivation ({
258255
(targetPlatform == hostPlatform && hostPlatform == buildPlatform)
259256
(if profiledCompiler then "profiledbootstrap" else "bootstrap");
260257

261-
dontStrip = !stripped;
258+
inherit
259+
(import ../common/strip-attributes.nix { inherit stdenv; })
260+
stripDebugList
261+
stripDebugListTarget
262+
preFixup;
262263

263264
doCheck = false; # requires a lot of tools, causes a dependency cycle for stdenv
264265

265-
installTargets = optional stripped "install-strip";
266-
267266
# https://gcc.gnu.org/install/specific.html#x86-64-x-solaris210
268267
${if hostPlatform.system == "x86_64-solaris" then "CC" else null} = "gcc -m64";
269268

@@ -316,8 +315,7 @@ stdenv.mkDerivation ({
316315
meta = {
317316
homepage = "https://gcc.gnu.org/";
318317
license = lib.licenses.gpl3Plus; # runtime support libraries are typically LGPLv3+
319-
description = "GNU Compiler Collection, version ${version}"
320-
+ (if stripped then "" else " (with debugging info)");
318+
description = "GNU Compiler Collection, version ${version}";
321319

322320
longDescription = ''
323321
The GNU Compiler Collection includes compiler front ends for C, C++,

pkgs/development/compilers/gcc/6/default.nix

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@
3131
, libcCross ? null
3232
, threadsCross ? null # for MinGW
3333
, crossStageStatic ? false
34-
, # Strip kills static libs of other archs (hence no cross)
35-
stripped ? stdenv.hostPlatform.system == stdenv.buildPlatform.system
36-
&& stdenv.targetPlatform.system == stdenv.hostPlatform.system
3734
, gnused ? null
3835
, cloog # unused; just for compat with gcc4, as we override the parameter on some places
3936
, buildPackages
@@ -121,7 +118,7 @@ in
121118
assert x11Support -> (filter (x: x == null) ([ gtk2 libart_lgpl ] ++ xlibs)) == [];
122119

123120
stdenv.mkDerivation ({
124-
pname = "${crossNameAddon}${name}${if stripped then "" else "-debug"}";
121+
pname = "${crossNameAddon}${name}";
125122
inherit version;
126123

127124
builder = ../builder.sh;
@@ -270,12 +267,14 @@ stdenv.mkDerivation ({
270267
(targetPlatform == hostPlatform && hostPlatform == buildPlatform)
271268
(if profiledCompiler then "profiledbootstrap" else "bootstrap");
272269

273-
dontStrip = !stripped;
270+
inherit
271+
(import ../common/strip-attributes.nix { inherit stdenv; })
272+
stripDebugList
273+
stripDebugListTarget
274+
preFixup;
274275

275276
doCheck = false; # requires a lot of tools, causes a dependency cycle for stdenv
276277

277-
installTargets = optional stripped "install-strip";
278-
279278
# https://gcc.gnu.org/install/specific.html#x86-64-x-solaris210
280279
${if hostPlatform.system == "x86_64-solaris" then "CC" else null} = "gcc -m64";
281280

@@ -328,8 +327,7 @@ stdenv.mkDerivation ({
328327
meta = {
329328
homepage = "https://gcc.gnu.org/";
330329
license = lib.licenses.gpl3Plus; # runtime support libraries are typically LGPLv3+
331-
description = "GNU Compiler Collection, version ${version}"
332-
+ (if stripped then "" else " (with debugging info)");
330+
description = "GNU Compiler Collection, version ${version}";
333331

334332
longDescription = ''
335333
The GNU Compiler Collection includes compiler front ends for C, C++,

0 commit comments

Comments
 (0)