[WIN32K]
[reactos.git] / reactos / 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}/include/c++)
74 include_directories(${REACTOS_SOURCE_DIR}/include/c++/stlport)
75 endif()
76 endif()
77
78 set(IS_CPP 1)
79 endmacro()
80
81 function(add_dependency_node _node)
82 if(GENERATE_DEPENDENCY_GRAPH)
83 get_target_property(_type ${_node} TYPE)
84 if(_type MATCHES SHARED_LIBRARY OR ${_node} MATCHES ntoskrnl)
85 file(APPEND ${REACTOS_BINARY_DIR}/dependencies.graphml " <node id=\"${_node}\"/>\n")
86 endif()
87 endif()
88 endfunction()
89
90 function(add_dependency_edge _source _target)
91 if(GENERATE_DEPENDENCY_GRAPH)
92 get_target_property(_type ${_source} TYPE)
93 if(_type MATCHES SHARED_LIBRARY)
94 file(APPEND ${REACTOS_BINARY_DIR}/dependencies.graphml " <edge source=\"${_source}\" target=\"${_target}\"/>\n")
95 endif()
96 endif()
97 endfunction()
98
99 function(add_dependency_header)
100 file(APPEND ${REACTOS_BINARY_DIR}/dependencies.graphml "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<graphml>\n <graph id=\"ReactOS dependencies\" edgedefault=\"directed\">\n")
101 endfunction()
102
103 function(add_dependency_footer)
104 add_dependency_node(ntdll)
105 file(APPEND ${REACTOS_BINARY_DIR}/dependencies.graphml " </graph>\n</graphml>\n")
106 endfunction()
107
108 function(add_message_headers _type)
109 if(${_type} STREQUAL UNICODE)
110 set(_flag "-U")
111 else()
112 set(_flag "-A")
113 endif()
114 foreach(_in_FILE ${ARGN})
115 get_filename_component(FILE ${_in_FILE} NAME_WE)
116 macro_mc(${_flag} ${FILE})
117 add_custom_command(
118 OUTPUT ${REACTOS_BINARY_DIR}/include/reactos/${FILE}.rc ${REACTOS_BINARY_DIR}/include/reactos/${FILE}.h
119 COMMAND ${COMMAND_MC} ${MC_FLAGS}
120 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${FILE}.mc)
121 set_source_files_properties(
122 ${REACTOS_BINARY_DIR}/include/reactos/${FILE}.h ${REACTOS_BINARY_DIR}/include/reactos/${FILE}.rc
123 PROPERTIES GENERATED TRUE)
124 add_custom_target(${FILE} ALL DEPENDS ${REACTOS_BINARY_DIR}/include/reactos/${FILE}.h ${REACTOS_BINARY_DIR}/include/reactos/${FILE}.rc)
125 endforeach()
126 endfunction()
127
128 function(add_link)
129 cmake_parse_arguments(_LINK "MINIMIZE" "NAME;PATH;CMD_LINE_ARGS;ICON;GUID" "" ${ARGN})
130 if(NOT _LINK_NAME OR NOT _LINK_PATH)
131 message(FATAL_ERROR "You must provide name and path")
132 endif()
133
134 if(_LINK_CMD_LINE_ARGS)
135 set(_LINK_CMD_LINE_ARGS -c ${_LINK_CMD_LINE_ARGS})
136 endif()
137
138 if(_LINK_ICON)
139 set(_LINK_ICON -i ${_LINK_ICON})
140 endif()
141
142 if(_LINK_GUID)
143 set(_LINK_GUID -g ${_LINK_GUID})
144 endif()
145
146 if(_LINK_MINIMIZE)
147 set(_LINK_MINIMIZE "-m")
148 endif()
149
150 add_custom_command(
151 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_LINK_NAME}.lnk
152 COMMAND native-mkshelllink -o ${CMAKE_CURRENT_BINARY_DIR}/${_LINK_NAME}.lnk ${_LINK_CMD_LINE_ARGS} ${_LINK_ICON} ${_LINK_GUID} ${_LINK_MINIMIZE} ${_LINK_PATH}
153 DEPENDS native-mkshelllink)
154 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${_LINK_NAME}.lnk PROPERTIES GENERATED TRUE)
155 endfunction()
156
157 macro(dir_to_num dir var)
158 if(${dir} STREQUAL reactos/system32)
159 set(${var} 1)
160 elseif(${dir} STREQUAL reactos/system32/drivers)
161 set(${var} 2)
162 elseif(${dir} STREQUAL reactos/Fonts)
163 set(${var} 3)
164 elseif(${dir} STREQUAL reactos)
165 set(${var} 4)
166 elseif(${dir} STREQUAL reactos/system32/drivers/etc)
167 set(${var} 5)
168 elseif(${dir} STREQUAL reactos/inf)
169 set(${var} 6)
170 elseif(${dir} STREQUAL reactos/bin)
171 set(${var} 7)
172 elseif(${dir} STREQUAL reactos/bin/data)
173 set(${var} 8)
174 elseif(${dir} STREQUAL reactos/media)
175 set(${var} 9)
176 elseif(${dir} STREQUAL reactos/Microsoft.NET)
177 set(${var} 10)
178 elseif(${dir} STREQUAL reactos/Microsoft.NET/Framework)
179 set(${var} 11)
180 elseif(${dir} STREQUAL reactos/Microsoft.NET/Framework/v1.0.3705)
181 set(${var} 12)
182 elseif(${dir} STREQUAL reactos/Microsoft.NET/Framework/v1.1.4322)
183 set(${var} 13)
184 elseif(${dir} STREQUAL reactos/Microsoft.NET/Framework/v2.0.50727)
185 set(${var} 14)
186 elseif(${dir} STREQUAL reactos/Resources)
187 set(${var} 15)
188 elseif(${dir} STREQUAL reactos/Resources/Themes)
189 set(${var} 16)
190 elseif(${dir} STREQUAL reactos/system32/wbem)
191 set(${var} 17)
192 elseif(${dir} STREQUAL reactos/Resources/Themes/Lautus)
193 set(${var} 18)
194 elseif(${dir} STREQUAL reactos/Help)
195 set(${var} 19)
196 elseif(${dir} STREQUAL reactos/Config)
197 set(${var} 20)
198 elseif(${dir} STREQUAL reactos/Cursors)
199 set(${var} 21)
200 elseif(${dir} STREQUAL reactos/system32/ShellExt)
201 set(${var} 22)
202 elseif(${dir} STREQUAL reactos/Temp)
203 set(${var} 23)
204 elseif(${dir} STREQUAL reactos/system32/spool)
205 set(${var} 24)
206 elseif(${dir} STREQUAL reactos/system32/spool/drivers)
207 set(${var} 25)
208 elseif(${dir} STREQUAL reactos/system32/spool/drivers/color)
209 set(${var} 26)
210 elseif(${dir} STREQUAL reactos/system32/spool/drivers/w32x86)
211 set(${var} 27)
212 elseif(${dir} STREQUAL reactos/system32/spool/drivers/w32x86/3)
213 set(${var} 28)
214 elseif(${dir} STREQUAL reactos/system32/spool/prtprocs)
215 set(${var} 29)
216 elseif(${dir} STREQUAL reactos/system32/spool/prtprocs/w32x86)
217 set(${var} 30)
218 elseif(${dir} STREQUAL reactos/system32/spool/PRINTERS)
219 set(${var} 31)
220 elseif(${dir} STREQUAL reactos/system32/wbem/Repository)
221 set(${var} 32)
222 elseif(${dir} STREQUAL reactos/system32/wbem/Repository/FS)
223 set(${var} 33)
224 elseif(${dir} STREQUAL reactos/system32/wbem/mof/good)
225 set(${var} 34)
226 elseif(${dir} STREQUAL reactos/system32/wbem/mof/bad)
227 set(${var} 35)
228 elseif(${dir} STREQUAL reactos/system32/wbem/AdStatus)
229 set(${var} 36)
230 elseif(${dir} STREQUAL reactos/system32/wbem/xml)
231 set(${var} 37)
232 elseif(${dir} STREQUAL reactos/system32/wbem/Logs)
233 set(${var} 38)
234 elseif(${dir} STREQUAL reactos/system32/wbem/AutoRecover)
235 set(${var} 39)
236 elseif(${dir} STREQUAL reactos/system32/wbem/snmp)
237 set(${var} 40)
238 elseif(${dir} STREQUAL reactos/system32/wbem/Performance)
239 set(${var} 41)
240 elseif(${dir} STREQUAL reactos/twain_32)
241 set(${var} 42)
242 elseif(${dir} STREQUAL reactos/repair)
243 set(${var} 43)
244 elseif(${dir} STREQUAL reactos/Web)
245 set(${var} 44)
246 elseif(${dir} STREQUAL reactos/Web/Wallpaper)
247 set(${var} 45)
248 elseif(${dir} STREQUAL reactos/Prefetch)
249 set(${var} 46)
250 elseif(${dir} STREQUAL reactos/security)
251 set(${var} 47)
252 elseif(${dir} STREQUAL reactos/security/Database)
253 set(${var} 48)
254 elseif(${dir} STREQUAL reactos/security/logs)
255 set(${var} 49)
256 elseif(${dir} STREQUAL reactos/security/templates)
257 set(${var} 50)
258 elseif(${dir} STREQUAL reactos/system32/CatRoot)
259 set(${var} 51)
260 elseif(${dir} STREQUAL reactos/system32/CatRoot2)
261 set(${var} 52)
262 else()
263 message(FATAL_ERROR "Wrong destination: ${dir}")
264 endif()
265 endmacro()
266
267 function(add_cd_file)
268 cmake_parse_arguments(_CD "NO_CAB" "DESTINATION;NAME_ON_CD;TARGET" "FILE;FOR" ${ARGN})
269 if(NOT (_CD_TARGET OR _CD_FILE))
270 message(FATAL_ERROR "You must provide a target or a file to install!")
271 endif()
272
273 if(NOT _CD_DESTINATION)
274 message(FATAL_ERROR "You must provide a destination")
275 elseif(${_CD_DESTINATION} STREQUAL root)
276 set(_CD_DESTINATION "")
277 endif()
278
279 if(NOT _CD_FOR)
280 message(FATAL_ERROR "You must provide a cd name (or \"all\" for all of them) to install the file on!")
281 endif()
282
283 #get file if we need to
284 if(NOT _CD_FILE)
285 get_target_property(_CD_FILE ${_CD_TARGET} LOCATION_${CMAKE_BUILD_TYPE})
286 endif()
287
288 #do we add it to all CDs?
289 if(_CD_FOR STREQUAL all)
290 set(_CD_FOR "bootcd;livecd;regtest")
291 endif()
292
293 #do we add it to bootcd?
294 list(FIND _CD_FOR bootcd __cd)
295 if(NOT __cd EQUAL -1)
296 #whether or not we should put it in reactos.cab or directly on cd
297 if(_CD_NO_CAB)
298 #directly on cd
299 foreach(item ${_CD_FILE})
300 if(_CD_NAME_ON_CD)
301 #rename it in the cd tree
302 set(__file ${_CD_NAME_ON_CD})
303 else()
304 get_filename_component(__file ${item} NAME)
305 endif()
306 set_property(GLOBAL APPEND PROPERTY BOOTCD_FILE_LIST "${_CD_DESTINATION}/${__file}=${item}")
307 endforeach()
308 if(_CD_TARGET)
309 #manage dependency
310 add_dependencies(bootcd ${_CD_TARGET} converted_hives)
311 endif()
312 else()
313 #add it in reactos.cab
314 dir_to_num(${_CD_DESTINATION} _num)
315 file(RELATIVE_PATH __relative_file ${REACTOS_SOURCE_DIR} ${_CD_FILE})
316 file(APPEND ${REACTOS_BINARY_DIR}/boot/bootdata/packages/reactos.dff.dyn "\"${__relative_file}\" ${_num}\n")
317 unset(__relative_file)
318 if(_CD_TARGET)
319 #manage dependency - target level
320 add_dependencies(reactos_cab_inf ${_CD_TARGET})
321 endif()
322 # manage dependency - file level
323 set_property(GLOBAL APPEND PROPERTY REACTOS_CAB_DEPENDS ${_CD_FILE})
324 endif()
325 endif() #end bootcd
326
327 #do we add it to livecd?
328 list(FIND _CD_FOR livecd __cd)
329 if(NOT __cd EQUAL -1)
330 #manage dependency
331 if(_CD_TARGET)
332 add_dependencies(livecd ${_CD_TARGET} converted_hives)
333 endif()
334 foreach(item ${_CD_FILE})
335 if(_CD_NAME_ON_CD)
336 #rename it in the cd tree
337 set(__file ${_CD_NAME_ON_CD})
338 else()
339 get_filename_component(__file ${item} NAME)
340 endif()
341 set_property(GLOBAL APPEND PROPERTY LIVECD_FILE_LIST "${_CD_DESTINATION}/${__file}=${item}")
342 endforeach()
343 endif() #end livecd
344
345 #do we add it to regtest?
346 list(FIND _CD_FOR regtest __cd)
347 if(NOT __cd EQUAL -1)
348 #whether or not we should put it in reactos.cab or directly on cd
349 if(_CD_NO_CAB)
350 #directly on cd
351 foreach(item ${_CD_FILE})
352 if(_CD_NAME_ON_CD)
353 #rename it in the cd tree
354 set(__file ${_CD_NAME_ON_CD})
355 else()
356 get_filename_component(__file ${item} NAME)
357 endif()
358 set_property(GLOBAL APPEND PROPERTY BOOTCDREGTEST_FILE_LIST "${_CD_DESTINATION}/${__file}=${item}")
359 endforeach()
360 if(_CD_TARGET)
361 #manage dependency
362 add_dependencies(bootcdregtest ${_CD_TARGET} converted_hives)
363 endif()
364 else()
365 #add it in reactos.cab
366 #dir_to_num(${_CD_DESTINATION} _num)
367 #file(APPEND ${REACTOS_BINARY_DIR}/boot/bootdata/packages/reactos.dff.dyn "${_CD_FILE} ${_num}\n")
368 #if(_CD_TARGET)
369 # #manage dependency
370 # add_dependencies(reactos_cab ${_CD_TARGET})
371 #endif()
372 endif()
373 endif() #end bootcd
374 endfunction()
375
376 function(create_iso_lists)
377 # generate reactos.cab before anything else
378 get_property(_filelist GLOBAL PROPERTY REACTOS_CAB_DEPENDS)
379
380 # begin with reactos.inf. We want this command to be always executed, so we pretend it generates another file although it will never do.
381 add_custom_command(
382 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/reactos.inf ${CMAKE_CURRENT_BINARY_DIR}/__some_non_existent_file
383 COMMAND ${CMAKE_COMMAND} -E copy_if_different ${REACTOS_BINARY_DIR}/boot/bootdata/packages/reactos.inf ${CMAKE_CURRENT_BINARY_DIR}/reactos.inf
384 DEPENDS ${REACTOS_BINARY_DIR}/boot/bootdata/packages/reactos.inf reactos_cab_inf)
385
386 add_custom_command(
387 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/reactos.cab
388 COMMAND native-cabman -C ${REACTOS_BINARY_DIR}/boot/bootdata/packages/reactos.dff -RC ${CMAKE_CURRENT_BINARY_DIR}/reactos.inf -N -P ${REACTOS_SOURCE_DIR}
389 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/reactos.inf native-cabman ${_filelist})
390
391 add_custom_target(reactos_cab DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/reactos.cab)
392 add_dependencies(reactos_cab reactos_cab_inf)
393
394 add_cd_file(
395 TARGET reactos_cab
396 FILE ${CMAKE_CURRENT_BINARY_DIR}/reactos.cab
397 DESTINATION reactos
398 NO_CAB FOR bootcd regtest)
399
400 get_property(_filelist GLOBAL PROPERTY BOOTCD_FILE_LIST)
401 string(REPLACE ";" "\n" _filelist "${_filelist}")
402 file(APPEND ${REACTOS_BINARY_DIR}/boot/bootcd.lst "${_filelist}")
403 unset(_filelist)
404
405 get_property(_filelist GLOBAL PROPERTY LIVECD_FILE_LIST)
406 string(REPLACE ";" "\n" _filelist "${_filelist}")
407 file(APPEND ${REACTOS_BINARY_DIR}/boot/livecd.lst "${_filelist}")
408 unset(_filelist)
409
410 get_property(_filelist GLOBAL PROPERTY BOOTCDREGTEST_FILE_LIST)
411 string(REPLACE ";" "\n" _filelist "${_filelist}")
412 file(APPEND ${REACTOS_BINARY_DIR}/boot/bootcdregtest.lst "${_filelist}")
413 unset(_filelist)
414 endfunction()
415
416 # Create module_clean targets
417 function(add_clean_target _target)
418 set(_clean_working_directory ${CMAKE_CURRENT_BINARY_DIR})
419 if(CMAKE_GENERATOR STREQUAL "Unix Makefiles" OR CMAKE_GENERATOR STREQUAL "MinGW Makefiles")
420 set(_clean_command make clean)
421 elseif(CMAKE_GENERATOR STREQUAL "NMake Makefiles")
422 set(_clean_command nmake /nologo clean)
423 elseif(CMAKE_GENERATOR STREQUAL "Ninja")
424 set(_clean_command ninja -t clean ${_target})
425 set(_clean_working_directory ${REACTOS_BINARY_DIR})
426 endif()
427 add_custom_target(${_target}_clean
428 COMMAND ${_clean_command}
429 WORKING_DIRECTORY ${_clean_working_directory}
430 COMMENT "Cleaning ${_target}")
431 endfunction()
432
433 if(NOT MSVC_IDE)
434 function(add_library name)
435 _add_library(${name} ${ARGN})
436 add_clean_target(${name})
437 endfunction()
438
439 function(add_executable name)
440 _add_executable(${name} ${ARGN})
441 add_clean_target(${name})
442 endfunction()
443 elseif(USE_FOLDER_STRUCTURE)
444 set_property(GLOBAL PROPERTY USE_FOLDERS ON)
445 string(LENGTH ${CMAKE_SOURCE_DIR} CMAKE_SOURCE_DIR_LENGTH)
446
447 function(add_custom_target name)
448 _add_custom_target(${name} ${ARGN})
449 string(SUBSTRING ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_SOURCE_DIR_LENGTH} -1 CMAKE_CURRENT_SOURCE_DIR_RELATIVE)
450 set_property(TARGET "${name}" PROPERTY FOLDER "${CMAKE_CURRENT_SOURCE_DIR_RELATIVE}")
451 endfunction()
452
453 function(add_library name)
454 _add_library(${name} ${ARGN})
455 get_target_property(_target_excluded ${name} EXCLUDE_FROM_ALL)
456 if(_target_excluded AND ${name} MATCHES "^lib.*")
457 set_property(TARGET "${name}" PROPERTY FOLDER "Importlibs")
458 else()
459 string(SUBSTRING ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_SOURCE_DIR_LENGTH} -1 CMAKE_CURRENT_SOURCE_DIR_RELATIVE)
460 set_property(TARGET "${name}" PROPERTY FOLDER "${CMAKE_CURRENT_SOURCE_DIR_RELATIVE}")
461 endif()
462 endfunction()
463
464 function(add_executable name)
465 _add_executable(${name} ${ARGN})
466 string(SUBSTRING ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_SOURCE_DIR_LENGTH} -1 CMAKE_CURRENT_SOURCE_DIR_RELATIVE)
467 set_property(TARGET "${name}" PROPERTY FOLDER "${CMAKE_CURRENT_SOURCE_DIR_RELATIVE}")
468 endfunction()
469 endif()
470
471 if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
472 macro(to_win_path _cmake_path _native_path)
473 string(REPLACE "/" "\\" ${_native_path} "${_cmake_path}")
474 endmacro()
475
476 # yeah the parameter mess sucks, but thats what works...
477 function(concatenate_files _file1 _target2 _output)
478 get_target_property(_file2 ${_target2} LOCATION)
479 to_win_path("${_file1}" _real_file1)
480 to_win_path("${_file2}" _real_file2)
481 to_win_path("${_output}" _real_output)
482 add_custom_command(
483 OUTPUT ${_output}
484 COMMAND cmd.exe /C "copy /Y /B ${_real_file1} + ${_real_file2} ${_real_output} > nul"
485 DEPENDS ${_file1}
486 DEPENDS ${_target2})
487 endfunction()
488 else()
489 macro(concatenate_files _file1 _target2 _output)
490 get_target_property(_file2 ${_target2} LOCATION)
491 add_custom_command(
492 OUTPUT ${_output}
493 COMMAND cat ${_file1} ${_file2} > ${_output}
494 DEPENDS ${_file1}
495 DEPENDS ${_target2})
496 endmacro()
497 endif()
498
499 function(add_importlibs _module)
500 add_dependency_node(${_module})
501 foreach(LIB ${ARGN})
502 if("${LIB}" MATCHES "msvcrt")
503 add_target_compile_definitions(${_module} _DLL __USE_CRTIMP)
504 target_link_libraries(${_module} msvcrtex)
505 if(MSVC)
506 if(RUNTIME_CHECKS)
507 if(NOT ${_module} STREQUAL "kernel32")
508 add_target_compile_flags(${_module} "/RTC1")
509 target_link_libraries(${_module} RunTmChk.lib)
510 endif()
511 endif()
512 endif()
513 endif()
514 target_link_libraries(${_module} lib${LIB})
515 add_dependencies(${_module} lib${LIB})
516 add_dependency_edge(${_module} ${LIB})
517 endforeach()
518 endfunction()
519
520 function(set_module_type MODULE TYPE)
521 cmake_parse_arguments(__module "UNICODE;HOTPATCHABLE" "IMAGEBASE" "ENTRYPOINT" ${ARGN})
522
523 if(__module_UNPARSED_ARGUMENTS)
524 message(STATUS "set_module_type : unparsed arguments ${__module_UNPARSED_ARGUMENTS}, module : ${MODULE}")
525 endif()
526
527 # Set subsystem. Also take this as an occasion
528 # to error out if someone gave a non existing type
529 if((${TYPE} STREQUAL nativecui) OR (${TYPE} STREQUAL nativedll)
530 OR (${TYPE} STREQUAL kernelmodedriver) OR (${TYPE} STREQUAL wdmdriver) OR (${TYPE} STREQUAL kerneldll))
531 set(__subsystem native)
532 elseif(${TYPE} STREQUAL win32cui)
533 set(__subsystem console)
534 elseif(${TYPE} STREQUAL win32gui)
535 set(__subsystem windows)
536 elseif(${TYPE} STREQUAL kbdlayout)
537 set_entrypoint(${MODULE} 0)
538 set_image_base(${MODULE} 0x5FFF0000)
539 set_subsystem(${MODULE} native)
540 if (MSVC)
541 # Merge the .text and .rdata section into the .data section
542 add_target_link_flags(${MODULE} "/ignore:4254 /SECTION:.data,ER /MERGE:.text=.data /MERGE:.rdata=.data /MERGE:.bss=.data /MERGE:.edata=.data")
543 else()
544 # Use a custom linker script
545 add_target_link_flags(${MODULE} "-Wl,-T,${CMAKE_SOURCE_DIR}/kbdlayout.lds")
546 endif()
547 elseif(NOT ((${TYPE} STREQUAL win32dll) OR (${TYPE} STREQUAL win32ocx)
548 OR (${TYPE} STREQUAL cpl) OR (${TYPE} STREQUAL module)))
549 message(FATAL_ERROR "Unknown type ${TYPE} for module ${MODULE}")
550 endif()
551
552 if(DEFINED __subsystem)
553 set_subsystem(${MODULE} ${__subsystem})
554 endif()
555
556 #set unicode definitions
557 if(__module_UNICODE)
558 add_target_compile_definitions(${MODULE} UNICODE _UNICODE)
559 endif()
560
561 # Handle hotpatchable images.
562 # GCC has this as a function attribute so we're handling it using DECLSPEC_HOTPATCH
563 if(__module_HOTPATCHABLE AND MSVC AND (NOT ARCH STREQUAL "arm"))
564 set_property(TARGET ${MODULE} APPEND_STRING PROPERTY COMPILE_FLAGS " /hotpatch")
565 if(ARCH STREQUAL "i386")
566 set_property(TARGET ${MODULE} APPEND_STRING PROPERTY LINK_FLAGS " /FUNCTIONPADMIN:5")
567 elseif(ARCH STREQUAL "amd64")
568 set_property(TARGET ${MODULE} APPEND_STRING PROPERTY LINK_FLAGS " /FUNCTIONPADMIN:6")
569 endif()
570 endif()
571
572 # set entry point
573 if(__module_ENTRYPOINT OR (__module_ENTRYPOINT STREQUAL "0"))
574 list(GET __module_ENTRYPOINT 0 __entrypoint)
575 list(LENGTH __module_ENTRYPOINT __length)
576 if(${__length} EQUAL 2)
577 list(GET __module_ENTRYPOINT 1 __entrystack)
578 elseif(NOT ${__length} EQUAL 1)
579 message(FATAL_ERROR "Wrong arguments for ENTRYPOINT parameter of set_module_type : ${__module_ENTRYPOINT}")
580 endif()
581 unset(__length)
582 elseif(${TYPE} STREQUAL nativecui)
583 set(__entrypoint NtProcessStartup)
584 set(__entrystack 4)
585 elseif(${TYPE} STREQUAL win32cui)
586 if(__module_UNICODE)
587 set(__entrypoint wmainCRTStartup)
588 else()
589 set(__entrypoint mainCRTStartup)
590 endif()
591 elseif(${TYPE} STREQUAL win32gui)
592 if(__module_UNICODE)
593 set(__entrypoint wWinMainCRTStartup)
594 else()
595 set(__entrypoint WinMainCRTStartup)
596 endif()
597 elseif((${TYPE} STREQUAL win32dll) OR (${TYPE} STREQUAL win32ocx)
598 OR (${TYPE} STREQUAL cpl))
599 set(__entrypoint DllMainCRTStartup)
600 set(__entrystack 12)
601 elseif((${TYPE} STREQUAL kernelmodedriver) OR (${TYPE} STREQUAL wdmdriver))
602 set(__entrypoint DriverEntry)
603 set(__entrystack 8)
604 elseif(${TYPE} STREQUAL nativedll)
605 set(__entrypoint DllMain)
606 set(__entrystack 12)
607 elseif(${TYPE} STREQUAL module)
608 set(__entrypoint 0)
609 endif()
610
611 if(DEFINED __entrypoint)
612 if(DEFINED __entrystack)
613 set_entrypoint(${MODULE} ${__entrypoint} ${__entrystack})
614 else()
615 set_entrypoint(${MODULE} ${__entrypoint})
616 endif()
617 endif()
618
619 #set base address
620 if(__module_IMAGEBASE)
621 set_image_base(${MODULE} __module_IMAGEBASE)
622 elseif(${TYPE} STREQUAL win32dll)
623 if(DEFINED baseaddress_${MODULE})
624 set_image_base(${MODULE} ${baseaddress_${MODULE}})
625 else()
626 message(STATUS "${MODULE} has no base address")
627 endif()
628 elseif((${TYPE} STREQUAL kernelmodedriver) OR (${TYPE} STREQUAL wdmdriver) OR (${TYPE} STREQUAL kernelmodedll))
629 set_image_base(${MODULE} 0x00010000)
630 endif()
631
632 # Now do some stuff which is specific to each type
633 if((${TYPE} STREQUAL kernelmodedriver) OR (${TYPE} STREQUAL wdmdriver) OR (${TYPE} STREQUAL kernelmodedll))
634 add_dependencies(${MODULE} bugcodes)
635 if((${TYPE} STREQUAL kernelmodedriver) OR (${TYPE} STREQUAL wdmdriver))
636 set_target_properties(${MODULE} PROPERTIES SUFFIX ".sys")
637 endif()
638 endif()
639
640 if(${TYPE} STREQUAL win32ocx)
641 set_target_properties(${MODULE} PROPERTIES SUFFIX ".ocx")
642 endif()
643
644 if(${TYPE} STREQUAL cpl)
645 set_target_properties(${MODULE} PROPERTIES SUFFIX ".cpl")
646 endif()
647
648 # do compiler specific stuff
649 set_module_type_toolchain(${MODULE} ${TYPE})
650 endfunction()
651
652 function(preprocess_file __in __out)
653 set(__arg ${__in})
654 foreach(__def in ${ARGN})
655 list(APPEND __arg -D${__def})
656 endforeach()
657 if(MSVC)
658 add_custom_command(OUTPUT ${_out}
659 COMMAND ${CMAKE_C_COMPILER} /EP ${__arg}
660 DEPENDS ${__in})
661 else()
662 add_custom_command(OUTPUT ${_out}
663 COMMAND ${CMAKE_C_COMPILER} -E ${__arg}
664 DEPENDS ${__in})
665 endif()
666 endfunction()
667
668 function(get_includes OUTPUT_VAR)
669 get_directory_property(_includes INCLUDE_DIRECTORIES)
670 foreach(arg ${_includes})
671 list(APPEND __tmp_var -I${arg})
672 endforeach()
673 set(${OUTPUT_VAR} ${__tmp_var} PARENT_SCOPE)
674 endfunction()
675
676 function(get_defines OUTPUT_VAR)
677 get_directory_property(_defines COMPILE_DEFINITIONS)
678 foreach(arg ${_defines})
679 list(APPEND __tmp_var -D${arg})
680 endforeach()
681 set(${OUTPUT_VAR} ${__tmp_var} PARENT_SCOPE)
682 endfunction()
683
684 if(NOT MSVC)
685 function(add_object_library _target)
686 add_library(${_target} OBJECT ${ARGN})
687 endfunction()
688 else()
689 function(add_object_library _target)
690 add_library(${_target} ${ARGN})
691 endfunction()
692 endif()
693
694 if(KDBG)
695 set(ROSSYM_LIB "rossym")
696 else()
697 set(ROSSYM_LIB "")
698 endif()