Skip to content

Commit d0cbe06

Browse files
committed
[build-script] Validate and parse '--clang-compiler-version' in argparse
1 parent 4926ed5 commit d0cbe06

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

utils/build-script

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import argparse
1515
import multiprocessing
1616
import os
1717
import platform
18+
import re
1819
import shutil
1920
import sys
2021
import pipes
@@ -76,6 +77,16 @@ def argparse_shell_split(string):
7677
return list(lex)
7778

7879

80+
# Parse version string and split into a tuple of strings (major, minor, patch)
81+
# Support only "MAJOR.MINOR.PATCH" format.
82+
def argparse_clang_compiler_version(string):
83+
m = re.match(r'([0-9]*)\.([0-9]*)\.([0-9]*)', string)
84+
if m is not None:
85+
return m.group(1, 2, 3)
86+
raise argparse.ArgumentTypeError(
87+
"%r is invalid version value. must be 'MAJOR.MINOR.PATCH'" % string)
88+
89+
7990
# Main entry point for the preset mode.
8091
def main_preset():
8192
parser = argparse.ArgumentParser(
@@ -838,6 +849,7 @@ details of the setups of other systems or automated environments.""")
838849
parser.add_argument(
839850
"--clang-compiler-version",
840851
help="string that indicates a compiler version for Clang",
852+
type=argparse_clang_compiler_version,
841853
metavar="MAJOR.MINOR.PATCH")
842854

843855
parser.add_argument(
@@ -1286,7 +1298,7 @@ details of the setups of other systems or automated environments.""")
12861298
build_script_impl_args += ["--enable-ubsan"]
12871299
if args.clang_compiler_version:
12881300
build_script_impl_args += [
1289-
"--clang-compiler-version", args.clang_compiler_version
1301+
"--clang-compiler-version=%s.%s.%s" % args.clang_compiler_version
12901302
]
12911303
if args.verbose_build:
12921304
build_script_impl_args += ["--verbose-build"]

utils/swift_build_support/swift_build_support/cmake.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
from numbers import Number
1818
import platform
19-
import re
2019
import subprocess
2120

2221
from . import xcrun
@@ -129,12 +128,10 @@ def common_options(self):
129128
"Debug;Release;MinSizeRel;RelWithDebInfo")
130129

131130
if args.clang_compiler_version:
132-
m = re.match(r'([0-9]*)\.([0-9]*)\.([0-9]*)',
133-
args.clang_compiler_version)
134-
if m is not None:
135-
define("LLVM_VERSION_MAJOR:STRING", m.group(1))
136-
define("LLVM_VERSION_MINOR:STRING", m.group(2))
137-
define("LLVM_VERSION_PATCH:STRING", m.group(3))
131+
major, minor, patch = args.clang_compiler_version
132+
define("LLVM_VERSION_MAJOR:STRING", major)
133+
define("LLVM_VERSION_MINOR:STRING", minor)
134+
define("LLVM_VERSION_PATCH:STRING", patch)
138135

139136
return options
140137

utils/swift_build_support/tests/test_cmake.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def test_common_options_xcode(self):
141141

142142
def test_common_options_clang_compiler_version(self):
143143
args = self.default_args()
144-
args.clang_compiler_version = "3.8.0"
144+
args.clang_compiler_version = ("3", "8", "0")
145145
cmake = self.cmake(args)
146146
self.assertEqual(
147147
list(cmake.common_options()),
@@ -159,7 +159,7 @@ def test_common_options_full(self):
159159
args.export_compile_commands = True
160160
args.distcc = True
161161
args.cmake_generator = 'Xcode'
162-
args.clang_compiler_version = "3.8.0"
162+
args.clang_compiler_version = ("3", "8", "0")
163163
cmake = self.cmake(args)
164164
self.assertEqual(
165165
list(cmake.common_options()),

0 commit comments

Comments
 (0)