af52304d37fcd83b4fbae48cd915388576a5e1ec
[reactos.git] / reactos / 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 function(set_entrypoint _module _entrypoint)
63 if(${_entrypoint} STREQUAL "0")
64 add_linkerflag(${_module} "/NOENTRY")
65 elseif(ARCH MATCHES i386)
66 set(_entrysymbol ${_entrypoint})
67 if (${ARGC} GREATER 2)
68 set(_entrysymbol ${_entrysymbol}@${ARGV2})
69 endif()
70 add_linkerflag(${_module} "/ENTRY:${_entrysymbol}")
71 else()
72 add_linkerflag(${_module} "/ENTRY:${_entrypoint}")
73 endif()
74 endfunction()
75
76 function(set_subsystem MODULE SUBSYSTEM)
77 add_linkerflag(${MODULE} "/subsystem:${SUBSYSTEM}")
78 endfunction()
79
80 function(set_image_base MODULE IMAGE_BASE)
81 add_linkerflag(${MODULE} "/BASE:${IMAGE_BASE}")
82 endfunction()
83
84 function(set_module_type MODULE TYPE)
85 add_dependencies(${MODULE} psdk)
86 if(${TYPE} MATCHES nativecui)
87 set_subsystem(${MODULE} native)
88 set_entrypoint(${MODULE} NtProcessStartup 4)
89 elseif (${TYPE} MATCHES win32gui)
90 set_subsystem(${MODULE} windows)
91 if(IS_UNICODE)
92 set_entrypoint(${MODULE} wWinMainCRTStartup)
93 else()
94 set_entrypoint(${MODULE} WinMainCRTStartup)
95 endif(IS_UNICODE)
96 elseif (${TYPE} MATCHES win32cui)
97 set_subsystem(${MODULE} console)
98 if(IS_UNICODE)
99 set_entrypoint(${MODULE} wmainCRTStartup)
100 else()
101 set_entrypoint(${MODULE} mainCRTStartup)
102 endif(IS_UNICODE)
103 elseif(${TYPE} MATCHES win32dll)
104 # Need this only because mingw library is broken
105 set_entrypoint(${MODULE} DllMainCRTStartup 12)
106 if(DEFINED baseaddress_${MODULE})
107 set_image_base(${MODULE} ${baseaddress_${MODULE}})
108 else()
109 message(STATUS "${MODULE} has no base address")
110 endif()
111 add_linkerflag(${MODULE} "/DLL")
112 elseif(${TYPE} MATCHES win32ocx)
113 set_entrypoint(${MODULE} DllMainCRTStartup 12)
114 set_target_properties(${MODULE} PROPERTIES SUFFIX ".ocx")
115 add_linkerflag(${MODULE} "/DLL")
116 elseif(${TYPE} MATCHES cpl)
117 set_entrypoint(${MODULE} DllMainCRTStartup 12)
118 set_target_properties(${MODULE} PROPERTIES SUFFIX ".cpl")
119 add_linkerflag(${MODULE} "/DLL")
120 elseif(${TYPE} MATCHES kernelmodedriver)
121 set_target_properties(${MODULE} PROPERTIES SUFFIX ".sys")
122 set_entrypoint(${MODULE} DriverEntry 8)
123 set_subsystem(${MODULE} native)
124 set_image_base(${MODULE} 0x00010000)
125 add_linkerflag(${MODULE} "/DRIVER")
126 add_dependencies(${MODULE} bugcodes)
127 elseif(${TYPE} MATCHES nativedll)
128 set_subsystem(${MODULE} native)
129 else()
130 message(FATAL_ERROR "Unknown module type : ${TYPE}")
131 endif()
132 endfunction()
133
134 function(set_rc_compiler)
135 # dummy, this workaround is only needed in mingw due to lack of RC support in cmake
136 endfunction()
137
138 # Thanks MS for creating a stupid linker
139 function(add_importlib_target _exports_file)
140 get_filename_component(_name ${_exports_file} NAME_WE)
141 get_target_property(_suffix ${_name} SUFFIX)
142 if(${_suffix} STREQUAL "_suffix-NOTFOUND")
143 get_target_property(_type ${_name} TYPE)
144 if(${_type} MATCHES EXECUTABLE)
145 set(_suffix ".exe")
146 else()
147 set(_suffix ".dll")
148 endif()
149 endif()
150
151 # Generate the asm stub file and the export def file
152 add_custom_command(
153 OUTPUT ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_stubs.asm ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_exp.def
154 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}
155 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_exports_file} native-spec2def)
156
157 # Assemble the stub file
158 add_custom_command(
159 OUTPUT ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_stubs.obj
160 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
161 DEPENDS "${CMAKE_BINARY_DIR}/importlibs/lib${_name}_stubs.asm")
162
163 # Add neccessary importlibs for redirections
164 set(_libraries "")
165 set(_dependencies "")
166 foreach(_lib ${ARGN})
167 list(APPEND _libraries "${CMAKE_BINARY_DIR}/importlibs/${_lib}.lib")
168 list(APPEND _dependencies ${_lib})
169 endforeach()
170
171 # Build the importlib
172 add_custom_command(
173 OUTPUT ${CMAKE_BINARY_DIR}/importlibs/lib${_name}.lib
174 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}
175 DEPENDS ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_stubs.obj ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_exp.def)
176
177 # Add the importlib target
178 add_custom_target(
179 lib${_name}
180 DEPENDS ${CMAKE_BINARY_DIR}/importlibs/lib${_name}.lib)
181
182 add_dependencies(lib${_name} asm ${_dependencies})
183 endfunction()
184
185 macro(add_delay_importlibs MODULE)
186 foreach(LIB ${ARGN})
187 add_linkerflag(${MODULE} "/DELAYLOAD:${LIB}.dll")
188 target_link_libraries(${MODULE} ${CMAKE_BINARY_DIR}/importlibs/lib${LIB}.LIB)
189 add_dependencies(${MODULE} lib${LIB})
190 endforeach()
191 target_link_libraries(${MODULE} delayimp)
192 endmacro()
193
194 function(spec2def _dllname _spec_file)
195 if(${ARGC} GREATER 2)
196 set(_file ${ARGV2})
197 else()
198 get_filename_component(_file ${_spec_file} NAME_WE)
199 endif()
200 add_custom_command(
201 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def ${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c
202 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}
203 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} native-spec2def)
204 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${_file}.def ${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c
205 PROPERTIES GENERATED TRUE)
206 endfunction()
207
208 macro(macro_mc FILE)
209 set(COMMAND_MC mc -r ${REACTOS_BINARY_DIR}/include/reactos -h ${REACTOS_BINARY_DIR}/include/reactos ${CMAKE_CURRENT_SOURCE_DIR}/${FILE}.mc)
210 endmacro()
211
212 file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/importlibs)
213
214 #pseh workaround
215 set(PSEH_LIB "pseh")
216
217 function(CreateBootSectorTarget2 _target_name _asm_file _binary_file _base_address)
218
219 set(_object_file ${_binary_file}.obj)
220 set(_temp_file ${_binary_file}.tmp)
221
222 add_custom_command(
223 OUTPUT ${_temp_file}
224 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}
225 DEPENDS ${_asm_file})
226
227 add_custom_command(
228 OUTPUT ${_object_file}
229 COMMAND ml /nologo /Cp /Fo${_object_file} /c /Ta ${_temp_file}
230 DEPENDS ${_temp_file})
231
232 add_custom_command(
233 OUTPUT ${_binary_file}
234 COMMAND native-obj2bin ${_object_file} ${_binary_file} ${_base_address}
235 DEPENDS ${_object_file} native-obj2bin)
236
237 set_source_files_properties(${_object_file} ${_temp_file} ${_binary_file} PROPERTIES GENERATED TRUE)
238
239 add_custom_target(${_target_name} ALL DEPENDS ${_binary_file})
240 endfunction()