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