xref: /reactos/sdk/cmake/CMakeMacros.cmake (revision 64daf542)
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.
47macro(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)
81endmacro()
82
83function(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()
90endfunction()
91
92function(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()
99endfunction()
100
101function(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")
103endfunction()
104
105function(add_dependency_footer)
106    add_dependency_node(ntdll)
107    file(APPEND ${REACTOS_BINARY_DIR}/dependencies.graphml "  </graph>\n</graphml>\n")
108endfunction()
109
110function(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()
134endfunction()
135
136function(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)
163endfunction()
164
165macro(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/bin/suppl)
183        set(${var} 80)
184    elseif(${dir} STREQUAL reactos/media)
185        set(${var} 9)
186    elseif(${dir} STREQUAL reactos/Microsoft.NET)
187        set(${var} 10)
188    elseif(${dir} STREQUAL reactos/Microsoft.NET/Framework)
189        set(${var} 11)
190    elseif(${dir} STREQUAL reactos/Microsoft.NET/Framework/v1.0.3705)
191        set(${var} 12)
192    elseif(${dir} STREQUAL reactos/Microsoft.NET/Framework/v1.1.4322)
193        set(${var} 13)
194    elseif(${dir} STREQUAL reactos/Microsoft.NET/Framework/v2.0.50727)
195        set(${var} 14)
196    elseif(${dir} STREQUAL reactos/Resources)
197        set(${var} 15)
198    elseif(${dir} STREQUAL reactos/Resources/Themes)
199        set(${var} 16)
200    elseif(${dir} STREQUAL reactos/system32/wbem)
201        set(${var} 17)
202    elseif(${dir} STREQUAL reactos/Resources/Themes/Lautus)
203        set(${var} 18)
204    elseif(${dir} STREQUAL reactos/Help)
205        set(${var} 19)
206    elseif(${dir} STREQUAL reactos/Config)
207        set(${var} 20)
208    elseif(${dir} STREQUAL reactos/Cursors)
209        set(${var} 21)
210    elseif(${dir} STREQUAL reactos/system32/ShellExt)
211        set(${var} 22)
212    elseif(${dir} STREQUAL reactos/Temp)
213        set(${var} 23)
214    elseif(${dir} STREQUAL reactos/system32/spool)
215        set(${var} 24)
216    elseif(${dir} STREQUAL reactos/system32/spool/drivers)
217        set(${var} 25)
218    elseif(${dir} STREQUAL reactos/system32/spool/drivers/color)
219        set(${var} 26)
220    elseif(${dir} STREQUAL reactos/system32/spool/drivers/w32x86)
221        set(${var} 27)
222    elseif(${dir} STREQUAL reactos/system32/spool/drivers/w32x86/3)
223        set(${var} 28)
224    elseif(${dir} STREQUAL reactos/system32/spool/prtprocs)
225        set(${var} 29)
226    elseif(${dir} STREQUAL reactos/system32/spool/prtprocs/w32x86)
227        set(${var} 30)
228    elseif(${dir} STREQUAL reactos/system32/spool/PRINTERS)
229        set(${var} 31)
230    elseif(${dir} STREQUAL reactos/system32/wbem/Repository)
231        set(${var} 32)
232    elseif(${dir} STREQUAL reactos/system32/wbem/Repository/FS)
233        set(${var} 33)
234    elseif(${dir} STREQUAL reactos/system32/wbem/mof/good)
235        set(${var} 34)
236    elseif(${dir} STREQUAL reactos/system32/wbem/mof/bad)
237        set(${var} 35)
238    elseif(${dir} STREQUAL reactos/system32/wbem/AdStatus)
239        set(${var} 36)
240    elseif(${dir} STREQUAL reactos/system32/wbem/xml)
241        set(${var} 37)
242    elseif(${dir} STREQUAL reactos/system32/wbem/Logs)
243        set(${var} 38)
244    elseif(${dir} STREQUAL reactos/system32/wbem/AutoRecover)
245        set(${var} 39)
246    elseif(${dir} STREQUAL reactos/system32/wbem/snmp)
247        set(${var} 40)
248    elseif(${dir} STREQUAL reactos/system32/wbem/Performance)
249        set(${var} 41)
250    elseif(${dir} STREQUAL reactos/twain_32)
251        set(${var} 42)
252    elseif(${dir} STREQUAL reactos/repair)
253        set(${var} 43)
254    elseif(${dir} STREQUAL reactos/Web)
255        set(${var} 44)
256    elseif(${dir} STREQUAL reactos/Web/Wallpaper)
257        set(${var} 45)
258    elseif(${dir} STREQUAL reactos/Prefetch)
259        set(${var} 46)
260    elseif(${dir} STREQUAL reactos/security)
261        set(${var} 47)
262    elseif(${dir} STREQUAL reactos/security/Database)
263        set(${var} 48)
264    elseif(${dir} STREQUAL reactos/security/logs)
265        set(${var} 49)
266    elseif(${dir} STREQUAL reactos/security/templates)
267        set(${var} 50)
268    elseif(${dir} STREQUAL reactos/system32/CatRoot)
269        set(${var} 51)
270    elseif(${dir} STREQUAL reactos/system32/CatRoot2)
271        set(${var} 52)
272    elseif(${dir} STREQUAL reactos/AppPatch)
273        set(${var} 53)
274    elseif(${dir} STREQUAL reactos/winsxs)
275        set(${var} 54)
276    elseif(${dir} STREQUAL reactos/winsxs/manifests)
277        set(${var} 55)
278    elseif(${dir} STREQUAL reactos/winsxs/x86_microsoft.windows.common-controls_6595b64144ccf1df_5.82.2600.2982_none_deadbeef)
279        set(${var} 56)
280    elseif(${dir} STREQUAL reactos/winsxs/x86_microsoft.windows.common-controls_6595b64144ccf1df_6.0.2600.2982_none_deadbeef)
281        set(${var} 57)
282    else()
283        message(FATAL_ERROR "Wrong destination: ${dir}")
284    endif()
285endmacro()
286
287function(add_cd_file)
288    cmake_parse_arguments(_CD "NO_CAB;NOT_IN_HYBRIDCD" "DESTINATION;NAME_ON_CD;TARGET" "FILE;FOR" ${ARGN})
289    if(NOT (_CD_TARGET OR _CD_FILE))
290        message(FATAL_ERROR "You must provide a target or a file to install!")
291    endif()
292
293    if(NOT _CD_DESTINATION)
294        message(FATAL_ERROR "You must provide a destination")
295    elseif(${_CD_DESTINATION} STREQUAL root)
296        set(_CD_DESTINATION "")
297    endif()
298
299    if(NOT _CD_FOR)
300        message(FATAL_ERROR "You must provide a cd name (or \"all\" for all of them) to install the file on!")
301    endif()
302
303    # get file if we need to
304    if(NOT _CD_FILE)
305        get_target_property(_CD_FILE ${_CD_TARGET} LOCATION_${CMAKE_BUILD_TYPE})
306    endif()
307
308    # do we add it to all CDs?
309    list(FIND _CD_FOR all __cd)
310    if(NOT __cd EQUAL -1)
311        list(REMOVE_AT _CD_FOR __cd)
312        list(INSERT _CD_FOR __cd "bootcd;livecd;regtest")
313    endif()
314
315    # do we add it to bootcd?
316    list(FIND _CD_FOR bootcd __cd)
317    if(NOT __cd EQUAL -1)
318        # whether or not we should put it in reactos.cab or directly on cd
319        if(_CD_NO_CAB)
320            # directly on cd
321            foreach(item ${_CD_FILE})
322                if(_CD_NAME_ON_CD)
323                    # rename it in the cd tree
324                    set(__file ${_CD_NAME_ON_CD})
325                else()
326                    get_filename_component(__file ${item} NAME)
327                endif()
328                set_property(GLOBAL APPEND PROPERTY BOOTCD_FILE_LIST "${_CD_DESTINATION}/${__file}=${item}")
329                # add it also into the hybridcd if not specified otherwise
330                if(NOT _CD_NOT_IN_HYBRIDCD)
331                    set_property(GLOBAL APPEND PROPERTY HYBRIDCD_FILE_LIST "bootcd/${_CD_DESTINATION}/${__file}=${item}")
332                endif()
333            endforeach()
334            # manage dependency
335            if(_CD_TARGET)
336                add_dependencies(bootcd ${_CD_TARGET} registry_inf)
337            endif()
338        else()
339            # add it in reactos.cab
340            dir_to_num(${_CD_DESTINATION} _num)
341            file(RELATIVE_PATH __relative_file ${REACTOS_SOURCE_DIR} ${_CD_FILE})
342            file(APPEND ${REACTOS_BINARY_DIR}/boot/bootdata/packages/reactos.dff.dyn "\"${__relative_file}\" ${_num}\n")
343            unset(__relative_file)
344            # manage dependency - target level
345            if(_CD_TARGET)
346                add_dependencies(reactos_cab_inf ${_CD_TARGET})
347            endif()
348            # manage dependency - file level
349            set_property(GLOBAL APPEND PROPERTY REACTOS_CAB_DEPENDS ${_CD_FILE})
350        endif()
351    endif() #end bootcd
352
353    # do we add it to livecd?
354    list(FIND _CD_FOR livecd __cd)
355    if(NOT __cd EQUAL -1)
356        # manage dependency
357        if(_CD_TARGET)
358            add_dependencies(livecd ${_CD_TARGET} registry_inf)
359        endif()
360        foreach(item ${_CD_FILE})
361            if(_CD_NAME_ON_CD)
362                # rename it in the cd tree
363                set(__file ${_CD_NAME_ON_CD})
364            else()
365                get_filename_component(__file ${item} NAME)
366            endif()
367            set_property(GLOBAL APPEND PROPERTY LIVECD_FILE_LIST "${_CD_DESTINATION}/${__file}=${item}")
368            # add it also into the hybridcd if not specified otherwise
369            if(NOT _CD_NOT_IN_HYBRIDCD)
370                set_property(GLOBAL APPEND PROPERTY HYBRIDCD_FILE_LIST "livecd/${_CD_DESTINATION}/${__file}=${item}")
371            endif()
372        endforeach()
373    endif() #end livecd
374
375    # do we need also to add it to hybridcd?
376    list(FIND _CD_FOR hybridcd __cd)
377    if(NOT __cd EQUAL -1)
378        # manage dependency
379        if(_CD_TARGET)
380            add_dependencies(hybridcd ${_CD_TARGET})
381        endif()
382        foreach(item ${_CD_FILE})
383            if(_CD_NAME_ON_CD)
384                # rename it in the cd tree
385                set(__file ${_CD_NAME_ON_CD})
386            else()
387                get_filename_component(__file ${item} NAME)
388            endif()
389            set_property(GLOBAL APPEND PROPERTY HYBRIDCD_FILE_LIST "${_CD_DESTINATION}/${__file}=${item}")
390        endforeach()
391    endif() #end hybridcd
392
393    # do we add it to regtest?
394    list(FIND _CD_FOR regtest __cd)
395    if(NOT __cd EQUAL -1)
396        # whether or not we should put it in reactos.cab or directly on cd
397        if(_CD_NO_CAB)
398            # directly on cd
399            foreach(item ${_CD_FILE})
400                if(_CD_NAME_ON_CD)
401                    # rename it in the cd tree
402                    set(__file ${_CD_NAME_ON_CD})
403                else()
404                    get_filename_component(__file ${item} NAME)
405                endif()
406                set_property(GLOBAL APPEND PROPERTY BOOTCDREGTEST_FILE_LIST "${_CD_DESTINATION}/${__file}=${item}")
407            endforeach()
408            # manage dependency
409            if(_CD_TARGET)
410                add_dependencies(bootcdregtest ${_CD_TARGET} registry_inf)
411            endif()
412        else()
413            #add it in reactos.cab
414            #dir_to_num(${_CD_DESTINATION} _num)
415            #file(APPEND ${REACTOS_BINARY_DIR}/boot/bootdata/packages/reactos.dff.dyn "${_CD_FILE} ${_num}\n")
416            #if(_CD_TARGET)
417            #    #manage dependency
418            #    add_dependencies(reactos_cab ${_CD_TARGET})
419            #endif()
420        endif()
421    endif() #end bootcd
422endfunction()
423
424function(create_iso_lists)
425    # generate reactos.cab before anything else
426    get_property(_filelist GLOBAL PROPERTY REACTOS_CAB_DEPENDS)
427
428    # begin with reactos.inf. We want this command to be always executed, so we pretend it generates another file although it will never do.
429    add_custom_command(
430        OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/reactos.inf ${CMAKE_CURRENT_BINARY_DIR}/__some_non_existent_file
431        COMMAND ${CMAKE_COMMAND} -E copy_if_different ${REACTOS_BINARY_DIR}/boot/bootdata/packages/reactos.inf ${CMAKE_CURRENT_BINARY_DIR}/reactos.inf
432        DEPENDS ${REACTOS_BINARY_DIR}/boot/bootdata/packages/reactos.inf reactos_cab_inf)
433
434    add_custom_command(
435        OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/reactos.cab
436        COMMAND native-cabman -C ${REACTOS_BINARY_DIR}/boot/bootdata/packages/reactos.dff -RC ${CMAKE_CURRENT_BINARY_DIR}/reactos.inf -N -P ${REACTOS_SOURCE_DIR}
437        DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/reactos.inf native-cabman ${_filelist})
438
439    add_custom_target(reactos_cab DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/reactos.cab)
440    add_dependencies(reactos_cab reactos_cab_inf)
441
442    add_cd_file(
443        TARGET reactos_cab
444        FILE ${CMAKE_CURRENT_BINARY_DIR}/reactos.cab
445        DESTINATION reactos
446        NO_CAB FOR bootcd regtest)
447
448    add_cd_file(
449        FILE ${CMAKE_CURRENT_BINARY_DIR}/livecd.iso
450        DESTINATION livecd
451        FOR hybridcd)
452
453    get_property(_filelist GLOBAL PROPERTY BOOTCD_FILE_LIST)
454    string(REPLACE ";" "\n" _filelist "${_filelist}")
455    file(APPEND ${REACTOS_BINARY_DIR}/boot/bootcd.lst "${_filelist}")
456    unset(_filelist)
457
458    get_property(_filelist GLOBAL PROPERTY LIVECD_FILE_LIST)
459    string(REPLACE ";" "\n" _filelist "${_filelist}")
460    file(APPEND ${REACTOS_BINARY_DIR}/boot/livecd.lst "${_filelist}")
461    unset(_filelist)
462
463    get_property(_filelist GLOBAL PROPERTY HYBRIDCD_FILE_LIST)
464    string(REPLACE ";" "\n" _filelist "${_filelist}")
465    file(APPEND ${REACTOS_BINARY_DIR}/boot/hybridcd.lst "${_filelist}")
466    unset(_filelist)
467
468    get_property(_filelist GLOBAL PROPERTY BOOTCDREGTEST_FILE_LIST)
469    string(REPLACE ";" "\n" _filelist "${_filelist}")
470    file(APPEND ${REACTOS_BINARY_DIR}/boot/bootcdregtest.lst "${_filelist}")
471    unset(_filelist)
472endfunction()
473
474# Create module_clean targets
475function(add_clean_target _target)
476    set(_clean_working_directory ${CMAKE_CURRENT_BINARY_DIR})
477    if(CMAKE_GENERATOR STREQUAL "Unix Makefiles" OR CMAKE_GENERATOR STREQUAL "MinGW Makefiles")
478        set(_clean_command make clean)
479    elseif(CMAKE_GENERATOR STREQUAL "NMake Makefiles")
480        set(_clean_command nmake /nologo clean)
481    elseif(CMAKE_GENERATOR STREQUAL "Ninja")
482        set(_clean_command ninja -t clean ${_target})
483        set(_clean_working_directory ${REACTOS_BINARY_DIR})
484    endif()
485    add_custom_target(${_target}_clean
486        COMMAND ${_clean_command}
487        WORKING_DIRECTORY ${_clean_working_directory}
488        COMMENT "Cleaning ${_target}")
489endfunction()
490
491if(NOT MSVC_IDE)
492    function(add_library name)
493        _add_library(${name} ${ARGN})
494        add_clean_target(${name})
495    endfunction()
496
497    function(add_executable name)
498        _add_executable(${name} ${ARGN})
499        add_clean_target(${name})
500    endfunction()
501elseif(USE_FOLDER_STRUCTURE)
502    set_property(GLOBAL PROPERTY USE_FOLDERS ON)
503    string(LENGTH ${CMAKE_SOURCE_DIR} CMAKE_SOURCE_DIR_LENGTH)
504
505    function(add_custom_target name)
506        _add_custom_target(${name} ${ARGN})
507        string(SUBSTRING ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_SOURCE_DIR_LENGTH} -1 CMAKE_CURRENT_SOURCE_DIR_RELATIVE)
508        set_property(TARGET "${name}" PROPERTY FOLDER "${CMAKE_CURRENT_SOURCE_DIR_RELATIVE}")
509    endfunction()
510
511    function(add_library name)
512        _add_library(${name} ${ARGN})
513        get_target_property(_target_excluded ${name} EXCLUDE_FROM_ALL)
514        if(_target_excluded AND ${name} MATCHES "^lib.*")
515            set_property(TARGET "${name}" PROPERTY FOLDER "Importlibs")
516        else()
517            string(SUBSTRING ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_SOURCE_DIR_LENGTH} -1 CMAKE_CURRENT_SOURCE_DIR_RELATIVE)
518            set_property(TARGET "${name}" PROPERTY FOLDER "${CMAKE_CURRENT_SOURCE_DIR_RELATIVE}")
519        endif()
520    endfunction()
521
522    function(add_executable name)
523        _add_executable(${name} ${ARGN})
524        string(SUBSTRING ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_SOURCE_DIR_LENGTH} -1 CMAKE_CURRENT_SOURCE_DIR_RELATIVE)
525        set_property(TARGET "${name}" PROPERTY FOLDER "${CMAKE_CURRENT_SOURCE_DIR_RELATIVE}")
526    endfunction()
527endif()
528
529if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
530    function(concatenate_files _output _file1)
531        file(TO_NATIVE_PATH "${_output}" _real_output)
532        file(TO_NATIVE_PATH "${_file1}" _file_list)
533        foreach(_file ${ARGN})
534            file(TO_NATIVE_PATH "${_file}" _real_file)
535            set(_file_list "${_file_list} + ${_real_file}")
536        endforeach()
537        add_custom_command(
538            OUTPUT ${_output}
539            COMMAND cmd.exe /C "copy /Y /B ${_file_list} ${_real_output} > nul"
540            DEPENDS ${_file1} ${ARGN})
541    endfunction()
542else()
543    macro(concatenate_files _output)
544        add_custom_command(
545            OUTPUT ${_output}
546            COMMAND cat ${ARGN} > ${_output}
547            DEPENDS ${ARGN})
548    endmacro()
549endif()
550
551function(add_importlibs _module)
552    add_dependency_node(${_module})
553    foreach(LIB ${ARGN})
554        if("${LIB}" MATCHES "msvcrt")
555            add_target_compile_definitions(${_module} _DLL __USE_CRTIMP)
556            target_link_libraries(${_module} msvcrtex)
557        endif()
558        target_link_libraries(${_module} lib${LIB})
559        add_dependencies(${_module} lib${LIB})
560        add_dependency_edge(${_module} ${LIB})
561    endforeach()
562endfunction()
563
564function(set_module_type MODULE TYPE)
565    cmake_parse_arguments(__module "UNICODE" "IMAGEBASE" "ENTRYPOINT" ${ARGN})
566
567    if(__module_UNPARSED_ARGUMENTS)
568        message(STATUS "set_module_type : unparsed arguments ${__module_UNPARSED_ARGUMENTS}, module : ${MODULE}")
569    endif()
570
571    # Add the module to the module group list, if it is defined
572    if(DEFINED CURRENT_MODULE_GROUP)
573        set_property(GLOBAL APPEND PROPERTY ${CURRENT_MODULE_GROUP}_MODULE_LIST "${MODULE}")
574    endif()
575
576    # Set subsystem. Also take this as an occasion
577    # to error out if someone gave a non existing type
578    if((${TYPE} STREQUAL nativecui) OR (${TYPE} STREQUAL nativedll)
579            OR (${TYPE} STREQUAL kernelmodedriver) OR (${TYPE} STREQUAL wdmdriver) OR (${TYPE} STREQUAL kerneldll))
580        set(__subsystem native)
581    elseif(${TYPE} STREQUAL win32cui)
582        set(__subsystem console)
583    elseif(${TYPE} STREQUAL win32gui)
584        set(__subsystem windows)
585    elseif(NOT ((${TYPE} STREQUAL win32dll) OR (${TYPE} STREQUAL win32ocx)
586            OR (${TYPE} STREQUAL cpl) OR (${TYPE} STREQUAL module)))
587        message(FATAL_ERROR "Unknown type ${TYPE} for module ${MODULE}")
588    endif()
589
590    if(DEFINED __subsystem)
591        set_subsystem(${MODULE} ${__subsystem})
592    endif()
593
594    # Set the PE image version numbers from the NT OS version ReactOS is based on
595    if (MSVC)
596        add_target_link_flags(${MODULE} "/VERSION:5.01")
597    else()
598        add_target_link_flags(${MODULE} "-Wl,--major-image-version,5 -Wl,--minor-image-version,01")
599        add_target_link_flags(${MODULE} "-Wl,--major-os-version,5 -Wl,--minor-os-version,01")
600    endif()
601
602    # Set unicode definitions
603    if(__module_UNICODE)
604        add_target_compile_definitions(${MODULE} UNICODE _UNICODE)
605    endif()
606
607    # Set entry point
608    if(__module_ENTRYPOINT OR (__module_ENTRYPOINT STREQUAL "0"))
609        list(GET __module_ENTRYPOINT 0 __entrypoint)
610        list(LENGTH __module_ENTRYPOINT __length)
611        if(${__length} EQUAL 2)
612            list(GET __module_ENTRYPOINT 1 __entrystack)
613        elseif(NOT ${__length} EQUAL 1)
614            message(FATAL_ERROR "Wrong arguments for ENTRYPOINT parameter of set_module_type : ${__module_ENTRYPOINT}")
615        endif()
616        unset(__length)
617    elseif(${TYPE} STREQUAL nativecui)
618        set(__entrypoint NtProcessStartup)
619        set(__entrystack 4)
620    elseif(${TYPE} STREQUAL win32cui)
621        if(__module_UNICODE)
622            set(__entrypoint wmainCRTStartup)
623        else()
624            set(__entrypoint mainCRTStartup)
625        endif()
626    elseif(${TYPE} STREQUAL win32gui)
627        if(__module_UNICODE)
628            set(__entrypoint wWinMainCRTStartup)
629        else()
630            set(__entrypoint WinMainCRTStartup)
631        endif()
632    elseif((${TYPE} STREQUAL win32dll) OR (${TYPE} STREQUAL win32ocx)
633            OR (${TYPE} STREQUAL cpl))
634        set(__entrypoint DllMainCRTStartup)
635        set(__entrystack 12)
636    elseif((${TYPE} STREQUAL kernelmodedriver) OR (${TYPE} STREQUAL wdmdriver))
637        set(__entrypoint DriverEntry)
638        set(__entrystack 8)
639    elseif(${TYPE} STREQUAL nativedll)
640        set(__entrypoint DllMain)
641        set(__entrystack 12)
642    elseif(${TYPE} STREQUAL module)
643        set(__entrypoint 0)
644    endif()
645
646    if(DEFINED __entrypoint)
647        if(DEFINED __entrystack)
648            set_entrypoint(${MODULE} ${__entrypoint} ${__entrystack})
649        else()
650            set_entrypoint(${MODULE} ${__entrypoint})
651        endif()
652    endif()
653
654    # Set base address
655    if(__module_IMAGEBASE)
656        set_image_base(${MODULE} ${__module_IMAGEBASE})
657    elseif(${TYPE} STREQUAL win32dll)
658        if(DEFINED baseaddress_${MODULE})
659            set_image_base(${MODULE} ${baseaddress_${MODULE}})
660        else()
661            message(STATUS "${MODULE} has no base address")
662        endif()
663    elseif((${TYPE} STREQUAL kernelmodedriver) OR (${TYPE} STREQUAL wdmdriver) OR (${TYPE} STREQUAL kerneldll))
664        set_image_base(${MODULE} 0x00010000)
665    endif()
666
667    # Now do some stuff which is specific to each type
668    if((${TYPE} STREQUAL kernelmodedriver) OR (${TYPE} STREQUAL wdmdriver) OR (${TYPE} STREQUAL kerneldll))
669        add_dependencies(${MODULE} bugcodes xdk)
670        if((${TYPE} STREQUAL kernelmodedriver) OR (${TYPE} STREQUAL wdmdriver))
671            set_target_properties(${MODULE} PROPERTIES SUFFIX ".sys")
672        endif()
673    endif()
674
675    if(${TYPE} STREQUAL win32ocx)
676        set_target_properties(${MODULE} PROPERTIES SUFFIX ".ocx")
677    endif()
678
679    if(${TYPE} STREQUAL cpl)
680        set_target_properties(${MODULE} PROPERTIES SUFFIX ".cpl")
681    endif()
682
683    # Do compiler specific stuff
684    set_module_type_toolchain(${MODULE} ${TYPE})
685endfunction()
686
687function(start_module_group __name)
688    if(DEFINED CURRENT_MODULE_GROUP)
689        message(FATAL_ERROR "CURRENT_MODULE_GROUP is already set ('${CURRENT_MODULE_GROUP}')")
690    endif()
691    set(CURRENT_MODULE_GROUP ${__name} PARENT_SCOPE)
692endfunction()
693
694function(end_module_group)
695    get_property(__modulelist GLOBAL PROPERTY ${CURRENT_MODULE_GROUP}_MODULE_LIST)
696    add_custom_target(${CURRENT_MODULE_GROUP})
697    foreach(__module ${__modulelist})
698        add_dependencies(${CURRENT_MODULE_GROUP} ${__module})
699    endforeach()
700    set(CURRENT_MODULE_GROUP PARENT_SCOPE)
701endfunction()
702
703function(preprocess_file __in __out)
704    set(__arg ${__in})
705    foreach(__def ${ARGN})
706        list(APPEND __arg -D${__def})
707    endforeach()
708    if(MSVC)
709        add_custom_command(OUTPUT ${_out}
710            COMMAND ${CMAKE_C_COMPILER} /EP ${__arg}
711            DEPENDS ${__in})
712    else()
713        add_custom_command(OUTPUT ${_out}
714            COMMAND ${CMAKE_C_COMPILER} -E ${__arg}
715            DEPENDS ${__in})
716    endif()
717endfunction()
718
719function(get_includes OUTPUT_VAR)
720    get_directory_property(_includes INCLUDE_DIRECTORIES)
721    foreach(arg ${_includes})
722        list(APPEND __tmp_var -I${arg})
723    endforeach()
724    set(${OUTPUT_VAR} ${__tmp_var} PARENT_SCOPE)
725endfunction()
726
727function(get_defines OUTPUT_VAR)
728    get_directory_property(_defines COMPILE_DEFINITIONS)
729    foreach(arg ${_defines})
730        list(APPEND __tmp_var -D${arg})
731    endforeach()
732    set(${OUTPUT_VAR} ${__tmp_var} PARENT_SCOPE)
733endfunction()
734
735if(NOT MSVC)
736    function(add_object_library _target)
737        add_library(${_target} OBJECT ${ARGN})
738    endfunction()
739else()
740    function(add_object_library _target)
741        add_library(${_target} ${ARGN})
742    endfunction()
743endif()
744
745function(add_registry_inf)
746    # Add to the inf files list
747    foreach(_file ${ARGN})
748        set(_source_file "${CMAKE_CURRENT_SOURCE_DIR}/${_file}")
749        set_property(GLOBAL APPEND PROPERTY REGISTRY_INF_LIST ${_source_file})
750    endforeach()
751endfunction()
752
753function(create_registry_hives)
754
755    # Shortcut to the registry.inf file
756    set(_registry_inf "${CMAKE_BINARY_DIR}/boot/bootdata/registry.inf")
757
758    # Get the list of inf files
759    get_property(_inf_files GLOBAL PROPERTY REGISTRY_INF_LIST)
760
761    # Convert files to utf16le
762    foreach(_file ${_inf_files})
763        get_filename_component(_file_name ${_file} NAME_WE)
764        string(REPLACE ${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR} _converted_file "${_file}")
765        string(REPLACE ${_file_name} "${_file_name}_utf16" _converted_file ${_converted_file})
766        add_custom_command(OUTPUT ${_converted_file}
767                           COMMAND native-utf16le ${_file} ${_converted_file}
768                           DEPENDS native-utf16le ${_file})
769        list(APPEND _converted_files ${_converted_file})
770    endforeach()
771
772    # Concatenate all registry files to registry.inf
773    concatenate_files(${_registry_inf} ${_converted_files})
774
775    # Add registry.inf to bootcd
776    add_custom_target(registry_inf DEPENDS ${_registry_inf})
777    add_cd_file(TARGET registry_inf
778                FILE ${_registry_inf}
779                DESTINATION reactos
780                NO_CAB
781                FOR bootcd regtest)
782
783    # livecd hives
784    list(APPEND _livecd_inf_files
785        ${_registry_inf}
786        ${CMAKE_SOURCE_DIR}/boot/bootdata/livecd.inf
787        ${CMAKE_SOURCE_DIR}/boot/bootdata/hiveinst.inf)
788
789    add_custom_command(
790        OUTPUT ${CMAKE_BINARY_DIR}/boot/bootdata/sam
791            ${CMAKE_BINARY_DIR}/boot/bootdata/default
792            ${CMAKE_BINARY_DIR}/boot/bootdata/security
793            ${CMAKE_BINARY_DIR}/boot/bootdata/software
794            ${CMAKE_BINARY_DIR}/boot/bootdata/system
795            ${CMAKE_BINARY_DIR}/boot/bootdata/BCD
796        COMMAND native-mkhive ${CMAKE_BINARY_DIR}/boot/bootdata ${_livecd_inf_files}
797        DEPENDS native-mkhive ${_livecd_inf_files})
798
799    add_custom_target(livecd_hives
800        DEPENDS ${CMAKE_BINARY_DIR}/boot/bootdata/sam
801            ${CMAKE_BINARY_DIR}/boot/bootdata/default
802            ${CMAKE_BINARY_DIR}/boot/bootdata/security
803            ${CMAKE_BINARY_DIR}/boot/bootdata/software
804            ${CMAKE_BINARY_DIR}/boot/bootdata/system
805            ${CMAKE_BINARY_DIR}/boot/bootdata/BCD)
806
807    add_cd_file(
808        FILE ${CMAKE_BINARY_DIR}/boot/bootdata/sam
809            ${CMAKE_BINARY_DIR}/boot/bootdata/default
810            ${CMAKE_BINARY_DIR}/boot/bootdata/security
811            ${CMAKE_BINARY_DIR}/boot/bootdata/software
812            ${CMAKE_BINARY_DIR}/boot/bootdata/system
813        TARGET livecd_hives
814        DESTINATION reactos/system32/config
815        FOR livecd)
816
817    add_cd_file(
818        FILE ${CMAKE_BINARY_DIR}/boot/bootdata/BCD
819        TARGET livecd_hives
820        DESTINATION efi/boot
821        NO_CAB
822        FOR bootcd regtest livecd)
823
824endfunction()
825
826if(KDBG)
827    set(ROSSYM_LIB "rossym")
828else()
829    set(ROSSYM_LIB "")
830endif()
831
832function(add_rc_deps _target_rc)
833    set_source_files_properties(${_target_rc} PROPERTIES OBJECT_DEPENDS "${ARGN}")
834endfunction()
835
836add_custom_target(rostests_install COMMAND ${CMAKE_COMMAND} -DCOMPONENT=rostests -P ${CMAKE_BINARY_DIR}/cmake_install.cmake)
837function(add_rostests_file)
838    cmake_parse_arguments(_ROSTESTS "" "RENAME;SUBDIR;TARGET" "FILE" ${ARGN})
839    if(NOT (_ROSTESTS_TARGET OR _ROSTESTS_FILE))
840        message(FATAL_ERROR "You must provide a target or a file to install!")
841    endif()
842
843    if(NOT _ROSTESTS_FILE)
844        get_target_property(_ROSTESTS_FILE ${_ROSTESTS_TARGET} LOCATION_${CMAKE_BUILD_TYPE})
845    endif()
846
847    if(NOT _ROSTESTS_RENAME)
848        get_filename_component(_ROSTESTS_RENAME ${_ROSTESTS_FILE} NAME)
849    endif()
850
851    if(_ROSTESTS_SUBDIR)
852        set(_ROSTESTS_SUBDIR "/${_ROSTESTS_SUBDIR}")
853    endif()
854
855    if(_ROSTESTS_TARGET)
856        add_cd_file(TARGET ${_ROSTESTS_TARGET} FILE ${_ROSTESTS_FILE} DESTINATION "reactos/bin${_ROSTESTS_SUBDIR}" NAME_ON_CD ${_ROSTESTS_RENAME} FOR all)
857    else()
858        add_cd_file(FILE ${_ROSTESTS_FILE} DESTINATION "reactos/bin${_ROSTESTS_SUBDIR}" NAME_ON_CD ${_ROSTESTS_RENAME} FOR all)
859    endif()
860
861    if(DEFINED ENV{ROSTESTS_INSTALL})
862        install(FILES ${_ROSTESTS_FILE} DESTINATION "$ENV{ROSTESTS_INSTALL}${_ROSTESTS_SUBDIR}" COMPONENT rostests RENAME ${_ROSTESTS_RENAME})
863    endif()
864endfunction()
865