Skip to content

Commit 4686897

Browse files
authored
Merge pull request #82035 from edymtt/edymtt/refactor-findswiftcore
[Runtimes][CMake] Refactor FindSwiftCore to put focus on targets...
2 parents fb994d8 + 4efbe16 commit 4686897

File tree

2 files changed

+97
-94
lines changed

2 files changed

+97
-94
lines changed

Runtimes/Supplemental/cmake/modules/FindSwiftCore.cmake

Lines changed: 95 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ The module may set the following variables if `SwiftCore_DIR` is not set.
3737
3838
#]=======================================================================]
3939

40+
include_guard(GLOBAL)
41+
4042
# If the SwiftCore_DIR_FLAG is specified, look there instead. The cmake-generated
4143
# config file is more accurate, but requires that the SDK has one available.
4244
if(SwiftCore_DIR)
@@ -51,113 +53,112 @@ if(SwiftCore_DIR)
5153
endif()
5254

5355
include(FindPackageHandleStandardArgs)
56+
include(PlatformInfo)
57+
58+
# This was loosely modelled after other find modules
59+
# (namely FindGLEW), where the equivalent parameter
60+
# is not stored in cache (possibly because we want
61+
# the project importing it to be able to
62+
# it "immediately")
63+
if(NOT DEFINED SwiftCore_USE_STATIC_LIBS)
64+
set(SwiftCore_USE_STATIC_LIBS OFF)
65+
if(NOT BUILD_SHARED_LIBS AND NOT APPLE)
66+
set(SwiftCore_USE_STATIC_LIBS ON)
67+
endif()
68+
endif()
5469

5570
if(APPLE)
71+
list(APPEND SwiftCore_INCLUDE_DIR_HINTS
72+
"${CMAKE_OSX_SYSROOT}/usr/lib/swift")
73+
list(APPEND SwiftCore_LIBRARY_HINTS
74+
"${CMAKE_OSX_SYSROOT}/usr/lib/swift")
5675
# When building for Apple platforms, SwiftCore always comes from within the
5776
# SDK as a tbd for a shared library in the shared cache.
58-
find_path(SwiftCore_INCLUDE_DIR
59-
"Swift.swiftmodule"
60-
HINTS
61-
"${CMAKE_OSX_SYSROOT}/usr/lib/swift")
62-
find_library(SwiftCore_IMPLIB
63-
NAMES "libswiftCore.tbd"
64-
HINTS
65-
"${CMAKE_OSX_SYSROOT}/usr/lib/swift")
66-
add_library(swiftCore SHARED IMPORTED GLOBAL)
67-
set_target_properties(swiftCore PROPERTIES
68-
IMPORTED_IMPLIB "${SwiftCore_IMPLIB}"
69-
INTERFACE_INCLUDE_DIRECTORIES "${SwiftCore_INCLUDE_DIR}")
70-
find_package_handle_standard_args(SwiftCore DEFAULT_MSG
71-
SwiftCore_IMPLIB SwiftCore_INCLUDE_DIR)
77+
list(APPEND SwiftCore_NAMES libswiftCore.tbd)
7278
elseif(LINUX)
73-
if (NOT BUILD_SHARED_LIBS)
74-
find_path(SwiftCore_INCLUDE_DIR
75-
"Swift.swiftmodule"
76-
HINTS
77-
"${Swift_SDKROOT}/usr/lib/swift_static/linux-static")
78-
find_library(SwiftCore_LIBRARY
79-
NAMES "libswiftCore.a"
80-
HINTS "${Swift_SDKROOT}/usr/lib/swift_static/linux-static")
81-
add_library(swiftCore STATIC IMPORTED GLOBAL)
79+
if (SwiftCore_USE_STATIC_LIBS)
80+
list(APPEND SwiftCore_INCLUDE_DIR_HINTS
81+
"${Swift_SDKROOT}/usr/lib/swift_static/linux-static")
82+
list(APPEND SwiftCore_LIBRARY_HINTS
83+
"${Swift_SDKROOT}/usr/lib/swift_static/linux-static")
84+
list(APPEND SwiftCore_NAMES libswiftCore.a)
8285
else()
83-
find_path(SwiftCore_INCLUDE_DIR
84-
"Swift.swiftmodule"
85-
HINTS
86-
"${Swift_SDKROOT}/usr/lib/swift/linux")
87-
find_library(SwiftCore_LIBRARY
88-
NAMES "libswiftCore.so"
89-
HINTS "${Swift_SDKROOT}/usr/lib/swift/linux")
90-
add_library(swiftCore SHARED IMPORTED GLOBAL)
86+
list(APPEND SwiftCore_INCLUDE_DIR_HINTS
87+
"${Swift_SDKROOT}/usr/lib/swift/linux")
88+
list(APPEND SwiftCore_LIBRARY_HINTS
89+
"${Swift_SDKROOT}/usr/lib/swift/linux")
90+
list(APPEND SwiftCore_NAMES libswiftCore.so)
9191
endif()
92-
set_target_properties(swiftCore PROPERTIES
93-
IMPORTED_LOCATION "${SwiftCore_LIBRARY}"
94-
INTERFACE_INCLUDE_DIRECTORIES "${SwiftCore_INCLUDE_DIR}")
95-
find_package_handle_standard_args(SwiftCore DEFAULT_MSG
96-
SwiftCore_LIBRARY SwiftCore_INCLUDE_DIR)
9792
elseif(WIN32)
98-
find_path(SwiftCore_INCLUDE_DIR
99-
"Swift.swiftmodule"
100-
HINTS
101-
"${Swift_SDKROOT}/usr/lib/swift/windows"
102-
"$ENV{SDKROOT}/usr/lib/swift/windows")
103-
find_library(SwiftCore_LIBRARY
104-
NAMES "libswiftCore.lib"
105-
HINTS
106-
"${Swift_SDKROOT}/usr/lib/swift/${SwiftCore_PLATFORM_SUBDIR}/${SwiftCore_ARCH_SUBDIR}"
107-
"${Swift_SDKROOT}/usr/lib/swift"
108-
"$ENV{SDKROOT}/usr/lib/swift/${SwiftCore_PLATFORM_SUBDIR}/${SwiftCore_ARCH_SUBDIR}"
109-
"$ENV{SDKROOT}/usr/lib/swift")
110-
111-
add_library(swiftCore SHARED IMPORTED GLOBAL)
112-
set_target_properties(swiftCore PROPERTIES
113-
IMPORTED_IMPLIB "${SwiftCore_LIBRARY}"
114-
INTERFACE_INCLUDE_DIRECTORIES "${SwiftCore_INCLUDE_DIR}")
115-
find_package_handle_standard_args(SwiftCore DEFAULT_MSG
116-
SwiftCore_LIBRARY SwiftCore_INCLUDE_DIR)
93+
list(APPEND SwiftCore_INCLUDE_DIR_HINTS
94+
"${Swift_SDKROOT}/usr/lib/swift/windows"
95+
"$ENV{SDKROOT}/usr/lib/swift/windows")
96+
list(APPEND SwiftCore_LIBRARY_HINTS
97+
"${Swift_SDKROOT}/usr/lib/swift/${${PROJECT_NAME}_PLATFORM_SUBDIR}/${${PROJECT_NAME}_ARCH_SUBDIR}"
98+
"${Swift_SDKROOT}/usr/lib/swift"
99+
"$ENV{SDKROOT}/usr/lib/swift/${${PROJECT_NAME}_PLATFORM_SUBDIR}/${${PROJECT_NAME}_ARCH_SUBDIR}"
100+
"$ENV{SDKROOT}/usr/lib/swift")
101+
if (SwiftCore_USE_STATIC_LIBS)
102+
list(APPEND SwiftCore_NAMES libswiftCore.lib)
103+
else()
104+
list(APPEND SwiftCore_NAMES swiftCore.lib)
105+
endif()
117106
elseif(ANDROID)
118-
if(BUILD_SHARED_LIBS)
119-
find_path(SwiftCore_INCLUDE_DIR
120-
"Swift.swiftmodule"
121-
NO_CMAKE_FIND_ROOT_PATH
122-
HINTS
123-
"${Swift_SDKROOT}/usr/lib/swift/android"
124-
"$ENV{SDKROOT}/usr/lib/swift/android")
125-
find_library(SwiftCore_LIBRARY
126-
NO_CMAKE_FIND_ROOT_PATH
127-
NAMES "libswiftCore.so"
128-
HINTS
129-
"${Swift_SDKROOT}/usr/lib/swift/android/${SwiftCore_ARCH_SUBDIR}"
130-
"${Swift_SDKROOT}/usr/lib/swift"
131-
"$ENV{SDKROOT}/usr/lib/swift/android/${SwiftCore_ARCH_SUBDIR}"
132-
"$ENV{SDKROOT}/usr/lib/swift")
133-
134-
add_library(swiftCore SHARED IMPORTED GLOBAL)
107+
if (SwiftCore_USE_STATIC_LIBS)
108+
list(APPEND SwiftCore_INCLUDE_DIR_HINTS
109+
"${Swift_SDKROOT}/usr/lib/swift_static/android"
110+
"$ENV{SDKROOT}/usr/lib/swift_static/android")
111+
list(APPEND SwiftCore_LIBRARY_HINTS
112+
"${Swift_SDKROOT}/usr/lib/swift_static/android/${${PROJECT_NAME}_ARCH_SUBDIR}"
113+
"${Swift_SDKROOT}/usr/lib/swift_static"
114+
"$ENV{SDKROOT}/usr/lib/swift_static/android/${${PROJECT_NAME}_ARCH_SUBDIR}"
115+
"$ENV{SDKROOT}/usr/lib/swift_static")
116+
list(APPEND SwiftCore_NAMES libswiftCore.a)
135117
else()
136-
find_path(SwiftCore_INCLUDE_DIR
137-
"Swift.swiftmodule"
138-
NO_CMAKE_FIND_ROOT_PATH
139-
HINTS
140-
"${Swift_SDKROOT}/usr/lib/swift_static/android"
141-
"$ENV{SDKROOT}/usr/lib/swift_static/android")
142-
find_library(SwiftCore_LIBRARY
143-
NO_CMAKE_FIND_ROOT_PATH
144-
NAMES "libswiftCore.a"
145-
HINTS
146-
"${Swift_SDKROOT}/usr/lib/swift_static/android/${SwiftCore_ARCH_SUBDIR}"
147-
"${Swift_SDKROOT}/usr/lib/swift_static"
148-
"$ENV{SDKROOT}/usr/lib/swift_static/android/${SwiftCore_ARCH_SUBDIR}"
149-
"$ENV{SDKROOT}/usr/lib/swift_static")
150-
151-
add_library(swiftCore STATIC IMPORTED GLOBAL)
118+
list(APPEND SwiftCore_INCLUDE_DIR_HINTS
119+
"${Swift_SDKROOT}/usr/lib/swift/android"
120+
"$ENV{SDKROOT}/usr/lib/swift/android")
121+
list(APPEND SwiftCore_LIBRARY_HINTS
122+
"${Swift_SDKROOT}/usr/lib/swift/android/${${PROJECT_NAME}_ARCH_SUBDIR}"
123+
"${Swift_SDKROOT}/usr/lib/swift"
124+
"$ENV{SDKROOT}/usr/lib/swift/android/${${PROJECT_NAME}_ARCH_SUBDIR}"
125+
"$ENV{SDKROOT}/usr/lib/swift")
126+
list(APPEND SwiftCore_NAMES libswiftCore.so)
152127
endif()
153-
154-
set_target_properties(swiftCore PROPERTIES
155-
IMPORTED_LOCATION "${SwiftCore_LIBRARY}"
156-
INTERFACE_INCLUDE_DIRECTORIES "${SwiftCore_INCLUDE_DIR}")
157-
find_package_handle_standard_args(SwiftCore DEFAULT_MSG
158-
SwiftCore_LIBRARY SwiftCore_INCLUDE_DIR)
159128
else()
160129
message(FATAL_ERROR "FindSwiftCore.cmake module search not implemented for targeted platform\n"
161130
" Build Core for your platform and set `SwiftCore_DIR` to"
162131
" the directory containing SwiftCoreConfig.cmake\n")
163132
endif()
133+
134+
find_path(SwiftCore_INCLUDE_DIR
135+
"Swift.swiftmodule"
136+
NO_CMAKE_FIND_ROOT_PATH
137+
HINTS
138+
${SwiftCore_INCLUDE_DIR_HINTS})
139+
find_library(SwiftCore_LIBRARY
140+
NAMES
141+
${SwiftCore_NAMES}
142+
NO_CMAKE_FIND_ROOT_PATH
143+
HINTS
144+
${SwiftCore_LIBRARY_HINTS})
145+
146+
if(SwiftCore_USE_STATIC_LIBS)
147+
add_library(swiftCore STATIC IMPORTED GLOBAL)
148+
else()
149+
add_library(swiftCore SHARED IMPORTED GLOBAL)
150+
endif()
151+
152+
set_target_properties(swiftCore PROPERTIES
153+
INTERFACE_INCLUDE_DIRECTORIES "${SwiftCore_INCLUDE_DIR}")
154+
155+
if(LINUX OR ANDROID)
156+
set_target_properties(swiftCore PROPERTIES
157+
IMPORTED_LOCATION "${SwiftCore_LIBRARY}")
158+
else()
159+
set_target_properties(swiftCore PROPERTIES
160+
IMPORTED_IMPLIB "${SwiftCore_LIBRARY}")
161+
endif()
162+
163+
find_package_handle_standard_args(SwiftCore DEFAULT_MSG
164+
SwiftCore_LIBRARY SwiftCore_INCLUDE_DIR)

Runtimes/Supplemental/cmake/modules/PlatformInfo.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
include_guard(GLOBAL)
2+
13
if(NOT ${PROJECT_NAME}_SIZEOF_POINTER)
24
set(${PROJECT_NAME}_SIZEOF_POINTER "${CMAKE_SIZEOF_VOID_P}" CACHE STRING "Size of a pointer in bytes")
35
message(CONFIGURE_LOG "Stdlib Pointer size: ${CMAKE_SIZEOF_VOID_P}")

0 commit comments

Comments
 (0)