Skip to content

Commit b8b6b25

Browse files
committed
Swift: cmake generator for better IDE support
A cmake generator in bazel is introduced allowing to import the Swift extractor as a CMake project while keeping Bazel files as the source of truth for the build. Using the CMake project: * requires bazel and clang to be installed and available on the command line * does not require a previous bazel build, however * will require a CMake reconfiguration for changes to generated code (like changes to the schema)
1 parent 6c781b5 commit b8b6b25

File tree

6 files changed

+302
-1
lines changed

6 files changed

+302
-1
lines changed

misc/bazel/cmake/BUILD.bazel

Whitespace-only changes.

misc/bazel/cmake/cmake.bzl

Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,250 @@
1+
CmakeInfo = provider(
2+
fields = {
3+
"name": "",
4+
"inputs": "",
5+
"kind": "",
6+
"modifier": "",
7+
"hdrs": "",
8+
"srcs": "",
9+
"deps": "",
10+
"includes": "",
11+
"stripped_includes": "",
12+
"imported_static_libs": "",
13+
"imported_dynamic_libs": "",
14+
"copts": "",
15+
"linkopts": "",
16+
"force_cxx_compilation": "",
17+
"defines": "",
18+
"local_defines": "",
19+
"transitive_deps": "",
20+
},
21+
)
22+
23+
def _cmake_name(label):
24+
return ("%s_%s_%s" % (label.workspace_name, label.package, label.name)).replace("/", "_")
25+
26+
def _cmake_file(file):
27+
if not file.is_source:
28+
return "${BAZEL_EXEC_ROOT}/" + file.path
29+
return _cmake_path(file.path)
30+
31+
def _cmake_path(path):
32+
if path.startswith("external/"):
33+
return "${BAZEL_OUTPUT_BASE}/" + path
34+
return "${BAZEL_WORKSPACE}/" + path
35+
36+
def _file_kind(file):
37+
ext = file.extension
38+
if ext in ("c", "cc", "cpp"):
39+
return "src"
40+
if ext in ("h", "hh", "hpp", "def", "inc"):
41+
return "hdr"
42+
if ext == "a":
43+
return "static_lib"
44+
if ext in ("so", "dylib"):
45+
return "dynamic_lib"
46+
return None
47+
48+
def _cmake_aspect_impl(target, ctx):
49+
if not ctx.rule.kind.startswith("cc_"):
50+
return [CmakeInfo(name = None, transitive_deps = depset())]
51+
52+
name = _cmake_name(ctx.label)
53+
54+
is_macos = "darwin" in ctx.var["TARGET_CPU"]
55+
56+
is_binary = ctx.rule.kind == "cc_binary"
57+
force_cxx_compilation = "force_cxx_compilation" in ctx.rule.attr.features
58+
attr = ctx.rule.attr
59+
srcs = attr.srcs + getattr(attr, "hdrs", []) + getattr(attr, "textual_hdrs", [])
60+
srcs = [f for src in srcs for f in src.files.to_list()]
61+
inputs = [f for f in srcs if not f.is_source or f.path.startswith("external/")]
62+
by_kind = {}
63+
for f in srcs:
64+
by_kind.setdefault(_file_kind(f), []).append(_cmake_file(f))
65+
hdrs = by_kind.get("hdr", [])
66+
srcs = by_kind.get("src", [])
67+
static_libs = by_kind.get("static_lib", [])
68+
dynamic_libs = by_kind.get("dynamic_lib", [])
69+
if not srcs and is_binary:
70+
empty = ctx.actions.declare_file(name + "_empty.cpp")
71+
ctx.actions.write(empty, "")
72+
inputs.append(empty)
73+
srcs = [_cmake_file(empty)]
74+
deps = ctx.rule.attr.deps if hasattr(ctx.rule.attr, "deps") else []
75+
76+
cxx_compilation = force_cxx_compilation or any([not src.endswith(".c") for src in srcs])
77+
78+
copts = ctx.fragments.cpp.copts + (ctx.fragments.cpp.cxxopts if cxx_compilation else ctx.fragments.cpp.conlyopts)
79+
copts += [ctx.expand_make_variables("copts", o, {}) for o in ctx.rule.attr.copts]
80+
81+
linkopts = ctx.fragments.cpp.linkopts
82+
linkopts += [ctx.expand_make_variables("linkopts", o, {}) for o in ctx.rule.attr.linkopts]
83+
84+
compilation_ctx = target[CcInfo].compilation_context
85+
includes = compilation_ctx.system_includes.to_list()
86+
includes += compilation_ctx.includes.to_list()
87+
includes += compilation_ctx.quote_includes.to_list()
88+
includes += [opt[2:] for opt in copts if opt.startswith("-I")]
89+
90+
# strip prefix is special, as in bazel it creates a _virtual_includes directory with symlinks
91+
# as we want to avoid relying on bazel having done that, we must undo that mechanism
92+
# also for some reason cmake fails to propagate these with target_include_directories,
93+
# so we propagate them ourselvels by using the stripped_includes field
94+
# also, including '.' on macOS creates a conflict between a `version` file at the root of the
95+
# workspace and a standard library, so we skip that (and hardcode an `-iquote .` in setup.cmake)
96+
includes = [_cmake_path(i) for i in includes if not ("/_virtual_includes/" in i or (is_macos and i == "."))]
97+
stripped_includes = []
98+
if getattr(ctx.rule.attr, "strip_include_prefix", ""):
99+
prefix = ctx.rule.attr.strip_include_prefix.strip("/")
100+
if ctx.label.workspace_name:
101+
stripped_includes = [
102+
"${BAZEL_OUTPUT_BASE}/external/%s/%s" % (ctx.label.workspace_name, prefix), # source
103+
"${BAZEL_EXEC_ROOT}/%s/external/%s/%s" % (ctx.var["BINDIR"], ctx.label.workspace_name, prefix), # generated
104+
]
105+
else:
106+
stripped_includes = [
107+
prefix, # source
108+
"${BAZEL_EXEC_ROOT}/%s/%s" % (ctx.var["BINDIR"], prefix), # generated
109+
]
110+
111+
copts = [opt for opt in copts if not opt.startswith("-I")]
112+
deps = [dep[CmakeInfo] for dep in deps if CmakeInfo in dep]
113+
114+
# by the book this should be done with depsets, but so far the performance implication is negligible
115+
for dep in deps:
116+
if dep.name:
117+
stripped_includes += dep.stripped_includes
118+
includes += stripped_includes
119+
120+
return [
121+
CmakeInfo(
122+
name = name,
123+
inputs = inputs,
124+
kind = "executable" if is_binary else "library",
125+
modifier = "INTERFACE" if not srcs and not is_binary else "",
126+
hdrs = hdrs,
127+
srcs = srcs,
128+
deps = [dep for dep in deps if dep.name != None],
129+
includes = includes,
130+
stripped_includes = stripped_includes,
131+
imported_static_libs = static_libs,
132+
imported_dynamic_libs = dynamic_libs,
133+
copts = copts,
134+
linkopts = linkopts,
135+
defines = compilation_ctx.defines.to_list(),
136+
local_defines = compilation_ctx.local_defines.to_list(),
137+
force_cxx_compilation = force_cxx_compilation,
138+
transitive_deps = depset(deps, transitive = [dep.transitive_deps for dep in deps]),
139+
),
140+
]
141+
142+
cmake_aspect = aspect(
143+
implementation = _cmake_aspect_impl,
144+
attr_aspects = ["deps"],
145+
fragments = ["cpp"],
146+
)
147+
148+
def _map_cmake_info(info):
149+
args = " ".join([info.name, info.modifier] + info.hdrs + info.srcs).strip()
150+
commands = [
151+
"add_%s(%s)" % (info.kind, args),
152+
]
153+
if info.imported_static_libs and info.imported_dynamic_libs:
154+
commands += [
155+
"if(BUILD_SHARED_LIBS)",
156+
" target_link_libraries(%s %s %s)" %
157+
(info.name, info.modifier or "PUBLIC", " ".join(info.imported_dynamic_libs)),
158+
"else()",
159+
" target_link_libraries(%s %s %s)" %
160+
(info.name, info.modifier or "PUBLIC", " ".join(info.imported_static_libs)),
161+
"endif()",
162+
]
163+
elif info.imported_static_libs or info.imported_dynamic_libs:
164+
commands += [
165+
"target_link_libraries(%s %s %s)" %
166+
(info.name, info.modifier or "PUBLIC", " ".join(info.imported_dynamic_lib + info.imported_static_libs)),
167+
]
168+
if info.deps:
169+
libs = {}
170+
if info.modifier == "INTERFACE":
171+
libs = {"INTERFACE": [lib.name for lib in info.deps]}
172+
else:
173+
for lib in info.deps:
174+
libs.setdefault(lib.modifier, []).append(lib.name)
175+
for modifier, names in libs.items():
176+
commands += [
177+
"target_link_libraries(%s %s %s)" % (info.name, modifier or "PUBLIC", " ".join(names)),
178+
]
179+
if info.includes:
180+
commands += [
181+
"target_include_directories(%s %s %s)" % (info.name, info.modifier or "PUBLIC", " ".join(info.includes)),
182+
]
183+
if info.copts and info.modifier != "INTERFACE":
184+
commands += [
185+
"target_compile_options(%s PRIVATE %s)" % (info.name, " ".join(info.copts)),
186+
]
187+
if info.linkopts:
188+
commands += [
189+
"target_link_options(%s %s %s)" % (info.name, info.modifier or "PUBLIC", " ".join(info.linkopts)),
190+
]
191+
if info.force_cxx_compilation and any([f.endswith(".c") for f in info.srcs]):
192+
commands += [
193+
"set_source_files_properties(%s PROPERTIES LANGUAGE CXX)" % " ".join([f for f in info.srcs if f.endswith(".c")]),
194+
]
195+
if info.defines:
196+
commands += [
197+
"target_compile_definitions(%s %s %s)" % (info.name, info.modifier or "PUBLIC", " ".join(info.defines)),
198+
]
199+
if info.local_defines:
200+
commands += [
201+
"target_compile_definitions(%s %s %s)" % (info.name, info.modifier or "PRIVATE", " ".join(info.local_defines)),
202+
]
203+
return commands
204+
205+
GeneratedCmakeFiles = provider(
206+
fields = {
207+
"files": "",
208+
},
209+
)
210+
211+
def _generate_cmake_impl(ctx):
212+
commands = []
213+
inputs = []
214+
215+
infos = {}
216+
for dep in ctx.attr.targets:
217+
for info in [dep[CmakeInfo]] + dep[CmakeInfo].transitive_deps.to_list():
218+
if info.name != None:
219+
inputs += info.inputs
220+
infos[info.name] = info
221+
222+
for info in infos.values():
223+
commands += _map_cmake_info(info)
224+
commands.append("")
225+
226+
for include in ctx.attr.includes:
227+
for file in include[GeneratedCmakeFiles].files.to_list():
228+
inputs.append(file)
229+
commands.append("include(${BAZEL_EXEC_ROOT}/%s)" % file.path)
230+
231+
# we want to use a run or run_shell action to register a bunch of files like inputs, but we cannot write all
232+
# in a shell command as we would hit the command size limit. So we first write the file and then copy it with
233+
# the dummy inputs
234+
tmp_output = ctx.actions.declare_file(ctx.label.name + ".cmake~")
235+
output = ctx.actions.declare_file(ctx.label.name + ".cmake")
236+
ctx.actions.write(tmp_output, "\n".join(commands))
237+
ctx.actions.run_shell(outputs = [output], inputs = inputs + [tmp_output], command = "cp %s %s" % (tmp_output.path, output.path))
238+
239+
return [
240+
DefaultInfo(files = depset([output])),
241+
GeneratedCmakeFiles(files = depset([output])),
242+
]
243+
244+
generate_cmake = rule(
245+
implementation = _generate_cmake_impl,
246+
attrs = {
247+
"targets": attr.label_list(aspects = [cmake_aspect]),
248+
"includes": attr.label_list(providers = [GeneratedCmakeFiles]),
249+
},
250+
)

misc/bazel/cmake/setup.cmake

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
option(BUILD_SHARED_LIBS "" 0)
2+
3+
execute_process(COMMAND bazel info workspace OUTPUT_VARIABLE BAZEL_WORKSPACE COMMAND_ERROR_IS_FATAL ANY OUTPUT_STRIP_TRAILING_WHITESPACE)
4+
5+
execute_process(COMMAND bazel info output_base OUTPUT_VARIABLE BAZEL_OUTPUT_BASE COMMAND_ERROR_IS_FATAL ANY OUTPUT_STRIP_TRAILING_WHITESPACE)
6+
string(REPLACE "-" "_" BAZEL_EXEC_ROOT ${PROJECT_NAME})
7+
set(BAZEL_EXEC_ROOT ${BAZEL_OUTPUT_BASE}/execroot/${BAZEL_EXEC_ROOT})
8+
9+
execute_process(COMMAND bazel query "kind(generate_cmake, //...)" OUTPUT_VARIABLE BAZEL_GENERATE_CMAKE_TARGETS COMMAND_ERROR_IS_FATAL ANY OUTPUT_STRIP_TRAILING_WHITESPACE)
10+
execute_process(COMMAND bazel build ${BAZEL_GENERATE_CMAKE_TARGETS} COMMAND_ERROR_IS_FATAL ANY)
11+
12+
string(REPLACE "//" "" BAZEL_GENERATE_CMAKE_TARGETS "${BAZEL_GENERATE_CMAKE_TARGETS}")
13+
string(REPLACE ":" "/" BAZEL_GENERATE_CMAKE_TARGETS "${BAZEL_GENERATE_CMAKE_TARGETS}")
14+
15+
foreach (target ${BAZEL_GENERATE_CMAKE_TARGETS})
16+
include(${BAZEL_WORKSPACE}/bazel-bin/${target}.cmake)
17+
endforeach ()
18+
19+
if (CMAKE_EXPORT_COMPILE_COMMANDS)
20+
file(CREATE_LINK ${PROJECT_BINARY_DIR}/compile_commands.json ${PROJECT_SOURCE_DIR}/compile_commands.json)
21+
endif ()

swift/.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,13 @@
33

44
# output files created by running tests
55
*.o
6+
7+
# compilation database
8+
compile_commands.json
9+
10+
# CLion project data and build directories
11+
/.idea
12+
/cmake*
13+
14+
# VSCode default build directory
15+
/build

swift/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# this uses generated cmake files to setup cmake compilation of the swift extractor
2+
# this is provided solely for IDE integration
3+
4+
cmake_minimum_required(VERSION 3.21)
5+
6+
set(CMAKE_CXX_STANDARD 17)
7+
set(CMAKE_CXX_EXTENSIONS OFF)
8+
9+
set(CMAKE_C_COMPILER clang)
10+
set(CMAKE_CXX_COMPILER clang++)
11+
12+
project(codeql)
13+
14+
include(../misc/bazel/cmake/setup.cmake)

swift/extractor/BUILD.bazel

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
load("//swift:rules.bzl", "swift_cc_binary")
2+
load("//misc/bazel/cmake:cmake.bzl", "generate_cmake")
23

34
swift_cc_binary(
45
name = "extractor",
@@ -9,8 +10,13 @@ swift_cc_binary(
910
visibility = ["//swift:__pkg__"],
1011
deps = [
1112
"//swift/extractor/infra",
12-
"//swift/extractor/visitors",
1313
"//swift/extractor/remapping",
14+
"//swift/extractor/visitors",
1415
"//swift/tools/prebuilt:swift-llvm-support",
1516
],
1617
)
18+
19+
generate_cmake(
20+
name = "cmake",
21+
targets = [":extractor"],
22+
)

0 commit comments

Comments
 (0)