[CMAKE] Allow overriding the .dll extension for delay imports in MSVC builds.
[reactos.git] / sdk / cmake / msvc.cmake
1
2 #if(${CMAKE_BUILD_TYPE} STREQUAL "Debug")
3 if(CMAKE_BUILD_TYPE STREQUAL "Debug")
4 # no optimization
5 add_compile_flags("/Ob0 /Od")
6 elseif(CMAKE_BUILD_TYPE STREQUAL "Release")
7 add_compile_flags("/Ox /Ob2 /Ot /Oy /GT /GF")
8 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /OPT:REF /OPT:ICF")
9 elseif(OPTIMIZE STREQUAL "1")
10 add_definitions(/O1)
11 elseif(OPTIMIZE STREQUAL "2")
12 add_definitions(/O2)
13 elseif(OPTIMIZE STREQUAL "3")
14 add_definitions(/Ot /Ox /GS-)
15 elseif(OPTIMIZE STREQUAL "4")
16 add_definitions(/Os /Ox /GS-)
17 elseif(OPTIMIZE STREQUAL "5")
18 add_definitions(/GF /Gy /Ob2 /Os /Ox /GS-)
19 endif()
20
21 # Enable function level linking and comdat folding
22 add_compile_flags("/Gy")
23 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /OPT:REF /OPT:ICF")
24 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /OPT:REF /OPT:ICF")
25
26 if(ARCH STREQUAL "i386")
27 add_definitions(/DWIN32 /D_WINDOWS)
28 endif()
29
30 add_definitions(/Dinline=__inline /D__STDC__=1)
31
32 if(NOT USE_CLANG_CL)
33 add_compile_flags("/X /Zl")
34 endif()
35
36 add_compile_flags("/GR- /EHs-c- /GS- /W3")
37
38 if(USE_CLANG_CL)
39 set(CMAKE_CL_SHOWINCLUDES_PREFIX "Note: including file: ")
40 endif()
41
42 # HACK: for VS 11+ we need to explicitly disable SSE, which is off by
43 # default for older compilers. See CORE-6507
44 if(MSVC_VERSION GREATER 1699 AND ARCH STREQUAL "i386")
45 add_compile_flags("/arch:IA32")
46 endif ()
47
48 # VS 12+ requires /FS when used in parallel compilations
49 if(MSVC_VERSION GREATER 1799 AND NOT MSVC_IDE)
50 add_compile_flags("/FS")
51 endif ()
52
53 # VS14+ tries to use thread-safe initialization
54 if(MSVC_VERSION GREATER 1899)
55 add_compile_flags("/Zc:threadSafeInit-")
56 endif ()
57
58 # Disable overly sensitive warnings as well as those that generally aren't
59 # useful to us.
60 # - C4244: implicit integer truncation
61 # - C4290: C++ exception specification ignored
62 # - C4800: forcing value to bool 'true' or 'false' (performance warning)
63 # - C4200: nonstandard extension used : zero-sized array in struct/union
64 # - C4214: nonstandard extension used : bit field types other than int
65 add_compile_flags("/wd4244 /wd4290 /wd4800 /wd4200 /wd4214")
66
67 # FIXME: Temporarily disable C4018 until we fix more of the others. CORE-10113
68 add_compile_flags("/wd4018")
69
70 # The following warnings are treated as errors:
71 # - C4013: implicit function declaration
72 # - C4020: too many actual parameters
73 # - C4022: pointer type mismatch for parameter
74 # - TODO: C4028: formal parameter different from declaration
75 # - C4047: different level of indirection
76 # - TODO: C4090: different 'modifier' qualifiers (for C programs only;
77 # for C++ programs, the compiler error C2440 is issued)
78 # - C4098: void function returning a value
79 # - C4113: parameter lists differ
80 # - C4129: unrecognized escape sequence
81 # - TODO: C4133: incompatible types
82 # - C4163: 'identifier': not available as an intrinsic function
83 # - C4229: modifiers on data are ignored
84 # - C4700: uninitialized variable usage
85 # - C4603: macro is not defined or definition is different after precompiled header use
86 # - C4716: function must return a value
87 add_compile_flags("/we4013 /we4020 /we4022 /we4047 /we4098 /we4113 /we4129 /we4163 /we4229 /we4700 /we4603 /we4716")
88
89 # - C4189: local variable initialized but not referenced
90 # Not in Release mode and not with MSVC 2010
91 if((NOT CMAKE_BUILD_TYPE STREQUAL "Release") AND (NOT MSVC_VERSION LESS 1700))
92 add_compile_flags("/we4189")
93 endif()
94
95 # Enable warnings above the default level, but don't treat them as errors:
96 # - C4115: named type definition in parentheses
97 add_compile_flags("/w14115")
98
99 if(USE_CLANG_CL)
100 add_compile_flags_language("-nostdinc -Wno-multichar -Wno-char-subscripts -Wno-microsoft-enum-forward-reference -Wno-pragma-pack -Wno-microsoft-anon-tag -Wno-unknown-pragmas" "C")
101 add_compile_flags_language("-nostdinc -Wno-multichar -Wno-char-subscripts -Wno-microsoft-enum-forward-reference -Wno-pragma-pack -Wno-microsoft-anon-tag -Wno-unknown-pragmas" "CXX")
102 endif()
103
104 # Debugging
105 #if(${CMAKE_BUILD_TYPE} STREQUAL "Debug")
106 if(CMAKE_BUILD_TYPE STREQUAL "Debug")
107 if(NOT (_PREFAST_ OR _VS_ANALYZE_))
108 add_compile_flags("/Zi")
109 endif()
110 #elseif(${CMAKE_BUILD_TYPE} STREQUAL "Release")
111 elseif(CMAKE_BUILD_TYPE STREQUAL "Release")
112 add_definitions("/D NDEBUG")
113 endif()
114
115 # Hotpatchable images
116 if(ARCH STREQUAL "i386")
117 if(NOT USE_CLANG_CL)
118 add_compile_flags("/hotpatch")
119 endif()
120 set(_hotpatch_link_flag "/FUNCTIONPADMIN:5")
121 elseif(ARCH STREQUAL "amd64")
122 set(_hotpatch_link_flag "/FUNCTIONPADMIN:6")
123 endif()
124
125 if(MSVC_IDE AND (NOT DEFINED USE_FOLDER_STRUCTURE))
126 set(USE_FOLDER_STRUCTURE TRUE)
127 endif()
128
129 if(NOT DEFINED RUNTIME_CHECKS)
130 set(RUNTIME_CHECKS FALSE)
131 endif()
132
133 if(RUNTIME_CHECKS)
134 add_definitions(-D__RUNTIME_CHECKS__)
135 add_compile_flags("/RTC1")
136 endif()
137
138 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /MANIFEST:NO /INCREMENTAL:NO /SAFESEH:NO /NODEFAULTLIB /RELEASE ${_hotpatch_link_flag}")
139 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /MANIFEST:NO /INCREMENTAL:NO /SAFESEH:NO /NODEFAULTLIB /RELEASE /IGNORE:4104 ${_hotpatch_link_flag}")
140 set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} /MANIFEST:NO /INCREMENTAL:NO /SAFESEH:NO /NODEFAULTLIB /RELEASE ${_hotpatch_link_flag}")
141
142 if(CMAKE_DISABLE_NINJA_DEPSLOG)
143 set(cl_includes_flag "")
144 else()
145 set(cl_includes_flag "/showIncludes")
146 endif()
147
148 if(MSVC_IDE AND (CMAKE_VERSION MATCHES "ReactOS"))
149 # For VS builds we'll only have en-US in resource files
150 add_definitions(/DLANGUAGE_EN_US)
151 else()
152 # Only VS 10+ resource compiler supports /nologo
153 # CMAKE_CXX_SIMULATE_VERSION is a similar check for our clang-cl builds
154 if((MSVC_VERSION GREATER 1599) OR (CMAKE_CXX_SIMULATE_VERSION VERSION_GREATER 15.99))
155 set(rc_nologo_flag "/nologo")
156 else()
157 set(rc_nologo_flag)
158 endif()
159 if(CMAKE_VERSION VERSION_LESS 3.4.0)
160 set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> ${rc_nologo_flag} <FLAGS> <DEFINES> ${I18N_DEFS} /fo<OBJECT> <SOURCE>")
161 if(ARCH STREQUAL "arm")
162 set(CMAKE_ASM_COMPILE_OBJECT
163 "cl ${cl_includes_flag} /nologo /X /I${REACTOS_SOURCE_DIR}/sdk/include/asm /I${REACTOS_BINARY_DIR}/sdk/include/asm <FLAGS> <DEFINES> /D__ASM__ /D_USE_ML /EP /c <SOURCE> > <OBJECT>.tmp"
164 "<CMAKE_ASM_COMPILER> -nologo -o <OBJECT> <OBJECT>.tmp")
165 else()
166 set(CMAKE_ASM_COMPILE_OBJECT
167 "cl ${cl_includes_flag} /nologo /X /I${REACTOS_SOURCE_DIR}/sdk/include/asm /I${REACTOS_BINARY_DIR}/sdk/include/asm <FLAGS> <DEFINES> /D__ASM__ /D_USE_ML /EP /c <SOURCE> > <OBJECT>.tmp"
168 "<CMAKE_ASM_COMPILER> /nologo /Cp /Fo<OBJECT> /c /Ta <OBJECT>.tmp")
169 endif()
170 else()
171 set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> ${rc_nologo_flag} <INCLUDES> <FLAGS> <DEFINES> ${I18N_DEFS} /fo<OBJECT> <SOURCE>")
172 if(ARCH STREQUAL "arm")
173 set(CMAKE_ASM_COMPILE_OBJECT
174 "cl ${cl_includes_flag} /nologo /X /I${REACTOS_SOURCE_DIR}/sdk/include/asm /I${REACTOS_BINARY_DIR}/sdk/include/asm <INCLUDES> <FLAGS> <DEFINES> /D__ASM__ /D_USE_ML /EP /c <SOURCE> > <OBJECT>.tmp"
175 "<CMAKE_ASM_COMPILER> -nologo -o <OBJECT> <OBJECT>.tmp")
176 else()
177 set(CMAKE_ASM_COMPILE_OBJECT
178 "cl ${cl_includes_flag} /nologo /X /I${REACTOS_SOURCE_DIR}/sdk/include/asm /I${REACTOS_BINARY_DIR}/sdk/include/asm <INCLUDES> <FLAGS> <DEFINES> /D__ASM__ /D_USE_ML /EP /c <SOURCE> > <OBJECT>.tmp"
179 "<CMAKE_ASM_COMPILER> /nologo /Cp /Fo<OBJECT> /c /Ta <OBJECT>.tmp")
180 endif()
181 endif()
182 endif()
183
184 if(_VS_ANALYZE_)
185 message("VS static analysis enabled!")
186 add_compile_flags("/analyze")
187 elseif(_PREFAST_)
188 message("PREFAST enabled!")
189 if(CMAKE_VERSION VERSION_LESS 3.4.0)
190 set(CMAKE_C_COMPILE_OBJECT "prefast <CMAKE_C_COMPILER> ${CMAKE_START_TEMP_FILE} ${CMAKE_CL_NOLOGO} <FLAGS> <DEFINES> /Fo<OBJECT> -c <SOURCE>${CMAKE_END_TEMP_FILE}"
191 "prefast LIST")
192 set(CMAKE_CXX_COMPILE_OBJECT "prefast <CMAKE_CXX_COMPILER> ${CMAKE_START_TEMP_FILE} ${CMAKE_CL_NOLOGO} <FLAGS> <DEFINES> /TP /Fo<OBJECT> -c <SOURCE>${CMAKE_END_TEMP_FILE}"
193 "prefast LIST")
194 set(CMAKE_C_LINK_EXECUTABLE
195 "<CMAKE_C_COMPILER> ${CMAKE_CL_NOLOGO} <OBJECTS> ${CMAKE_START_TEMP_FILE} <FLAGS> /Fe<TARGET> -link /implib:<TARGET_IMPLIB> /version:<TARGET_VERSION_MAJOR>.<TARGET_VERSION_MINOR> <CMAKE_C_LINK_FLAGS> <LINK_FLAGS> <LINK_LIBRARIES>${CMAKE_END_TEMP_FILE}")
196 set(CMAKE_CXX_LINK_EXECUTABLE
197 "<CMAKE_CXX_COMPILER> ${CMAKE_CL_NOLOGO} <OBJECTS> ${CMAKE_START_TEMP_FILE} <FLAGS> /Fe<TARGET> -link /implib:<TARGET_IMPLIB> /version:<TARGET_VERSION_MAJOR>.<TARGET_VERSION_MINOR> <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> <LINK_LIBRARIES>${CMAKE_END_TEMP_FILE}")
198 else()
199 set(CMAKE_C_COMPILE_OBJECT "prefast <CMAKE_C_COMPILER> ${CMAKE_START_TEMP_FILE} ${CMAKE_CL_NOLOGO} <INCLUDES> <FLAGS> <DEFINES> /Fo<OBJECT> -c <SOURCE>${CMAKE_END_TEMP_FILE}"
200 "prefast LIST")
201 set(CMAKE_CXX_COMPILE_OBJECT "prefast <CMAKE_CXX_COMPILER> ${CMAKE_START_TEMP_FILE} ${CMAKE_CL_NOLOGO} <INCLUDES> <FLAGS> <DEFINES> /TP /Fo<OBJECT> -c <SOURCE>${CMAKE_END_TEMP_FILE}"
202 "prefast LIST")
203 set(CMAKE_C_LINK_EXECUTABLE
204 "<CMAKE_C_COMPILER> ${CMAKE_CL_NOLOGO} <OBJECTS> ${CMAKE_START_TEMP_FILE} <INCLUDES> <FLAGS> /Fe<TARGET> -link /implib:<TARGET_IMPLIB> /version:<TARGET_VERSION_MAJOR>.<TARGET_VERSION_MINOR> <CMAKE_C_LINK_FLAGS> <LINK_FLAGS> <LINK_LIBRARIES>${CMAKE_END_TEMP_FILE}")
205 set(CMAKE_CXX_LINK_EXECUTABLE
206 "<CMAKE_CXX_COMPILER> ${CMAKE_CL_NOLOGO} <OBJECTS> ${CMAKE_START_TEMP_FILE} <INCLUDES> <FLAGS> /Fe<TARGET> -link /implib:<TARGET_IMPLIB> /version:<TARGET_VERSION_MAJOR>.<TARGET_VERSION_MINOR> <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> <LINK_LIBRARIES>${CMAKE_END_TEMP_FILE}")
207 endif()
208 endif()
209
210 set(CMAKE_RC_CREATE_SHARED_LIBRARY ${CMAKE_C_CREATE_SHARED_LIBRARY})
211 set(CMAKE_ASM_CREATE_SHARED_LIBRARY ${CMAKE_C_CREATE_SHARED_LIBRARY})
212 set(CMAKE_ASM_CREATE_STATIC_LIBRARY ${CMAKE_C_CREATE_STATIC_LIBRARY})
213
214 if(PCH)
215 macro(add_pch _target _pch _sources)
216
217 # Workaround for the MSVC toolchain (MSBUILD) /MP bug
218 set(_temp_gch ${CMAKE_CURRENT_BINARY_DIR}/${_target}.pch)
219 if(MSVC_IDE)
220 file(TO_NATIVE_PATH ${_temp_gch} _gch)
221 else()
222 set(_gch ${_temp_gch})
223 endif()
224
225 if(IS_CPP)
226 set(_pch_language CXX)
227 if(NOT USE_CLANG_CL)
228 set(_cl_lang_flag "/TP")
229 endif()
230 else()
231 set(_pch_language C)
232 set(_cl_lang_flag "/TC")
233 endif()
234
235 if(MSVC_IDE)
236 set(_pch_path_name_flag "/Fp${_gch}")
237 endif()
238
239 if(USE_CLANG_CL)
240 set(_pch_compile_flags "${_cl_lang_flag} /Yc${_pch} /FI${_pch} /Fp${_gch}")
241 else()
242 set(_pch_compile_flags "${_cl_lang_flag} /Yc /Fp${_gch}")
243 endif()
244
245 # Build the precompiled header
246 # HEADER_FILE_ONLY FALSE: force compiling the header
247 set_source_files_properties(${_pch} PROPERTIES
248 HEADER_FILE_ONLY FALSE
249 LANGUAGE ${_pch_language}
250 COMPILE_FLAGS ${_pch_compile_flags}
251 OBJECT_OUTPUTS ${_gch})
252
253 # Prevent a race condition related to writing to the PDB files between the PCH and the excluded list of source files
254 get_target_property(_target_sources ${_target} SOURCES)
255 list(REMOVE_ITEM _target_sources ${_pch})
256 foreach(_target_src ${_target_sources})
257 set_property(SOURCE ${_target_src} APPEND PROPERTY OBJECT_DEPENDS ${_gch})
258 endforeach()
259
260 # Use the precompiled header with the specified source files, skipping the pch itself
261 list(REMOVE_ITEM ${_sources} ${_pch})
262 foreach(_src ${${_sources}})
263 set_property(SOURCE ${_src} APPEND_STRING PROPERTY COMPILE_FLAGS " /FI${_gch} /Yu${_gch} ${_pch_path_name_flag}")
264 endforeach()
265 endmacro()
266 else()
267 macro(add_pch _target _pch _sources)
268 endmacro()
269 endif()
270
271 function(set_entrypoint _module _entrypoint)
272 if(${_entrypoint} STREQUAL "0")
273 add_target_link_flags(${_module} "/NOENTRY")
274 elseif(ARCH STREQUAL "i386")
275 set(_entrysymbol ${_entrypoint})
276 if(${ARGC} GREATER 2)
277 set(_entrysymbol ${_entrysymbol}@${ARGV2})
278 endif()
279 add_target_link_flags(${_module} "/ENTRY:${_entrysymbol}")
280 else()
281 add_target_link_flags(${_module} "/ENTRY:${_entrypoint}")
282 endif()
283 endfunction()
284
285 function(set_subsystem MODULE SUBSYSTEM)
286 string(TOUPPER ${SUBSYSTEM} _subsystem)
287 if(ARCH STREQUAL "amd64")
288 add_target_link_flags(${MODULE} "/SUBSYSTEM:${_subsystem},5.02")
289 elseif(ARCH STREQUAL "arm")
290 add_target_link_flags(${MODULE} "/SUBSYSTEM:${_subsystem},6.02")
291 else()
292 add_target_link_flags(${MODULE} "/SUBSYSTEM:${_subsystem},5.01")
293 endif()
294 endfunction()
295
296 function(set_image_base MODULE IMAGE_BASE)
297 add_target_link_flags(${MODULE} "/BASE:${IMAGE_BASE}")
298 endfunction()
299
300 function(set_module_type_toolchain MODULE TYPE)
301 if(CPP_USE_STL)
302 if((${TYPE} STREQUAL "kernelmodedriver") OR (${TYPE} STREQUAL "wdmdriver"))
303 message(FATAL_ERROR "Use of STL in kernelmodedriver or wdmdriver type module prohibited")
304 endif()
305 target_link_libraries(${MODULE} cpprt stlport oldnames)
306 elseif(CPP_USE_RT)
307 target_link_libraries(${MODULE} cpprt)
308 endif()
309 if((${TYPE} STREQUAL "win32dll") OR (${TYPE} STREQUAL "win32ocx") OR (${TYPE} STREQUAL "cpl"))
310 add_target_link_flags(${MODULE} "/DLL")
311 elseif(${TYPE} STREQUAL "kernelmodedriver")
312 # Disable linker warning 4078 (multiple sections found with different attributes) for INIT section use
313 add_target_link_flags(${MODULE} "/DRIVER /IGNORE:4078")
314 elseif(${TYPE} STREQUAL "wdmdriver")
315 add_target_link_flags(${MODULE} "/DRIVER:WDM /IGNORE:4078")
316 endif()
317
318 if(RUNTIME_CHECKS)
319 target_link_libraries(${MODULE} runtmchk)
320 endif()
321
322 endfunction()
323
324 # Define those for having real libraries
325 set(CMAKE_IMPLIB_CREATE_STATIC_LIBRARY "LINK /LIB /NOLOGO <LINK_FLAGS> /OUT:<TARGET> <OBJECTS>")
326
327 if(ARCH STREQUAL "arm")
328 set(CMAKE_STUB_ASM_COMPILE_OBJECT "<CMAKE_ASM_COMPILER> -nologo -o <OBJECT> <SOURCE>")
329 else()
330 set(CMAKE_STUB_ASM_COMPILE_OBJECT "<CMAKE_ASM_COMPILER> /nologo /Cp /Fo<OBJECT> /c /Ta <SOURCE>")
331 endif()
332
333 function(add_delay_importlibs _module)
334 get_target_property(_module_type ${_module} TYPE)
335 if(_module_type STREQUAL "STATIC_LIBRARY")
336 message(FATAL_ERROR "Cannot add delay imports to a static library")
337 endif()
338 foreach(_lib ${ARGN})
339 get_filename_component(_basename "${_lib}" NAME_WE)
340 get_filename_component(_ext "${_lib}" EXT)
341 if(NOT _ext)
342 set(_ext ".dll")
343 endif()
344 add_target_link_flags(${_module} "/DELAYLOAD:${_basename}${_ext}")
345 target_link_libraries(${_module} "lib${_basename}")
346 endforeach()
347 target_link_libraries(${_module} delayimp)
348 endfunction()
349
350 function(generate_import_lib _libname _dllname _spec_file)
351
352 set(_def_file ${CMAKE_CURRENT_BINARY_DIR}/${_libname}_exp.def)
353 set(_asm_stubs_file ${CMAKE_CURRENT_BINARY_DIR}/${_libname}_stubs.asm)
354
355 # Generate the asm stub file and the def file for import library
356 add_custom_command(
357 OUTPUT ${_asm_stubs_file} ${_def_file}
358 COMMAND native-spec2def --ms -a=${SPEC2DEF_ARCH} --implib -n=${_dllname} -d=${_def_file} -l=${_asm_stubs_file} ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file}
359 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} native-spec2def)
360
361 if(MSVC_IDE)
362 # Compile the generated asm stub file
363 if(ARCH STREQUAL "arm")
364 set(_asm_stub_command ${CMAKE_ASM_COMPILER} -nologo -o ${_asm_stubs_file}.obj ${_asm_stubs_file})
365 else()
366 set(_asm_stub_command ${CMAKE_ASM_COMPILER} /Cp /Fo${_asm_stubs_file}.obj /c /Ta ${_asm_stubs_file})
367 endif()
368 add_custom_command(
369 OUTPUT ${_asm_stubs_file}.obj
370 COMMAND ${_asm_stub_command}
371 DEPENDS ${_asm_stubs_file})
372 else()
373 # Be clear about the "language"
374 # Thanks MS for creating a stupid linker
375 set_source_files_properties(${_asm_stubs_file} PROPERTIES LANGUAGE "STUB_ASM")
376 endif()
377
378 # Add our library
379 if(MSVC_IDE)
380 add_library(${_libname} STATIC EXCLUDE_FROM_ALL ${_asm_stubs_file}.obj)
381 set_source_files_properties(${_asm_stubs_file}.obj PROPERTIES EXTERNAL_OBJECT 1)
382 set_target_properties(${_libname} PROPERTIES LINKER_LANGUAGE "C")
383 else()
384 # NOTE: as stub file and def file are generated in one pass, depending on one is like depending on the other
385 add_library(${_libname} STATIC EXCLUDE_FROM_ALL ${_asm_stubs_file})
386 # set correct "link rule"
387 set_target_properties(${_libname} PROPERTIES LINKER_LANGUAGE "IMPLIB")
388 endif()
389 set_target_properties(${_libname} PROPERTIES STATIC_LIBRARY_FLAGS "/DEF:${_def_file}")
390 endfunction()
391
392 if(ARCH STREQUAL "amd64")
393 # This is NOT a typo.
394 # See https://software.intel.com/en-us/forums/topic/404643
395 add_definitions(/D__x86_64)
396 set(SPEC2DEF_ARCH x86_64)
397 elseif(ARCH STREQUAL "arm")
398 add_definitions(/D__arm__)
399 set(SPEC2DEF_ARCH arm)
400 else()
401 set(SPEC2DEF_ARCH i386)
402 endif()
403 function(spec2def _dllname _spec_file)
404
405 cmake_parse_arguments(__spec2def "ADD_IMPORTLIB;NO_PRIVATE_WARNINGS" "" "" ${ARGN})
406
407 # Get library basename
408 get_filename_component(_file ${_dllname} NAME_WE)
409
410 # Error out on anything else than spec
411 if(NOT ${_spec_file} MATCHES ".*\\.spec")
412 message(FATAL_ERROR "spec2def only takes spec files as input.")
413 endif()
414
415 # Generate exports def and C stubs file for the DLL
416 add_custom_command(
417 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def ${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c
418 COMMAND native-spec2def --ms -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}
419 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} native-spec2def)
420
421 if(__spec2def_ADD_IMPORTLIB)
422 generate_import_lib(lib${_file} ${_dllname} ${_spec_file})
423 if(__spec2def_NO_PRIVATE_WARNINGS)
424 add_target_property(lib${_file} STATIC_LIBRARY_FLAGS "/ignore:4104")
425 endif()
426 endif()
427 endfunction()
428
429 macro(macro_mc FLAG FILE)
430 set(COMMAND_MC ${CMAKE_MC_COMPILER} -u ${FLAG} -b -h ${CMAKE_CURRENT_BINARY_DIR}/ -r ${CMAKE_CURRENT_BINARY_DIR}/ ${FILE})
431 endmacro()
432
433 # PSEH workaround
434 set(PSEH_LIB "pseh")
435
436 # Use a full path for the x86 version of ml when using x64 VS.
437 # It's not a problem when using the DDK/WDK because, in x64 mode,
438 # both the x86 and x64 versions of ml are available.
439 if((ARCH STREQUAL "amd64") AND (DEFINED ENV{VCToolsInstallDir}))
440 set(CMAKE_ASM16_COMPILER $ENV{VCToolsInstallDir}/bin/HostX86/x86/ml.exe)
441 elseif((ARCH STREQUAL "amd64") AND (DEFINED ENV{VCINSTALLDIR}))
442 set(CMAKE_ASM16_COMPILER $ENV{VCINSTALLDIR}/bin/ml.exe)
443 elseif(ARCH STREQUAL "arm")
444 set(CMAKE_ASM16_COMPILER armasm.exe)
445 else()
446 set(CMAKE_ASM16_COMPILER ml.exe)
447 endif()
448
449 function(CreateBootSectorTarget _target_name _asm_file _binary_file _base_address)
450 set(_object_file ${_binary_file}.obj)
451 set(_temp_file ${_binary_file}.tmp)
452
453 if(USE_CLANG_CL)
454 set(_no_std_includes_flag "-nostdinc")
455 else()
456 set(_no_std_includes_flag "/X")
457 endif()
458
459 add_custom_command(
460 OUTPUT ${_temp_file}
461 COMMAND ${CMAKE_C_COMPILER} /nologo ${_no_std_includes_flag} /I${REACTOS_SOURCE_DIR}/sdk/include/asm /I${REACTOS_BINARY_DIR}/sdk/include/asm /I${REACTOS_SOURCE_DIR}/boot/freeldr /D__ASM__ /D_USE_ML /EP /c ${_asm_file} > ${_temp_file}
462 DEPENDS ${_asm_file})
463
464 if(ARCH STREQUAL "arm")
465 set(_asm16_command ${CMAKE_ASM16_COMPILER} -nologo -o ${_object_file} ${_temp_file})
466 else()
467 set(_asm16_command ${CMAKE_ASM16_COMPILER} /nologo /Cp /Fo${_object_file} /c /Ta ${_temp_file})
468 endif()
469
470 add_custom_command(
471 OUTPUT ${_object_file}
472 COMMAND ${_asm16_command}
473 DEPENDS ${_temp_file})
474
475 add_custom_command(
476 OUTPUT ${_binary_file}
477 COMMAND native-obj2bin ${_object_file} ${_binary_file} ${_base_address}
478 DEPENDS ${_object_file} native-obj2bin)
479
480 set_source_files_properties(${_object_file} ${_temp_file} ${_binary_file} PROPERTIES GENERATED TRUE)
481
482 add_custom_target(${_target_name} ALL DEPENDS ${_binary_file})
483 endfunction()
484
485 function(allow_warnings __module)
486 endfunction()
487
488 macro(add_asm_files _target)
489 if(MSVC_IDE AND (CMAKE_VERSION MATCHES "ReactOS"))
490 get_defines(_directory_defines)
491 get_includes(_directory_includes)
492 get_directory_property(_defines COMPILE_DEFINITIONS)
493 foreach(_source_file ${ARGN})
494 get_filename_component(_source_file_base_name ${_source_file} NAME_WE)
495 get_filename_component(_source_file_full_path ${_source_file} ABSOLUTE)
496 set(_preprocessed_asm_file ${CMAKE_CURRENT_BINARY_DIR}/asm/${_source_file_base_name}_${_target}.tmp)
497 set(_object_file ${CMAKE_CURRENT_BINARY_DIR}/asm/${_source_file_base_name}_${_target}.obj)
498 get_source_file_property(_defines_semicolon_list ${_source_file_full_path} COMPILE_DEFINITIONS)
499 unset(_source_file_defines)
500 foreach(_define ${_defines_semicolon_list})
501 if(NOT ${_define} STREQUAL "NOTFOUND")
502 list(APPEND _source_file_defines -D${_define})
503 endif()
504 endforeach()
505 if(ARCH STREQUAL "arm")
506 set(_pp_asm_compile_command ${CMAKE_ASM_COMPILER} -nologo -o ${_object_file} ${_preprocessed_asm_file})
507 else()
508 set(_pp_asm_compile_command ${CMAKE_ASM_COMPILER} /nologo /Cp /Fo${_object_file} /c /Ta ${_preprocessed_asm_file})
509 endif()
510 add_custom_command(
511 OUTPUT ${_preprocessed_asm_file} ${_object_file}
512 COMMAND cl /nologo /X /I${REACTOS_SOURCE_DIR}/sdk/include/asm /I${REACTOS_BINARY_DIR}/sdk/include/asm ${_directory_includes} ${_source_file_defines} ${_directory_defines} /D__ASM__ /D_USE_ML /EP /c ${_source_file_full_path} > ${_preprocessed_asm_file} && ${_pp_asm_compile_command}
513 DEPENDS ${_source_file_full_path})
514 set_source_files_properties(${_object_file} PROPERTIES EXTERNAL_OBJECT 1)
515 list(APPEND ${_target} ${_object_file})
516 endforeach()
517 else()
518 list(APPEND ${_target} ${ARGN})
519 endif()
520 endmacro()