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