[REACTOS]
[reactos.git] / reactos / 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 else()
115 message(ERROR "Wrong destination: ${dir}")
116 endif()
117 endmacro()
118
119 function(add_cd_file)
120 cmake_parse_arguments(_CD "NO_CAB" "DESTINATION;NAME_ON_CD;TARGET" "FILE;FOR" ${ARGN})
121 if(NOT (_CD_TARGET OR _CD_FILE))
122 message(FATAL_ERROR "You must provide a target or a file to install!")
123 endif()
124
125 if(NOT _CD_DESTINATION)
126 message(FATAL_ERROR "You must provide a destination")
127 elseif(${_CD_DESTINATION} STREQUAL root)
128 set(_CD_DESTINATION "")
129 endif()
130
131 if(NOT _CD_FOR)
132 message(FATAL_ERROR "You must provide a cd name (or "all" for all of them) to install the file on!")
133 endif()
134
135 #get file if we need to
136 if(NOT _CD_FILE)
137 get_target_property(_CD_FILE ${_CD_TARGET} LOCATION_${CMAKE_BUILD_TYPE})
138 endif()
139
140 #do we add it to all CDs?
141 if(_CD_FOR STREQUAL all)
142 set(_CD_FOR "bootcd;livecd;regtest")
143 endif()
144
145 #do we add it to bootcd?
146 list(FIND _CD_FOR bootcd __cd)
147 if(NOT __cd EQUAL -1)
148 #whether or not we should put it in reactos.cab or directly on cd
149 if(_CD_NO_CAB)
150 #directly on cd
151 foreach(item ${_CD_FILE})
152 file(APPEND ${REACTOS_BINARY_DIR}/boot/bootcd.cmake "file(COPY \"${item}\" DESTINATION \"\${CD_DIR}/${_CD_DESTINATION}\")\n")
153 endforeach()
154 if(_CD_NAME_ON_CD)
155 get_filename_component(__file ${_CD_FILE} NAME)
156 #rename it in the cd tree
157 file(APPEND ${REACTOS_BINARY_DIR}/boot/bootcd.cmake "file(RENAME \${CD_DIR}/${_CD_DESTINATION}/${__file} \${CD_DIR}/${_CD_DESTINATION}/${_CD_NAME_ON_CD})\n")
158 endif()
159 if(_CD_TARGET)
160 #manage dependency
161 add_dependencies(bootcd ${_CD_TARGET})
162 endif()
163 else()
164 #add it in reactos.cab
165 dir_to_num(${_CD_DESTINATION} _num)
166 file(RELATIVE_PATH __relative_file ${REACTOS_SOURCE_DIR} ${_CD_FILE})
167 file(APPEND ${REACTOS_BINARY_DIR}/boot/bootdata/packages/reactos.dff.dyn "\"${__relative_file}\" ${_num}\n")
168 unset(__relative_file)
169 if(_CD_TARGET)
170 #manage dependency
171 add_dependencies(reactos_cab ${_CD_TARGET})
172 endif()
173 endif()
174 endif() #end bootcd
175
176 #do we add it to livecd?
177 list(FIND _CD_FOR livecd __cd)
178 if(NOT __cd EQUAL -1)
179 #manage dependency
180 if(_CD_TARGET)
181 add_dependencies(livecd ${_CD_TARGET})
182 endif()
183 foreach(item ${_CD_FILE})
184 file(APPEND ${REACTOS_BINARY_DIR}/boot/livecd.cmake "file(COPY \"${item}\" DESTINATION \"\${CD_DIR}/${_CD_DESTINATION}\")\n")
185 endforeach()
186 if(_CD_NAME_ON_CD)
187 get_filename_component(__file ${_CD_FILE} NAME)
188 #rename it in the cd tree
189 file(APPEND ${REACTOS_BINARY_DIR}/boot/livecd.cmake "file(RENAME \${CD_DIR}/${_CD_DESTINATION}/${__file} \${CD_DIR}/${_CD_DESTINATION}/${_CD_NAME_ON_CD})\n")
190 endif()
191 endif() #end livecd
192
193 #do we add it to regtest?
194 list(FIND _CD_FOR regtest __cd)
195 if(NOT __cd EQUAL -1)
196 #whether or not we should put it in reactos.cab or directly on cd
197 if(_CD_NO_CAB)
198 #directly on cd
199 foreach(item ${_CD_FILE})
200 file(APPEND ${REACTOS_BINARY_DIR}/boot/bootcdregtest.cmake "file(COPY \"${item}\" DESTINATION \"\${CD_DIR}/${_CD_DESTINATION}\")\n")
201 endforeach()
202 if(_CD_NAME_ON_CD)
203 get_filename_component(__file ${_CD_FILE} NAME)
204 #rename it in the cd tree
205 file(APPEND ${REACTOS_BINARY_DIR}/boot/bootcdregtest.cmake "file(RENAME \${CD_DIR}/${_CD_DESTINATION}/${__file} \${CD_DIR}/${_CD_DESTINATION}/${_CD_NAME_ON_CD})\n")
206 endif()
207 if(_CD_TARGET)
208 #manage dependency
209 add_dependencies(bootcdregtest ${_CD_TARGET})
210 endif()
211 else()
212 #add it in reactos.cab
213 #dir_to_num(${_CD_DESTINATION} _num)
214 #file(APPEND ${REACTOS_BINARY_DIR}/boot/bootdata/packages/reactos.dff.dyn "${_CD_FILE} ${_num}\n")
215 #if(_CD_TARGET)
216 # #manage dependency
217 # add_dependencies(reactos_cab ${_CD_TARGET})
218 #endif()
219 endif()
220 endif() #end bootcd
221 endfunction()
222
223 # Create module_clean targets
224 function(add_clean_target _target)
225 set(_clean_working_directory ${CMAKE_CURRENT_BINARY_DIR})
226 if(CMAKE_GENERATOR STREQUAL "Unix Makefiles" OR CMAKE_GENERATOR STREQUAL "MinGW Makefiles")
227 set(_clean_command make clean)
228 elseif(CMAKE_GENERATOR STREQUAL "NMake Makefiles")
229 set(_clean_command nmake /nologo clean)
230 elseif(CMAKE_GENERATOR STREQUAL "Ninja")
231 set(_clean_command ninja -t clean ${_target})
232 set(_clean_working_directory ${REACTOS_BINARY_DIR})
233 endif()
234 add_custom_target(${_target}_clean
235 COMMAND ${_clean_command}
236 WORKING_DIRECTORY ${_clean_working_directory}
237 COMMENT "Cleaning ${_target}")
238 endfunction()
239
240 if(NOT MSVC_IDE)
241 function(add_library name)
242 _add_library(${name} ${ARGN})
243 add_clean_target(${name})
244 endfunction()
245
246 function(add_executable name)
247 _add_executable(${name} ${ARGN})
248 add_clean_target(${name})
249 endfunction()
250 endif()
251
252 if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
253 macro(to_win_path _cmake_path _native_path)
254 string(REPLACE "/" "\\" ${_native_path} "${_cmake_path}")
255 endmacro()
256
257 # yeah the parameter mess sucks, but thats what works...
258 function(concatenate_files _file1 _target2 _output)
259 get_target_property(_file2 ${_target2} LOCATION)
260 to_win_path("${_file1}" _real_file1)
261 to_win_path("${_file2}" _real_file2)
262 to_win_path("${_output}" _real_output)
263 add_custom_command(
264 OUTPUT ${_output}
265 COMMAND cmd.exe /C "copy /Y /B ${_real_file1} + ${_real_file2} ${_real_output} > nul"
266 DEPENDS ${_file1}
267 DEPENDS ${_target2})
268 endfunction()
269 else()
270 macro(concatenate_files _file1 _target2 _output)
271 get_target_property(_file2 ${_target2} LOCATION)
272 add_custom_command(
273 OUTPUT ${_output}
274 COMMAND cat ${_file1} ${_file2} > ${_output}
275 DEPENDS ${_file1}
276 DEPENDS ${_target2})
277 endmacro()
278 endif()
279
280 function(add_importlibs _module)
281 add_dependency_node(${_module})
282 foreach(LIB ${ARGN})
283 if("${LIB}" MATCHES "msvcrt")
284 add_target_compile_definitions(${_module} _DLL __USE_CRTIMP)
285 target_link_libraries(${_module} msvcrtex)
286 endif()
287 target_link_libraries(${_module} lib${LIB})
288 add_dependencies(${_module} lib${LIB})
289 add_dependency_edge(${_module} ${LIB})
290 endforeach()
291 endfunction()
292
293 function(set_module_type MODULE TYPE)
294 cmake_parse_arguments(__module "UNICODE" "IMAGEBASE" "ENTRYPOINT" ${ARGN})
295
296 if(__module_UNPARSED_ARGUMENTS)
297 message(STATUS "set_module_type : unparsed arguments ${__module_UNPARSED_ARGUMENTS}, module : ${MODULE}")
298 endif()
299
300 # Set subsystem. Also take this as an occasion
301 # to error out if someone gave a non existing type
302 if((${TYPE} STREQUAL nativecui) OR (${TYPE} STREQUAL nativedll) OR (${TYPE} STREQUAL kernelmodedriver) OR (${TYPE} STREQUAL wdmdriver))
303 set(__subsystem native)
304 elseif(${TYPE} STREQUAL win32cui)
305 set(__subsystem console)
306 elseif(${TYPE} STREQUAL win32gui)
307 set(__subsystem windows)
308 elseif(NOT ((${TYPE} STREQUAL win32dll) OR (${TYPE} STREQUAL win32ocx)
309 OR (${TYPE} STREQUAL cpl) OR (${TYPE} STREQUAL module)))
310 message(FATAL_ERROR "Unknown type ${TYPE} for module ${MODULE}")
311 endif()
312
313 if(DEFINED __subsystem)
314 set_subsystem(${MODULE} ${__subsystem})
315 endif()
316
317 #set unicode definitions
318 if(__module_UNICODE)
319 add_target_compile_definitions(${MODULE} UNICODE _UNICODE)
320 endif()
321
322 # set entry point
323 if(__module_ENTRYPOINT OR (__module_ENTRYPOINT STREQUAL "0"))
324 list(GET __module_ENTRYPOINT 0 __entrypoint)
325 list(LENGTH __module_ENTRYPOINT __length)
326 if(${__length} EQUAL 2)
327 list(GET __module_ENTRYPOINT 1 __entrystack)
328 elseif(NOT ${__length} EQUAL 1)
329 message(FATAL_ERROR "Wrong arguments for ENTRYPOINT parameter of set_module_type : ${__module_ENTRYPOINT}")
330 endif()
331 unset(__length)
332 elseif(${TYPE} STREQUAL nativecui)
333 set(__entrypoint NtProcessStartup)
334 set(__entrystack 4)
335 elseif((${TYPE} STREQUAL win32gui) OR (${TYPE} STREQUAL win32cui))
336 if(__module_UNICODE)
337 set(__entrypoint wWinMainCRTStartup)
338 else()
339 set(__entrypoint WinMainCRTStartup)
340 endif()
341 elseif((${TYPE} STREQUAL win32dll) OR (${TYPE} STREQUAL win32ocx)
342 OR (${TYPE} STREQUAL cpl))
343 set(__entrypoint DllMainCRTStartup)
344 set(__entrystack 12)
345 elseif((${TYPE} STREQUAL kernelmodedriver) OR (${TYPE} STREQUAL wdmdriver))
346 set(__entrypoint DriverEntry)
347 set(__entrystack 8)
348 elseif(${TYPE} STREQUAL nativedll)
349 set(__entrypoint DllMain)
350 set(__entrystack 12)
351 elseif(${TYPE} STREQUAL module)
352 set(__entrypoint 0)
353 endif()
354
355 if(DEFINED __entrypoint)
356 if(DEFINED __entrystack)
357 set_entrypoint(${MODULE} ${__entrypoint} ${__entrystack})
358 else()
359 set_entrypoint(${MODULE} ${__entrypoint})
360 endif()
361 endif()
362
363 #set base address
364 if(__module_IMAGEBASE)
365 set_image_base(${MODULE} __module_IMAGEBASE)
366 elseif(${TYPE} STREQUAL win32dll)
367 if(DEFINED baseaddress_${MODULE})
368 set_image_base(${MODULE} ${baseaddress_${MODULE}})
369 else()
370 message(STATUS "${MODULE} has no base address")
371 endif()
372 elseif((${TYPE} STREQUAL kernelmodedriver) OR (${TYPE} STREQUAL wdmdriver))
373 set_image_base(${MODULE} 0x00010000)
374 endif()
375
376 # Now do some stuff which is specific to each type
377 if((${TYPE} STREQUAL kernelmodedriver) OR (${TYPE} STREQUAL wdmdriver))
378 add_dependencies(${MODULE} bugcodes)
379 set_target_properties(${MODULE} PROPERTIES SUFFIX ".sys")
380 endif()
381
382 if(${TYPE} STREQUAL win32ocx)
383 set_target_properties(${MODULE} PROPERTIES SUFFIX ".ocx")
384 endif()
385
386 if(${TYPE} STREQUAL cpl)
387 set_target_properties(${MODULE} PROPERTIES SUFFIX ".cpl")
388 endif()
389
390 # do compiler specific stuff
391 set_module_type_toolchain(${MODULE} ${TYPE})
392 endfunction()
393
394 function(preprocess_file __in __out)
395 set(__arg ${__in})
396 foreach(__def in ${ARGN})
397 list(APPEND __arg -D${__def})
398 endforeach()
399 if(MSVC)
400 add_custom_command(OUTPUT ${_out}
401 COMMAND ${CMAKE_C_COMPILER} /EP ${__arg}
402 DEPENDS ${__in})
403 else()
404 add_custom_command(OUTPUT ${_out}
405 COMMAND ${CMAKE_C_COMPILER} -E ${__arg}
406 DEPENDS ${__in})
407 endif()
408 endfunction()
409
410 function(get_includes OUTPUT_VAR)
411 get_directory_property(_includes INCLUDE_DIRECTORIES)
412 foreach(arg ${_includes})
413 list(APPEND __tmp_var -I${arg})
414 endforeach()
415 set(${OUTPUT_VAR} ${__tmp_var} PARENT_SCOPE)
416 endfunction()
417
418 function(get_defines OUTPUT_VAR)
419 get_directory_property(_defines COMPILE_DEFINITIONS)
420 foreach(arg ${_defines})
421 list(APPEND __tmp_var -D${arg})
422 endforeach()
423 set(${OUTPUT_VAR} ${__tmp_var} PARENT_SCOPE)
424 endfunction()
425
426 if(NOT MSVC AND (CMAKE_VERSION VERSION_GREATER 2.8.7))
427 function(add_object_library _target)
428 add_library(${_target} OBJECT ${ARGN})
429 endfunction()
430 else()
431 function(add_object_library _target)
432 add_library(${_target} ${ARGN})
433 endfunction()
434 endif()