summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKatalin Rebhan <me@dblsaiko.net>2025-02-25 18:52:51 +0100
committerDylan Baker <dylan@pnwbakers.com>2025-06-05 14:34:27 -0700
commit6624e4a896c31ca036fe58def4d8ace4a7776715 (patch)
treef34f09d49b3989118e81b3c07c6e4d2d88db6cdf
parent7c84341e7ad2b9bb73eea85fe02ec155ef575128 (diff)
downloadmeson-6624e4a896c31ca036fe58def4d8ace4a7776715.tar.gz
swift: Pass C base compile options to swiftc
-rw-r--r--docs/markdown/snippets/swift-pass-c-compiler-options.md8
-rw-r--r--mesonbuild/compilers/swift.py8
-rw-r--r--test cases/swift/11 c std passthrough/header.h10
-rw-r--r--test cases/swift/11 c std passthrough/main.swift3
-rw-r--r--test cases/swift/11 c std passthrough/meson.build3
5 files changed, 31 insertions, 1 deletions
diff --git a/docs/markdown/snippets/swift-pass-c-compiler-options.md b/docs/markdown/snippets/swift-pass-c-compiler-options.md
new file mode 100644
index 000000000..3610a8ea1
--- /dev/null
+++ b/docs/markdown/snippets/swift-pass-c-compiler-options.md
@@ -0,0 +1,8 @@
+## Swift compiler receives select C family compiler options
+
+Meson now passes select few C family (C/Obj-C) compiler options to the
+Swift compiler, notably *-std=*, in order to improve the compatibility
+of C code as interpreted by the C compiler and the Swift compiler.
+
+NB: This does not include any of the options set in the target's
+c_flags.
diff --git a/mesonbuild/compilers/swift.py b/mesonbuild/compilers/swift.py
index 528d76f6a..bc0948b7a 100644
--- a/mesonbuild/compilers/swift.py
+++ b/mesonbuild/compilers/swift.py
@@ -8,7 +8,7 @@ import subprocess, os.path
import typing as T
from .. import mlog, options
-from ..mesonlib import MesonException, version_compare
+from ..mesonlib import first, MesonException, version_compare
from .compilers import Compiler, clike_debug_args
@@ -139,6 +139,12 @@ class SwiftCompiler(Compiler):
if std != 'none':
args += ['-swift-version', std]
+ # Pass C compiler -std=... arg to swiftc
+ c_lang = first(['objc', 'c'], lambda x: x in target.compilers)
+ if c_lang is not None:
+ cc = target.compilers[c_lang]
+ args.extend(arg for c_arg in cc.get_option_std_args(target, env, subproject) for arg in ['-Xcc', c_arg])
+
return args
def get_working_directory_args(self, path: str) -> T.Optional[T.List[str]]:
diff --git a/test cases/swift/11 c std passthrough/header.h b/test cases/swift/11 c std passthrough/header.h
new file mode 100644
index 000000000..287cdf4d7
--- /dev/null
+++ b/test cases/swift/11 c std passthrough/header.h
@@ -0,0 +1,10 @@
+#pragma once
+
+// let's just assume the default isn't c18.
+#if __STDC_VERSION__ == 201710L
+typedef struct Datatype {
+ int x;
+} Datatype;
+#else
+#error C standard version not set!
+#endif
diff --git a/test cases/swift/11 c std passthrough/main.swift b/test cases/swift/11 c std passthrough/main.swift
new file mode 100644
index 000000000..f6358dba1
--- /dev/null
+++ b/test cases/swift/11 c std passthrough/main.swift
@@ -0,0 +1,3 @@
+let d = Datatype(x: 1)
+
+precondition(d.x == 1)
diff --git a/test cases/swift/11 c std passthrough/meson.build b/test cases/swift/11 c std passthrough/meson.build
new file mode 100644
index 000000000..202768fcf
--- /dev/null
+++ b/test cases/swift/11 c std passthrough/meson.build
@@ -0,0 +1,3 @@
+project('c std passthrough', 'swift', 'c', default_options: {'c_std': 'c18'})
+
+executable('program', 'main.swift', 'header.h')