2d4822277300e365cc5f0a9e0773c2a30385d750
[reactos.git] / sdk / cmake / msvc.cmake
1
2 if(CMAKE_BUILD_TYPE STREQUAL "Release")
3 add_compile_options(/Ox /Ob2 /Ot /Oy)
4 # Avoid spam in clang-cl as it doesn't support /GT
5 if(CMAKE_C_COMPILER_ID STREQUAL "MSVC")
6 add_compile_options(/GT)
7 endif()
8 elseif(OPTIMIZE STREQUAL "1")
9 add_compile_options(/O1)
10 elseif(OPTIMIZE STREQUAL "2")
11 add_compile_options(/O2)
12 elseif(OPTIMIZE STREQUAL "3")
13 add_compile_options(/Ot /Ox /GS-)
14 elseif(OPTIMIZE STREQUAL "4")
15 add_compile_options(/Ob0 /Od)
16 elseif(OPTIMIZE STREQUAL "5")
17 add_compile_options(/Ob2 /Os /Ox /GS-)
18 endif()
19
20 # Always use string pooling: this helps reducing the binaries size since a lot
21 # of redundancy come from the usage of __FILE__ / __RELFILE__ in the debugging
22 # helper macros. Note also that GCC builds use string pooling by default.
23 add_compile_options(/GF)
24
25 # Enable function level linking and comdat folding
26 add_compile_options(/Gy)
27 add_link_options(/OPT:REF /OPT:ICF)
28
29 if(ARCH STREQUAL "i386")
30 add_definitions(/DWIN32 /D_WINDOWS)
31 endif()
32
33 add_definitions(/D__STDC__=1)
34
35 # Ignore any "standard" include paths, and do not use any default CRT library.
36 if(NOT USE_CLANG_CL)
37 add_compile_options(/X /Zl)
38 endif()
39
40 # Disable buffer security checks by default.
41 add_compile_options(/GS-)
42
43 if(USE_CLANG_CL)
44 set(CMAKE_CL_SHOWINCLUDES_PREFIX "Note: including file: ")
45 endif()
46
47 # HACK: for VS 11+ we need to explicitly disable SSE, which is off by
48 # default for older compilers. See CORE-6507
49 if(ARCH STREQUAL "i386")
50 # Clang's IA32 means i386, which doesn't have cmpxchg8b
51 if(USE_CLANG_CL)
52 add_compile_options(-march=${OARCH})
53 else()
54 add_compile_options(/arch:IA32)
55 endif()
56 endif()
57
58 # VS 12+ requires /FS when used in parallel compilations
59 if(NOT MSVC_IDE)
60 add_compile_options(/FS)
61 endif()
62
63 # VS14+ tries to use thread-safe initialization
64 add_compile_options(/Zc:threadSafeInit-)
65
66 # HACK: Disable use of __CxxFrameHandler4 on VS 16.3+ (x64 only)
67 # See https://developercommunity.visualstudio.com/content/problem/746534/visual-c-163-runtime-uses-an-unsupported-api-for-u.html
68 if(ARCH STREQUAL "amd64" AND MSVC_VERSION GREATER 1922)
69 if (NOT CMAKE_C_COMPILER_ID STREQUAL "Clang")
70 add_compile_options(/d2FH4-)
71 endif()
72 add_link_options(/d2:-FH4-)
73 endif()
74
75 # Generate Warnings Level 3
76 add_compile_options(/W3)
77
78 # Disable overly sensitive warnings as well as those that generally aren't
79 # useful to us.
80 # - C4244: implicit integer truncation
81 # - C4290: C++ exception specification ignored
82 # - C4800: forcing value to bool 'true' or 'false' (performance warning)
83 # - C4200: nonstandard extension used : zero-sized array in struct/union
84 # - C4214: nonstandard extension used : bit field types other than int
85 add_compile_options(/wd4244 /wd4290 /wd4800 /wd4200 /wd4214)
86
87 # FIXME: Temporarily disable C4018 until we fix more of the others. CORE-10113
88 add_compile_options(/wd4018)
89
90 # Allow all warnings on msbuild/VS IDE
91 if (MSVC_IDE)
92 set(ALLOW_WARNINGS TRUE)
93 endif()
94
95 # On x86 Debug builds, if it's not Clang-CL or msbuild, treat all warnings as errors
96 if ((ARCH STREQUAL "i386") AND (CMAKE_BUILD_TYPE STREQUAL "Debug") AND (NOT USE_CLANG_CL) AND (NOT MSVC_IDE))
97 set(TREAT_ALL_WARNINGS_AS_ERRORS=TRUE)
98 endif()
99
100 # Define ALLOW_WARNINGS=TRUE on the cmake/configure command line to bypass errors
101 if (ALLOW_WARNINGS)
102 # Nothing
103 elseif (TREAT_ALL_WARNINGS_AS_ERRORS)
104 add_compile_options(/WX)
105 else()
106 # The following warnings are treated as errors:
107 # - C4013: implicit function declaration
108 # - C4020: too many actual parameters
109 # - C4022: pointer type mismatch for parameter
110 # - C4028: formal parameter different from declaration
111 # - C4047: different level of indirection
112 # - C4090: different 'modifier' qualifiers (for C programs only;
113 # for C++ programs, the compiler error C2440 is issued)
114 # - C4098: void function returning a value
115 # - C4113: parameter lists differ
116 # - C4129: unrecognized escape sequence
117 # - C4133: incompatible types - from '<x> *' to '<y> *'
118 # - C4163: 'identifier': not available as an intrinsic function
119 # - C4229: modifiers on data are ignored
120 # - C4311: pointer truncation from '<pointer>' to '<integer>'
121 # - C4312: conversion from '<integer>' to '<pointer>' of greater size
122 # - C4313: 'fprintf': '%x' in format string conflicts with argument n of type 'HANDLE'
123 # - C4477: '_snprintf' : format string '%ld' requires an argument of type 'long', but variadic argument 1 has type 'DWORD_PTR'
124 # - C4603: macro is not defined or definition is different after precompiled header use
125 # - C4700: uninitialized variable usage
126 # - C4715: 'function': not all control paths return a value
127 # - C4716: function must return a value
128 add_compile_options(/we4013 /we4020 /we4022 /we4028 /we4047 /we4090 /we4098 /we4113 /we4129 /we4133 /we4163 /we4229 /we4311 /we4312 /we4313 /we4477 /we4603 /we4700 /we4715 /we4716)
129
130 # - C4101: unreferenced local variable
131 # - C4189: local variable initialized but not referenced
132 # Not in Release mode, msbuild generator doesn't like CMAKE_BUILD_TYPE
133 if(MSVC_IDE OR CMAKE_BUILD_TYPE STREQUAL "Debug")
134 add_compile_options(/we4101 /we4189)
135 endif()
136
137 endif()
138
139 # Enable warnings above the default level, but don't treat them as errors:
140 # - C4115: named type definition in parentheses
141 add_compile_options(/w14115)
142
143 if(USE_CLANG_CL)
144 add_compile_options("$<$<COMPILE_LANGUAGE:C,CXX>:-nostdinc;-Wno-multichar;-Wno-char-subscripts;-Wno-microsoft-enum-forward-reference;-Wno-pragma-pack;-Wno-microsoft-anon-tag;-Wno-parentheses-equality;-Wno-unknown-pragmas>")
145 endif()
146
147 # Debugging
148 if(NOT (_PREFAST_ OR _VS_ANALYZE_))
149 add_compile_options($<$<CONFIG:Debug>:/Zi>)
150 endif()
151 add_compile_definitions($<$<CONFIG:Release>:NDEBUG>)
152
153 # Hotpatchable images
154 if(ARCH STREQUAL "i386")
155 if(NOT USE_CLANG_CL)
156 add_compile_options(/hotpatch)
157 endif()
158 set(_hotpatch_link_flag "/FUNCTIONPADMIN:5")
159 elseif(ARCH STREQUAL "amd64")
160 set(_hotpatch_link_flag "/FUNCTIONPADMIN:6")
161 endif()
162
163 if(MSVC_IDE AND (NOT DEFINED USE_FOLDER_STRUCTURE))
164 set(USE_FOLDER_STRUCTURE TRUE)
165 endif()
166
167 if(RUNTIME_CHECKS)
168 add_definitions(-D__RUNTIME_CHECKS__)
169 add_compile_options(/RTC1)
170 endif()
171
172 add_link_options(/MANIFEST:NO /INCREMENTAL:NO /SAFESEH:NO /NODEFAULTLIB /RELEASE ${_hotpatch_link_flag} /IGNORE:4039)
173
174 set(CMAKE_MSVC_RUNTIME_LIBRARY "")
175
176 # HACK: Remove the /implib argument, implibs are generated separately
177 string(REPLACE "/implib:<TARGET_IMPLIB>" "" CMAKE_C_LINK_EXECUTABLE "${CMAKE_C_LINK_EXECUTABLE}")
178 string(REPLACE "/implib:<TARGET_IMPLIB>" "" CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_CXX_LINK_EXECUTABLE}")
179 string(REPLACE "/implib:<TARGET_IMPLIB>" "" CMAKE_C_CREATE_SHARED_LIBRARY "${CMAKE_C_CREATE_SHARED_LIBRARY}")
180 string(REPLACE "/implib:<TARGET_IMPLIB>" "" CMAKE_CXX_CREATE_SHARED_LIBRARY "${CMAKE_CXX_CREATE_SHARED_LIBRARY}")
181 string(REPLACE "/implib:<TARGET_IMPLIB>" "" CMAKE_C_CREATE_SHARED_MODULE "${CMAKE_C_CREATE_SHARED_MODULE}")
182 string(REPLACE "/implib:<TARGET_IMPLIB>" "" CMAKE_CXX_CREATE_SHARED_MODULE "${CMAKE_CXX_CREATE_SHARED_MODULE}")
183
184 # HACK2: CMake lacks the ability to completely remove the 'implib' argument for solution files...
185 # To work around this, we just let it create a dummy file
186 if(MSVC_IDE)
187 set(CMAKE_IMPORT_LIBRARY_SUFFIX ".dummy")
188 endif()
189
190
191 if(CMAKE_DISABLE_NINJA_DEPSLOG)
192 set(cl_includes_flag "")
193 else()
194 set(cl_includes_flag "/showIncludes")
195 endif()
196
197 if(MSVC_IDE)
198 # For VS builds we'll only have en-US in resource files
199 add_definitions(/DLANGUAGE_EN_US)
200 else()
201 set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> /nologo <INCLUDES> <FLAGS> <DEFINES> ${I18N_DEFS} /fo <OBJECT> <SOURCE>")
202 endif()
203
204 # We don't put <INCLUDES> <DEFINES> <FLAGS> because this is handled in add_asm_files macro
205 if (NOT MSVC_IDE)
206 if(ARCH STREQUAL "arm" OR ARCH STREQUAL "arm64")
207 set(CMAKE_ASM_MASM_COMPILE_OBJECT "<CMAKE_ASM_MASM_COMPILER> -nologo -o <OBJECT> <SOURCE>")
208 else()
209 set(CMAKE_ASM_MASM_COMPILE_OBJECT "<CMAKE_ASM_MASM_COMPILER> /nologo /Cp /Fo <OBJECT> /c /Ta <SOURCE>")
210 endif()
211 endif()
212
213 if(_VS_ANALYZE_)
214 message("-- VS static analysis enabled!")
215 add_compile_options(/analyze:WX-)
216 elseif(_PREFAST_)
217 message("PREFAST enabled!")
218 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}"
219 "prefast LIST")
220 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}"
221 "prefast LIST")
222 set(CMAKE_C_LINK_EXECUTABLE
223 "<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}")
224 set(CMAKE_CXX_LINK_EXECUTABLE
225 "<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}")
226 endif()
227
228 set(CMAKE_RC_CREATE_SHARED_LIBRARY ${CMAKE_C_CREATE_SHARED_LIBRARY})
229 set(CMAKE_ASM_MASM_CREATE_SHARED_LIBRARY ${CMAKE_C_CREATE_SHARED_LIBRARY})
230 set(CMAKE_RC_CREATE_SHARED_MODULE ${CMAKE_C_CREATE_SHARED_MODULE})
231 set(CMAKE_ASM_MASM_CREATE_SHARED_MODULE ${CMAKE_C_CREATE_SHARED_MODULE})
232 set(CMAKE_ASM_CREATE_STATIC_LIBRARY ${CMAKE_C_CREATE_STATIC_LIBRARY})
233
234 function(set_entrypoint _module _entrypoint)
235 if(${_entrypoint} STREQUAL "0")
236 target_link_options(${_module} PRIVATE "/NOENTRY")
237 elseif(ARCH STREQUAL "i386")
238 set(_entrysymbol ${_entrypoint})
239 if(${ARGC} GREATER 2)
240 set(_entrysymbol ${_entrysymbol}@${ARGV2})
241 endif()
242 target_link_options(${_module} PRIVATE "/ENTRY:${_entrysymbol}")
243 else()
244 target_link_options(${_module} PRIVATE "/ENTRY:${_entrypoint}")
245 endif()
246 endfunction()
247
248 function(set_subsystem MODULE SUBSYSTEM)
249 string(TOUPPER ${SUBSYSTEM} _subsystem)
250 if(ARCH STREQUAL "amd64")
251 target_link_options(${MODULE} PRIVATE "/SUBSYSTEM:${_subsystem},5.02")
252 elseif(ARCH STREQUAL "arm")
253 target_link_options(${MODULE} PRIVATE "/SUBSYSTEM:${_subsystem},6.02")
254 elseif(ARCH STREQUAL "arm64")
255 target_link_options(${MODULE} PRIVATE "/SUBSYSTEM:${_subsystem},6.02")
256 else()
257 target_link_options(${MODULE} PRIVATE "/SUBSYSTEM:${_subsystem},5.01")
258 endif()
259 endfunction()
260
261 function(set_image_base MODULE IMAGE_BASE)
262 target_link_options(${MODULE} PRIVATE "/BASE:${IMAGE_BASE}")
263 endfunction()
264
265 function(set_module_type_toolchain MODULE TYPE)
266 # Set the PE image version numbers from the NT OS version ReactOS is based on
267 target_link_options(${MODULE} PRIVATE "/VERSION:5.01")
268
269 if((TYPE STREQUAL win32dll) OR (TYPE STREQUAL win32ocx) OR (TYPE STREQUAL cpl))
270 target_link_options(${MODULE} PRIVATE /DLL)
271 elseif(TYPE IN_LIST KERNEL_MODULE_TYPES)
272 # Mark INIT section as Executable Read Write Discardable
273 target_link_options(${MODULE} PRIVATE /SECTION:INIT,ERWD)
274
275 if(TYPE STREQUAL kernelmodedriver)
276 target_link_options(${MODULE} PRIVATE /DRIVER)
277 elseif(TYPE STREQUAL wdmdriver)
278 target_link_options(${MODULE} PRIVATE /DRIVER:WDM)
279 elseif (TYPE STREQUAL kernel)
280 # Mark .rsrc section as non-disposable non-pageable, as bugcheck code needs to access it
281 target_link_options(${MODULE} PRIVATE /SECTION:.rsrc,!DP)
282 endif()
283 endif()
284
285 if(RUNTIME_CHECKS)
286 target_link_libraries(${MODULE} runtmchk)
287 endif()
288
289 endfunction()
290
291 function(add_delay_importlibs _module)
292 get_target_property(_module_type ${_module} TYPE)
293 if(_module_type STREQUAL "STATIC_LIBRARY")
294 message(FATAL_ERROR "Cannot add delay imports to a static library")
295 endif()
296 foreach(_lib ${ARGN})
297 get_filename_component(_basename "${_lib}" NAME_WE)
298 get_filename_component(_ext "${_lib}" EXT)
299 if(NOT _ext)
300 set(_ext ".dll")
301 endif()
302 target_link_options(${_module} PRIVATE "/DELAYLOAD:${_basename}${_ext}")
303 target_link_libraries(${_module} "lib${_basename}")
304 endforeach()
305 target_link_libraries(${_module} delayimp)
306 endfunction()
307
308 function(fixup_load_config _target)
309 # msvc knows how to generate a load_config so no hacks here
310 endfunction()
311
312 function(generate_import_lib _libname _dllname _spec_file)
313
314 set(_def_file ${CMAKE_CURRENT_BINARY_DIR}/${_libname}_implib.def)
315 set(_asm_stubs_file ${CMAKE_CURRENT_BINARY_DIR}/${_libname}_stubs.asm)
316
317 # Generate the def and asm stub files
318 add_custom_command(
319 OUTPUT ${_asm_stubs_file} ${_def_file}
320 COMMAND native-spec2def --ms -a=${SPEC2DEF_ARCH} --implib -n=${_dllname} -d=${_def_file} -l=${_asm_stubs_file} ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file}
321 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} native-spec2def)
322
323 # Compile the generated asm stub file
324 if(ARCH STREQUAL "arm" OR ARCH STREQUAL "arm64")
325 set(_asm_stub_command ${CMAKE_ASM_MASM_COMPILER} -nologo -o ${_asm_stubs_file}.obj ${_asm_stubs_file})
326 else()
327 set(_asm_stub_command ${CMAKE_ASM_MASM_COMPILER} /nologo /Cp /Fo${_asm_stubs_file}.obj /c /Ta ${_asm_stubs_file})
328 endif()
329 add_custom_command(
330 OUTPUT ${_asm_stubs_file}.obj
331 COMMAND ${_asm_stub_command}
332 DEPENDS ${_asm_stubs_file})
333
334 # generate the intermediate import lib
335 set(_libfile_tmp ${CMAKE_CURRENT_BINARY_DIR}/${_libname}_tmp.lib)
336 set(_static_lib_options )
337
338 set(_implib_command ${CMAKE_LINKER} /LIB /NOLOGO /MACHINE:${WINARCH}
339 $<TARGET_PROPERTY:${_libname},STATIC_LIBRARY_FLAGS> $<TARGET_PROPERTY:${_libname},STATIC_LIBRARY_OPTIONS>
340 /DEF:${_def_file} /OUT:${_libfile_tmp} ${_asm_stubs_file}.obj)
341
342 add_custom_command(
343 OUTPUT ${_libfile_tmp}
344 COMMAND ${_implib_command}
345 DEPENDS ${_asm_stubs_file}.obj ${_def_file})
346
347 # By giving the import lib as an object input, LIB extracts the relevant object files and make a new library.
348 # This allows us to treat the implib as a regular static library
349 set_source_files_properties(${_libfile_tmp} PROPERTIES EXTERNAL_OBJECT TRUE)
350 add_library(${_libname} STATIC ${_libfile_tmp})
351
352 set_target_properties(${_libname} PROPERTIES LINKER_LANGUAGE "C")
353 endfunction()
354
355 if(ARCH STREQUAL "amd64")
356 # This is NOT a typo.
357 # See https://software.intel.com/en-us/forums/topic/404643
358 add_definitions(/D__x86_64)
359 set(SPEC2DEF_ARCH x86_64)
360 elseif(ARCH STREQUAL "arm")
361 set(SPEC2DEF_ARCH arm)
362 elseif(ARCH STREQUAL "arm64")
363 add_definitions(/D__arm64__)
364 set(SPEC2DEF_ARCH arm64)
365 else()
366 set(SPEC2DEF_ARCH i386)
367 endif()
368 function(spec2def _dllname _spec_file)
369
370 cmake_parse_arguments(__spec2def "ADD_IMPORTLIB;NO_PRIVATE_WARNINGS;WITH_RELAY" "VERSION" "" ${ARGN})
371
372 # Get library basename
373 get_filename_component(_file ${_dllname} NAME_WE)
374
375 # Error out on anything else than spec
376 if(NOT ${_spec_file} MATCHES ".*\\.spec")
377 message(FATAL_ERROR "spec2def only takes spec files as input.")
378 endif()
379
380 if(__spec2def_WITH_RELAY)
381 set(__with_relay_arg "--with-tracing")
382 endif()
383
384 if(__spec2def_VERSION)
385 set(__version_arg "--version=0x${__spec2def_VERSION}")
386 endif()
387
388 # Generate exports def and C stubs file for the DLL
389 add_custom_command(
390 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_file}.def ${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c
391 COMMAND native-spec2def --ms -a=${SPEC2DEF_ARCH} -n=${_dllname} -d=${CMAKE_CURRENT_BINARY_DIR}/${_file}.def -s=${CMAKE_CURRENT_BINARY_DIR}/${_file}_stubs.c ${__with_relay_arg} ${__version_arg} ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file}
392 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_spec_file} native-spec2def)
393
394 if(__spec2def_ADD_IMPORTLIB)
395 generate_import_lib(lib${_file} ${_dllname} ${_spec_file})
396 if(__spec2def_NO_PRIVATE_WARNINGS)
397 set_property(TARGET lib${_file} APPEND PROPERTY STATIC_LIBRARY_OPTIONS /ignore:4104)
398 endif()
399 endif()
400 endfunction()
401
402 macro(macro_mc FLAG FILE)
403 set(COMMAND_MC ${CMAKE_MC_COMPILER} -u ${FLAG} -b -h ${CMAKE_CURRENT_BINARY_DIR}/ -r ${CMAKE_CURRENT_BINARY_DIR}/ ${FILE})
404 endmacro()
405
406 # PSEH workaround
407 set(PSEH_LIB "pseh")
408
409 # Use a full path for the x86 version of ml when using x64 VS.
410 # It's not a problem when using the DDK/WDK because, in x64 mode,
411 # both the x86 and x64 versions of ml are available.
412 if(ARCH STREQUAL "amd64")
413 if((MSVC_VERSION LESS_EQUAL 1900) AND (DEFINED ENV{VCINSTALLDIR}))
414 set(CMAKE_ASM16_COMPILER $ENV{VCINSTALLDIR}/bin/ml.exe)
415 elseif(DEFINED ENV{VCToolsInstallDir})
416 set(CMAKE_ASM16_COMPILER $ENV{VCToolsInstallDir}/bin/HostX86/x86/ml.exe)
417 else()
418 set(CMAKE_ASM16_COMPILER ml.exe)
419 endif()
420 elseif(ARCH STREQUAL "arm")
421 set(CMAKE_ASM16_COMPILER armasm.exe)
422 elseif(ARCH STREQUAL "arm64")
423 set(CMAKE_ASM16_COMPILER armasm64.exe)
424 else()
425 set(CMAKE_ASM16_COMPILER ml.exe)
426 endif()
427
428 function(CreateBootSectorTarget _target_name _asm_file _binary_file _base_address)
429 set(_object_file ${_binary_file}.obj)
430 set(_temp_file ${_binary_file}.tmp)
431
432 get_defines(_defines)
433 get_includes(_includes)
434
435 if(USE_CLANG_CL)
436 set(_no_std_includes_flag "-nostdinc")
437 else()
438 set(_no_std_includes_flag "/X")
439 endif()
440
441 add_custom_command(
442 OUTPUT ${_temp_file}
443 COMMAND ${CMAKE_C_COMPILER} /nologo ${_no_std_includes_flag} /I${REACTOS_SOURCE_DIR}/sdk/include/asm /I${REACTOS_BINARY_DIR}/sdk/include/asm ${_includes} ${_defines} /D__ASM__ /D_USE_ML /EP /c ${_asm_file} > ${_temp_file}
444 DEPENDS ${_asm_file})
445
446 if(ARCH STREQUAL "arm" OR ARCH STREQUAL "arm64")
447 set(_asm16_command ${CMAKE_ASM16_COMPILER} -nologo -o ${_object_file} ${_temp_file})
448 else()
449 set(_asm16_command ${CMAKE_ASM16_COMPILER} /nologo /Cp /Fo${_object_file} /c /Ta ${_temp_file})
450 endif()
451
452 add_custom_command(
453 OUTPUT ${_object_file}
454 COMMAND ${_asm16_command}
455 DEPENDS ${_temp_file})
456
457 add_custom_command(
458 OUTPUT ${_binary_file}
459 COMMAND native-obj2bin ${_object_file} ${_binary_file} ${_base_address}
460 DEPENDS ${_object_file} native-obj2bin)
461
462 set_source_files_properties(${_object_file} ${_temp_file} ${_binary_file} PROPERTIES GENERATED TRUE)
463
464 add_custom_target(${_target_name} ALL DEPENDS ${_binary_file})
465 endfunction()
466
467 function(allow_warnings __module)
468 endfunction()
469
470 macro(add_asm_files _target)
471 get_defines(_directory_defines)
472 get_includes(_directory_includes)
473 get_directory_property(_defines COMPILE_DEFINITIONS)
474 foreach(_source_file ${ARGN})
475 get_filename_component(_source_file_base_name ${_source_file} NAME_WE)
476 get_filename_component(_source_file_full_path ${_source_file} ABSOLUTE)
477 set(_preprocessed_asm_file ${CMAKE_CURRENT_BINARY_DIR}/asm/${_source_file_base_name}_${_target}.asm)
478 get_source_file_property(_defines_semicolon_list ${_source_file_full_path} COMPILE_DEFINITIONS)
479 unset(_source_file_defines)
480 foreach(_define ${_defines_semicolon_list})
481 if(NOT ${_define} STREQUAL "NOTFOUND")
482 list(APPEND _source_file_defines -D${_define})
483 endif()
484 endforeach()
485 add_custom_command(
486 OUTPUT ${_preprocessed_asm_file}
487 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}
488 DEPENDS ${_source_file_full_path})
489 list(APPEND ${_target} ${_preprocessed_asm_file})
490 endforeach()
491 endmacro()
492
493 function(add_linker_script _target _linker_script_file)
494 get_filename_component(_file_full_path ${_linker_script_file} ABSOLUTE)
495 get_filename_component(_file_name ${_linker_script_file} NAME)
496 set(_generated_file_path_prefix "${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${_target}.dir/${_file_name}")
497
498 # Generate the ASM module containing sections specifications and layout.
499 set(_generated_file "${_generated_file_path_prefix}.S")
500 add_custom_command(
501 OUTPUT ${_generated_file}
502 COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${_file_full_path}" "${_generated_file}"
503 DEPENDS ${_file_full_path})
504 set_source_files_properties(${_generated_file} PROPERTIES LANGUAGE "ASM_MASM" GENERATED TRUE)
505 add_asm_files(${_target}_linker_file ${_generated_file})
506
507 # Generate the C module containing extra sections specifications and layout,
508 # as well as comment-type linker #pragma directives.
509 set(_generated_file "${_generated_file_path_prefix}.c")
510 add_custom_command(
511 OUTPUT ${_generated_file}
512 COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${_file_full_path}" "${_generated_file}"
513 DEPENDS ${_file_full_path})
514 set_source_files_properties(${_generated_file} PROPERTIES LANGUAGE "C" GENERATED TRUE)
515 list(APPEND ${_target}_linker_file ${_generated_file})
516
517 # Add both files to the sources of the target.
518 target_sources(${_target} PRIVATE "${${_target}_linker_file}")
519
520 # Create the additional linker response file.
521 set(_generated_file "${_generated_file_path_prefix}.rsp")
522 if(USE_CLANG_CL)
523 set(_no_std_includes_flag "-nostdinc")
524 else()
525 set(_no_std_includes_flag "/X")
526 endif()
527 if(MSVC_IDE)
528 # MSBuild, via the VS IDE, uses response files when calling CL or LINK.
529 # We cannot specify a custom response file on the linker command-line,
530 # since specifying response files from within response files is forbidden.
531 # We therefore have to pre-process, at configuration time, the linker
532 # script so as to retrieve the custom linker options to be appended
533 # to the linker command-line.
534 execute_process(
535 COMMAND ${CMAKE_C_COMPILER} /nologo ${_no_std_includes_flag} /D__LINKER__ /EP /c "${_file_full_path}"
536 # OUTPUT_FILE "${_generated_file}"
537 OUTPUT_VARIABLE linker_options
538 ERROR_QUIET
539 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
540 RESULT_VARIABLE linker_rsp_result
541 OUTPUT_STRIP_TRAILING_WHITESPACE)
542 if(NOT linker_rsp_result EQUAL 0)
543 message(FATAL_ERROR "Generating pre-processed linker options for target '${_target}' failed with error ${linker_rsp_result}.")
544 endif()
545 # file(STRINGS ${_generated_file} linker_options NEWLINE_CONSUME)
546 string(REGEX REPLACE "[\r\n]+" ";" linker_options "${linker_options}")
547 target_link_options(${_target} PRIVATE ${linker_options})
548 else()
549 # Generate at compile-time a linker response file and append it
550 # to the linker command-line.
551 add_custom_command(
552 # OUTPUT ${_generated_file}
553 TARGET ${_target} PRE_LINK # PRE_BUILD
554 COMMAND ${CMAKE_C_COMPILER} /nologo ${_no_std_includes_flag} /D__LINKER__ /EP /c "${_file_full_path}" > "${_generated_file}"
555 DEPENDS ${_file_full_path}
556 VERBATIM)
557 set_source_files_properties(${_generated_file} PROPERTIES GENERATED TRUE)
558 # add_custom_target("${_target}_${_file_name}" ALL DEPENDS ${_generated_file})
559 # add_dependencies(${_target} "${_target}_${_file_name}")
560 target_link_options(${_target} PRIVATE "@${_generated_file}")
561 set_property(TARGET ${_target} APPEND PROPERTY LINK_DEPENDS ${_file_full_path})
562 endif()
563 endfunction()
564
565 # handle C++ options
566 # disable RTTI unless said so
567 add_compile_options("$<$<COMPILE_LANGUAGE:CXX>:$<IF:$<BOOL:$<TARGET_PROPERTY:WITH_CXX_RTTI>>,/GR,/GR->>")
568 # disable exceptions unless said so
569 add_compile_options("$<$<COMPILE_LANGUAGE:CXX>:$<IF:$<BOOL:$<TARGET_PROPERTY:WITH_CXX_EXCEPTIONS>>,/EHsc,/EHs-c->>")
570
571 # Create our interface libraries wrapping the needed library for this compiler
572 add_library(cppstl INTERFACE)
573 target_link_libraries(cppstl INTERFACE cpprt stlport oldnames)
574 # We set this properties through our INTERFACE library
575 set_target_properties(cppstl PROPERTIES INTERFACE_WITH_CXX_STL TRUE)
576 # add_library(cpprt INTERFACE)
577 # Our runtime library is already called cpprt