From 207d288ae7bd783089c38f19f4c70c5540c5199a Mon Sep 17 00:00:00 2001 From: Lars Toenning Date: Tue, 11 Jun 2024 23:10:47 +0200 Subject: [PATCH] build: Use argparse for CLI arguments in build script --- .github/workflows/build.yml | 8 ++-- scripts/build.py | 74 +++++++++++-------------------------- 2 files changed, 25 insertions(+), 57 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 639704591..7635dd055 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -156,7 +156,7 @@ jobs: - name: Build swift (with symbols) if: ${{ env.do_symbols == 'true' }} run: | - python3 -u scripts/build.py -w 64 -t gcc -c "-DVATSIM_KEY_JSON=$vatsim_key_file" --upload + python3 -u scripts/build.py -w 64 -t gcc -c "-DVATSIM_KEY_JSON=$vatsim_key_file" --upload-symbols - name: Upload installer uses: actions/upload-artifact@v4 with: @@ -254,7 +254,7 @@ jobs: - name: Build swift (with symbols) if: ${{ env.do_symbols == 'true' }} run: | - python -u scripts/build.py -w 64 -t msvc -c "-DVATSIM_KEY_JSON=$env:vatsim_key_file" --upload + python -u scripts/build.py -w 64 -t msvc -c "-DVATSIM_KEY_JSON=$env:vatsim_key_file" --upload-symbols - name: Upload installer uses: actions/upload-artifact@v4 with: @@ -352,7 +352,7 @@ jobs: - name: Build swift (with symbols) if: ${{ env.do_symbols == 'true' }} run: | - python -u scripts/build.py -w 32 -t msvc -c "-DVATSIM_KEY_JSON=$env:vatsim_key_file" --upload + python -u scripts/build.py -w 32 -t msvc -c "-DVATSIM_KEY_JSON=$env:vatsim_key_file" --upload-symbols - name: Upload installer uses: actions/upload-artifact@v4 with: @@ -440,7 +440,7 @@ jobs: - name: Build swift (with symbols) if: ${{ env.do_symbols == 'true' }} run: | - python3 -u scripts/build.py -w 64 -t clang -c "-DVATSIM_KEY_JSON=$vatsim_key_file" --upload + python3 -u scripts/build.py -w 64 -t clang -c "-DVATSIM_KEY_JSON=$vatsim_key_file" --upload-symbols - name: Upload installer uses: actions/upload-artifact@v4 with: diff --git a/scripts/build.py b/scripts/build.py index 3408f8156..58107a37c 100644 --- a/scripts/build.py +++ b/scripts/build.py @@ -3,13 +3,13 @@ # SPDX-FileCopyrightText: Copyright (C) 2017 swift Project Community / Contributors # SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1 -import getopt +import argparse import os import os.path as path import platform + import requests import subprocess -import sys import datastore import tarfile from lib.util import get_vs_env @@ -122,10 +122,10 @@ class Builder: os_map = {'Linux': 'linux', 'Darwin': 'macos', 'Windows': 'windows'} extension_map = {'Linux': 'run', 'Darwin': 'dmg', 'Windows': 'exe'} version_segments = self.version.split('.') - lastSegment = version_segments.pop() + last_segment = version_segments.pop() version_without_timestamp = '.'.join(version_segments) installer_name_old = '-'.join(['swiftinstaller', os_map[platform.system()], self.word_size, version_without_timestamp]) - installer_name_new = '.'.join([installer_name_old, lastSegment]) + installer_name_new = '.'.join([installer_name_old, last_segment]) installer_name_old = installer_name_old + '.' + extension_map[platform.system()] installer_name_new = installer_name_new + '.' + extension_map[platform.system()] build_path = os.path.abspath(path.join(self._get_swift_build_path(), installer_name_old)) @@ -390,48 +390,8 @@ class MacOSBuilder(Builder): Builder.__init__(self, word_size) -def print_help(): - supported_compilers = {'Linux': ['gcc'], - 'Darwin': ['clang'], - 'Windows': ['msvc'] - } - compiler_help = '|'.join(supported_compilers[platform.system()]) - print('build.py -w <32|64> -t <' + compiler_help + '> [-c ]') - - # Entry point if called as a standalone program -def main(argv): - word_size = '' - tool_chain = '' - upload_symbols = False - cmake_args = [] - - try: - opts, args = getopt.getopt(argv, 'hw:t:uc', ['wordsize=', 'toolchain=', 'upload', 'cmake-arg=']) - except getopt.GetoptError: - print_help() - sys.exit(2) - - if len(opts) == 0: - print_help() - sys.exit(2) - - for opt, arg in opts: - if opt == '-h': - print_help() - sys.exit() - elif opt in ('-w', '--wordsize'): - word_size = arg - elif opt in ('-t', '--toolchain'): - tool_chain = arg - elif opt in ('-u', '--upload'): - upload_symbols = True - elif opt in ('-c', '--cmake-arg'): - cmake_args += [arg] - - if word_size not in ['32', '64']: - print('Unsupported word size. Choose 32 or 64') - sys.exit(2) +def main(): builders = {'Linux': { 'gcc': LinuxBuilder}, @@ -442,23 +402,31 @@ def main(argv): } } - if tool_chain not in builders[platform.system()]: - print('Unknown or unsupported tool chain!') - sys.exit(2) + supported_wordsizes = ['32', '64'] if platform.system() == 'Windows' else ['64'] - builder = builders[platform.system()][tool_chain](word_size) + supported_toolchains = list(builders[platform.system()].keys()) + + parser = argparse.ArgumentParser(prog="swift build helper") + parser.add_argument("-w", "--wordsize", choices=supported_wordsizes, required=True, help='Wordsize for the build') + parser.add_argument("-t", "--toolchain", choices=supported_toolchains, required=True, help='Toolchain for the build') + parser.add_argument("-u", "--upload-symbols", action='store_true', help='Upload the symbols') + parser.add_argument("-c", "--cmake-args", action='extend', nargs="+", type=str, help='Arguments to pass to CMake') + + args = parser.parse_args() + + builder = builders[platform.system()][args.toolchain](args.wordsize) builder.prepare() - builder.build(cmake_args) + builder.build(args.cmake_args) builder.bundle_csl2xsb() builder.checks() builder.install() builder.publish() - if word_size == '64': + if args.wordsize == '64': builder.package_xswiftbus() - builder.symbols(upload_symbols) + builder.symbols(args.upload_symbols) # run main if run directly if __name__ == "__main__": - main(sys.argv[1:]) + main()