Skip to content

Commit e95453e

Browse files
committed
[CMake] Merge Clang's features file with Swift's
Clang has a new `redirecting-with` property in the VFS overlay files. While this could be added to Swift's features as well, it is generally the case that features provided by Clang can also be useful to know for clients of Swift. Merge the features from Clang into Swift's features file with the "clang-" prefix to differentiate them.
1 parent fb178c6 commit e95453e

File tree

2 files changed

+86
-3
lines changed

2 files changed

+86
-3
lines changed

lib/Option/CMakeLists.txt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,20 @@ add_dependencies(swiftOption
66
target_link_libraries(swiftOption PRIVATE
77
swiftBasic)
88

9-
set(features_file_src "${CMAKE_CURRENT_SOURCE_DIR}/features.json")
9+
set(features_merger "${SWIFT_SOURCE_DIR}/utils/merge-features.py")
10+
set(features_file_swift_src "${CMAKE_CURRENT_SOURCE_DIR}/features.json")
11+
set(features_file_clang_src "${LLVM_MAIN_SRC_DIR}/../clang/tools/driver/features.json")
1012
set(features_file_dest "${CMAKE_BINARY_DIR}/share/swift/features.json")
1113

1214
add_custom_command(
1315
OUTPUT
1416
${features_file_dest}
1517
COMMAND
16-
${CMAKE_COMMAND} -E copy ${features_file_src} ${features_file_dest}
18+
${features_merger} -f ${features_file_swift_src} -p \"\" -f ${features_file_clang_src} -p clang- > ${features_file_dest}
1719
DEPENDS
18-
${features_file_src}
20+
${features_merger}
21+
${features_file_swift_src}
22+
${features_file_clang_src}
1923
)
2024
2125
add_custom_target(swift-features-file DEPENDS ${features_file_dest})

utils/merge-features.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/usr/bin/env python3
2+
3+
import argparse
4+
import json
5+
import sys
6+
7+
8+
def error(message):
9+
sys.stderr.write(message)
10+
sys.stderr.write("\n")
11+
sys.exit(1)
12+
13+
14+
def invalid_file(features_file, message):
15+
error(f"Invalid features file '{features_file}': {message}")
16+
17+
18+
def parse_args():
19+
parser = argparse.ArgumentParser(
20+
formatter_class=argparse.RawDescriptionHelpFormatter,
21+
description="""
22+
Merges the given feature files together into stdout. Each file FILE
23+
must be given a corresponding PREFIX to prefix the name of each entry
24+
in its features list, though these could be empty if no prefix is
25+
required.
26+
27+
Note that the files and prefixes are treated as an ordered list, ie.
28+
the first FILE corresponds to the first PREFIX.
29+
""")
30+
31+
parser.add_argument(
32+
"--file", "-f", action="append", dest="files",
33+
help="path of a file to merge"
34+
)
35+
parser.add_argument(
36+
"--prefix", "-p", action="append", dest="prefixes",
37+
help="prefix to prepend to the name of each feature"
38+
)
39+
40+
return parser.parse_known_args()
41+
42+
43+
def read_features(from_file, add_prefix):
44+
with open(from_file, "r") as f:
45+
features_dict = json.load(f)
46+
47+
if "features" not in features_dict:
48+
invalid_file(from_file, "missing 'features' key")
49+
50+
features = []
51+
for feature in features_dict["features"]:
52+
if "name" not in feature:
53+
invalid_file(from_file, "missing name in features list")
54+
55+
features.append({"name": add_prefix + feature["name"]})
56+
return features
57+
58+
59+
def main():
60+
(args, _) = parse_args()
61+
62+
if not args.files:
63+
error("No files to merge were provided")
64+
65+
if len(args.files) != len(args.prefixes):
66+
error("Must supply the same number of files and prefixes")
67+
68+
features = []
69+
for i, (f, prefix) in enumerate(zip(args.files, args.prefixes)):
70+
features.extend(read_features(f, prefix))
71+
72+
data = {
73+
"features": features
74+
}
75+
sys.stdout.write(json.dumps(data, indent=2))
76+
77+
78+
if __name__ == '__main__':
79+
main()

0 commit comments

Comments
 (0)