Skip to content

Commit 4274233

Browse files
authored
[build-script] Argument Builder DSL Conversion: Episode 1 (#13117)
* Imported the new argparse overlay module and added the setup code for the DSL. * Converted the CMake generator flags to use the new builder DSL. * Converted the assertions argument group to use the new builder DSL. * Converted the LLVM-specific settings argument group to use the new builder DSL. * Converted the Android build settings argument group to use the new builder DSL. * Removed unused action aliases from the builder DSL setup section to appease the flake8 gods. * Fixed small typo in help message for --eclipse option.
1 parent 5412040 commit 4274233

File tree

2 files changed

+141
-147
lines changed

2 files changed

+141
-147
lines changed

utils/build_swift/defaults.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212

1313
__all__ = [
1414
# Command line configuarable
15+
'BUILD_VARIANT',
16+
'CMAKE_GENERATOR',
17+
'COMPILER_VENDOR',
1518
'SWIFT_USER_VISIBLE_VERSION',
1619
'CLANG_USER_VISIBLE_VERSION',
1720
'SWIFT_ANALYZE_CODE_COVERAGE',

utils/build_swift/driver_arguments.py

Lines changed: 138 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
88

99

10-
import argparse
1110
import multiprocessing
1211

1312
import android.adb.commands
@@ -18,6 +17,7 @@
1817
from swift_build_support.swift_build_support.targets import \
1918
StdlibDeploymentTarget
2019

20+
from . import argparse
2121
from . import defaults
2222

2323

@@ -224,22 +224,39 @@ def _apply_default_arguments(args):
224224
def create_argument_parser():
225225
"""Return a configured argument parser."""
226226

227+
# NOTE: USAGE, DESCRIPTION and EPILOG are defined at the bottom of the file
227228
parser = _ApplyDefaultsArgumentParser(
228229
apply_defaults=_apply_default_arguments,
229230
formatter_class=argparse.RawDescriptionHelpFormatter,
230231
usage=USAGE,
231232
description=DESCRIPTION,
232233
epilog=EPILOG)
233234

235+
builder = parser.to_builder()
236+
237+
# Prepare DSL functions
238+
option = builder.add_option
239+
set_defaults = builder.set_defaults
240+
in_group = builder.in_group
241+
mutually_exclusive_group = builder.mutually_exclusive_group
242+
243+
# Prepare DSL actions
244+
store = builder.actions.store
245+
store_path = builder.actions.store_path
246+
247+
# -------------------------------------------------------------------------
248+
# Top-level options
249+
234250
parser.add_argument(
235251
'-n', '--dry-run',
236252
action='store_true',
237253
default=False,
238254
help='print the commands that would be executed, but do not execute '
239255
'them')
240256
parser.add_argument(
241-
'--no-legacy-impl', dest='legacy_impl',
257+
'--no-legacy-impl',
242258
action='store_false',
259+
dest='legacy_impl',
243260
default=True,
244261
help='avoid legacy implementation')
245262

@@ -541,6 +558,7 @@ def create_argument_parser():
541558
'--skip-test-optimized',
542559
action=arguments.action.unavailable)
543560

561+
# -------------------------------------------------------------------------
544562
targets_group = parser.add_argument_group(
545563
title='Host and cross-compilation targets')
546564
targets_group.add_argument(
@@ -570,6 +588,7 @@ def create_argument_parser():
570588
help='A space-separated list that filters which of the configured '
571589
'targets to build the Swift standard library for, or "all".')
572590

591+
# -------------------------------------------------------------------------
573592
projects_group = parser.add_argument_group(
574593
title='Options to select projects')
575594
projects_group.add_argument(
@@ -622,6 +641,7 @@ def create_argument_parser():
622641
action=arguments.action.enable,
623642
help='build the Ninja tool')
624643

644+
# -------------------------------------------------------------------------
625645
extra_actions_group = parser.add_argument_group(
626646
title='Extra actions to perform before or in addition to building')
627647
extra_actions_group.add_argument(
@@ -638,6 +658,7 @@ def create_argument_parser():
638658
help='if provided, an archive of the symbols directory will be '
639659
'generated at this path')
640660

661+
# -------------------------------------------------------------------------
641662
build_variant_group = parser.add_mutually_exclusive_group(required=False)
642663
build_variant_group.add_argument(
643664
'-d', '--debug',
@@ -661,6 +682,7 @@ def create_argument_parser():
661682
dest='build_variant',
662683
help='build the Release variant of everything (default is Debug)')
663684

685+
# -------------------------------------------------------------------------
664686
build_variant_override_group = parser.add_argument_group(
665687
title='Override build variant for a specific project')
666688
build_variant_override_group.add_argument(
@@ -713,99 +735,73 @@ def create_argument_parser():
713735
dest='libicu_build_variant',
714736
help='build the Debug variant of libicu')
715737

716-
assertions_group = parser.add_mutually_exclusive_group(required=False)
717-
assertions_group.add_argument(
718-
'--assertions',
719-
action='store_const',
720-
const=True,
721-
dest='assertions',
722-
help='enable assertions in all projects')
723-
assertions_group.add_argument(
724-
'--no-assertions',
725-
action='store_const',
726-
const=False,
727-
dest='assertions',
728-
help='disable assertions in all projects')
729-
730-
assertions_override_group = parser.add_argument_group(
731-
title='Control assertions in a specific project')
732-
assertions_override_group.add_argument(
733-
'--cmark-assertions',
734-
action='store_const',
735-
const=True,
736-
dest='cmark_assertions',
737-
help='enable assertions in CommonMark')
738-
assertions_override_group.add_argument(
739-
'--llvm-assertions',
740-
action='store_const',
741-
const=True,
742-
dest='llvm_assertions',
743-
help='enable assertions in LLVM')
744-
assertions_override_group.add_argument(
745-
'--no-llvm-assertions',
746-
action='store_const',
747-
const=False,
748-
dest='llvm_assertions',
749-
help='disable assertions in LLVM')
750-
assertions_override_group.add_argument(
751-
'--swift-assertions',
752-
action='store_const',
753-
const=True,
754-
dest='swift_assertions',
755-
help='enable assertions in Swift')
756-
assertions_override_group.add_argument(
757-
'--no-swift-assertions',
758-
action='store_const',
759-
const=False,
760-
dest='swift_assertions',
761-
help='disable assertions in Swift')
762-
assertions_override_group.add_argument(
763-
'--swift-stdlib-assertions',
764-
action='store_const',
765-
const=True,
766-
dest='swift_stdlib_assertions',
767-
help='enable assertions in the Swift standard library')
768-
assertions_override_group.add_argument(
769-
'--no-swift-stdlib-assertions',
770-
action='store_const',
771-
const=False,
772-
dest='swift_stdlib_assertions',
773-
help='disable assertions in the Swift standard library')
774-
assertions_override_group.add_argument(
775-
'--lldb-assertions',
776-
action='store_const',
777-
const=True,
778-
dest='lldb_assertions',
779-
help='enable assertions in LLDB')
780-
assertions_override_group.add_argument(
781-
'--no-lldb-assertions',
782-
action='store_const',
783-
const=False,
784-
dest='lldb_assertions',
785-
help='disable assertions in LLDB')
786-
787-
# FIXME: This should be one option using choices=[...]
788-
cmake_generator_group = parser.add_argument_group(
789-
title='Select the CMake generator')
790-
cmake_generator_group.add_argument(
791-
'-x', '--xcode',
792-
action='store_const',
793-
const='Xcode',
794-
dest='cmake_generator',
795-
help="use CMake's Xcode generator (default is Ninja)")
796-
cmake_generator_group.add_argument(
797-
'-m', '--make',
798-
action='store_const',
799-
const='Unix Makefiles',
800-
dest='cmake_generator',
801-
help="use CMake's Makefile generator (default is Ninja)")
802-
cmake_generator_group.add_argument(
803-
'-e', '--eclipse',
804-
action='store_const',
805-
const='Eclipse CDT4 - Ninja',
806-
dest='cmake_generator',
807-
help="use CMake's Eclipse generator (default is Ninja)")
808-
738+
# -------------------------------------------------------------------------
739+
# Assertions group
740+
741+
with mutually_exclusive_group():
742+
set_defaults(assertions=True)
743+
744+
# TODO: Convert to store_true
745+
option('--assertions', store,
746+
const=True,
747+
help='enable assertions in all projects')
748+
749+
# TODO: Convert to store_false
750+
option('--no-assertions', store('assertions'),
751+
const=False,
752+
help='disable assertions in all projects')
753+
754+
# -------------------------------------------------------------------------
755+
in_group('Control assertions in a specific project')
756+
757+
option('--cmark-assertions', store,
758+
const=True,
759+
help='enable assertions in CommonMark')
760+
761+
option('--llvm-assertions', store,
762+
const=True,
763+
help='enable assertions in LLVM')
764+
option('--no-llvm-assertions', store('llvm_assertions'),
765+
const=False,
766+
help='disable assertions in LLVM')
767+
768+
option('--swift-assertions', store,
769+
const=True,
770+
help='enable assertions in Swift')
771+
option('--no-swift-assertions', store('swift_assertions'),
772+
const=False,
773+
help='disable assertions in Swift')
774+
775+
option('--swift-stdlib-assertions', store,
776+
const=True,
777+
help='enable assertions in the Swift standard library')
778+
option('--no-swift-stdlib-assertions', store('swift_stdlib_assertions'),
779+
const=False,
780+
help='disable assertions in the Swift standard library')
781+
782+
option('--lldb-assertions', store,
783+
const=True,
784+
help='enable assertions in LLDB')
785+
option('--no-lldb-assertions', store('lldb_assertions'),
786+
const=False,
787+
help='disable assertions in LLDB')
788+
789+
# -------------------------------------------------------------------------
790+
in_group('Select the CMake generator')
791+
792+
set_defaults(cmake_generator=defaults.CMAKE_GENERATOR)
793+
794+
option(['-e', '--eclipse'], store('cmake_generator'),
795+
const='Eclipse CDT4 - Ninja',
796+
help="use CMake's Eclipse generator (%(default)s by default)")
797+
option(['-m', '--make'], store('cmake_generator'),
798+
const='Unix Makefiles',
799+
help="use CMake's Makefile generator (%(default)s by default)")
800+
option(['-x', '--xcode'], store('cmake_generator'),
801+
const='Xcode',
802+
help="use CMake's Xcode generator (%(default)s by default)")
803+
804+
# -------------------------------------------------------------------------
809805
run_tests_group = parser.add_argument_group(
810806
title='Run tests')
811807

@@ -907,6 +903,7 @@ def create_argument_parser():
907903
dest='test_cygwin',
908904
help='skip testing Swift stdlibs for Cygwin')
909905

906+
# -------------------------------------------------------------------------
910907
run_build_group = parser.add_argument_group(
911908
title='Run build')
912909
run_build_group.add_argument(
@@ -1028,6 +1025,7 @@ def create_argument_parser():
10281025
dest='build_external_benchmarks',
10291026
help='skip building Swift Benchmark Suite')
10301027

1028+
# -------------------------------------------------------------------------
10311029
skip_test_group = parser.add_argument_group(
10321030
title='Skip testing specified targets')
10331031
skip_test_group.add_argument(
@@ -1093,58 +1091,51 @@ def create_argument_parser():
10931091
help='skip testing Android device targets on the host machine (the '
10941092
'phone itself)')
10951093

1096-
llvm_group = parser.add_argument_group(
1097-
title='Build settings specific for LLVM')
1098-
llvm_group.add_argument(
1099-
'--llvm-targets-to-build',
1100-
default='X86;ARM;AArch64;PowerPC;SystemZ;Mips',
1101-
help='LLVM target generators to build')
1102-
1103-
android_group = parser.add_argument_group(
1104-
title='Build settings for Android')
1105-
android_group.add_argument(
1106-
'--android-ndk',
1107-
metavar='PATH',
1108-
help='An absolute path to the NDK that will be used as a libc '
1109-
'implementation for Android builds')
1110-
android_group.add_argument(
1111-
'--android-api-level',
1112-
default='21',
1113-
help='The Android API level to target when building for Android. '
1114-
'Currently only 21 or above is supported')
1115-
android_group.add_argument(
1116-
'--android-ndk-gcc-version',
1117-
choices=['4.8', '4.9'],
1118-
default='4.9',
1119-
help='The GCC version to use when building for Android. Currently '
1120-
'only 4.9 is supported. %(default)s is also the default value. '
1121-
'This option may be used when experimenting with versions '
1122-
'of the Android NDK not officially supported by Swift')
1123-
android_group.add_argument(
1124-
'--android-icu-uc',
1125-
metavar='PATH',
1126-
help='Path to a directory containing libicuuc.so')
1127-
android_group.add_argument(
1128-
'--android-icu-uc-include',
1129-
metavar='PATH',
1130-
help='Path to a directory containing headers for libicuuc')
1131-
android_group.add_argument(
1132-
'--android-icu-i18n',
1133-
metavar='PATH',
1134-
help='Path to a directory containing libicui18n.so')
1135-
android_group.add_argument(
1136-
'--android-icu-i18n-include',
1137-
metavar='PATH',
1138-
help='Path to a directory containing headers libicui18n')
1139-
android_group.add_argument(
1140-
'--android-deploy-device-path',
1141-
default=android.adb.commands.DEVICE_TEMP_DIR,
1142-
metavar='PATH',
1143-
help='Path on an Android device to which built Swift stdlib products '
1144-
'will be deployed. If running host tests, specify the "{}" '
1145-
'directory.'.format(android.adb.commands.DEVICE_TEMP_DIR))
1146-
1147-
return parser
1094+
# -------------------------------------------------------------------------
1095+
in_group('Build settings specific for LLVM')
1096+
1097+
option('--llvm-targets-to-build', store,
1098+
default='X86;ARM;AArch64;PowerPC;SystemZ;Mips',
1099+
help='LLVM target generators to build')
1100+
1101+
# -------------------------------------------------------------------------
1102+
in_group('Build settings for Android')
1103+
1104+
option('--android-ndk', store_path,
1105+
help='An absolute path to the NDK that will be used as a libc '
1106+
'implementation for Android builds')
1107+
1108+
option('--android-api-level', store,
1109+
default='21',
1110+
help='The Android API level to target when building for Android. '
1111+
'Currently only 21 or above is supported')
1112+
1113+
option('--android-ndk-gcc-version', store,
1114+
choices=['4.8', '4.9'],
1115+
default='4.9',
1116+
help='The GCC version to use when building for Android. Currently '
1117+
'only 4.9 is supported. %(default)s is also the default '
1118+
'value. This option may be used when experimenting with '
1119+
'versions of the Android NDK not officially supported by '
1120+
'Swift')
1121+
1122+
option('--android-icu-uc', store_path,
1123+
help='Path to a directory containing libicuuc.so')
1124+
option('--android-icu-uc-include', store_path,
1125+
help='Path to a directory containing headers for libicuuc')
1126+
option('--android-icu-i18n', store_path,
1127+
help='Path to a directory containing libicui18n.so')
1128+
option('--android-icu-i18n-include', store_path,
1129+
help='Path to a directory containing headers libicui18n')
1130+
option('--android-deploy-device-path', store_path,
1131+
default=android.adb.commands.DEVICE_TEMP_DIR,
1132+
help='Path on an Android device to which built Swift stdlib '
1133+
'products will be deployed. If running host tests, specify '
1134+
'the "{}" directory.'.format(
1135+
android.adb.commands.DEVICE_TEMP_DIR))
1136+
1137+
# -------------------------------------------------------------------------
1138+
return builder.build()
11481139

11491140

11501141
# ----------------------------------------------------------------------------

0 commit comments

Comments
 (0)