Skip to content

Commit 7a4de28

Browse files
rigor789edusperoni
andauthored
fix(build): stop forcing a relink on every incremental build (#529)
* fix(build): stop forcing a relink on every incremental build The metadata files are link inputs, so pre-build and the metadata generator now leave them alone unless their content changed. The strip script skips simulator builds, whose extra architectures never ship. * fix(build): relink when only the metadata filters change Metadata is generated during linking, and existing metadata files no longer get new timestamps on every build, so Xcode skipped the link when only whitelist.mdg or blacklist.mdg changed and the app kept the previous metadata. Pre-build now touches the metadata files when the filters' hash changes. * fix(build): treat a missing metadata filter stamp as a change Metadata from before the stamp existed may have been generated with other filters. * fix(build): strip a symlinked framework binary through its target The strip loop now walks the top level of the Frameworks folder, so for a versioned framework it reaches Foo.framework/Foo, a symlink into Versions/Current. lipo -output on that path replaces the symlink with a regular file, leaving the bundle with two binaries and the real one unstripped, which breaks code signing. Resolve symlinks first and strip the binary they point to, as the previous find -type f walk did. Same for a dylib reached through an alias. --------- Co-authored-by: Eduardo Speroni <edusperoni@gmail.com>
1 parent 8b0b1d0 commit 7a4de28

5 files changed

Lines changed: 90 additions & 19 deletions

File tree

‎metadata-generator/build-step-metadata-generator.py‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import shlex
55
import subprocess
6+
import filecmp
67
import sys
78

89
print("Python version: " + sys.version)
@@ -126,7 +127,7 @@ def save_stream_to_file(filename, stream):
126127
def generate_metadata(arch):
127128
# metadata generator arguments
128129
generator_call = ["./objc-metadata-generator", "-verbose",
129-
"-output-bin", "{}/metadata-{}.bin".format(conf_build_dir, arch),
130+
"-output-bin", "{}/metadata-{}.bin.tmp".format(conf_build_dir, arch),
130131
"-output-umbrella", "{}/umbrella-{}.h".format(conf_build_dir, arch),
131132
"-docset-path", docset_path]
132133

@@ -192,6 +193,14 @@ def generate_metadata(arch):
192193
print(error_stream_content)
193194
sys.exit(1)
194195

196+
# Generated during linking, but also an input of the link (-sectcreate):
197+
# replacing unchanged metadata would make Xcode relink on every build.
198+
output_bin = "{}/metadata-{}.bin".format(conf_build_dir, arch)
199+
if os.path.exists(output_bin) and filecmp.cmp(output_bin + ".tmp", output_bin, shallow=False):
200+
os.remove(output_bin + ".tmp")
201+
else:
202+
os.replace(output_bin + ".tmp", output_bin)
203+
195204

196205
for arch in env("ARCHS").split():
197206
# skip metadata generation for architectures different than the one specified in the command line
Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,30 @@
11
#!/usr/bin/env bash
22

33
# Ignore errors while inserting environment variables (some could be readonly)
4-
(echo "set +e"; echo "set +o posix"; export; echo "set -o posix"; echo "set -e";) > ./.build_env_vars.sh
4+
ENV_FILE=./.build_env_vars.sh
5+
(echo "set +e"; echo "set +o posix"; export; echo "set -o posix"; echo "set -e";) > "$ENV_FILE.tmp"
6+
if cmp -s "$ENV_FILE.tmp" "$ENV_FILE"; then
7+
rm "$ENV_FILE.tmp"
8+
else
9+
mv "$ENV_FILE.tmp" "$ENV_FILE"
10+
fi
511

6-
# create temporary empty metadata files for XCode LD/LDPLUSPLUS flag analysis
12+
# create temporary empty metadata files for XCode LD/LDPLUSPLUS flag analysis.
13+
# Never touch existing ones: the link step reads them, so a newer timestamp
14+
# would make Xcode relink on every build.
715
for arch in $VALID_ARCHS
816
do :
9-
touch "$CONFIGURATION_BUILD_DIR/metadata-$arch.bin"
17+
[ -e "$CONFIGURATION_BUILD_DIR/metadata-$arch.bin" ] || touch "$CONFIGURATION_BUILD_DIR/metadata-$arch.bin"
1018
done
19+
20+
# The metadata generator reads these during linking, which Xcode would skip when
21+
# only they changed.
22+
FILTERS_STAMP="$CONFIGURATION_BUILD_DIR/metadata-filters.stamp"
23+
FILTERS_HASH=$(cd "$SRCROOT" && shasum whitelist.mdg blacklist.mdg 2>/dev/null || true)
24+
if [ ! -e "$FILTERS_STAMP" ] || [ "$FILTERS_HASH" != "$(cat "$FILTERS_STAMP")" ]; then
25+
for arch in $VALID_ARCHS
26+
do :
27+
touch "$CONFIGURATION_BUILD_DIR/metadata-$arch.bin"
28+
done
29+
printf '%s' "$FILTERS_HASH" > "$FILTERS_STAMP"
30+
fi

‎project-template-ios/internal/strip-dynamic-framework-architectures.sh‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,26 @@ if [ ! -e "${BUILT_PRODUCTS_DIR}/${FRAMEWORKS_FOLDER_PATH}" ]; then
4141
exit 0
4242
fi
4343

44-
echo "Stripping frameworks"
44+
# Simulator builds never ship, so extra architectures in them are harmless.
45+
if [[ "${PLATFORM_NAME}" == *simulator ]]; then
46+
exit 0
47+
fi
48+
4549
cd "${BUILT_PRODUCTS_DIR}/${FRAMEWORKS_FOLDER_PATH}"
4650

47-
for file in $(find . -type f); do
48-
# Skip non-dynamic libraries
49-
if ! [[ "$(file "$file")" == *"dynamically linked shared library"* ]]; then
51+
shopt -s nullglob
52+
for file in *.framework/* *.dylib; do
53+
# Only a framework's main binary (named after it) and loose dylibs are Mach-O
54+
if [[ "$file" == *.framework/* && "$(basename "$file")" != "$(basename "$(dirname "$file")" .framework)" ]]; then
5055
continue
5156
fi
52-
# Get architectures for current file
53-
archs="$(lipo -info "${file}" | rev | cut -d ':' -f1 | rev)"
57+
# A versioned framework's binary (and a dylib's alias) is a symlink, which
58+
# lipo -output would replace with a regular file; strip the real binary.
59+
if [ -L "$file" ]; then
60+
file="$(readlink -f "$file")"
61+
fi
62+
63+
archs="$(lipo -archs "$file" 2>/dev/null)" || continue
5464
stripped=""
5565
for arch in $archs; do
5666
if ! [[ "${VALID_ARCHS}" == *"$arch"* ]]; then
@@ -59,6 +69,7 @@ for file in $(find . -type f); do
5969
stripped="$stripped $arch"
6070
fi
6171
done
72+
6273
if [[ "$stripped" != "" ]]; then
6374
echo "Stripped $file of architectures:$stripped"
6475
if [ "${CODE_SIGNING_ALLOWED}" == "YES" ]; then
Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,30 @@
11
#!/usr/bin/env bash
22

33
# Ignore errors while inserting environment variables (some could be readonly)
4-
(echo "set +e"; echo "set +o posix"; export; echo "set -o posix"; echo "set -e";) > ./.build_env_vars.sh
4+
ENV_FILE=./.build_env_vars.sh
5+
(echo "set +e"; echo "set +o posix"; export; echo "set -o posix"; echo "set -e";) > "$ENV_FILE.tmp"
6+
if cmp -s "$ENV_FILE.tmp" "$ENV_FILE"; then
7+
rm "$ENV_FILE.tmp"
8+
else
9+
mv "$ENV_FILE.tmp" "$ENV_FILE"
10+
fi
511

6-
# create temporary empty metadata files for XCode LD/LDPLUSPLUS flag analysis
12+
# create temporary empty metadata files for XCode LD/LDPLUSPLUS flag analysis.
13+
# Never touch existing ones: the link step reads them, so a newer timestamp
14+
# would make Xcode relink on every build.
715
for arch in $VALID_ARCHS
816
do :
9-
touch "$CONFIGURATION_BUILD_DIR/metadata-$arch.bin"
17+
[ -e "$CONFIGURATION_BUILD_DIR/metadata-$arch.bin" ] || touch "$CONFIGURATION_BUILD_DIR/metadata-$arch.bin"
1018
done
19+
20+
# The metadata generator reads these during linking, which Xcode would skip when
21+
# only they changed.
22+
FILTERS_STAMP="$CONFIGURATION_BUILD_DIR/metadata-filters.stamp"
23+
FILTERS_HASH=$(cd "$SRCROOT" && shasum whitelist.mdg blacklist.mdg 2>/dev/null || true)
24+
if [ ! -e "$FILTERS_STAMP" ] || [ "$FILTERS_HASH" != "$(cat "$FILTERS_STAMP")" ]; then
25+
for arch in $VALID_ARCHS
26+
do :
27+
touch "$CONFIGURATION_BUILD_DIR/metadata-$arch.bin"
28+
done
29+
printf '%s' "$FILTERS_HASH" > "$FILTERS_STAMP"
30+
fi

‎project-template-vision/internal/strip-dynamic-framework-architectures.sh‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,26 @@ if [ ! -e "${BUILT_PRODUCTS_DIR}/${FRAMEWORKS_FOLDER_PATH}" ]; then
4141
exit 0
4242
fi
4343

44-
echo "Stripping frameworks"
44+
# Simulator builds never ship, so extra architectures in them are harmless.
45+
if [[ "${PLATFORM_NAME}" == *simulator ]]; then
46+
exit 0
47+
fi
48+
4549
cd "${BUILT_PRODUCTS_DIR}/${FRAMEWORKS_FOLDER_PATH}"
4650

47-
for file in $(find . -type f); do
48-
# Skip non-dynamic libraries
49-
if ! [[ "$(file "$file")" == *"dynamically linked shared library"* ]]; then
51+
shopt -s nullglob
52+
for file in *.framework/* *.dylib; do
53+
# Only a framework's main binary (named after it) and loose dylibs are Mach-O
54+
if [[ "$file" == *.framework/* && "$(basename "$file")" != "$(basename "$(dirname "$file")" .framework)" ]]; then
5055
continue
5156
fi
52-
# Get architectures for current file
53-
archs="$(lipo -info "${file}" | rev | cut -d ':' -f1 | rev)"
57+
# A versioned framework's binary (and a dylib's alias) is a symlink, which
58+
# lipo -output would replace with a regular file; strip the real binary.
59+
if [ -L "$file" ]; then
60+
file="$(readlink -f "$file")"
61+
fi
62+
63+
archs="$(lipo -archs "$file" 2>/dev/null)" || continue
5464
stripped=""
5565
for arch in $archs; do
5666
if ! [[ "${VALID_ARCHS}" == *"$arch"* ]]; then
@@ -59,6 +69,7 @@ for file in $(find . -type f); do
5969
stripped="$stripped $arch"
6070
fi
6171
done
72+
6273
if [[ "$stripped" != "" ]]; then
6374
echo "Stripped $file of architectures:$stripped"
6475
if [ "${CODE_SIGNING_ALLOWED}" == "YES" ]; then

0 commit comments

Comments
 (0)