Skip to content

Commit dc26dc8

Browse files
authored
Merge pull request github#13370 from github/redsun82/swift-fix-cmake
Swift: fix cmake generation
2 parents 52fb00c + be9d32a commit dc26dc8

File tree

7 files changed

+41
-65
lines changed

7 files changed

+41
-65
lines changed

misc/bazel/cmake/cmake.bzl

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ CmakeInfo = provider(
1111
"includes": "",
1212
"quote_includes": "",
1313
"stripped_includes": "",
14-
"imported_static_libs": "",
15-
"imported_dynamic_libs": "",
14+
"imported_libs": "",
1615
"copts": "",
1716
"linkopts": "",
1817
"force_cxx_compilation": "",
@@ -41,10 +40,8 @@ def _file_kind(file):
4140
return "src"
4241
if ext in ("h", "hh", "hpp", "def", "inc"):
4342
return "hdr"
44-
if ext == "a":
45-
return "static_lib"
46-
if ext in ("so", "dylib"):
47-
return "dynamic_lib"
43+
if ext in ("a", "so", "dylib"):
44+
return "lib"
4845
return None
4946

5047
def _get_includes(includes):
@@ -70,8 +67,7 @@ def _cmake_aspect_impl(target, ctx):
7067
by_kind.setdefault(_file_kind(f), []).append(_cmake_file(f))
7168
hdrs = by_kind.get("hdr", [])
7269
srcs = by_kind.get("src", [])
73-
static_libs = by_kind.get("static_lib", [])
74-
dynamic_libs = by_kind.get("dynamic_lib", [])
70+
libs = by_kind.get("lib", [])
7571
if not srcs and is_binary:
7672
empty = ctx.actions.declare_file(name + "_empty.cpp")
7773
ctx.actions.write(empty, "")
@@ -134,12 +130,15 @@ def _cmake_aspect_impl(target, ctx):
134130
system_includes = system_includes,
135131
quote_includes = quote_includes,
136132
stripped_includes = stripped_includes,
137-
imported_static_libs = static_libs,
138-
imported_dynamic_libs = dynamic_libs,
133+
imported_libs = libs,
139134
copts = copts,
140135
linkopts = linkopts,
141136
defines = compilation_ctx.defines.to_list(),
142-
local_defines = compilation_ctx.local_defines.to_list(),
137+
local_defines = [
138+
d
139+
for d in compilation_ctx.local_defines.to_list()
140+
if not d.startswith("BAZEL_CURRENT_REPOSITORY")
141+
],
143142
force_cxx_compilation = force_cxx_compilation,
144143
transitive_deps = depset(deps, transitive = [dep.transitive_deps for dep in deps]),
145144
),
@@ -156,20 +155,10 @@ def _map_cmake_info(info, is_windows):
156155
commands = [
157156
"add_%s(%s)" % (info.kind, args),
158157
]
159-
if info.imported_static_libs and info.imported_dynamic_libs:
160-
commands += [
161-
"if(BUILD_SHARED_LIBS)",
162-
" target_link_libraries(%s %s %s)" %
163-
(info.name, info.modifier or "PUBLIC", " ".join(info.imported_dynamic_libs)),
164-
"else()",
165-
" target_link_libraries(%s %s %s)" %
166-
(info.name, info.modifier or "PUBLIC", " ".join(info.imported_static_libs)),
167-
"endif()",
168-
]
169-
elif info.imported_static_libs or info.imported_dynamic_libs:
158+
if info.imported_libs:
170159
commands += [
171160
"target_link_libraries(%s %s %s)" %
172-
(info.name, info.modifier or "PUBLIC", " ".join(info.imported_dynamic_lib + info.imported_static_libs)),
161+
(info.name, info.modifier or "PUBLIC", " ".join(info.imported_libs)),
173162
]
174163
if info.deps:
175164
libs = {}
@@ -223,7 +212,7 @@ def _map_cmake_info(info, is_windows):
223212

224213
GeneratedCmakeFiles = provider(
225214
fields = {
226-
"files": "",
215+
"targets": "",
227216
},
228217
)
229218

@@ -232,7 +221,11 @@ def _generate_cmake_impl(ctx):
232221
inputs = []
233222

234223
infos = {}
235-
for dep in ctx.attr.targets:
224+
targets = list(ctx.attr.targets)
225+
for include in ctx.attr.includes:
226+
targets += include[GeneratedCmakeFiles].targets.to_list()
227+
228+
for dep in targets:
236229
for info in [dep[CmakeInfo]] + dep[CmakeInfo].transitive_deps.to_list():
237230
if info.name != None:
238231
inputs += info.inputs
@@ -244,11 +237,6 @@ def _generate_cmake_impl(ctx):
244237
commands += _map_cmake_info(info, is_windows)
245238
commands.append("")
246239

247-
for include in ctx.attr.includes:
248-
for file in include[GeneratedCmakeFiles].files.to_list():
249-
inputs.append(file)
250-
commands.append("include(${BAZEL_EXEC_ROOT}/%s)" % file.path)
251-
252240
# we want to use a run or run_shell action to register a bunch of files like inputs, but we cannot write all
253241
# in a shell command as we would hit the command size limit. So we first write the file and then copy it with
254242
# the dummy inputs
@@ -259,7 +247,7 @@ def _generate_cmake_impl(ctx):
259247

260248
return [
261249
DefaultInfo(files = depset([output])),
262-
GeneratedCmakeFiles(files = depset([output])),
250+
GeneratedCmakeFiles(targets = depset(ctx.attr.targets)),
263251
]
264252

265253
generate_cmake = rule(

swift/BUILD.bazel

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ load("@rules_pkg//:mappings.bzl", "pkg_attributes", "pkg_filegroup", "pkg_files"
22
load("@rules_pkg//:install.bzl", "pkg_install")
33
load("//:defs.bzl", "codeql_platform")
44
load("//misc/bazel:pkg_runfiles.bzl", "pkg_runfiles")
5+
load("//misc/bazel/cmake:cmake.bzl", "generate_cmake")
56

67
filegroup(
78
name = "schema",
@@ -106,3 +107,15 @@ py_binary(
106107
main = "create_extractor_pack.py",
107108
deps = [":_create_extractor_pack"],
108109
)
110+
111+
generate_cmake(
112+
name = "cmake",
113+
targets = [
114+
"//swift/extractor:extractor.real",
115+
"//swift/logging/tests/assertion-diagnostics:assert-false",
116+
] + select({
117+
"@platforms//os:linux": ["//swift/tools/autobuilder-diagnostics:incompatible-os"],
118+
"@platforms//os:macos": ["//swift/xcode-autobuilder"],
119+
}),
120+
visibility = ["//visibility:public"],
121+
)

swift/CMakeLists.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,4 @@ project(codeql)
1717

1818
include(../misc/bazel/cmake/setup.cmake)
1919

20-
include_generated(//swift/extractor:cmake)
21-
if (APPLE)
22-
include_generated(//swift/xcode-autobuilder:cmake)
23-
endif ()
20+
include_generated(//swift:cmake)

swift/extractor/BUILD.bazel

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ swift_cc_binary(
88
"*.h",
99
"*.cpp",
1010
]),
11+
visibility = ["//swift:__pkg__"],
1112
deps = [
1213
"//swift/extractor/config",
1314
"//swift/extractor/infra",
@@ -19,12 +20,6 @@ swift_cc_binary(
1920
],
2021
)
2122

22-
generate_cmake(
23-
name = "cmake",
24-
targets = [":extractor.real"],
25-
visibility = ["//visibility:public"],
26-
)
27-
2823
sh_binary(
2924
name = "extractor",
3025
srcs = ["extractor.sh"],

swift/logging/tests/assertion-diagnostics/BUILD.bazel

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ load("//misc/bazel/cmake:cmake.bzl", "generate_cmake")
44
swift_cc_binary(
55
name = "assert-false",
66
srcs = ["AssertFalse.cpp"],
7-
visibility = ["//visibility:private"],
7+
visibility = ["//swift:__pkg__"],
88
deps = [
99
"//swift/logging",
1010
],
@@ -14,12 +14,9 @@ py_test(
1414
name = "test",
1515
size = "small",
1616
srcs = ["test.py"],
17+
data = [
18+
"diagnostics.expected",
19+
":assert-false",
20+
],
1721
deps = ["//swift/integration-tests:integration_tests"],
18-
data = [":assert-false", "diagnostics.expected"],
19-
)
20-
21-
generate_cmake(
22-
name = "cmake",
23-
targets = [":assert-false"],
24-
visibility = ["//visibility:public"],
2522
)
Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
load("//swift:rules.bzl", "swift_cc_binary")
2-
load("//misc/bazel/cmake:cmake.bzl", "generate_cmake")
32

43
swift_cc_binary(
54
name = "incompatible-os",
@@ -9,9 +8,3 @@ swift_cc_binary(
98
"//swift/logging",
109
],
1110
)
12-
13-
generate_cmake(
14-
name = "cmake",
15-
targets = [":incompatible-os"],
16-
visibility = ["//visibility:public"],
17-
)

swift/xcode-autobuilder/BUILD.bazel

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,19 @@
11
load("//swift:rules.bzl", "swift_cc_binary")
2-
load("//misc/bazel/cmake:cmake.bzl", "generate_cmake")
32

43
swift_cc_binary(
54
name = "xcode-autobuilder",
65
srcs = glob([
76
"*.cpp",
87
"*.h",
98
]),
10-
visibility = ["//swift:__subpackages__"],
119
linkopts = [
1210
"-lxml2",
1311
"-framework CoreFoundation",
1412
],
1513
target_compatible_with = ["@platforms//os:macos"],
14+
visibility = ["//swift:__subpackages__"],
1615
deps = [
17-
"@absl//absl/strings",
1816
"//swift/logging",
17+
"@absl//absl/strings",
1918
],
2019
)
21-
22-
generate_cmake(
23-
name = "cmake",
24-
targets = [":xcode-autobuilder"],
25-
visibility = ["//visibility:public"],
26-
)

0 commit comments

Comments
 (0)