[TRANSLATION]
[reactos.git] / reactos / cmake / CMakeMacros.cmake
1
2 macro(set_cpp)
3 include_directories(BEFORE ${REACTOS_SOURCE_DIR}/include/c++/stlport)
4 set(IS_CPP 1)
5 add_definitions(
6 -DNATIVE_CPP_INCLUDE=${REACTOS_SOURCE_DIR}/include/c++
7 -DNATIVE_C_INCLUDE=${REACTOS_SOURCE_DIR}/include/crt)
8 endmacro()
9
10 function(add_dependency_node _node)
11 if(GENERATE_DEPENDENCY_GRAPH)
12 get_target_property(_type ${_node} TYPE)
13 if(_type MATCHES SHARED_LIBRARY OR ${_node} MATCHES ntoskrnl)
14 file(APPEND ${REACTOS_BINARY_DIR}/dependencies.graphml " <node id=\"${_node}\"/>\n")
15 endif()
16 endif()
17 endfunction()
18
19 function(add_dependency_edge _source _target)
20 if(GENERATE_DEPENDENCY_GRAPH)
21 get_target_property(_type ${_source} TYPE)
22 if(_type MATCHES SHARED_LIBRARY)
23 file(APPEND ${REACTOS_BINARY_DIR}/dependencies.graphml " <edge source=\"${_source}\" target=\"${_target}\"/>\n")
24 endif()
25 endif()
26 endfunction()
27
28 function(add_dependency_header)
29 file(APPEND ${REACTOS_BINARY_DIR}/dependencies.graphml "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<graphml>\n <graph id=\"ReactOS dependencies\" edgedefault=\"directed\">\n")
30 endfunction()
31
32 function(add_dependency_footer)
33 add_dependency_node(ntdll)
34 file(APPEND ${REACTOS_BINARY_DIR}/dependencies.graphml " </graph>\n</graphml>\n")
35 endfunction()
36
37 function(add_message_headers)
38 foreach(_in_FILE ${ARGN})
39 get_filename_component(FILE ${_in_FILE} NAME_WE)
40 macro_mc(${FILE})
41 add_custom_command(
42 OUTPUT ${REACTOS_BINARY_DIR}/include/reactos/${FILE}.rc ${REACTOS_BINARY_DIR}/include/reactos/${FILE}.h
43 COMMAND ${COMMAND_MC}
44 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${FILE}.mc)
45 set_source_files_properties(
46 ${REACTOS_BINARY_DIR}/include/reactos/${FILE}.h ${REACTOS_BINARY_DIR}/include/reactos/${FILE}.rc
47 PROPERTIES GENERATED TRUE)
48 add_custom_target(${FILE} ALL DEPENDS ${REACTOS_BINARY_DIR}/include/reactos/${FILE}.h ${REACTOS_BINARY_DIR}/include/reactos/${FILE}.rc)
49 endforeach()
50 endfunction()
51
52 macro(dir_to_num dir var)
53 if(${dir} STREQUAL reactos/system32)
54 set(${var} 1)
55 elseif(${dir} STREQUAL reactos/system32/drivers)
56 set(${var} 2)
57 elseif(${dir} STREQUAL reactos/Fonts)
58 set(${var} 3)
59 elseif(${dir} STREQUAL reactos)
60 set(${var} 4)
61 elseif(${dir} STREQUAL reactos/system32/drivers/etc)
62 set(${var} 5)
63 elseif(${dir} STREQUAL reactos/inf)
64 set(${var} 6)
65 elseif(${dir} STREQUAL reactos/bin)
66 set(${var} 7)
67 elseif(${dir} STREQUAL reactos/media)
68 set(${var} 8)
69 else()
70 message(ERROR "Wrong destination: ${dir}")
71 endif()
72 endmacro()
73
74 function(add_cd_file)
75 cmake_parse_arguments(_CD "NO_CAB" "DESTINATION;NAME_ON_CD;TARGET" "FILE;FOR" ${ARGN})
76 if(NOT (_CD_TARGET OR _CD_FILE))
77 message(FATAL_ERROR "You must provide a target or a file to install!")
78 endif()
79
80 if(NOT _CD_DESTINATION)
81 message(FATAL_ERROR "You must provide a destination")
82 elseif(${_CD_DESTINATION} STREQUAL root)
83 set(_CD_DESTINATION "")
84 endif()
85
86 if(NOT _CD_FOR)
87 message(FATAL_ERROR "You must provide a cd name (or "all" for all of them) to install the file on!")
88 endif()
89
90 #get file if we need to
91 if(NOT _CD_FILE)
92 get_target_property(_CD_FILE ${_CD_TARGET} LOCATION)
93 endif()
94
95 #do we add it to all CDs?
96 if(_CD_FOR STREQUAL all)
97 set(_CD_FOR "bootcd;livecd;regtest")
98 endif()
99
100 #do we add it to bootcd?
101 list(FIND _CD_FOR bootcd __cd)
102 if(NOT __cd EQUAL -1)
103 #whether or not we should put it in reactos.cab or directly on cd
104 if(_CD_NO_CAB)
105 #directly on cd
106 foreach(item ${_CD_FILE})
107 file(APPEND ${REACTOS_BINARY_DIR}/boot/bootcd.cmake "file(COPY \"${item}\" DESTINATION \"\${CD_DIR}/${_CD_DESTINATION}\")\n")
108 endforeach()
109 if(_CD_NAME_ON_CD)
110 get_filename_component(__file ${_CD_FILE} NAME)
111 #rename it in the cd tree
112 file(APPEND ${REACTOS_BINARY_DIR}/boot/bootcd.cmake "file(RENAME \${CD_DIR}/${_CD_DESTINATION}/${__file} \${CD_DIR}/${_CD_DESTINATION}/${_CD_NAME_ON_CD})\n")
113 endif()
114 if(_CD_TARGET)
115 #manage dependency
116 add_dependencies(bootcd ${_CD_TARGET})
117 endif()
118 else()
119 #add it in reactos.cab
120 dir_to_num(${_CD_DESTINATION} _num)
121 if(CMAKE_HOST_SYSTEM_NAME MATCHES Windows)
122 file(APPEND ${REACTOS_BINARY_DIR}/boot/bootdata/packages/reactos.dff.dyn "${_CD_FILE} ${_num}\n")
123 else()
124 file(APPEND ${REACTOS_BINARY_DIR}/boot/bootdata/packages/reactos.dff.dyn "\"${_CD_FILE}\" ${_num}\n")
125 endif()
126 if(_CD_TARGET)
127 #manage dependency
128 add_dependencies(reactos_cab ${_CD_TARGET})
129 endif()
130 endif()
131 endif() #end bootcd
132
133 #do we add it to livecd?
134 list(FIND _CD_FOR livecd __cd)
135 if(NOT __cd EQUAL -1)
136 #manage dependency
137 if(_CD_TARGET)
138 add_dependencies(livecd ${_CD_TARGET})
139 endif()
140 foreach(item ${_CD_FILE})
141 file(APPEND ${REACTOS_BINARY_DIR}/boot/livecd.cmake "file(COPY \"${item}\" DESTINATION \"\${CD_DIR}/${_CD_DESTINATION}\")\n")
142 endforeach()
143 if(_CD_NAME_ON_CD)
144 get_filename_component(__file ${_CD_FILE} NAME)
145 #rename it in the cd tree
146 file(APPEND ${REACTOS_BINARY_DIR}/boot/livecd.cmake "file(RENAME \${CD_DIR}/${_CD_DESTINATION}/${__file} \${CD_DIR}/${_CD_DESTINATION}/${_CD_NAME_ON_CD})\n")
147 endif()
148 endif() #end livecd
149
150 #do we add it to regtest?
151 list(FIND _CD_FOR regtest __cd)
152 if(NOT __cd EQUAL -1)
153 #whether or not we should put it in reactos.cab or directly on cd
154 if(_CD_NO_CAB)
155 #directly on cd
156 foreach(item ${_CD_FILE})
157 file(APPEND ${REACTOS_BINARY_DIR}/boot/bootcdregtest.cmake "file(COPY \"${item}\" DESTINATION \"\${CD_DIR}/${_CD_DESTINATION}\")\n")
158 endforeach()
159 if(_CD_NAME_ON_CD)
160 get_filename_component(__file ${_CD_FILE} NAME)
161 #rename it in the cd tree
162 file(APPEND ${REACTOS_BINARY_DIR}/boot/bootcdregtest.cmake "file(RENAME \${CD_DIR}/${_CD_DESTINATION}/${__file} \${CD_DIR}/${_CD_DESTINATION}/${_CD_NAME_ON_CD})\n")
163 endif()
164 if(_CD_TARGET)
165 #manage dependency
166 add_dependencies(bootcdregtest ${_CD_TARGET})
167 endif()
168 else()
169 #add it in reactos.cab
170 #dir_to_num(${_CD_DESTINATION} _num)
171 #file(APPEND ${REACTOS_BINARY_DIR}/boot/bootdata/packages/reactos.dff.dyn "${_CD_FILE} ${_num}\n")
172 #if(_CD_TARGET)
173 # #manage dependency
174 # add_dependencies(reactos_cab ${_CD_TARGET})
175 #endif()
176 endif()
177 endif() #end bootcd
178 endfunction()
179
180 # Create module_clean targets
181 function(add_clean_target target)
182 if(CMAKE_GENERATOR MATCHES "Unix Makefiles" OR CMAKE_GENERATOR MATCHES "MinGW Makefiles")
183 set(CLEAN_COMMAND make clean)
184 elseif(CMAKE_GENERATOR MATCHES "NMake Makefiles")
185 set(CLEAN_COMMAND nmake /nologo clean)
186 endif()
187 add_custom_target(${target}_clean
188 COMMAND ${CLEAN_COMMAND}
189 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
190 COMMENT "Cleaning ${target}")
191 endfunction()
192
193 if(NOT MSVC_IDE)
194 function(add_library name)
195 _add_library(${name} ${ARGN})
196 add_clean_target(${name})
197 endfunction()
198
199 function(add_executable name)
200 _add_executable(${name} ${ARGN})
201 add_clean_target(${name})
202 endfunction()
203 endif()
204
205 if(CMAKE_HOST_SYSTEM_NAME MATCHES Windows)
206 macro(to_win_path _cmake_path _native_path)
207 string(REPLACE "/" "\\" ${_native_path} "${_cmake_path}")
208 endmacro()
209
210 function(concatenate_files _file1 _file2 _output)
211 to_win_path("${_file1}" _real_file1)
212 to_win_path("${_file2}" _real_file2)
213 to_win_path("${_output}" _real_output)
214 add_custom_command(
215 OUTPUT ${_output}
216 COMMAND cmd.exe /C "copy /Y /B ${_real_file1} + ${_real_file2} ${_real_output} > nul"
217 DEPENDS ${_file1}
218 DEPENDS ${_file2})
219 endfunction()
220 else()
221 macro(concatenate_files _file1 _file2 _output)
222 add_custom_command(
223 OUTPUT ${_output}
224 COMMAND cat ${_file1} ${_file2} > ${_output}
225 DEPENDS ${_file1}
226 DEPENDS ${_file2})
227 endmacro()
228 endif()
229
230 function(add_importlibs _module)
231 add_dependency_node(${_module})
232 foreach(LIB ${ARGN})
233 if ("${LIB}" MATCHES "msvcrt")
234 add_definitions(-D_DLL -D__USE_CRTIMP)
235 target_link_libraries(${_module} msvcrtex)
236 endif()
237 target_link_libraries(${_module} ${CMAKE_BINARY_DIR}/importlibs/lib${LIB}${CMAKE_STATIC_LIBRARY_SUFFIX})
238 add_dependencies(${_module} lib${LIB})
239 add_dependency_edge(${_module} ${LIB})
240 endforeach()
241 endfunction()