xref: /reactos/sdk/cmake/CMakeMacros.cmake (revision 0c2cdcae)
1
2function(add_dependency_node _node)
3    if(GENERATE_DEPENDENCY_GRAPH)
4        get_target_property(_type ${_node} TYPE)
5        if(_type MATCHES SHARED_LIBRARY|MODULE_LIBRARY OR ${_node} MATCHES ntoskrnl)
6            file(APPEND ${REACTOS_BINARY_DIR}/dependencies.graphml "    <node id=\"${_node}\"/>\n")
7        endif()
8     endif()
9endfunction()
10
11function(add_dependency_edge _source _target)
12    if(GENERATE_DEPENDENCY_GRAPH)
13        get_target_property(_type ${_source} TYPE)
14        if(_type MATCHES SHARED_LIBRARY|MODULE_LIBRARY)
15            file(APPEND ${REACTOS_BINARY_DIR}/dependencies.graphml "    <edge source=\"${_source}\" target=\"${_target}\"/>\n")
16        endif()
17    endif()
18endfunction()
19
20function(add_dependency_header)
21    if(GENERATE_DEPENDENCY_GRAPH)
22        file(WRITE ${REACTOS_BINARY_DIR}/dependencies.graphml "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<graphml>\n  <graph id=\"ReactOS dependencies\" edgedefault=\"directed\">\n")
23    endif()
24endfunction()
25
26function(add_dependency_footer)
27    if(GENERATE_DEPENDENCY_GRAPH)
28        add_dependency_node(ntdll)
29        file(APPEND ${REACTOS_BINARY_DIR}/dependencies.graphml "  </graph>\n</graphml>\n")
30    endif()
31endfunction()
32
33function(add_message_headers _type)
34    if(${_type} STREQUAL UNICODE)
35        set(_flag "-U")
36    else()
37        set(_flag "-A")
38    endif()
39    foreach(_file ${ARGN})
40        get_filename_component(_file_name ${_file} NAME_WE)
41        set(_converted_file ${CMAKE_CURRENT_BINARY_DIR}/${_file}) ## ${_file_name}.mc
42        set(_source_file ${CMAKE_CURRENT_SOURCE_DIR}/${_file})    ## ${_file_name}.mc
43        utf16le_convert(${_source_file} ${_converted_file} nobom)
44        macro_mc(${_flag} ${_converted_file})
45        add_custom_command(
46            OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_file_name}.h ${CMAKE_CURRENT_BINARY_DIR}/${_file_name}.rc
47            COMMAND ${COMMAND_MC}
48            DEPENDS "${_converted_file}")
49        set_source_files_properties(
50            ${CMAKE_CURRENT_BINARY_DIR}/${_file_name}.h ${CMAKE_CURRENT_BINARY_DIR}/${_file_name}.rc
51            PROPERTIES GENERATED TRUE)
52        add_custom_target(${_file_name} ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${_file_name}.h ${CMAKE_CURRENT_BINARY_DIR}/${_file_name}.rc)
53    endforeach()
54endfunction()
55
56function(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(
83        ${CMAKE_CURRENT_BINARY_DIR}/${_LINK_NAME}.lnk PROPERTIES GENERATED TRUE)
84endfunction()
85
86#
87# WARNING!
88# Please keep the numbering in this list in sync with
89# boot/bootdata/packages/reactos.dff.in
90#
91macro(dir_to_num dir var)
92    if(${dir} STREQUAL reactos)
93        set(${var} 1)
94    elseif(${dir} STREQUAL reactos/system32)
95        set(${var} 2)
96    elseif(${dir} STREQUAL reactos/system32/drivers)
97        set(${var} 3)
98    elseif(${dir} STREQUAL reactos/Fonts)
99        set(${var} 4)
100    elseif(${dir} STREQUAL reactos/system32/drivers/etc)
101        set(${var} 5)
102    elseif(${dir} STREQUAL reactos/inf)
103        set(${var} 6)
104    elseif(${dir} STREQUAL reactos/bin)
105        set(${var} 7)
106    elseif(${dir} STREQUAL reactos/bin/testdata)
107        set(${var} 8)
108    elseif(${dir} STREQUAL reactos/media)
109        set(${var} 9)
110    elseif(${dir} STREQUAL reactos/Microsoft.NET)
111        set(${var} 10)
112    elseif(${dir} STREQUAL reactos/Microsoft.NET/Framework)
113        set(${var} 11)
114    elseif(${dir} STREQUAL reactos/Microsoft.NET/Framework/v1.0.3705)
115        set(${var} 12)
116    elseif(${dir} STREQUAL reactos/Microsoft.NET/Framework/v1.1.4322)
117        set(${var} 13)
118    elseif(${dir} STREQUAL reactos/Microsoft.NET/Framework/v2.0.50727)
119        set(${var} 14)
120    elseif(${dir} STREQUAL reactos/Resources)
121        set(${var} 15)
122    elseif(${dir} STREQUAL reactos/Resources/Themes)
123        set(${var} 16)
124    elseif(${dir} STREQUAL reactos/system32/wbem)
125        set(${var} 17)
126    elseif(${dir} STREQUAL reactos/Resources/Themes/Lautus)
127        set(${var} 18)
128    elseif(${dir} STREQUAL reactos/Help)
129        set(${var} 19)
130    elseif(${dir} STREQUAL reactos/Config)
131        set(${var} 20)
132    elseif(${dir} STREQUAL reactos/Cursors)
133        set(${var} 21)
134    elseif(${dir} STREQUAL reactos/system32/ShellExt)
135        set(${var} 22)
136    elseif(${dir} STREQUAL reactos/Temp)
137        set(${var} 23)
138    elseif(${dir} STREQUAL reactos/system32/spool)
139        set(${var} 24)
140    elseif(${dir} STREQUAL reactos/system32/spool/drivers)
141        set(${var} 25)
142    elseif(${dir} STREQUAL reactos/system32/spool/drivers/color)
143        set(${var} 26)
144    elseif(${dir} STREQUAL reactos/system32/spool/drivers/w32x86)
145        set(${var} 27)
146    elseif(${dir} STREQUAL reactos/system32/spool/drivers/w32x86/3)
147        set(${var} 28)
148    elseif(${dir} STREQUAL reactos/system32/spool/prtprocs)
149        set(${var} 29)
150    elseif(${dir} STREQUAL reactos/system32/spool/prtprocs/w32x86)
151        set(${var} 30)
152    elseif(${dir} STREQUAL reactos/system32/spool/PRINTERS)
153        set(${var} 31)
154    elseif(${dir} STREQUAL reactos/system32/wbem/Repository)
155        set(${var} 32)
156    elseif(${dir} STREQUAL reactos/system32/wbem/Repository/FS)
157        set(${var} 33)
158    elseif(${dir} STREQUAL reactos/system32/wbem/mof/good)
159        set(${var} 34)
160    elseif(${dir} STREQUAL reactos/system32/wbem/mof/bad)
161        set(${var} 35)
162    elseif(${dir} STREQUAL reactos/system32/wbem/AdStatus)
163        set(${var} 36)
164    elseif(${dir} STREQUAL reactos/system32/wbem/xml)
165        set(${var} 37)
166    elseif(${dir} STREQUAL reactos/system32/wbem/Logs)
167        set(${var} 38)
168    elseif(${dir} STREQUAL reactos/system32/wbem/AutoRecover)
169        set(${var} 39)
170    elseif(${dir} STREQUAL reactos/system32/wbem/snmp)
171        set(${var} 40)
172    elseif(${dir} STREQUAL reactos/system32/wbem/Performance)
173        set(${var} 41)
174    elseif(${dir} STREQUAL reactos/twain_32)
175        set(${var} 42)
176    elseif(${dir} STREQUAL reactos/repair)
177        set(${var} 43)
178    elseif(${dir} STREQUAL reactos/Web)
179        set(${var} 44)
180    elseif(${dir} STREQUAL reactos/Web/Wallpaper)
181        set(${var} 45)
182    elseif(${dir} STREQUAL reactos/Prefetch)
183        set(${var} 46)
184    elseif(${dir} STREQUAL reactos/security)
185        set(${var} 47)
186    elseif(${dir} STREQUAL reactos/security/Database)
187        set(${var} 48)
188    elseif(${dir} STREQUAL reactos/security/logs)
189        set(${var} 49)
190    elseif(${dir} STREQUAL reactos/security/templates)
191        set(${var} 50)
192    elseif(${dir} STREQUAL reactos/system32/CatRoot)
193        set(${var} 51)
194    elseif(${dir} STREQUAL reactos/system32/CatRoot2)
195        set(${var} 52)
196    elseif(${dir} STREQUAL reactos/AppPatch)
197        set(${var} 53)
198    elseif(${dir} STREQUAL reactos/winsxs)
199        set(${var} 54)
200    elseif(${dir} STREQUAL reactos/winsxs/manifests)
201        set(${var} 55)
202    elseif(${dir} STREQUAL reactos/winsxs/x86_microsoft.windows.common-controls_6595b64144ccf1df_5.82.2600.2982_none_deadbeef)
203        set(${var} 56)
204    elseif(${dir} STREQUAL reactos/winsxs/x86_microsoft.windows.common-controls_6595b64144ccf1df_6.0.2600.2982_none_deadbeef)
205        set(${var} 57)
206    elseif(${dir} STREQUAL reactos/winsxs/x86_microsoft.windows.gdiplus_6595b64144ccf1df_1.1.7601.23038_none_deadbeef)
207        set(${var} 58)
208    elseif(${dir} STREQUAL reactos/bin/suppl)
209        set(${var} 60)
210    elseif(${dir} STREQUAL reactos/winsxs/x86_microsoft.windows.gdiplus_6595b64144ccf1df_1.0.14393.0_none_deadbeef)
211        set(${var} 61)
212    elseif(${dir} STREQUAL reactos/Resources/Themes/Modern)
213        set(${var} 62)
214    elseif(${dir} STREQUAL reactos/3rdParty)
215        set(${var} 63)
216    elseif(${dir} STREQUAL reactos/Resources/Themes/Lunar)
217        set(${var} 64)
218    elseif(${dir} STREQUAL reactos/Resources/Themes/Mizu)
219        set(${var} 65)
220    elseif(${dir} STREQUAL reactos/system32/spool/prtprocs/x64)
221        set(${var} 66)
222    elseif(${dir} STREQUAL reactos/winsxs/amd64_microsoft.windows.common-controls_6595b64144ccf1df_5.82.2600.2982_none_deadbeef)
223        set(${var} 67)
224    elseif(${dir} STREQUAL reactos/winsxs/amd64_microsoft.windows.common-controls_6595b64144ccf1df_6.0.2600.2982_none_deadbeef)
225        set(${var} 68)
226    elseif(${dir} STREQUAL reactos/winsxs/amd64_microsoft.windows.gdiplus_6595b64144ccf1df_1.1.7601.23038_none_deadbeef)
227        set(${var} 69)
228    elseif(${dir} STREQUAL reactos/winsxs/amd64_microsoft.windows.gdiplus_6595b64144ccf1df_1.0.14393.0_none_deadbeef)
229        set(${var} 71)
230
231    elseif(${dir} STREQUAL reactos/winsxs/arm_microsoft.windows.common-controls_6595b64144ccf1df_5.82.2600.2982_none_deadbeef)
232        set(${var} 72)
233    elseif(${dir} STREQUAL reactos/winsxs/arm_microsoft.windows.common-controls_6595b64144ccf1df_6.0.2600.2982_none_deadbeef)
234        set(${var} 73)
235    elseif(${dir} STREQUAL reactos/winsxs/arm_microsoft.windows.gdiplus_6595b64144ccf1df_1.1.7601.23038_none_deadbeef)
236        set(${var} 74)
237    elseif(${dir} STREQUAL reactos/winsxs/arm_microsoft.windows.gdiplus_6595b64144ccf1df_1.0.14393.0_none_deadbeef)
238        set(${var} 76)
239
240    elseif(${dir} STREQUAL reactos/winsxs/arm64_microsoft.windows.common-controls_6595b64144ccf1df_5.82.2600.2982_none_deadbeef)
241        set(${var} 77)
242    elseif(${dir} STREQUAL reactos/winsxs/arm64_microsoft.windows.common-controls_6595b64144ccf1df_6.0.2600.2982_none_deadbeef)
243        set(${var} 78)
244    elseif(${dir} STREQUAL reactos/winsxs/arm64_microsoft.windows.gdiplus_6595b64144ccf1df_1.1.7601.23038_none_deadbeef)
245        set(${var} 79)
246    elseif(${dir} STREQUAL reactos/winsxs/arm64_microsoft.windows.gdiplus_6595b64144ccf1df_1.0.14393.0_none_deadbeef)
247        set(${var} 81)
248
249
250    else()
251        message(FATAL_ERROR "Wrong destination: ${dir}")
252    endif()
253endmacro()
254
255function(add_cd_file)
256    cmake_parse_arguments(_CD "NO_CAB;NOT_IN_HYBRIDCD" "DESTINATION;NAME_ON_CD;TARGET" "FILE;FOR" ${ARGN})
257    if(NOT (_CD_TARGET OR _CD_FILE))
258        message(FATAL_ERROR "You must provide a target or a file to install!")
259    endif()
260
261    if(NOT _CD_DESTINATION)
262        message(FATAL_ERROR "You must provide a destination")
263    elseif(${_CD_DESTINATION} STREQUAL root)
264        set(_CD_DESTINATION "")
265    endif()
266
267    if(NOT _CD_FOR)
268        message(FATAL_ERROR "You must provide a cd name (or \"all\" for all of them) to install the file on!")
269    endif()
270
271    # get file if we need to
272    if(NOT _CD_FILE)
273        set(_CD_FILE "$<TARGET_FILE:${_CD_TARGET}>")
274        if(NOT _CD_NAME_ON_CD)
275            set(_CD_NAME_ON_CD "$<TARGET_FILE_NAME:${_CD_TARGET}>")
276        endif()
277    endif()
278
279    # do we add it to all CDs?
280    list(FIND _CD_FOR "all" __cd)
281    if(NOT __cd EQUAL -1)
282        list(REMOVE_ITEM _CD_FOR "all")
283        list(APPEND _CD_FOR "bootcd;livecd;regtest")
284    endif()
285
286    # do we add it to bootcd?
287    list(FIND _CD_FOR bootcd __cd)
288    if(NOT __cd EQUAL -1)
289        # whether or not we should put it in reactos.cab or directly on cd
290        if(_CD_NO_CAB)
291            # directly on cd
292            foreach(item ${_CD_FILE})
293                if(_CD_NAME_ON_CD)
294                    # rename it in the cd tree
295                    set(__file ${_CD_NAME_ON_CD})
296                else()
297                    get_filename_component(__file ${item} NAME)
298                endif()
299                set_property(GLOBAL APPEND PROPERTY BOOTCD_FILE_LIST "${_CD_DESTINATION}/${__file}=${item}")
300                # add it also into the hybridcd if not specified otherwise
301                if(NOT _CD_NOT_IN_HYBRIDCD)
302                    set_property(GLOBAL APPEND PROPERTY HYBRIDCD_FILE_LIST "bootcd/${_CD_DESTINATION}/${__file}=${item}")
303                endif()
304            endforeach()
305            # manage dependency
306            if(_CD_TARGET)
307                add_dependencies(bootcd ${_CD_TARGET} registry_inf)
308            endif()
309        else()
310            dir_to_num(${_CD_DESTINATION} _num)
311            foreach(item ${_CD_FILE})
312                # add it in reactos.cab
313                file(APPEND ${REACTOS_BINARY_DIR}/boot/bootdata/packages/reactos.dff.cmake "\"${item}\" ${_num}\n")
314
315                # manage dependency - file level
316                set_property(GLOBAL APPEND PROPERTY REACTOS_CAB_DEPENDS ${item})
317            endforeach()
318
319            # manage dependency - target level
320            if(_CD_TARGET)
321                add_dependencies(reactos_cab_inf ${_CD_TARGET})
322            endif()
323        endif()
324    endif() #end bootcd
325
326    # do we add it to livecd?
327    list(FIND _CD_FOR livecd __cd)
328    if(NOT __cd EQUAL -1)
329        # manage dependency
330        if(_CD_TARGET)
331            add_dependencies(livecd ${_CD_TARGET} registry_inf)
332        endif()
333        foreach(item ${_CD_FILE})
334            if(_CD_NAME_ON_CD)
335                # rename it in the cd tree
336                set(__file ${_CD_NAME_ON_CD})
337            else()
338                get_filename_component(__file ${item} NAME)
339            endif()
340            set_property(GLOBAL APPEND PROPERTY LIVECD_FILE_LIST "${_CD_DESTINATION}/${__file}=${item}")
341            # add it also into the hybridcd if not specified otherwise
342            if(NOT _CD_NOT_IN_HYBRIDCD)
343                set_property(GLOBAL APPEND PROPERTY HYBRIDCD_FILE_LIST "livecd/${_CD_DESTINATION}/${__file}=${item}")
344            endif()
345        endforeach()
346    endif() #end livecd
347
348    # do we need also to add it to hybridcd?
349    list(FIND _CD_FOR hybridcd __cd)
350    if(NOT __cd EQUAL -1)
351        # manage dependency
352        if(_CD_TARGET)
353            add_dependencies(hybridcd ${_CD_TARGET})
354        endif()
355        foreach(item ${_CD_FILE})
356            if(_CD_NAME_ON_CD)
357                # rename it in the cd tree
358                set(__file ${_CD_NAME_ON_CD})
359            else()
360                get_filename_component(__file ${item} NAME)
361            endif()
362            set_property(GLOBAL APPEND PROPERTY HYBRIDCD_FILE_LIST "${_CD_DESTINATION}/${__file}=${item}")
363        endforeach()
364    endif() #end hybridcd
365
366    # do we add it to regtest?
367    list(FIND _CD_FOR regtest __cd)
368    if(NOT __cd EQUAL -1)
369        # whether or not we should put it in reactos.cab or directly on cd
370        if(_CD_NO_CAB)
371            # directly on cd
372            foreach(item ${_CD_FILE})
373                if(_CD_NAME_ON_CD)
374                    # rename it in the cd tree
375                    set(__file ${_CD_NAME_ON_CD})
376                else()
377                    get_filename_component(__file ${item} NAME)
378                endif()
379                set_property(GLOBAL APPEND PROPERTY BOOTCDREGTEST_FILE_LIST "${_CD_DESTINATION}/${__file}=${item}")
380            endforeach()
381            # manage dependency
382            if(_CD_TARGET)
383                add_dependencies(bootcdregtest ${_CD_TARGET} registry_inf)
384            endif()
385        else()
386            #add it in reactos.cab
387            #dir_to_num(${_CD_DESTINATION} _num)
388            #file(APPEND ${REACTOS_BINARY_DIR}/boot/bootdata/packages/reactos.dff.dyn "${_CD_FILE} ${_num}\n")
389            #if(_CD_TARGET)
390            #    #manage dependency
391            #    add_dependencies(reactos_cab ${_CD_TARGET})
392            #endif()
393        endif()
394    endif() #end bootcd
395endfunction()
396
397function(create_iso_lists)
398    # generate reactos.cab before anything else
399    get_property(_filelist GLOBAL PROPERTY REACTOS_CAB_DEPENDS)
400
401    # begin with reactos.inf. We want this command to be always executed, so we pretend it generates another file although it will never do.
402    add_custom_command(
403        OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/reactos.inf ${CMAKE_CURRENT_BINARY_DIR}/__some_non_existent_file
404        COMMAND ${CMAKE_COMMAND} -E copy_if_different ${REACTOS_BINARY_DIR}/boot/bootdata/packages/reactos.inf ${CMAKE_CURRENT_BINARY_DIR}/reactos.inf
405        DEPENDS ${REACTOS_BINARY_DIR}/boot/bootdata/packages/reactos.inf reactos_cab_inf)
406
407    add_custom_command(
408        OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/reactos.cab
409        COMMAND native-cabman -C ${REACTOS_BINARY_DIR}/boot/bootdata/packages/reactos.dff -RC ${CMAKE_CURRENT_BINARY_DIR}/reactos.inf -N -P ${REACTOS_SOURCE_DIR}
410        DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/reactos.inf native-cabman ${_filelist})
411
412    add_custom_target(reactos_cab DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/reactos.cab)
413    add_dependencies(reactos_cab reactos_cab_inf)
414
415    add_cd_file(
416        TARGET reactos_cab
417        FILE ${CMAKE_CURRENT_BINARY_DIR}/reactos.cab
418        DESTINATION reactos
419        NO_CAB FOR bootcd regtest)
420
421    add_cd_file(
422        FILE ${CMAKE_CURRENT_BINARY_DIR}/livecd.iso
423        DESTINATION livecd
424        FOR hybridcd)
425
426    get_property(_filelist GLOBAL PROPERTY BOOTCD_FILE_LIST)
427    string(REPLACE ";" "\n" _filelist "${_filelist}")
428    file(APPEND ${REACTOS_BINARY_DIR}/boot/bootcd.cmake.lst "${_filelist}")
429    unset(_filelist)
430    file(GENERATE
431         OUTPUT ${REACTOS_BINARY_DIR}/boot/bootcd.$<CONFIG>.lst
432         INPUT ${REACTOS_BINARY_DIR}/boot/bootcd.cmake.lst)
433
434    get_property(_filelist GLOBAL PROPERTY LIVECD_FILE_LIST)
435    string(REPLACE ";" "\n" _filelist "${_filelist}")
436    file(APPEND ${REACTOS_BINARY_DIR}/boot/livecd.cmake.lst "${_filelist}")
437    unset(_filelist)
438    file(GENERATE
439         OUTPUT ${REACTOS_BINARY_DIR}/boot/livecd.$<CONFIG>.lst
440         INPUT ${REACTOS_BINARY_DIR}/boot/livecd.cmake.lst)
441
442    get_property(_filelist GLOBAL PROPERTY HYBRIDCD_FILE_LIST)
443    string(REPLACE ";" "\n" _filelist "${_filelist}")
444    file(APPEND ${REACTOS_BINARY_DIR}/boot/hybridcd.cmake.lst "${_filelist}")
445    unset(_filelist)
446    file(GENERATE
447         OUTPUT ${REACTOS_BINARY_DIR}/boot/hybridcd.$<CONFIG>.lst
448         INPUT ${REACTOS_BINARY_DIR}/boot/hybridcd.cmake.lst)
449
450    get_property(_filelist GLOBAL PROPERTY BOOTCDREGTEST_FILE_LIST)
451    string(REPLACE ";" "\n" _filelist "${_filelist}")
452    file(APPEND ${REACTOS_BINARY_DIR}/boot/bootcdregtest.cmake.lst "${_filelist}")
453    unset(_filelist)
454    file(GENERATE
455         OUTPUT ${REACTOS_BINARY_DIR}/boot/bootcdregtest.$<CONFIG>.lst
456         INPUT ${REACTOS_BINARY_DIR}/boot/bootcdregtest.cmake.lst)
457endfunction()
458
459# Create module_clean targets
460function(add_clean_target _target)
461    set(_clean_working_directory ${CMAKE_CURRENT_BINARY_DIR})
462    if(CMAKE_GENERATOR STREQUAL "Unix Makefiles" OR CMAKE_GENERATOR STREQUAL "MinGW Makefiles")
463        set(_clean_command make clean)
464    elseif(CMAKE_GENERATOR STREQUAL "NMake Makefiles")
465        set(_clean_command nmake /nologo clean)
466    elseif(CMAKE_GENERATOR STREQUAL "Ninja")
467        set(_clean_command ninja -t clean ${_target})
468        set(_clean_working_directory ${REACTOS_BINARY_DIR})
469    endif()
470    add_custom_target(${_target}_clean
471        COMMAND ${_clean_command}
472        WORKING_DIRECTORY ${_clean_working_directory}
473        COMMENT "Cleaning ${_target}")
474endfunction()
475
476if(NOT MSVC_IDE)
477    function(add_library name)
478        _add_library(${name} ${ARGN})
479        add_clean_target(${name})
480        # cmake adds a module_EXPORTS define when compiling a module or a shared library. We don't use that.
481        get_target_property(_type ${name} TYPE)
482        if(_type MATCHES SHARED_LIBRARY|MODULE_LIBRARY)
483            set_target_properties(${name} PROPERTIES DEFINE_SYMBOL "")
484        endif()
485    endfunction()
486
487    function(add_executable name)
488        _add_executable(${name} ${ARGN})
489        add_clean_target(${name})
490    endfunction()
491elseif(USE_FOLDER_STRUCTURE)
492    set_property(GLOBAL PROPERTY USE_FOLDERS ON)
493    string(LENGTH ${CMAKE_SOURCE_DIR} CMAKE_SOURCE_DIR_LENGTH)
494
495    function(add_custom_target name)
496        _add_custom_target(${name} ${ARGN})
497        string(SUBSTRING ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_SOURCE_DIR_LENGTH} -1 CMAKE_CURRENT_SOURCE_DIR_RELATIVE)
498        set_property(TARGET "${name}" PROPERTY FOLDER "${CMAKE_CURRENT_SOURCE_DIR_RELATIVE}")
499    endfunction()
500
501    function(add_library name)
502        _add_library(${name} ${ARGN})
503        get_target_property(_type ${name} TYPE)
504        if (NOT _type STREQUAL "INTERFACE_LIBRARY")
505            get_target_property(_target_excluded ${name} EXCLUDE_FROM_ALL)
506            if(_target_excluded AND ${name} MATCHES "^lib.*")
507                set_property(TARGET "${name}" PROPERTY FOLDER "Importlibs")
508            else()
509                string(SUBSTRING ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_SOURCE_DIR_LENGTH} -1 CMAKE_CURRENT_SOURCE_DIR_RELATIVE)
510                set_property(TARGET "${name}" PROPERTY FOLDER "${CMAKE_CURRENT_SOURCE_DIR_RELATIVE}")
511            endif()
512        endif()
513        # cmake adds a module_EXPORTS define when compiling a module or a shared library. We don't use that.
514        if(_type MATCHES SHARED_LIBRARY|MODULE_LIBRARY)
515            set_target_properties(${name} PROPERTIES DEFINE_SYMBOL "")
516        endif()
517    endfunction()
518
519    function(add_executable name)
520        _add_executable(${name} ${ARGN})
521        string(SUBSTRING ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_SOURCE_DIR_LENGTH} -1 CMAKE_CURRENT_SOURCE_DIR_RELATIVE)
522        set_property(TARGET "${name}" PROPERTY FOLDER "${CMAKE_CURRENT_SOURCE_DIR_RELATIVE}")
523    endfunction()
524else()
525    function(add_library name)
526        _add_library(${name} ${ARGN})
527        # cmake adds a module_EXPORTS define when compiling a module or a shared library. We don't use that.
528        get_target_property(_type ${name} TYPE)
529        if(_type MATCHES SHARED_LIBRARY|MODULE_LIBRARY)
530            set_target_properties(${name} PROPERTIES DEFINE_SYMBOL "")
531        endif()
532    endfunction()
533endif()
534
535if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
536    function(concatenate_files _output _file1)
537        file(TO_NATIVE_PATH "${_output}" _real_output)
538        file(TO_NATIVE_PATH "${_file1}" _file_list)
539        foreach(_file ${ARGN})
540            file(TO_NATIVE_PATH "${_file}" _real_file)
541            set(_file_list "${_file_list} + ${_real_file}")
542        endforeach()
543        add_custom_command(
544            OUTPUT ${_output}
545            COMMAND cmd.exe /C "copy /Y /B ${_file_list} ${_real_output} > nul"
546            DEPENDS ${_file1} ${ARGN})
547    endfunction()
548else()
549    macro(concatenate_files _output)
550        add_custom_command(
551            OUTPUT ${_output}
552            COMMAND cat ${ARGN} > ${_output}
553            DEPENDS ${ARGN})
554    endmacro()
555endif()
556
557function(add_importlibs _module)
558    add_dependency_node(${_module})
559    foreach(LIB ${ARGN})
560        target_link_libraries(${_module} lib${LIB})
561        add_dependency_edge(${_module} ${LIB})
562    endforeach()
563endfunction()
564
565# Some helper lists
566list(APPEND VALID_MODULE_TYPES kernel kerneldll kernelmodedriver wdmdriver nativecui nativedll win32cui win32gui win32dll win32ocx cpl module)
567list(APPEND KERNEL_MODULE_TYPES kernel kerneldll kernelmodedriver wdmdriver)
568list(APPEND NATIVE_MODULE_TYPES kernel kerneldll kernelmodedriver wdmdriver nativecui nativedll)
569
570function(set_module_type MODULE TYPE)
571    cmake_parse_arguments(__module "UNICODE" "IMAGEBASE" "ENTRYPOINT" ${ARGN})
572
573    if(__module_UNPARSED_ARGUMENTS)
574        message(STATUS "set_module_type : unparsed arguments ${__module_UNPARSED_ARGUMENTS}, module : ${MODULE}")
575    endif()
576
577    # Check this is a type that we know
578    if (NOT TYPE IN_LIST VALID_MODULE_TYPES)
579        message(FATAL_ERROR "Unknown type ${TYPE} for module ${MODULE}")
580    endif()
581
582    # Set our target property
583    set_target_properties(${MODULE} PROPERTIES REACTOS_MODULE_TYPE ${TYPE})
584
585    # Add the module to the module group list, if it is defined
586    if(DEFINED CURRENT_MODULE_GROUP)
587        set_property(GLOBAL APPEND PROPERTY ${CURRENT_MODULE_GROUP}_MODULE_LIST "${MODULE}")
588    endif()
589
590    # Set subsystem.
591    if(TYPE IN_LIST NATIVE_MODULE_TYPES)
592        set_subsystem(${MODULE} native)
593    elseif(${TYPE} STREQUAL win32cui)
594        set_subsystem(${MODULE} console)
595    elseif(${TYPE} STREQUAL win32gui)
596        set_subsystem(${MODULE} windows)
597    endif()
598
599    # Set unicode definitions
600    if(__module_UNICODE)
601        target_compile_definitions(${MODULE} PRIVATE UNICODE _UNICODE)
602    endif()
603
604    # Set entry point
605    if(__module_ENTRYPOINT OR (__module_ENTRYPOINT STREQUAL "0"))
606        set_entrypoint(${MODULE} ${__module_ENTRYPOINT})
607    elseif(${TYPE} STREQUAL nativecui)
608        set_entrypoint(${MODULE} NtProcessStartup 4)
609    elseif(${TYPE} STREQUAL win32cui)
610        if(__module_UNICODE)
611            set_entrypoint(${MODULE} wmainCRTStartup)
612        else()
613            set_entrypoint(${MODULE} mainCRTStartup)
614        endif()
615    elseif(${TYPE} STREQUAL win32gui)
616        if(__module_UNICODE)
617            set_entrypoint(${MODULE} wWinMainCRTStartup)
618        else()
619            set_entrypoint(${MODULE} WinMainCRTStartup)
620        endif()
621    elseif((${TYPE} STREQUAL win32dll) OR (${TYPE} STREQUAL win32ocx)
622            OR (${TYPE} STREQUAL cpl))
623        set_entrypoint(${MODULE} DllMainCRTStartup 12)
624    elseif((${TYPE} STREQUAL kernelmodedriver) OR (${TYPE} STREQUAL wdmdriver))
625        set_entrypoint(${MODULE} DriverEntry 8)
626    elseif(${TYPE} STREQUAL nativedll)
627        set_entrypoint(${MODULE} DllMain 12)
628    elseif(TYPE STREQUAL kernel)
629        set_entrypoint(${MODULE} KiSystemStartup 4)
630    elseif(${TYPE} STREQUAL module)
631        set_entrypoint(${MODULE} 0)
632    endif()
633
634    # Set base address
635    # Use 'IMAGEBASE default' to skip these set_image_base(), especially for win32dll test files
636    if(__module_IMAGEBASE)
637        if(NOT ${__module_IMAGEBASE} STREQUAL "default")
638            set_image_base(${MODULE} ${__module_IMAGEBASE})
639        endif()
640    elseif(${TYPE} STREQUAL win32dll)
641        if(DEFINED baseaddress_${MODULE})
642            set_image_base(${MODULE} ${baseaddress_${MODULE}})
643        else()
644            message(STATUS "${MODULE} has no base address")
645        endif()
646    elseif(TYPE IN_LIST KERNEL_MODULE_TYPES)
647        # special case for kernel
648        if (TYPE STREQUAL kernel)
649            set_image_base(${MODULE} 0x00400000)
650        else()
651            set_image_base(${MODULE} 0x00010000)
652        endif()
653    endif()
654
655    # Now do some stuff which is specific to each type
656    if(TYPE IN_LIST KERNEL_MODULE_TYPES)
657        add_dependencies(${MODULE} bugcodes xdk)
658        if((${TYPE} STREQUAL kernelmodedriver) OR (${TYPE} STREQUAL wdmdriver))
659            set_target_properties(${MODULE} PROPERTIES SUFFIX ".sys")
660        endif()
661    endif()
662
663    if(TYPE STREQUAL kernel)
664        # Kernels are executables with exports
665        set_target_properties(${MODULE}
666            PROPERTIES
667            ENABLE_EXPORTS TRUE
668            DEFINE_SYMBOL "")
669    endif()
670
671    if(${TYPE} STREQUAL win32ocx)
672        set_target_properties(${MODULE} PROPERTIES SUFFIX ".ocx")
673    endif()
674
675    if(${TYPE} STREQUAL cpl)
676        set_target_properties(${MODULE} PROPERTIES SUFFIX ".cpl")
677    endif()
678
679    # Do compiler specific stuff
680    set_module_type_toolchain(${MODULE} ${TYPE})
681endfunction()
682
683function(start_module_group __name)
684    if(DEFINED CURRENT_MODULE_GROUP)
685        message(FATAL_ERROR "CURRENT_MODULE_GROUP is already set ('${CURRENT_MODULE_GROUP}')")
686    endif()
687    set(CURRENT_MODULE_GROUP ${__name} PARENT_SCOPE)
688endfunction()
689
690function(end_module_group)
691    get_property(__modulelist GLOBAL PROPERTY ${CURRENT_MODULE_GROUP}_MODULE_LIST)
692    add_custom_target(${CURRENT_MODULE_GROUP})
693    foreach(__module ${__modulelist})
694        add_dependencies(${CURRENT_MODULE_GROUP} ${__module})
695    endforeach()
696    set(CURRENT_MODULE_GROUP PARENT_SCOPE)
697endfunction()
698
699function(utf16le_convert _in _out)
700    add_custom_command(OUTPUT "${_out}"
701                       COMMAND native-utf16le "${_in}" "${_out}" ${ARGN}
702                       DEPENDS native-utf16le "${_in}")
703    set_source_files_properties("${_out}" PROPERTIES GENERATED TRUE)
704endfunction()
705
706function(preprocess_file __in __out)
707    set(__arg ${__in})
708    foreach(__def ${ARGN})
709        list(APPEND __arg -D${__def})
710    endforeach()
711    if(MSVC)
712        add_custom_command(OUTPUT ${_out}
713            COMMAND ${CMAKE_C_COMPILER} /EP ${__arg}
714            DEPENDS ${__in})
715    else()
716        add_custom_command(OUTPUT ${_out}
717            COMMAND ${CMAKE_C_COMPILER} -E ${__arg}
718            DEPENDS ${__in})
719    endif()
720endfunction()
721
722function(get_includes OUTPUT_VAR)
723    get_directory_property(_includes INCLUDE_DIRECTORIES)
724    foreach(arg ${_includes})
725        list(APPEND __tmp_var -I${arg})
726    endforeach()
727    set(${OUTPUT_VAR} ${__tmp_var} PARENT_SCOPE)
728endfunction()
729
730function(get_defines OUTPUT_VAR)
731    get_directory_property(_defines COMPILE_DEFINITIONS)
732    foreach(arg ${_defines})
733        # Skip generator expressions
734        if (NOT arg MATCHES [[^\$<.*>$]])
735            list(APPEND __tmp_var -D${arg})
736        endif()
737    endforeach()
738    set(${OUTPUT_VAR} ${__tmp_var} PARENT_SCOPE)
739endfunction()
740
741function(add_registry_inf)
742    # Add to the inf files list
743    foreach(_file ${ARGN})
744        set(_source_file "${CMAKE_CURRENT_SOURCE_DIR}/${_file}")
745        set_property(GLOBAL APPEND PROPERTY REGISTRY_INF_LIST ${_source_file})
746    endforeach()
747endfunction()
748
749function(create_registry_hives)
750
751    # Shortcut to the registry.inf file
752    set(_registry_inf "${CMAKE_BINARY_DIR}/boot/bootdata/registry.inf")
753
754    # Get the list of inf files
755    get_property(_inf_files GLOBAL PROPERTY REGISTRY_INF_LIST)
756
757    # Convert files to utf16le
758    foreach(_file ${_inf_files})
759        get_filename_component(_file_name ${_file} NAME_WE)
760        file(RELATIVE_PATH _subdir ${CMAKE_SOURCE_DIR} ${_file})
761        get_filename_component(_subdir ${_subdir}  DIRECTORY)
762        set(_converted_file ${CMAKE_BINARY_DIR}/${_subdir}/${_file_name}_utf16.inf)
763        utf16le_convert(${_file} ${_converted_file})
764        list(APPEND _converted_files ${_converted_file})
765    endforeach()
766
767    # Concatenate all registry files to registry.inf
768    concatenate_files(${_registry_inf} ${_converted_files})
769
770    # Add registry.inf to bootcd
771    add_custom_target(registry_inf DEPENDS ${_registry_inf})
772    add_cd_file(TARGET registry_inf
773                FILE ${_registry_inf}
774                DESTINATION reactos
775                NO_CAB
776                FOR bootcd regtest)
777
778    # BootCD setup system hive
779    add_custom_command(
780        OUTPUT ${CMAKE_BINARY_DIR}/boot/bootdata/SETUPREG.HIV
781        COMMAND native-mkhive -h:SETUPREG -u -d:${CMAKE_BINARY_DIR}/boot/bootdata ${_registry_inf} ${CMAKE_SOURCE_DIR}/boot/bootdata/setupreg.inf
782        DEPENDS native-mkhive ${_registry_inf})
783
784    add_custom_target(bootcd_hives
785        DEPENDS ${CMAKE_BINARY_DIR}/boot/bootdata/SETUPREG.HIV)
786
787    add_cd_file(
788        FILE ${CMAKE_BINARY_DIR}/boot/bootdata/SETUPREG.HIV
789        TARGET bootcd_hives
790        DESTINATION reactos
791        NO_CAB
792        FOR bootcd regtest)
793
794    # LiveCD hives
795    list(APPEND _livecd_inf_files
796        ${_registry_inf}
797        ${CMAKE_SOURCE_DIR}/boot/bootdata/livecd.inf
798        ${CMAKE_SOURCE_DIR}/boot/bootdata/caroots.inf)
799    if(SARCH STREQUAL "xbox")
800        list(APPEND _livecd_inf_files
801            ${CMAKE_SOURCE_DIR}/boot/bootdata/hiveinst_xbox.inf)
802    elseif(SARCH STREQUAL "pc98")
803        list(APPEND _livecd_inf_files
804            ${CMAKE_SOURCE_DIR}/boot/bootdata/hiveinst_pc98.inf)
805    else()
806        list(APPEND _livecd_inf_files
807            ${CMAKE_SOURCE_DIR}/boot/bootdata/hiveinst.inf)
808    endif()
809
810    add_custom_command(
811        OUTPUT ${CMAKE_BINARY_DIR}/boot/bootdata/system
812               ${CMAKE_BINARY_DIR}/boot/bootdata/software
813               ${CMAKE_BINARY_DIR}/boot/bootdata/default
814               ${CMAKE_BINARY_DIR}/boot/bootdata/sam
815               ${CMAKE_BINARY_DIR}/boot/bootdata/security
816        COMMAND native-mkhive -h:SYSTEM,SOFTWARE,DEFAULT,SAM,SECURITY -d:${CMAKE_BINARY_DIR}/boot/bootdata ${_livecd_inf_files}
817        DEPENDS native-mkhive ${_livecd_inf_files})
818
819    add_custom_target(livecd_hives
820        DEPENDS ${CMAKE_BINARY_DIR}/boot/bootdata/system
821                ${CMAKE_BINARY_DIR}/boot/bootdata/software
822                ${CMAKE_BINARY_DIR}/boot/bootdata/default
823                ${CMAKE_BINARY_DIR}/boot/bootdata/sam
824                ${CMAKE_BINARY_DIR}/boot/bootdata/security)
825
826    add_cd_file(
827        FILE ${CMAKE_BINARY_DIR}/boot/bootdata/system
828             ${CMAKE_BINARY_DIR}/boot/bootdata/software
829             ${CMAKE_BINARY_DIR}/boot/bootdata/default
830             ${CMAKE_BINARY_DIR}/boot/bootdata/sam
831             ${CMAKE_BINARY_DIR}/boot/bootdata/security
832        TARGET livecd_hives
833        DESTINATION reactos/system32/config
834        FOR livecd)
835
836    # BCD Hive
837    add_custom_command(
838        OUTPUT ${CMAKE_BINARY_DIR}/boot/bootdata/BCD
839        COMMAND native-mkhive -h:BCD -u -d:${CMAKE_BINARY_DIR}/boot/bootdata ${CMAKE_BINARY_DIR}/boot/bootdata/hivebcd_utf16.inf
840        DEPENDS native-mkhive ${CMAKE_BINARY_DIR}/boot/bootdata/hivebcd_utf16.inf)
841
842    add_custom_target(bcd_hive
843        DEPENDS ${CMAKE_BINARY_DIR}/boot/bootdata/BCD)
844
845    add_cd_file(
846        FILE ${CMAKE_BINARY_DIR}/boot/bootdata/BCD
847        TARGET bcd_hive
848        DESTINATION efi/boot
849        NO_CAB
850        FOR bootcd regtest livecd)
851
852endfunction()
853
854function(add_driver_inf _module)
855    # Add to the inf files list
856    foreach(_file ${ARGN})
857        set(_converted_item ${CMAKE_CURRENT_BINARY_DIR}/${_file})
858        set(_source_item ${CMAKE_CURRENT_SOURCE_DIR}/${_file})
859        utf16le_convert(${_source_item} ${_converted_item})
860        list(APPEND _converted_inf_files ${_converted_item})
861    endforeach()
862
863    add_custom_target(${_module}_inf_files DEPENDS ${_converted_inf_files})
864    add_cd_file(FILE ${_converted_inf_files} TARGET ${_module}_inf_files DESTINATION reactos/inf FOR all)
865endfunction()
866
867if(KDBG)
868    set(ROSSYM_LIB "rossym")
869else()
870    set(ROSSYM_LIB "")
871endif()
872
873function(add_rc_deps _target_rc)
874    set_source_files_properties(${_target_rc} PROPERTIES OBJECT_DEPENDS "${ARGN}")
875endfunction()
876
877add_custom_target(rostests_install COMMAND ${CMAKE_COMMAND} -DCOMPONENT=rostests -P ${CMAKE_BINARY_DIR}/cmake_install.cmake)
878function(add_rostests_file)
879    cmake_parse_arguments(_ROSTESTS "" "RENAME;SUBDIR;TARGET" "FILE" ${ARGN})
880    if(NOT (_ROSTESTS_TARGET OR _ROSTESTS_FILE))
881        message(FATAL_ERROR "You must provide a target or a file to install!")
882    endif()
883
884    set(_ROSTESTS_NAME_ON_CD "${_ROSTESTS_RENAME}")
885    if(NOT _ROSTESTS_FILE)
886        set(_ROSTESTS_FILE "$<TARGET_FILE:${_ROSTESTS_TARGET}>")
887        if(NOT _ROSTESTS_RENAME)
888            set(_ROSTESTS_NAME_ON_CD "$<TARGET_FILE_NAME:${_ROSTESTS_TARGET}>")
889        endif()
890    else()
891        if(NOT _ROSTESTS_RENAME)
892            get_filename_component(_ROSTESTS_NAME_ON_CD ${_ROSTESTS_FILE} NAME)
893        endif()
894    endif()
895
896    if(_ROSTESTS_SUBDIR)
897        set(_ROSTESTS_SUBDIR "/${_ROSTESTS_SUBDIR}")
898    endif()
899
900    if(_ROSTESTS_TARGET)
901        add_cd_file(TARGET ${_ROSTESTS_TARGET} FILE ${_ROSTESTS_FILE} DESTINATION "reactos/bin${_ROSTESTS_SUBDIR}" NAME_ON_CD ${_ROSTESTS_NAME_ON_CD} FOR all)
902    else()
903        add_cd_file(FILE ${_ROSTESTS_FILE} DESTINATION "reactos/bin${_ROSTESTS_SUBDIR}" NAME_ON_CD ${_ROSTESTS_NAME_ON_CD} FOR all)
904    endif()
905
906    if(DEFINED ENV{ROSTESTS_INSTALL})
907        if(_ROSTESTS_RENAME)
908            install(FILES ${_ROSTESTS_FILE} DESTINATION "$ENV{ROSTESTS_INSTALL}${_ROSTESTS_SUBDIR}" COMPONENT rostests RENAME ${_ROSTESTS_RENAME})
909        else()
910            install(FILES ${_ROSTESTS_FILE} DESTINATION "$ENV{ROSTESTS_INSTALL}${_ROSTESTS_SUBDIR}" COMPONENT rostests)
911        endif()
912    endif()
913endfunction()
914
915if(PCH)
916    macro(add_pch _target _pch _skip_list)
917        target_precompile_headers(${_target} PRIVATE ${_pch})
918        set_source_files_properties(${_skip_list} PROPERTIES SKIP_PRECOMPILE_HEADERS ON)
919    endmacro()
920else()
921    macro(add_pch _target _pch _skip_list)
922    endmacro()
923endif()
924
925function(set_target_cpp_properties _target)
926    cmake_parse_arguments(_CPP "WITH_EXCEPTIONS;WITH_RTTI" "" "" ${ARGN})
927
928    if (_CPP_WITH_EXCEPTIONS)
929        set_target_properties(${_target} PROPERTIES WITH_CXX_EXCEPTIONS TRUE)
930    endif()
931
932    if (_CPP_WITH_RTTI)
933        set_target_properties(${_target} PROPERTIES WITH_CXX_RTTI TRUE)
934    endif()
935endfunction()
936