[CMAKE]
[reactos.git] / reactos / cmake / widl-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 /nologo /win32 /no_def_idir)
14 elseif(ARCH MATCHES amd64)
15 set(IDL_FLAGS /nologo /amd64 /no_def_idir)
16 else()
17 set(IDL_FLAGS /nologo /no_def_idir)
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)
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 endfunction()
68
69 function(add_idl_headers TARGET)
70 get_includes(INCLUDES)
71 get_defines(DEFINES)
72 foreach(FILE ${ARGN})
73 get_filename_component(NAME ${FILE} NAME_WE)
74 set(HEADER ${CMAKE_CURRENT_BINARY_DIR}/${NAME}.h)
75 add_custom_command(
76 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${NAME}.h
77 COMMAND ${IDL_COMPILER} ${INCLUDES} ${DEFINES} ${IDL_FLAGS} ${IDL_HEADER_ARG} ${CMAKE_CURRENT_BINARY_DIR}/${NAME}.h ${CMAKE_CURRENT_SOURCE_DIR}/${FILE}
78 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${FILE} ${IDL_DEPENDS})
79 list(APPEND HEADERS ${HEADER})
80 endforeach()
81 add_custom_target(${TARGET} DEPENDS ${HEADERS})
82 endfunction()
83
84 function(add_rpcproxy_files)
85 get_includes(INCLUDES)
86 get_defines(DEFINES)
87
88 if(MSVC)
89 set(DLLDATA_ARG /dlldata ${CMAKE_CURRENT_BINARY_DIR}/proxy.dlldata.c)
90 set(OUTPUT_FILES "")
91 endif()
92 foreach(FILE ${ARGN})
93 get_filename_component(NAME ${FILE} NAME_WE)
94 if(MSVC)
95 add_custom_command(
96 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${NAME}_p.c ${CMAKE_CURRENT_BINARY_DIR}/${NAME}_p.h ${CMAKE_CURRENT_BINARY_DIR}/proxy.dlldata.c
97 COMMAND midl ${INCLUDES} ${DEFINES} ${IDL_FLAGS} /proxy ${CMAKE_CURRENT_BINARY_DIR}/${NAME}_p.c /h ${NAME}_p.h ${CMAKE_CURRENT_SOURCE_DIR}/${FILE} /dlldata ${CMAKE_CURRENT_BINARY_DIR}/proxy.dlldata.c
98 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${FILE} ${OUTPUT_FILES})
99 list(APPEND OUTPUT_FILES ${CMAKE_CURRENT_BINARY_DIR}/${NAME}_p.c)
100 list(APPEND OUTPUT_FILES ${CMAKE_CURRENT_BINARY_DIR}/${NAME}_p.h)
101 else()
102 list(APPEND IDLS ${CMAKE_CURRENT_SOURCE_DIR}/${FILE})
103 add_custom_command(
104 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${NAME}_p.c ${CMAKE_CURRENT_BINARY_DIR}/${NAME}_p.h
105 COMMAND native-widl ${INCLUDES} ${DEFINES} ${IDL_FLAGS} -p -o ${CMAKE_CURRENT_BINARY_DIR}/${NAME}_p.c -h -H ${NAME}_p.h ${CMAKE_CURRENT_SOURCE_DIR}/${FILE}
106 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${FILE} native-widl)
107 endif()
108 endforeach()
109
110 # Extra pass to generate dlldata
111 if(MSVC)
112 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/proxy.dlldata.c PROPERTIES GENERATED TRUE)
113 else()
114 add_custom_command(
115 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/proxy.dlldata.c
116 COMMAND ${IDL_COMPILER} ${INCLUDES} ${DEFINES} ${IDL_FLAGS} --dlldata-only -o ${CMAKE_CURRENT_BINARY_DIR}/proxy.dlldata.c ${IDLS}
117 DEPENDS ${IDLS} ${IDL_DEPENDS})
118 endif()
119 endfunction()
120
121 function(add_rpc_files __type)
122 get_includes(INCLUDES)
123 get_defines(DEFINES)
124 # Is it a client or server module?
125 if(__type STREQUAL server)
126 set(__server_client ${IDL_SERVER_ARG})
127 set(__suffix _s)
128 elseif(__type STREQUAL client)
129 set(__server_client ${IDL_CLIENT_ARG})
130 set(__suffix _c)
131 else()
132 message(FATAL_ERROR "Please pass either server or client as argument to add_rpc_files")
133 endif()
134 foreach(FILE ${ARGN})
135 get_filename_component(__name ${FILE} NAME_WE)
136 set(__name ${__name}${__suffix})
137 if(NOT IS_ABSOLUTE ${FILE})
138 set(FILE ${CMAKE_CURRENT_SOURCE_DIR}/${FILE})
139 endif()
140 add_custom_command(
141 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${__name}.c ${CMAKE_CURRENT_BINARY_DIR}/${__name}.h
142 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}
143 DEPENDS ${FILE} ${IDL_DEPENDS})
144 endforeach()
145 endfunction()
146
147 function(generate_idl_iids IDL_FILE)
148 get_filename_component(FILE ${IDL_FILE} NAME)
149 if(FILE STREQUAL "${IDL_FILE}")
150 set(IDL_FILE_FULL "${CMAKE_CURRENT_SOURCE_DIR}/${IDL_FILE}")
151 else()
152 set(IDL_FILE_FULL ${IDL_FILE})
153 endif()
154 get_includes(INCLUDES)
155 get_defines(DEFINES)
156 get_filename_component(NAME ${IDL_FILE} NAME_WE)
157 add_custom_command(
158 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${NAME}_i.c
159 COMMAND ${IDL_COMPILER} ${INCLUDES} ${DEFINES} ${IDL_FLAGS} ${IDL_INTERFACE_ARG} ${CMAKE_CURRENT_BINARY_DIR}/${NAME}_i.c ${IDL_FILE_FULL}
160 DEPENDS ${IDL_FILE_FULL} ${IDL_DEPENDS})
161 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${NAME}_i.c PROPERTIES GENERATED TRUE)
162 endfunction()
163
164 function(add_iid_library TARGET)
165 foreach(IDL_FILE ${ARGN})
166 get_filename_component(NAME ${IDL_FILE} NAME_WE)
167 generate_idl_iids(${IDL_FILE})
168 list(APPEND IID_SOURCES ${NAME}_i.c)
169 endforeach()
170 add_library(${TARGET} ${IID_SOURCES})
171 add_dependencies(${TARGET} psdk)
172 set_target_properties(${TARGET} PROPERTIES EXCLUDE_FROM_ALL TRUE)
173 endfunction()