05558ea426b85240f27587893c141cd8e409c26f
[reactos.git] / CMakeMacros.cmake
1
2 MACRO(_PCH_GET_COMPILE_FLAGS _target_name _out_compile_flags _header_filename)
3
4 # Add the precompiled header to the build
5 get_filename_component(FILE ${_header_filename} NAME)
6 set(_gch_filename "${_target_name}_${FILE}.gch")
7 list(APPEND ${_out_compile_flags} -c ${_header_filename} -o ${_gch_filename})
8
9 # This gets us our includes
10 get_directory_property(DIRINC INCLUDE_DIRECTORIES)
11 foreach(item ${DIRINC})
12 list(APPEND ${_out_compile_flags} -I${item})
13 endforeach(item)
14
15 # This is a particular bit of undocumented/hacky magic I'm quite proud of
16 get_directory_property(_compiler_flags DEFINITIONS)
17 string(REPLACE "\ " "\t" _compiler_flags ${_compiler_flags})
18 list(APPEND ${_out_compile_flags} ${_compiler_flags})
19
20 # This gets any specific definitions that were added with set-target-property
21 get_target_property(_target_defs ${_target_name} COMPILE_DEFINITIONS)
22 if (_target_defs)
23 foreach(item ${_target_defs})
24 list(APPEND ${_out_compile_flags} -D${item})
25 endforeach(item)
26 endif()
27
28 ENDMACRO(_PCH_GET_COMPILE_FLAGS)
29
30 MACRO(add_pch _target_name _header_filename _src_list)
31
32 get_filename_component(FILE ${_header_filename} NAME)
33 set(_gch_filename "${_target_name}_${FILE}.gch")
34 list(APPEND ${_src_list} ${_gch_filename})
35 _PCH_GET_COMPILE_FLAGS(${_target_name} _args ${_header_filename})
36 file(REMOVE ${_gch_filename})
37 add_custom_command(
38 OUTPUT ${_gch_filename}
39 COMMAND ${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1} ${_args}
40 DEPENDS ${_header_filename})
41
42 ENDMACRO(add_pch _target_name _header_filename _src_list)
43
44 MACRO(spec2def _target_name _spec_file _def_file)
45
46 add_custom_command(
47 OUTPUT ${_def_file}
48 COMMAND native-winebuild -o ${_def_file} --def -E ${_spec_file} --filename ${_target_name}.dll
49 DEPENDS native-winebuild)
50 set_source_files_properties(${_def_file} PROPERTIES GENERATED TRUE)
51 add_custom_target(${_target_name}_def ALL DEPENDS ${_def_file})
52
53 ENDMACRO(spec2def _target_name _spec_file _def_file)
54
55 if (NOT MSVC)
56 MACRO(CreateBootSectorTarget _target_name _asm_file _object_file)
57
58 get_filename_component(OBJECT_PATH ${_object_file} PATH)
59 get_filename_component(OBJECT_NAME ${_object_file} NAME)
60 file(MAKE_DIRECTORY ${OBJECT_PATH})
61 get_directory_property(defines COMPILE_DEFINITIONS)
62 get_directory_property(includes INCLUDE_DIRECTORIES)
63
64 foreach(arg ${defines})
65 set(result_defs ${result_defs} -D${arg})
66 endforeach(arg ${defines})
67
68 foreach(arg ${includes})
69 set(result_incs -I${arg} ${result_incs})
70 endforeach(arg ${includes})
71
72 add_custom_command(
73 OUTPUT ${_object_file}
74 COMMAND nasm -o ${_object_file} ${result_incs} ${result_defs} -f bin ${_asm_file}
75 DEPENDS native-winebuild)
76 set_source_files_properties(${_object_file} PROPERTIES GENERATED TRUE)
77 add_custom_target(${_target_name} ALL DEPENDS ${_object_file})
78 add_minicd(${_object_file} loader ${OBJECT_NAME})
79 ENDMACRO(CreateBootSectorTarget _target_name _asm_file _object_file)
80 else()
81 MACRO(CreateBootSectorTarget _target_name _asm_file _object_file)
82 ENDMACRO()
83 endif()
84
85 MACRO(MACRO_IDL_COMPILE_OBJECT OBJECT SOURCE)
86 GET_PROPERTY(FLAGS SOURCE ${SOURCE} PROPERTY COMPILE_FLAGS)
87 GET_PROPERTY(DEFINES SOURCE ${SOURCE} PROPERTY COMPILE_DEFINITIONS)
88 GET_PROPERTY(INCLUDE_DIRECTORIES DIRECTORY PROPERTY INCLUDE_DIRECTORIES)
89 FOREACH(DIR ${INCLUDE_DIRECTORIES})
90 SET(FLAGS "${FLAGS} -I${DIR}")
91 ENDFOREACH()
92
93 SET(IDL_COMMAND ${CMAKE_IDL_COMPILE_OBJECT})
94 STRING(REPLACE "<CMAKE_IDL_COMPILER>" "${CMAKE_IDL_COMPILER}" IDL_COMMAND "${IDL_COMMAND}")
95 STRING(REPLACE <FLAGS> "${FLAGS}" IDL_COMMAND "${IDL_COMMAND}")
96 STRING(REPLACE "<DEFINES>" "${DEFINES}" IDL_COMMAND "${IDL_COMMAND}")
97 STRING(REPLACE "<OBJECT>" "${OBJECT}" IDL_COMMAND "${IDL_COMMAND}")
98 STRING(REPLACE "<SOURCE>" "${SOURCE}" IDL_COMMAND "${IDL_COMMAND}")
99 SEPARATE_ARGUMENTS(IDL_COMMAND)
100
101 ADD_CUSTOM_COMMAND(
102 OUTPUT ${OBJECT}
103 COMMAND ${IDL_COMMAND}
104 DEPENDS ${SOURCE}
105 VERBATIM
106 )
107 ENDMACRO()
108
109 MACRO(ADD_INTERFACE_DEFINITIONS TARGET)
110 FOREACH(SOURCE ${ARGN})
111 GET_FILENAME_COMPONENT(FILE ${SOURCE} NAME_WE)
112 SET(OBJECT ${CMAKE_CURRENT_BINARY_DIR}/${FILE}.h)
113 MACRO_IDL_COMPILE_OBJECT(${OBJECT} ${CMAKE_CURRENT_SOURCE_DIR}/${SOURCE})
114 LIST(APPEND OBJECTS ${OBJECT})
115 ENDFOREACH()
116 ADD_CUSTOM_TARGET(${TARGET} ALL DEPENDS ${OBJECTS})
117 ENDMACRO()
118
119 MACRO(add_minicd_target _targetname _dir) # optional parameter: _nameoncd
120 if("${ARGN}" STREQUAL "")
121 get_target_property(FILENAME ${_targetname} LOCATION)
122 get_filename_component(_nameoncd ${FILENAME} NAME)
123 else()
124 set(_nameoncd ${ARGN})
125 endif()
126
127 file(APPEND ${REACTOS_BINARY_DIR}/boot/ros_minicd_target.txt "${_targetname}\t${_dir}\t${_nameoncd}\n")
128 ENDMACRO(add_minicd_target)
129
130 MACRO(add_minicd FILENAME _dir _nameoncd)
131 file(APPEND ${REACTOS_BINARY_DIR}/boot/ros_minicd.txt "${FILENAME}\t${_dir}\t${_nameoncd}\n")
132 ENDMACRO(add_minicd)
133
134 macro(set_cpp)
135 include_directories(BEFORE ${REACTOS_SOURCE_DIR}/lib/3rdparty/stlport/stlport)
136 set(IS_CPP 1)
137 endmacro()
138
139 MACRO(add_livecd_target _targetname _dir )# optional parameter : _nameoncd
140 if("${ARGN}" STREQUAL "")
141 get_target_property(FILENAME ${_targetname} LOCATION)
142 get_filename_component(_nameoncd ${FILENAME} NAME)
143 else()
144 set(_nameoncd ${ARGN})
145 endif()
146
147 file(APPEND ${REACTOS_BINARY_DIR}/boot/ros_livecd_target.txt "${_targetname}\t${_dir}\t${_nameoncd}\n")
148 ENDMACRO(add_livecd_target)
149
150 MACRO(add_livecd FILENAME _dir)# optional parameter : _nameoncd
151 if("${ARGN}" STREQUAL "")
152 get_filename_component(_nameoncd ${FILENAME} NAME)
153 else()
154 set(_nameoncd ${ARGN})
155 endif()
156 file(APPEND ${REACTOS_BINARY_DIR}/boot/ros_livecd.txt "${FILENAME}\t${_dir}\t${_nameoncd}\n")
157 ENDMACRO(add_livecd)
158
159 macro(cab_to_dir _dir_num _var_name)
160 # 1 = system32
161 # 2 = system32\drivers
162 # 3 = Fonts
163 # 4 =
164 # 5 = system32\drivers\etc
165 # 6 = inf
166 # 7 = bin
167 # 8 = media
168 if(${_dir_num} STREQUAL "1")
169 set(${_var_name} "reactos/system32")
170 elseif(${_dir_num} STREQUAL "2")
171 set(${_var_name} "reactos/system32/drivers")
172 elseif(${_dir_num} STREQUAL "3")
173 set(${_var_name} "reactos/fonts")
174 elseif(${_dir_num} STREQUAL "4")
175 set(${_var_name} "reactos")
176 elseif(${_dir_num} STREQUAL "5")
177 set(${_var_name} "reactos/system32/drivers/etc")
178 elseif(${_dir_num} STREQUAL "6")
179 set(${_var_name} "reactos/inf")
180 elseif(${_dir_num} STREQUAL "7")
181 set(${_var_name} "reactos/bin")
182 elseif(${_dir_num} STREQUAL "8")
183 set(${_var_name} "reactos/system32/drivers")
184 else()
185 message(FATAL_ERROR "Wrong directory ${_dir_num}")
186 endif()
187 endmacro()
188
189 MACRO(add_cab_target _targetname _num )
190 file(APPEND ${REACTOS_BINARY_DIR}/boot/ros_cab_target.txt "${_targetname}\t${_num}\n")
191 cab_to_dir(${_num} _dir)
192 add_livecd_target(${_targetname} ${_dir})
193 ENDMACRO(add_cab_target)
194
195 MACRO(add_cab FILENAME _num)
196 file(APPEND ${REACTOS_BINARY_DIR}/boot/ros_cab.txt "${FILENAME}\t${_num}\n")
197 cab_to_dir(${_num} _dir)
198 add_livecd(${FILENAME} ${_dir})
199 ENDMACRO(add_cab)
200
201 macro(custom_incdefs)
202 if(NOT DEFINED result_incs) #rpc_defines
203 get_directory_property(rpc_defines COMPILE_DEFINITIONS)
204 get_directory_property(rpc_includes INCLUDE_DIRECTORIES)
205
206 foreach(arg ${rpc_defines})
207 set(result_defs ${result_defs} -D${arg})
208 endforeach(arg ${defines})
209
210 foreach(arg ${rpc_includes})
211 set(result_incs -I${arg} ${result_incs})
212 endforeach(arg ${includes})
213 endif()
214 endmacro(custom_incdefs)
215
216 macro(rpcproxy TARGET)
217 custom_incdefs()
218 list(APPEND SOURCE ${CMAKE_CURRENT_BINARY_DIR}/${TARGET}_proxy.dlldata.c)
219
220 foreach(_in_FILE ${ARGN})
221 get_filename_component(FILE ${_in_FILE} NAME_WE)
222 add_custom_command(
223 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${FILE}_p.h ${CMAKE_CURRENT_BINARY_DIR}/${FILE}_p.c
224 COMMAND native-widl ${result_incs} ${result_defs} -m32 --win32 -h -H ${CMAKE_CURRENT_BINARY_DIR}/${FILE}_p.h -p -P ${CMAKE_CURRENT_BINARY_DIR}/${FILE}_p.c ${CMAKE_CURRENT_SOURCE_DIR}/${FILE}.idl
225 DEPENDS native-widl)
226 set_source_files_properties(
227 ${CMAKE_CURRENT_BINARY_DIR}/${FILE}_c.h ${CMAKE_CURRENT_BINARY_DIR}/${FILE}_p.c
228 PROPERTIES GENERATED TRUE)
229 list(APPEND SOURCE ${CMAKE_CURRENT_BINARY_DIR}/${FILE}_p.c)
230 list(APPEND IDLS ${CMAKE_CURRENT_SOURCE_DIR}/${FILE}.idl)
231 list(APPEND PROXY_DEPENDS ${TARGET}_${FILE}_p)
232 add_custom_target(${TARGET}_${FILE}_p
233 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${FILE}_p.c)
234 #add_dependencies(${TARGET}_proxy ${TARGET}_${FILE}_p)
235 endforeach(_in_FILE ${ARGN})
236
237 add_custom_command(
238 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${TARGET}_proxy.dlldata.c
239 COMMAND native-widl ${result_incs} ${result_defs} -m32 --win32 --dlldata-only --dlldata=${CMAKE_CURRENT_BINARY_DIR}/${TARGET}_proxy.dlldata.c ${IDLS}
240 DEPENDS native-widl)
241 set_source_files_properties(
242 ${CMAKE_CURRENT_BINARY_DIR}/${TARGET}_proxy.dlldata.c
243 PROPERTIES GENERATED TRUE)
244
245 add_library(${TARGET}_proxy ${SOURCE})
246 add_dependencies(${TARGET}_proxy psdk ${PROXY_DEPENDS})
247 endmacro(rpcproxy)
248
249 macro (MACRO_IDL_FILES)
250 custom_incdefs()
251 foreach(_in_FILE ${ARGN})
252 get_filename_component(FILE ${_in_FILE} NAME_WE)
253 add_custom_command(
254 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${FILE}_s.h ${CMAKE_CURRENT_BINARY_DIR}/${FILE}_s.c
255 COMMAND ${IDL_COMPILER} ${result_incs} ${result_defs} ${IDL_FLAGS} ${IDL_HEADER_ARG} ${CMAKE_CURRENT_BINARY_DIR}/${FILE}_s.h ${IDL_SERVER_ARG} ${CMAKE_CURRENT_BINARY_DIR}/${FILE}_s.c ${CMAKE_CURRENT_SOURCE_DIR}/${FILE}.idl
256 DEPENDS ${_in_file})
257 set_source_files_properties(
258 ${CMAKE_CURRENT_BINARY_DIR}/${FILE}_s.h ${CMAKE_CURRENT_BINARY_DIR}/${FILE}_s.c
259 PROPERTIES GENERATED TRUE)
260 add_library(${FILE}_server ${CMAKE_CURRENT_BINARY_DIR}/${FILE}_s.c)
261 add_dependencies(${FILE}_server psdk)
262
263 add_custom_command(
264 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${FILE}_c.h ${CMAKE_CURRENT_BINARY_DIR}/${FILE}_c.c
265 COMMAND ${IDL_COMPILER} ${result_incs} ${result_defs} ${IDL_FLAGS} ${IDL_HEADER_ARG} ${CMAKE_CURRENT_BINARY_DIR}/${FILE}_c.h ${IDL_CLIENT_ARG} ${CMAKE_CURRENT_BINARY_DIR}/${FILE}_c.c ${CMAKE_CURRENT_SOURCE_DIR}/${FILE}.idl
266 DEPENDS ${_in_file})
267 set_source_files_properties(
268 ${CMAKE_CURRENT_BINARY_DIR}/${FILE}_c.h ${CMAKE_CURRENT_BINARY_DIR}/${FILE}_c.c
269 PROPERTIES GENERATED TRUE)
270 add_library(${FILE}_client ${CMAKE_CURRENT_BINARY_DIR}/${FILE}_c.c)
271 add_dependencies(${FILE}_client psdk)
272 endforeach(_in_FILE ${ARGN})
273
274 endmacro (MACRO_IDL_FILES)
275
276 macro(ADD_TYPELIB TARGET)
277 custom_incdefs()
278 foreach(SOURCE ${ARGN})
279 get_filename_component(FILE ${SOURCE} NAME_WE)
280 set(OBJECT ${CMAKE_CURRENT_BINARY_DIR}/${FILE}.tlb)
281 add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${FILE}.tlb
282 COMMAND ${IDL_COMPILER} ${result_incs} ${IDL_FLAGS} ${IDL_TYPELIB_ARG} ${CMAKE_CURRENT_BINARY_DIR}/${FILE}.tlb ${CMAKE_CURRENT_SOURCE_DIR}/${SOURCE}
283 DEPENDS ${SOURCE})
284 list(APPEND OBJECTS ${OBJECT})
285 endforeach()
286 add_custom_target(${TARGET} ALL DEPENDS ${OBJECTS})
287 endmacro()