7ca60975695736b336563493d27519040e874ed8
[reactos.git] / 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 /h) #.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 else()
20 set(IDL_COMPILER native-widl)
21 set(IDL_HEADER_ARG -h -o) #.h
22 set(IDL_HEADER_ARG2 -h -H) #.h
23 set(IDL_TYPELIB_ARG -t -o) #.tlb
24 set(IDL_SERVER_ARG -s -S) #.c for server library
25 set(IDL_CLIENT_ARG -c -C) #.c for stub client library
26 set(IDL_PROXY_ARG -p -P)
27 set(IDL_INTERFACE_ARG -u -o)
28 if(ARCH MATCHES i386)
29 set(IDL_FLAGS -m32 --win32)
30 elseif(ARCH MATCHES amd64)
31 set(IDL_FLAGS -m64 --win64)
32 else()
33 set(IDL_FLAGS "")
34 endif()
35 endif()
36
37
38 macro(get_includes OUTPUT_VAR)
39 set(${OUTPUT_VAR} "")
40 get_directory_property(_includes INCLUDE_DIRECTORIES)
41 foreach(arg ${_includes})
42 set(${OUTPUT_VAR} -I${arg} ${${OUTPUT_VAR}})
43 endforeach()
44 endmacro()
45
46 macro(get_defines OUTPUT_VAR)
47 set(${OUTPUT_VAR} "")
48 get_directory_property(_defines COMPILE_DEFINITIONS)
49 foreach(arg ${_defines})
50 set(${OUTPUT_VAR} ${${OUTPUT_VAR}} -D${arg})
51 endforeach()
52 endmacro()
53
54 macro(add_typelib TARGET)
55 get_includes(INCLUDES)
56 get_defines(DEFINES)
57 foreach(FILE ${ARGN})
58 get_filename_component(NAME ${FILE} NAME_WE)
59 add_custom_command(
60 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${NAME}.tlb
61 COMMAND ${IDL_COMPILER} ${INCLUDES} ${DEFINES} ${IDL_FLAGS} ${IDL_TYPELIB_ARG} ${CMAKE_CURRENT_BINARY_DIR}/${NAME}.tlb ${CMAKE_CURRENT_SOURCE_DIR}/${FILE}
62 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${FILE})
63 list(APPEND OBJECTS ${CMAKE_CURRENT_BINARY_DIR}/${NAME}.tlb)
64 endforeach()
65 add_custom_target(${TARGET} ALL DEPENDS ${OBJECTS})
66 endmacro()
67
68 macro(add_idl_headers TARGET)
69 get_includes(INCLUDES)
70 get_defines(DEFINES)
71 foreach(FILE ${ARGN})
72 get_filename_component(NAME ${FILE} NAME_WE)
73 set(HEADER ${CMAKE_CURRENT_BINARY_DIR}/${NAME}.h)
74 add_custom_command(
75 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${NAME}.h
76 COMMAND ${IDL_COMPILER} ${INCLUDES} ${DEFINES} ${IDL_FLAGS} ${IDL_HEADER_ARG} ${CMAKE_CURRENT_BINARY_DIR}/${NAME}.h ${CMAKE_CURRENT_SOURCE_DIR}/${FILE}
77 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${FILE})
78 list(APPEND HEADERS ${HEADER})
79 endforeach()
80 add_custom_target(${TARGET} ALL DEPENDS ${HEADERS})
81 endmacro()
82
83 macro(add_rpcproxy_library TARGET)
84 get_includes(INCLUDES)
85 get_defines(DEFINES)
86 foreach(FILE ${ARGN})
87 get_filename_component(NAME ${FILE} NAME_WE)
88 if(MSVC)
89 set(IDL_DLLDATA_ARG /dlldata ${CMAKE_CURRENT_BINARY_DIR}/${TARGET}_proxy.dlldata.c)
90 else()
91 set(IDL_DLLDATA_ARG "")
92 endif()
93 add_custom_command(
94 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${NAME}_p.h ${CMAKE_CURRENT_BINARY_DIR}/${NAME}_p.c
95 COMMAND ${IDL_COMPILER} ${INCLUDES} ${DEFINES} ${IDL_FLAGS} ${IDL_HEADER_ARG} ${CMAKE_CURRENT_BINARY_DIR}/${NAME}_p.h ${IDL_PROXY_ARG} ${CMAKE_CURRENT_BINARY_DIR}/${NAME}_p.c ${IDL_DLLDATA_ARG} ${CMAKE_CURRENT_SOURCE_DIR}/${FILE}
96 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${FILE})
97 set_source_files_properties(
98 ${CMAKE_CURRENT_BINARY_DIR}/${NAME}_c.h ${CMAKE_CURRENT_BINARY_DIR}/${NAME}_p.c
99 PROPERTIES GENERATED TRUE)
100 list(APPEND rpcproxy_SOURCES ${CMAKE_CURRENT_BINARY_DIR}/${NAME}_p.c)
101 list(APPEND IDLS ${CMAKE_CURRENT_SOURCE_DIR}/${NAME}.idl)
102 endforeach()
103
104 if(NOT MSVC)
105 # Extra pass to generate dlldata for widl
106 add_custom_command(
107 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${TARGET}_proxy.dlldata.c
108 COMMAND ${IDL_COMPILER} ${INCLUDES} ${DEFINES} ${IDL_FLAGS} --dlldata-only -o ${CMAKE_CURRENT_BINARY_DIR}/${TARGET}_proxy.dlldata.c ${IDLS}
109 DEPENDS ${IDLS})
110 endif()
111 set_source_files_properties(
112 ${CMAKE_CURRENT_BINARY_DIR}/${TARGET}_proxy.dlldata.c
113 PROPERTIES GENERATED TRUE)
114 add_library(${TARGET} ${CMAKE_CURRENT_BINARY_DIR}/${TARGET}_proxy.dlldata.c ${rpcproxy_SOURCES})
115 endmacro()
116
117 macro(add_rpc_library TARGET)
118 get_includes(INCLUDES)
119 get_defines(DEFINES)
120 foreach(FILE ${ARGN})
121 get_filename_component(NAME ${FILE} NAME_WE)
122 add_custom_command(
123 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${NAME}_s.h ${CMAKE_CURRENT_BINARY_DIR}/${NAME}_s.c
124 COMMAND ${IDL_COMPILER} ${INCLUDES} ${DEFINES} ${IDL_FLAGS} ${IDL_HEADER_ARG2} ${CMAKE_CURRENT_BINARY_DIR}/${NAME}_s.h ${IDL_SERVER_ARG} ${CMAKE_CURRENT_BINARY_DIR}/${NAME}_s.c ${CMAKE_CURRENT_SOURCE_DIR}/${FILE}
125 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${FILE})
126 set_source_files_properties(
127 ${CMAKE_CURRENT_BINARY_DIR}/${NAME}_s.h ${CMAKE_CURRENT_BINARY_DIR}/${NAME}_s.c
128 PROPERTIES GENERATED TRUE)
129 list(APPEND server_SOURCES ${CMAKE_CURRENT_BINARY_DIR}/${NAME}_s.c)
130
131 add_custom_command(
132 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${NAME}_c.h ${CMAKE_CURRENT_BINARY_DIR}/${NAME}_c.c
133 COMMAND ${IDL_COMPILER} ${INCLUDES} ${DEFINES} ${IDL_FLAGS} ${IDL_HEADER_ARG2} ${CMAKE_CURRENT_BINARY_DIR}/${NAME}_c.h ${IDL_CLIENT_ARG} ${CMAKE_CURRENT_BINARY_DIR}/${NAME}_c.c ${CMAKE_CURRENT_SOURCE_DIR}/${FILE}
134 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${FILE})
135 set_source_files_properties(
136 ${CMAKE_CURRENT_BINARY_DIR}/${NAME}_c.h ${CMAKE_CURRENT_BINARY_DIR}/${NAME}_c.c
137 PROPERTIES GENERATED TRUE)
138 list(APPEND client_SOURCES ${CMAKE_CURRENT_BINARY_DIR}/${NAME}_c.c)
139 endforeach()
140 add_library(${TARGET} ${server_SOURCES} ${client_SOURCES})
141 add_dependencies(${TARGET} psdk)
142 endmacro()
143
144 macro(generate_idl_iids IDL_FILE)
145 get_filename_component(FILE ${IDL_FILE} NAME)
146 if(FILE STREQUAL "${IDL_FILE}")
147 set(IDL_FILE_FULL "${CMAKE_CURRENT_SOURCE_DIR}/${IDL_FILE}")
148 else()
149 set(IDL_FILE_FULL ${IDL_FILE})
150 endif()
151 get_includes(INCLUDES)
152 get_defines(DEFINES)
153 get_filename_component(NAME ${IDL_FILE} NAME_WE)
154 add_custom_command(
155 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${NAME}_i.c
156 COMMAND ${IDL_COMPILER} ${INCLUDES} ${DEFINES} ${IDL_FLAGS} ${IDL_INTERFACE_ARG} ${CMAKE_CURRENT_BINARY_DIR}/${NAME}_i.c ${IDL_FILE_FULL}
157 DEPENDS ${IDL_FILE_FULL})
158 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${NAME}_i.c PROPERTIES GENERATED TRUE)
159 endmacro()
160
161 macro(add_iid_library TARGET)
162 foreach(IDL_FILE ${ARGN})
163 get_filename_component(NAME ${IDL_FILE} NAME_WE)
164 generate_idl_iids(${IDL_FILE})
165 list(APPEND IID_SOURCES ${NAME}_i.c)
166 endforeach()
167 add_library(${TARGET} ${IID_SOURCES})
168 add_dependencies(${TARGET} psdk)
169 endmacro()