851518fff3c1c91cbf73d152d664008400ac01e0
[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> /nologo <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 get_directory_property(defines COMPILE_DEFINITIONS)
136 get_directory_property(includes INCLUDE_DIRECTORIES)
137
138 foreach(arg ${defines})
139 set(rc_result_defs "${rc_result_defs} /D${arg}")
140 endforeach()
141
142 foreach(arg ${includes})
143 set(rc_result_incs "/I${arg} ${rc_result_incs}")
144 endforeach()
145
146 set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> /nologo ${rc_result_defs} /I${CMAKE_CURRENT_SOURCE_DIR} ${rc_result_incs} /fo <OBJECT> <SOURCE>" PARENT_SCOPE)
147 endfunction()
148
149 # Thanks MS for creating a stupid linker
150 function(add_importlib_target _exports_file)
151 get_filename_component(_name ${_exports_file} NAME_WE)
152 get_target_property(_suffix ${_name} SUFFIX)
153 if(${_suffix} STREQUAL "_suffix-NOTFOUND")
154 get_target_property(_type ${_name} TYPE)
155 if(${_type} MATCHES EXECUTABLE)
156 set(_suffix ".exe")
157 else()
158 set(_suffix ".dll")
159 endif()
160 endif()
161
162 # Generate the asm stub file and the export def file
163 add_custom_command(
164 OUTPUT ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_stubs.asm ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_exp.def
165 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}
166 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_exports_file} native-spec2def)
167
168 # Assemble the stub file
169 add_custom_command(
170 OUTPUT ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_stubs.obj
171 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
172 DEPENDS "${CMAKE_BINARY_DIR}/importlibs/lib${_name}_stubs.asm")
173
174 # Add neccessary importlibs for redirections
175 set(_libraries "")
176 set(_dependencies "")
177 foreach(_lib ${ARGN})
178 list(APPEND _libraries "${CMAKE_BINARY_DIR}/importlibs/${_lib}.lib")
179 list(APPEND _dependencies ${_lib})
180 endforeach()
181
182 # Build the importlib
183 add_custom_command(
184 OUTPUT ${CMAKE_BINARY_DIR}/importlibs/lib${_name}.lib
185 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}
186 DEPENDS ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_stubs.obj ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_exp.def)
187
188 # Add the importlib target
189 add_custom_target(
190 lib${_name}
191 DEPENDS ${CMAKE_BINARY_DIR}/importlibs/lib${_name}.lib)
192
193 add_dependencies(lib${_name} asm ${_dependencies})
194 endfunction()
195
196 macro(add_delay_importlibs MODULE)
197 foreach(LIB ${ARGN})
198 add_linkerflag(${MODULE} "/DELAYLOAD:${LIB}.dll")
199 target_link_libraries(${MODULE} ${CMAKE_BINARY_DIR}/importlibs/lib${LIB}.LIB)
200 add_dependencies(${MODULE} lib${LIB})
201 endforeach()
202 target_link_libraries(${MODULE} delayimp)
203 endmacro()
204
205 function(spec2def _dllname _spec_file)
206 if(${ARGC} GREATER 2)
207 set(_file ${ARGV2})
208 else()
209 get_filename_component(_file ${_spec_file} NAME_WE)
210 endif()
211 add_custom_command(
212 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def ${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c
213 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}
214 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} native-spec2def)
215 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${_file}.def ${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c
216 PROPERTIES GENERATED TRUE)
217 endfunction()
218
219 macro(macro_mc FILE)
220 set(COMMAND_MC mc -r ${REACTOS_BINARY_DIR}/include/reactos -h ${REACTOS_BINARY_DIR}/include/reactos ${CMAKE_CURRENT_SOURCE_DIR}/${FILE}.mc)
221 endmacro()
222
223 file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/importlibs)
224
225 #pseh workaround
226 set(PSEH_LIB "pseh")
227
228 function(CreateBootSectorTarget2 _target_name _asm_file _binary_file _base_address)
229
230 set(_object_file ${_binary_file}.obj)
231 set(_temp_file ${_binary_file}.tmp)
232
233 add_custom_command(
234 OUTPUT ${_temp_file}
235 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}
236 DEPENDS ${_asm_file})
237
238 add_custom_command(
239 OUTPUT ${_object_file}
240 COMMAND ml /nologo /Cp /Fo${_object_file} /c /Ta ${_temp_file}
241 DEPENDS ${_temp_file})
242
243 add_custom_command(
244 OUTPUT ${_binary_file}
245 COMMAND native-obj2bin ${_object_file} ${_binary_file} ${_base_address}
246 DEPENDS ${_object_file} native-obj2bin)
247
248 set_source_files_properties(${_object_file} ${_temp_file} ${_binary_file} PROPERTIES GENERATED TRUE)
249
250 add_custom_target(${_target_name} ALL DEPENDS ${_binary_file})
251 endfunction()