Open kassane opened 2 months ago
For musl support - need SDK
New tests swiftc + zig (none SDK), like D lang -betterC
mode.
$ swiftc --version
Swift version 6.1-dev (LLVM fe0f467028c23cf, Swift 406b8bd8921b5dc)
Target: x86_64-unknown-linux-gnu
$ swiftc examples/main.swift -parse-as-library -enable-experimental-feature Embedded -wmo -Xfrontend -function-sections -Xfrontend -disable-stack-protector -import-bridging-header $PWD/examples/bridging.h -Xcc --target=aarch64-linux -c
<unknown>:0: warning: argument unused during compilation: '-mcx16'
<unknown>:0: warning: argument unused during compilation: '-mcx16'
$ file main.o
main.o: ELF 64-bit LSB relocatable, ARM aarch64, version 1 (GNU/Linux), not stripped
# note: zig cross-compile use musl-libc by default [linux-target only]
$ zig cc main.o -o swift_main -target aarch64-linux
$ ./swift_main
Hello from Swift
$ ldd swift_main
not a dynamic executable
Similar to flang, dropping some swiftc ldflags in zcc-wrapper
can solve cross-minimal support (without stdlib).
After #9. Tried some tests betwen targets (cross-compilation)
Sources
https://github.com/kassane/anotherBuildStep/tree/main/tests/swift_ffi
Build
native
```bash $ ~/Downloads/zig-linux-x86_64-0.13.0/zig build --summary all --verbose /home/kassane/Downloads/zig-linux-x86_64-0.13.0/zig build-lib -OReleaseSmall -Mroot=/home/kassane/anotherBuildStep/examples/zig_abi.zig -lc --cache-dir /home/kassane/anotherBuildStep/.zig-cache --global-cache-dir /home/kassane/.cache/zig --name zig_abi -static --listen=- /home/kassane/Downloads/zig-linux-x86_64-0.13.0/zig build-exe -OReleaseSafe --dep build_options -Mroot=/home/kassane/anotherBuildStep/tools/zigcc.zig -Mbuild_options=/home/kassane/anotherBuildStep/.zig-cache/c/c3a9b1f2fdf4bbdd8a1160b6f08cd482/options.zig --cache-dir /home/kassane/anotherBuildStep/.zig-cache --global-cache-dir /home/kassane/.cache/zig --name zcc --listen=- swiftc -import-bridging-header examples/c_include.h -g -clang-scanner-module-cache-path /home/kassane/anotherBuildStep/.zig-cache/o/dd6ab5043092597b04e52dd988141101 -module-cache-path /home/kassane/anotherBuildStep/.zig-cache/o/dd6ab5043092597b04e52dd988141101 -Xfrontend -disable-llvm-verify examples/main.swift -v -cxx-interoperability-mode=default -parse-as-library -Xcc -stdlib=libc++ -target x86_64-unknown-linux-gnu -o /home/kassane/anotherBuildStep/zig-out/bin/swift_example -ld-path=/home/kassane/anotherBuildStep/.zig-cache/o/f1f9a84666bfa391f83adf5acb58f32b/zcc -use-ld=/home/kassane/anotherBuildStep/.zig-cache/o/f1f9a84666bfa391f83adf5acb58f32b/zcc /home/kassane/anotherBuildStep/.zig-cache/o/bc3cf51116dd07753cb6e0e1bc7adfd9/libzig_abi.a Swift version 6.1-dev (LLVM 9d45b9c9d5c48a0, Swift 08721cf939ec503) Target: x86_64-unknown-linux-gnu /home/kassane/.local/share/swiftly/toolchains/main-snapshot-2024-08-21/usr/bin/swift-frontend -frontend -target x86_64-unknown-linux-gnu -disable-objc-interop -cxx-interoperability-mode=default -color-diagnostics -g -debug-info-format=dwarf -dwarf-version=4 -module-cache-path /home/kassane/anotherBuildStep/.zig-cache/o/dd6ab5043092597b04e52dd988141101 -disable-llvm-verify -empty-abi-descriptor -Xcc -working-directory -Xcc /home/kassane/anotherBuildStep -resource-dir /home/kassane/.local/share/swiftly/toolchains/main-snapshot-2024-08-21/usr/lib/swift -enable-anonymous-context-mangled-names -file-compilation-dir /home/kassane/anotherBuildStep -Xcc -stdlib=libc++ -import-objc-header /home/kassane/anotherBuildStep/examples/c_include.h -module-name swift_example -in-process-plugin-server-path /home/kassane/.local/share/swiftly/toolchains/main-snapshot-2024-08-21/usr/lib/swift/host/libSwiftInProcPluginServer.so -plugin-path /home/kassane/.local/share/swiftly/toolchains/main-snapshot-2024-08-21/usr/lib/swift/host/plugins -plugin-path /home/kassane/.local/share/swiftly/toolchains/main-snapshot-2024-08-21/usr/local/lib/swift/host/plugins -emit-pch -o /tmp/TemporaryDirectory.lQzIxg/c_include.pch /home/kassane/anotherBuildStep/examples/c_include.h /home/kassane/.local/share/swiftly/toolchains/main-snapshot-2024-08-21/usr/bin/swift-frontend -frontend -c -primary-file /home/kassane/anotherBuildStep/examples/main.swift -target x86_64-unknown-linux-gnu -disable-objc-interop -cxx-interoperability-mode=default -color-diagnostics -g -debug-info-format=dwarf -dwarf-version=4 -module-cache-path /home/kassane/anotherBuildStep/.zig-cache/o/dd6ab5043092597b04e52dd988141101 -disable-llvm-verify -empty-abi-descriptor -Xcc -working-directory -Xcc /home/kassane/anotherBuildStep -resource-dir /home/kassane/.local/share/swiftly/toolchains/main-snapshot-2024-08-21/usr/lib/swift -enable-anonymous-context-mangled-names -file-compilation-dir /home/kassane/anotherBuildStep -Xcc -stdlib=libc++ -import-objc-header /tmp/TemporaryDirectory.lQzIxg/c_include.pch -module-name swift_example -in-process-plugin-server-path /home/kassane/.local/share/swiftly/toolchains/main-snapshot-2024-08-21/usr/lib/swift/host/libSwiftInProcPluginServer.so -plugin-path /home/kassane/.local/share/swiftly/toolchains/main-snapshot-2024-08-21/usr/lib/swift/host/plugins -plugin-path /home/kassane/.local/share/swiftly/toolchains/main-snapshot-2024-08-21/usr/local/lib/swift/host/plugins -parse-as-library -o /tmp/TemporaryDirectory.lQzIxg/main-1.o /home/kassane/.local/share/swiftly/toolchains/main-snapshot-2024-08-21/usr/bin/swift-frontend -frontend -emit-module -experimental-skip-non-inlinable-function-bodies-without-types /home/kassane/anotherBuildStep/examples/main.swift -target x86_64-unknown-linux-gnu -disable-objc-interop -cxx-interoperability-mode=default -color-diagnostics -g -debug-info-format=dwarf -dwarf-version=4 -module-cache-path /home/kassane/anotherBuildStep/.zig-cache/o/dd6ab5043092597b04e52dd988141101 -disable-llvm-verify -empty-abi-descriptor -Xcc -working-directory -Xcc /home/kassane/anotherBuildStep -resource-dir /home/kassane/.local/share/swiftly/toolchains/main-snapshot-2024-08-21/usr/lib/swift -enable-anonymous-context-mangled-names -file-compilation-dir /home/kassane/anotherBuildStep -Xcc -stdlib=libc++ -import-objc-header /tmp/TemporaryDirectory.lQzIxg/c_include.pch -module-name swift_example -in-process-plugin-server-path /home/kassane/.local/share/swiftly/toolchains/main-snapshot-2024-08-21/usr/lib/swift/host/libSwiftInProcPluginServer.so -plugin-path /home/kassane/.local/share/swiftly/toolchains/main-snapshot-2024-08-21/usr/lib/swift/host/plugins -plugin-path /home/kassane/.local/share/swiftly/toolchains/main-snapshot-2024-08-21/usr/local/lib/swift/host/plugins -emit-module-doc-path /tmp/TemporaryDirectory.lQzIxg/swift_example-1.swiftdoc -emit-module-source-info-path /tmp/TemporaryDirectory.lQzIxg/swift_example-1.swiftsourceinfo -parse-as-library -o /tmp/TemporaryDirectory.lQzIxg/swift_example-1.swiftmodule /home/kassane/.local/share/swiftly/toolchains/main-snapshot-2024-08-21/usr/bin/swift-frontend -modulewrap /tmp/TemporaryDirectory.lQzIxg/swift_example-1.swiftmodule -target x86_64-unknown-linux-gnu -o /tmp/TemporaryDirectory.lQzIxg/swift_example-1.o /home/kassane/.local/share/swiftly/toolchains/main-snapshot-2024-08-21/usr/bin/swift-autolink-extract /tmp/TemporaryDirectory.lQzIxg/swift_example-1.o /tmp/TemporaryDirectory.lQzIxg/main-1.o /home/kassane/anotherBuildStep/.zig-cache/o/bc3cf51116dd07753cb6e0e1bc7adfd9/libzig_abi.a -o /tmp/TemporaryDirectory.lQzIxg/swift_example-2.autolink /home/kassane/.local/share/swiftly/toolchains/main-snapshot-2024-08-21/usr/bin/clang -fuse-ld=/home/kassane/anotherBuildStep/.zig-cache/o/f1f9a84666bfa391f83adf5acb58f32b/zcc --ld-path=/home/kassane/anotherBuildStep/.zig-cache/o/f1f9a84666bfa391f83adf5acb58f32b/zcc -pie -Xlinker --build-id -Xlinker -rpath -Xlinker /home/kassane/.local/share/swiftly/toolchains/main-snapshot-2024-08-21/usr/lib/swift/linux /home/kassane/.local/share/swiftly/toolchains/main-snapshot-2024-08-21/usr/lib/swift/linux/x86_64/swiftrt.o /tmp/TemporaryDirectory.lQzIxg/swift_example-1.o /tmp/TemporaryDirectory.lQzIxg/main-1.o /home/kassane/anotherBuildStep/.zig-cache/o/bc3cf51116dd07753cb6e0e1bc7adfd9/libzig_abi.a @/tmp/TemporaryDirectory.lQzIxg/swift_example-2.autolink -L /home/kassane/.local/share/swiftly/toolchains/main-snapshot-2024-08-21/usr/lib/swift/linux -lswiftCore --target=x86_64-unknown-linux-gnu -v -o /home/kassane/anotherBuildStep/zig-out/bin/swift_example [zig cc] flags: "-pie /lib64/ld-linux-x86-64.so.2 -o /home/kassane/anotherBuildStep/zig-out/bin/swift_example -L/home/kassane/.local/share/swiftly/toolchains/main-snapshot-2024-08-21/usr/lib/swift/linux -L/usr/lib64/gcc/x86_64-pc-linux-gnu/14.2.1 -L/usr/lib64/gcc/x86_64-pc-linux-gnu/14.2.1/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/lib -L/usr/lib -rpath /home/kassane/.local/share/swiftly/toolchains/main-snapshot-2024-08-21/usr/lib/swift/linux /home/kassane/.local/share/swiftly/toolchains/main-snapshot-2024-08-21/usr/lib/swift/linux/x86_64/swiftrt.o /tmp/TemporaryDirectory.lQzIxg/swift_example-1.o /tmp/TemporaryDirectory.lQzIxg/main-1.o /home/kassane/anotherBuildStep/.zig-cache/o/bc3cf51116dd07753cb6e0e1bc7adfd9/libzig_abi.a -lc++ -lswiftCxx -lswiftCxxStdlib -lswiftSwiftOnoneSupport -lswiftCore -lswift_Concurrency -lswift_StringProcessing -lswift_RegexParser -lswiftCore -lc -target native-native -mcpu=znver3 -lunwind " clang version 17.0.0 (https://github.com/swiftlang/llvm-project.git 9d45b9c9d5c48a092cca2ace3743125b4e42725c) Target: x86_64-unknown-linux-gnu Thread model: posix InstalledDir: /home/kassane/.local/share/swiftly/toolchains/main-snapshot-2024-08-21/usr/bin Found candidate GCC installation: /usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1 Found candidate GCC installation: /usr/lib64/gcc/x86_64-pc-linux-gnu/14.2.1 Selected GCC installation: /usr/lib64/gcc/x86_64-pc-linux-gnu/14.2.1 Candidate multilib: .;@m64 Candidate multilib: 32;@m32 Selected multilib: .;@m64 "/home/kassane/anotherBuildStep/.zig-cache/o/f1f9a84666bfa391f83adf5acb58f32b/zcc" -pie --hash-style=gnu --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o /home/kassane/anotherBuildStep/zig-out/bin/swift_example /usr/lib64/gcc/x86_64-pc-linux-gnu/14.2.1/../../../../lib64/Scrt1.o /usr/lib64/gcc/x86_64-pc-linux-gnu/14.2.1/../../../../lib64/crti.o /usr/lib64/gcc/x86_64-pc-linux-gnu/14.2.1/crtbeginS.o -L/home/kassane/.local/share/swiftly/toolchains/main-snapshot-2024-08-21/usr/lib/swift/linux -L/usr/lib64/gcc/x86_64-pc-linux-gnu/14.2.1 -L/usr/lib64/gcc/x86_64-pc-linux-gnu/14.2.1/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/lib -L/usr/lib --build-id -rpath /home/kassane/.local/share/swiftly/toolchains/main-snapshot-2024-08-21/usr/lib/swift/linux /home/kassane/.local/share/swiftly/toolchains/main-snapshot-2024-08-21/usr/lib/swift/linux/x86_64/swiftrt.o /tmp/TemporaryDirectory.lQzIxg/swift_example-1.o /tmp/TemporaryDirectory.lQzIxg/main-1.o /home/kassane/anotherBuildStep/.zig-cache/o/bc3cf51116dd07753cb6e0e1bc7adfd9/libzig_abi.a -lc++ -lswiftCxx -lswiftCxxStdlib -lswiftSwiftOnoneSupport -lswiftCore -lswift_Concurrency -lswift_StringProcessing -lswift_RegexParser -lswiftCore -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib64/gcc/x86_64-pc-linux-gnu/14.2.1/crtendS.o /usr/lib64/gcc/x86_64-pc-linux-gnu/14.2.1/../../../../lib64/crtn.o Build Summary: 5/5 steps succeeded install success └─ swift_example success 437ms MaxRSS:163M ├─ zig build-exe zcc ReleaseSafe native success 7s MaxRSS:229M │ └─ options success ├─ zig build-exe zcc ReleaseSafe native (+1 more reused dependencies) └─ zig build-lib zig_abi ReleaseSmall native success 45ms MaxRSS:117M ```cross-build
```bash ~/Downloads/zig-linux-x86_64-0.13.0/zig build --summary all --verbose -Dtarget=aarch64-linux /home/kassane/Downloads/zig-linux-x86_64-0.13.0/zig build-lib -OReleaseSmall -target aarch64-linux -mcpu baseline -Mroot=/home/kassane/anotherBuildStep/examples/zig_abi.zig -lc --cache-dir /home/kassane/anotherBuildStep/.zig-cache --global-cache-dir /home/kassane/.cache/zig --name zig_abi -static --listen=- /home/kassane/Downloads/zig-linux-x86_64-0.13.0/zig build-exe -OReleaseSafe --dep build_options -Mroot=/home/kassane/anotherBuildStep/tools/zigcc.zig -Mbuild_options=/home/kassane/anotherBuildStep/.zig-cache/c/ac477cbcacdcdbe3f19ea1d29c9e0030/options.zig --cache-dir /home/kassane/anotherBuildStep/.zig-cache --global-cache-dir /home/kassane/.cache/zig --name zcc --listen=- swiftc -import-bridging-header examples/c_include.h -g -clang-scanner-module-cache-path /home/kassane/anotherBuildStep/.zig-cache/o/dd6ab5043092597b04e52dd988141101 -module-cache-path /home/kassane/anotherBuildStep/.zig-cache/o/dd6ab5043092597b04e52dd988141101 -Xfrontend -disable-llvm-verify examples/main.swift -v -cxx-interoperability-mode=default -parse-as-library -Xcc -stdlib=libc++ -parse-stdlib -target arm64-unknown-linux-musl -o /home/kassane/anotherBuildStep/zig-out/bin/swift_example -ld-path=/home/kassane/anotherBuildStep/.zig-cache/o/53df6cb343ca4fdf7b1eef8868271284/zcc -use-ld=/home/kassane/anotherBuildStep/.zig-cache/o/53df6cb343ca4fdf7b1eef8868271284/zcc /home/kassane/anotherBuildStep/.zig-cache/o/a3817ff4d248ac9b4c1ed32cf21e9939/libzig_abi.a Swift version 6.1-dev (LLVM 9d45b9c9d5c48a0, Swift 08721cf939ec503) Target: arm64-unknown-linux-musl /home/kassane/.local/share/swiftly/toolchains/main-snapshot-2024-08-21/usr/bin/swift-frontend -frontend -target arm64-unknown-linux-musl -Xllvm -aarch64-use-tbi -disable-objc-interop -cxx-interoperability-mode=default -color-diagnostics -g -debug-info-format=dwarf -dwarf-version=4 -module-cache-path /home/kassane/anotherBuildStep/.zig-cache/o/dd6ab5043092597b04e52dd988141101 -parse-stdlib -disable-llvm-verify -empty-abi-descriptor -Xcc -working-directory -Xcc /home/kassane/anotherBuildStep -resource-dir /home/kassane/.local/share/swiftly/toolchains/main-snapshot-2024-08-21/usr/lib/swift -enable-anonymous-context-mangled-names -file-compilation-dir /home/kassane/anotherBuildStep -Xcc -stdlib=libc++ -import-objc-header /home/kassane/anotherBuildStep/examples/c_include.h -module-name swift_example -in-process-plugin-server-path /home/kassane/.local/share/swiftly/toolchains/main-snapshot-2024-08-21/usr/lib/swift/host/libSwiftInProcPluginServer.so -plugin-path /home/kassane/.local/share/swiftly/toolchains/main-snapshot-2024-08-21/usr/lib/swift/host/plugins -plugin-path /home/kassane/.local/share/swiftly/toolchains/main-snapshot-2024-08-21/usr/local/lib/swift/host/plugins -emit-pch -o /tmp/TemporaryDirectory.51cI71/c_include.pch /home/kassane/anotherBuildStep/examples/c_include.h /home/kassane/.local/share/swiftly/toolchains/main-snapshot-2024-08-21/usr/bin/swift-frontend -frontend -c -primary-file /home/kassane/anotherBuildStep/examples/main.swift -disable-objc-attr-requires-foundation-module -target arm64-unknown-linux-musl -Xllvm -aarch64-use-tbi -disable-objc-interop -cxx-interoperability-mode=default -color-diagnostics -g -debug-info-format=dwarf -dwarf-version=4 -module-cache-path /home/kassane/anotherBuildStep/.zig-cache/o/dd6ab5043092597b04e52dd988141101 -parse-stdlib -disable-llvm-verify -empty-abi-descriptor -Xcc -working-directory -Xcc /home/kassane/anotherBuildStep -resource-dir /home/kassane/.local/share/swiftly/toolchains/main-snapshot-2024-08-21/usr/lib/swift -enable-anonymous-context-mangled-names -file-compilation-dir /home/kassane/anotherBuildStep -Xcc -stdlib=libc++ -import-objc-header /tmp/TemporaryDirectory.51cI71/c_include.pch -module-name swift_example -in-process-plugin-server-path /home/kassane/.local/share/swiftly/toolchains/main-snapshot-2024-08-21/usr/lib/swift/host/libSwiftInProcPluginServer.so -plugin-path /home/kassane/.local/share/swiftly/toolchains/main-snapshot-2024-08-21/usr/lib/swift/host/plugins -plugin-path /home/kassane/.local/share/swiftly/toolchains/main-snapshot-2024-08-21/usr/local/lib/swift/host/plugins -parse-as-library -o /tmp/TemporaryDirectory.51cI71/main-1.o error: compile command failed due to signal 6 (use -v to see invocation) swift-frontend: /home/build-user/swift/lib/ClangImporter/ImportType.cpp:1613: swift::ImportedType adjustTypeForConcreteImport(ClangImporter::Implementation &, (anonymous namespace)::ImportResult, swift::ImportTypeKind, bool, swift::Bridgeability, llvm::function_ref