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 )
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} )
14
+ if (AST_TARGET )
15
+ list (APPEND flags -target;${AST_TARGET} )
17
16
endif ()
18
- if (ASL_MODULE_NAME )
19
- list (APPEND flags -module-name;${ASL_MODULE_NAME} )
20
- 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 (NOT AST_OUTPUT )
44
+ if (AST_LIBRARY )
45
+ set (AST_OUTPUT ${CMAKE_CURRENT_BINARY_DIR} /${target}.dir/${CMAKE_SHARED_LIBRARY_PREFIX}${target}${CMAKE_SHARED_LIBRARY_SUFFIX} )
46
+ else ()
47
+ set (AST_OUTPUT ${CMAKE_CURRENT_BINARY_DIR} /${target}.dir/${target}${CMAKE_EXECUTABLE_SUFFIX} )
48
+ endif ()
49
+ endif ()
44
50
45
51
set (sources )
46
- foreach (source ${ASL_SOURCES } )
52
+ foreach (source ${AST_SOURCES } )
47
53
get_filename_component (location ${source} PATH )
48
54
if (IS_ABSOLUTE ${location} )
49
55
list (APPEND sources ${source} )
@@ -52,25 +58,77 @@ function(add_swift_library library)
52
58
endif ()
53
59
endforeach ()
54
60
55
- get_filename_component (module_directory ${ASL_MODULE_PATH} DIRECTORY )
61
+ set (objs )
62
+ set (mods )
63
+ set (docs )
64
+ set (i 0 )
65
+ foreach (source ${sources} )
66
+ get_filename_component (name ${source} NAME )
67
+
68
+ set (obj ${CMAKE_CURRENT_BINARY_DIR} /${target}.dir/${name}${CMAKE_C_OUTPUT_EXTENSION} )
69
+ set (mod ${CMAKE_CURRENT_BINARY_DIR} /${target}.dir/${name}.swiftmodule )
70
+ set (doc ${CMAKE_CURRENT_BINARY_DIR} /${target}.dir/${name}.swiftdoc )
71
+
72
+ set (all_sources ${sources} )
73
+ list (INSERT all_sources ${i} -primary-file )
74
+
75
+ add_custom_command (OUTPUT
76
+ ${obj}
77
+ ${mod}
78
+ ${doc}
79
+ DEPENDS
80
+ ${source}
81
+ COMMAND
82
+ ${CMAKE_SWIFT_COMPILER} -frontend ${flags} -emit-module-path ${mod} -emit-module-doc-path ${doc} -o ${obj} -c ${all_sources} )
83
+
84
+ list (APPEND objs ${obj} )
85
+ list (APPEND mods ${mod} )
86
+ list (APPEND docs ${doc} )
87
+
88
+ math (EXPR i "${i} +1" )
89
+ endforeach ()
90
+
91
+ if (AST_LIBRARY )
92
+ get_filename_component (module_directory ${AST_MODULE_PATH} DIRECTORY )
93
+
94
+ set (module ${AST_MODULE_PATH} )
95
+ set (documentation ${module_directory} /${AST_MODULE_NAME}.swiftdoc )
96
+
97
+ add_custom_command (OUTPUT
98
+ ${module}
99
+ ${documentation}
100
+ DEPENDS
101
+ ${mods}
102
+ ${docs}
103
+ COMMAND
104
+ ${CMAKE_SWIFT_COMPILER} -frontend ${flags} -sil-merge-partial-modules -emit-module ${mods} -o ${module} -emit-module-doc-path ${documentation} )
105
+ endif ()
56
106
107
+ if (AST_LIBRARY )
108
+ set (emit_library -emit-library )
109
+ endif ()
57
110
add_custom_command (OUTPUT
58
- ${ASL_OUTPUT}
59
- ${ASL_MODULE_PATH}
60
- ${module_directory} /${ASL_MODULE_NAME}.swiftdoc
111
+ ${AST_OUTPUT}
61
112
DEPENDS
62
- ${ASL_SOURCES}
63
- ${CMAKE_SWIFT_COMPILER}
64
- ${ASL_DEPENDS}
113
+ ${objs}
65
114
COMMAND
66
- ${CMAKE_COMMAND} -E make_directory ${module_directory }
115
+ ${CMAKE_SWIFT_COMPILER} ${emit_library} ${link_flags} -o ${AST_OUTPUT} ${objs }
67
116
COMMAND
68
- ${CMAKE_SWIFT_COMPILER} ${flags} -c ${sources} -o ${ASL_OUTPUT} )
69
- add_custom_target (${library}
117
+ ${CMAKE_COMMAND} -E copy ${AST_OUTPUT} ${CMAKE_CURRENT_BINARY_DIR} )
118
+ add_custom_target (${target}
119
+ ALL
70
120
DEPENDS
71
- ${ASL_OUTPUT}
72
- ${ASL_MODULE_PATH}
73
- ${module_directory} /${ASL_MODULE_NAME}.swiftdoc )
121
+ ${AST_OUTPUT}
122
+ ${module}
123
+ ${documentation} )
124
+ endfunction ()
125
+
126
+ function (add_swift_library library )
127
+ add_swift_target (${library} LIBRARY ${ARGN} )
128
+ endfunction ()
129
+
130
+ function (add_swift_executable executable )
131
+ add_swift_target (${executable} ${ARGN} )
74
132
endfunction ()
75
133
76
134
# Returns the current achitecture name in a variable
0 commit comments