summaryrefslogtreecommitdiff
path: root/mesonbuild
diff options
context:
space:
mode:
authorKatalin Rebhan <me@dblsaiko.net>2025-02-17 18:25:40 +0100
committerJussi Pakkanen <jussi.pakkanen@mailbox.org>2025-07-24 13:54:53 +0300
commitf34d2c3aa3047562f7ecb4174c5d513b9d6ebe74 (patch)
tree05ed2151baa5b34863ceb0848cacc161e75e5169 /mesonbuild
parentc07eb44c2b54025a98162e0ccd4c70c0f9b2d244 (diff)
downloadmeson-f34d2c3aa3047562f7ecb4174c5d513b9d6ebe74.tar.gz
Add -parse-as-library to Swift library targets
Diffstat (limited to 'mesonbuild')
-rw-r--r--mesonbuild/backend/ninjabackend.py7
-rw-r--r--mesonbuild/compilers/swift.py3
2 files changed, 10 insertions, 0 deletions
diff --git a/mesonbuild/backend/ninjabackend.py b/mesonbuild/backend/ninjabackend.py
index ba75ce737..82411b0e6 100644
--- a/mesonbuild/backend/ninjabackend.py
+++ b/mesonbuild/backend/ninjabackend.py
@@ -2288,6 +2288,13 @@ class NinjaBackend(backends.Backend):
compile_args += swiftc.get_cxx_interoperability_args(target.compilers)
compile_args += self.build.get_project_args(swiftc, target.subproject, target.for_machine)
compile_args += self.build.get_global_args(swiftc, target.for_machine)
+ if isinstance(target, (build.StaticLibrary, build.SharedLibrary)):
+ # swiftc treats modules with a single source file, and the main.swift file in multi-source file modules
+ # as top-level code. This is undesirable in library targets since it emits a main function. Add the
+ # -parse-as-library option as necessary to prevent emitting the main function while keeping files explicitly
+ # named main.swift treated as the entrypoint of the module in case this is desired.
+ if len(abssrc) == 1 and os.path.basename(abssrc[0]) != 'main.swift':
+ compile_args += swiftc.get_library_args()
for i in reversed(target.get_include_dirs()):
basedir = i.get_curdir()
for d in i.get_incdirs():
diff --git a/mesonbuild/compilers/swift.py b/mesonbuild/compilers/swift.py
index 47d254be5..3fff7a1ff 100644
--- a/mesonbuild/compilers/swift.py
+++ b/mesonbuild/compilers/swift.py
@@ -159,6 +159,9 @@ class SwiftCompiler(Compiler):
else:
return ['-cxx-interoperability-mode=off']
+ def get_library_args(self) -> T.List[str]:
+ return ['-parse-as-library']
+
def compute_parameters_with_absolute_paths(self, parameter_list: T.List[str],
build_dir: str) -> T.List[str]:
for idx, i in enumerate(parameter_list):