Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[sdk] Add "-Xlinker" to linker for Swift projects #895

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 44 additions & 20 deletions .github/workflows/swift-toolchain.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2017,8 +2017,9 @@ jobs:
cxxflags: ${{ inputs.WINDOWS_CMAKE_CXX_FLAGS }}
swiftflags: ${{ inputs.CMAKE_Swift_FLAGS }}
os: Windows
linker_flags: '-D CMAKE_EXE_LINKER_FLAGS="${{ inputs.WINDOWS_CMAKE_EXE_LINKER_FLAGS }}" -D CMAKE_SHARED_LINKER_FLAGS="${{ inputs.WINDOWS_CMAKE_SHARED_LINKER_FLAGS }}" -D CMAKE_STATIC_LIBRARY_PREFIX_Swift=lib'
extra_flags:
exe_linker_flags: ${{ inputs.WINDOWS_CMAKE_EXE_LINKER_FLAGS }}
shared_linker_flags: ${{ inputs.WINDOWS_CMAKE_SHARED_LINKER_FLAGS }}
extra_flags: '-D CMAKE_STATIC_LIBRARY_PREFIX_Swift=lib'

- arch: arm64
cpu: 'aarch64'
Expand All @@ -2030,8 +2031,9 @@ jobs:
cxxflags: ${{ inputs.WINDOWS_CMAKE_CXX_FLAGS }}
swiftflags: ${{ inputs.CMAKE_Swift_FLAGS }}
os: Windows
linker_flags: '-D CMAKE_EXE_LINKER_FLAGS="${{ inputs.WINDOWS_CMAKE_EXE_LINKER_FLAGS }}" -D CMAKE_SHARED_LINKER_FLAGS="${{ inputs.WINDOWS_CMAKE_SHARED_LINKER_FLAGS }}" -D CMAKE_STATIC_LIBRARY_PREFIX_Swift=lib'
extra_flags:
exe_linker_flags: ${{ inputs.WINDOWS_CMAKE_EXE_LINKER_FLAGS }}
shared_linker_flags: ${{ inputs.WINDOWS_CMAKE_SHARED_LINKER_FLAGS }}
extra_flags: '-D CMAKE_STATIC_LIBRARY_PREFIX_Swift=lib'

- arch: x86
cpu: 'i686'
Expand All @@ -2043,8 +2045,9 @@ jobs:
cxxflags: ${{ inputs.WINDOWS_CMAKE_CXX_FLAGS }}
swiftflags: ${{ inputs.CMAKE_Swift_FLAGS }}
os: Windows
linker_flags: '-D CMAKE_EXE_LINKER_FLAGS="${{ inputs.WINDOWS_CMAKE_EXE_LINKER_FLAGS }}" -D CMAKE_SHARED_LINKER_FLAGS="${{ inputs.WINDOWS_CMAKE_SHARED_LINKER_FLAGS }}" -D CMAKE_STATIC_LIBRARY_PREFIX_Swift=lib'
extra_flags:
exe_linker_flags: ${{ inputs.WINDOWS_CMAKE_EXE_LINKER_FLAGS }}
shared_linker_flags: ${{ inputs.WINDOWS_CMAKE_SHARED_LINKER_FLAGS }}
extra_flags: '-D CMAKE_STATIC_LIBRARY_PREFIX_Swift=lib'

- arch: arm64
cpu: 'aarch64'
Expand All @@ -2056,7 +2059,8 @@ jobs:
cxxflags: ${{ inputs.ANDROID_CMAKE_CXX_FLAGS }}
swiftflags: -sdk $NDKPATH/toolchains/llvm/prebuilt/windows-x86_64/sysroot -Xclang-linker -target -Xclang-linker aarch64-unknown-linux-android${{ inputs.ANDROID_API_LEVEL }} -Xclang-linker --sysroot -Xclang-linker $NDKPATH/toolchains/llvm/prebuilt/windows-x86_64/sysroot -Xclang-linker -resource-dir -Xclang-linker $NDKPATH/toolchains/llvm/prebuilt/windows-x86_64/lib/clang/17 -L ${{ github.workspace }}/BinaryCache/swift/lib/swift/android -g
os: Android
linker_flags: '-D CMAKE_EXE_LINKER_FLAGS="${{ inputs.ANDROID_CMAKE_EXE_LINKER_FLAGS }}" -D CMAKE_SHARED_LINKER_FLAGS="${{ inputs.ANDROID_CMAKE_SHARED_LINKER_FLAGS }}"'
exe_linker_flags: ${{ inputs.ANDROID_CMAKE_EXE_LINKER_FLAGS }}
shared_linker_flags: ${{ inputs.ANDROID_CMAKE_SHARED_LINKER_FLAGS }}
extra_flags: -DSWIFT_BUILD_CLANG_OVERLAYS_SKIP_BUILTIN_FLOAT=YES -DLLVM_ENABLE_LIBCXX=YES -DSWIFT_USE_LINKER=lld -DCMAKE_ANDROID_API=${{ inputs.ANDROID_API_LEVEL }} -DCMAKE_ANDROID_ARCH_ABI=arm64-v8a

- arch: armv7
Expand All @@ -2069,7 +2073,8 @@ jobs:
cxxflags: ${{ inputs.ANDROID_CMAKE_CXX_FLAGS }}
swiftflags: -sdk $NDKPATH/toolchains/llvm/prebuilt/windows-x86_64/sysroot -Xclang-linker -target -Xclang-linker armv7a-unknown-linux-androideabi${{ inputs.ANDROID_API_LEVEL }} -Xclang-linker --sysroot -Xclang-linker $NDKPATH/toolchains/llvm/prebuilt/windows-x86_64/sysroot -Xclang-linker -resource-dir -Xclang-linker $NDKPATH/toolchains/llvm/prebuilt/windows-x86_64/lib/clang/17 -L ${{ github.workspace }}/BinaryCache/swift/lib/swift/android -g
os: Android
linker_flags: '-D CMAKE_EXE_LINKER_FLAGS="${{ inputs.ANDROID_CMAKE_EXE_LINKER_FLAGS }}" -D CMAKE_SHARED_LINKER_FLAGS="${{ inputs.ANDROID_CMAKE_SHARED_LINKER_FLAGS }}"'
exe_linker_flags: ${{ inputs.ANDROID_CMAKE_EXE_LINKER_FLAGS }}
shared_linker_flags: ${{ inputs.ANDROID_CMAKE_SHARED_LINKER_FLAGS }}
extra_flags: -DSWIFT_BUILD_CLANG_OVERLAYS_SKIP_BUILTIN_FLOAT=YES -DLLVM_ENABLE_LIBCXX=YES -DSWIFT_USE_LINKER=lld -DCMAKE_ANDROID_API=${{ inputs.ANDROID_API_LEVEL }} -DCMAKE_ANDROID_ARCH_ABI=armeabi-v7a

- arch: i686
Expand All @@ -2082,7 +2087,8 @@ jobs:
cxxflags: ${{ inputs.ANDROID_CMAKE_CXX_FLAGS }}
swiftflags: -sdk $NDKPATH/toolchains/llvm/prebuilt/windows-x86_64/sysroot -Xclang-linker -target -Xclang-linker i686-unknown-linux-android${{ inputs.ANDROID_API_LEVEL }} -Xclang-linker --sysroot -Xclang-linker $NDKPATH/toolchains/llvm/prebuilt/windows-x86_64/sysroot -Xclang-linker -resource-dir -Xclang-linker $NDKPATH/toolchains/llvm/prebuilt/windows-x86_64/lib/clang/17 -L ${{ github.workspace }}/BinaryCache/swift/lib/swift/android -g
os: Android
linker_flags: '-D CMAKE_EXE_LINKER_FLAGS="${{ inputs.ANDROID_CMAKE_EXE_LINKER_FLAGS }}" -D CMAKE_SHARED_LINKER_FLAGS="${{ inputs.ANDROID_CMAKE_SHARED_LINKER_FLAGS }}"'
exe_linker_flags: ${{ inputs.ANDROID_CMAKE_EXE_LINKER_FLAGS }}
shared_linker_flags: ${{ inputs.ANDROID_CMAKE_SHARED_LINKER_FLAGS }}
extra_flags: -DSWIFT_BUILD_CLANG_OVERLAYS_SKIP_BUILTIN_FLOAT=YES -DLLVM_ENABLE_LIBCXX=YES -DSWIFT_USE_LINKER=lld -DCMAKE_ANDROID_API=${{ inputs.ANDROID_API_LEVEL }} -DCMAKE_ANDROID_ARCH_ABI=x86

- arch: x86_64
Expand All @@ -2095,7 +2101,8 @@ jobs:
cxxflags: ${{ inputs.ANDROID_CMAKE_CXX_FLAGS }}
swiftflags: -sdk $NDKPATH/toolchains/llvm/prebuilt/windows-x86_64/sysroot -Xclang-linker -target -Xclang-linker x86_64-unknown-linux-android${{ inputs.ANDROID_API_LEVEL }} -Xclang-linker --sysroot -Xclang-linker $NDKPATH/toolchains/llvm/prebuilt/windows-x86_64/sysroot -Xclang-linker -resource-dir -Xclang-linker $NDKPATH/toolchains/llvm/prebuilt/windows-x86_64/lib/clang/17 -L ${{ github.workspace }}/BinaryCache/swift/lib/swift/android -g
os: Android
linker_flags: '-D CMAKE_EXE_LINKER_FLAGS="${{ inputs.ANDROID_CMAKE_EXE_LINKER_FLAGS }}" -D CMAKE_SHARED_LINKER_FLAGS="${{ inputs.ANDROID_CMAKE_SHARED_LINKER_FLAGS }}"'
exe_linker_flags: ${{ inputs.ANDROID_CMAKE_EXE_LINKER_FLAGS }}
shared_linker_flags: ${{ inputs.ANDROID_CMAKE_SHARED_LINKER_FLAGS }}
extra_flags: -DSWIFT_BUILD_CLANG_OVERLAYS_SKIP_BUILTIN_FLOAT=YES -DLLVM_ENABLE_LIBCXX=YES -DSWIFT_USE_LINKER=lld -DCMAKE_ANDROID_API=${{ inputs.ANDROID_API_LEVEL }} -DCMAKE_ANDROID_ARCH_ABI=x86_64

name: ${{ matrix.os }} ${{ matrix.arch }} SDK
Expand Down Expand Up @@ -2197,11 +2204,6 @@ jobs:
components: 'Microsoft.VisualStudio.Component.VC.Tools.x86.x64;Microsoft.VisualStudio.Component.VC.Tools.ARM64'
arch: ${{ matrix.arch }}

# FIXME(compnerd): workaround CMake 3.29-3.30 issue
- uses: lukka/get-cmake@aa1df13cce8c30d2cb58efa871271c5a764623f8 # main
with:
cmakeVersion: 3.28.6

- uses: nttld/setup-ndk@v1
if: matrix.os == 'Android' && inputs.build_android
id: setup-ndk
Expand Down Expand Up @@ -2250,7 +2252,8 @@ jobs:
-D CMAKE_SYSTEM_PROCESSOR=${CMAKE_CPU} `
-D MSVC_C_ARCHITECTURE_ID=${{ matrix.arch }} `
-D MSVC_CXX_ARCHITECTURE_ID=${{ matrix.arch }} `
${{ matrix.linker_flags }} `
-D CMAKE_EXE_LINKER_FLAGS="${{ matrix.exe_linker_flags }}" `
-D CMAKE_SHARED_LINKER_FLAGS="${{ matrix.shared_linker_flags }}" `
${{ matrix.extra_flags }} `
$CMAKE_NDK_FLAG `
$SWIFT_NDK_FLAG `
Expand All @@ -2270,6 +2273,13 @@ jobs:
$CLANG_CL = cygpath -m ${{ github.workspace }}/BinaryCache/Library/Developer/Toolchains/${{ inputs.swift_version }}+Asserts/usr/bin/clang-cl.exe
$SWIFTC = cygpath -m ${{ github.workspace }}/BinaryCache/Library/Developer/Toolchains/${{ inputs.swift_version }}+Asserts/usr/bin/swiftc.exe

# Worarkound CMake 3.30 issue where CMAKE_[*]_FLAGS are passed as-is to the linker driver.
# TODO: Once we have CMake 4.0, set CMP0181 to NEW and pass these as "LINKER:" flags.
$CMAKE_SHARED_LINKER_FLAGS =
("${{ matrix.shared_linker_flags }}".Split(" ").Where({ $_.Trim() -ne ''}) | ForEach-Object { "-Xlinker $_" }) -join " "
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if there is a space in a path?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will break, but it also breaks currently too as CMake also parses the space as a separator. Note that we don't perform any kind of escape for quotes here either.
The good news is that when we switch to CMake 4.0, we'll be able to handle these properly since LINKER: supports comma-separated values.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I thought it was possible to get that passed properly. Well, I guess if it's no worse off ...

$CMAKE_EXE_LINKER_FLAGS =
("${{ matrix.exe_linker_flags }}".Split(" ").Where({ $_.Trim() -ne ''}) | ForEach-Object { "-Xlinker $_" }) -join " "

if ("${{ matrix.os }}" -eq "Android") {
$NDKPATH = cygpath -m ${{ steps.setup-ndk.outputs.ndk-path }}
# Since win/arm64 doesn't have one, this logic is necessary because
Expand Down Expand Up @@ -2307,7 +2317,6 @@ jobs:
-D CMAKE_CXX_COMPILER=${{ matrix.cxx }} `
-D CMAKE_CXX_COMPILER_TARGET=${{ matrix.triple }} `
-D CMAKE_CXX_FLAGS="${{ matrix.cxxflags }}" `
-D CMAKE_EXE_LINKER_FLAGS="${{ inputs.WINDOWS_CMAKE_EXE_LINKER_FLAGS }}" `
-D CMAKE_FIND_PACKAGE_PREFER_CONFIG=YES `
-D CMAKE_INSTALL_PREFIX=${{ github.workspace }}/BuildRoot/Library/Developer/Platforms/${{ matrix.os }}.platform/Developer/SDKs/${{ matrix.os }}.sdk/usr `
-D CMAKE_Swift_COMPILER=${SWIFTC} `
Expand All @@ -2318,7 +2327,10 @@ jobs:
-D CMAKE_SYSTEM_PROCESSOR=${CMAKE_CPU} `
-D MSVC_C_ARCHITECTURE_ID=${{ matrix.arch }} `
-D MSVC_CXX_ARCHITECTURE_ID=${{ matrix.arch }} `
${{ matrix.linker_flags }} `
-D CMAKE_EXE_LINKER_FLAGS="${CMAKE_EXE_LINKER_FLAGS}" `
-D CMAKE_SHARED_LINKER_FLAGS="${CMAKE_SHARED_LINKER_FLAGS}" `
-D CMAKE_EXE_LINKER_FLAGS_RELEASE="" `
-D CMAKE_SHARED_LINKER_FLAGS_RELEASE="" `
${{ matrix.extra_flags }} `
$CMAKE_NDK_FLAG `
$SWIFT_NDK_FLAG `
Expand Down Expand Up @@ -2349,6 +2361,14 @@ jobs:
$CLANG_CL = cygpath -m ${{ github.workspace }}/BinaryCache/Library/Developer/Toolchains/${{ inputs.swift_version }}+Asserts/usr/bin/clang-cl.exe
$SWIFTC = cygpath -m ${{ github.workspace }}/BinaryCache/Library/Developer/Toolchains/${{ inputs.swift_version }}+Asserts/usr/bin/swiftc.exe

# Worarkound CMake 3.30 issue where CMAKE_[*]_FLAGS are passed as-is to the linker driver.
# Pass these as "-Xlinker" flags to avoid the issue.
# TODO: Once we use CMake 4.0, set CMP0181 to NEW and pass these as "LINKER:" flags.
$CMAKE_SHARED_LINKER_FLAGS =
("${{ matrix.shared_linker_flags }}".Split(" ").Where({ $_.Trim() -ne ''}) | ForEach-Object { "-Xlinker $_" }) -join " "
$CMAKE_EXE_LINKER_FLAGS =
("${{ matrix.exe_linker_flags }}".Split(" ").Where({ $_.Trim() -ne ''}) | ForEach-Object { "-Xlinker $_" }) -join " "

if ("${{ matrix.os }}" -eq "Android") {
$NDKPATH = cygpath -m ${{ steps.setup-ndk.outputs.ndk-path }}
# Since win/arm64 doesn't have one, this logic is necessary because
Expand Down Expand Up @@ -2383,7 +2403,10 @@ jobs:
-D CMAKE_Swift_FLAGS_RELEASE="-O" `
-D CMAKE_SYSTEM_NAME=${{ matrix.os }} `
-D CMAKE_SYSTEM_PROCESSOR=${CMAKE_CPU} `
${{ matrix.linker_flags }} `
-D CMAKE_EXE_LINKER_FLAGS="${CMAKE_EXE_LINKER_FLAGS}" `
-D CMAKE_SHARED_LINKER_FLAGS="${CMAKE_SHARED_LINKER_FLAGS}" `
-D CMAKE_EXE_LINKER_FLAGS_RELEASE="" `
-D CMAKE_SHARED_LINKER_FLAGS_RELEASE="" `
${{ matrix.extra_flags }} `
$CMAKE_NDK_FLAG `
$SWIFT_NDK_FLAG `
Expand Down Expand Up @@ -2438,7 +2461,8 @@ jobs:
-D CMAKE_Swift_FLAGS_RELEASE="-O" `
-D CMAKE_SYSTEM_NAME=${{ matrix.os }} `
-D CMAKE_SYSTEM_PROCESSOR=${CMAKE_CPU} `
${{ matrix.linker_flags }} `
-D CMAKE_EXE_LINKER_FLAGS="${{ matrix.exe_linker_flags }}" `
-D CMAKE_SHARED_LINKER_FLAGS="${{ matrix.shared_linker_flags }}" `
${{ matrix.extra_flags }} `
$CMAKE_NDK_FLAG `
$SWIFT_NDK_FLAG `
Expand Down