Skip to content

Commit 0e489e9

Browse files
authored
Add CMake build of SwiftPM dependencies (#5233)
* Add CMake build of SwiftPM dependencies * Specify cl as the C compiler * Update readme
1 parent 7ae6266 commit 0e489e9

File tree

3 files changed

+208
-0
lines changed

3 files changed

+208
-0
lines changed

CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ endif()
4040
project(Foundation
4141
LANGUAGES C Swift)
4242

43+
option(FOUNDATION_SWIFTPM_DEPS "build Windows SwiftPM dependencies via CMake" NO)
44+
if(FOUNDATION_SWIFTPM_DEPS)
45+
include(WindowsSwiftPMDependencies)
46+
_foundation_setup_windows_swiftpm_dependencies_target()
47+
return()
48+
endif()
49+
4350
if(NOT SWIFT_SYSTEM_NAME)
4451
if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
4552
set(SWIFT_SYSTEM_NAME macosx)

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,17 @@ swift-corelibs-foundation builds as a standalone project using Swift Package Man
7272

7373
swift-corelibs-foundation also builds as part of the toolchain for non-Darwin platforms. Instructions on building the toolchain are available in the [Swift project](https://github.com/swiftlang/swift?tab=readme-ov-file#building).
7474

75+
### Building swift-corelibs-foundation on Windows
76+
77+
When building Foundation as a standalone project, it requires you to provide some dependencies that it will link during the build. SwiftPM already fetches most of these dependencies and on Linux the remaining dependencies (dispatch, zlib, curl, libxml) are found in the Swift toolchain or on the host OS. However, Windows does not ship with zlib/curl/libxml on the host OS. In order to build swift-corelibs-foundation as a package on Windows, you must first checkout and build these dependenies before running `swift build` as recommended above. To do this, you can build the provided CMake target which (instead of building Foundation via CMake) will checkout and build these 3 dependencies via CMake and provide environment variables that will connect the SwiftPM build to these dependencies. To build these targets, run the following commands:
78+
79+
```
80+
cmake -G Ninja -B <build folder> -DFOUNDATION_SWIFTPM_DEPS=YES
81+
cmake --build <build folder> --target --target WindowsSwiftPMDependencies
82+
```
83+
84+
After running these commands, the output will include a list of environment variables to set. After setting these environment variables, you can run `swift test`/`swift build` just like on Linux in order to build swift-corelibs-foundation with an existing Swift toolchain.
85+
7586
## Contributions
7687

7788
We welcome contributions to Foundation! Please see the [known issues](Docs/Issues.md) page if you are looking for an area where we need help. We are also standing by on the [mailing lists](https://swift.org/community/#communication) to answer questions about what is most important to do and what we will accept into the project.
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
##===----------------------------------------------------------------------===##
2+
##
3+
## This source file is part of the Swift open source project
4+
##
5+
## Copyright (c) 2025 Apple Inc. and the Swift project authors
6+
## Licensed under Apache License v2.0
7+
##
8+
## See LICENSE.txt for license information
9+
## See CONTRIBUTORS.md for the list of Swift project authors
10+
##
11+
## SPDX-License-Identifier: Apache-2.0
12+
##
13+
##===----------------------------------------------------------------------===##
14+
15+
# Builds Windows CMake dependencies for a SwiftPM build (zlib, libxml, and curl)
16+
function(_foundation_setup_windows_swiftpm_dependencies_target)
17+
18+
message(STATUS "Configuring Windows SwiftPM dependencies target")
19+
20+
if(NOT CMAKE_HOST_SYSTEM_NAME STREQUAL Windows)
21+
message(FATAL_ERROR "Windows SwiftPM dependencies is only allowed on Windows hosts. Building on Linux does not require pre-building dependencies via CMake.")
22+
endif()
23+
24+
include(ExternalProject)
25+
26+
set(DEST_DIR "${CMAKE_BINARY_DIR}/windows-deps")
27+
28+
ExternalProject_Add(zlib
29+
GIT_REPOSITORY https://github.com/madler/zlib.git
30+
GIT_TAG v1.3.1
31+
CMAKE_ARGS
32+
-DCMAKE_INSTALL_PREFIX=${DEST_DIR}/zlib
33+
-DCMAKE_C_COMPILER=cl
34+
-DBUILD_SHARED_LIBS=NO
35+
-DCMAKE_POSITION_INDEPENDENT_CODE=YES
36+
-DCMAKE_BUILD_TYPE=Release
37+
EXCLUDE_FROM_ALL YES
38+
)
39+
40+
ExternalProject_Add(libxml
41+
GIT_REPOSITORY https://github.com/gnome/libxml2.git
42+
GIT_TAG v2.11.5
43+
CMAKE_ARGS
44+
-DCMAKE_INSTALL_PREFIX=${DEST_DIR}/libxml
45+
-DCMAKE_C_COMPILER=cl
46+
-DBUILD_SHARED_LIBS=NO
47+
-DLIBXML2_WITH_ICONV=NO
48+
-DLIBXML2_WITH_ICU=NO
49+
-DLIBXML2_WITH_LZMA=NO
50+
-DLIBXML2_WITH_PYTHON=NO
51+
-DLIBXML2_WITH_TESTS=NO
52+
-DLIBXML2_WITH_THREADS=YES
53+
-DLIBXML2_WITH_ZLIB=NO
54+
-DCMAKE_BUILD_TYPE=Release
55+
EXCLUDE_FROM_ALL YES
56+
)
57+
58+
set(ZLIB_ROOT "${DEST_DIR}/zlib")
59+
set(ZLIB_LIBRARY_DIR "${ZLIB_ROOT}/lib")
60+
set(ZLIB_INCLUDE_DIR "${ZLIB_ROOT}/include")
61+
set(ZLIB_LIBRARY_PATH "${ZLIB_LIBRARY_DIR}/zlibstatic.lib")
62+
63+
# Add a custom target for zlib's install step that curl can depend on
64+
ExternalProject_Add_StepTargets(zlib install)
65+
66+
ExternalProject_Add(curl
67+
GIT_REPOSITORY https://github.com/curl/curl.git
68+
GIT_TAG curl-8_9_1
69+
CMAKE_ARGS
70+
-DCMAKE_INSTALL_PREFIX=${DEST_DIR}/curl
71+
-DCMAKE_C_COMPILER=cl
72+
-DBUILD_SHARED_LIBS=NO
73+
-DBUILD_TESTING=NO
74+
-DBUILD_CURL_EXE=NO
75+
-DCURL_CA_BUNDLE=none
76+
-DCURL_CA_FALLBACK=NO
77+
-DCURL_CA_PATH=none
78+
-DCURL_BROTLI=NO
79+
-DCURL_DISABLE_ALTSVC=NO
80+
-DCURL_DISABLE_AWS=YES
81+
-DCURL_DISABLE_BASIC_AUTH=NO
82+
-DCURL_DISABLE_BEARER_AUTH=NO
83+
-DCURL_DISABLE_COOKIES=NO
84+
-DCURL_DISABLE_DICT=YES
85+
-DCURL_DISABLE_DIGEST_AUTH=NO
86+
-DCURL_DISABLE_DOH=NO
87+
-DCURL_DISABLE_FILE=YES
88+
-DCURL_DISABLE_FORM_API=NO
89+
-DCURL_DISABLE_FTP=YES
90+
-DCURL_DISABLE_GETOPTIONS=NO
91+
-DCURL_DISABLE_GOPHER=YES
92+
-DCURL_DISABLE_HSTS=NO
93+
-DCURL_DISABLE_HTTP=NO
94+
-DCURL_DISABLE_HTTP_AUTH=NO
95+
-DCURL_DISABLE_IMAP=YES
96+
-DCURL_DISABLE_KERBEROS_AUTH=NO
97+
-DCURL_DISABLE_LDAP=YES
98+
-DCURL_DISABLE_LDAPS=YES
99+
-DCURL_DISABLE_MIME=NO
100+
-DCURL_DISABLE_MQTT=YES
101+
-DCURL_DISABLE_NEGOTIATE_AUTH=NO
102+
-DCURL_DISABLE_NETRC=NO
103+
-DCURL_DISABLE_NTLM=NO
104+
-DCURL_DISABLE_PARSEDATE=NO
105+
-DCURL_DISABLE_POP3=YES
106+
-DCURL_DISABLE_PROGRESS_METER=YES
107+
-DCURL_DISABLE_PROXY=NO
108+
-DCURL_DISABLE_RTSP=YES
109+
-DCURL_DISABLE_SHUFFLE_DNS=YES
110+
-DCURL_DISABLE_SMB=YES
111+
-DCURL_DISABLE_SMTP=YES
112+
-DCURL_DISABLE_SOCKETPAIR=YES
113+
-DCURL_DISABLE_SRP=NO
114+
-DCURL_DISABLE_TELNET=YES
115+
-DCURL_DISABLE_TFTP=YES
116+
-DCURL_DISABLE_VERBOSE_STRINGS=NO
117+
-DCURL_LTO=NO
118+
-DCURL_USE_BEARSSL=NO
119+
-DCURL_USE_GNUTLS=NO
120+
-DCURL_USE_GSSAPI=NO
121+
-DCURL_USE_LIBPSL=NO
122+
-DCURL_USE_LIBSSH=NO
123+
-DCURL_USE_LIBSSH2=NO
124+
-DCURL_USE_MBEDTLS=NO
125+
-DCURL_USE_OPENSSL=NO
126+
-DCURL_USE_SCHANNEL=YES
127+
-DCURL_USE_WOLFSSL=NO
128+
-DCURL_WINDOWS_SSPI=YES
129+
-DCURL_ZLIB=YES
130+
-DCURL_ZSTD=NO
131+
-DENABLE_ARES=NO
132+
-DENABLE_CURLDEBUG=NO
133+
-DENABLE_DEBUG=NO
134+
-DENABLE_IPV6=YES
135+
-DENABLE_MANUAL=NO
136+
-DENABLE_THREADED_RESOLVER=NO
137+
-DENABLE_UNICODE=YES
138+
-DENABLE_UNIX_SOCKETS=NO
139+
-DENABLE_WEBSOCKETS=YES
140+
-DHAVE_POLL_FINE=NO
141+
-DUSE_IDN2=NO
142+
-DUSE_MSH3=NO
143+
-DUSE_NGHTTP2=NO
144+
-DUSE_NGTCP2=NO
145+
-DUSE_QUICHE=NO
146+
-DUSE_WIN32_IDN=YES
147+
-DUSE_WIN32_LARGE_FILES=YES
148+
-DUSE_WIN32_LDAP=NO
149+
-DCMAKE_BUILD_TYPE=Release
150+
-DZLIB_ROOT=${ZLIB_ROOT}
151+
-DZLIB_LIBRARY=${ZLIB_LIBRARY_PATH}
152+
-DZLIB_INCLUDE_DIR=${ZLIB_INCLUDE_DIR}
153+
DEPENDS zlib-install
154+
EXCLUDE_FROM_ALL YES
155+
)
156+
157+
158+
set(LIBXML_LIBRARY_DIR "${DEST_DIR}/libxml/lib")
159+
set(LIBXML_INCLUDE_DIR "${DEST_DIR}/libxml/include/libxml2")
160+
161+
set(CURL_LIBRARY_DIR "${DEST_DIR}/curl/lib")
162+
set(CURL_INCLUDE_DIR "${DEST_DIR}/curl/include")
163+
164+
message(STATUS "LIBXML_INCLUDE_PATH=${LIBXML_INCLUDE_DIR}")
165+
message(STATUS "LIBXML_LIBRARY_PATH=${LIBXML_LIBRARY_DIR}")
166+
message(STATUS "CURL_INCLUDE_PATH=${CURL_INCLUDE_DIR}")
167+
message(STATUS "CURL_LIBRARY_PATH=${CURL_LIBRARY_DIR}")
168+
message(STATUS "ZLIB_LIBRARY_PATH=${ZLIB_LIBRARY_DIR}")
169+
170+
ExternalProject_Add_StepTargets(libxml install)
171+
ExternalProject_Add_StepTargets(curl install)
172+
add_custom_target(WindowsSwiftPMDependencies
173+
DEPENDS libxml-install curl-install)
174+
175+
add_custom_command(TARGET WindowsSwiftPMDependencies POST_BUILD
176+
COMMAND echo Please set the following environment variables for the SwiftPM build:)
177+
add_custom_command(TARGET WindowsSwiftPMDependencies POST_BUILD
178+
COMMAND echo:)
179+
add_custom_command(TARGET WindowsSwiftPMDependencies POST_BUILD
180+
COMMAND echo LIBXML_INCLUDE_PATH=${LIBXML_INCLUDE_DIR})
181+
add_custom_command(TARGET WindowsSwiftPMDependencies POST_BUILD
182+
COMMAND echo LIBXML_LIBRARY_PATH=${LIBXML_LIBRARY_DIR})
183+
add_custom_command(TARGET WindowsSwiftPMDependencies POST_BUILD
184+
COMMAND echo CURL_INCLUDE_PATH=${CURL_INCLUDE_DIR})
185+
add_custom_command(TARGET WindowsSwiftPMDependencies POST_BUILD
186+
COMMAND echo CURL_LIBRARY_PATH=${CURL_LIBRARY_DIR})
187+
add_custom_command(TARGET WindowsSwiftPMDependencies POST_BUILD
188+
COMMAND echo ZLIB_LIBRARY_PATH=${ZLIB_LIBRARY_DIR})
189+
190+
endfunction()

0 commit comments

Comments
 (0)