-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfastbuild.cmake
278 lines (262 loc) · 7.77 KB
/
fastbuild.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
set(LOG "")
set(configTypes "DEBUG;RELEASE;RELWITHDEBINFO;MINSIZEREL")
set(anyConfigTypes ";_DEBUG;_RELEASE;_RELWITHDEBINFO;_MINSIZEREL")
macro(dbgf variable)
set(LOG "${LOG}${variable}: ${${variable}}\n")
endmacro()
function(get_libraries out target)
get_target_property(libs ${TARGET} LINK_LIBRARIES)
set(ret)
foreach(lib ${libs})
if(TARGET ${lib})
foreach(configType ${anyConfigTypes})
set(name "IMPORTED_IMPLIB${configType}")
get_target_property(path ${lib} ${name})
if(path)
break()
endif()
endforeach()
list(APPEND ret " ${path}")
else()
list(APPEND ret " -l${lib}")
endif()
endforeach()
set(${out} ${ret} PARENT_SCOPE)
endfunction()
function(toFastbuildArray listVar out)
list(GET listVar 0 elem)
set(ret "{\"${elem}\"")
list(LENGTH listVar size)
math(EXPR size "${size}-1")
if(${size} GREATER 0)
foreach(i RANGE 1 ${size})
LIST(GET listVar ${i} elem)
#message(${elem})
set(ret "${ret}, \"${elem}\"")
endforeach()
endif()
set(ret "${ret}}")
set(${out} ${ret} PARENT_SCOPE)
endfunction()
function(DBG VAR)
message("${VAR}: ${${VAR}}")
endfunction()
macro(get_compile_flags target flags)
set(flags "")
foreach(configType ${configTypes})
string(TOUPPER "CMAKE_CXX_FLAGS_${configType}" name)
set(flags "${flags}$<$<CONFIG:${configType}>:${name}> ")
endforeach()
dbgf(flags)
set(flags "${${name}} ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_COMPILER_ARG1}")
get_target_property(value ${target} COMPILE_FLAGS)
if (value)
list(APPEND flags ${value})
endif()
get_target_property(value ${target} COMPILE_DEFINITIONS)
if (value)
foreach(item ${value})
list(APPEND flags "-D${item}")
endforeach()
endif()
get_directory_property(value COMPILE_DEFINITIONS)
if (value)
list(APPEND flags ${value})
endif()
get_directory_property(value INCLUDE_DIRECTORIES)
if (value)
foreach(item ${value})
list(APPEND flags "-I${item}")
endforeach()
endif()
get_target_property(value ${target} INCLUDE_DIRECTORIES)
if (value)
foreach(item ${value})
list(APPEND flags "-I${item}")
endforeach()
endif()
get_target_property(value ${target} COMPILE_OPTIONS)
if (value)
foreach(item ${value})
list(APPEND flags "${item}")
endforeach()
endif()
get_target_property(value ${target} CMAKE_CXX_STANDARD)
if (value AND CMAKE_COMPILER_IS_GNUCC)
list(APPEND flags "-std=c++${item}")
elseif(${CMAKE_CXX_STANDARD} AND CMAKE_COMPILER_IS_GNUCC)
#message("appending")
list(APPEND flags "-std=c++${CMAKE_CXX_STANDARD}")
endif()
endmacro()
function(add_source_file SOURCE out name)
get_source_file_property(HEADER_FILE_ONLY ${SOURCE} HEADER_FILE_ONLY)
if(NOT HEADER_FILE_ONLY)
set(srcflags "")
get_source_file_property(LOCATION ${SOURCE} LOCATION)
set(objectName ${LOCATION})
string(REPLACE "/" "-" objectName "${objectName}")
string(REPLACE "\\" "-" objectName "${objectName}")
string(REPLACE "." "-" objectName "${objectName}")
string(REPLACE ":" "-" objectName "${objectName}")
set(${name} ${objectName} PARENT_SCOPE)
get_source_file_property(value ${SOURCE} COMPILE_DEFINITIONS)
if (value)
foreach(item ${value})
list(APPEND srcflags "-D${item}")
endforeach()
endif()
get_source_file_property(value ${SOURCE} COMPILE_FLAGS)
if (value)
foreach(item ${value})
list(APPEND srcflags "${item}")
endforeach()
endif()
get_source_file_property(value ${SOURCE} COMPILE_OPTIONS)
if (value)
foreach(item ${value})
list(APPEND srcflags "${item}")
endforeach()
endif()
get_source_file_property(value ${SOURCE} INCLUDE_DIRECTORIES)
if (value)
foreach(item ${value})
list(APPEND srcflags "-I${item}")
endforeach()
endif()
string(REPLACE ";" " " srcflags "${srcflags}")
set(outputOpt "/Fo")
if(CMAKE_COMPILER_IS_GNUCC)
set(outputOpt "-o")
endif()
set(ret2 "
ObjectList( '${objectName}' ) {
.CompilerInputFiles = {'${LOCATION}'}
.CompilerOutputPath = '/out/'
.CompilerOptions + ' %1 -c ${outputOpt}\"%2\" ${srcflags}'
}")
set(${out} "${${out}}${ret2}" PARENT_SCOPE)
endif()
endfunction()
macro(map_type wtf out)
dbg(wtf)
if(${type} STREQUAL "STATIC_LIBRARY")
set(out "STATIC")
endif()
if(${type} STREQUAL "MODULE_LIBRARY")
set(out "MODULE")
endif()
if(${type} STREQUAL "SHARED_LIBRARY")
set(out "SHARED")
endif()
if(${type} STREQUAL "INTERFACE_LIBRARY")
set(out "what")
endif()
if(${type} STREQUAL "EXECUTABLE")
set(out "EXE")
endif()
endmacro()
function(add_fastbuild_target out TARGET)
get_target_property(TYPE ${TARGET} TYPE)
get_target_property(LINK_FLAGS ${TARGET} LINK_FLAGS)
if(NOT LINK_FLAGS)
set(LINK_FLAGS "")
endif()
get_target_property(NAME ${TARGET} NAME )
get_target_property(SOURCES ${TARGET} SOURCES)
get_compile_flags(${TARGET} flags)
if(TYPE STREQUAL "SHARED_LIBRARY")
list(APPEND flags "-shared")
endif()
get_libraries(IncludeLibs target)
string(REPLACE ";" " " IncludeLibs "${IncludeLibs}")
string(REPLACE ";" " " flags "${flags}")
set(ret "\n.CompilerOptions = '${flags}'\n")
set(libraries "")
foreach(i ${SOURCES})
add_source_file(${i} ret srcName)
list(APPEND libraries "${srcName}")
endforeach()
toFastbuildArray("${libraries}" libraries)
if(TYPE STREQUAL "EXECUTABLE" OR TYPE STREQUAL "SHARED_LIBRARY")
set(outputOpt "/OUT:")
if(CMAKE_COMPILER_IS_GNUCC)
set(outputOpt "-o")
endif()
if(${TYPE} STREQUAL "STATIC_LIBRARY")
set(mappedType "STATIC")
endif()
if(${TYPE} STREQUAL "MODULE_LIBRARY")
set(mappedType "MODULE")
endif()
if(${TYPE} STREQUAL "SHARED_LIBRARY")
set(mappedType "SHARED")
endif()
if(${TYPE} STREQUAL "INTERFACE_LIBRARY")
set(mappedType "what")
endif()
if(${TYPE} STREQUAL "EXECUTABLE")
set(mappedType "EXE")
endif()
dbg(mappedType)
set(CMAKE_LINKER_FLAGS "CMAKE_${mappedType}_LINKER_FLAGS")
dbg(${CMAKE_LINKER_FLAGS})
set(LINK_FLAGS "${LINK_FLAGS} ${${CMAKE_LINKER_FLAGS}} ")
foreach(configType ${configTypes})
set(name "${CMAKE_LINKER_FLAGS}_${configType}")
dbg(${name})
set(LINK_FLAGS "${LINK_FLAGS}$<$<CONFIG:${configType}>:${${name}}>")
endforeach()
set(LINK_FLAGS "${LINK_FLAGS} ")
foreach(configType ${configTypes})
set(name "LINK_FLAGS_${configType}")
get_target_property(${name} ${TARGET} ${name})
#dbg(${name})
if(${name})
set(LINK_FLAGS "${LINK_FLAGS}$<$<CONFIG:${configType}>:${${name}}>")
endif()
endforeach()
set(ret "${ret}
Executable( '${NAME}' ) {
.Libraries = ${libraries}
.LinkerOutput = '$<TARGET_FILE:${TARGET}>'
.LinkerOptions = '%1'
+ ' ${outputOpt}\"%2\"'
+ '${IncludeLibs}'
+ '${LINK_FLAGS}'
}
"
)
endif()
set(${out} "${${out}}${ret}" PARENT_SCOPE)
endfunction()
function(init_fastbuild out)
set(linker ${CMAKE_LINKER})
if(CMAKE_COMPILER_IS_GNUCC)
set(linker ${CMAKE_CXX_COMPILER})
endif()
set(ret ".Compiler = '${CMAKE_CXX_COMPILER}'\n.Linker = '${linker}'\n")
set(${out} "${ret}" PARENT_SCOPE)
endfunction()
function(alias_all_fastbuild out TARGETS)
#DBG(TARGETS)
set(ret)
foreach(i ${TARGETS})
get_target_property(NAME ${i} NAME )
list(APPEND ret ${NAME})
endforeach()
#DBG(ret)
toFastbuildArray("${ret}" ret)
set(ret "\nAlias( 'all' ) {\n .Targets = ${ret}\n}")
set(${out} "${${out}}${ret}" PARENT_SCOPE)
endfunction()
function(generate_fastbuild TARGETS)
set(fastbuildfile "")
init_fastbuild(fastbuildfile)
foreach(i ${TARGETS})
add_fastbuild_target(fastbuildfile ${i})
endforeach()
alias_all_fastbuild(fastbuildfile "${TARGETS}")
file(GENERATE OUTPUT "fbuild$<CONFIG>.bff" CONTENT ${fastbuildfile})
file(GENERATE OUTPUT "log$<CONFIG>.txt" CONTENT "${LOG}")
endfunction()