1
1
2
2
include (CMakeParseArguments )
3
3
4
- function (add_swift_library library )
5
- set (options )
4
+ function (add_swift_target target )
5
+ set (options LIBRARY;SHARED;STATIC )
6
6
set (single_value_options MODULE_NAME;MODULE_LINK_NAME;MODULE_PATH;MODULE_CACHE_PATH;OUTPUT;TARGET )
7
- set (multiple_value_options SOURCES ;SWIFT_FLAGS; CFLAGS;DEPENDS )
7
+ set (multiple_value_options CFLAGS;DEPENDS;LINK_FLAGS;SOURCES;SWIFT_FLAGS )
8
8
9
- cmake_parse_arguments (ASL "${options} " "${single_value_options} " "${multiple_value_options} " ${ARGN} )
9
+ cmake_parse_arguments (AST "${options} " "${single_value_options} " "${multiple_value_options} " ${ARGN} )
10
10
11
11
set (flags ${CMAKE_SWIFT_FLAGS} )
12
+ set (link_flags )
12
13
13
- list (APPEND flags -emit-library )
14
-
15
- if (ASL_TARGET )
16
- list (APPEND FLAGS -target;${ASL_TARGET} )
17
- endif ()
18
- if (ASL_MODULE_NAME )
19
- list (APPEND flags -module-name;${ASL_MODULE_NAME} )
14
+ if (AST_TARGET )
15
+ list (APPEND flags -target;${AST_TARGET} )
20
16
endif ()
21
- if (ASL_MODULE_LINK_NAME )
22
- list (APPEND flags -module-link-name;${ASL_MODULE_LINK_NAME} )
17
+ if (AST_MODULE_NAME )
18
+ list (APPEND flags -module-name;${AST_MODULE_NAME} )
19
+ else ()
20
+ list (APPEND flags -module-name;${target} )
23
21
endif ()
24
- if (ASL_MODULE_PATH )
25
- list (APPEND flags -emit- module-path ;${ASL_MODULE_PATH } )
22
+ if (AST_MODULE_LINK_NAME )
23
+ list (APPEND flags -module-link-name ;${AST_MODULE_LINK_NAME } )
26
24
endif ()
27
- if (ASL_MODULE_CACHE_PATH )
28
- list (APPEND flags -module-cache-path;${ASL_MODULE_CACHE_PATH } )
25
+ if (AST_MODULE_CACHE_PATH )
26
+ list (APPEND flags -module-cache-path;${AST_MODULE_CACHE_PATH } )
29
27
endif ()
30
- if (ASL_SWIFT_FLAGS )
31
- foreach (flag ${ASL_SWIFT_FLAGS } )
28
+ if (AST_SWIFT_FLAGS )
29
+ foreach (flag ${AST_SWIFT_FLAGS } )
32
30
list (APPEND flags ${flag} )
33
31
endforeach ()
34
32
endif ()
35
- if (ASL_CFLAGS )
36
- foreach (flag ${ASL_CFLAGS } )
33
+ if (AST_CFLAGS )
34
+ foreach (flag ${AST_CFLAGS } )
37
35
list (APPEND flags -Xcc;${flag} )
38
36
endforeach ()
39
37
endif ()
40
-
41
- # FIXME: We shouldn't /have/ to build things in a single process.
42
- # <rdar://problem/15972329>
43
- list (APPEND flags -force-single-frontend-invocation )
38
+ if (AST_LINK_FLAGS )
39
+ foreach (flag ${AST_LINK_FLAGS} )
40
+ list (APPEND link_flags ${flag} )
41
+ endforeach ()
42
+ 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 ()
56
+ if (NOT AST_OUTPUT )
57
+ if (AST_LIBRARY )
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 ()
65
+ else ()
66
+ set (AST_OUTPUT ${CMAKE_CURRENT_BINARY_DIR} /${target}.dir/${target}${CMAKE_EXECUTABLE_SUFFIX} )
67
+ endif ()
68
+ endif ()
44
69
45
70
set (sources )
46
- foreach (source ${ASL_SOURCES } )
71
+ foreach (source ${AST_SOURCES } )
47
72
get_filename_component (location ${source} PATH )
48
73
if (IS_ABSOLUTE ${location} )
49
74
list (APPEND sources ${source} )
@@ -52,25 +77,97 @@ function(add_swift_library library)
52
77
endif ()
53
78
endforeach ()
54
79
55
- get_filename_component (module_directory ${ASL_MODULE_PATH} DIRECTORY )
56
-
57
- add_custom_command (OUTPUT
58
- ${ASL_OUTPUT}
59
- ${ASL_MODULE_PATH}
60
- ${module_directory} /${ASL_MODULE_NAME}.swiftdoc
61
- DEPENDS
62
- ${ASL_SOURCES}
63
- ${CMAKE_SWIFT_COMPILER}
64
- ${ASL_DEPENDS}
65
- COMMAND
66
- ${CMAKE_COMMAND} -E make_directory ${module_directory}
67
- COMMAND
68
- ${CMAKE_SWIFT_COMPILER} ${flags} -c ${sources} -o ${ASL_OUTPUT} )
69
- add_custom_target (${library}
70
- DEPENDS
71
- ${ASL_OUTPUT}
72
- ${ASL_MODULE_PATH}
73
- ${module_directory} /${ASL_MODULE_NAME}.swiftdoc )
80
+ set (objs )
81
+ set (mods )
82
+ set (docs )
83
+ set (i 0 )
84
+ foreach (source ${sources} )
85
+ get_filename_component (name ${source} NAME )
86
+
87
+ set (obj ${CMAKE_CURRENT_BINARY_DIR} /${target}.dir/${name}${CMAKE_C_OUTPUT_EXTENSION} )
88
+ set (mod ${CMAKE_CURRENT_BINARY_DIR} /${target}.dir/${name}.swiftmodule )
89
+ set (doc ${CMAKE_CURRENT_BINARY_DIR} /${target}.dir/${name}.swiftdoc )
90
+
91
+ set (all_sources ${sources} )
92
+ list (INSERT all_sources ${i} -primary-file )
93
+
94
+ add_custom_command (OUTPUT
95
+ ${obj}
96
+ ${mod}
97
+ ${doc}
98
+ DEPENDS
99
+ ${source}
100
+ ${AST_DEPENDS}
101
+ COMMAND
102
+ ${CMAKE_SWIFT_COMPILER} -frontend ${flags} -emit-module-path ${mod} -emit-module-doc-path ${doc} -o ${obj} -c ${all_sources} )
103
+
104
+ list (APPEND objs ${obj} )
105
+ list (APPEND mods ${mod} )
106
+ list (APPEND docs ${doc} )
107
+
108
+ math (EXPR i "${i} +1" )
109
+ endforeach ()
110
+
111
+ if (AST_LIBRARY )
112
+ get_filename_component (module_directory ${AST_MODULE_PATH} DIRECTORY )
113
+
114
+ set (module ${AST_MODULE_PATH} )
115
+ set (documentation ${module_directory} /${AST_MODULE_NAME}.swiftdoc )
116
+
117
+ add_custom_command (OUTPUT
118
+ ${module}
119
+ ${documentation}
120
+ DEPENDS
121
+ ${mods}
122
+ ${docs}
123
+ ${AST_DEPENDS}
124
+ COMMAND
125
+ ${CMAKE_SWIFT_COMPILER} -frontend ${flags} -sil-merge-partial-modules -emit-module ${mods} -o ${module} -emit-module-doc-path ${documentation} )
126
+ endif ()
127
+
128
+ if (AST_LIBRARY )
129
+ set (emit_library -emit-library )
130
+ endif ()
131
+ if (library_kind STREQUAL SHARED )
132
+ add_custom_command (OUTPUT
133
+ ${AST_OUTPUT}
134
+ DEPENDS
135
+ ${objs}
136
+ ${AST_DEPENDS}
137
+ COMMAND
138
+ ${CMAKE_SWIFT_COMPILER} ${emit_library} ${link_flags} -o ${AST_OUTPUT} ${objs}
139
+ COMMAND
140
+ ${CMAKE_COMMAND} -E copy ${AST_OUTPUT} ${CMAKE_CURRENT_BINARY_DIR} )
141
+ add_custom_target (${target}
142
+ ALL
143
+ DEPENDS
144
+ ${AST_OUTPUT}
145
+ ${module}
146
+ ${documentation} )
147
+ else ()
148
+ add_library (${target} -static STATIC ${objs} )
149
+ get_filename_component (ast_output_bn ${AST_OUTPUT} NAME )
150
+ get_filename_component (ast_output_dn ${AST_OUTPUT} DIRECTORY )
151
+ set_target_properties (${target} -static
152
+ PROPERTIES
153
+ LINKER_LANGUAGE C
154
+ OUTPUT_DIRECTORY ${ast_output_dn}
155
+ OUTPUT_NAME ${ast_output_bn} )
156
+ add_custom_target (${target}
157
+ ALL
158
+ DEPENDS
159
+ ${target} -static
160
+ ${module}
161
+ ${documentation} )
162
+ endif ()
163
+ endfunction ()
164
+
165
+ function (add_swift_library library )
166
+ add_swift_target (${library} LIBRARY ${ARGN} )
167
+ endfunction ()
168
+
169
+ function (add_swift_executable executable )
170
+ add_swift_target (${executable} ${ARGN} )
74
171
endfunction ()
75
172
76
173
# Returns the current achitecture name in a variable
0 commit comments