- Update to r53061
[reactos.git] / cmake / msvc.cmake
1
2 if(${CMAKE_BUILD_TYPE} MATCHES Debug)
3 # no optimitation
4 elseif(OPTIMIZE STREQUAL "1")
5 add_definitions(/O1)
6 elseif(OPTIMIZE STREQUAL "2")
7 add_definitions(/O2)
8 elseif(OPTIMIZE STREQUAL "3")
9 add_definitions(/Ot /Ox /GS-)
10 elseif(OPTIMIZE STREQUAL "4")
11 add_definitions(/Os /Ox /GS-)
12 elseif(OPTIMIZE STREQUAL "5")
13 add_definitions(/GF /Gy /Ob2 /Os /Ox /GS-)
14 endif()
15
16 if(ARCH MATCHES i386)
17 add_definitions(/DWIN32 /D_WINDOWS)
18 endif()
19
20 add_definitions(/Dinline=__inline /D__STDC__=1)
21
22 add_compiler_flags(/X /GR- /GS- /Zl /W3)
23
24 if(${_MACHINE_ARCH_FLAG} MATCHES X86)
25 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SAFESEH:NO /NODEFAULTLIB")
26 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /SAFESEH:NO /NODEFAULTLIB")
27 set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} /SAFESEH:NO /NODEFAULTLIB")
28 endif()
29
30 if(${ARCH} MATCHES amd64)
31 add_definitions(/D__x86_64)
32 set(SPEC2DEF_ARCH x86_64)
33 else()
34 set(SPEC2DEF_ARCH i386)
35 endif()
36
37 link_directories(${REACTOS_SOURCE_DIR}/importlibs ${REACTOS_BINARY_DIR}/importlibs ${REACTOS_BINARY_DIR}/lib/sdk/crt)
38
39 set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> <DEFINES> /I${REACTOS_SOURCE_DIR}/include/psdk /I${REACTOS_BINARY_DIR}/include/psdk /I${REACTOS_SOURCE_DIR}/include /I${REACTOS_SOURCE_DIR}/include/reactos /I${REACTOS_BINARY_DIR}/include/reactos /I${REACTOS_SOURCE_DIR}/include/reactos/wine /I${REACTOS_SOURCE_DIR}/include/crt /I${REACTOS_SOURCE_DIR}/include/crt/mingw32 /fo <OBJECT> <SOURCE>")
40
41 if(MSVC_IDE)
42 # Asm source files are not supported in VS generators yet. As a result, <DEFINES> isn't recognized.
43 # We may temporarily use just the global defines, but this is not a solution as some modules (minihal for example) apply additional definitions to source files, so we get an incorrect build of such targets.
44 get_directory_property(definitions DEFINITIONS)
45 set(CMAKE_ASM_COMPILE_OBJECT
46 "<CMAKE_C_COMPILER> /nologo /X /I${REACTOS_SOURCE_DIR}/include/asm /I${REACTOS_BINARY_DIR}/include/asm <FLAGS> ${definitions} /D__ASM__ /D_USE_ML /EP /c <SOURCE> > <OBJECT>.tmp"
47 "<CMAKE_ASM_COMPILER> /nologo /Cp /Fo<OBJECT> /c /Ta <OBJECT>.tmp")
48 else()
49 # NMake Makefiles
50 set(CMAKE_ASM_COMPILE_OBJECT
51 "<CMAKE_C_COMPILER> /nologo /X /I${REACTOS_SOURCE_DIR}/include/asm /I${REACTOS_BINARY_DIR}/include/asm <FLAGS> <DEFINES> /D__ASM__ /D_USE_ML /EP /c <SOURCE> > <OBJECT>.tmp"
52 "<CMAKE_ASM_COMPILER> /nologo /Cp /Fo<OBJECT> /c /Ta <OBJECT>.tmp")
53 endif()
54
55 set(CMAKE_RC_CREATE_SHARED_LIBRARY ${CMAKE_C_CREATE_SHARED_LIBRARY})
56 set(CMAKE_ASM_CREATE_SHARED_LIBRARY ${CMAKE_C_CREATE_SHARED_LIBRARY})
57 set(CMAKE_ASM_CREATE_STATIC_LIBRARY ${CMAKE_C_CREATE_STATIC_LIBRARY})
58
59 macro(add_pch _target_name _FILE)
60 endmacro()
61
62 macro(set_entrypoint MODULE ENTRYPOINT)
63 if(${ENTRYPOINT} STREQUAL "0")
64 add_linkerflag(${MODULE} "/NOENTRY")
65 else()
66 add_linkerflag(${MODULE} "/ENTRY:${ENTRYPOINT}")
67 endif()
68 endmacro()
69
70 macro(set_subsystem MODULE SUBSYSTEM)
71 add_linkerflag(${MODULE} "/subsystem:${SUBSYSTEM}")
72 endmacro()
73
74 macro(set_image_base MODULE IMAGE_BASE)
75 add_linkerflag(${MODULE} "/BASE:${IMAGE_BASE}")
76 endmacro()
77
78 macro(set_module_type MODULE TYPE)
79 add_dependencies(${MODULE} psdk)
80 if(${TYPE} MATCHES nativecui)
81 set_subsystem(${MODULE} native)
82 set_entrypoint(${MODULE} NtProcessStartup@4)
83 elseif (${TYPE} MATCHES win32gui)
84 set_subsystem(${MODULE} windows)
85 if(IS_UNICODE)
86 set_entrypoint(${MODULE} wWinMainCRTStartup)
87 else()
88 set_entrypoint(${MODULE} WinMainCRTStartup)
89 endif(IS_UNICODE)
90 elseif (${TYPE} MATCHES win32cui)
91 set_subsystem(${MODULE} console)
92 if(IS_UNICODE)
93 set_entrypoint(${MODULE} wmainCRTStartup)
94 else()
95 set_entrypoint(${MODULE} mainCRTStartup)
96 endif(IS_UNICODE)
97 elseif(${TYPE} MATCHES win32dll)
98 # Need this only because mingw library is broken
99 set_entrypoint(${MODULE} DllMainCRTStartup@12)
100 if(DEFINED baseaddress_${MODULE})
101 set_image_base(${MODULE} ${baseaddress_${MODULE}})
102 else()
103 message(STATUS "${MODULE} has no base address")
104 endif()
105 add_linkerflag(${MODULE} "/DLL")
106 elseif(${TYPE} MATCHES win32ocx)
107 set_entrypoint(${MODULE} DllMainCRTStartup@12)
108 set_target_properties(${MODULE} PROPERTIES SUFFIX ".ocx")
109 add_linkerflag(${MODULE} "/DLL")
110 elseif(${TYPE} MATCHES cpl)
111 set_entrypoint(${MODULE} DllMainCRTStartup@12)
112 set_target_properties(${MODULE} PROPERTIES SUFFIX ".cpl")
113 add_linkerflag(${MODULE} "/DLL")
114 elseif(${TYPE} MATCHES kernelmodedriver)
115 set_target_properties(${MODULE} PROPERTIES SUFFIX ".sys")
116 set_entrypoint(${MODULE} DriverEntry@8)
117 set_subsystem(${MODULE} native)
118 set_image_base(${MODULE} 0x00010000)
119 add_linkerflag(${MODULE} "/DRIVER")
120 add_dependencies(${MODULE} bugcodes)
121 elseif(${TYPE} MATCHES nativedll)
122 set_subsystem(${MODULE} native)
123 else()
124 message(FATAL_ERROR "Unknown module type : ${TYPE}")
125 endif()
126 endmacro()
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 # Thanks MS for creating a stupid linker
133 macro(add_importlib_target _exports_file)
134 get_filename_component(_name ${_exports_file} NAME_WE)
135 get_target_property(_suffix ${_name} SUFFIX)
136 if(${_suffix} STREQUAL "_suffix-NOTFOUND")
137 get_target_property(_type ${_name} TYPE)
138 if(${_type} MATCHES EXECUTABLE)
139 set(_suffix ".exe")
140 else()
141 set(_suffix ".dll")
142 endif()
143 endif()
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 --ms --kill-at -a=${SPEC2DEF_ARCH} --implib -n=${_name}${_suffix} -d=${CMAKE_BINARY_DIR}/importlibs/lib${_name}_exp.def -l=${CMAKE_BINARY_DIR}/importlibs/lib${_name}_stubs.asm ${CMAKE_CURRENT_SOURCE_DIR}/${_exports_file}
149 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_exports_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 /Cp /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 /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_delay_importlibs MODULE)
179 # TODO. For now forward to normal import libs
180 add_importlibs(${MODULE} ${ARGN})
181 endmacro()
182
183 macro(spec2def _dllname _spec_file)
184 get_filename_component(_file ${_spec_file} NAME_WE)
185 add_custom_command(
186 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def ${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c
187 COMMAND native-spec2def --ms --kill-at -a=${SPEC2DEF_ARCH} -n=${_dllname} -d=${CMAKE_CURRENT_BINARY_DIR}/${_file}.def -s=${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file}
188 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file})
189 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${_file}.def ${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c
190 PROPERTIES GENERATED TRUE)
191 endmacro()
192
193 macro(macro_mc FILE)
194 set(COMMAND_MC mc -r ${REACTOS_BINARY_DIR}/include/reactos -h ${REACTOS_BINARY_DIR}/include/reactos ${CMAKE_CURRENT_SOURCE_DIR}/${FILE}.mc)
195 endmacro()
196
197 file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/importlibs)
198
199 #pseh workaround
200 set(PSEH_LIB "pseh")
201
202 macro(CreateBootSectorTarget2 _target_name _asm_file _binary_file _base_address)
203
204 set(_object_file ${_binary_file}.obj)
205 set(_temp_file ${_binary_file}.tmp)
206
207 add_custom_command(
208 OUTPUT ${_temp_file}
209 COMMAND ${CMAKE_C_COMPILER} /nologo /X /I${REACTOS_SOURCE_DIR}/include/asm /I${REACTOS_BINARY_DIR}/include/asm /D__ASM__ /D_USE_ML /EP /c ${_asm_file} > ${_temp_file}
210 DEPENDS ${_asm_file})
211
212 add_custom_command(
213 OUTPUT ${_object_file}
214 COMMAND ml /nologo /Cp /Fo${_object_file} /c /Ta ${_temp_file}
215 DEPENDS ${_temp_file})
216
217 add_custom_command(
218 OUTPUT ${_binary_file}
219 COMMAND native-obj2bin ${_object_file} ${_binary_file} ${_base_address}
220 DEPENDS ${_object_file})
221
222 set_source_files_properties(${_object_file} ${_temp_file} ${_binary_file} PROPERTIES GENERATED TRUE)
223
224 add_custom_target(${_target_name} ALL DEPENDS ${_binary_file})
225 endmacro()