# CMake, Swift-Syntax and Windows

**URL:** <https://forums.swift.org/t/cmake-swift-syntax-and-windows/85027>\
**Category:** Windows\
**Tags:** swift-syntax, cmake\
**Created:** [February 26, 2026, 6:34pm UTC](https://forums.swift.org/t/cmake-swift-syntax-and-windows/85027 "2026-02-26T18:34:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zaph](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/zaph/32/42892_2.png) [@zaph](https://forums.swift.org/u/zaph)\
**Post date:** [February 26, 2026, 6:34pm UTC](https://forums.swift.org/t/cmake-swift-syntax-and-windows/85027/1 "2026-02-26T18:34:57Z")

</div>

I've been adding cmake support to [qtbridge-swift](https://github.com/qt/qtbridge-swift) with this [commit](https://github.com/qt/qtbridge-swift/commit/037dca0837c5b0342acb6d964dea3c756b30d137). It works perfectly fine on macOS and linux. I suspect it'd be relatively trivial to get it to work on other platforms that support c++ interop. Apart from Windows that is ):

This is the relevant [patch](https://codereview.qt-project.org/c/qt/qtbridge-swift/+/714222) with the main hiccups being todo with swift-syntax. The first issue is long paths:

```swift
C:/qtbridge-swift/Examples/ChatApp/ChatApp/build/_deps/qtbridgelocal-build/QtBridgeMacros_External-prefix/src/QtBridgeMacros_External-build/_deps/swiftsyntax-build/Sources/_SwiftLibraryPluginProviderCShims/CMakeFiles/_SwiftLibraryPluginProviderCShims.dir/./

  has 257 characters. The maximum full path to an object file is 250
  characters (see CMAKE_OBJECT_PATH_MAX).

```

Which can at least be side-stepped a bit by shortening the prefix.

The other issue is that `swiftc` seems to really want lib-prefixed libraries:

```swift
LINK : fatal error LNK1104: cannot open file 'libSwiftSyntaxMacros.lib'
clang: error: linker command failed with exit code 1104 (use -v to see invocation)
ninja: build stopped: subcommand failed

```

So I've had to do a bit of a hack to copy the .libs and prefix them:

```cmake
function(qtbridge_fix_lib_prefix)
    if(WIN32)
        set(multiValueArgs TARGETS)
        cmake_parse_arguments(FIX_LIB "" "" "${multiValueArgs}" ${ARGN})

        foreach(_t IN LISTS FIX_LIB_TARGETS)
            if(TARGET ${_t})
                add_custom_target(_qtbridge_copy_${_t} ALL
                    COMMAND ${CMAKE_COMMAND} -E copy_if_different
                        "$<TARGET_FILE:${_t}>"
                        "$<TARGET_FILE_DIR:${_t}>/lib$<TARGET_FILE_BASE_NAME:${_t}>$<TARGET_FILE_SUFFIX:${_t}>"
                    DEPENDS ${_t}
                    COMMENT "Copy ${_t} to lib-prefixed name for swiftc on Windows"
                )
            endif()
        endforeach()
    endif()
endfunction()

```

From the top of my head, this solely arises for swift-syntax and not other libraries?

`keeshux`'s Swon library has run into the exact same two [problems](https://github.com/keeshux/swon/issues/12).

The fact that the official [example](https://github.com/swiftlang/swift-cmake-examples/blob/main/4_swift_macros/readme.md) didn't need to cater to those issues makes me assume that they weren't around at one point in time?

---

<div class="post-metadata">

**Author:** ![compnerd](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/compnerd/32/2165_2.png) [@compnerd](https://forums.swift.org/u/compnerd)\
**Post date:** [February 26, 2026, 8:23pm UTC](https://forums.swift.org/t/cmake-swift-syntax-and-windows/85027/2 "2026-02-26T20:23:14Z")

</div>

> [@zaph](#):
>
> The other issue is that `swiftc` seems to really want lib-prefixed libraries:

I think that this is misinterpreting the error message. Linking on Windows is a different model (more similar to modern day macOS, and to AIX).

For dynamically linked library (DLL), you have two pieces.

1. The DLL, which the developer may not see - it is purely a runtime component. The code should magically appear when needed (aka, it is part of the application distribution or system). These do not have `lib` prefixes unlike the Unix environments.
2. The import library - this is all the developer sees - a promise of the ABI. You link against this, and is what the developer receives from the vendor. Traditionally, this is named the same as the DLL with a `.lib` suffix. So, if your DLL is `SwiftSyntaxMacros.dll`, you would have `SwiftSyntaxMacros.lib` as the import library.

For static libraries, you have only the library itself. Static libraries on Unix use `.a` as the suffix, but Windows uses `.lib`. So, now if you have a static and dynamic library option, and you want to share that with the developer, you get `SwiftSyntaxMacros.lib` and `SwiftSyntaxMacros.lib`. Since you cannot have both in the same location, I opted to adopt a Microsoft runtime convention: static libraries are `lib<name>.lib` and import libraries are `<name>.lib`.

Now, you did not share the CMake invocation, but I suspect that you did not explicitly add in `-D BUILD_SHARED_LIBS=YES` and so you are generating static libraries. The fact that you are building static libraries is **critical**.

Unlike Unix, static and dynamic libraries are not interchangeable on Windows. That directly impacts the code generation. To accommodate that, we embed that information into the Swift module, which is then consumed for both code generation as well as the auto-linking.

This is not a bug or an issue, its simply a mismatch in what you are building and trying to use.

---

<div class="post-metadata">

**Author:** ![zaph](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/zaph/32/42892_2.png) [@zaph](https://forums.swift.org/u/zaph)\
**Post date:** [March 3, 2026, 9:48pm UTC](https://forums.swift.org/t/cmake-swift-syntax-and-windows/85027/3 "2026-03-03T21:48:13Z")

</div>

Yup, that was it! Thanks a lot 😃 :

```cmake
include(ExternalProject)
set(QTBRIDGE_MACROS_EP_ROOT "${CMAKE_BINARY_DIR}/qtbridge_macros_ep")
ExternalProject_Add(QtBridgeMacros_External
    PREFIX "${QTBRIDGE_MACROS_EP_ROOT}"
    BINARY_DIR "${QTBRIDGE_MACROS_EP_ROOT}/build"
    SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/Sources/QtBridgeMacros"
    CMAKE_ARGS
        -G Ninja
        -DCMAKE_BUILD_TYPE=Release
        -DBUILD_SHARED_LIBS=YES
    USES_TERMINAL_BUILD TRUE
    INSTALL_COMMAND ""
)
ExternalProject_Get_Property(QtBridgeMacros_External BINARY_DIR)
set(QTBRIDGE_MACROS_BINARY_DIR "${BINARY_DIR}" CACHE PATH "Path to QtBridgeMacros build")

```

Is there a more reliable workaround or a solution to the long paths?

---

<div class="post-metadata">

**Author:** ![compnerd](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/compnerd/32/2165_2.png) [@compnerd](https://forums.swift.org/u/compnerd)\
**Post date:** [March 3, 2026, 11:34pm UTC](https://forums.swift.org/t/cmake-swift-syntax-and-windows/85027/4 "2026-03-03T23:34:35Z")

</div>

`CMAKE_NINJA_FORCE_RESPONSE_FILE` should allow you to get away with the longer paths. To be honest, I'm a bit surprised as I would expect that to generally kick in when appropriate.

CC: @etcwilde

---

<div class="post-metadata">

**Author:** ![etcwilde](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/etcwilde/32/13687_2.png) [@etcwilde](https://forums.swift.org/u/etcwilde)\
**Post date:** [March 4, 2026, 12:03am UTC](https://forums.swift.org/t/cmake-swift-syntax-and-windows/85027/5 "2026-03-04T00:03:15Z")

</div>

It's complicated. There is logic in the generator to do that. It's based on the command line length:

> <https://github.com/Kitware/CMake/blob/e0fb92d86ef61b062324c90165fca0c6d7cb386d/Source/cmGlobalNinjaGenerator.cxx#L309-L319>

AND.... it's funnier than that.

The calls to write the build pass in a `commandLineLenghtLimit`:

> <https://github.com/Kitware/CMake/blob/e0fb92d86ef61b062324c90165fca0c6d7cb386d/Source/cmNinjaTargetGenerator.cxx#L1642-L1643>

that is either `0` or `-1`, based on whether or not you are forcing the response file...

> <https://github.com/Kitware/CMake/blob/e0fb92d86ef61b062324c90165fca0c6d7cb386d/Source/cmNinjaTargetGenerator.cxx#L1417-L1418>

(Here's the invocation for building Swift modules)

> <https://github.com/Kitware/CMake/blob/e0fb92d86ef61b062324c90165fca0c6d7cb386d/Source/cmNinjaTargetGenerator.cxx#L2105-L2107>

> <https://github.com/Kitware/CMake/blob/e0fb92d86ef61b062324c90165fca0c6d7cb386d/Source/cmNinjaTargetGenerator.cxx#L2564-L2569>

The cutoff should probably be a number somewhere between zero and infinity, but this is where it's standing today. 😅
