Skip to content

Commit 120ec99

Browse files
committed
[build-script] Archive symbols in Python
Rather than archiving symbols at the very end of the build-script-impl shellscript, do so at the end of the Python build-script. A small step towards achieving SR-237.
1 parent 3821e6e commit 120ec99

File tree

4 files changed

+103
-21
lines changed

4 files changed

+103
-21
lines changed

utils/build-script

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,15 @@ from SwiftBuildSupport import (
3333
get_preset_options,
3434
print_with_argv0,
3535
quote_shell_command,
36+
WorkingDirectory,
3637
)
3738

3839
sys.path.append(os.path.join(os.path.dirname(__file__), 'swift_build_support'))
3940
import swift_build_support.clang
4041
import swift_build_support.cmake
4142
import swift_build_support.debug
4243
import swift_build_support.ninja
44+
import swift_build_support.tar
4345
import swift_build_support.targets
4446
from swift_build_support.migration import migrate_impl_args
4547

@@ -336,6 +338,10 @@ details of the setups of other systems or automated environments.""")
336338
extra_actions_group.add_argument("--export-compile-commands",
337339
help="generate compilation databases in addition to building",
338340
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")
339345

340346
build_variant_group = parser.add_mutually_exclusive_group(required=False)
341347
build_variant_group.add_argument("-d", "--debug",
@@ -538,6 +544,9 @@ placed""",
538544
"(like bin, lib, and include) will be installed.",
539545
metavar="PATH",
540546
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")
541550

542551
parser.add_argument("-j", "--jobs",
543552
help="""
@@ -573,12 +582,24 @@ the number of parallel build jobs to use""",
573582
'--skip-build',
574583
'--show-sdks',
575584
'--install-prefix',
585+
'--install-symroot',
586+
'--symbols-package',
576587
]))
577588

578589
if args.host_target is None:
579590
print_with_argv0("Unknown operating system.")
580591
return 1
581592

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+
582603
# Build cmark if any cmark-related options were specified.
583604
if (args.cmark_build_variant is not None):
584605
args.build_cmark = True
@@ -891,6 +912,24 @@ the number of parallel build jobs to use""",
891912

892913
check_call(build_script_impl_args)
893914

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+
894933
return 0
895934

896935

utils/build-script-impl

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ KNOWN_SETTINGS=(
9393
llvm-install-components "" "a semicolon-separated list of LLVM components to install"
9494
installable-package "" "the path to the archive of the installation directory"
9595
test-installable-package "" "whether to run post-packaging tests on the produced package"
96-
symbols-package "" "the path to the archive of the symbols directory"
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"
@@ -662,14 +661,6 @@ case "${INSTALLABLE_PACKAGE}" in
662661
exit 1
663662
;;
664663
esac
665-
case "${SYMBOLS_PACKAGE}" in
666-
/*) ;;
667-
"") ;;
668-
*)
669-
echo "symbols-package must be an absolute path (was '${SYMBOLS_PACKAGE}')"
670-
exit 1
671-
;;
672-
esac
673664

674665
# WORKSPACE must exist
675666
if [ ! -e "${WORKSPACE}" ] ; then
@@ -2324,15 +2315,3 @@ if [[ "${INSTALLABLE_PACKAGE}" ]] ; then
23242315
{ set +x; } 2>/dev/null
23252316
fi
23262317
fi
2327-
2328-
if [[ "${SYMBOLS_PACKAGE}" ]] ; then
2329-
echo "--- Creating symbols package ---"
2330-
echo "-- Package file: ${SYMBOLS_PACKAGE} --"
2331-
if [[ "$(uname -s)" == "Darwin" ]] ; then
2332-
(cd "${INSTALL_SYMROOT}" &&
2333-
tar -c -z -f "${SYMBOLS_PACKAGE}" "${TOOLCHAIN_PREFIX/#\/}")
2334-
else
2335-
(cd "${INSTALL_SYMROOT}" &&
2336-
tar -c -z -f "${SYMBOLS_PACKAGE}" --owner=0 --group=0 "${INSTALL_PREFIX/#\/}")
2337-
fi
2338-
fi
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# swift_build_support/tar.py - Call tar from Python -*- 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+
import platform
12+
import subprocess
13+
14+
15+
def tar(source, destination):
16+
"""
17+
Create a gzip archive of the file at 'source' at the given
18+
'destination' path.
19+
"""
20+
# We do not use `tarfile` here because:
21+
# - We wish to support LZMA2 compression while also supporting Python 2.7.
22+
# - We wish to explicitly set the owner and group of the archive.
23+
args = ['tar', '-c', '-z', '-f', destination]
24+
25+
if platform.system() != 'Darwin':
26+
args += ['--owner=0', '--group=0']
27+
28+
# Capture stderr output such as 'tar: Failed to open ...'. We'll detect
29+
# these cases using the exit code, which should cause 'check_call' to
30+
# raise.
31+
subprocess.check_call(args + [source], stderr=subprocess.PIPE)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# test_tar.py - Unit tests for swift_build_support.tar -*- 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+
import os
12+
import subprocess
13+
import tempfile
14+
import unittest
15+
16+
from swift_build_support.tar import tar
17+
18+
19+
class TarTestCase(unittest.TestCase):
20+
def test_tar_this_file_succeeds(self):
21+
# `tar` complains about absolute paths, so use a relative path here.
22+
source = os.path.relpath(__file__)
23+
_, destination = tempfile.mkstemp()
24+
tar(source=source, destination=destination)
25+
26+
def test_tar_nonexistent_file_raises(self):
27+
with self.assertRaises(subprocess.CalledProcessError):
28+
tar(source='/path/to/something/that/surely/doesnt/exist',
29+
destination='/another/path/that/shouldnt/exist')
30+
31+
32+
if __name__ == '__main__':
33+
unittest.main()

0 commit comments

Comments
 (0)