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