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