Skip to content

Commit 5c59362

Browse files
committed
Merge pull request #1267 from modocache/sr-237
[SR-237][build-script] Migrate more of build-script-impl to Python
2 parents e5848cc + 120ec99 commit 5c59362

File tree

8 files changed

+285
-88
lines changed

8 files changed

+285
-88
lines changed

utils/build-script

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ from __future__ import print_function
1414
import argparse
1515
import multiprocessing
1616
import os
17+
import platform
1718
import shutil
1819
import sys
1920
import textwrap
@@ -32,11 +33,15 @@ from SwiftBuildSupport import (
3233
get_preset_options,
3334
print_with_argv0,
3435
quote_shell_command,
36+
WorkingDirectory,
3537
)
3638

3739
sys.path.append(os.path.join(os.path.dirname(__file__), 'swift_build_support'))
3840
import swift_build_support.clang
3941
import swift_build_support.cmake
42+
import swift_build_support.debug
43+
import swift_build_support.ninja
44+
import swift_build_support.tar
4045
import swift_build_support.targets
4146
from swift_build_support.migration import migrate_impl_args
4247

@@ -333,6 +338,10 @@ details of the setups of other systems or automated environments.""")
333338
extra_actions_group.add_argument("--export-compile-commands",
334339
help="generate compilation databases in addition to building",
335340
action="store_true")
341+
extra_actions_group.add_argument("--symbols-package",
342+
metavar="PATH",
343+
help="if provided, an archive of the symbols directory will be "
344+
"generated at this path")
336345

337346
build_variant_group = parser.add_mutually_exclusive_group(required=False)
338347
build_variant_group.add_argument("-d", "--debug",
@@ -530,6 +539,14 @@ also build for Apple watchos, but disallow tests that require an watchOS device"
530539
name of the directory under $SWIFT_BUILD_ROOT where the build products will be
531540
placed""",
532541
metavar="PATH")
542+
parser.add_argument("--install-prefix",
543+
help="The installation prefix. This is where built Swift products "
544+
"(like bin, lib, and include) will be installed.",
545+
metavar="PATH",
546+
default=swift_build_support.targets.install_prefix())
547+
parser.add_argument("--install-symroot",
548+
help="the path to install debug symbols into",
549+
metavar="PATH")
533550

534551
parser.add_argument("-j", "--jobs",
535552
help="""
@@ -544,6 +561,9 @@ the number of parallel build jobs to use""",
544561
parser.add_argument("--cmake",
545562
help="the path to a CMake executable that will be "
546563
"used to build Swift")
564+
parser.add_argument("--show-sdks",
565+
help="print installed Xcode and SDK versions",
566+
action="store_true")
547567

548568
parser.add_argument("--extra-swift-args", help=textwrap.dedent("""
549569
Pass through extra flags to swift in the form of a cmake list 'module_regexp;flag'. Can
@@ -559,12 +579,27 @@ the number of parallel build jobs to use""",
559579
'--darwin-xcrun-toolchain',
560580
'--cmake',
561581
'--host-target',
582+
'--skip-build',
583+
'--show-sdks',
584+
'--install-prefix',
585+
'--install-symroot',
586+
'--symbols-package',
562587
]))
563588

564589
if args.host_target is None:
565590
print_with_argv0("Unknown operating system.")
566591
return 1
567592

593+
if args.symbols_package:
594+
if not os.path.isabs(args.symbols_package):
595+
print('--symbols-package must be an absolute path '
596+
'(was \'{}\')'.format(args.symbols_package))
597+
return 1
598+
if not args.install_symroot:
599+
print_with_argv0("--install-symroot is required when specifying "
600+
"--symbols-package.")
601+
return 1
602+
568603
# Build cmark if any cmark-related options were specified.
569604
if (args.cmark_build_variant is not None):
570605
args.build_cmark = True
@@ -726,7 +761,32 @@ the number of parallel build jobs to use""",
726761

727762
if args.skip_build:
728763
build_script_impl_inferred_args += [
729-
"--skip-build"
764+
"--skip-build-cmark",
765+
"--skip-build-llvm",
766+
"--skip-build-swift",
767+
"--skip-build-osx",
768+
"--skip-build-ios",
769+
"--skip-build-ios-device",
770+
"--skip-build-ios-simulator",
771+
"--skip-build-tvos",
772+
"--skip-build-tvos_device",
773+
"--skip-build-tvos-simulator",
774+
"--skip-build-watchos",
775+
"--skip-build-watchos-device",
776+
"--skip-build-watchos-simulator",
777+
"--skip-build-lldb",
778+
"--skip-build-llbuild",
779+
"--skip-build-swiftpm",
780+
"--skip-build-xctest",
781+
"--skip-build-foundation",
782+
"--skip-build-libdispatch",
783+
"--skip-build-benchmarks",
784+
]
785+
786+
if platform.system() == 'Darwin':
787+
build_script_impl_inferred_args += [
788+
"--toolchain-prefix",
789+
swift_build_support.targets.darwin_toolchain_prefix(args.install_prefix),
730790
]
731791

732792
if args.build_subdir is None:
@@ -804,6 +864,7 @@ the number of parallel build jobs to use""",
804864
build_script_impl_args = [
805865
os.path.join(SWIFT_SOURCE_ROOT, "swift", "utils", "build-script-impl"),
806866
"--build-dir", build_dir,
867+
"--install-prefix", os.path.abspath(args.install_prefix),
807868
"--host-target", args.host_target,
808869
"--host-cc", host_clang.cc,
809870
"--host-cxx", host_clang.cxx,
@@ -826,6 +887,9 @@ the number of parallel build jobs to use""",
826887
build_script_impl_args += [
827888
"--foundation-build-type", args.foundation_build_variant
828889
]
890+
if args.cmake_generator == 'Ninja' and \
891+
not swift_build_support.ninja.is_ninja_installed():
892+
build_script_impl_args += ["--build-ninja"]
829893
build_script_impl_args += build_script_impl_inferred_args
830894

831895
# If we have extra_swift_args, combine all of them together and then add
@@ -839,8 +903,33 @@ the number of parallel build jobs to use""",
839903
for v in ['MAKEFLAGS', 'SDKROOT', 'MACOSX_DEPLOYMENT_TARGET', 'IPHONEOS_DEPLOYMENT_TARGET']:
840904
os.environ.pop(v, None)
841905

906+
if args.show_sdks:
907+
swift_build_support.debug.print_xcodebuild_versions([
908+
'iphonesimulator',
909+
'appletvsimulator',
910+
'watchsimulator',
911+
])
912+
842913
check_call(build_script_impl_args)
843914

915+
if args.symbols_package:
916+
print('--- Creating symbols package ---')
917+
print('-- Package file: {} --'.format(args.symbols_package))
918+
919+
if platform.system() == 'Darwin':
920+
prefix = swift_build_support.targets.darwin_toolchain_prefix(
921+
args.install_prefix)
922+
else:
923+
prefix = args.install_prefix
924+
925+
# As a security measure, `tar` normally strips leading '/' from paths
926+
# it is archiving. To stay safe, we change working directories, then
927+
# run `tar` without the leading '/' (we remove it ourselves to keep
928+
# `tar` from emitting a warning).
929+
with WorkingDirectory(args.install_symroot):
930+
swift_build_support.tar.tar(source=prefix.lstrip('/'),
931+
destination=args.symbols_package)
932+
844933
return 0
845934

846935

utils/build-script-impl

Lines changed: 1 addition & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -86,22 +86,20 @@ KNOWN_SETTINGS=(
8686
cmake-generator "Unix Makefiles" "kind of build system to generate; see output of 'cmake --help' for choices"
8787
verbose-build "" "print the commands executed during the build"
8888
install-prefix "" "installation prefix"
89+
toolchain-prefix "" "the path to the .xctoolchain directory that houses the install prefix path"
8990
install-destdir "" "the path to use as the filesystem root for the installation"
9091
install-symroot "" "the path to install debug symbols into"
9192
swift-install-components "" "a semicolon-separated list of Swift components to install"
9293
llvm-install-components "" "a semicolon-separated list of LLVM components to install"
9394
installable-package "" "the path to the archive of the installation directory"
9495
test-installable-package "" "whether to run post-packaging tests on the produced package"
95-
symbols-package "" "the path to the archive of the symbols directory"
96-
show-sdks "" "print installed Xcode and SDK versions"
9796
reconfigure "" "force a CMake configuration run even if CMakeCache.txt already exists"
9897
swift-sdks "" "build target binaries only for specified SDKs (semicolon-separated list)"
9998
swift-primary-variant-sdk "" "default SDK for target binaries"
10099
swift-primary-variant-arch "" "default arch for target binaries"
101100
skip-ios "" "set to skip everything iOS-related"
102101
skip-tvos "" "set to skip everything tvOS-related"
103102
skip-watchos "" "set to skip everything watchOS-related"
104-
skip-build "" "set to skip building anything"
105103
skip-build-cmark "" "set to skip building CommonMark"
106104
skip-build-llvm "" "set to skip building LLVM/Clang"
107105
skip-build-swift "" "set to skip building Swift"
@@ -539,29 +537,6 @@ while [[ "$1" ]] ; do
539537
shift
540538
done
541539

542-
if [[ "${SKIP_BUILD}" ]]; then
543-
SKIP_BUILD_CMARK=1
544-
SKIP_BUILD_LLVM=1
545-
SKIP_BUILD_SWIFT=1
546-
SKIP_BUILD_OSX=1
547-
SKIP_BUILD_IOS=1
548-
SKIP_BUILD_IOS_DEVICE=1
549-
SKIP_BUILD_IOS_SIMULATOR=1
550-
SKIP_BUILD_TVOS=1
551-
SKIP_BUILD_TVOS_DEVICE=1
552-
SKIP_BUILD_TVOS_SIMULATOR=1
553-
SKIP_BUILD_WATCHOS=1
554-
SKIP_BUILD_WATCHOS_DEVICE=1
555-
SKIP_BUILD_WATCHOS_SIMULATOR=1
556-
SKIP_BUILD_LLDB=1
557-
SKIP_BUILD_LLBUILD=1
558-
SKIP_BUILD_SWIFTPM=1
559-
SKIP_BUILD_XCTEST=1
560-
SKIP_BUILD_FOUNDATION=1
561-
SKIP_BUILD_LIBDISPATCH=1
562-
SKIP_BUILD_BENCHMARKS=1
563-
fi
564-
565540
if [[ "${SKIP_IOS}" ]] ; then
566541
SKIP_BUILD_IOS=1
567542
SKIP_BUILD_IOS_DEVICE=1
@@ -658,12 +633,6 @@ if [[ "${SKIP_TEST_WATCHOS}" ]] ; then
658633
SKIP_TEST_WATCHOS_SIMULATOR=1
659634
fi
660635

661-
if [[ "${CMAKE_GENERATOR}" == "Ninja" && \
662-
-z "$(which ninja 2>/dev/null)" &&
663-
-z "$(which ninja-build 2>/dev/null)" ]] ; then
664-
BUILD_NINJA=1
665-
fi
666-
667636
# WORKSPACE, BUILD_DIR and INSTALLABLE_PACKAGE must be absolute paths
668637
case "${WORKSPACE}" in
669638
/*) ;;
@@ -692,14 +661,6 @@ case "${INSTALLABLE_PACKAGE}" in
692661
exit 1
693662
;;
694663
esac
695-
case "${SYMBOLS_PACKAGE}" in
696-
/*) ;;
697-
"") ;;
698-
*)
699-
echo "symbols-package must be an absolute path (was '${SYMBOLS_PACKAGE}')"
700-
exit 1
701-
;;
702-
esac
703664

704665
# WORKSPACE must exist
705666
if [ ! -e "${WORKSPACE}" ] ; then
@@ -745,23 +706,6 @@ function true_false() {
745706
esac
746707
}
747708

748-
#
749-
# Set default values for command-line parameters.
750-
#
751-
752-
if [[ "${INSTALL_PREFIX}" == "" ]] ; then
753-
if [[ "$(uname -s)" == "Darwin" ]] ; then
754-
INSTALL_PREFIX="/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr"
755-
else
756-
INSTALL_PREFIX="/usr"
757-
fi
758-
fi
759-
760-
if [[ "$(uname -s)" == "Darwin" ]] ; then
761-
TOOLCHAIN_PREFIX=$(echo ${INSTALL_PREFIX} | sed -E 's/\/usr$//')
762-
fi
763-
764-
765709
# A list of deployment targets to cross-compile the Swift host tools for.
766710
# We can't run the resulting binaries on the build machine.
767711
CROSS_TOOLS_DEPLOYMENT_TARGETS=()
@@ -1147,24 +1091,6 @@ if [[ "${CLANG_COMPILER_VERSION}" ]] ; then
11471091
)
11481092
fi
11491093

1150-
#
1151-
# Record SDK and tools versions for posterity
1152-
#
1153-
if [[ "${SHOW_SDKS}" ]] ; then
1154-
echo "--- SDK versions ---"
1155-
xcodebuild -version || :
1156-
echo
1157-
if [[ ! "${SKIP_IOS}" ]] ; then
1158-
xcodebuild -version -sdk iphonesimulator || :
1159-
fi
1160-
if [[ ! "${SKIP_TVOS}" ]] ; then
1161-
xcodebuild -version -sdk appletvsimulator || :
1162-
fi
1163-
if [[ ! "${SKIP_WATCHOS}" ]] ; then
1164-
xcodebuild -version -sdk watchsimulator || :
1165-
fi
1166-
fi
1167-
11681094
function build_directory() {
11691095
deployment_target=$1
11701096
product=$2
@@ -2389,15 +2315,3 @@ if [[ "${INSTALLABLE_PACKAGE}" ]] ; then
23892315
{ set +x; } 2>/dev/null
23902316
fi
23912317
fi
2392-
2393-
if [[ "${SYMBOLS_PACKAGE}" ]] ; then
2394-
echo "--- Creating symbols package ---"
2395-
echo "-- Package file: ${SYMBOLS_PACKAGE} --"
2396-
if [[ "$(uname -s)" == "Darwin" ]] ; then
2397-
(cd "${INSTALL_SYMROOT}" &&
2398-
tar -c -z -f "${SYMBOLS_PACKAGE}" "${TOOLCHAIN_PREFIX/#\/}")
2399-
else
2400-
(cd "${INSTALL_SYMROOT}" &&
2401-
tar -c -z -f "${SYMBOLS_PACKAGE}" --owner=0 --group=0 "${INSTALL_PREFIX/#\/}")
2402-
fi
2403-
fi
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# swift_build_support/debug.py - Print information on the build -*- python -*-
2+
#
3+
# This source file is part of the Swift.org open source project
4+
#
5+
# Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6+
# Licensed under Apache License v2.0 with Runtime Library Exception
7+
#
8+
# See http://swift.org/LICENSE.txt for license information
9+
# See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
#
11+
# ----------------------------------------------------------------------------
12+
#
13+
# Convenient functions for printing out information on the build process.
14+
#
15+
# ----------------------------------------------------------------------------
16+
17+
from __future__ import print_function
18+
19+
import subprocess
20+
import sys
21+
22+
23+
def _output(args):
24+
try:
25+
out = subprocess.check_output(args, stderr=subprocess.PIPE)
26+
return out.rstrip()
27+
except subprocess.CalledProcessError:
28+
return None
29+
30+
31+
def print_xcodebuild_versions(sdks, file=sys.stdout):
32+
"""
33+
Print the host machine's `xcodebuild` version, as well as version
34+
information for each of the given SDKs (for a full list of available
35+
SDKs, invoke `xcodebuild -showsdks` on the command line).
36+
"""
37+
print(u'--- SDK versions ---', file=file)
38+
print(u'{}\n'.format(_output(['xcodebuild', '-version'])), file=file)
39+
for sdk in sdks:
40+
print(u'{}\n'.format(_output(['xcodebuild', '-version', '-sdk', sdk])),
41+
file=file)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# swift_build_support/ninja.py - Detect host machine's Ninja -*- python -*-
2+
#
3+
# This source file is part of the Swift.org open source project
4+
#
5+
# Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6+
# Licensed under Apache License v2.0 with Runtime Library Exception
7+
#
8+
# See http://swift.org/LICENSE.txt for license information
9+
# See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
11+
from .which import which
12+
13+
14+
def is_ninja_installed():
15+
"""
16+
Return whether `ninja` or `ninja-build` are available on the host machine.
17+
"""
18+
if which('ninja') or which('ninja-build'):
19+
return True
20+
else:
21+
return False

0 commit comments

Comments
 (0)