Skip to content

Commit 4d4448e

Browse files
committed
build: copy static/shared library support from libdispatch
Update SwiftSupport.cmake from libdispatch for the shared/static library work tht I did there.
1 parent 007f527 commit 4d4448e

File tree

1 file changed

+69
-15
lines changed

1 file changed

+69
-15
lines changed

Diff for: cmake/modules/SwiftSupport.cmake

+69-15
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
include(CMakeParseArguments)
33

44
function(add_swift_target target)
5-
set(options LIBRARY)
5+
set(options LIBRARY;SHARED;STATIC)
66
set(single_value_options MODULE_NAME;MODULE_LINK_NAME;MODULE_PATH;MODULE_CACHE_PATH;OUTPUT;TARGET)
77
set(multiple_value_options CFLAGS;DEPENDS;LINK_FLAGS;RESOURCES;SOURCES;SWIFT_FLAGS)
88

@@ -44,13 +44,35 @@ function(add_swift_target target)
4444
list(APPEND link_flags ${flag})
4545
endforeach()
4646
endif()
47+
if(AST_LIBRARY)
48+
if(AST_STATIC AND AST_SHARED)
49+
message(SEND_ERROR "add_swift_target asked to create library as STATIC and SHARED")
50+
elseif(AST_STATIC OR NOT BUILD_SHARED_LIBS)
51+
set(library_kind STATIC)
52+
elseif(AST_SHARED OR BUILD_SHARED_LIBS)
53+
set(library_kind SHARED)
54+
endif()
55+
else()
56+
if(AST_STATIC OR AST_SHARED)
57+
message(SEND_ERROR "add_swift_target asked to create executable as STATIC or SHARED")
58+
endif()
59+
endif()
4760
if(NOT AST_OUTPUT)
4861
if(AST_LIBRARY)
49-
set(AST_OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${target}.dir/${CMAKE_SHARED_LIBRARY_PREFIX}${target}${CMAKE_SHARED_LIBRARY_SUFFIX})
62+
if(AST_SHARED OR BUILD_SHARED_LIBS)
63+
set(AST_OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${target}.dir/${CMAKE_SHARED_LIBRARY_PREFIX}${target}${CMAKE_SHARED_LIBRARY_SUFFIX})
64+
else()
65+
set(AST_OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${target}.dir/${CMAKE_STATIC_LIBRARY_PREFIX}${target}${CMAKE_STATIC_LIBRARY_SUFFIX})
66+
endif()
5067
else()
5168
set(AST_OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${target}.dir/${target}${CMAKE_EXECUTABLE_SUFFIX})
5269
endif()
5370
endif()
71+
if(CMAKE_SYSTEM_NAME STREQUAL Windows)
72+
if(AST_SHARED OR BUILD_SHARED_LIBS)
73+
set(IMPORT_LIBRARY ${CMAKE_CURRENT_BINARY_DIR}/${target}.dir/${CMAKE_IMPORT_LIBRARY_PREFIX}${target}${CMAKE_IMPORT_LIBRARY_SUFFIX})
74+
endif()
75+
endif()
5476

5577
set(sources)
5678
foreach(source ${AST_SOURCES})
@@ -113,19 +135,40 @@ function(add_swift_target target)
113135
if(AST_LIBRARY)
114136
set(emit_library -emit-library)
115137
endif()
116-
add_custom_command(OUTPUT
117-
${AST_OUTPUT}
118-
DEPENDS
119-
${objs}
120-
${AST_DEPENDS}
121-
COMMAND
122-
${CMAKE_SWIFT_COMPILER} ${emit_library} ${link_flags} -o ${AST_OUTPUT} ${objs})
123-
add_custom_target(${target}
124-
ALL
125-
DEPENDS
126-
${AST_OUTPUT}
127-
${module}
128-
${documentation})
138+
if(NOT AST_LIBRARY OR library_kind STREQUAL SHARED)
139+
add_custom_command(OUTPUT
140+
${AST_OUTPUT}
141+
DEPENDS
142+
${objs}
143+
${AST_DEPENDS}
144+
COMMAND
145+
${CMAKE_SWIFT_COMPILER} ${emit_library} ${link_flags} -o ${AST_OUTPUT} ${objs})
146+
add_custom_target(${target}
147+
ALL
148+
DEPENDS
149+
${AST_OUTPUT}
150+
${module}
151+
${documentation})
152+
else()
153+
add_library(${target}-static STATIC ${objs})
154+
add_dependencies(${target}-static ${AST_DEPENDS})
155+
get_filename_component(ast_output_bn ${AST_OUTPUT} NAME)
156+
string(REGEX REPLACE "^${CMAKE_STATIC_LIBRARY_PREFIX}" "" ast_output_bn ${ast_output_bn})
157+
string(REGEX REPLACE "${CMAKE_STATIC_LIBRARY_SUFFIX}$" "" ast_output_bn ${ast_output_bn})
158+
get_filename_component(ast_output_dn ${AST_OUTPUT} DIRECTORY)
159+
set_target_properties(${target}-static
160+
PROPERTIES
161+
LINKER_LANGUAGE C
162+
ARCHIVE_OUTPUT_DIRECTORY ${ast_output_dn}
163+
OUTPUT_DIRECTORY ${ast_output_dn}
164+
OUTPUT_NAME ${ast_output_bn})
165+
add_custom_target(${target}
166+
ALL
167+
DEPENDS
168+
${target}-static
169+
${module}
170+
${documentation})
171+
endif()
129172

130173
if(AST_RESOURCES)
131174
add_custom_command(TARGET
@@ -150,6 +193,15 @@ function(add_swift_target target)
150193
POST_BUILD
151194
COMMAND
152195
${CMAKE_COMMAND} -E copy ${AST_OUTPUT} ${CMAKE_CURRENT_BINARY_DIR})
196+
if(CMAKE_SYSTEM_NAME STREQUAL Windows)
197+
if(AST_SHARED OR BUILD_SHARED_LIBS)
198+
add_custom_command(TARGET
199+
${target}
200+
POST_BUILD
201+
COMMAND
202+
${CMAKE_COMMAND} -E copy ${IMPORT_LIBRARY} ${CMAKE_CURRENT_BINARY_DIR})
203+
endif()
204+
endif()
153205
endif()
154206
endfunction()
155207

@@ -189,6 +241,8 @@ function(get_swift_host_arch result_var_name)
189241
set("${result_var_name}" "itanium" PARENT_SCOPE)
190242
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86")
191243
set("${result_var_name}" "i686" PARENT_SCOPE)
244+
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "i686")
245+
set("${result_var_name}" "i686" PARENT_SCOPE)
192246
else()
193247
message(FATAL_ERROR "Unrecognized architecture on host system: ${CMAKE_SYSTEM_PROCESSOR}")
194248
endif()

0 commit comments

Comments
 (0)