9317ac621200076804b44b19d883517310421d35
[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)
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)
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)
95 add_linkerflag(${MODULE} "/DLL")
96 endif()
97 if(${TYPE} MATCHES win32ocx)
98 set_entrypoint(${MODULE} DllMain@12)
99 set_target_properties(${MODULE} PROPERTIES SUFFIX ".ocx")
100 endif()
101 if(${TYPE} MATCHES cpl)
102 set_entrypoint(${MODULE} DllMain@12)
103 set_target_properties(${MODULE} PROPERTIES SUFFIX ".cpl")
104 endif()
105 if(${TYPE} MATCHES kernelmodedriver)
106 set_target_properties(${MODULE} PROPERTIES SUFFIX ".sys")
107 set_entrypoint(${MODULE} DriverEntry@8)
108 set_subsystem(${MODULE} native)
109 set_image_base(${MODULE} 0x00010000)
110 add_dependencies(${MODULE} bugcodes)
111 endif()
112
113 endmacro()
114
115 macro(set_unicode)
116 add_definitions(-DUNICODE -D_UNICODE)
117 set(IS_UNICODE 1)
118 endmacro()
119
120 set(CMAKE_C_FLAGS_DEBUG_INIT "/D_DEBUG /MDd /Zi /Ob0 /Od")
121 set(CMAKE_CXX_FLAGS_DEBUG_INIT "/D_DEBUG /MDd /Zi /Ob0 /Od")
122
123 macro(set_rc_compiler)
124 # dummy, this workaround is only needed in mingw due to lack of RC support in cmake
125 endmacro()
126
127 #idl files support
128 set(IDL_COMPILER midl)
129 set(IDL_FLAGS /win32 /Dstrict_context_handle=)
130 set(IDL_HEADER_ARG /h) #.h
131 set(IDL_TYPELIB_ARG /tlb) #.tlb
132 set(IDL_SERVER_ARG /sstub) #.c for stub server library
133 set(IDL_CLIENT_ARG /cstub) #.c for stub client library
134
135 # Thanks MS for creating a stupid linker
136 macro(add_importlib_target _spec_file)
137 get_filename_component(_name ${_spec_file} NAME_WE)
138
139 # Generate the asm stub file
140 add_custom_command(
141 OUTPUT ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_stubs.asm
142 COMMAND native-spec2def -s ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_stubs.asm
143 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file})
144
145 # Generate a the export def file
146 add_custom_command(
147 OUTPUT ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_exp.def
148 COMMAND native-spec2def -n -r ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_exp.def
149 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file})
150
151 # Assemble the 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
158 # Add neccessary importlibs for redirections
159 set(_libraries "")
160 foreach(_lib ${ARGN})
161 list(APPEND _libraries "${CMAKE_BINARY_DIR}/importlibs/${_lib}.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 ${_libraries}
169 )
170
171 # Add the importlib target
172 add_custom_target(
173 lib${_name}
174 DEPENDS ${CMAKE_BINARY_DIR}/importlibs/lib${_name}.lib
175 )
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
189 COMMAND native-spec2def -n --dll ${_dllname} ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def
190 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file})
191 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${_file}.def
192 PROPERTIES GENERATED TRUE)
193 endmacro()
194
195 # Optional 3rd parameter: dllname
196 macro(set_export_spec _module _spec_file)
197 get_filename_component(_file ${_spec_file} NAME_WE)
198 if (${ARGC} GREATER 2)
199 set(_dllname ${ARGV2})
200 else()
201 set(_dllname ${_file}.dll)
202 endif()
203 spec2def(${_dllname} ${_spec_file})
204 endmacro()
205
206 file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/importlibs)
207
208 #pseh workaround
209 set(PSEH_LIB "")
210
211 endif()