Repository: mesonbuild/meson

Issue:
Swift: `static_library()` ignores `swift_args`
**Describe the bug**

I'm trying to enable interoperability with C++ by passing `-cxx-interoperability-mode=default` to `swiftc` but   `static_library()` ignores `swift_args`.

**To Reproduce**

```
project('swift_lib', 'swift')

br_inc = include_directories('Sources/CppBridge')

lib = static_library(
  'MyLib',
  'Sources/MyLib/MyLib.swift',
  swift_args: ['-cxx-interoperability-mode=default'],
  include_directories: br_inc
)

dep = declare_dependency(
  include_directories: [
    include_directories('.'),
    br_inc
  ],
  link_with: lib
)
 ```

The logs show that `swift_args` flags are not passed to `swiftc`:

```
/usr/bin/meson --internal dirchanger ~/proj/subprojects/swift/libMyLib.a.p swiftc -c -O \
  -module-name MyLib \
  -I~/proj/subprojects/swift/Sources/CppBridge \
  ~/proj/subprojects/swift/Sources/MyLib/MyLib.swift
```

**Expected behavior**

The flags listed in `swift_args` are passed to `swiftc`.

**system parameters**
> Is this a [cross build](https://mesonbuild.com/Cross-compilation.html) or just a plain native build (for the same computer)?

native

> what operating system (e.g. MacOS Catalina, Windows 10, CentOS 8.0, Ubuntu 18.04, etc.)

Debian 12

> what Python version are you using e.g. 3.8.0

3.11.2

> what `meson --version`

1.0.1

> what `ninja --version` if it's a Ninja build

1.11.1



Discussion:
We don't have any Swift contributors to my knowledge. Would you be interested in sending a PR
If you're interested, the place to start is in `backend/ninjabackend.py` and the function is either `generate_swift_compile` or `generate_swift_target` (can't remember off the top of my head). Pretty sure rust used to have the same issue, so if you look through the git log for `rust_args` you'll probably find something helpful
Sure, I will to make a PR.

Resolving patch:
diff --git a/mesonbuild/backend/ninjabackend.py b/mesonbuild/backend/ninjabackend.py
--- a/mesonbuild/backend/ninjabackend.py
+++ b/mesonbuild/backend/ninjabackend.py
@@ -2172,6 +2172,7 @@ def generate_swift_target(self, target):
                 srctreedir = os.path.normpath(os.path.join(self.environment.get_build_dir(), self.build_to_src, expdir))
                 sargs = swiftc.get_include_args(srctreedir, False)
                 compile_args += sargs
+        compile_args += target.get_extra_args('swift')
         link_args = swiftc.get_output_args(os.path.join(self.environment.get_build_dir(), self.get_target_filename(target)))
         link_args += self.build.get_project_link_args(swiftc, target.subproject, target.for_machine)
         link_args += self.build.get_global_link_args(swiftc, target.for_machine)


Test patch:
diff --git a/test cases/swift/8 extra args/main.swift b/test cases/swift/8 extra args/main.swift
new file mode 100644
--- /dev/null
+++ b/test cases/swift/8 extra args/main.swift	
@@ -0,0 +1 @@
+print("test")
diff --git a/test cases/swift/8 extra args/meson.build b/test cases/swift/8 extra args/meson.build
new file mode 100644
--- /dev/null
+++ b/test cases/swift/8 extra args/meson.build	
@@ -0,0 +1,18 @@
+project('extra args', 'swift')
+
+trace_fname = 'trace.json'
+
+lib = static_library('main',
+  'main.swift',
+  swift_args: [
+    '-emit-loaded-module-trace',
+    '-emit-loaded-module-trace-path', '../' + trace_fname
+  ],
+)
+
+custom_target('trace_check',
+  output: trace_fname,
+  command: ['cat', '@OUTPUT@'],
+  depends: lib,
+  build_always: true,
+)