Skip to content

Commit 868a2f5

Browse files
committed
Avoid using patchelf entirely
1 parent dda9ac7 commit 868a2f5

File tree

6 files changed

+100
-47
lines changed

6 files changed

+100
-47
lines changed

openssl/build.sh

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ cd $build_dir
2626
tar -xf $version_dir/$src_filename
2727
cd $(basename $src_filename .tar.gz)
2828

29-
patch -p1 -i $recipe_dir/at_secure.patch
29+
patch -p1 -i "$recipe_dir/at_secure.patch"
30+
patch -p1 -i "$recipe_dir/configuration.patch"
31+
32+
version_major=$(echo "$version" | sed 's/\..*//')
33+
patch -p1 -i "$recipe_dir/soname-$version_major.patch"
3034

3135
# CFLAGS environment variable replaces default flags rather than adding to them.
3236
CFLAGS+=" -O2"
@@ -37,7 +41,7 @@ if [[ $HOST =~ '64' ]]; then
3741
else
3842
bits="32"
3943
fi
40-
./Configure linux-generic$bits shared
44+
./Configure android-python$bits shared
4145
make -j $CPU_COUNT
4246

4347
prefix=$build_dir/prefix
@@ -46,29 +50,3 @@ make install_sw DESTDIR=$prefix
4650

4751
mv $prefix/usr/local/* $prefix
4852
rm -r $prefix/usr
49-
50-
# We add a _python suffix in case libraries of the same name are provided by Android
51-
# itself. And we update the SONAME to match, so that anything compiled against the
52-
# library will store the modified name. This is necessary on API 22 and older, where the
53-
# dynamic linker ignores the SONAME attribute and uses the filename instead.
54-
cd $prefix/lib
55-
for name in crypto ssl; do
56-
old_name=$(basename $(realpath lib$name.so)) # Follow symlinks.
57-
58-
# Strip before patching, otherwise the libraries may be corrupted:
59-
# https://github.com/NixOS/patchelf/issues?q=is%3Aissue+strip+in%3Atitle
60-
"$STRIP" "$old_name"
61-
62-
new_name="lib${name}_python.so"
63-
if [ "$name" = "crypto" ]; then
64-
crypto_old_name=$old_name
65-
crypto_new_name=$new_name
66-
fi
67-
68-
mv "$old_name" "$new_name"
69-
ln -s "$new_name" "$old_name"
70-
patchelf --set-soname "$new_name" "$new_name"
71-
if [ "$name" = "ssl" ]; then
72-
patchelf --replace-needed "$crypto_old_name" "$crypto_new_name" "$new_name"
73-
fi
74-
done

openssl/configuration.patch

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--- a/Configurations/15-android-python.conf 1970-01-01 00:00:00.000000000 +0000
2+
+++ b/Configurations/15-android-python.conf 2024-10-12 15:18:42.238444227 +0000
3+
@@ -0,0 +1,18 @@
4+
+# CPython: OpenSSL comes with an "android" configuration, but we don't use it
5+
+# because it mostly duplicates what we already have in android-env.sh, but in a
6+
+# very complicated way.
7+
+
8+
+my %targets = (
9+
+ "android-python32" => {
10+
+ inherit_from => [ "linux-generic32" ],
11+
+
12+
+ # The Android Gradle plugin will only package libraries whose names end
13+
+ # with ".so".
14+
+ shared_extension => ".so",
15+
+ },
16+
+
17+
+ "android-python64" => {
18+
+ inherit_from => [ "linux-generic64" ],
19+
+ shared_extension => ".so",
20+
+ },
21+
+);

openssl/soname-1.patch

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--- a/Configurations/unix-Makefile.tmpl 2023-09-11 14:08:11.000000000 +0000
2+
+++ b/Configurations/unix-Makefile.tmpl 2024-10-12 17:57:17.055533654 +0000
3+
@@ -66,7 +66,20 @@
4+
sub shlib {
5+
my $lib = shift;
6+
return () if $disabled{shared} || $lib =~ /\.a$/;
7+
- return $unified_info{sharednames}->{$lib}. $shlibvariant. '$(SHLIB_EXT)';
8+
+
9+
+ # CPython: We add a _python suffix to avoid conflict with libraries of the
10+
+ # same name that are provided by Android itself, or other components of an
11+
+ # app.
12+
+ #
13+
+ # We used to do this with patchelf, but patching a library before stripping
14+
+ # it may corrupt it (https://github.com/NixOS/patchelf/issues/507). And the
15+
+ # Android Gradle plugin will strip all libraries in an app, if its preferred
16+
+ # NDK version is installed.
17+
+ #
18+
+ # We can't do this with the shlib_variant setting either, because that
19+
+ # would change the symbol versions, and break binary compatibliity with
20+
+ # existing Chaquopy wheels.
21+
+ return $unified_info{sharednames}->{$lib}. '_python'. '$(SHLIB_EXT)';
22+
}
23+
sub shlib_simple {
24+
my $lib = shift;

openssl/soname-3.patch

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--- a/Configurations/platform/Unix.pm 2024-09-03 13:30:13.000000000 +0000
2+
+++ b/Configurations/platform/Unix.pm 2024-10-12 17:53:57.971844640 +0000
3+
@@ -54,8 +54,20 @@
4+
}
5+
6+
sub sharedname {
7+
+ # CPython: We add a _python suffix to avoid conflict with libraries of the
8+
+ # same name that are provided by Android itself, or other components of an
9+
+ # app.
10+
+ #
11+
+ # We used to do this with patchelf, but patching a library before stripping
12+
+ # it may corrupt it (https://github.com/NixOS/patchelf/issues/507). And the
13+
+ # Android Gradle plugin will strip all libraries in an app, if its preferred
14+
+ # NDK version is installed.
15+
+ #
16+
+ # We can't do this with the shlib_variant setting either, because that
17+
+ # would change the symbol versions, and break binary compatibliity with
18+
+ # existing Chaquopy wheels.
19+
return platform::BASE::__concat(platform::BASE->sharedname($_[1]),
20+
- ($_[0]->shlibvariant() // ''));
21+
+ '_python');
22+
}
23+
24+
sub sharedname_simple {

sqlite/build.sh

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ cd $build_dir
2626
tar -xf $version_dir/$src_filename
2727
cd $(basename $src_filename .tar.gz)
2828

29-
patch -p1 -i $recipe_dir/strerror_r.patch
29+
patch -p1 -i "$recipe_dir/soname.patch"
30+
patch -p1 -i "$recipe_dir/strerror_r.patch"
3031

3132
CFLAGS+=" -Os" # This is off by default, but it's recommended in the README.
3233
./configure --host=$HOST --disable-static --disable-static-shell --with-pic
@@ -35,21 +36,3 @@ make -j $CPU_COUNT
3536
prefix=$build_dir/prefix
3637
mkdir $prefix
3738
make install prefix=$prefix
38-
39-
# We add a _python suffix in case libraries of the same name are provided by Android
40-
# itself. And we update the SONAME to match, so that anything compiled against the
41-
# library will store the modified name. This is necessary on API 22 and older, where the
42-
# dynamic linker ignores the SONAME attribute and uses the filename instead.
43-
cd $prefix/lib
44-
for name in sqlite3; do
45-
old_name=$(basename $(realpath lib$name.so)) # Follow symlinks.
46-
47-
# Strip before patching, otherwise the libraries may be corrupted:
48-
# https://github.com/NixOS/patchelf/issues?q=is%3Aissue+strip+in%3Atitle
49-
"$STRIP" "$old_name"
50-
51-
new_name="lib${name}_python.so"
52-
mv "$old_name" "$new_name"
53-
ln -s "$new_name" "$old_name"
54-
patchelf --set-soname "$new_name" "$new_name"
55-
done

sqlite/soname.patch

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--- a/configure 2024-04-15 13:46:16.000000000 +0000
2+
+++ b/configure 2024-10-12 18:41:30.139425614 +0000
3+
@@ -12522,8 +12522,18 @@
4+
version_type=none # Android doesn't support versioned libraries.
5+
need_lib_prefix=no
6+
need_version=no
7+
- library_names_spec='$libname$release$shared_ext'
8+
- soname_spec='$libname$release$shared_ext'
9+
+
10+
+ # CPython: We add a _python suffix to avoid conflict with libraries of the
11+
+ # same name that are provided by Android itself, or other components of an
12+
+ # app.
13+
+ #
14+
+ # We used to do this with patchelf, but patching a library before stripping
15+
+ # it may corrupt it (https://github.com/NixOS/patchelf/issues/507). And the
16+
+ # Android Gradle plugin will strip all libraries in an app, if its preferred
17+
+ # NDK version is installed.
18+
+ library_names_spec='${libname}_python$release$shared_ext $libname$release$shared_ext'
19+
+ soname_spec='${libname}_python$release$shared_ext'
20+
+
21+
finish_cmds=
22+
shlibpath_var=LD_LIBRARY_PATH
23+
shlibpath_overrides_runpath=yes

0 commit comments

Comments
 (0)