Skip to content

Commit 965739e

Browse files
committed
cmake: support static and shared swift libraries
Enhance add_wift_target to support building static libraries. We would always generate shared libraries previously.
1 parent 71942ed commit 965739e

File tree

1 file changed

+52
-16
lines changed

1 file changed

+52
-16
lines changed

cmake/modules/SwiftSupport.cmake

Lines changed: 52 additions & 16 deletions
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;SOURCES;SWIFT_FLAGS)
88

@@ -40,9 +40,28 @@ function(add_swift_target target)
4040
list(APPEND link_flags ${flag})
4141
endforeach()
4242
endif()
43+
if(AST_LIBRARY)
44+
if(AST_STATIC AND AST_SHARED)
45+
message(SEND_ERROR "add_swift_target asked to create library as STATIC and SHARED")
46+
elseif(AST_STATIC OR NOT BUILD_SHARED_LIBS)
47+
set(library_kind STATIC)
48+
elseif(AST_SHARED OR BUILD_SHARED_LIBS)
49+
set(library_kind SHARED)
50+
endif()
51+
else()
52+
if(AST_STATIC OR AST_SHARED)
53+
message(SEND_ERROR "add_swift_target asked to create executable as STATIC or SHARED")
54+
endif()
55+
endif()
4356
if(NOT AST_OUTPUT)
4457
if(AST_LIBRARY)
45-
set(AST_OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${target}.dir/${CMAKE_SHARED_LIBRARY_PREFIX}${target}${CMAKE_SHARED_LIBRARY_SUFFIX})
58+
if(AST_SHARED OR BUILD_SHARED_LIBS)
59+
set(AST_OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${target}.dir/${CMAKE_SHARED_LIBRARY_PREFIX}${target}${CMAKE_SHARED_LIBRARY_SUFFIX})
60+
else()
61+
# NOTE(compnerd) this is a hack for the computation of the
62+
# basename/dirname below for the static path.
63+
set(AST_OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${target}.dir/${target})
64+
endif()
4665
else()
4766
set(AST_OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${target}.dir/${target}${CMAKE_EXECUTABLE_SUFFIX})
4867
endif()
@@ -107,20 +126,37 @@ function(add_swift_target target)
107126
if(AST_LIBRARY)
108127
set(emit_library -emit-library)
109128
endif()
110-
add_custom_command(OUTPUT
111-
${AST_OUTPUT}
112-
DEPENDS
113-
${objs}
114-
COMMAND
115-
${CMAKE_SWIFT_COMPILER} ${emit_library} ${link_flags} -o ${AST_OUTPUT} ${objs}
116-
COMMAND
117-
${CMAKE_COMMAND} -E copy ${AST_OUTPUT} ${CMAKE_CURRENT_BINARY_DIR})
118-
add_custom_target(${target}
119-
ALL
120-
DEPENDS
121-
${AST_OUTPUT}
122-
${module}
123-
${documentation})
129+
if(library_kind STREQUAL SHARED)
130+
add_custom_command(OUTPUT
131+
${AST_OUTPUT}
132+
DEPENDS
133+
${objs}
134+
COMMAND
135+
${CMAKE_SWIFT_COMPILER} ${emit_library} ${link_flags} -o ${AST_OUTPUT} ${objs}
136+
COMMAND
137+
${CMAKE_COMMAND} -E copy ${AST_OUTPUT} ${CMAKE_CURRENT_BINARY_DIR})
138+
add_custom_target(${target}
139+
ALL
140+
DEPENDS
141+
${AST_OUTPUT}
142+
${module}
143+
${documentation})
144+
else()
145+
add_library(${target}-static STATIC ${objs})
146+
get_filename_component(ast_output_bn ${AST_OUTPUT} NAME)
147+
get_filename_component(ast_output_dn ${AST_OUTPUT} DIRECTORY)
148+
set_target_properties(${target}-static
149+
PROPERTIES
150+
LINKER_LANGUAGE C
151+
OUTPUT_DIRECTORY ${ast_output_dn}
152+
OUTPUT_NAME ${ast_output_bn})
153+
add_custom_target(${target}
154+
ALL
155+
DEPENDS
156+
${target}-static
157+
${module}
158+
${documentation})
159+
endif()
124160
endfunction()
125161

126162
function(add_swift_library library)

0 commit comments

Comments
 (0)