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