Skip to content

Commit 3b04f69

Browse files
authored
Merge pull request #11 from dsp-testing/redsun82/swift-5.8
Fix linux compilation with swift parser
2 parents cf1b1ff + 0dd4521 commit 3b04f69

8 files changed

+107
-81
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ project(codeql-swift-artifacts C CXX)
44
find_package(LLVM REQUIRED CONFIG)
55
find_package(Clang REQUIRED CONFIG)
66
find_package(Swift REQUIRED CONFIG)
7+
find_package(SwiftSyntax REQUIRED CONFIG)
78

89
message("Using LLVM_CONFIG: ${Swift_CONFIG}")
910
message("Using Clang_CONFIG: ${LLVM_CONFIG}")
@@ -31,4 +32,3 @@ if(APPLE)
3132
target_link_directories(codeql-swift-artifacts PUBLIC ${TOOLCHAIN_LIB_DIR})
3233
target_link_directories(codeql-swift-artifacts PUBLIC ${CODEQL_MACOS_SDK_PATH}/usr/lib/swift)
3334
endif()
34-
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
diff --git a/cmake/modules/AddSwift.cmake b/cmake/modules/AddSwift.cmake
2+
index 58c7eb1bd4f..4f4d5b7edea 100644
3+
--- a/cmake/modules/AddSwift.cmake
4+
+++ b/cmake/modules/AddSwift.cmake
5+
@@ -912,17 +912,22 @@ function(add_swift_host_tool executable)
6+
endif()
7+
8+
if(SWIFT_SWIFT_PARSER)
9+
- set(extra_relative_rpath "")
10+
- if(NOT ${ASHT_BOOTSTRAPPING} STREQUAL "")
11+
- if (${executable} MATCHES "-bootstrapping")
12+
- set(extra_relative_rpath "../")
13+
+ if (SWIFT_HOST_VARIANT_SDK STREQUAL LINUX)
14+
+ if(NOT ${ASHT_BOOTSTRAPPING} STREQUAL "" AND ${executable} MATCHES "-bootstrapping")
15+
+ set_property(TARGET ${executable} APPEND PROPERTY INSTALL_RPATH "$ORIGIN/../../lib/swift/host")
16+
+ else()
17+
+ set_property(TARGET ${executable} APPEND PROPERTY INSTALL_RPATH "$ORIGIN/../lib/swift/host")
18+
+ endif()
19+
+ else()
20+
+ set(extra_relative_rpath "")
21+
+ if(NOT ${ASHT_BOOTSTRAPPING} STREQUAL "" AND ${executable} MATCHES "-bootstrapping")
22+
+ set(extra_relative_rpath "../")
23+
endif()
24+
+ set_property(
25+
+ TARGET ${executable}
26+
+ APPEND PROPERTY INSTALL_RPATH
27+
+ "@executable_path/../${extra_relative_rpath}lib/swift/host")
28+
endif()
29+
-
30+
- set_property(
31+
- TARGET ${executable}
32+
- APPEND PROPERTY INSTALL_RPATH
33+
- "@executable_path/../${extra_relative_rpath}lib/swift/host")
34+
endif()
35+
36+
if(ASHT_THINLTO_LD64_ADD_FLTO_CODEGEN_ONLY)
37+
diff --git a/lib/ASTGen/CMakeLists.txt b/lib/ASTGen/CMakeLists.txt
38+
index c7334a6da0b..2a0cb80b743 100644
39+
--- a/lib/ASTGen/CMakeLists.txt
40+
+++ b/lib/ASTGen/CMakeLists.txt
41+
@@ -1,4 +1,11 @@
42+
if (SWIFT_SWIFT_PARSER)
43+
+ if (SWIFT_HOST_VARIANT_SDK STREQUAL "LINUX")
44+
+ # including libdispatch leads to multiple module definition problems, as it seems
45+
+ # it is already available to the swift toolchain
46+
+ get_property(dirs DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY INCLUDE_DIRECTORIES)
47+
+ list(REMOVE_ITEM dirs "${SWIFT_PATH_TO_LIBDISPATCH_SOURCE}")
48+
+ set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY INCLUDE_DIRECTORIES "${dirs}")
49+
+ endif()
50+
# Ensure that we do not link the _StringProcessing module. But we can
51+
# only pass this flag for new-enough compilers that support it.
52+
file(WRITE "${CMAKE_BINARY_DIR}/tmp/empty-check-string-processing.swift" "")

pkg_swift_llvm.py

Lines changed: 40 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@
1414

1515
def getoptions():
1616
parser = argparse.ArgumentParser(description="package swift for codeql compilation")
17-
parser.add_argument(f"--llvm-build-tree", required=True, type=resolve,
18-
metavar="DIR", help=f"path to LLVM build tree")
19-
parser.add_argument(f"--swift-build-tree", required=True, type=resolve,
20-
metavar="DIR", help=f"path to Swift build tree")
17+
parser.add_argument(f"--build-tree", required=True, type=resolve,
18+
metavar="DIR", help=f"path to the build tree")
2119
parser.add_argument(f"--swift-source-tree", required=True, type=resolve,
2220
metavar="DIR", help=f"path to Swift source tree")
2321

@@ -34,57 +32,54 @@ def getoptions():
3432
return opts
3533

3634

37-
Libs = namedtuple("Libs", ("archive", "static", "shared", "linker_flags"))
38-
3935
EXPORTED_LIB = "CodeQLSwiftFrontendTool"
4036

37+
Libs = namedtuple("Libs", ("static", "shared", "linker_flags"))
38+
4139

4240
def resolve(p):
4341
return pathlib.Path(p).resolve()
4442

4543

4644
def run(prog, *, cwd, env=None, input=None):
47-
print("running", " ".join(prog), f"(cwd={cwd})")
45+
print("running", *prog, f"(cwd={cwd})")
4846
if env is not None:
4947
runenv = dict(os.environ)
5048
runenv.update(env)
5149
else:
5250
runenv = None
53-
subprocess.run(prog, cwd=cwd, env=runenv, input=input, text=True)
51+
subprocess.run(prog, cwd=cwd, env=runenv, input=input, text=True, check=True)
5452

5553

5654
def get_platform():
5755
return "linux" if platform.system() == "Linux" else "macos"
5856

5957

60-
def configure_dummy_project(tmp, *, llvm=None, swift=None):
58+
def configure_dummy_project(tmp, prefixes):
6159
print("configuring dummy cmake project")
6260
script_dir = pathlib.Path(os.path.realpath(__file__)).parent
6361
print(script_dir)
6462
shutil.copy(script_dir / "CMakeLists.txt", tmp / "CMakeLists.txt")
6563
shutil.copy(script_dir / "empty.cpp", tmp / "empty.cpp")
6664
tgt = tmp / "build"
6765
tgt.mkdir()
68-
run(["cmake", f"-DCMAKE_PREFIX_PATH={llvm};{swift}", "-DBUILD_SHARED_LIBS=OFF", ".."],
69-
cwd=tgt)
66+
prefixes = ';'.join(str(p) for p in prefixes)
67+
run(["cmake", f"-DCMAKE_PREFIX_PATH={prefixes}", "-DBUILD_SHARED_LIBS=OFF", ".."], cwd=tgt)
7068
return tgt
7169

7270

7371
def get_libs(configured):
7472
print("extracting linking information from dummy project")
7573
with open(configured / "CMakeFiles" / "codeql-swift-artifacts.dir" / "link.txt") as link:
7674
libs = link.read().split()
77-
libs = libs[libs.index('codeql-swift-artifacts')+1:] # skip up to -o dummy
78-
ret = Libs([], [], [], [])
75+
libs = libs[libs.index('codeql-swift-artifacts') + 1:] # skip up to -o dummy
76+
ret = Libs([], [], [])
7977
for l in libs:
8078
if l.endswith(".a"):
81-
ret.static.append(str((configured / l).resolve()))
79+
ret.static.append((configured / l).absolute())
8280
elif l.endswith(".so") or l.endswith(".tbd") or l.endswith(".dylib"):
83-
l = pathlib.Path(l).stem
84-
ret.shared.append(f"-l{l[3:]}") # drop 'lib' prefix and '.so' suffix
85-
elif l.startswith("-l"):
86-
ret.shared.append(l)
87-
elif l.startswith("-L") or l.startswith("-Wl"):
81+
ret.shared.append((configured / l).absolute())
82+
elif l.startswith(("-L", "-Wl", "-l")):
8883
ret.linker_flags.append(l)
8984
else:
9085
raise ValueError(f"cannot understand link.txt: " + l)
@@ -94,60 +89,27 @@ def get_libs(configured):
9489
def get_tgt(tgt, filename):
9590
if tgt.is_dir():
9691
tgt /= filename
97-
return tgt.resolve()
92+
return tgt.absolute()
9893

9994

10095
def create_static_lib(tgt, libs):
10196
tgt = get_tgt(tgt, f"lib{EXPORTED_LIB}.a")
10297
print(f"packaging {tgt.name}")
10398
if sys.platform == 'linux':
104-
includedlibs = "\n".join(f"addlib {l}" for l in libs.archive + libs.static)
99+
includedlibs = "\n".join(f"addlib {l}" for l in libs.static)
105100
mriscript = f"create {tgt}\n{includedlibs}\nsave\nend"
106101
run(["ar", "-M"], cwd=tgt.parent, input=mriscript)
107102
else:
108103
libtool_args = ["libtool", "-static"]
109-
libtool_args.extend(libs.archive)
110104
libtool_args.extend(libs.static)
111105
libtool_args.append("-o")
112106
libtool_args.append(str(tgt))
113107
run(libtool_args, cwd=tgt.parent)
114108
return tgt
115109

116110

117-
def create_shared_lib(tgt, libs):
118-
ext = "so"
119-
if sys.platform != 'linux':
120-
ext = "dylib"
121-
libname = f"lib{EXPORTED_LIB}.{ext}"
122-
tgt = get_tgt(tgt, libname)
123-
print(f"packaging {libname}")
124-
compiler = os.environ.get("CC", "clang")
125-
cmd = [compiler, "-shared"]
126-
cmd.extend(libs.linker_flags)
127-
128-
if sys.platform == 'linux':
129-
cmd.append("-Wl,--whole-archive")
130-
else:
131-
cmd.append("-Wl,-all_load")
132-
133-
cmd.append(f"-o{tgt}")
134-
cmd.extend(libs.archive)
135-
136-
if sys.platform == 'linux':
137-
cmd.append("-Wl,--no-whole-archive")
138-
else:
139-
cmd.append("-lc++")
140-
141-
cmd.extend(libs.static)
142-
cmd.extend(libs.shared)
143-
run(cmd, cwd=tgt.parent)
144-
if sys.platform != "linux":
145-
run(["install_name_tool", "-id", f"@executable_path/{libname}", libname], cwd=tgt.parent)
146-
return tgt
147-
148-
149111
def copy_includes(src, tgt):
150-
print("copying includes")
112+
print(f"copying includes from {src}")
151113
for dir, exts in (("include", ("h", "def", "inc")), ("stdlib", ("h",))):
152114
srcdir = src / dir
153115
for ext in exts:
@@ -159,7 +121,7 @@ def copy_includes(src, tgt):
159121

160122
def export_sdk(tgt, swift_source_tree, swift_build_tree):
161123
print("assembling sdk")
162-
srcdir = swift_build_tree/ "lib" / "swift"
124+
srcdir = swift_build_tree / "lib" / "swift"
163125
tgtdir = tgt / "usr" / "lib" / "swift"
164126
if get_platform() == "linux":
165127
srcdir /= "linux"
@@ -168,8 +130,8 @@ def export_sdk(tgt, swift_source_tree, swift_build_tree):
168130
srcdir /= "macosx"
169131
for mod in srcdir.glob("*.swiftmodule"):
170132
shutil.copytree(mod, tgtdir / mod.name)
171-
shutil.copytree(swift_source_tree / "stdlib" / "public" / "SwiftShims",
172-
tgt / "usr" / "include" / "SwiftShims",
133+
shutil.copytree(swift_source_tree / "stdlib" / "public" / "SwiftShims" / "swift" / "shims",
134+
tgt / "usr" / "lib" / "swift" / "shims",
173135
ignore=shutil.ignore_patterns('CMakeLists.txt'))
174136

175137

@@ -181,11 +143,11 @@ def export_stdlibs(exported_dir, swift_build_tree):
181143
ext = 'so'
182144
lib_dir = swift_build_tree / 'lib/swift' / platform
183145
stdlibs = [
184-
f'libswiftCore.{ext}',
185-
'libswiftCompatibility50.a',
186-
'libswiftCompatibility51.a',
187-
'libswiftCompatibilityConcurrency.a',
188-
'libswiftCompatibilityDynamicReplacements.a']
146+
f'libswiftCore.{ext}',
147+
'libswiftCompatibility50.a',
148+
'libswiftCompatibility51.a',
149+
'libswiftCompatibilityConcurrency.a',
150+
'libswiftCompatibilityDynamicReplacements.a']
189151
for stdlib in stdlibs:
190152
lib_path = lib_dir / stdlib
191153
if lib_path.exists():
@@ -197,13 +159,11 @@ def export_stdlibs(exported_dir, swift_build_tree):
197159

198160
def export_libs(exported_dir, libs, swift_build_tree):
199161
print("exporting libraries")
200-
exportedlibs = [
201-
create_static_lib(exported_dir, libs),
202-
create_shared_lib(exported_dir, libs)
203-
]
204-
205-
for l in exportedlibs:
206-
l.rename(exported_dir / l.name)
162+
create_static_lib(exported_dir, libs)
163+
for lib in libs.shared:
164+
# export libraries under the build tree (e.g. libSwiftSyntax.so)
165+
if lib.is_relative_to(swift_build_tree.parent):
166+
shutil.copy(lib, exported_dir)
207167
export_stdlibs(exported_dir, swift_build_tree)
208168

209169

@@ -213,7 +173,8 @@ def export_headers(exported_dir, swift_source_tree, llvm_build_tree, swift_build
213173
llvm_source_tree = swift_source_tree.parent / 'llvm-project/llvm'
214174
clang_source_tree = swift_source_tree.parent / 'llvm-project/clang'
215175
clang_tools_build_tree = llvm_build_tree / 'tools/clang'
216-
header_dirs = [ llvm_source_tree, clang_source_tree, swift_source_tree, llvm_build_tree, swift_build_tree, clang_tools_build_tree ]
176+
header_dirs = [llvm_source_tree, clang_source_tree, swift_source_tree, llvm_build_tree, swift_build_tree,
177+
clang_tools_build_tree]
217178
for h in header_dirs:
218179
copy_includes(h, exported_dir)
219180

@@ -230,18 +191,21 @@ def main(opts):
230191
if os.path.exists(tmp):
231192
shutil.rmtree(tmp)
232193
os.mkdir(tmp)
233-
configured = configure_dummy_project(tmp, llvm=opts.llvm_build_tree, swift=opts.swift_build_tree)
194+
llvm_build_tree = next(opts.build_tree.glob("llvm-*"))
195+
swift_build_tree = next(opts.build_tree.glob("swift-*"))
196+
earlyswiftsyntax_build_tree = next(opts.build_tree.glob("earlyswiftsyntax-*"))
197+
configured = configure_dummy_project(tmp, prefixes=[llvm_build_tree, swift_build_tree,
198+
earlyswiftsyntax_build_tree / "cmake" / "modules"])
234199
libs = get_libs(configured)
235200

236201
exported = tmp / "exported"
237202
exported.mkdir()
238-
export_libs(exported, libs, opts.swift_build_tree)
239-
export_headers(exported, opts.swift_source_tree, opts.llvm_build_tree, opts.swift_build_tree)
240-
export_sdk(exported / "sdk", opts.swift_source_tree, opts.swift_build_tree)
203+
export_libs(exported, libs, swift_build_tree)
204+
export_headers(exported, opts.swift_source_tree, llvm_build_tree, swift_build_tree)
205+
export_sdk(exported / "sdk", opts.swift_source_tree, swift_build_tree)
241206

242207
zip_dir(exported, opts.output)
243208

244209

245210
if __name__ == "__main__":
246211
main(getoptions())
247-

swift-build-presets

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
[preset: codeql-baseline]
2-
bootstrapping=hosttools
3-
42
llvm-cmake-options=-DLLVM_ENABLE_TERMINFO=OFF -DLLVM_TARGETS_TO_BUILD=X86;ARM;AArch64
53

64
skip-ios
@@ -28,13 +26,25 @@ skip-build-android
2826
enable-experimental-string-processing
2927
swift-enable-experimental-string-processing=1
3028

31-
[preset: codeql]
29+
no-assertions
30+
31+
reconfigure
32+
33+
[preset: codeql-release]
3234
mixin-preset=codeql-baseline
3335
release
3436
build-subdir=codeql
3537

38+
[preset: codeql-Linux]
39+
mixin-preset=codeql-release
40+
bootstrapping=hosttools
41+
42+
[preset: codeql-macOS]
43+
mixin-preset=codeql-release
44+
bootstrapping=bootstrapping
45+
3646
[preset: codeql-debug]
3747
mixin-preset=codeql-baseline
3848
debug
3949
build-subdir=codeql-debug
40-
50+
bootstrapping=bootstrapping

0 commit comments

Comments
 (0)