Skip to content

Commit e40ebba

Browse files
committed
unix: build patchelf from source
The patchelf available in Debian Jessie is quite old and lacks a feature (rewriting DT_NEEDED entries) that an upcoming commit will want to use. This commit refactors the build system to build patchelf from source so we are using a modern patchelf.
1 parent 1ad672b commit e40ebba

File tree

7 files changed

+84
-5
lines changed

7 files changed

+84
-5
lines changed

cpython-unix/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@ $(OUTDIR)/libedit-$(LIBEDIT_VERSION)-$(PACKAGE_SUFFIX).tar: $(LIBEDIT_DEPENDS)
187187
$(OUTDIR)/libressl-$(LIBRESSL_VERSION)-$(PACKAGE_SUFFIX).tar: $(PYTHON_DEP_DEPENDS) $(HERE)/build-libressl.sh
188188
$(RUN_BUILD) --docker-image $(DOCKER_IMAGE_BUILD) libressl
189189

190+
$(OUTDIR)/patchelf-$(PATCHELF_VERSION)-$(PACKAGE_SUFFIX).tar: $(PYTHON_DEP_DEPENDS) $(HERE)/build-patchelf.sh
191+
$(RUN_BUILD) --docker-image $(DOCKER_IMAGE_BUILD) patchelf
192+
190193
READLINE_DEPENDS = \
191194
$(PYTHON_DEP_DEPENDS) \
192195
$(OUTDIR)/ncurses-$(NCURSES_VERSION)-$(PACKAGE_SUFFIX).tar \
@@ -258,6 +261,7 @@ PYTHON_DEPENDS := \
258261
$(if $(NEED_LIBRESSL),$(OUTDIR)/libressl-$(LIBRESSL_VERSION)-$(PACKAGE_SUFFIX).tar) \
259262
$(if $(NEED_NCURSES),$(OUTDIR)/ncurses-$(NCURSES_VERSION)-$(PACKAGE_SUFFIX).tar) \
260263
$(if $(NEED_OPENSSL),$(OUTDIR)/openssl-$(OPENSSL_VERSION)-$(PACKAGE_SUFFIX).tar) \
264+
$(if $(NEED_PATCHELF),$(OUTDIR)/patchelf-$(PATCHELF_VERSION)-$(PACKAGE_SUFFIX).tar) \
261265
$(if $(NEED_READLINE),$(OUTDIR)/readline-$(READLINE_VERSION)-$(PACKAGE_SUFFIX).tar) \
262266
$(if $(NEED_SQLITE),$(OUTDIR)/sqlite-$(SQLITE_VERSION)-$(PACKAGE_SUFFIX).tar) \
263267
$(if $(NEED_TIX),$(OUTDIR)/tix-$(TIX_VERSION)-$(PACKAGE_SUFFIX).tar) \

cpython-unix/build-patchelf.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env bash
2+
# This Source Code Form is subject to the terms of the Mozilla Public
3+
# License, v. 2.0. If a copy of the MPL was not distributed with this
4+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
5+
6+
set -ex
7+
8+
ROOT=`pwd`
9+
10+
export PATH=/tools/${TOOLCHAIN}/bin:/tools/host/bin:$PATH
11+
12+
tar -xf patchelf-${PATCHELF_VERSION}.tar.bz2
13+
14+
pushd patchelf-0.12.20200827.8d3a16e
15+
16+
CC="${HOST_CC}" CXX="${HOST_CXX}" CLFAGS="${EXTRA_HOST_CFLAGS} -fPIC" CPPFLAGS="${EXTRA_HOST_CFLAGS} -fPIC" \
17+
./configure \
18+
--build=${BUILD_TRIPLE} \
19+
--host=${TARGET_TRIPLE} \
20+
--prefix=/tools/host
21+
22+
make -j `nproc`
23+
make -j `nproc` install DESTDIR=${ROOT}/out
24+
25+
# Update DT_NEEDED to use the host toolchain's shared libraries, otherwise
26+
# the defaults of the OS may be used, which would be too old. We run the
27+
# patched binary afterwards to verify it works without LD_LIBRARY_PATH
28+
# modification.
29+
if [ -d /tools/clang-linux64/lib ]; then
30+
LD_LIBRARY_PATH=/tools/clang-linux64/lib src/patchelf --replace-needed libstdc++.so.6 /tools/clang-linux64/lib/libstdc++.so.6 ${ROOT}/out/tools/host/bin/patchelf
31+
LD_LIBRARY_PATH=/tools/clang-linux64/lib src/patchelf --replace-needed libgcc_s.so.1 /tools/clang-linux64/lib/libgcc_s.so.1 ${ROOT}/out/tools/host/bin/patchelf
32+
fi
33+
34+
${ROOT}/out/tools/host/bin/patchelf --version

cpython-unix/build.Dockerfile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,17 @@
77
# can build the ctypes and zlib extensions. So comment in build-cpython.sh
88
# for more context.
99
#
10-
# patchelf is needed by Python build to muck with rpath.
11-
#
1210
# Compression packages are needed to extract archives.
1311
#
1412
# Various other build tools are needed for various building.
1513
RUN apt-get install \
14+
bzip2 \
1615
file \
1716
libc6-dev \
1817
libc6-dev:i386 \
1918
libffi-dev \
2019
make \
2120
patch \
22-
patchelf \
2321
perl \
2422
pkg-config \
2523
tar \

cpython-unix/build.cross.Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@ RUN apt-get update
3131

3232
# Host building.
3333
RUN apt-get install \
34+
bzip2 \
3435
gcc \
36+
g++ \
3537
libc6-dev \
3638
libffi-dev \
3739
make \
3840
patch \
39-
patchelf \
4041
perl \
4142
pkg-config \
4243
tar \

cpython-unix/build.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ def add_target_env(env, build_platform, target_triple, build_env):
8080
settings = get_target_settings(TARGETS_CONFIG, target_triple)
8181

8282
env["HOST_CC"] = settings["host_cc"]
83+
env["HOST_CXX"] = settings["host_cxx"]
8384
env["CC"] = settings["target_cc"]
8485

8586
env["PYBUILD_PLATFORM"] = build_platform
@@ -206,6 +207,7 @@ def simple_build(
206207
optimizations,
207208
dest_archive,
208209
extra_archives=None,
210+
tools_path="deps",
209211
):
210212
archive = download_entry(entry, DOWNLOADS_PATH)
211213

@@ -238,7 +240,7 @@ def simple_build(
238240

239241
build_env.run("build-%s.sh" % entry, environment=env)
240242

241-
build_env.get_tools_archive(dest_archive, "deps")
243+
build_env.get_tools_archive(dest_archive, tools_path)
242244

243245

244246
def build_binutils(client, image, host_platform):
@@ -1047,6 +1049,7 @@ def main():
10471049
"libressl",
10481050
"ncurses",
10491051
"openssl",
1052+
"patchelf",
10501053
"sqlite",
10511054
"tcl",
10521055
"uuid",
@@ -1058,6 +1061,8 @@ def main():
10581061
"xz",
10591062
"zlib",
10601063
):
1064+
tools_path = "host" if action == "patchelf" else "deps"
1065+
10611066
simple_build(
10621067
settings,
10631068
client,
@@ -1067,6 +1072,7 @@ def main():
10671072
target_triple=target_triple,
10681073
optimizations=optimizations,
10691074
dest_archive=dest_archive,
1075+
tools_path=tools_path,
10701076
)
10711077

10721078
elif action == "libX11":

cpython-unix/targets.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
# Path to C compiler to use when producing binaries targeting the
1919
# current build environment.
2020
#
21+
# host_cxx
22+
# Path to C++ compiler to use when producing binaries targeting the
23+
# current build environment.
24+
#
2125
# target_cc
2226
# Path to C compiler to use for building binaries targeting the
2327
# target architecture.
@@ -55,6 +59,7 @@ aarch64-apple-darwin:
5559
- '3.9'
5660
needs_toolchain: true
5761
host_cc: clang
62+
host_cxx: clang++
5863
target_cc: clang
5964
apple_sdk_platform: macosx
6065
target_cflags:
@@ -97,6 +102,7 @@ aarch64-apple-ios:
97102
needs_toolchain: true
98103
apple_sdk_platform: iphoneos
99104
host_cc: clang
105+
host_cxx: clang++
100106
target_cc: clang
101107
target_cflags:
102108
- '-arch'
@@ -131,6 +137,7 @@ aarch64-unknown-linux-gnu:
131137
- '3.9'
132138
docker_image_suffix: .cross
133139
host_cc: /usr/bin/x86_64-linux-gnu-gcc
140+
host_cxx: /usr/bin/x86_64-linux-gnu-g++
134141
target_cc: /usr/bin/aarch64-linux-gnu-gcc
135142
needs:
136143
- bdb
@@ -143,6 +150,7 @@ aarch64-unknown-linux-gnu:
143150
- libXau
144151
- libxcb
145152
- ncurses
153+
- patchelf
146154
- readline
147155
- sqlite
148156
- tcl
@@ -162,6 +170,7 @@ arm64-apple-tvos:
162170
needs_toolchain: true
163171
apple_sdk_platform: appletvos
164172
host_cc: clang
173+
host_cxx: clang++
165174
target_cc: clang
166175
target_cflags:
167176
- '-arch'
@@ -195,6 +204,7 @@ armv7-unknown-linux-gnueabi:
195204
- '3.9'
196205
docker_image_suffix: .cross
197206
host_cc: /usr/bin/x86_64-linux-gnu-gcc
207+
host_cxx: /usr/bin/x86_64-linux-gnu-g++
198208
target_cc: /usr/bin/arm-linux-gnueabi-gcc
199209
needs:
200210
- bdb
@@ -207,6 +217,7 @@ armv7-unknown-linux-gnueabi:
207217
- libXau
208218
- libxcb
209219
- ncurses
220+
- patchelf
210221
- readline
211222
- sqlite
212223
- tcl
@@ -225,6 +236,7 @@ armv7-unknown-linux-gnueabihf:
225236
- '3.9'
226237
docker_image_suffix: .cross
227238
host_cc: /usr/bin/x86_64-linux-gnu-gcc
239+
host_cxx: /usr/bin/x86_64-linux-gnu-g++
228240
target_cc: /usr/bin/arm-linux-gnueabihf-gcc
229241
needs:
230242
- bdb
@@ -237,6 +249,7 @@ armv7-unknown-linux-gnueabihf:
237249
- libXau
238250
- libxcb
239251
- ncurses
252+
- patchelf
240253
- readline
241254
- sqlite
242255
- tcl
@@ -257,6 +270,7 @@ i686-unknown-linux-gnu:
257270
- '3.10'
258271
needs_toolchain: true
259272
host_cc: clang
273+
host_cxx: clang++
260274
target_cc: clang
261275
target_cflags:
262276
- '-m32'
@@ -275,6 +289,7 @@ i686-unknown-linux-gnu:
275289
- libXau
276290
- libxcb
277291
- ncurses
292+
- patchelf
278293
- readline
279294
- sqlite
280295
- tcl
@@ -293,6 +308,7 @@ mips-unknown-linux-gnu:
293308
- '3.9'
294309
docker_image_suffix: .cross
295310
host_cc: /usr/bin/x86_64-linux-gnu-gcc
311+
host_cxx: /usr/bin/x86_64-linux-gnu-g++
296312
target_cc: /usr/bin/mips-linux-gnu-gcc
297313
needs:
298314
- bdb
@@ -305,6 +321,7 @@ mips-unknown-linux-gnu:
305321
- libXau
306322
- libxcb
307323
- ncurses
324+
- patchelf
308325
- readline
309326
- sqlite
310327
- tcl
@@ -323,6 +340,7 @@ mipsel-unknown-linux-gnu:
323340
- '3.9'
324341
docker_image_suffix: .cross
325342
host_cc: /usr/bin/x86_64-linux-gnu-gcc
343+
host_cxx: /usr/bin/x86_64-linux-gnu-g++
326344
target_cc: /usr/bin/mipsel-linux-gnu-gcc
327345
needs:
328346
- bdb
@@ -335,6 +353,7 @@ mipsel-unknown-linux-gnu:
335353
- libXau
336354
- libxcb
337355
- ncurses
356+
- patchelf
338357
- readline
339358
- sqlite
340359
- tcl
@@ -353,6 +372,7 @@ s390x-unknown-linux-gnu:
353372
- '3.9'
354373
docker_image_suffix: .cross
355374
host_cc: /usr/bin/x86_64-linux-gnu-gcc
375+
host_cxx: /usr/bin/x86_64-linux-gnu-g++
356376
target_cc: /usr/bin/s390x-linux-gnu-gcc
357377
needs:
358378
- bdb
@@ -365,6 +385,7 @@ s390x-unknown-linux-gnu:
365385
- libXau
366386
- libxcb
367387
- ncurses
388+
- patchelf
368389
- readline
369390
- sqlite
370391
- tcl
@@ -384,6 +405,7 @@ thumb7k-apple-watchos:
384405
needs_toolchain: true
385406
apple_sdk_platform: watchos
386407
host_cc: clang
408+
host_cxx: clang++
387409
target_cc: clang
388410
target_cflags:
389411
- '-arch'
@@ -423,6 +445,7 @@ x86_64-apple-darwin:
423445
needs_toolchain: true
424446
apple_sdk_platform: macosx
425447
host_cc: clang
448+
host_cxx: clang++
426449
target_cc: clang
427450
target_cflags:
428451
- '-arch'
@@ -464,6 +487,7 @@ x86_64-apple-ios:
464487
needs_toolchain: true
465488
apple_sdk_platform: iphonesimulator
466489
host_cc: clang
490+
host_cxx: clang++
467491
target_cc: clang
468492
target_cflags:
469493
- '-arch'
@@ -499,6 +523,7 @@ x86_64-apple-tvos:
499523
needs_toolchain: true
500524
apple_sdk_platform: appletvsimulator
501525
host_cc: clang
526+
host_cxx: clang++
502527
target_cc: clang
503528
target_cflags:
504529
- '-arch'
@@ -532,6 +557,7 @@ x86_64-apple-watchos:
532557
needs_toolchain: true
533558
apple_sdk_platform: watchsimulator
534559
host_cc: clang
560+
host_cxx: clang++
535561
target_cc: clang
536562
target_cflags:
537563
- '-arch'
@@ -567,6 +593,7 @@ x86_64-unknown-linux-gnu:
567593
- '3.10'
568594
needs_toolchain: true
569595
host_cc: clang
596+
host_cxx: clang++
570597
target_cc: clang
571598
needs:
572599
- bdb
@@ -580,6 +607,7 @@ x86_64-unknown-linux-gnu:
580607
- libXau
581608
- libxcb
582609
- ncurses
610+
- patchelf
583611
- readline
584612
- sqlite
585613
- tcl
@@ -600,6 +628,7 @@ x86_64-unknown-linux-musl:
600628
- '3.10'
601629
needs_toolchain: true
602630
host_cc: clang
631+
host_cxx: clang++
603632
target_cc: musl-clang
604633
needs:
605634
- bdb
@@ -614,6 +643,7 @@ x86_64-unknown-linux-musl:
614643
- libxcb
615644
- musl
616645
- ncurses
646+
- patchelf
617647
- readline
618648
- sqlite
619649
- tcl

pythonbuild/downloads.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,12 @@
279279
"sha256": "8af0ae5ceed63fa8a2ded611d44cc341027a91df22aaaa071efedc81437412a5",
280280
"version": "2.11.06",
281281
},
282+
"patchelf": {
283+
"url": "https://github.com/NixOS/patchelf/releases/download/0.12/patchelf-0.12.tar.bz2",
284+
"size": 165069,
285+
"sha256": "699a31cf52211cf5ad6e35a8801eb637bc7f3c43117140426400d67b7babd792",
286+
"version": "0.12",
287+
},
282288
"pip": {
283289
"url": "https://files.pythonhosted.org/packages/94/b0/e10bdc8809c81796c80aa3644a8e3dc16594fb1bd68f5996929f26cad980/pip-21.1.1.tar.gz",
284290
"size": 1552786,

0 commit comments

Comments
 (0)