Skip to content

Commit 552590e

Browse files
committed
[build-script] Introduce -n, --dry-run feature
If -n or --dry-run is specified in command line arguments, print the commands that would be executed to stdout, but do not execute them. Supported in build-script and build-toolchain. utils/build-script -n -Rt utils/build-script -n --preset=buildbot_incremental,tools=RA,stdlib=RA utils/build-toolchain -n local.swift
1 parent e74ebd5 commit 552590e

File tree

3 files changed

+92
-33
lines changed

3 files changed

+92
-33
lines changed

utils/build-script

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ def main_preset():
5757
parser = argparse.ArgumentParser(
5858
formatter_class=argparse.RawDescriptionHelpFormatter,
5959
description="""Builds Swift using a preset.""")
60+
parser.add_argument(
61+
"-n", "--dry-run",
62+
help="print the commands that would be executed, but do not execute "
63+
"them",
64+
action="store_true",
65+
default=False)
6066
parser.add_argument(
6167
"--preset-file",
6268
help="load presets from the specified file",
@@ -110,6 +116,8 @@ def main_preset():
110116
args.preset_substitutions, args.preset_file_names, args.preset)
111117

112118
build_script_args = [sys.argv[0]]
119+
if args.dry_run:
120+
build_script_args += ["--dry-run"]
113121
build_script_args += preset_args
114122
if args.distcc:
115123
build_script_args += ["--distcc"]
@@ -306,6 +314,13 @@ It is a policy decision aimed at making the builds uniform across all
306314
environments and easily reproducible by engineers who are not familiar with the
307315
details of the setups of other systems or automated environments.""")
308316

317+
parser.add_argument(
318+
"-n", "--dry-run",
319+
help="print the commands that would be executed, but do not execute "
320+
"them",
321+
action="store_true",
322+
default=False)
323+
309324
targets_group = parser.add_argument_group(
310325
title="Host and cross-compilation targets")
311326
targets_group.add_argument(
@@ -968,6 +983,8 @@ details of the setups of other systems or automated environments.""")
968983
if '--check-args-only' in args.build_script_impl_args:
969984
return 0
970985

986+
shell.dry_run = args.dry_run
987+
971988
# Prepare and validate toolchain
972989
toolchain = host_toolchain(xcrun_toolchain=args.darwin_xcrun_toolchain)
973990

@@ -1419,6 +1436,9 @@ details of the setups of other systems or automated environments.""")
14191436

14201437
build_script_impl_args += args.build_script_impl_args
14211438

1439+
if args.dry_run:
1440+
build_script_impl_args += ["--dry-run"]
1441+
14221442
check_call([build_script_impl] + build_script_impl_args,
14231443
disable_sleep=True)
14241444

utils/build-script-impl

Lines changed: 65 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ LLVM_TARGETS_TO_BUILD="X86;ARM;AArch64;PowerPC;SystemZ"
5555
# referred to as `SWIFT_INSTALL_COMPONENTS` in the remainder of this script.
5656
KNOWN_SETTINGS=(
5757
# name default description
58+
dry-run "" "print the commands that would be executed, but do not execute them"
5859
build-args "" "arguments to the build tool; defaults to -j8 when CMake generator is \"Unix Makefiles\""
5960
build-dir "" "out-of-tree build directory; default is in-tree. **This argument is required**"
6061
host-cc "" "the path to CC, the 'clang' compiler for the host platform. **This argument is required**"
@@ -222,23 +223,42 @@ KNOWN_SETTINGS=(
222223
# Centalized access point for traced command invocation.
223224
# Every operation that might mutates file system should be called via
224225
# these functions.
226+
225227
function call() {
226-
{ set -x; } 2>/dev/null
227-
"$@"
228-
{ set +x; } 2>/dev/null
228+
if [[ ${DRY_RUN} ]]; then
229+
echo "${PS4}"$(quoted_print "$@")
230+
else
231+
{ set -x; } 2>/dev/null
232+
"$@"
233+
{ set +x; } 2>/dev/null
234+
fi
229235
}
230236

231237
function with_pushd() {
232238
local dir=$1
233239
shift
234-
set -x
235-
pushd "${dir}"
236-
"$@"
237-
{ set -x; } 2>/dev/null # because $@ might includes { set +x; }
238-
popd
239-
{ set +x; } 2>/dev/null
240+
if [[ "$1" == "call" ]]; then
241+
shift
242+
fi
243+
if [[ ${DRY_RUN} ]]; then
244+
echo ${PS4}pushd "${dir}"
245+
echo "${PS4}"$(quoted_print "$@")
246+
echo ${PS4}popd
247+
else
248+
set -x
249+
pushd "${dir}"
250+
"$@"
251+
{ set -x; } 2>/dev/null # because $@ might includes { set +x; }
252+
popd
253+
{ set +x; } 2>/dev/null
254+
fi
240255
}
241256

257+
function quoted_print() {
258+
python -c 'import pipes; import sys; print(" ".join(pipes.quote(arg) for arg in sys.argv[1:]))' "$@"
259+
}
260+
261+
242262
function toupper() {
243263
echo "$@" | tr '[:lower:]' '[:upper:]'
244264
}
@@ -1434,7 +1454,7 @@ function set_swiftpm_bootstrap_command() {
14341454
XCTEST_BUILD_DIR=$(build_directory ${host} xctest)
14351455
fi
14361456
fi
1437-
if [ ! -e "${LLBUILD_BIN}" ]; then
1457+
if [ "${SKIP_BUILD_LLBUILD}" ]; then
14381458
echo "Error: Cannot build swiftpm without llbuild (swift-build-tool)."
14391459
exit 1
14401460
fi
@@ -2334,7 +2354,11 @@ for host in "${ALL_HOSTS[@]}"; do
23342354
if [[ "${target}" != "" ]]; then
23352355
echo "--- ${target} ---"
23362356
trap "tests_busted ${product} '(${target})'" ERR
2337-
if [[ "${CMAKE_GENERATOR}" == Ninja ]] && !( "${build_cmd[@]}" --version 2>&1 | grep -i -q llbuild ); then
2357+
2358+
# NOTE: In dry-run mode, build_dir might not exist yet. In that
2359+
# case, -n query will fail. So, in dry-run mode,
2360+
# we don't expand test script.
2361+
if [[ ! "${DRY_RUN}" && "${CMAKE_GENERATOR}" == Ninja ]] && !( "${build_cmd[@]}" --version 2>&1 | grep -i -q llbuild ); then
23382362
# Ninja buffers command output to avoid scrambling the output
23392363
# of parallel jobs, which is awesome... except that it
23402364
# interferes with the progress meter when testing. Instead of
@@ -2530,29 +2554,38 @@ for host in "${ALL_HOSTS[@]}"; do
25302554
done
25312555

25322556
if [[ "${DARWIN_INSTALL_EXTRACT_SYMBOLS}" ]] && [[ $(host_has_darwin_symbols ${host}) ]]; then
2533-
set -x
2534-
# Copy executables and shared libraries from the `host_install_destdir` to
2535-
# INSTALL_SYMROOT and run dsymutil on them.
2536-
(cd "${host_install_destdir}" &&
2537-
find ./"${TOOLCHAIN_PREFIX}" -perm -0111 -type f -print | cpio -pdm "${INSTALL_SYMROOT}")
2557+
echo "--- Extracting symbols ---"
25382558

2539-
# Run dsymutil on executables and shared libraries.
2540-
#
2541-
# Exclude shell scripts.
2542-
(cd "${INSTALL_SYMROOT}" &&
2543-
find ./"${TOOLCHAIN_PREFIX}" -perm -0111 -type f -print | \
2544-
grep -v swift-stdlib-tool | \
2545-
grep -v crashlog.py | \
2546-
grep -v symbolication.py | \
2547-
xargs -n 1 -P ${BUILD_JOBS} $(xcrun_find_tool dsymutil))
2548-
2549-
# Strip executables, shared libraries and static libraries in
2550-
# `host_install_destdir`.
2551-
find "${host_install_destdir}${TOOLCHAIN_PREFIX}/" \
2552-
-perm -0111 -or -name "*.a" -type f -print | \
2553-
xargs -n 1 -P ${BUILD_JOBS} $(xcrun_find_tool strip) -S
2559+
# FIXME: Since it's hard to trace output pipe call,
2560+
# For now, We don't support dry-run trace for this block
2561+
# Instead, just echo we do "darwin_intall_extract_symbols".
2562+
if [[ "${DRY_RUN}" ]]; then
2563+
call darwin_install_extract_symbols
2564+
else
2565+
set -x
2566+
# Copy executables and shared libraries from the `host_install_destdir` to
2567+
# INSTALL_SYMROOT and run dsymutil on them.
2568+
(cd "${host_install_destdir}" &&
2569+
find ./"${TOOLCHAIN_PREFIX}" -perm -0111 -type f -print | cpio -pdm "${INSTALL_SYMROOT}")
25542570

2555-
{ set +x; } 2>/dev/null
2571+
# Run dsymutil on executables and shared libraries.
2572+
#
2573+
# Exclude shell scripts.
2574+
(cd "${INSTALL_SYMROOT}" &&
2575+
find ./"${TOOLCHAIN_PREFIX}" -perm -0111 -type f -print | \
2576+
grep -v swift-stdlib-tool | \
2577+
grep -v crashlog.py | \
2578+
grep -v symbolication.py | \
2579+
xargs -n 1 -P ${BUILD_JOBS} $(xcrun_find_tool dsymutil))
2580+
2581+
# Strip executables, shared libraries and static libraries in
2582+
# `host_install_destdir`.
2583+
find "${host_install_destdir}${TOOLCHAIN_PREFIX}/" \
2584+
-perm -0111 -or -name "*.a" -type f -print | \
2585+
xargs -n 1 -P ${BUILD_JOBS} $(xcrun_find_tool strip) -S
2586+
2587+
{ set +x; } 2>/dev/null
2588+
fi
25562589
fi
25572590
done
25582591
# Everything is 'installed', but some products may be awaiting lipo.

utils/build-toolchain

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
cd "$(dirname $0)/.." || exit
1414
SRC_DIR=$PWD
1515

16+
DRY_RUN=
17+
if [[ "$1" == "-n" || "$1" == "--dry-run" ]] ; then
18+
DRY_RUN="-n"
19+
shift
20+
fi
21+
1622
YEAR=$(date +"%Y")
1723
MONTH=$(date +"%m")
1824
DAY=$(date +"%d")
@@ -37,7 +43,7 @@ else
3743
SWIFT_PACKAGE=buildbot_linux
3844
fi
3945

40-
./utils/build-script --preset="${SWIFT_PACKAGE}" \
46+
./utils/build-script ${DRY_RUN} --preset="${SWIFT_PACKAGE}" \
4147
install_destdir="${SWIFT_INSTALL_DIR}" \
4248
installable_package="${SWIFT_INSTALLABLE_PACKAGE}" \
4349
install_toolchain_dir="${SWIFT_TOOLCHAIN_DIR}" \

0 commit comments

Comments
 (0)