[USB-BRINGUP-TRUNK]
[reactos.git] / cmake / CMakeMacros.cmake
1
2 macro(set_cpp)
3 set(IS_CPP 1)
4 if(MSVC)
5 include_directories(BEFORE ${REACTOS_SOURCE_DIR}/include/c++/stlport)
6 add_definitions(
7 -DNATIVE_CPP_INCLUDE=${REACTOS_SOURCE_DIR}/include/c++
8 -DNATIVE_C_INCLUDE=${REACTOS_SOURCE_DIR}/include/crt)
9 endif()
10 endmacro()
11
12 function(add_dependency_node _node)
13 if(GENERATE_DEPENDENCY_GRAPH)
14 get_target_property(_type ${_node} TYPE)
15 if(_type MATCHES SHARED_LIBRARY OR ${_node} MATCHES ntoskrnl)
16 file(APPEND ${REACTOS_BINARY_DIR}/dependencies.graphml " <node id=\"${_node}\"/>\n")
17 endif()
18 endif()
19 endfunction()
20
21 function(add_dependency_edge _source _target)
22 if(GENERATE_DEPENDENCY_GRAPH)
23 get_target_property(_type ${_source} TYPE)
24 if(_type MATCHES SHARED_LIBRARY)
25 file(APPEND ${REACTOS_BINARY_DIR}/dependencies.graphml " <edge source=\"${_source}\" target=\"${_target}\"/>\n")
26 endif()
27 endif()
28 endfunction()
29
30 function(add_dependency_header)
31 file(APPEND ${REACTOS_BINARY_DIR}/dependencies.graphml "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<graphml>\n <graph id=\"ReactOS dependencies\" edgedefault=\"directed\">\n")
32 endfunction()
33
34 function(add_dependency_footer)
35 add_dependency_node(ntdll)
36 file(APPEND ${REACTOS_BINARY_DIR}/dependencies.graphml " </graph>\n</graphml>\n")
37 endfunction()
38
39 function(add_message_headers _type)
40 if(${_type} STREQUAL UNICODE)
41 set(_flag "-U")
42 else()
43 set(_flag "-A")
44 endif()
45 foreach(_in_FILE ${ARGN})
46 get_filename_component(FILE ${_in_FILE} NAME_WE)
47 macro_mc(${_flag} ${FILE})
48 add_custom_command(
49 OUTPUT ${REACTOS_BINARY_DIR}/include/reactos/${FILE}.rc ${REACTOS_BINARY_DIR}/include/reactos/${FILE}.h
50 COMMAND ${COMMAND_MC} ${MC_FLAGS}
51 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${FILE}.mc)
52 set_source_files_properties(
53 ${REACTOS_BINARY_DIR}/include/reactos/${FILE}.h ${REACTOS_BINARY_DIR}/include/reactos/${FILE}.rc
54 PROPERTIES GENERATED TRUE)
55 add_custom_target(${FILE} ALL DEPENDS ${REACTOS_BINARY_DIR}/include/reactos/${FILE}.h ${REACTOS_BINARY_DIR}/include/reactos/${FILE}.rc)
56 endforeach()
57 endfunction()
58
59 function(add_link)
60 cmake_parse_arguments(_LINK "MINIMIZE" "NAME;PATH;CMD_LINE_ARGS;ICON;GUID" "" ${ARGN})
61 if(NOT _LINK_NAME OR NOT _LINK_PATH)
62 message(FATAL_ERROR "You must provide name and path")
63 endif()
64
65 if(_LINK_CMD_LINE_ARGS)
66 set(_LINK_CMD_LINE_ARGS -c ${_LINK_CMD_LINE_ARGS})
67 endif()
68
69 if(_LINK_ICON)
70 set(_LINK_ICON -i ${_LINK_ICON})
71 endif()
72
73 if(_LINK_GUID)
74 set(_LINK_GUID -g ${_LINK_GUID})
75 endif()
76
77 if(_LINK_MINIMIZE)
78 set(_LINK_MINIMIZE "-m")
79 endif()
80
81 add_custom_command(
82 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_LINK_NAME}.lnk
83 COMMAND native-mkshelllink -o ${CMAKE_CURRENT_BINARY_DIR}/${_LINK_NAME}.lnk ${_LINK_CMD_LINE_ARGS} ${_LINK_ICON} ${_LINK_GUID} ${_LINK_MINIMIZE} ${_LINK_PATH}
84 DEPENDS native-mkshelllink)
85 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${_LINK_NAME}.lnk PROPERTIES GENERATED TRUE)
86 endfunction()
87
88 macro(dir_to_num dir var)
89 if(${dir} STREQUAL reactos/system32)
90 set(${var} 1)
91 elseif(${dir} STREQUAL reactos/system32/drivers)
92 set(${var} 2)
93 elseif(${dir} STREQUAL reactos/Fonts)
94 set(${var} 3)
95 elseif(${dir} STREQUAL reactos)
96 set(${var} 4)
97 elseif(${dir} STREQUAL reactos/system32/drivers/etc)
98 set(${var} 5)
99 elseif(${dir} STREQUAL reactos/inf)
100 set(${var} 6)
101 elseif(${dir} STREQUAL reactos/bin)
102 set(${var} 7)
103 elseif(${dir} STREQUAL reactos/media)
104 set(${var} 8)
105 else()
106 message(ERROR "Wrong destination: ${dir}")
107 endif()
108 endmacro()
109
110 function(add_cd_file)
111 cmake_parse_arguments(_CD "NO_CAB" "DESTINATION;NAME_ON_CD;TARGET" "FILE;FOR" ${ARGN})
112 if(NOT (_CD_TARGET OR _CD_FILE))
113 message(FATAL_ERROR "You must provide a target or a file to install!")
114 endif()
115
116 if(NOT _CD_DESTINATION)
117 message(FATAL_ERROR "You must provide a destination")
118 elseif(${_CD_DESTINATION} STREQUAL root)
119 set(_CD_DESTINATION "")
120 endif()
121
122 if(NOT _CD_FOR)
123 message(FATAL_ERROR "You must provide a cd name (or "all" for all of them) to install the file on!")
124 endif()
125
126 #get file if we need to
127 if(NOT _CD_FILE)
128 get_target_property(_CD_FILE ${_CD_TARGET} LOCATION)
129 endif()
130
131 #do we add it to all CDs?
132 if(_CD_FOR STREQUAL all)
133 set(_CD_FOR "bootcd;livecd;regtest")
134 endif()
135
136 #do we add it to bootcd?
137 list(FIND _CD_FOR bootcd __cd)
138 if(NOT __cd EQUAL -1)
139 #whether or not we should put it in reactos.cab or directly on cd
140 if(_CD_NO_CAB)
141 #directly on cd
142 foreach(item ${_CD_FILE})
143 file(APPEND ${REACTOS_BINARY_DIR}/boot/bootcd.cmake "file(COPY \"${item}\" DESTINATION \"\${CD_DIR}/${_CD_DESTINATION}\")\n")
144 endforeach()
145 if(_CD_NAME_ON_CD)
146 get_filename_component(__file ${_CD_FILE} NAME)
147 #rename it in the cd tree
148 file(APPEND ${REACTOS_BINARY_DIR}/boot/bootcd.cmake "file(RENAME \${CD_DIR}/${_CD_DESTINATION}/${__file} \${CD_DIR}/${_CD_DESTINATION}/${_CD_NAME_ON_CD})\n")
149 endif()
150 if(_CD_TARGET)
151 #manage dependency
152 add_dependencies(bootcd ${_CD_TARGET})
153 endif()
154 else()
155 #add it in reactos.cab
156 dir_to_num(${_CD_DESTINATION} _num)
157 if(CMAKE_HOST_SYSTEM_NAME MATCHES Windows)
158 file(APPEND ${REACTOS_BINARY_DIR}/boot/bootdata/packages/reactos.dff.dyn "${_CD_FILE} ${_num}\n")
159 else()
160 file(APPEND ${REACTOS_BINARY_DIR}/boot/bootdata/packages/reactos.dff.dyn "\"${_CD_FILE}\" ${_num}\n")
161 endif()
162 if(_CD_TARGET)
163 #manage dependency
164 add_dependencies(reactos_cab ${_CD_TARGET})
165 endif()
166 endif()
167 endif() #end bootcd
168
169 #do we add it to livecd?
170 list(FIND _CD_FOR livecd __cd)
171 if(NOT __cd EQUAL -1)
172 #manage dependency
173 if(_CD_TARGET)
174 add_dependencies(livecd ${_CD_TARGET})
175 endif()
176 foreach(item ${_CD_FILE})
177 file(APPEND ${REACTOS_BINARY_DIR}/boot/livecd.cmake "file(COPY \"${item}\" DESTINATION \"\${CD_DIR}/${_CD_DESTINATION}\")\n")
178 endforeach()
179 if(_CD_NAME_ON_CD)
180 get_filename_component(__file ${_CD_FILE} NAME)
181 #rename it in the cd tree
182 file(APPEND ${REACTOS_BINARY_DIR}/boot/livecd.cmake "file(RENAME \${CD_DIR}/${_CD_DESTINATION}/${__file} \${CD_DIR}/${_CD_DESTINATION}/${_CD_NAME_ON_CD})\n")
183 endif()
184 endif() #end livecd
185
186 #do we add it to regtest?
187 list(FIND _CD_FOR regtest __cd)
188 if(NOT __cd EQUAL -1)
189 #whether or not we should put it in reactos.cab or directly on cd
190 if(_CD_NO_CAB)
191 #directly on cd
192 foreach(item ${_CD_FILE})
193 file(APPEND ${REACTOS_BINARY_DIR}/boot/bootcdregtest.cmake "file(COPY \"${item}\" DESTINATION \"\${CD_DIR}/${_CD_DESTINATION}\")\n")
194 endforeach()
195 if(_CD_NAME_ON_CD)
196 get_filename_component(__file ${_CD_FILE} NAME)
197 #rename it in the cd tree
198 file(APPEND ${REACTOS_BINARY_DIR}/boot/bootcdregtest.cmake "file(RENAME \${CD_DIR}/${_CD_DESTINATION}/${__file} \${CD_DIR}/${_CD_DESTINATION}/${_CD_NAME_ON_CD})\n")
199 endif()
200 if(_CD_TARGET)
201 #manage dependency
202 add_dependencies(bootcdregtest ${_CD_TARGET})
203 endif()
204 else()
205 #add it in reactos.cab
206 #dir_to_num(${_CD_DESTINATION} _num)
207 #file(APPEND ${REACTOS_BINARY_DIR}/boot/bootdata/packages/reactos.dff.dyn "${_CD_FILE} ${_num}\n")
208 #if(_CD_TARGET)
209 # #manage dependency
210 # add_dependencies(reactos_cab ${_CD_TARGET})
211 #endif()
212 endif()
213 endif() #end bootcd
214 endfunction()
215
216 # Create module_clean targets
217 function(add_clean_target target)
218 if(CMAKE_GENERATOR MATCHES "Unix Makefiles" OR CMAKE_GENERATOR MATCHES "MinGW Makefiles")
219 set(CLEAN_COMMAND make clean)
220 elseif(CMAKE_GENERATOR MATCHES "NMake Makefiles")
221 set(CLEAN_COMMAND nmake /nologo clean)
222 endif()
223 add_custom_target(${target}_clean
224 COMMAND ${CLEAN_COMMAND}
225 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
226 COMMENT "Cleaning ${target}")
227 endfunction()
228
229 if(NOT MSVC_IDE)
230 function(add_library name)
231 _add_library(${name} ${ARGN})
232 add_clean_target(${name})
233 endfunction()
234
235 function(add_executable name)
236 _add_executable(${name} ${ARGN})
237 add_clean_target(${name})
238 endfunction()
239 endif()
240
241 if(CMAKE_HOST_SYSTEM_NAME MATCHES Windows)
242 macro(to_win_path _cmake_path _native_path)
243 string(REPLACE "/" "\\" ${_native_path} "${_cmake_path}")
244 endmacro()
245
246 # yeah the parameter mess sucks, but thats what works...
247 function(concatenate_files _file1 _target2 _output)
248 get_target_property(_file2 ${_target2} LOCATION)
249 to_win_path("${_file1}" _real_file1)
250 to_win_path("${_file2}" _real_file2)
251 to_win_path("${_output}" _real_output)
252 add_custom_command(
253 OUTPUT ${_output}
254 COMMAND cmd.exe /C "copy /Y /B ${_real_file1} + ${_real_file2} ${_real_output} > nul"
255 DEPENDS ${_file1}
256 DEPENDS ${_target2})
257 endfunction()
258 else()
259 macro(concatenate_files _file1 _target2 _output)
260 get_target_property(_file2 ${_target2} LOCATION)
261 add_custom_command(
262 OUTPUT ${_output}
263 COMMAND cat ${_file1} ${_file2} > ${_output}
264 DEPENDS ${_file1}
265 DEPENDS ${_target2})
266 endmacro()
267 endif()
268
269 function(add_importlibs _module)
270 add_dependency_node(${_module})
271 foreach(LIB ${ARGN})
272 if("${LIB}" MATCHES "msvcrt")
273 add_target_compile_definitions(${_module} _DLL __USE_CRTIMP)
274 target_link_libraries(${_module} msvcrtex)
275 endif()
276 target_link_libraries(${_module} ${CMAKE_BINARY_DIR}/importlibs/lib${LIB}${CMAKE_STATIC_LIBRARY_SUFFIX})
277 add_dependencies(${_module} lib${LIB})
278 add_dependency_edge(${_module} ${LIB})
279 endforeach()
280 endfunction()
281
282 function(set_module_type MODULE TYPE)
283 cmake_parse_arguments(__module "UNICODE" "IMAGEBASE" "ENTRYPOINT" ${ARGN})
284
285 if(__module_UNPARSED_ARGUMENTS)
286 message(STATUS "set_module_type : unparsed arguments ${__module_UNPARSED_ARGUMENTS}, module : ${MODULE}")
287 endif()
288
289 # Set subsystem. Also take this as an occasion
290 # to error out if someone gave a non existing type
291 if((${TYPE} STREQUAL nativecui) OR (${TYPE} STREQUAL nativedll) OR (${TYPE} STREQUAL kernelmodedriver))
292 set(__subsystem native)
293 elseif(${TYPE} STREQUAL win32cui)
294 set(__subsystem console)
295 elseif(${TYPE} STREQUAL win32gui)
296 set(__subsystem windows)
297 elseif(NOT ((${TYPE} STREQUAL win32dll) OR (${TYPE} STREQUAL win32ocx)
298 OR (${TYPE} STREQUAL cpl) OR (${TYPE} STREQUAL module)))
299 message(FATAL_ERROR "Unknown type ${TYPE} for module ${MODULE}")
300 endif()
301
302 if(DEFINED __subsystem)
303 set_subsystem(${MODULE} ${__subsystem})
304 endif()
305
306 #set unicode definitions
307 if(__module_UNICODE)
308 add_target_compile_definitions(${MODULE} UNICODE _UNICODE)
309 endif()
310
311 # set entry point
312 if(__module_ENTRYPOINT OR (__module_ENTRYPOINT STREQUAL "0"))
313 list(GET __module_ENTRYPOINT 0 __entrypoint)
314 list(LENGTH __module_ENTRYPOINT __length)
315 if(${__length} EQUAL 2)
316 list(GET __module_ENTRYPOINT 1 __entrystack)
317 elseif(NOT ${__length} EQUAL 1)
318 message(FATAL_ERROR "Wrong arguments for ENTRYPOINT parameter of set_module_type : ${__module_ENTRYPOINT}")
319 endif()
320 unset(__length)
321 elseif(${TYPE} STREQUAL nativecui)
322 set(__entrypoint NtProcessStartup)
323 set(__entrystack 4)
324 elseif((${TYPE} STREQUAL win32gui) OR (${TYPE} STREQUAL win32cui))
325 if(__module_UNICODE)
326 set(__entrypoint wWinMainCRTStartup)
327 else()
328 set(__entrypoint WinMainCRTStartup)
329 endif()
330 elseif((${TYPE} STREQUAL win32dll) OR (${TYPE} STREQUAL win32ocx)
331 OR (${TYPE} STREQUAL cpl))
332 set(__entrypoint DllMainCRTStartup)
333 set(__entrystack 12)
334 elseif(${TYPE} STREQUAL kernelmodedriver)
335 set(__entrypoint DriverEntry)
336 set(__entrystack 8)
337 elseif(${TYPE} STREQUAL nativedll)
338 set(__entrypoint DllMain)
339 set(__entrystack 12)
340 elseif(${TYPE} STREQUAL module)
341 set(__entrypoint 0)
342 endif()
343
344 if(DEFINED __entrypoint)
345 if(DEFINED __entrystack)
346 set_entrypoint(${MODULE} ${__entrypoint} ${__entrystack})
347 else()
348 set_entrypoint(${MODULE} ${__entrypoint})
349 endif()
350 endif()
351
352 #set base address
353 if(__module_IMAGEBASE)
354 set_image_base(${MODULE} __module_IMAGEBASE)
355 elseif(${TYPE} STREQUAL win32dll)
356 if(DEFINED baseaddress_${MODULE})
357 set_image_base(${MODULE} ${baseaddress_${MODULE}})
358 else()
359 message(STATUS "${MODULE} has no base address")
360 endif()
361 elseif(${TYPE} STREQUAL kernelmodedriver)
362 set_image_base(${MODULE} 0x00010000)
363 endif()
364
365 # Now do some stuff which is specific to each type
366 if(${TYPE} STREQUAL kernelmodedriver)
367 add_dependencies(${MODULE} bugcodes)
368 set_target_properties(${MODULE} PROPERTIES SUFFIX ".sys")
369 endif()
370
371 if(${TYPE} STREQUAL win32ocx)
372 set_target_properties(${MODULE} PROPERTIES SUFFIX ".ocx")
373 endif()
374
375 if(${TYPE} STREQUAL cpl)
376 set_target_properties(${MODULE} PROPERTIES SUFFIX ".cpl")
377 endif()
378
379 # do compiler specific stuff
380 set_module_type_toolchain(${MODULE} ${TYPE})
381 endfunction()