Skip to content

Commit 5df0f04

Browse files
committed
build: use add_subdirectory instead of add_external_project
This switches the CoreFoundation sub-build to use `add_subdirectory`. This has a few **MAJOR** benefits: - CoreFoundation changes get tracked and cause compile/link triggers - CoreFoundation warnings are surfaced in the build - CoreFoundation dependencies are reflected in the right location - Foundation dependencies are reflected in the right location - The overall build is simpler Eventually, it should be possible to further simplify this. Unfortunately, a couple of hacks are needed to make this work. The sub-projects properties must be cached, but they were just passthrough previously, so this is not too terrible. Additionally, the library control know needs to be saved/restored around it. The worst thing here is the need to manually collect the dependency as we do not have a proper library target for the Foundation (Swift) library. When CMake gains proper Swift support, it should be possible to use `add_library` and get a property target that we can use `target_link_libraries` with permitting the dependency tracking to allow us to get that without manual intervention.
1 parent dfb1ba5 commit 5df0f04

File tree

2 files changed

+48
-63
lines changed

2 files changed

+48
-63
lines changed

CMakeLists.txt

Lines changed: 37 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -27,34 +27,14 @@ include(ExternalProject)
2727
string(TOLOWER ${CMAKE_SYSTEM_NAME} swift_os)
2828
get_swift_host_arch(swift_arch)
2929

30-
ExternalProject_Add(CoreFoundation
31-
SOURCE_DIR
32-
${CMAKE_CURRENT_SOURCE_DIR}/CoreFoundation
33-
CMAKE_COMMAND
34-
${CMAKE_COMMAND}
35-
CMAKE_ARGS
36-
-DBUILD_SHARED_LIBS=NO
37-
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
38-
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
39-
-DCMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM}
40-
-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
41-
-DCMAKE_INSTALL_LIBDIR=usr/lib
42-
-DCMAKE_SYSTEM_NAME=${CMAKE_SYSTEM_NAME}
43-
-DCMAKE_SYSTEM_PROCESSOR=${CMAKE_SYSTEM_PROCESSOR}
44-
-DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}
45-
-DCF_DEPLOYMENT_SWIFT=YES
46-
-DCF_ENABLE_LIBDISPATCH=${FOUNDATION_ENABLE_LIBDISPATCH}
47-
-DCF_PATH_TO_LIBDISPATCH_SOURCE=${FOUNDATION_PATH_TO_LIBDISPATCH_SOURCE}
48-
-DCF_PATH_TO_LIBDISPATCH_BUILD=${FOUNDATION_PATH_TO_LIBDISPATCH_BUILD}
49-
-DICU_LIBRARY=${ICU_LIBRARY}
50-
-DICU_INCLUDE_DIR=${ICU_INCLUDE_DIR}
51-
-DCURL_LIBRARY=${CURL_LIBRARY}
52-
-DCURL_INCLUDE_DIR=${CURL_INCLUDE_DIR}
53-
-DLIBXML2_LIBRARY=${LIBXML2_LIBRARY}
54-
-DLIBXML2_INCLUDE_DIR=${LIBXML2_INCLUDE_DIR}
55-
INSTALL_COMMAND
56-
${CMAKE_COMMAND} -E env --unset=DESTDIR ${CMAKE_COMMAND} --build . --target install)
57-
ExternalProject_Get_Property(CoreFoundation install_dir)
30+
set(CF_PATH_TO_LIBDISPATCH_SOURCE ${FOUNDATION_PATH_TO_LIBDISPATCH_SOURCE} CACHE PATH "Path to libdispatch source" FORCE)
31+
set(CF_PATH_TO_LIBDISPATCH_BUILD ${FOUNDATION_PATH_TO_LIBDISPATCH_BUILD} CACHE PATH "Path to libdispatch build" FORCE)
32+
set(CF_DEPLOYMENT_SWIFT YES CACHE BOOL "Build for Swift" FORCE)
33+
34+
set(SAVED_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS})
35+
set(BUILD_SHARED_LIBS NO)
36+
add_subdirectory(CoreFoundation)
37+
set(BUILD_SHARED_LIBS ${SAVED_BUILD_SHARED_LIBS})
5838

5939
add_library(uuid
6040
STATIC
@@ -67,13 +47,14 @@ set_target_properties(uuid
6747
# the dependency on TargetConditionals.h
6848
target_compile_options(uuid
6949
PUBLIC
70-
-I${install_dir}/System/Library/Frameworks/CoreFoundation.framework/Headers)
50+
-I${CMAKE_CURRENT_BINARY_DIR}/CoreFoundation.framework/Headers)
7151
if(CMAKE_SYSTEM_NAME STREQUAL Windows)
7252
target_compile_definitions(uuid
7353
PRIVATE
7454
_CRT_NONSTDC_NO_WARNINGS
7555
_CRT_SECURE_NO_DEPRECATE
7656
_CRT_SECURE_NO_WARNINGS)
57+
target_link_libraries(uuid PRIVATE Bcrypt)
7758
endif()
7859
add_dependencies(uuid CoreFoundation)
7960

@@ -107,32 +88,24 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL FreeBSD)
10788
set(Foundation_RPATH -Xlinker;-rpath;-Xlinker;"\\\$\$ORIGIN")
10889
elseif(CMAKE_SYSTEM_NAME STREQUAL Windows)
10990
set(deployment_target -DDEPLOYMENT_TARGET_WINDOWS)
110-
# FIXME(compnerd) these are not all CoreFoundation dependencies, some of them
111-
# are Foundation's and others are libcurl's. We should split them up
112-
# accordingly.
113-
set(CoreFoundation_INTERFACE_LIBRARIES
114-
-lAdvAPI32
115-
-lCrypt32
116-
-lDbgHelp
117-
-lShell32
118-
-lOle32
119-
-lRpcRT4
120-
-lSecur32
121-
-lShLwApi
122-
-lUser32
123-
-lWldap32
124-
-lWS2_32
125-
-liphlpapi
126-
-lmincore
127-
-lnormaliz
128-
-lpathcch
129-
-lucrt
130-
-lshell32)
13191
# FIXME(SR9138) Silence "locally defined symbol '…' imported in function '…'
13292
set(WORKAROUND_SR9138 -Xlinker;-ignore:4217)
13393
set(WORKAROUND_SR9995 -Xlinker;-nodefaultlib:libcmt)
13494
endif()
13595

96+
# NOTE(compnerd) this is a horrible hack to work around the fact that we do not
97+
# have a proper library target for Foundation which can link against the
98+
# CoreFoundation target. When we gain proper CMake support for Swift, we should
99+
# be able to remove this and just use
100+
# `target_link_libraries(Foundation PRIVATE CoreFoundation)`.
101+
set(CoreFoundation_LIBRARIES $<TARGET_FILE:CoreFoundation>)
102+
get_target_property(CoreFoundation_LINK_LIBRARIES CoreFoundation LINK_LIBRARIES)
103+
foreach(library ${CoreFoundation_LINK_LIBRARIES})
104+
if(NOT library STREQUAL Threads::Threads)
105+
list(APPEND CoreFoundation_LIBRARIES -l${library})
106+
endif()
107+
endforeach()
108+
136109
add_swift_library(Foundation
137110
MODULE_NAME
138111
Foundation
@@ -308,21 +281,25 @@ add_swift_library(Foundation
308281
CFLAGS
309282
${deployment_target}
310283
${deployment_enable_libdispatch}
311-
-F${install_dir}/System/Library/Frameworks
284+
-F${CMAKE_CURRENT_BINARY_DIR}
312285
-D_DLL
313286
LINK_FLAGS
314-
-L${install_dir}/usr/lib
315-
-lCoreFoundation
287+
${CoreFoundation_LIBRARIES}
316288
${CURL_LIBRARIES}
317289
${ICU_UC_LIBRARY} ${ICU_I18N_LIBRARY}
318290
${LIBXML2_LIBRARIES}
319291
${libdispatch_ldflags}
320-
-L${CMAKE_CURRENT_BINARY_DIR}
321-
-luuid
292+
$<TARGET_FILE:uuid>
322293
${Foundation_RPATH}
323-
${CoreFoundation_INTERFACE_LIBRARIES}
324294
${WORKAROUND_SR9138}
325295
${WORKAROUND_SR9995}
296+
$<$<PLATFORM_ID:Windows>:-lDbgHelp>
297+
$<$<PLATFORM_ID:Windows>:-lOle32>
298+
$<$<PLATFORM_ID:Windows>:-lShLwApi>
299+
$<$<PLATFORM_ID:Windows>:-lShell32>
300+
$<$<PLATFORM_ID:Windows>:-lWS2_32>
301+
$<$<PLATFORM_ID:Windows>:-liphlpapi>
302+
$<$<PLATFORM_ID:Windows>:-lpathcch>
326303
SWIFT_FLAGS
327304
-DDEPLOYMENT_RUNTIME_SWIFT
328305
${deployment_enable_libdispatch}
@@ -352,7 +329,7 @@ add_swift_executable(plutil
352329
CFLAGS
353330
${deployment_target}
354331
${deployment_enable_libdispatch}
355-
-F${install_dir}/System/Library/Frameworks
332+
-F${CMAKE_CURRENT_BINARY_DIR}
356333
LINK_FLAGS
357334
${libdispatch_ldflags}
358335
-L${CMAKE_CURRENT_BINARY_DIR}
@@ -378,7 +355,7 @@ if(ENABLE_TESTING)
378355
CFLAGS
379356
${deployment_target}
380357
${deployment_enable_libdispatch}
381-
-F${install_dir}/System/Library/Frameworks
358+
-F${CMAKE_CURRENT_BINARY_DIR}
382359
LINK_FLAGS
383360
${libdispatch_ldflags}
384361
-L${CMAKE_CURRENT_BINARY_DIR}
@@ -490,7 +467,7 @@ if(ENABLE_TESTING)
490467
CFLAGS
491468
${deployment_target}
492469
${deployment_enable_libdispatch}
493-
-F${install_dir}/System/Library/Frameworks
470+
-F${CMAKE_CURRENT_BINARY_DIR}
494471
LINK_FLAGS
495472
${libdispatch_ldflags}
496473
-L${CMAKE_CURRENT_BINARY_DIR}
@@ -576,7 +553,7 @@ else()
576553
endif()
577554
# TODO(compnerd) install as a Framework as that is how swift actually is built
578555
install(DIRECTORY
579-
${install_dir}/System/Library/Frameworks/CoreFoundation.framework/Headers/
556+
${CMAKE_CURRENT_BINARY_DIR}/CoreFoundation.framework/Headers/
580557
DESTINATION
581558
lib/swift/CoreFoundation
582559
FILES_MATCHING PATTERN "*.h")

CoreFoundation/CMakeLists.txt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ target_compile_definitions(CoreFoundation
373373

374374
target_include_directories(CoreFoundation
375375
PRIVATE
376-
${CMAKE_SOURCE_DIR})
376+
${PROJECT_SOURCE_DIR})
377377
if(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin)
378378
find_package(LibXml2 REQUIRED)
379379
target_include_directories(CoreFoundation
@@ -403,11 +403,11 @@ endif()
403403
if("${CMAKE_C_SIMULATE_ID}" STREQUAL "MSVC")
404404
target_compile_options(CoreFoundation
405405
PRIVATE
406-
$<$<COMPILE_LANGUAGE:C>:/FI${CMAKE_SOURCE_DIR}/Base.subproj/CoreFoundation_Prefix.h>)
406+
$<$<COMPILE_LANGUAGE:C>:/FI${PROJECT_SOURCE_DIR}/Base.subproj/CoreFoundation_Prefix.h>)
407407
else()
408408
target_compile_options(CoreFoundation
409409
PRIVATE
410-
$<$<COMPILE_LANGUAGE:C>:-include;${CMAKE_SOURCE_DIR}/Base.subproj/CoreFoundation_Prefix.h>)
410+
$<$<COMPILE_LANGUAGE:C>:-include;${PROJECT_SOURCE_DIR}/Base.subproj/CoreFoundation_Prefix.h>)
411411
endif()
412412

413413
if("${CMAKE_C_SIMULATE_ID}" STREQUAL "MSVC")
@@ -462,6 +462,14 @@ target_link_libraries(CoreFoundation
462462
PRIVATE
463463
Threads::Threads
464464
${CMAKE_DL_LIBS})
465+
if(CMAKE_SYSTEM_NAME STREQUAL Windows)
466+
target_link_libraries(CoreFoundation
467+
PRIVATE
468+
AdvAPI32
469+
Secur32
470+
User32
471+
mincore)
472+
endif()
465473
if(NOT CMAKE_SYSTEM_NAME STREQUAL Windows AND NOT CMAKE_SYSTEM_NAME STREQUAL Darwin)
466474
target_link_libraries(CoreFoundation
467475
PRIVATE

0 commit comments

Comments
 (0)