07a8458d0ffb390bae598759e3fc9c71c9e116a1
[reactos.git] / reactos / cmake / idl-support.cmake
1
2 #idl files support
3 if(MSVC)
4 set(IDL_COMPILER midl)
5 set(IDL_HEADER_ARG /h) #.h
6 set(IDL_HEADER_ARG2 /header) #.h
7 set(IDL_TYPELIB_ARG /tlb) #.tlb
8 set(IDL_SERVER_ARG /sstub) #.c for stub server library
9 set(IDL_CLIENT_ARG /cstub) #.c for stub client library
10 set(IDL_PROXY_ARG /proxy)
11 set(IDL_INTERFACE_ARG /iid)
12 if(ARCH MATCHES i386)
13 set(IDL_FLAGS /win32)
14 elseif(ARCH MATCHES amd64)
15 set(IDL_FLAGS /amd64)
16 else()
17 set(IDL_FLAGS "")
18 endif()
19 set(IDL_DEPENDS "")
20 else()
21 set(IDL_COMPILER native-widl)
22 set(IDL_HEADER_ARG -h -o) #.h
23 set(IDL_HEADER_ARG2 -h -H) #.h
24 set(IDL_TYPELIB_ARG -t -o) #.tlb
25 set(IDL_SERVER_ARG -Oif -s -o) #.c for server library
26 set(IDL_CLIENT_ARG -Oif -c -o) #.c for stub client library
27 set(IDL_PROXY_ARG -p -o)
28 set(IDL_INTERFACE_ARG -u -o)
29 if(ARCH MATCHES i386)
30 set(IDL_FLAGS -m32 --win32)
31 elseif(ARCH MATCHES amd64)
32 set(IDL_FLAGS -m64 --win64)
33 else()
34 set(IDL_FLAGS "")
35 endif()
36 set(IDL_DEPENDS native-widl)
37 endif()
38
39
40 function(get_includes OUTPUT_VAR)
41 get_directory_property(_includes INCLUDE_DIRECTORIES)
42 foreach(arg ${_includes})
43 list(APPEND __tmp_var -I${arg})
44 endforeach()
45 set(${OUTPUT_VAR} ${__tmp_var} PARENT_SCOPE)
46 endfunction()
47
48 function(get_defines OUTPUT_VAR)
49 get_directory_property(_defines COMPILE_DEFINITIONS)
50 foreach(arg ${_defines})
51 list(APPEND __tmp_var -D${arg})
52 endforeach()
53 set(${OUTPUT_VAR} ${__tmp_var} PARENT_SCOPE)
54 endfunction()
55
56 function(add_typelib TARGET)
57 get_includes(INCLUDES)
58 get_defines(DEFINES)
59 foreach(FILE ${ARGN})
60 get_filename_component(NAME ${FILE} NAME_WE)
61 add_custom_command(
62 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${NAME}.tlb
63 COMMAND ${IDL_COMPILER} ${INCLUDES} ${DEFINES} ${IDL_FLAGS} ${IDL_TYPELIB_ARG} ${CMAKE_CURRENT_BINARY_DIR}/${NAME}.tlb ${CMAKE_CURRENT_SOURCE_DIR}/${FILE}
64 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${FILE} ${IDL_DEPENDS})
65 list(APPEND OBJECTS ${CMAKE_CURRENT_BINARY_DIR}/${NAME}.tlb)
66 endforeach()
67 add_custom_target(${TARGET} ALL DEPENDS ${OBJECTS})
68 endfunction()
69
70 function(add_idl_headers TARGET)
71 get_includes(INCLUDES)
72 get_defines(DEFINES)
73 foreach(FILE ${ARGN})
74 get_filename_component(NAME ${FILE} NAME_WE)
75 set(HEADER ${CMAKE_CURRENT_BINARY_DIR}/${NAME}.h)
76 add_custom_command(
77 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${NAME}.h
78 COMMAND ${IDL_COMPILER} ${INCLUDES} ${DEFINES} ${IDL_FLAGS} ${IDL_HEADER_ARG} ${CMAKE_CURRENT_BINARY_DIR}/${NAME}.h ${CMAKE_CURRENT_SOURCE_DIR}/${FILE}
79 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${FILE} ${IDL_DEPENDS})
80 list(APPEND HEADERS ${HEADER})
81 endforeach()
82 add_custom_target(${TARGET} DEPENDS ${HEADERS})
83 endfunction()
84
85 function(add_rpcproxy_files)
86 get_includes(INCLUDES)
87 get_defines(DEFINES)
88
89 if(MSVC)
90 set(DLLDATA_ARG /dlldata ${CMAKE_CURRENT_BINARY_DIR}/proxy.dlldata.c)
91 set(DLLDATA_DEPENDENCIES "")
92 endif()
93 foreach(FILE ${ARGN})
94 get_filename_component(NAME ${FILE} NAME_WE)
95 if(MSVC)
96 set(DLLDATA_DEPENDENCIES ${DLLDATA_DEPENDENCIES} ${CMAKE_CURRENT_BINARY_DIR}/${NAME}_p.c)
97 else()
98 list(APPEND IDLS ${CMAKE_CURRENT_SOURCE_DIR}/${FILE})
99 endif()
100 add_custom_command(
101 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${NAME}_p.c ${CMAKE_CURRENT_BINARY_DIR}/${NAME}_p.h
102 COMMAND ${IDL_COMPILER} ${INCLUDES} ${DEFINES} ${IDL_FLAGS} ${IDL_PROXY_ARG} ${CMAKE_CURRENT_BINARY_DIR}/${NAME}_p.c ${IDL_HEADER_ARG2} ${NAME}_p.h ${CMAKE_CURRENT_SOURCE_DIR}/${FILE} ${DLLDATA_ARG}
103 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${FILE} ${IDL_DEPENDS})
104 endforeach()
105
106 # Extra pass to generate dlldata
107 if(MSVC)
108 #touch it, so we're sure it's older than its dependencies
109 add_custom_command(
110 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/proxy.dlldata.c
111 COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/proxy.dlldata.c
112 DEPENDS ${DLLDATA_DEPENDENCIES})
113 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/proxy.dlldata.c PROPERTIES GENERATED TRUE)
114 else()
115 add_custom_command(
116 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/proxy.dlldata.c
117 COMMAND ${IDL_COMPILER} ${INCLUDES} ${DEFINES} ${IDL_FLAGS} --dlldata-only -o ${CMAKE_CURRENT_BINARY_DIR}/proxy.dlldata.c ${IDLS}
118 DEPENDS ${IDLS} ${IDL_DEPENDS})
119 endif()
120 endfunction()
121
122 function(add_rpc_files __type)
123 get_includes(INCLUDES)
124 get_defines(DEFINES)
125 # Is it a client or server module?
126 if(__type STREQUAL server)
127 set(__server_client ${IDL_SERVER_ARG})
128 set(__suffix _s)
129 elseif(__type STREQUAL client)
130 set(__server_client ${IDL_CLIENT_ARG})
131 set(__suffix _c)
132 else()
133 message(FATAL_ERROR "Please pass either server or client as argument to add_rpc_files")
134 endif()
135 foreach(FILE ${ARGN})
136 get_filename_component(__name ${FILE} NAME_WE)
137 set(__name ${__name}${__suffix})
138 if(NOT IS_ABSOLUTE ${FILE})
139 set(FILE ${CMAKE_CURRENT_SOURCE_DIR}/${FILE})
140 endif()
141 add_custom_command(
142 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${__name}.c ${CMAKE_CURRENT_BINARY_DIR}/${__name}.h
143 COMMAND ${IDL_COMPILER} ${INCLUDES} ${DEFINES} ${IDL_FLAGS} ${IDL_HEADER_ARG2} ${CMAKE_CURRENT_BINARY_DIR}/${__name}.h ${__server_client} ${CMAKE_CURRENT_BINARY_DIR}/${__name}.c ${FILE}
144 DEPENDS ${FILE} ${IDL_DEPENDS})
145 endforeach()
146 endfunction()
147
148 function(generate_idl_iids IDL_FILE)
149 get_filename_component(FILE ${IDL_FILE} NAME)
150 if(FILE STREQUAL "${IDL_FILE}")
151 set(IDL_FILE_FULL "${CMAKE_CURRENT_SOURCE_DIR}/${IDL_FILE}")
152 else()
153 set(IDL_FILE_FULL ${IDL_FILE})
154 endif()
155 get_includes(INCLUDES)
156 get_defines(DEFINES)
157 get_filename_component(NAME ${IDL_FILE} NAME_WE)
158 add_custom_command(
159 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${NAME}_i.c
160 COMMAND ${IDL_COMPILER} ${INCLUDES} ${DEFINES} ${IDL_FLAGS} ${IDL_INTERFACE_ARG} ${CMAKE_CURRENT_BINARY_DIR}/${NAME}_i.c ${IDL_FILE_FULL}
161 DEPENDS ${IDL_FILE_FULL} ${IDL_DEPENDS})
162 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${NAME}_i.c PROPERTIES GENERATED TRUE)
163 endfunction()
164
165 function(add_iid_library TARGET)
166 foreach(IDL_FILE ${ARGN})
167 get_filename_component(NAME ${IDL_FILE} NAME_WE)
168 generate_idl_iids(${IDL_FILE})
169 list(APPEND IID_SOURCES ${NAME}_i.c)
170 endforeach()
171 add_library(${TARGET} ${IID_SOURCES})
172 add_dependencies(${TARGET} psdk)
173 set_target_properties(${TARGET} PROPERTIES EXCLUDE_FROM_ALL TRUE)
174 endfunction()