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