[CMAKE]
[reactos.git] / msc.cmake
1
2 if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86")
3 add_definitions(-D__i386__)
4 endif()
5
6 add_definitions(-Dinline=__inline)
7
8 if(NOT CMAKE_CROSSCOMPILING)
9
10 else()
11
12 add_definitions(/GS- /Zl /Zi)
13 add_definitions(-Dinline=__inline -D__STDC__=1)
14
15 IF(${_MACHINE_ARCH_FLAG} MATCHES X86)
16 SET (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SAFESEH:NO")
17 SET (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /SAFESEH:NO")
18 SET (CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} /SAFESEH:NO")
19 ENDIF()
20
21 link_directories("${REACTOS_BINARY_DIR}/importlibs" ${REACTOS_BINARY_DIR}/lib/3rdparty/mingw)
22
23 set(CMAKE_RC_CREATE_SHARED_LIBRARY "<CMAKE_C_COMPILER> <CMAKE_SHARED_LIBRARY_C_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>")
24
25 MACRO(add_pch _target_name _header_filename _src_list)
26 get_filename_component(FILE ${_header_filename} NAME)
27 set(_gch_filename "${_target_name}_${FILE}.gch")
28 add_custom_command(
29 OUTPUT ${_gch_filename}
30 COMMAND echo Ignoring precompiled header
31 DEPENDS ${_header_filename})
32 ENDMACRO(add_pch _target_name _header_filename _src_list)
33
34 macro(add_linkerflag MODULE _flag)
35 set(NEW_LINKER_FLAGS ${_flag})
36 get_target_property(LINKER_FLAGS ${MODULE} LINK_FLAGS)
37 if(LINKER_FLAGS)
38 set(NEW_LINKER_FLAGS "${LINKER_FLAGS} ${NEW_LINKER_FLAGS}")
39 endif()
40 set_target_properties(${MODULE} PROPERTIES LINK_FLAGS ${NEW_LINKER_FLAGS})
41 endmacro()
42
43 macro(set_entrypoint MODULE ENTRYPOINT)
44 if(${ENTRYPOINT} STREQUAL "0")
45 add_linkerflag(${MODULE} "/NOENTRY")
46 else()
47 add_linkerflag(${MODULE} "/ENTRY:${ENTRYPOINT}")
48 endif()
49 endmacro()
50
51 macro(set_subsystem MODULE SUBSYSTEM)
52 add_linkerflag(${MODULE} "/subsystem:${SUBSYSTEM}")
53 endmacro()
54
55 macro(set_image_base MODULE IMAGE_BASE)
56 add_linkerflag(${MODULE} "/BASE:${IMAGE_BASE}")
57 endmacro()
58
59 macro(set_module_type MODULE TYPE)
60 add_dependencies(${MODULE} psdk buildno_header)
61 if(${TYPE} MATCHES nativecui)
62 set_subsystem(${MODULE} native)
63 set_entrypoint(${MODULE} NtProcessStartup@4)
64 endif()
65 if (${TYPE} MATCHES win32gui)
66 set_subsystem(${MODULE} windows)
67 set_entrypoint(${MODULE} WinMainCRTStartup)
68 if(IS_UNICODE)
69 target_link_libraries(${MODULE} mingw_wmain)
70 else()
71 target_link_libraries(${MODULE} mingw_main)
72 endif()
73 target_link_libraries(${MODULE} mingw_common msvcsup)
74 endif ()
75 if (${TYPE} MATCHES win32cui)
76 set_subsystem(${MODULE} console)
77 set_entrypoint(${MODULE} mainCRTStartup)
78 if(IS_UNICODE)
79 target_link_libraries(${MODULE} mingw_wmain)
80 else()
81 target_link_libraries(${MODULE} mingw_main)
82 endif()
83 target_link_libraries(${MODULE} mingw_common msvcsup)
84 endif ()
85 if(${TYPE} MATCHES win32dll)
86 # Need this only because mingw library is broken
87 set_entrypoint(${MODULE} DllMainCRTStartup@12)
88 if(DEFINED baseaddress_${MODULE})
89 set_image_base(${MODULE} ${baseaddress_${MODULE}})
90 else()
91 message(STATUS "${MODULE} has no base address")
92 endif()
93 target_link_libraries(${MODULE} mingw_common mingw_dllmain msvcsup)
94 add_linkerflag(${MODULE} "/DLL")
95 endif()
96 if(${TYPE} MATCHES win32ocx)
97 set_entrypoint(${MODULE} DllMainCRTStartup@12)
98 set_target_properties(${MODULE} PROPERTIES SUFFIX ".ocx")
99 target_link_libraries(${MODULE} mingw_common mingw_dllmain msvcsup)
100 add_linkerflag(${MODULE} "/DLL")
101 endif()
102 if(${TYPE} MATCHES cpl)
103 set_entrypoint(${MODULE} DllMainCRTStartup@12)
104 set_target_properties(${MODULE} PROPERTIES SUFFIX ".cpl")
105 target_link_libraries(${MODULE} mingw_common mingw_dllmain msvcsup)
106 add_linkerflag(${MODULE} "/DLL")
107 endif()
108 if(${TYPE} MATCHES kernelmodedriver)
109 set_target_properties(${MODULE} PROPERTIES SUFFIX ".sys")
110 set_entrypoint(${MODULE} DriverEntry@8)
111 set_subsystem(${MODULE} native)
112 set_image_base(${MODULE} 0x00010000)
113 add_linkerflag(${MODULE} "/DRIVER")
114 add_dependencies(${MODULE} bugcodes)
115 endif()
116
117 endmacro()
118
119 macro(set_unicode)
120 add_definitions(-DUNICODE -D_UNICODE)
121 set(IS_UNICODE 1)
122 endmacro()
123
124 set(CMAKE_C_FLAGS_DEBUG_INIT "/D_DEBUG /MDd /Zi /Ob0 /Od")
125 set(CMAKE_CXX_FLAGS_DEBUG_INIT "/D_DEBUG /MDd /Zi /Ob0 /Od")
126
127 macro(set_rc_compiler)
128 # dummy, this workaround is only needed in mingw due to lack of RC support in cmake
129 endmacro()
130
131 #idl files support
132 set(IDL_COMPILER midl)
133 set(IDL_FLAGS /win32)
134 set(IDL_HEADER_ARG /h) #.h
135 set(IDL_TYPELIB_ARG /tlb) #.tlb
136 set(IDL_SERVER_ARG /sstub) #.c for stub server library
137 set(IDL_CLIENT_ARG /cstub) #.c for stub client library
138 set(IDL_PROXY_ARG /proxy)
139 set(IDL_DLLDATA_ARG /dlldata )
140
141 # Thanks MS for creating a stupid linker
142 macro(add_importlib_target _spec_file)
143 get_filename_component(_name ${_spec_file} NAME_WE)
144
145 # Generate the asm stub file and the export def file
146 add_custom_command(
147 OUTPUT ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_stubs.asm ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_exp.def
148 COMMAND native-spec2def -@ -r -d=${CMAKE_BINARY_DIR}/importlibs/lib${_name}_exp.def -l=${CMAKE_BINARY_DIR}/importlibs/lib${_name}_stubs.asm ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file}
149 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file})
150
151 # Assemble the stub file
152 add_custom_command(
153 OUTPUT ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_stubs.obj
154 COMMAND ${CMAKE_ASM_COMPILER} /nologo /Fo${CMAKE_BINARY_DIR}/importlibs/lib${_name}_stubs.obj /c /Ta ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_stubs.asm
155 DEPENDS "${CMAKE_BINARY_DIR}/importlibs/lib${_name}_stubs.asm")
156
157 # Add neccessary importlibs for redirections
158 set(_libraries "")
159 foreach(_lib ${ARGN})
160 list(APPEND _libraries "${CMAKE_BINARY_DIR}/importlibs/${_lib}.lib")
161 list(APPEND _dependencies ${_lib})
162 endforeach()
163
164 # Build the importlib
165 add_custom_command(
166 OUTPUT ${CMAKE_BINARY_DIR}/importlibs/lib${_name}.lib
167 COMMAND LINK /LIB /NOLOGO /MACHINE:X86 /DEF:${CMAKE_BINARY_DIR}/importlibs/lib${_name}_exp.def /OUT:${CMAKE_BINARY_DIR}/importlibs/lib${_name}.lib ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_stubs.obj ${_libraries}
168 DEPENDS ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_stubs.obj ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_exp.def)
169
170 # Add the importlib target
171 add_custom_target(
172 lib${_name}
173 DEPENDS ${CMAKE_BINARY_DIR}/importlibs/lib${_name}.lib)
174
175 add_dependencies(lib${_name} asm ${_dependencies})
176 endmacro()
177
178 macro(add_importlibs MODULE)
179 foreach(LIB ${ARGN})
180 target_link_libraries(${MODULE} ${CMAKE_BINARY_DIR}/importlibs/lib${LIB}.lib)
181 add_dependencies(${MODULE} lib${LIB})
182 endforeach()
183 endmacro()
184
185 macro(spec2def _dllname _spec_file)
186 get_filename_component(_file ${_spec_file} NAME_WE)
187 add_custom_command(
188 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def ${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c
189 COMMAND native-spec2def -@ -n=${_dllname} -d=${CMAKE_CURRENT_BINARY_DIR}/${_file}.def -s=${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file}
190 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file})
191 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${_file}.def ${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c
192 PROPERTIES GENERATED TRUE)
193 list(APPEND SOURCE ${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c)
194 endmacro()
195
196 # Optional 3rd parameter: dllname
197 macro(set_export_spec _module _spec_file)
198 get_filename_component(_file ${_spec_file} NAME_WE)
199 if (${ARGC} GREATER 2)
200 set(_dllname ${ARGV2})
201 else()
202 set(_dllname ${_file}.dll)
203 endif()
204 spec2def(${_dllname} ${_spec_file})
205 endmacro()
206
207 file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/importlibs)
208
209 #pseh workaround
210 set(PSEH_LIB "")
211
212 endif()