[REACTOS]: Do not gather all the generated message resource files in the same directo...
[reactos.git] / reactos / sdk / cmake / CMakeMacros.cmake
1
2 # set_cpp
3 # Marks the current folder as containing C++ modules, additionally enabling
4 # specific C++ language features as specified (all of these default to off):
5 #
6 # WITH_RUNTIME
7 # Links with the C++ runtime. Enable this for modules which use new/delete or
8 # RTTI, but do not require STL. This is the right choice if you see undefined
9 # references to operator new/delete, vector constructor/destructor iterator,
10 # type_info::vtable, ...
11 # Note: this only affects linking, so cannot be used for static libraries.
12 # WITH_RTTI
13 # Enables run-time type information. Enable this if the module uses typeid or
14 # dynamic_cast. You will probably need to enable WITH_RUNTIME as well, if
15 # you're not already using STL.
16 # WITH_EXCEPTIONS
17 # Enables C++ exception handling. Enable this if the module uses try/catch or
18 # throw. You might also need this if you use a standard operator new (the one
19 # without nothrow).
20 # WITH_STL
21 # Enables standard C++ headers and links to the Standard Template Library.
22 # Use this for modules using anything from the std:: namespace, e.g. maps,
23 # strings, vectors, etc.
24 # Note: this affects both compiling (via include directories) and
25 # linking (by adding STL). Implies WITH_RUNTIME.
26 # FIXME: WITH_STL is currently also required for runtime headers such as
27 # <new> and <exception>. This is not a big issue because in stl-less
28 # environments you usually don't want those anyway; but we might want
29 # to have modules like this in the future.
30 #
31 # Examples:
32 # set_cpp()
33 # Enables the C++ language, but will cause errors if any runtime or standard
34 # library features are used. This should be the default for C++ in kernel
35 # mode or otherwise restricted environments.
36 # Note: this is required to get libgcc (for multiplication/division) linked
37 # in for C++ modules, and to set the correct language for precompiled
38 # header files, so it IS required even with no features specified.
39 # set_cpp(WITH_RUNTIME)
40 # Links with the C++ runtime, so that e.g. custom operator new implementations
41 # can be used in a restricted environment. This is also required for linking
42 # with libraries (such as ATL) which have RTTI enabled, even if the module in
43 # question does not use WITH_RTTI.
44 # set_cpp(WITH_RTTI WITH_EXCEPTIONS WITH_STL)
45 # The full package. This will adjust compiler and linker so that all C++
46 # features can be used.
47 macro(set_cpp)
48 cmake_parse_arguments(__cppopts "WITH_RUNTIME;WITH_RTTI;WITH_EXCEPTIONS;WITH_STL" "" "" ${ARGN})
49 if(__cppopts_UNPARSED_ARGUMENTS)
50 message(FATAL_ERROR "set_cpp: unparsed arguments ${__cppopts_UNPARSED_ARGUMENTS}")
51 endif()
52
53 if(__cppopts_WITH_RUNTIME)
54 set(CPP_USE_RT 1)
55 endif()
56 if(__cppopts_WITH_RTTI)
57 if(MSVC)
58 replace_compile_flags("/GR-" "/GR")
59 else()
60 replace_compile_flags_language("-fno-rtti" "-frtti" "CXX")
61 endif()
62 endif()
63 if(__cppopts_WITH_EXCEPTIONS)
64 if(MSVC)
65 replace_compile_flags("/EHs-c-" "/EHsc")
66 else()
67 replace_compile_flags_language("-fno-exceptions" "-fexceptions" "CXX")
68 endif()
69 endif()
70 if(__cppopts_WITH_STL)
71 set(CPP_USE_STL 1)
72 if(MSVC)
73 add_definitions(-DNATIVE_CPP_INCLUDE=${REACTOS_SOURCE_DIR}/sdk/include/c++)
74 include_directories(${REACTOS_SOURCE_DIR}/sdk/include/c++/stlport)
75 else()
76 replace_compile_flags("-nostdinc" " ")
77 endif()
78 endif()
79
80 set(IS_CPP 1)
81 endmacro()
82
83 function(add_dependency_node _node)
84 if(GENERATE_DEPENDENCY_GRAPH)
85 get_target_property(_type ${_node} TYPE)
86 if(_type MATCHES SHARED_LIBRARY OR ${_node} MATCHES ntoskrnl)
87 file(APPEND ${REACTOS_BINARY_DIR}/dependencies.graphml " <node id=\"${_node}\"/>\n")
88 endif()
89 endif()
90 endfunction()
91
92 function(add_dependency_edge _source _target)
93 if(GENERATE_DEPENDENCY_GRAPH)
94 get_target_property(_type ${_source} TYPE)
95 if(_type MATCHES SHARED_LIBRARY)
96 file(APPEND ${REACTOS_BINARY_DIR}/dependencies.graphml " <edge source=\"${_source}\" target=\"${_target}\"/>\n")
97 endif()
98 endif()
99 endfunction()
100
101 function(add_dependency_header)
102 file(APPEND ${REACTOS_BINARY_DIR}/dependencies.graphml "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<graphml>\n <graph id=\"ReactOS dependencies\" edgedefault=\"directed\">\n")
103 endfunction()
104
105 function(add_dependency_footer)
106 add_dependency_node(ntdll)
107 file(APPEND ${REACTOS_BINARY_DIR}/dependencies.graphml " </graph>\n</graphml>\n")
108 endfunction()
109
110 function(add_message_headers _type)
111 if(${_type} STREQUAL UNICODE)
112 set(_flag "-U")
113 else()
114 set(_flag "-A")
115 endif()
116 foreach(_file ${ARGN})
117 get_filename_component(_file_name ${_file} NAME_WE)
118 set(_converted_file ${CMAKE_CURRENT_BINARY_DIR}/${_file}) ## ${_file_name}.mc
119 set(_source_file ${CMAKE_CURRENT_SOURCE_DIR}/${_file}) ## ${_file_name}.mc
120 add_custom_command(
121 OUTPUT "${_converted_file}"
122 COMMAND native-utf16le "${_source_file}" "${_converted_file}" nobom
123 DEPENDS native-utf16le "${_source_file}")
124 macro_mc(${_flag} ${_converted_file})
125 add_custom_command(
126 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_file_name}.h ${CMAKE_CURRENT_BINARY_DIR}/${_file_name}.rc
127 COMMAND ${COMMAND_MC}
128 DEPENDS "${_converted_file}")
129 set_source_files_properties(
130 ${CMAKE_CURRENT_BINARY_DIR}/${_file_name}.h ${CMAKE_CURRENT_BINARY_DIR}/${_file_name}.rc
131 PROPERTIES GENERATED TRUE)
132 add_custom_target(${_file_name} ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${_file_name}.h ${CMAKE_CURRENT_BINARY_DIR}/${_file_name}.rc)
133 endforeach()
134 endfunction()
135
136 function(add_link)
137 cmake_parse_arguments(_LINK "MINIMIZE" "NAME;PATH;CMD_LINE_ARGS;ICON;GUID" "" ${ARGN})
138 if(NOT _LINK_NAME OR NOT _LINK_PATH)
139 message(FATAL_ERROR "You must provide name and path")
140 endif()
141
142 if(_LINK_CMD_LINE_ARGS)
143 set(_LINK_CMD_LINE_ARGS -c ${_LINK_CMD_LINE_ARGS})
144 endif()
145
146 if(_LINK_ICON)
147 set(_LINK_ICON -i ${_LINK_ICON})
148 endif()
149
150 if(_LINK_GUID)
151 set(_LINK_GUID -g ${_LINK_GUID})
152 endif()
153
154 if(_LINK_MINIMIZE)
155 set(_LINK_MINIMIZE "-m")
156 endif()
157
158 add_custom_command(
159 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_LINK_NAME}.lnk
160 COMMAND native-mkshelllink -o ${CMAKE_CURRENT_BINARY_DIR}/${_LINK_NAME}.lnk ${_LINK_CMD_LINE_ARGS} ${_LINK_ICON} ${_LINK_GUID} ${_LINK_MINIMIZE} ${_LINK_PATH}
161 DEPENDS native-mkshelllink)
162 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${_LINK_NAME}.lnk PROPERTIES GENERATED TRUE)
163 endfunction()
164
165 macro(dir_to_num dir var)
166 if(${dir} STREQUAL reactos/system32)
167 set(${var} 1)
168 elseif(${dir} STREQUAL reactos/system32/drivers)
169 set(${var} 2)
170 elseif(${dir} STREQUAL reactos/Fonts)
171 set(${var} 3)
172 elseif(${dir} STREQUAL reactos)
173 set(${var} 4)
174 elseif(${dir} STREQUAL reactos/system32/drivers/etc)
175 set(${var} 5)
176 elseif(${dir} STREQUAL reactos/inf)
177 set(${var} 6)
178 elseif(${dir} STREQUAL reactos/bin)
179 set(${var} 7)
180 elseif(${dir} STREQUAL reactos/bin/testdata)
181 set(${var} 8)
182 elseif(${dir} STREQUAL reactos/media)
183 set(${var} 9)
184 elseif(${dir} STREQUAL reactos/Microsoft.NET)
185 set(${var} 10)
186 elseif(${dir} STREQUAL reactos/Microsoft.NET/Framework)
187 set(${var} 11)
188 elseif(${dir} STREQUAL reactos/Microsoft.NET/Framework/v1.0.3705)
189 set(${var} 12)
190 elseif(${dir} STREQUAL reactos/Microsoft.NET/Framework/v1.1.4322)
191 set(${var} 13)
192 elseif(${dir} STREQUAL reactos/Microsoft.NET/Framework/v2.0.50727)
193 set(${var} 14)
194 elseif(${dir} STREQUAL reactos/Resources)
195 set(${var} 15)
196 elseif(${dir} STREQUAL reactos/Resources/Themes)
197 set(${var} 16)
198 elseif(${dir} STREQUAL reactos/system32/wbem)
199 set(${var} 17)
200 elseif(${dir} STREQUAL reactos/Resources/Themes/Lautus)
201 set(${var} 18)
202 elseif(${dir} STREQUAL reactos/Help)
203 set(${var} 19)
204 elseif(${dir} STREQUAL reactos/Config)
205 set(${var} 20)
206 elseif(${dir} STREQUAL reactos/Cursors)
207 set(${var} 21)
208 elseif(${dir} STREQUAL reactos/system32/ShellExt)
209 set(${var} 22)
210 elseif(${dir} STREQUAL reactos/Temp)
211 set(${var} 23)
212 elseif(${dir} STREQUAL reactos/system32/spool)
213 set(${var} 24)
214 elseif(${dir} STREQUAL reactos/system32/spool/drivers)
215 set(${var} 25)
216 elseif(${dir} STREQUAL reactos/system32/spool/drivers/color)
217 set(${var} 26)
218 elseif(${dir} STREQUAL reactos/system32/spool/drivers/w32x86)
219 set(${var} 27)
220 elseif(${dir} STREQUAL reactos/system32/spool/drivers/w32x86/3)
221 set(${var} 28)
222 elseif(${dir} STREQUAL reactos/system32/spool/prtprocs)
223 set(${var} 29)
224 elseif(${dir} STREQUAL reactos/system32/spool/prtprocs/w32x86)
225 set(${var} 30)
226 elseif(${dir} STREQUAL reactos/system32/spool/PRINTERS)
227 set(${var} 31)
228 elseif(${dir} STREQUAL reactos/system32/wbem/Repository)
229 set(${var} 32)
230 elseif(${dir} STREQUAL reactos/system32/wbem/Repository/FS)
231 set(${var} 33)
232 elseif(${dir} STREQUAL reactos/system32/wbem/mof/good)
233 set(${var} 34)
234 elseif(${dir} STREQUAL reactos/system32/wbem/mof/bad)
235 set(${var} 35)
236 elseif(${dir} STREQUAL reactos/system32/wbem/AdStatus)
237 set(${var} 36)
238 elseif(${dir} STREQUAL reactos/system32/wbem/xml)
239 set(${var} 37)
240 elseif(${dir} STREQUAL reactos/system32/wbem/Logs)
241 set(${var} 38)
242 elseif(${dir} STREQUAL reactos/system32/wbem/AutoRecover)
243 set(${var} 39)
244 elseif(${dir} STREQUAL reactos/system32/wbem/snmp)
245 set(${var} 40)
246 elseif(${dir} STREQUAL reactos/system32/wbem/Performance)
247 set(${var} 41)
248 elseif(${dir} STREQUAL reactos/twain_32)
249 set(${var} 42)
250 elseif(${dir} STREQUAL reactos/repair)
251 set(${var} 43)
252 elseif(${dir} STREQUAL reactos/Web)
253 set(${var} 44)
254 elseif(${dir} STREQUAL reactos/Web/Wallpaper)
255 set(${var} 45)
256 elseif(${dir} STREQUAL reactos/Prefetch)
257 set(${var} 46)
258 elseif(${dir} STREQUAL reactos/security)
259 set(${var} 47)
260 elseif(${dir} STREQUAL reactos/security/Database)
261 set(${var} 48)
262 elseif(${dir} STREQUAL reactos/security/logs)
263 set(${var} 49)
264 elseif(${dir} STREQUAL reactos/security/templates)
265 set(${var} 50)
266 elseif(${dir} STREQUAL reactos/system32/CatRoot)
267 set(${var} 51)
268 elseif(${dir} STREQUAL reactos/system32/CatRoot2)
269 set(${var} 52)
270 elseif(${dir} STREQUAL reactos/AppPatch)
271 set(${var} 53)
272 else()
273 message(FATAL_ERROR "Wrong destination: ${dir}")
274 endif()
275 endmacro()
276
277 function(add_cd_file)
278 cmake_parse_arguments(_CD "NO_CAB;NOT_IN_HYBRIDCD" "DESTINATION;NAME_ON_CD;TARGET" "FILE;FOR" ${ARGN})
279 if(NOT (_CD_TARGET OR _CD_FILE))
280 message(FATAL_ERROR "You must provide a target or a file to install!")
281 endif()
282
283 if(NOT _CD_DESTINATION)
284 message(FATAL_ERROR "You must provide a destination")
285 elseif(${_CD_DESTINATION} STREQUAL root)
286 set(_CD_DESTINATION "")
287 endif()
288
289 if(NOT _CD_FOR)
290 message(FATAL_ERROR "You must provide a cd name (or \"all\" for all of them) to install the file on!")
291 endif()
292
293 #get file if we need to
294 if(NOT _CD_FILE)
295 get_target_property(_CD_FILE ${_CD_TARGET} LOCATION_${CMAKE_BUILD_TYPE})
296 endif()
297
298 #do we add it to all CDs?
299 list(FIND _CD_FOR all __cd)
300 if(NOT __cd EQUAL -1)
301 list(REMOVE_AT _CD_FOR __cd)
302 list(INSERT _CD_FOR __cd "bootcd;livecd;regtest")
303 endif()
304
305 #do we add it to bootcd?
306 list(FIND _CD_FOR bootcd __cd)
307 if(NOT __cd EQUAL -1)
308 #whether or not we should put it in reactos.cab or directly on cd
309 if(_CD_NO_CAB)
310 #directly on cd
311 foreach(item ${_CD_FILE})
312 if(_CD_NAME_ON_CD)
313 #rename it in the cd tree
314 set(__file ${_CD_NAME_ON_CD})
315 else()
316 get_filename_component(__file ${item} NAME)
317 endif()
318 set_property(GLOBAL APPEND PROPERTY BOOTCD_FILE_LIST "${_CD_DESTINATION}/${__file}=${item}")
319 #add it also into the hybridcd if not specified otherwise
320 if(NOT _CD_NOT_IN_HYBRIDCD)
321 set_property(GLOBAL APPEND PROPERTY HYBRIDCD_FILE_LIST "bootcd/${_CD_DESTINATION}/${__file}=${item}")
322 endif()
323 endforeach()
324 if(_CD_TARGET)
325 #manage dependency
326 add_dependencies(bootcd ${_CD_TARGET} registry_inf)
327 endif()
328 else()
329 #add it in reactos.cab
330 dir_to_num(${_CD_DESTINATION} _num)
331 file(RELATIVE_PATH __relative_file ${REACTOS_SOURCE_DIR} ${_CD_FILE})
332 file(APPEND ${REACTOS_BINARY_DIR}/boot/bootdata/packages/reactos.dff.dyn "\"${__relative_file}\" ${_num}\n")
333 unset(__relative_file)
334 if(_CD_TARGET)
335 #manage dependency - target level
336 add_dependencies(reactos_cab_inf ${_CD_TARGET})
337 endif()
338 # manage dependency - file level
339 set_property(GLOBAL APPEND PROPERTY REACTOS_CAB_DEPENDS ${_CD_FILE})
340 endif()
341 endif() #end bootcd
342
343 #do we add it to livecd?
344 list(FIND _CD_FOR livecd __cd)
345 if(NOT __cd EQUAL -1)
346 #manage dependency
347 if(_CD_TARGET)
348 add_dependencies(livecd ${_CD_TARGET} registry_inf)
349 endif()
350 foreach(item ${_CD_FILE})
351 if(_CD_NAME_ON_CD)
352 #rename it in the cd tree
353 set(__file ${_CD_NAME_ON_CD})
354 else()
355 get_filename_component(__file ${item} NAME)
356 endif()
357 set_property(GLOBAL APPEND PROPERTY LIVECD_FILE_LIST "${_CD_DESTINATION}/${__file}=${item}")
358 #add it also into the hybridcd if not specified otherwise
359 if(NOT _CD_NOT_IN_HYBRIDCD)
360 set_property(GLOBAL APPEND PROPERTY HYBRIDCD_FILE_LIST "livecd/${_CD_DESTINATION}/${__file}=${item}")
361 endif()
362 endforeach()
363 endif() #end livecd
364
365 #do we need also to add it to hybridcd?
366 list(FIND _CD_FOR hybridcd __cd)
367 if(NOT __cd EQUAL -1)
368 foreach(item ${_CD_FILE})
369 if(_CD_NAME_ON_CD)
370 #rename it in the cd tree
371 set(__file ${_CD_NAME_ON_CD})
372 else()
373 get_filename_component(__file ${item} NAME)
374 endif()
375 set_property(GLOBAL APPEND PROPERTY HYBRIDCD_FILE_LIST "${_CD_DESTINATION}/${__file}=${item}")
376 endforeach()
377 endif() #end hybridcd
378
379 #do we add it to regtest?
380 list(FIND _CD_FOR regtest __cd)
381 if(NOT __cd EQUAL -1)
382 #whether or not we should put it in reactos.cab or directly on cd
383 if(_CD_NO_CAB)
384 #directly on cd
385 foreach(item ${_CD_FILE})
386 if(_CD_NAME_ON_CD)
387 #rename it in the cd tree
388 set(__file ${_CD_NAME_ON_CD})
389 else()
390 get_filename_component(__file ${item} NAME)
391 endif()
392 set_property(GLOBAL APPEND PROPERTY BOOTCDREGTEST_FILE_LIST "${_CD_DESTINATION}/${__file}=${item}")
393 endforeach()
394 if(_CD_TARGET)
395 #manage dependency
396 add_dependencies(bootcdregtest ${_CD_TARGET} registry_inf)
397 endif()
398 else()
399 #add it in reactos.cab
400 #dir_to_num(${_CD_DESTINATION} _num)
401 #file(APPEND ${REACTOS_BINARY_DIR}/boot/bootdata/packages/reactos.dff.dyn "${_CD_FILE} ${_num}\n")
402 #if(_CD_TARGET)
403 # #manage dependency
404 # add_dependencies(reactos_cab ${_CD_TARGET})
405 #endif()
406 endif()
407 endif() #end bootcd
408 endfunction()
409
410 function(create_iso_lists)
411 # generate reactos.cab before anything else
412 get_property(_filelist GLOBAL PROPERTY REACTOS_CAB_DEPENDS)
413
414 # begin with reactos.inf. We want this command to be always executed, so we pretend it generates another file although it will never do.
415 add_custom_command(
416 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/reactos.inf ${CMAKE_CURRENT_BINARY_DIR}/__some_non_existent_file
417 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${REACTOS_BINARY_DIR}/boot/bootdata/packages/reactos.inf ${CMAKE_CURRENT_BINARY_DIR}/reactos.inf
418 DEPENDS ${REACTOS_BINARY_DIR}/boot/bootdata/packages/reactos.inf reactos_cab_inf)
419
420 add_custom_command(
421 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/reactos.cab
422 COMMAND native-cabman -C ${REACTOS_BINARY_DIR}/boot/bootdata/packages/reactos.dff -RC ${CMAKE_CURRENT_BINARY_DIR}/reactos.inf -N -P ${REACTOS_SOURCE_DIR}
423 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/reactos.inf native-cabman ${_filelist})
424
425 add_custom_target(reactos_cab DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/reactos.cab)
426 add_dependencies(reactos_cab reactos_cab_inf)
427
428 add_cd_file(
429 TARGET reactos_cab
430 FILE ${CMAKE_CURRENT_BINARY_DIR}/reactos.cab
431 DESTINATION reactos
432 NO_CAB FOR bootcd regtest)
433
434 add_cd_file(
435 FILE ${CMAKE_CURRENT_BINARY_DIR}/livecd.iso
436 DESTINATION livecd
437 FOR hybridcd)
438
439 get_property(_filelist GLOBAL PROPERTY BOOTCD_FILE_LIST)
440 string(REPLACE ";" "\n" _filelist "${_filelist}")
441 file(APPEND ${REACTOS_BINARY_DIR}/boot/bootcd.lst "${_filelist}")
442 unset(_filelist)
443
444 get_property(_filelist GLOBAL PROPERTY LIVECD_FILE_LIST)
445 string(REPLACE ";" "\n" _filelist "${_filelist}")
446 file(APPEND ${REACTOS_BINARY_DIR}/boot/livecd.lst "${_filelist}")
447 unset(_filelist)
448
449 get_property(_filelist GLOBAL PROPERTY HYBRIDCD_FILE_LIST)
450 string(REPLACE ";" "\n" _filelist "${_filelist}")
451 file(APPEND ${REACTOS_BINARY_DIR}/boot/hybridcd.lst "${_filelist}")
452 unset(_filelist)
453
454 get_property(_filelist GLOBAL PROPERTY BOOTCDREGTEST_FILE_LIST)
455 string(REPLACE ";" "\n" _filelist "${_filelist}")
456 file(APPEND ${REACTOS_BINARY_DIR}/boot/bootcdregtest.lst "${_filelist}")
457 unset(_filelist)
458 endfunction()
459
460 # Create module_clean targets
461 function(add_clean_target _target)
462 set(_clean_working_directory ${CMAKE_CURRENT_BINARY_DIR})
463 if(CMAKE_GENERATOR STREQUAL "Unix Makefiles" OR CMAKE_GENERATOR STREQUAL "MinGW Makefiles")
464 set(_clean_command make clean)
465 elseif(CMAKE_GENERATOR STREQUAL "NMake Makefiles")
466 set(_clean_command nmake /nologo clean)
467 elseif(CMAKE_GENERATOR STREQUAL "Ninja")
468 set(_clean_command ninja -t clean ${_target})
469 set(_clean_working_directory ${REACTOS_BINARY_DIR})
470 endif()
471 add_custom_target(${_target}_clean
472 COMMAND ${_clean_command}
473 WORKING_DIRECTORY ${_clean_working_directory}
474 COMMENT "Cleaning ${_target}")
475 endfunction()
476
477 if(NOT MSVC_IDE)
478 function(add_library name)
479 _add_library(${name} ${ARGN})
480 add_clean_target(${name})
481 endfunction()
482
483 function(add_executable name)
484 _add_executable(${name} ${ARGN})
485 add_clean_target(${name})
486 endfunction()
487 elseif(USE_FOLDER_STRUCTURE)
488 set_property(GLOBAL PROPERTY USE_FOLDERS ON)
489 string(LENGTH ${CMAKE_SOURCE_DIR} CMAKE_SOURCE_DIR_LENGTH)
490
491 function(add_custom_target name)
492 _add_custom_target(${name} ${ARGN})
493 string(SUBSTRING ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_SOURCE_DIR_LENGTH} -1 CMAKE_CURRENT_SOURCE_DIR_RELATIVE)
494 set_property(TARGET "${name}" PROPERTY FOLDER "${CMAKE_CURRENT_SOURCE_DIR_RELATIVE}")
495 endfunction()
496
497 function(add_library name)
498 _add_library(${name} ${ARGN})
499 get_target_property(_target_excluded ${name} EXCLUDE_FROM_ALL)
500 if(_target_excluded AND ${name} MATCHES "^lib.*")
501 set_property(TARGET "${name}" PROPERTY FOLDER "Importlibs")
502 else()
503 string(SUBSTRING ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_SOURCE_DIR_LENGTH} -1 CMAKE_CURRENT_SOURCE_DIR_RELATIVE)
504 set_property(TARGET "${name}" PROPERTY FOLDER "${CMAKE_CURRENT_SOURCE_DIR_RELATIVE}")
505 endif()
506 endfunction()
507
508 function(add_executable name)
509 _add_executable(${name} ${ARGN})
510 string(SUBSTRING ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_SOURCE_DIR_LENGTH} -1 CMAKE_CURRENT_SOURCE_DIR_RELATIVE)
511 set_property(TARGET "${name}" PROPERTY FOLDER "${CMAKE_CURRENT_SOURCE_DIR_RELATIVE}")
512 endfunction()
513 endif()
514
515 if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
516 function(concatenate_files _output _file1)
517 file(TO_NATIVE_PATH "${_output}" _real_output)
518 file(TO_NATIVE_PATH "${_file1}" _file_list)
519 foreach(_file ${ARGN})
520 file(TO_NATIVE_PATH "${_file}" _real_file)
521 set(_file_list "${_file_list} + ${_real_file}")
522 endforeach()
523 add_custom_command(
524 OUTPUT ${_output}
525 COMMAND cmd.exe /C "copy /Y /B ${_file_list} ${_real_output} > nul"
526 DEPENDS ${_file1} ${ARGN})
527 endfunction()
528 else()
529 macro(concatenate_files _output)
530 add_custom_command(
531 OUTPUT ${_output}
532 COMMAND cat ${ARGN} > ${_output}
533 DEPENDS ${ARGN})
534 endmacro()
535 endif()
536
537 function(add_importlibs _module)
538 add_dependency_node(${_module})
539 foreach(LIB ${ARGN})
540 if("${LIB}" MATCHES "msvcrt")
541 add_target_compile_definitions(${_module} _DLL __USE_CRTIMP)
542 target_link_libraries(${_module} msvcrtex)
543 endif()
544 target_link_libraries(${_module} lib${LIB})
545 add_dependencies(${_module} lib${LIB})
546 add_dependency_edge(${_module} ${LIB})
547 endforeach()
548 endfunction()
549
550 function(set_module_type MODULE TYPE)
551 cmake_parse_arguments(__module "UNICODE" "IMAGEBASE" "ENTRYPOINT" ${ARGN})
552
553 if(__module_UNPARSED_ARGUMENTS)
554 message(STATUS "set_module_type : unparsed arguments ${__module_UNPARSED_ARGUMENTS}, module : ${MODULE}")
555 endif()
556
557 # Add the module to the module group list, if it is defined
558 if(DEFINED CURRENT_MODULE_GROUP)
559 set_property(GLOBAL APPEND PROPERTY ${CURRENT_MODULE_GROUP}_MODULE_LIST "${MODULE}")
560 endif()
561
562 # Set subsystem. Also take this as an occasion
563 # to error out if someone gave a non existing type
564 if((${TYPE} STREQUAL nativecui) OR (${TYPE} STREQUAL nativedll)
565 OR (${TYPE} STREQUAL kernelmodedriver) OR (${TYPE} STREQUAL wdmdriver) OR (${TYPE} STREQUAL kerneldll))
566 set(__subsystem native)
567 elseif(${TYPE} STREQUAL win32cui)
568 set(__subsystem console)
569 elseif(${TYPE} STREQUAL win32gui)
570 set(__subsystem windows)
571 elseif(NOT ((${TYPE} STREQUAL win32dll) OR (${TYPE} STREQUAL win32ocx)
572 OR (${TYPE} STREQUAL cpl) OR (${TYPE} STREQUAL module)))
573 message(FATAL_ERROR "Unknown type ${TYPE} for module ${MODULE}")
574 endif()
575
576 if(DEFINED __subsystem)
577 set_subsystem(${MODULE} ${__subsystem})
578 endif()
579
580 #set unicode definitions
581 if(__module_UNICODE)
582 add_target_compile_definitions(${MODULE} UNICODE _UNICODE)
583 endif()
584
585 # set entry point
586 if(__module_ENTRYPOINT OR (__module_ENTRYPOINT STREQUAL "0"))
587 list(GET __module_ENTRYPOINT 0 __entrypoint)
588 list(LENGTH __module_ENTRYPOINT __length)
589 if(${__length} EQUAL 2)
590 list(GET __module_ENTRYPOINT 1 __entrystack)
591 elseif(NOT ${__length} EQUAL 1)
592 message(FATAL_ERROR "Wrong arguments for ENTRYPOINT parameter of set_module_type : ${__module_ENTRYPOINT}")
593 endif()
594 unset(__length)
595 elseif(${TYPE} STREQUAL nativecui)
596 set(__entrypoint NtProcessStartup)
597 set(__entrystack 4)
598 elseif(${TYPE} STREQUAL win32cui)
599 if(__module_UNICODE)
600 set(__entrypoint wmainCRTStartup)
601 else()
602 set(__entrypoint mainCRTStartup)
603 endif()
604 elseif(${TYPE} STREQUAL win32gui)
605 if(__module_UNICODE)
606 set(__entrypoint wWinMainCRTStartup)
607 else()
608 set(__entrypoint WinMainCRTStartup)
609 endif()
610 elseif((${TYPE} STREQUAL win32dll) OR (${TYPE} STREQUAL win32ocx)
611 OR (${TYPE} STREQUAL cpl))
612 set(__entrypoint DllMainCRTStartup)
613 set(__entrystack 12)
614 elseif((${TYPE} STREQUAL kernelmodedriver) OR (${TYPE} STREQUAL wdmdriver))
615 set(__entrypoint DriverEntry)
616 set(__entrystack 8)
617 elseif(${TYPE} STREQUAL nativedll)
618 set(__entrypoint DllMain)
619 set(__entrystack 12)
620 elseif(${TYPE} STREQUAL module)
621 set(__entrypoint 0)
622 endif()
623
624 if(DEFINED __entrypoint)
625 if(DEFINED __entrystack)
626 set_entrypoint(${MODULE} ${__entrypoint} ${__entrystack})
627 else()
628 set_entrypoint(${MODULE} ${__entrypoint})
629 endif()
630 endif()
631
632 #set base address
633 if(__module_IMAGEBASE)
634 set_image_base(${MODULE} ${__module_IMAGEBASE})
635 elseif(${TYPE} STREQUAL win32dll)
636 if(DEFINED baseaddress_${MODULE})
637 set_image_base(${MODULE} ${baseaddress_${MODULE}})
638 else()
639 message(STATUS "${MODULE} has no base address")
640 endif()
641 elseif((${TYPE} STREQUAL kernelmodedriver) OR (${TYPE} STREQUAL wdmdriver) OR (${TYPE} STREQUAL kerneldll))
642 set_image_base(${MODULE} 0x00010000)
643 endif()
644
645 # Now do some stuff which is specific to each type
646 if((${TYPE} STREQUAL kernelmodedriver) OR (${TYPE} STREQUAL wdmdriver) OR (${TYPE} STREQUAL kerneldll))
647 add_dependencies(${MODULE} bugcodes xdk)
648 if((${TYPE} STREQUAL kernelmodedriver) OR (${TYPE} STREQUAL wdmdriver))
649 set_target_properties(${MODULE} PROPERTIES SUFFIX ".sys")
650 endif()
651 endif()
652
653 if(${TYPE} STREQUAL win32ocx)
654 set_target_properties(${MODULE} PROPERTIES SUFFIX ".ocx")
655 endif()
656
657 if(${TYPE} STREQUAL cpl)
658 set_target_properties(${MODULE} PROPERTIES SUFFIX ".cpl")
659 endif()
660
661 # do compiler specific stuff
662 set_module_type_toolchain(${MODULE} ${TYPE})
663 endfunction()
664
665 function(start_module_group __name)
666 if(DEFINED CURRENT_MODULE_GROUP)
667 message(FATAL_ERROR "CURRENT_MODULE_GROUP is already set ('${CURRENT_MODULE_GROUP}')")
668 endif()
669 set(CURRENT_MODULE_GROUP ${__name} PARENT_SCOPE)
670 endfunction()
671
672 function(end_module_group)
673 get_property(__modulelist GLOBAL PROPERTY ${CURRENT_MODULE_GROUP}_MODULE_LIST)
674 add_custom_target(${CURRENT_MODULE_GROUP})
675 foreach(__module ${__modulelist})
676 add_dependencies(${CURRENT_MODULE_GROUP} ${__module})
677 endforeach()
678 set(CURRENT_MODULE_GROUP PARENT_SCOPE)
679 endfunction()
680
681 function(preprocess_file __in __out)
682 set(__arg ${__in})
683 foreach(__def ${ARGN})
684 list(APPEND __arg -D${__def})
685 endforeach()
686 if(MSVC)
687 add_custom_command(OUTPUT ${_out}
688 COMMAND ${CMAKE_C_COMPILER} /EP ${__arg}
689 DEPENDS ${__in})
690 else()
691 add_custom_command(OUTPUT ${_out}
692 COMMAND ${CMAKE_C_COMPILER} -E ${__arg}
693 DEPENDS ${__in})
694 endif()
695 endfunction()
696
697 function(get_includes OUTPUT_VAR)
698 get_directory_property(_includes INCLUDE_DIRECTORIES)
699 foreach(arg ${_includes})
700 list(APPEND __tmp_var -I${arg})
701 endforeach()
702 set(${OUTPUT_VAR} ${__tmp_var} PARENT_SCOPE)
703 endfunction()
704
705 function(get_defines OUTPUT_VAR)
706 get_directory_property(_defines COMPILE_DEFINITIONS)
707 foreach(arg ${_defines})
708 list(APPEND __tmp_var -D${arg})
709 endforeach()
710 set(${OUTPUT_VAR} ${__tmp_var} PARENT_SCOPE)
711 endfunction()
712
713 if(NOT MSVC)
714 function(add_object_library _target)
715 add_library(${_target} OBJECT ${ARGN})
716 endfunction()
717 else()
718 function(add_object_library _target)
719 add_library(${_target} ${ARGN})
720 endfunction()
721 endif()
722
723 function(add_registry_inf)
724 # Add to the inf files list
725 foreach(_file ${ARGN})
726 set(_source_file "${CMAKE_CURRENT_SOURCE_DIR}/${_file}")
727 set_property(GLOBAL APPEND PROPERTY REGISTRY_INF_LIST ${_source_file})
728 endforeach()
729 endfunction()
730
731 function(create_registry_hives)
732
733 # Shortcut to the registry.inf file
734 set(_registry_inf "${CMAKE_BINARY_DIR}/boot/bootdata/registry.inf")
735
736 # Get the list of inf files
737 get_property(_inf_files GLOBAL PROPERTY REGISTRY_INF_LIST)
738
739 # Convert files to utf16le
740 foreach(_file ${_inf_files})
741 get_filename_component(_file_name ${_file} NAME_WE)
742 string(REPLACE ${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR} _converted_file "${_file}")
743 string(REPLACE ${_file_name} "${_file_name}_utf16" _converted_file ${_converted_file})
744 add_custom_command(OUTPUT ${_converted_file}
745 COMMAND native-utf16le ${_file} ${_converted_file}
746 DEPENDS native-utf16le ${_file})
747 list(APPEND _converted_files ${_converted_file})
748 endforeach()
749
750 # Concatenate all registry files to registry.inf
751 concatenate_files(${_registry_inf} ${_converted_files})
752
753 # Add registry.inf to bootcd
754 add_custom_target(registry_inf DEPENDS ${_registry_inf})
755 add_cd_file(TARGET registry_inf
756 FILE ${_registry_inf}
757 DESTINATION reactos
758 NO_CAB
759 FOR bootcd regtest)
760
761 # livecd hives
762 list(APPEND _livecd_inf_files
763 ${_registry_inf}
764 ${CMAKE_SOURCE_DIR}/boot/bootdata/livecd.inf
765 ${CMAKE_SOURCE_DIR}/boot/bootdata/hiveinst.inf)
766
767 add_custom_command(
768 OUTPUT ${CMAKE_BINARY_DIR}/boot/bootdata/sam
769 ${CMAKE_BINARY_DIR}/boot/bootdata/default
770 ${CMAKE_BINARY_DIR}/boot/bootdata/security
771 ${CMAKE_BINARY_DIR}/boot/bootdata/software
772 ${CMAKE_BINARY_DIR}/boot/bootdata/system
773 COMMAND native-mkhive ${CMAKE_BINARY_DIR}/boot/bootdata ${_livecd_inf_files}
774 DEPENDS native-mkhive ${_livecd_inf_files})
775
776 add_custom_target(livecd_hives
777 DEPENDS ${CMAKE_BINARY_DIR}/boot/bootdata/sam
778 ${CMAKE_BINARY_DIR}/boot/bootdata/default
779 ${CMAKE_BINARY_DIR}/boot/bootdata/security
780 ${CMAKE_BINARY_DIR}/boot/bootdata/software
781 ${CMAKE_BINARY_DIR}/boot/bootdata/system)
782
783 add_cd_file(
784 FILE ${CMAKE_BINARY_DIR}/boot/bootdata/sam
785 ${CMAKE_BINARY_DIR}/boot/bootdata/default
786 ${CMAKE_BINARY_DIR}/boot/bootdata/security
787 ${CMAKE_BINARY_DIR}/boot/bootdata/software
788 ${CMAKE_BINARY_DIR}/boot/bootdata/system
789 TARGET livecd_hives
790 DESTINATION reactos/system32/config
791 FOR livecd)
792
793 # BCD Hive
794 add_custom_command(
795 OUTPUT ${CMAKE_BINARY_DIR}/boot/bootdata/BCD
796 COMMAND native-mkhive ${CMAKE_BINARY_DIR}/boot/bootdata ${CMAKE_BINARY_DIR}/boot/bootdata/hivebcd_utf16.inf
797 DEPENDS native-mkhive ${CMAKE_SOURCE_DIR}/boot/bootdata/hivebcd.inf)
798
799 add_custom_target(bcd_hive
800 DEPENDS ${CMAKE_BINARY_DIR}/boot/bootdata/BCD)
801
802 add_cd_file(
803 FILE ${CMAKE_BINARY_DIR}/boot/bootdata/BCD
804 TARGET bcd_hive
805 DESTINATION efi/boot
806 NO_CAB
807 FOR bootcd regtest livecd)
808
809 endfunction()
810
811 if(KDBG)
812 set(ROSSYM_LIB "rossym")
813 else()
814 set(ROSSYM_LIB "")
815 endif()
816
817 function(add_rc_deps _target_rc)
818 set_source_files_properties(${_target_rc} PROPERTIES OBJECT_DEPENDS "${ARGN}")
819 endfunction()