1 # Set initial flags for each configuration
3 set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
4 set(CMAKE_C_STANDARD 11)
5 set(CMAKE_C_STANDARD_REQUIRED ON)
6 set(CMAKE_CXX_STANDARD 11)
7 set(CMAKE_CXX_STANDARD_REQUIRED ON)
9 set(CLR_DEFINES_DEBUG_INIT DEBUG _DEBUG _DBG URTBLDENV_FRIENDLY=Checked BUILDENV_CHECKED=1)
10 set(CLR_DEFINES_CHECKED_INIT DEBUG _DEBUG _DBG URTBLDENV_FRIENDLY=Checked BUILDENV_CHECKED=1)
11 set(CLR_DEFINES_RELEASE_INIT NDEBUG URTBLDENV_FRIENDLY=Retail)
12 set(CLR_DEFINES_RELWITHDEBINFO_INIT NDEBUG URTBLDENV_FRIENDLY=Retail)
14 include(${CMAKE_CURRENT_LIST_DIR}/configureoptimization.cmake)
16 #----------------------------------------
17 # Detect and set platform variable names
18 # - for non-windows build platform & architecture is detected using inbuilt CMAKE variables and cross target component configure
19 # - for windows we use the passed in parameter to CMAKE to determine build arch
20 #----------------------------------------
21 if(CMAKE_SYSTEM_NAME STREQUAL Linux)
22 set(CLR_CMAKE_PLATFORM_UNIX 1)
23 if(CLR_CROSS_COMPONENTS_BUILD)
24 # CMAKE_HOST_SYSTEM_PROCESSOR returns the value of `uname -p` on host.
25 if(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL x86_64 OR CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL amd64)
26 if(CLR_CMAKE_TARGET_ARCH STREQUAL "arm" OR CLR_CMAKE_TARGET_ARCH STREQUAL "armel")
27 if($ENV{CROSSCOMPILE} STREQUAL "1")
28 set(CLR_CMAKE_PLATFORM_UNIX_X86 1)
30 set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
33 set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
35 elseif(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL i686)
36 set(CLR_CMAKE_PLATFORM_UNIX_X86 1)
41 # CMAKE_SYSTEM_PROCESSOR returns the value of `uname -p` on target.
42 # For the AMD/Intel 64bit architecture two different strings are common.
43 # Linux and Darwin identify it as "x86_64" while FreeBSD and netbsd uses the
44 # "amd64" string. Accept either of the two here.
45 if(CMAKE_SYSTEM_PROCESSOR STREQUAL x86_64 OR CMAKE_SYSTEM_PROCESSOR STREQUAL amd64)
46 set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
47 elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL armv7l)
48 set(CLR_CMAKE_PLATFORM_UNIX_ARM 1)
49 elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL arm)
50 set(CLR_CMAKE_PLATFORM_UNIX_ARM 1)
51 elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL aarch64)
52 set(CLR_CMAKE_PLATFORM_UNIX_ARM64 1)
53 elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL i686)
54 set(CLR_CMAKE_PLATFORM_UNIX_X86 1)
59 set(CLR_CMAKE_PLATFORM_LINUX 1)
62 if(DEFINED CLR_CMAKE_LINUX_ID)
63 if(CLR_CMAKE_LINUX_ID STREQUAL ubuntu)
64 set(CLR_CMAKE_TARGET_UBUNTU_LINUX 1)
65 elseif(CLR_CMAKE_LINUX_ID STREQUAL tizen)
66 set(CLR_CMAKE_TARGET_TIZEN_LINUX 1)
67 elseif(CLR_CMAKE_LINUX_ID STREQUAL alpine)
68 set(CLR_CMAKE_PLATFORM_ALPINE_LINUX 1)
70 if(CLR_CMAKE_LINUX_ID STREQUAL ubuntu)
71 set(CLR_CMAKE_PLATFORM_UBUNTU_LINUX 1)
73 endif(DEFINED CLR_CMAKE_LINUX_ID)
74 endif(CMAKE_SYSTEM_NAME STREQUAL Linux)
76 if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
77 set(CLR_CMAKE_PLATFORM_UNIX 1)
78 set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
79 set(CLR_CMAKE_PLATFORM_DARWIN 1)
80 set(CMAKE_ASM_COMPILE_OBJECT "${CMAKE_C_COMPILER} <FLAGS> <DEFINES> <INCLUDES> -o <OBJECT> -c <SOURCE>")
81 endif(CMAKE_SYSTEM_NAME STREQUAL Darwin)
83 if(CMAKE_SYSTEM_NAME STREQUAL FreeBSD)
84 set(CLR_CMAKE_PLATFORM_UNIX 1)
85 set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
86 set(CLR_CMAKE_PLATFORM_FREEBSD 1)
87 endif(CMAKE_SYSTEM_NAME STREQUAL FreeBSD)
89 if(CMAKE_SYSTEM_NAME STREQUAL OpenBSD)
90 set(CLR_CMAKE_PLATFORM_UNIX 1)
91 set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
92 set(CLR_CMAKE_PLATFORM_OPENBSD 1)
93 endif(CMAKE_SYSTEM_NAME STREQUAL OpenBSD)
95 if(CMAKE_SYSTEM_NAME STREQUAL NetBSD)
96 set(CLR_CMAKE_PLATFORM_UNIX 1)
97 set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
98 set(CLR_CMAKE_PLATFORM_NETBSD 1)
99 endif(CMAKE_SYSTEM_NAME STREQUAL NetBSD)
101 if(CMAKE_SYSTEM_NAME STREQUAL SunOS)
102 set(CLR_CMAKE_PLATFORM_UNIX 1)
105 OUTPUT_VARIABLE SUNOS_NATIVE_INSTRUCTION_SET
107 if(SUNOS_NATIVE_INSTRUCTION_SET MATCHES "amd64")
108 set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
109 set(CMAKE_SYSTEM_PROCESSOR "amd64")
113 set(CLR_CMAKE_PLATFORM_SUNOS 1)
114 endif(CMAKE_SYSTEM_NAME STREQUAL SunOS)
116 #--------------------------------------------
117 # This repo builds two set of binaries
118 # 1. binaries which execute on target arch machine
119 # - for such binaries host architecture & target architecture are same
121 # 2. binaries which execute on host machine but target another architecture
122 # - host architecture is different from target architecture
123 # - eg. crossgen.exe - runs on x64 machine and generates nis targeting arm64
124 # - for complete list of such binaries refer to file crosscomponents.cmake
125 #-------------------------------------------------------------
126 # Set HOST architecture variables
127 if(CLR_CMAKE_PLATFORM_UNIX_ARM)
128 set(CLR_CMAKE_PLATFORM_ARCH_ARM 1)
129 set(CLR_CMAKE_HOST_ARCH "arm")
130 elseif(CLR_CMAKE_PLATFORM_UNIX_ARM64)
131 set(CLR_CMAKE_PLATFORM_ARCH_ARM64 1)
132 set(CLR_CMAKE_HOST_ARCH "arm64")
133 elseif(CLR_CMAKE_PLATFORM_UNIX_AMD64)
134 set(CLR_CMAKE_PLATFORM_ARCH_AMD64 1)
135 set(CLR_CMAKE_HOST_ARCH "x64")
136 elseif(CLR_CMAKE_PLATFORM_UNIX_X86)
137 set(CLR_CMAKE_PLATFORM_ARCH_I386 1)
138 set(CLR_CMAKE_HOST_ARCH "x86")
140 # CLR_CMAKE_HOST_ARCH is passed in as param to cmake
141 if (CLR_CMAKE_HOST_ARCH STREQUAL x64)
142 set(CLR_CMAKE_PLATFORM_ARCH_AMD64 1)
143 elseif(CLR_CMAKE_HOST_ARCH STREQUAL x86)
144 set(CLR_CMAKE_PLATFORM_ARCH_I386 1)
145 elseif(CLR_CMAKE_HOST_ARCH STREQUAL arm)
146 set(CLR_CMAKE_PLATFORM_ARCH_ARM 1)
147 elseif(CLR_CMAKE_HOST_ARCH STREQUAL arm64)
148 set(CLR_CMAKE_PLATFORM_ARCH_ARM64 1)
154 # Set TARGET architecture variables
155 # Target arch will be a cmake param (optional) for both windows as well as non-windows build
156 # if target arch is not specified then host & target are same
157 if(NOT DEFINED CLR_CMAKE_TARGET_ARCH OR CLR_CMAKE_TARGET_ARCH STREQUAL "" )
158 set(CLR_CMAKE_TARGET_ARCH ${CLR_CMAKE_HOST_ARCH})
161 # Set target architecture variables
162 if (CLR_CMAKE_TARGET_ARCH STREQUAL x64)
163 set(CLR_CMAKE_TARGET_ARCH_AMD64 1)
164 elseif(CLR_CMAKE_TARGET_ARCH STREQUAL x86)
165 set(CLR_CMAKE_TARGET_ARCH_I386 1)
166 elseif(CLR_CMAKE_TARGET_ARCH STREQUAL arm64)
167 set(CLR_CMAKE_TARGET_ARCH_ARM64 1)
168 elseif(CLR_CMAKE_TARGET_ARCH STREQUAL arm)
169 set(CLR_CMAKE_TARGET_ARCH_ARM 1)
170 elseif(CLR_CMAKE_TARGET_ARCH STREQUAL armel)
171 set(CLR_CMAKE_TARGET_ARCH_ARM 1)
177 # check if host & target arch combination are valid
178 if(NOT(CLR_CMAKE_TARGET_ARCH STREQUAL CLR_CMAKE_HOST_ARCH))
179 if(NOT((CLR_CMAKE_PLATFORM_ARCH_AMD64 AND CLR_CMAKE_TARGET_ARCH_ARM64) OR (CLR_CMAKE_PLATFORM_ARCH_I386 AND CLR_CMAKE_TARGET_ARCH_ARM) OR (CLR_CMAKE_PLATFORM_ARCH_AMD64 AND CLR_CMAKE_TARGET_ARCH_ARM)))
180 message(FATAL_ERROR "Invalid host and target arch combination")
184 #-----------------------------------------------------
185 # Initialize Cmake compiler flags and other variables
186 #-----------------------------------------------------
189 add_compile_options(/Zi /FC /Zc:strictStrings)
190 elseif (CLR_CMAKE_PLATFORM_UNIX)
191 add_compile_options(-g)
192 add_compile_options(-Wall)
193 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
194 add_compile_options(-Wno-null-conversion)
196 add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-Werror=conversion-null>)
200 if (CMAKE_CONFIGURATION_TYPES) # multi-configuration generator?
201 set(CMAKE_CONFIGURATION_TYPES "Debug;Checked;Release;RelWithDebInfo" CACHE STRING "" FORCE)
202 endif (CMAKE_CONFIGURATION_TYPES)
204 set(CMAKE_C_FLAGS_CHECKED "")
205 set(CMAKE_CXX_FLAGS_CHECKED "")
206 set(CMAKE_EXE_LINKER_FLAGS_CHECKED "")
207 set(CMAKE_SHARED_LINKER_FLAGS_CHECKED "")
209 set(CMAKE_CXX_STANDARD_LIBRARIES "") # do not link against standard win32 libs i.e. kernel32, uuid, user32, etc.
212 # For multi-configuration toolset (as Visual Studio)
213 # set the different configuration defines.
214 foreach (Config DEBUG CHECKED RELEASE RELWITHDEBINFO)
215 foreach (Definition IN LISTS CLR_DEFINES_${Config}_INIT)
216 set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS $<$<CONFIG:${Config}>:${Definition}>)
217 endforeach (Definition)
220 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /GUARD:CF")
221 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /GUARD:CF")
225 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /MANIFEST:NO") #Do not create Side-by-Side Assembly Manifest
227 if (CLR_CMAKE_PLATFORM_ARCH_ARM)
228 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /SUBSYSTEM:WINDOWS,6.02") #windows subsystem - arm minimum is 6.02
229 elseif(CLR_CMAKE_PLATFORM_ARCH_ARM64)
230 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /SUBSYSTEM:WINDOWS,6.03") #windows subsystem - arm64 minimum is 6.03
232 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /SUBSYSTEM:WINDOWS,6.01") #windows subsystem
235 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /LARGEADDRESSAWARE") # can handle addresses larger than 2 gigabytes
236 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /NXCOMPAT") #Compatible with Data Execution Prevention
237 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /DYNAMICBASE") #Use address space layout randomization
238 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /PDBCOMPRESS") #shrink pdb size
239 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /DEBUG")
240 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /IGNORE:4197,4013,4254,4070,4221")
242 set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} /IGNORE:4221")
244 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /DEBUG /PDBCOMPRESS")
245 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /STACK:1572864")
247 # Temporarily disable incremental link due to incremental linking CFG bug crashing crossgen.
248 # See https://github.com/dotnet/coreclr/issues/12592
249 # This has been fixed in VS 2017 Update 5 but we're keeping this around until everyone is off
250 # the versions that have the bug. The bug manifests itself as a bad crash.
251 set(NO_INCREMENTAL_LINKER_FLAGS "/INCREMENTAL:NO")
253 # Debug build specific flags
254 set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "/NOVCFEATURE ${NO_INCREMENTAL_LINKER_FLAGS}")
255 set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${NO_INCREMENTAL_LINKER_FLAGS}")
257 # Checked build specific flags
258 set(CMAKE_SHARED_LINKER_FLAGS_CHECKED "${CMAKE_SHARED_LINKER_FLAGS_CHECKED} /OPT:REF /OPT:NOICF /NOVCFEATURE ${NO_INCREMENTAL_LINKER_FLAGS}")
259 set(CMAKE_STATIC_LINKER_FLAGS_CHECKED "${CMAKE_STATIC_LINKER_FLAGS_CHECKED}")
260 set(CMAKE_EXE_LINKER_FLAGS_CHECKED "${CMAKE_EXE_LINKER_FLAGS_CHECKED} /OPT:REF /OPT:NOICF ${NO_INCREMENTAL_LINKER_FLAGS}")
262 # Release build specific flags
263 set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /LTCG /OPT:REF /OPT:ICF ${NO_INCREMENTAL_LINKER_FLAGS}")
264 set(CMAKE_STATIC_LINKER_FLAGS_RELEASE "${CMAKE_STATIC_LINKER_FLAGS_RELEASE} /LTCG")
265 set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /LTCG /OPT:REF /OPT:ICF ${NO_INCREMENTAL_LINKER_FLAGS}")
267 # ReleaseWithDebugInfo build specific flags
268 set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO} /LTCG /OPT:REF /OPT:ICF ${NO_INCREMENTAL_LINKER_FLAGS}")
269 set(CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO} /LTCG")
270 set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} /LTCG /OPT:REF /OPT:ICF ${NO_INCREMENTAL_LINKER_FLAGS}")
272 # Force uCRT to be dynamically linked for Release build
273 set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /NODEFAULTLIB:libucrt.lib /DEFAULTLIB:ucrt.lib")
274 set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /NODEFAULTLIB:libucrt.lib /DEFAULTLIB:ucrt.lib")
275 set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO} /NODEFAULTLIB:libucrt.lib /DEFAULTLIB:ucrt.lib")
276 set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} /NODEFAULTLIB:libucrt.lib /DEFAULTLIB:ucrt.lib")
278 elseif (CLR_CMAKE_PLATFORM_UNIX)
279 # Set the values to display when interactively configuring CMAKE_BUILD_TYPE
280 set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "DEBUG;CHECKED;RELEASE;RELWITHDEBINFO")
282 # Use uppercase CMAKE_BUILD_TYPE for the string comparisons below
283 string(TOUPPER ${CMAKE_BUILD_TYPE} UPPERCASE_CMAKE_BUILD_TYPE)
285 # For single-configuration toolset
286 # set the different configuration defines.
287 if (UPPERCASE_CMAKE_BUILD_TYPE STREQUAL DEBUG)
289 set_property(DIRECTORY PROPERTY COMPILE_DEFINITIONS ${CLR_DEFINES_DEBUG_INIT})
290 elseif (UPPERCASE_CMAKE_BUILD_TYPE STREQUAL CHECKED)
292 set_property(DIRECTORY PROPERTY COMPILE_DEFINITIONS ${CLR_DEFINES_CHECKED_INIT})
293 elseif (UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELEASE)
295 set_property(DIRECTORY PROPERTY COMPILE_DEFINITIONS ${CLR_DEFINES_RELEASE_INIT})
296 elseif (UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELWITHDEBINFO)
297 # And then RELWITHDEBINFO
298 set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS ${CLR_DEFINES_RELWITHDEBINFO_INIT})
300 message(FATAL_ERROR "Unknown build type! Set CMAKE_BUILD_TYPE to DEBUG, CHECKED, RELEASE, or RELWITHDEBINFO!")
303 # set the CLANG sanitizer flags for debug build
304 if(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL DEBUG OR UPPERCASE_CMAKE_BUILD_TYPE STREQUAL CHECKED)
305 # obtain settings from running enablesanitizers.sh
306 string(FIND "$ENV{DEBUG_SANITIZERS}" "asan" __ASAN_POS)
307 string(FIND "$ENV{DEBUG_SANITIZERS}" "ubsan" __UBSAN_POS)
308 if ((${__ASAN_POS} GREATER -1) OR (${__UBSAN_POS} GREATER -1))
309 set(CLR_SANITIZE_CXX_FLAGS "${CLR_SANITIZE_CXX_FLAGS} -fsanitize-blacklist=${CMAKE_CURRENT_SOURCE_DIR}/sanitizerblacklist.txt -fsanitize=")
310 set(CLR_SANITIZE_LINK_FLAGS "${CLR_SANITIZE_LINK_FLAGS} -fsanitize=")
311 if (${__ASAN_POS} GREATER -1)
312 set(CLR_SANITIZE_CXX_FLAGS "${CLR_SANITIZE_CXX_FLAGS}address,")
313 set(CLR_SANITIZE_LINK_FLAGS "${CLR_SANITIZE_LINK_FLAGS}address,")
314 add_definitions(-DHAS_ASAN)
315 message("Address Sanitizer (asan) enabled")
317 if (${__UBSAN_POS} GREATER -1)
318 # all sanitizier flags are enabled except alignment (due to heavy use of __unaligned modifier)
319 set(CLR_SANITIZE_CXX_FLAGS "${CLR_SANITIZE_CXX_FLAGS}bool,bounds,enum,float-cast-overflow,float-divide-by-zero,function,integer,nonnull-attribute,null,object-size,return,returns-nonnull-attribute,shift,unreachable,vla-bound,vptr")
320 set(CLR_SANITIZE_LINK_FLAGS "${CLR_SANITIZE_LINK_FLAGS}undefined")
321 message("Undefined Behavior Sanitizer (ubsan) enabled")
324 # -fdata-sections -ffunction-sections: each function has own section instead of one per .o file (needed for --gc-sections)
325 # -fPIC: enable Position Independent Code normally just for shared libraries but required when linking with address sanitizer
326 # -O1: optimization level used instead of -O0 to avoid compile error "invalid operand for inline asm constraint"
327 set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${CLR_SANITIZE_CXX_FLAGS} -fdata-sections -ffunction-sections -fPIC -O1")
328 set(CMAKE_CXX_FLAGS_CHECKED "${CMAKE_CXX_FLAGS_CHECKED} ${CLR_SANITIZE_CXX_FLAGS} -fdata-sections -ffunction-sections -fPIC -O1")
330 set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} ${CLR_SANITIZE_LINK_FLAGS}")
331 set(CMAKE_EXE_LINKER_FLAGS_CHECKED "${CMAKE_EXE_LINKER_FLAGS_CHECKED} ${CLR_SANITIZE_LINK_FLAGS}")
333 # -Wl and --gc-sections: drop unused sections\functions (similar to Windows /Gy function-level-linking)
334 set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} ${CLR_SANITIZE_LINK_FLAGS} -Wl,--gc-sections")
335 set(CMAKE_SHARED_LINKER_FLAGS_CHECKED "${CMAKE_SHARED_LINKER_FLAGS_CHECKED} ${CLR_SANITIZE_LINK_FLAGS} -Wl,--gc-sections")
337 endif(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL DEBUG OR UPPERCASE_CMAKE_BUILD_TYPE STREQUAL CHECKED)
340 # CLR_ADDITIONAL_LINKER_FLAGS - used for passing additional arguments to linker
341 # CLR_ADDITIONAL_COMPILER_OPTIONS - used for passing additional arguments to compiler
344 # ./build-native.sh cmakeargs "-DCLR_ADDITIONAL_COMPILER_OPTIONS=<...>" cmakeargs "-DCLR_ADDITIONAL_LINKER_FLAGS=<...>"
346 if(CLR_CMAKE_PLATFORM_UNIX)
347 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${CLR_ADDITIONAL_LINKER_FLAGS}")
348 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${CLR_ADDITIONAL_LINKER_FLAGS}" )
349 endif(CLR_CMAKE_PLATFORM_UNIX)
351 if(CLR_CMAKE_PLATFORM_LINUX)
352 set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -Wa,--noexecstack")
353 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--build-id=sha1")
354 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--build-id=sha1")
355 endif(CLR_CMAKE_PLATFORM_LINUX)
356 if(CLR_CMAKE_PLATFORM_FREEBSD)
357 set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -Wa,--noexecstack")
358 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fuse-ld=lld -Xlinker --build-id=sha1")
359 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=lld -Xlinker --build-id=sha1")
360 endif(CLR_CMAKE_PLATFORM_FREEBSD)
362 #------------------------------------
363 # Definitions (for platform)
364 #-----------------------------------
365 if (CLR_CMAKE_PLATFORM_ARCH_AMD64)
366 add_definitions(-D_AMD64_)
367 add_definitions(-D_WIN64)
368 add_definitions(-DAMD64)
369 add_definitions(-DBIT64=1)
370 elseif (CLR_CMAKE_PLATFORM_ARCH_I386)
371 add_definitions(-D_X86_)
372 elseif (CLR_CMAKE_PLATFORM_ARCH_ARM)
373 add_definitions(-D_ARM_)
374 add_definitions(-DARM)
375 elseif (CLR_CMAKE_PLATFORM_ARCH_ARM64)
376 add_definitions(-D_ARM64_)
377 add_definitions(-DARM64)
378 add_definitions(-D_WIN64)
379 add_definitions(-DBIT64=1)
384 if (CLR_CMAKE_PLATFORM_UNIX)
385 if(CLR_CMAKE_PLATFORM_LINUX)
386 if(CLR_CMAKE_PLATFORM_UNIX_AMD64)
387 message("Detected Linux x86_64")
388 add_definitions(-DLINUX64)
389 elseif(CLR_CMAKE_PLATFORM_UNIX_ARM)
390 message("Detected Linux ARM")
391 add_definitions(-DLINUX32)
392 elseif(CLR_CMAKE_PLATFORM_UNIX_ARM64)
393 message("Detected Linux ARM64")
394 add_definitions(-DLINUX64)
395 elseif(CLR_CMAKE_PLATFORM_UNIX_X86)
396 message("Detected Linux i686")
397 add_definitions(-DLINUX32)
401 endif(CLR_CMAKE_PLATFORM_LINUX)
402 endif(CLR_CMAKE_PLATFORM_UNIX)
404 if (CLR_CMAKE_PLATFORM_UNIX)
405 add_definitions(-DPLATFORM_UNIX=1)
407 if(CLR_CMAKE_PLATFORM_DARWIN)
408 message("Detected OSX x86_64")
409 endif(CLR_CMAKE_PLATFORM_DARWIN)
411 if(CLR_CMAKE_PLATFORM_FREEBSD)
412 message("Detected FreeBSD amd64")
413 endif(CLR_CMAKE_PLATFORM_FREEBSD)
415 if(CLR_CMAKE_PLATFORM_NETBSD)
416 message("Detected NetBSD amd64")
417 endif(CLR_CMAKE_PLATFORM_NETBSD)
418 endif(CLR_CMAKE_PLATFORM_UNIX)
421 add_definitions(-DPLATFORM_WINDOWS=1)
423 # Define the CRT lib references that link into Desktop imports
424 set(STATIC_MT_CRT_LIB "libcmt$<$<OR:$<CONFIG:Debug>,$<CONFIG:Checked>>:d>.lib")
425 set(STATIC_MT_VCRT_LIB "libvcruntime$<$<OR:$<CONFIG:Debug>,$<CONFIG:Checked>>:d>.lib")
426 set(STATIC_MT_CPP_LIB "libcpmt$<$<OR:$<CONFIG:Debug>,$<CONFIG:Checked>>:d>.lib")
429 # Architecture specific files folder name
430 if (CLR_CMAKE_TARGET_ARCH_AMD64)
431 set(ARCH_SOURCES_DIR amd64)
432 elseif (CLR_CMAKE_TARGET_ARCH_ARM64)
433 set(ARCH_SOURCES_DIR arm64)
434 elseif (CLR_CMAKE_TARGET_ARCH_ARM)
435 set(ARCH_SOURCES_DIR arm)
436 elseif (CLR_CMAKE_TARGET_ARCH_I386)
437 set(ARCH_SOURCES_DIR i386)
442 #--------------------------------------
444 #--------------------------------------
445 if (CLR_CMAKE_PLATFORM_UNIX)
446 # Disable frame pointer optimizations so profilers can get better call stacks
447 add_compile_options(-fno-omit-frame-pointer)
449 # The -fms-extensions enable the stuff like __if_exists, __declspec(uuid()), etc.
450 add_compile_options(-fms-extensions )
451 #-fms-compatibility Enable full Microsoft Visual C++ compatibility
452 #-fms-extensions Accept some non-standard constructs supported by the Microsoft compiler
454 # Make signed arithmetic overflow of addition, subtraction, and multiplication wrap around
455 # using twos-complement representation (this is normally undefined according to the C++ spec).
456 add_compile_options(-fwrapv)
458 if(CLR_CMAKE_PLATFORM_DARWIN)
459 # We cannot enable "stack-protector-strong" on OS X due to a bug in clang compiler (current version 7.0.2)
460 add_compile_options(-fstack-protector)
462 add_compile_options(-fstack-protector-strong)
463 endif(CLR_CMAKE_PLATFORM_DARWIN)
465 # Contracts are disabled on UNIX.
466 add_definitions(-DDISABLE_CONTRACTS)
468 if (CLR_CMAKE_WARNINGS_ARE_ERRORS)
469 # All warnings that are not explicitly disabled are reported as errors
470 add_compile_options(-Werror)
471 endif(CLR_CMAKE_WARNINGS_ARE_ERRORS)
473 # Disabled common warnings
474 add_compile_options(-Wno-unused-variable)
475 add_compile_options(-Wno-unused-value)
476 add_compile_options(-Wno-unused-function)
478 #These seem to indicate real issues
479 add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-Wno-invalid-offsetof>)
481 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
482 # The -ferror-limit is helpful during the porting, it makes sure the compiler doesn't stop
483 # after hitting just about 20 errors.
484 add_compile_options(-ferror-limit=4096)
487 add_compile_options(-Wno-unused-private-field)
488 # Explicit constructor calls are not supported by clang (this->ClassName::ClassName())
489 add_compile_options(-Wno-microsoft)
490 # This warning is caused by comparing 'this' to NULL
491 add_compile_options(-Wno-tautological-compare)
492 # There are constants of type BOOL used in a condition. But BOOL is defined as int
493 # and so the compiler thinks that there is a mistake.
494 add_compile_options(-Wno-constant-logical-operand)
495 # We use pshpack1/2/4/8.h and poppack.h headers to set and restore packing. However
496 # clang 6.0 complains when the packing change lifetime is not contained within
498 add_compile_options(-Wno-pragma-pack)
500 add_compile_options(-Wno-unknown-warning-option)
502 # The following warning indicates that an attribute __attribute__((__ms_struct__)) was applied
503 # to a struct or a class that has virtual members or a base class. In that case, clang
504 # may not generate the same object layout as MSVC.
505 add_compile_options(-Wno-incompatible-ms-struct)
507 add_compile_options(-Wno-unused-variable)
508 add_compile_options(-Wno-unused-but-set-variable)
509 add_compile_options(-fms-extensions)
510 add_compile_options(-Wno-unknown-pragmas)
511 if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 7.0)
512 add_compile_options(-Wno-nonnull-compare)
514 if (COMPILER_SUPPORTS_F_ALIGNED_NEW)
515 add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-faligned-new>)
519 # Some architectures (e.g., ARM) assume char type is unsigned while CoreCLR assumes char is signed
520 # as x64 does. It has been causing issues in ARM (https://github.com/dotnet/coreclr/issues/4746)
521 add_compile_options(-fsigned-char)
523 # We mark the function which needs exporting with DLLEXPORT
524 add_compile_options(-fvisibility=hidden)
526 # Specify the minimum supported version of macOS
527 if(CLR_CMAKE_PLATFORM_DARWIN)
528 set(MACOS_VERSION_MIN_FLAGS "-mmacosx-version-min=10.12")
529 add_compile_options("${MACOS_VERSION_MIN_FLAGS}")
530 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${MACOS_VERSION_MIN_FLAGS}")
531 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${MACOS_VERSION_MIN_FLAGS}")
532 endif(CLR_CMAKE_PLATFORM_DARWIN)
533 endif(CLR_CMAKE_PLATFORM_UNIX)
535 if(CLR_CMAKE_PLATFORM_UNIX_ARM)
536 # Because we don't use CMAKE_C_COMPILER/CMAKE_CXX_COMPILER to use clang
537 # we have to set the triple by adding a compiler argument
538 add_compile_options(-mthumb)
539 add_compile_options(-mfpu=vfpv3)
540 add_compile_options(-march=armv7-a)
542 add_definitions(-DARM_SOFTFP)
543 add_compile_options(-mfloat-abi=softfp)
545 endif(CLR_CMAKE_PLATFORM_UNIX_ARM)
547 if(CLR_CMAKE_PLATFORM_UNIX)
548 add_compile_options(${CLR_ADDITIONAL_COMPILER_OPTIONS})
549 endif(CLR_CMAKE_PLATFORM_UNIX)
552 # Compile options for targeting windows
554 # The following options are set by the razzle build
555 add_compile_options(/TP) # compile all files as C++
556 add_compile_options(/d2Zi+) # make optimized builds debugging easier
557 add_compile_options(/nologo) # Suppress Startup Banner
558 add_compile_options(/W3) # set warning level to 3
559 add_compile_options(/WX) # treat warnings as errors
560 add_compile_options(/Oi) # enable intrinsics
561 add_compile_options(/Oy-) # disable suppressing of the creation of frame pointers on the call stack for quicker function calls
562 add_compile_options(/U_MT) # undefine the predefined _MT macro
563 add_compile_options(/GF) # enable read-only string pooling
564 add_compile_options(/Gm-) # disable minimal rebuild
565 add_compile_options(/EHa) # enable C++ EH (w/ SEH exceptions)
566 add_compile_options(/Zp8) # pack structs on 8-byte boundary
567 add_compile_options(/Gy) # separate functions for linker
568 add_compile_options(/Zc:wchar_t-) # C++ language conformance: wchar_t is NOT the native type, but a typedef
569 add_compile_options(/Zc:forScope) # C++ language conformance: enforce Standard C++ for scoping rules
570 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /GR-") # disable C++ RTTI
571 add_compile_options(/FC) # use full pathnames in diagnostics
572 add_compile_options(/MP) # Build with Multiple Processes (number of processes equal to the number of processors)
573 add_compile_options(/GS) # Buffer Security Check
574 add_compile_options(/Zm200) # Specify Precompiled Header Memory Allocation Limit of 150MB
575 add_compile_options(/wd4960 /wd4961 /wd4603 /wd4627 /wd4838 /wd4456 /wd4457 /wd4458 /wd4459 /wd4091 /we4640)
576 add_compile_options(/Zi) # enable debugging information
577 add_compile_options(/ZH:SHA_256) # use SHA256 for generating hashes of compiler processed source files.
578 add_compile_options(/source-charset:utf-8) # Force MSVC to compile source as UTF-8.
580 if (CLR_CMAKE_PLATFORM_ARCH_I386)
581 add_compile_options(/Gz)
582 endif (CLR_CMAKE_PLATFORM_ARCH_I386)
584 add_compile_options($<$<OR:$<CONFIG:Release>,$<CONFIG:Relwithdebinfo>>:/GL>)
585 add_compile_options($<$<OR:$<OR:$<CONFIG:Release>,$<CONFIG:Relwithdebinfo>>,$<CONFIG:Checked>>:/O1>)
587 if (CLR_CMAKE_PLATFORM_ARCH_AMD64)
588 # The generator expression in the following command means that the /homeparams option is added only for debug builds
589 add_compile_options($<$<CONFIG:Debug>:/homeparams>) # Force parameters passed in registers to be written to the stack
590 endif (CLR_CMAKE_PLATFORM_ARCH_AMD64)
592 # enable control-flow-guard support for native components for non-Arm64 builds
593 # Added using variables instead of add_compile_options to let individual projects override it
594 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /guard:cf")
595 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /guard:cf")
597 # Statically linked CRT (libcmt[d].lib, libvcruntime[d].lib and libucrt[d].lib) by default. This is done to avoid
598 # linking in VCRUNTIME140.DLL for a simplified xcopy experience by reducing the dependency on VC REDIST.
600 # For Release builds, we shall dynamically link into uCRT [ucrtbase.dll] (which is pushed down as a Windows Update on downlevel OS) but
601 # wont do the same for debug/checked builds since ucrtbased.dll is not redistributable and Debug/Checked builds are not
602 # production-time scenarios.
603 add_compile_options($<$<OR:$<CONFIG:Release>,$<CONFIG:Relwithdebinfo>>:/MT>)
604 add_compile_options($<$<OR:$<CONFIG:Debug>,$<CONFIG:Checked>>:/MTd>)
606 set(CMAKE_ASM_MASM_FLAGS "${CMAKE_ASM_MASM_FLAGS} /ZH:SHA_256")
608 if (CLR_CMAKE_TARGET_ARCH_ARM OR CLR_CMAKE_TARGET_ARCH_ARM64)
609 # Contracts work too slow on ARM/ARM64 DEBUG/CHECKED.
610 add_definitions(-DDISABLE_CONTRACTS)
611 endif (CLR_CMAKE_TARGET_ARCH_ARM OR CLR_CMAKE_TARGET_ARCH_ARM64)
615 if(CLR_CMAKE_ENABLE_CODE_COVERAGE)
617 if(CLR_CMAKE_PLATFORM_UNIX)
618 string(TOUPPER ${CMAKE_BUILD_TYPE} UPPERCASE_CMAKE_BUILD_TYPE)
619 if(NOT UPPERCASE_CMAKE_BUILD_TYPE STREQUAL DEBUG)
620 message( WARNING "Code coverage results with an optimised (non-Debug) build may be misleading" )
621 endif(NOT UPPERCASE_CMAKE_BUILD_TYPE STREQUAL DEBUG)
623 add_compile_options(-fprofile-arcs)
624 add_compile_options(-ftest-coverage)
625 set(CLANG_COVERAGE_LINK_FLAGS "--coverage")
626 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${CLANG_COVERAGE_LINK_FLAGS}")
627 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${CLANG_COVERAGE_LINK_FLAGS}")
629 message(FATAL_ERROR "Code coverage builds not supported on current platform")
630 endif(CLR_CMAKE_PLATFORM_UNIX)
632 endif(CLR_CMAKE_ENABLE_CODE_COVERAGE)