* Missed some assignments in debug.c.
[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_compile_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_target_link_flags(${_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_target_link_flags(${_module} "/ENTRY:${_entrysymbol}")
71 else()
72 add_target_link_flags(${_module} "/ENTRY:${_entrypoint}")
73 endif()
74 endfunction()
75
76 function(set_subsystem MODULE SUBSYSTEM)
77 add_target_link_flags(${MODULE} "/subsystem:${SUBSYSTEM}")
78 endfunction()
79
80 function(set_image_base MODULE IMAGE_BASE)
81 add_target_link_flags(${MODULE} "/BASE:${IMAGE_BASE}")
82 endfunction()
83
84 function(set_module_type_toolchain MODULE TYPE)
85 if((${TYPE} STREQUAL win32dll) OR (${TYPE} STREQUAL win32ocx) OR (${TYPE} STREQUAL cpl))
86 add_target_link_flags(${MODULE} "/DLL")
87 elseif(${TYPE} STREQUAL kernelmodedriver)
88 add_target_link_flags(${MODULE} "/DRIVER")
89 endif()
90 endfunction()
91
92 function(set_rc_compiler)
93 get_directory_property(defines COMPILE_DEFINITIONS)
94 get_directory_property(includes INCLUDE_DIRECTORIES)
95
96 foreach(arg ${defines})
97 set(rc_result_defs "${rc_result_defs} /D${arg}")
98 endforeach()
99
100 foreach(arg ${includes})
101 set(rc_result_incs "/I${arg} ${rc_result_incs}")
102 endforeach()
103
104 set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> ${rc_result_defs} /I${CMAKE_CURRENT_SOURCE_DIR} ${rc_result_incs} /fo <OBJECT> <SOURCE>" PARENT_SCOPE)
105 endfunction()
106
107 # Thanks MS for creating a stupid linker
108 function(add_importlib_target _exports_file)
109 get_filename_component(_name ${_exports_file} NAME_WE)
110 get_target_property(_suffix ${_name} SUFFIX)
111 if(${_suffix} STREQUAL "_suffix-NOTFOUND")
112 get_target_property(_type ${_name} TYPE)
113 if(${_type} MATCHES EXECUTABLE)
114 set(_suffix ".exe")
115 else()
116 set(_suffix ".dll")
117 endif()
118 endif()
119
120 # Generate the asm stub file and the export def file
121 add_custom_command(
122 OUTPUT ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_stubs.asm ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_exp.def
123 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}
124 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_exports_file} native-spec2def)
125
126 # Assemble the stub file
127 add_custom_command(
128 OUTPUT ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_stubs.obj
129 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
130 DEPENDS "${CMAKE_BINARY_DIR}/importlibs/lib${_name}_stubs.asm")
131
132 # Add neccessary importlibs for redirections
133 set(_libraries "")
134 set(_dependencies "")
135 foreach(_lib ${ARGN})
136 list(APPEND _libraries "${CMAKE_BINARY_DIR}/importlibs/${_lib}.lib")
137 list(APPEND _dependencies ${_lib})
138 endforeach()
139
140 # Build the importlib
141 add_custom_command(
142 OUTPUT ${CMAKE_BINARY_DIR}/importlibs/lib${_name}.lib
143 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}
144 DEPENDS ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_stubs.obj ${CMAKE_BINARY_DIR}/importlibs/lib${_name}_exp.def)
145
146 # Add the importlib target
147 add_custom_target(
148 lib${_name}
149 DEPENDS ${CMAKE_BINARY_DIR}/importlibs/lib${_name}.lib)
150
151 add_dependencies(lib${_name} asm ${_dependencies})
152 endfunction()
153
154 macro(add_delay_importlibs MODULE)
155 foreach(LIB ${ARGN})
156 add_target_link_flags(${MODULE} "/DELAYLOAD:${LIB}.dll")
157 target_link_libraries(${MODULE} ${CMAKE_BINARY_DIR}/importlibs/lib${LIB}.LIB)
158 add_dependencies(${MODULE} lib${LIB})
159 endforeach()
160 target_link_libraries(${MODULE} delayimp)
161 endmacro()
162
163 function(spec2def _dllname _spec_file)
164 if(${ARGC} GREATER 2)
165 set(_file ${ARGV2})
166 else()
167 get_filename_component(_file ${_spec_file} NAME_WE)
168 endif()
169 add_custom_command(
170 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def ${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c
171 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}
172 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} native-spec2def)
173 set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${_file}.def ${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c
174 PROPERTIES GENERATED TRUE)
175 endfunction()
176
177 macro(macro_mc FILE)
178 set(COMMAND_MC mc -r ${REACTOS_BINARY_DIR}/include/reactos -h ${REACTOS_BINARY_DIR}/include/reactos ${CMAKE_CURRENT_SOURCE_DIR}/${FILE}.mc)
179 endmacro()
180
181 file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/importlibs)
182
183 #pseh workaround
184 set(PSEH_LIB "pseh")
185
186 # Use full path for ml when using x64 VS
187 if((ARCH MATCHES amd64) AND ($ENV{VCINSTALLDIR}))
188 set(CMAKE_ASM16_COMPILER $ENV{VCINSTALLDIR}/bin/ml.exe)
189 else()
190 set(CMAKE_ASM16_COMPILER ml.exe)
191 endif()
192
193 function(CreateBootSectorTarget2 _target_name _asm_file _binary_file _base_address)
194
195 set(_object_file ${_binary_file}.obj)
196 set(_temp_file ${_binary_file}.tmp)
197
198 add_custom_command(
199 OUTPUT ${_temp_file}
200 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}
201 DEPENDS ${_asm_file})
202
203 add_custom_command(
204 OUTPUT ${_object_file}
205 COMMAND ${CMAKE_ASM16_COMPILER} /nologo /Cp /Fo${_object_file} /c /Ta ${_temp_file}
206 DEPENDS ${_temp_file})
207
208 add_custom_command(
209 OUTPUT ${_binary_file}
210 COMMAND native-obj2bin ${_object_file} ${_binary_file} ${_base_address}
211 DEPENDS ${_object_file} native-obj2bin)
212
213 set_source_files_properties(${_object_file} ${_temp_file} ${_binary_file} PROPERTIES GENERATED TRUE)
214
215 add_custom_target(${_target_name} ALL DEPENDS ${_binary_file})
216 endfunction()
217
218 function(allow_warnings __module)
219 endfunction()