[LT2013]
[reactos.git] / cmake / CMakeMacros.cmake
1
2 macro(set_cpp)
3 set(IS_CPP 1)
4 if(MSVC)
5 include_directories(${REACTOS_SOURCE_DIR}/include/c++)
6 endif()
7 endmacro()
8
9 function(add_dependency_node _node)
10 if(GENERATE_DEPENDENCY_GRAPH)
11 get_target_property(_type ${_node} TYPE)
12 if(_type MATCHES SHARED_LIBRARY OR ${_node} MATCHES ntoskrnl)
13 file(APPEND ${REACTOS_BINARY_DIR}/dependencies.graphml " <node id=\"${_node}\"/>\n")
14 endif()
15 endif()
16 endfunction()
17
18 function(add_dependency_edge _source _target)
19 if(GENERATE_DEPENDENCY_GRAPH)
20 get_target_property(_type ${_source} TYPE)
21 if(_type MATCHES SHARED_LIBRARY)
22 file(APPEND ${REACTOS_BINARY_DIR}/dependencies.graphml " <edge source=\"${_source}\" target=\"${_target}\"/>\n")
23 endif()
24 endif()
25 endfunction()
26
27 function(add_dependency_header)
28 file(APPEND ${REACTOS_BINARY_DIR}/dependencies.graphml "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<graphml>\n <graph id=\"ReactOS dependencies\" edgedefault=\"directed\">\n")
29 endfunction()
30
31 function(add_dependency_footer)
32 add_dependency_node(ntdll)
33 file(APPEND ${REACTOS_BINARY_DIR}/dependencies.graphml " </graph>\n</graphml>\n")
34 endfunction()
35
36 function(add_message_headers _type)
37 if(${_type} STREQUAL UNICODE)
38 set(_flag "-U")
39 else()
40 set(_flag "-A")
41 endif()
42 foreach(_in_FILE ${ARGN})
43 get_filename_component(FILE ${_in_FILE} NAME_WE)
44 macro_mc(${_flag} ${FILE})
45 add_custom_command(
46 OUTPUT ${REACTOS_BINARY_DIR}/include/reactos/${FILE}.rc ${REACTOS_BINARY_DIR}/include/reactos/${FILE}.h
47 COMMAND ${COMMAND_MC} ${MC_FLAGS}
48 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${FILE}.mc)
49 set_source_files_properties(
50 ${REACTOS_BINARY_DIR}/include/reactos/${FILE}.h ${REACTOS_BINARY_DIR}/include/reactos/${FILE}.rc
51 PROPERTIES GENERATED TRUE)
52 add_custom_target(${FILE} ALL DEPENDS ${REACTOS_BINARY_DIR}/include/reactos/${FILE}.h ${REACTOS_BINARY_DIR}/include/reactos/${FILE}.rc)
53 endforeach()
54 endfunction()
55
56 function(add_link)
57 cmake_parse_arguments(_LINK "MINIMIZE" "NAME;PATH;CMD_LINE_ARGS;ICON;GUID" "" ${ARGN})
58 if(NOT _LINK_NAME OR NOT _LINK_PATH)
59 message(FATAL_ERROR "You must provide name and path")
60 endif()
61
62 if(_LINK_CMD_LINE_ARGS)
63 set(_LINK_CMD_LINE_ARGS -c ${_LINK_CMD_LINE_ARGS})
64 endif()
65
66 if(_LINK_ICON)
67 set(_LINK_ICON -i ${_LINK_ICON})
68 endif()
69
70 if(_LINK_GUID)
71 set(_LINK_GUID -g ${_LINK_GUID})
72 endif()
73
74 if(_LINK_MINIMIZE)
75 set(_LINK_MINIMIZE "-m")
76 endif()
77
78 add_custom_command(
79 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_LINK_NAME}.lnk
80 COMMAND native-mkshelllink -o ${CMAKE_CURRENT_BINARY_DIR}/${_LINK_NAME}.lnk ${_LINK_CMD_LINE_ARGS} ${_LINK_ICON} ${_LINK_GUID} ${_LINK_MINIMIZE} ${_LINK_PATH}
81 DEPENDS native-mkshelllink)
82 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${_LINK_NAME}.lnk PROPERTIES GENERATED TRUE)
83 endfunction()
84
85 macro(dir_to_num dir var)
86 if(${dir} STREQUAL reactos/system32)
87 set(${var} 1)
88 elseif(${dir} STREQUAL reactos/system32/drivers)
89 set(${var} 2)
90 elseif(${dir} STREQUAL reactos/Fonts)
91 set(${var} 3)
92 elseif(${dir} STREQUAL reactos)
93 set(${var} 4)
94 elseif(${dir} STREQUAL reactos/system32/drivers/etc)
95 set(${var} 5)
96 elseif(${dir} STREQUAL reactos/inf)
97 set(${var} 6)
98 elseif(${dir} STREQUAL reactos/bin)
99 set(${var} 7)
100 elseif(${dir} STREQUAL reactos/bin/data)
101 set(${var} 8)
102 elseif(${dir} STREQUAL reactos/media)
103 set(${var} 9)
104 elseif(${dir} STREQUAL reactos/Microsoft.NET)
105 set(${var} 10)
106 elseif(${dir} STREQUAL reactos/Microsoft.NET/Framework)
107 set(${var} 11)
108 elseif(${dir} STREQUAL reactos/Microsoft.NET/Framework/v1.0.3705)
109 set(${var} 12)
110 elseif(${dir} STREQUAL reactos/Microsoft.NET/Framework/v1.1.4322)
111 set(${var} 13)
112 elseif(${dir} STREQUAL reactos/Microsoft.NET/Framework/v2.0.50727)
113 set(${var} 14)
114 elseif(${dir} STREQUAL reactos/Resources)
115 set(${var} 15)
116 elseif(${dir} STREQUAL reactos/Resources/Themes)
117 set(${var} 16)
118 else()
119 message(ERROR "Wrong destination: ${dir}")
120 endif()
121 endmacro()
122
123 function(add_cd_file)
124 cmake_parse_arguments(_CD "NO_CAB" "DESTINATION;NAME_ON_CD;TARGET" "FILE;FOR" ${ARGN})
125 if(NOT (_CD_TARGET OR _CD_FILE))
126 message(FATAL_ERROR "You must provide a target or a file to install!")
127 endif()
128
129 if(NOT _CD_DESTINATION)
130 message(FATAL_ERROR "You must provide a destination")
131 elseif(${_CD_DESTINATION} STREQUAL root)
132 set(_CD_DESTINATION "")
133 endif()
134
135 if(NOT _CD_FOR)
136 message(FATAL_ERROR "You must provide a cd name (or "all" for all of them) to install the file on!")
137 endif()
138
139 #get file if we need to
140 if(NOT _CD_FILE)
141 get_target_property(_CD_FILE ${_CD_TARGET} LOCATION_${CMAKE_BUILD_TYPE})
142 endif()
143
144 #do we add it to all CDs?
145 if(_CD_FOR STREQUAL all)
146 set(_CD_FOR "bootcd;livecd;regtest")
147 endif()
148
149 #do we add it to bootcd?
150 list(FIND _CD_FOR bootcd __cd)
151 if(NOT __cd EQUAL -1)
152 #whether or not we should put it in reactos.cab or directly on cd
153 if(_CD_NO_CAB)
154 #directly on cd
155 foreach(item ${_CD_FILE})
156 file(APPEND ${REACTOS_BINARY_DIR}/boot/bootcd.cmake "file(COPY \"${item}\" DESTINATION \"\${CD_DIR}/${_CD_DESTINATION}\")\n")
157 endforeach()
158 if(_CD_NAME_ON_CD)
159 get_filename_component(__file ${_CD_FILE} NAME)
160 #rename it in the cd tree
161 file(APPEND ${REACTOS_BINARY_DIR}/boot/bootcd.cmake "file(RENAME \${CD_DIR}/${_CD_DESTINATION}/${__file} \${CD_DIR}/${_CD_DESTINATION}/${_CD_NAME_ON_CD})\n")
162 endif()
163 if(_CD_TARGET)
164 #manage dependency
165 add_dependencies(bootcd ${_CD_TARGET})
166 endif()
167 else()
168 #add it in reactos.cab
169 dir_to_num(${_CD_DESTINATION} _num)
170 file(RELATIVE_PATH __relative_file ${REACTOS_SOURCE_DIR} ${_CD_FILE})
171 file(APPEND ${REACTOS_BINARY_DIR}/boot/bootdata/packages/reactos.dff.dyn "\"${__relative_file}\" ${_num}\n")
172 unset(__relative_file)
173 if(_CD_TARGET)
174 #manage dependency
175 add_dependencies(reactos_cab ${_CD_TARGET})
176 endif()
177 endif()
178 endif() #end bootcd
179
180 #do we add it to livecd?
181 list(FIND _CD_FOR livecd __cd)
182 if(NOT __cd EQUAL -1)
183 #manage dependency
184 if(_CD_TARGET)
185 add_dependencies(livecd ${_CD_TARGET})
186 endif()
187 foreach(item ${_CD_FILE})
188 file(APPEND ${REACTOS_BINARY_DIR}/boot/livecd.cmake "file(COPY \"${item}\" DESTINATION \"\${CD_DIR}/${_CD_DESTINATION}\")\n")
189 endforeach()
190 if(_CD_NAME_ON_CD)
191 get_filename_component(__file ${_CD_FILE} NAME)
192 #rename it in the cd tree
193 file(APPEND ${REACTOS_BINARY_DIR}/boot/livecd.cmake "file(RENAME \${CD_DIR}/${_CD_DESTINATION}/${__file} \${CD_DIR}/${_CD_DESTINATION}/${_CD_NAME_ON_CD})\n")
194 endif()
195 endif() #end livecd
196
197 #do we add it to regtest?
198 list(FIND _CD_FOR regtest __cd)
199 if(NOT __cd EQUAL -1)
200 #whether or not we should put it in reactos.cab or directly on cd
201 if(_CD_NO_CAB)
202 #directly on cd
203 foreach(item ${_CD_FILE})
204 file(APPEND ${REACTOS_BINARY_DIR}/boot/bootcdregtest.cmake "file(COPY \"${item}\" DESTINATION \"\${CD_DIR}/${_CD_DESTINATION}\")\n")
205 endforeach()
206 if(_CD_NAME_ON_CD)
207 get_filename_component(__file ${_CD_FILE} NAME)
208 #rename it in the cd tree
209 file(APPEND ${REACTOS_BINARY_DIR}/boot/bootcdregtest.cmake "file(RENAME \${CD_DIR}/${_CD_DESTINATION}/${__file} \${CD_DIR}/${_CD_DESTINATION}/${_CD_NAME_ON_CD})\n")
210 endif()
211 if(_CD_TARGET)
212 #manage dependency
213 add_dependencies(bootcdregtest ${_CD_TARGET})
214 endif()
215 else()
216 #add it in reactos.cab
217 #dir_to_num(${_CD_DESTINATION} _num)
218 #file(APPEND ${REACTOS_BINARY_DIR}/boot/bootdata/packages/reactos.dff.dyn "${_CD_FILE} ${_num}\n")
219 #if(_CD_TARGET)
220 # #manage dependency
221 # add_dependencies(reactos_cab ${_CD_TARGET})
222 #endif()
223 endif()
224 endif() #end bootcd
225 endfunction()
226
227 # Create module_clean targets
228 function(add_clean_target _target)
229 set(_clean_working_directory ${CMAKE_CURRENT_BINARY_DIR})
230 if(CMAKE_GENERATOR STREQUAL "Unix Makefiles" OR CMAKE_GENERATOR STREQUAL "MinGW Makefiles")
231 set(_clean_command make clean)
232 elseif(CMAKE_GENERATOR STREQUAL "NMake Makefiles")
233 set(_clean_command nmake /nologo clean)
234 elseif(CMAKE_GENERATOR STREQUAL "Ninja")
235 set(_clean_command ninja -t clean ${_target})
236 set(_clean_working_directory ${REACTOS_BINARY_DIR})
237 endif()
238 add_custom_target(${_target}_clean
239 COMMAND ${_clean_command}
240 WORKING_DIRECTORY ${_clean_working_directory}
241 COMMENT "Cleaning ${_target}")
242 endfunction()
243
244 if(NOT MSVC_IDE)
245 function(add_library name)
246 _add_library(${name} ${ARGN})
247 add_clean_target(${name})
248 endfunction()
249
250 function(add_executable name)
251 _add_executable(${name} ${ARGN})
252 add_clean_target(${name})
253 endfunction()
254 endif()
255
256 if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
257 macro(to_win_path _cmake_path _native_path)
258 string(REPLACE "/" "\\" ${_native_path} "${_cmake_path}")
259 endmacro()
260
261 # yeah the parameter mess sucks, but thats what works...
262 function(concatenate_files _file1 _target2 _output)
263 get_target_property(_file2 ${_target2} LOCATION)
264 to_win_path("${_file1}" _real_file1)
265 to_win_path("${_file2}" _real_file2)
266 to_win_path("${_output}" _real_output)
267 add_custom_command(
268 OUTPUT ${_output}
269 COMMAND cmd.exe /C "copy /Y /B ${_real_file1} + ${_real_file2} ${_real_output} > nul"
270 DEPENDS ${_file1}
271 DEPENDS ${_target2})
272 endfunction()
273 else()
274 macro(concatenate_files _file1 _target2 _output)
275 get_target_property(_file2 ${_target2} LOCATION)
276 add_custom_command(
277 OUTPUT ${_output}
278 COMMAND cat ${_file1} ${_file2} > ${_output}
279 DEPENDS ${_file1}
280 DEPENDS ${_target2})
281 endmacro()
282 endif()
283
284 function(add_importlibs _module)
285 add_dependency_node(${_module})
286 foreach(LIB ${ARGN})
287 if("${LIB}" MATCHES "msvcrt")
288 add_target_compile_definitions(${_module} _DLL __USE_CRTIMP)
289 target_link_libraries(${_module} msvcrtex)
290 endif()
291 target_link_libraries(${_module} lib${LIB})
292 add_dependencies(${_module} lib${LIB})
293 add_dependency_edge(${_module} ${LIB})
294 endforeach()
295 endfunction()
296
297 function(set_module_type MODULE TYPE)
298 cmake_parse_arguments(__module "UNICODE" "IMAGEBASE" "ENTRYPOINT" ${ARGN})
299
300 if(__module_UNPARSED_ARGUMENTS)
301 message(STATUS "set_module_type : unparsed arguments ${__module_UNPARSED_ARGUMENTS}, module : ${MODULE}")
302 endif()
303
304 # Set subsystem. Also take this as an occasion
305 # to error out if someone gave a non existing type
306 if((${TYPE} STREQUAL nativecui) OR (${TYPE} STREQUAL nativedll) OR (${TYPE} STREQUAL kernelmodedriver))
307 set(__subsystem native)
308 elseif(${TYPE} STREQUAL win32cui)
309 set(__subsystem console)
310 elseif(${TYPE} STREQUAL win32gui)
311 set(__subsystem windows)
312 elseif(NOT ((${TYPE} STREQUAL win32dll) OR (${TYPE} STREQUAL win32ocx)
313 OR (${TYPE} STREQUAL cpl) OR (${TYPE} STREQUAL module)))
314 message(FATAL_ERROR "Unknown type ${TYPE} for module ${MODULE}")
315 endif()
316
317 if(DEFINED __subsystem)
318 set_subsystem(${MODULE} ${__subsystem})
319 endif()
320
321 #set unicode definitions
322 if(__module_UNICODE)
323 add_target_compile_definitions(${MODULE} UNICODE _UNICODE)
324 endif()
325
326 # set entry point
327 if(__module_ENTRYPOINT OR (__module_ENTRYPOINT STREQUAL "0"))
328 list(GET __module_ENTRYPOINT 0 __entrypoint)
329 list(LENGTH __module_ENTRYPOINT __length)
330 if(${__length} EQUAL 2)
331 list(GET __module_ENTRYPOINT 1 __entrystack)
332 elseif(NOT ${__length} EQUAL 1)
333 message(FATAL_ERROR "Wrong arguments for ENTRYPOINT parameter of set_module_type : ${__module_ENTRYPOINT}")
334 endif()
335 unset(__length)
336 elseif(${TYPE} STREQUAL nativecui)
337 set(__entrypoint NtProcessStartup)
338 set(__entrystack 4)
339 elseif((${TYPE} STREQUAL win32gui) OR (${TYPE} STREQUAL win32cui))
340 if(__module_UNICODE)
341 set(__entrypoint wWinMainCRTStartup)
342 else()
343 set(__entrypoint WinMainCRTStartup)
344 endif()
345 elseif((${TYPE} STREQUAL win32dll) OR (${TYPE} STREQUAL win32ocx)
346 OR (${TYPE} STREQUAL cpl))
347 set(__entrypoint DllMainCRTStartup)
348 set(__entrystack 12)
349 elseif(${TYPE} STREQUAL kernelmodedriver)
350 set(__entrypoint DriverEntry)
351 set(__entrystack 8)
352 elseif(${TYPE} STREQUAL nativedll)
353 set(__entrypoint DllMain)
354 set(__entrystack 12)
355 elseif(${TYPE} STREQUAL module)
356 set(__entrypoint 0)
357 endif()
358
359 if(DEFINED __entrypoint)
360 if(DEFINED __entrystack)
361 set_entrypoint(${MODULE} ${__entrypoint} ${__entrystack})
362 else()
363 set_entrypoint(${MODULE} ${__entrypoint})
364 endif()
365 endif()
366
367 #set base address
368 if(__module_IMAGEBASE)
369 set_image_base(${MODULE} __module_IMAGEBASE)
370 elseif(${TYPE} STREQUAL win32dll)
371 if(DEFINED baseaddress_${MODULE})
372 set_image_base(${MODULE} ${baseaddress_${MODULE}})
373 else()
374 message(STATUS "${MODULE} has no base address")
375 endif()
376 elseif(${TYPE} STREQUAL kernelmodedriver)
377 set_image_base(${MODULE} 0x00010000)
378 endif()
379
380 # Now do some stuff which is specific to each type
381 if(${TYPE} STREQUAL kernelmodedriver)
382 add_dependencies(${MODULE} bugcodes)
383 set_target_properties(${MODULE} PROPERTIES SUFFIX ".sys")
384 endif()
385
386 if(${TYPE} STREQUAL win32ocx)
387 set_target_properties(${MODULE} PROPERTIES SUFFIX ".ocx")
388 endif()
389
390 if(${TYPE} STREQUAL cpl)
391 set_target_properties(${MODULE} PROPERTIES SUFFIX ".cpl")
392 endif()
393
394 # do compiler specific stuff
395 set_module_type_toolchain(${MODULE} ${TYPE})
396 endfunction()
397
398 function(preprocess_file __in __out)
399 set(__arg ${__in})
400 foreach(__def in ${ARGN})
401 list(APPEND __arg -D${__def})
402 endforeach()
403 if(MSVC)
404 add_custom_command(OUTPUT ${_out}
405 COMMAND ${CMAKE_C_COMPILER} /EP ${__arg}
406 DEPENDS ${__in})
407 else()
408 add_custom_command(OUTPUT ${_out}
409 COMMAND ${CMAKE_C_COMPILER} -E ${__arg}
410 DEPENDS ${__in})
411 endif()
412 endfunction()
413
414 function(get_includes OUTPUT_VAR)
415 get_directory_property(_includes INCLUDE_DIRECTORIES)
416 foreach(arg ${_includes})
417 list(APPEND __tmp_var -I${arg})
418 endforeach()
419 set(${OUTPUT_VAR} ${__tmp_var} PARENT_SCOPE)
420 endfunction()
421
422 function(get_defines OUTPUT_VAR)
423 get_directory_property(_defines COMPILE_DEFINITIONS)
424 foreach(arg ${_defines})
425 list(APPEND __tmp_var -D${arg})
426 endforeach()
427 set(${OUTPUT_VAR} ${__tmp_var} PARENT_SCOPE)
428 endfunction()