1 #----------------------------------------
2 # Detect and set platform variable names
3 # - for non-windows build platform & architecture is detected using inbuilt CMAKE variables and cross target component configure
4 # - for windows we use the passed in parameter to CMAKE to determine build arch
5 #----------------------------------------
6 if(CMAKE_SYSTEM_NAME STREQUAL Linux)
7 set(CLR_CMAKE_PLATFORM_UNIX 1)
8 if(CLR_CROSS_COMPONENTS_BUILD)
9 # CMAKE_HOST_SYSTEM_PROCESSOR returns the value of `uname -p` on host.
10 if(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL x86_64 OR CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL amd64)
11 if(CLR_CMAKE_TARGET_ARCH STREQUAL "arm")
12 set(CLR_CMAKE_PLATFORM_UNIX_X86 1)
14 set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
16 elseif(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL i686)
17 set(CLR_CMAKE_PLATFORM_UNIX_X86 1)
22 # CMAKE_SYSTEM_PROCESSOR returns the value of `uname -p` on target.
23 # For the AMD/Intel 64bit architecture two different strings are common.
24 # Linux and Darwin identify it as "x86_64" while FreeBSD and netbsd uses the
25 # "amd64" string. Accept either of the two here.
26 if(CMAKE_SYSTEM_PROCESSOR STREQUAL x86_64 OR CMAKE_SYSTEM_PROCESSOR STREQUAL amd64)
27 set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
28 elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL armv7l)
29 set(CLR_CMAKE_PLATFORM_UNIX_ARM 1)
30 elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL arm)
31 set(CLR_CMAKE_PLATFORM_UNIX_ARM 1)
32 elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL aarch64)
33 set(CLR_CMAKE_PLATFORM_UNIX_ARM64 1)
34 elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL i686)
35 set(CLR_CMAKE_PLATFORM_UNIX_X86 1)
40 set(CLR_CMAKE_PLATFORM_LINUX 1)
43 if(DEFINED CLR_CMAKE_LINUX_ID)
44 if(CLR_CMAKE_LINUX_ID STREQUAL ubuntu)
45 set(CLR_CMAKE_TARGET_UBUNTU_LINUX 1)
46 elseif(CLR_CMAKE_LINUX_ID STREQUAL tizen)
47 set(CLR_CMAKE_TARGET_TIZEN_LINUX 1)
48 elseif(CLR_CMAKE_LINUX_ID STREQUAL alpine)
49 set(CLR_CMAKE_PLATFORM_ALPINE_LINUX 1)
51 if(CLR_CMAKE_LINUX_ID STREQUAL ubuntu)
52 set(CLR_CMAKE_PLATFORM_UBUNTU_LINUX 1)
54 endif(DEFINED CLR_CMAKE_LINUX_ID)
55 endif(CMAKE_SYSTEM_NAME STREQUAL Linux)
57 if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
58 set(CLR_CMAKE_PLATFORM_UNIX 1)
59 set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
60 set(CLR_CMAKE_PLATFORM_DARWIN 1)
61 if(CMAKE_VERSION VERSION_LESS "3.4.0")
62 set(CMAKE_ASM_COMPILE_OBJECT "${CMAKE_C_COMPILER} <FLAGS> <DEFINES> -o <OBJECT> -c <SOURCE>")
64 set(CMAKE_ASM_COMPILE_OBJECT "${CMAKE_C_COMPILER} <FLAGS> <DEFINES> <INCLUDES> -o <OBJECT> -c <SOURCE>")
65 endif(CMAKE_VERSION VERSION_LESS "3.4.0")
66 endif(CMAKE_SYSTEM_NAME STREQUAL Darwin)
68 if(CMAKE_SYSTEM_NAME STREQUAL FreeBSD)
69 set(CLR_CMAKE_PLATFORM_UNIX 1)
70 set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
71 set(CLR_CMAKE_PLATFORM_FREEBSD 1)
72 endif(CMAKE_SYSTEM_NAME STREQUAL FreeBSD)
74 if(CMAKE_SYSTEM_NAME STREQUAL OpenBSD)
75 set(CLR_CMAKE_PLATFORM_UNIX 1)
76 set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
77 set(CLR_CMAKE_PLATFORM_OPENBSD 1)
78 endif(CMAKE_SYSTEM_NAME STREQUAL OpenBSD)
80 if(CMAKE_SYSTEM_NAME STREQUAL NetBSD)
81 set(CLR_CMAKE_PLATFORM_UNIX 1)
82 set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
83 set(CLR_CMAKE_PLATFORM_NETBSD 1)
84 endif(CMAKE_SYSTEM_NAME STREQUAL NetBSD)
86 if(CMAKE_SYSTEM_NAME STREQUAL SunOS)
87 set(CLR_CMAKE_PLATFORM_UNIX 1)
90 OUTPUT_VARIABLE SUNOS_NATIVE_INSTRUCTION_SET
92 if(SUNOS_NATIVE_INSTRUCTION_SET MATCHES "amd64")
93 set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
94 set(CMAKE_SYSTEM_PROCESSOR "amd64")
98 set(CLR_CMAKE_PLATFORM_SUNOS 1)
99 endif(CMAKE_SYSTEM_NAME STREQUAL SunOS)
101 #--------------------------------------------
102 # This repo builds two set of binaries
103 # 1. binaries which execute on target arch machine
104 # - for such binaries host architecture & target architecture are same
106 # 2. binaries which execute on host machine but target another architecture
107 # - host architecture is different from target architecture
108 # - eg. crossgen.exe - runs on x64 machine and generates nis targeting arm64
109 # - for complete list of such binaries refer to file crosscomponents.cmake
110 #-------------------------------------------------------------
111 # Set HOST architecture variables
112 if(CLR_CMAKE_PLATFORM_UNIX_ARM)
113 set(CLR_CMAKE_PLATFORM_ARCH_ARM 1)
114 set(CLR_CMAKE_HOST_ARCH "arm")
115 elseif(CLR_CMAKE_PLATFORM_UNIX_ARM64)
116 set(CLR_CMAKE_PLATFORM_ARCH_ARM64 1)
117 set(CLR_CMAKE_HOST_ARCH "arm64")
118 elseif(CLR_CMAKE_PLATFORM_UNIX_AMD64)
119 set(CLR_CMAKE_PLATFORM_ARCH_AMD64 1)
120 set(CLR_CMAKE_HOST_ARCH "x64")
121 elseif(CLR_CMAKE_PLATFORM_UNIX_X86)
122 set(CLR_CMAKE_PLATFORM_ARCH_I386 1)
123 set(CLR_CMAKE_HOST_ARCH "x86")
125 # CLR_CMAKE_HOST_ARCH is passed in as param to cmake
126 if (CLR_CMAKE_HOST_ARCH STREQUAL x64)
127 set(CLR_CMAKE_PLATFORM_ARCH_AMD64 1)
128 elseif(CLR_CMAKE_HOST_ARCH STREQUAL x86)
129 set(CLR_CMAKE_PLATFORM_ARCH_I386 1)
130 elseif(CLR_CMAKE_HOST_ARCH STREQUAL arm)
131 set(CLR_CMAKE_PLATFORM_ARCH_ARM 1)
132 elseif(CLR_CMAKE_HOST_ARCH STREQUAL arm64)
133 set(CLR_CMAKE_PLATFORM_ARCH_ARM64 1)
139 # Set TARGET architecture variables
140 # Target arch will be a cmake param (optional) for both windows as well as non-windows build
141 # if target arch is not specified then host & target are same
142 if(NOT DEFINED CLR_CMAKE_TARGET_ARCH OR CLR_CMAKE_TARGET_ARCH STREQUAL "" )
143 set(CLR_CMAKE_TARGET_ARCH ${CLR_CMAKE_HOST_ARCH})
146 # Set target architecture variables
147 if (CLR_CMAKE_TARGET_ARCH STREQUAL x64)
148 set(CLR_CMAKE_TARGET_ARCH_AMD64 1)
149 elseif(CLR_CMAKE_TARGET_ARCH STREQUAL x86)
150 set(CLR_CMAKE_TARGET_ARCH_I386 1)
151 elseif(CLR_CMAKE_TARGET_ARCH STREQUAL arm64)
152 set(CLR_CMAKE_TARGET_ARCH_ARM64 1)
153 elseif(CLR_CMAKE_TARGET_ARCH STREQUAL arm)
154 set(CLR_CMAKE_TARGET_ARCH_ARM 1)
159 # check if host & target arch combination are valid
160 if(NOT(CLR_CMAKE_TARGET_ARCH STREQUAL CLR_CMAKE_HOST_ARCH))
161 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)))
162 message(FATAL_ERROR "Invalid host and target arch combination")
166 #-----------------------------------------------------
167 # Initialize Cmake compiler flags and other variables
168 #-----------------------------------------------------
170 if (CMAKE_CONFIGURATION_TYPES) # multi-configuration generator?
171 set(CMAKE_CONFIGURATION_TYPES "Debug;Checked;Release;RelWithDebInfo" CACHE STRING "" FORCE)
172 endif (CMAKE_CONFIGURATION_TYPES)
174 set(CMAKE_C_FLAGS_CHECKED ${CLR_C_FLAGS_CHECKED_INIT} CACHE STRING "Flags used by the compiler during checked builds.")
175 set(CMAKE_CXX_FLAGS_CHECKED ${CLR_CXX_FLAGS_CHECKED_INIT} CACHE STRING "Flags used by the compiler during checked builds.")
176 set(CMAKE_EXE_LINKER_FLAGS_CHECKED "")
177 set(CMAKE_SHARED_LINKER_FLAGS_CHECKED "")
179 set(CMAKE_CXX_STANDARD_LIBRARIES "") # do not link against standard win32 libs i.e. kernel32, uuid, user32, etc.
182 # For multi-configuration toolset (as Visual Studio)
183 # set the different configuration defines.
184 foreach (Config DEBUG CHECKED RELEASE RELWITHDEBINFO)
185 foreach (Definition IN LISTS CLR_DEFINES_${Config}_INIT)
186 set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS $<$<CONFIG:${Config}>:${Definition}>)
187 endforeach (Definition)
190 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /GUARD:CF")
191 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /GUARD:CF")
195 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /MANIFEST:NO") #Do not create Side-by-Side Assembly Manifest
197 if (CLR_CMAKE_PLATFORM_ARCH_ARM)
198 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /SUBSYSTEM:WINDOWS,6.02") #windows subsystem - arm minimum is 6.02
199 elseif(CLR_CMAKE_PLATFORM_ARCH_ARM64)
200 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /SUBSYSTEM:WINDOWS,6.03") #windows subsystem - arm64 minimum is 6.03
202 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /SUBSYSTEM:WINDOWS,6.01") #windows subsystem
205 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /LARGEADDRESSAWARE") # can handle addresses larger than 2 gigabytes
206 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /NXCOMPAT") #Compatible with Data Execution Prevention
207 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /DYNAMICBASE") #Use address space layout randomization
208 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /PDBCOMPRESS") #shrink pdb size
209 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /DEBUG")
210 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /IGNORE:4197,4013,4254,4070,4221")
212 set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} /IGNORE:4221")
214 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /DEBUG /PDBCOMPRESS")
215 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /STACK:1572864")
217 # Temporarily disable incremental link due to incremental linking CFG bug crashing crossgen.
218 # See https://github.com/dotnet/coreclr/issues/12592
219 # This has been fixed in VS 2017 Update 5 but we're keeping this around until everyone is off
220 # the versions that have the bug. The bug manifests itself as a bad crash.
221 set(NO_INCREMENTAL_LINKER_FLAGS "/INCREMENTAL:NO")
223 # Debug build specific flags
224 set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "/NOVCFEATURE ${NO_INCREMENTAL_LINKER_FLAGS}")
225 set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${NO_INCREMENTAL_LINKER_FLAGS}")
227 # Checked build specific flags
228 set(CMAKE_SHARED_LINKER_FLAGS_CHECKED "${CMAKE_SHARED_LINKER_FLAGS_CHECKED} /OPT:REF /OPT:NOICF /NOVCFEATURE ${NO_INCREMENTAL_LINKER_FLAGS}")
229 set(CMAKE_STATIC_LINKER_FLAGS_CHECKED "${CMAKE_STATIC_LINKER_FLAGS_CHECKED}")
230 set(CMAKE_EXE_LINKER_FLAGS_CHECKED "${CMAKE_EXE_LINKER_FLAGS_CHECKED} /OPT:REF /OPT:NOICF ${NO_INCREMENTAL_LINKER_FLAGS}")
232 # Release build specific flags
233 set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /LTCG /OPT:REF /OPT:ICF ${NO_INCREMENTAL_LINKER_FLAGS}")
234 set(CMAKE_STATIC_LINKER_FLAGS_RELEASE "${CMAKE_STATIC_LINKER_FLAGS_RELEASE} /LTCG")
235 set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /LTCG /OPT:REF /OPT:ICF ${NO_INCREMENTAL_LINKER_FLAGS}")
237 # ReleaseWithDebugInfo build specific flags
238 set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO} /LTCG /OPT:REF /OPT:ICF ${NO_INCREMENTAL_LINKER_FLAGS}")
239 set(CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO} /LTCG")
240 set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} /LTCG /OPT:REF /OPT:ICF ${NO_INCREMENTAL_LINKER_FLAGS}")
242 # Force uCRT to be dynamically linked for Release build
243 set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /NODEFAULTLIB:libucrt.lib /DEFAULTLIB:ucrt.lib")
244 set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /NODEFAULTLIB:libucrt.lib /DEFAULTLIB:ucrt.lib")
245 set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO} /NODEFAULTLIB:libucrt.lib /DEFAULTLIB:ucrt.lib")
246 set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} /NODEFAULTLIB:libucrt.lib /DEFAULTLIB:ucrt.lib")
248 elseif (CLR_CMAKE_PLATFORM_UNIX)
249 # Set the values to display when interactively configuring CMAKE_BUILD_TYPE
250 set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "DEBUG;CHECKED;RELEASE;RELWITHDEBINFO")
252 # Use uppercase CMAKE_BUILD_TYPE for the string comparisons below
253 string(TOUPPER ${CMAKE_BUILD_TYPE} UPPERCASE_CMAKE_BUILD_TYPE)
255 # For single-configuration toolset
256 # set the different configuration defines.
257 if (UPPERCASE_CMAKE_BUILD_TYPE STREQUAL DEBUG)
259 set_property(DIRECTORY PROPERTY COMPILE_DEFINITIONS ${CLR_DEFINES_DEBUG_INIT})
260 elseif (UPPERCASE_CMAKE_BUILD_TYPE STREQUAL CHECKED)
262 set_property(DIRECTORY PROPERTY COMPILE_DEFINITIONS ${CLR_DEFINES_CHECKED_INIT})
263 elseif (UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELEASE)
265 set_property(DIRECTORY PROPERTY COMPILE_DEFINITIONS ${CLR_DEFINES_RELEASE_INIT})
266 elseif (UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELWITHDEBINFO)
267 # And then RELWITHDEBINFO
268 set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS ${CLR_DEFINES_RELWITHDEBINFO_INIT})
270 message(FATAL_ERROR "Unknown build type! Set CMAKE_BUILD_TYPE to DEBUG, CHECKED, RELEASE, or RELWITHDEBINFO!")
273 # set the CLANG sanitizer flags for debug build
274 if(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL DEBUG OR UPPERCASE_CMAKE_BUILD_TYPE STREQUAL CHECKED)
275 # obtain settings from running enablesanitizers.sh
276 string(FIND "$ENV{DEBUG_SANITIZERS}" "asan" __ASAN_POS)
277 string(FIND "$ENV{DEBUG_SANITIZERS}" "ubsan" __UBSAN_POS)
278 if ((${__ASAN_POS} GREATER -1) OR (${__UBSAN_POS} GREATER -1))
279 set(CLR_SANITIZE_CXX_FLAGS "${CLR_SANITIZE_CXX_FLAGS} -fsanitize-blacklist=${CMAKE_CURRENT_SOURCE_DIR}/sanitizerblacklist.txt -fsanitize=")
280 set(CLR_SANITIZE_LINK_FLAGS "${CLR_SANITIZE_LINK_FLAGS} -fsanitize=")
281 if (${__ASAN_POS} GREATER -1)
282 set(CLR_SANITIZE_CXX_FLAGS "${CLR_SANITIZE_CXX_FLAGS}address,")
283 set(CLR_SANITIZE_LINK_FLAGS "${CLR_SANITIZE_LINK_FLAGS}address,")
284 add_definitions(-DHAS_ASAN)
285 message("Address Sanitizer (asan) enabled")
287 if (${__UBSAN_POS} GREATER -1)
288 # all sanitizier flags are enabled except alignment (due to heavy use of __unaligned modifier)
289 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")
290 set(CLR_SANITIZE_LINK_FLAGS "${CLR_SANITIZE_LINK_FLAGS}undefined")
291 message("Undefined Behavior Sanitizer (ubsan) enabled")
294 # -fdata-sections -ffunction-sections: each function has own section instead of one per .o file (needed for --gc-sections)
295 # -fPIC: enable Position Independent Code normally just for shared libraries but required when linking with address sanitizer
296 # -O1: optimization level used instead of -O0 to avoid compile error "invalid operand for inline asm constraint"
297 set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${CLR_SANITIZE_CXX_FLAGS} -fdata-sections -ffunction-sections -fPIC -O1")
298 set(CMAKE_CXX_FLAGS_CHECKED "${CMAKE_CXX_FLAGS_CHECKED} ${CLR_SANITIZE_CXX_FLAGS} -fdata-sections -ffunction-sections -fPIC -O1")
300 set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} ${CLR_SANITIZE_LINK_FLAGS}")
301 set(CMAKE_EXE_LINKER_FLAGS_CHECKED "${CMAKE_EXE_LINKER_FLAGS_CHECKED} ${CLR_SANITIZE_LINK_FLAGS}")
303 # -Wl and --gc-sections: drop unused sections\functions (similar to Windows /Gy function-level-linking)
304 set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} ${CLR_SANITIZE_LINK_FLAGS} -Wl,--gc-sections")
305 set(CMAKE_SHARED_LINKER_FLAGS_CHECKED "${CMAKE_SHARED_LINKER_FLAGS_CHECKED} ${CLR_SANITIZE_LINK_FLAGS} -Wl,--gc-sections")
307 endif(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL DEBUG OR UPPERCASE_CMAKE_BUILD_TYPE STREQUAL CHECKED)
310 # CLR_ADDITIONAL_LINKER_FLAGS - used for passing additional arguments to linker
311 # CLR_ADDITIONAL_COMPILER_OPTIONS - used for passing additional arguments to compiler
314 # ./build-native.sh cmakeargs "-DCLR_ADDITIONAL_COMPILER_OPTIONS=<...>" cmakeargs "-DCLR_ADDITIONAL_LINKER_FLAGS=<...>"
316 if(CLR_CMAKE_PLATFORM_UNIX)
317 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${CLR_ADDITIONAL_LINKER_FLAGS}")
318 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${CLR_ADDITIONAL_LINKER_FLAGS}" )
319 add_compile_options(${CLR_ADDITIONAL_COMPILER_OPTIONS})
320 endif(CLR_CMAKE_PLATFORM_UNIX)
322 if(CLR_CMAKE_PLATFORM_LINUX)
323 set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -Wa,--noexecstack")
324 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--build-id=sha1")
325 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--build-id=sha1")
326 endif(CLR_CMAKE_PLATFORM_LINUX)
328 #------------------------------------
329 # Definitions (for platform)
330 #-----------------------------------
331 if (CLR_CMAKE_PLATFORM_ARCH_AMD64)
332 add_definitions(-D_AMD64_)
333 add_definitions(-D_WIN64)
334 add_definitions(-DAMD64)
335 add_definitions(-DBIT64=1)
336 elseif (CLR_CMAKE_PLATFORM_ARCH_I386)
337 add_definitions(-D_X86_)
338 elseif (CLR_CMAKE_PLATFORM_ARCH_ARM)
339 add_definitions(-D_ARM_)
340 add_definitions(-DARM)
341 elseif (CLR_CMAKE_PLATFORM_ARCH_ARM64)
342 add_definitions(-D_ARM64_)
343 add_definitions(-DARM64)
344 add_definitions(-D_WIN64)
345 add_definitions(-DBIT64=1)
350 if (CLR_CMAKE_PLATFORM_UNIX)
351 if(CLR_CMAKE_PLATFORM_LINUX)
352 if(CLR_CMAKE_PLATFORM_UNIX_AMD64)
353 message("Detected Linux x86_64")
354 add_definitions(-DLINUX64)
355 elseif(CLR_CMAKE_PLATFORM_UNIX_ARM)
356 message("Detected Linux ARM")
357 add_definitions(-DLINUX32)
358 elseif(CLR_CMAKE_PLATFORM_UNIX_ARM64)
359 message("Detected Linux ARM64")
360 add_definitions(-DLINUX64)
361 elseif(CLR_CMAKE_PLATFORM_UNIX_X86)
362 message("Detected Linux i686")
363 add_definitions(-DLINUX32)
367 endif(CLR_CMAKE_PLATFORM_LINUX)
368 endif(CLR_CMAKE_PLATFORM_UNIX)
370 if (CLR_CMAKE_PLATFORM_UNIX)
371 add_definitions(-DPLATFORM_UNIX=1)
373 if(CLR_CMAKE_PLATFORM_DARWIN)
374 message("Detected OSX x86_64")
375 endif(CLR_CMAKE_PLATFORM_DARWIN)
377 if(CLR_CMAKE_PLATFORM_FREEBSD)
378 message("Detected FreeBSD amd64")
379 endif(CLR_CMAKE_PLATFORM_FREEBSD)
381 if(CLR_CMAKE_PLATFORM_NETBSD)
382 message("Detected NetBSD amd64")
383 endif(CLR_CMAKE_PLATFORM_NETBSD)
384 endif(CLR_CMAKE_PLATFORM_UNIX)
387 # Define the CRT lib references that link into Desktop imports
388 set(STATIC_MT_CRT_LIB "libcmt$<$<OR:$<CONFIG:Debug>,$<CONFIG:Checked>>:d>.lib")
389 set(STATIC_MT_VCRT_LIB "libvcruntime$<$<OR:$<CONFIG:Debug>,$<CONFIG:Checked>>:d>.lib")
390 set(STATIC_MT_CPP_LIB "libcpmt$<$<OR:$<CONFIG:Debug>,$<CONFIG:Checked>>:d>.lib")
393 # Architecture specific files folder name
394 if (CLR_CMAKE_TARGET_ARCH_AMD64)
395 set(ARCH_SOURCES_DIR amd64)
396 elseif (CLR_CMAKE_TARGET_ARCH_ARM64)
397 set(ARCH_SOURCES_DIR arm64)
398 elseif (CLR_CMAKE_TARGET_ARCH_ARM)
399 set(ARCH_SOURCES_DIR arm)
400 elseif (CLR_CMAKE_TARGET_ARCH_I386)
401 set(ARCH_SOURCES_DIR i386)
406 #--------------------------------------
408 #--------------------------------------
409 if (CLR_CMAKE_PLATFORM_UNIX)
410 # Disable frame pointer optimizations so profilers can get better call stacks
411 add_compile_options(-fno-omit-frame-pointer)
413 # The -fms-extensions enable the stuff like __if_exists, __declspec(uuid()), etc.
414 add_compile_options(-fms-extensions )
415 #-fms-compatibility Enable full Microsoft Visual C++ compatibility
416 #-fms-extensions Accept some non-standard constructs supported by the Microsoft compiler
418 # Make signed arithmetic overflow of addition, subtraction, and multiplication wrap around
419 # using twos-complement representation (this is normally undefined according to the C++ spec).
420 add_compile_options(-fwrapv)
422 if(CLR_CMAKE_PLATFORM_DARWIN)
423 # We cannot enable "stack-protector-strong" on OS X due to a bug in clang compiler (current version 7.0.2)
424 add_compile_options(-fstack-protector)
426 add_compile_options(-fstack-protector-strong)
427 endif(CLR_CMAKE_PLATFORM_DARWIN)
429 add_definitions(-DDISABLE_CONTRACTS)
430 # The -ferror-limit is helpful during the porting, it makes sure the compiler doesn't stop
431 # after hitting just about 20 errors.
432 add_compile_options(-ferror-limit=4096)
434 if (CLR_CMAKE_WARNINGS_ARE_ERRORS)
435 # All warnings that are not explicitly disabled are reported as errors
436 add_compile_options(-Werror)
437 endif(CLR_CMAKE_WARNINGS_ARE_ERRORS)
440 add_compile_options(-Wno-unused-private-field)
441 add_compile_options(-Wno-unused-variable)
442 # Explicit constructor calls are not supported by clang (this->ClassName::ClassName())
443 add_compile_options(-Wno-microsoft)
444 # This warning is caused by comparing 'this' to NULL
445 add_compile_options(-Wno-tautological-compare)
446 # There are constants of type BOOL used in a condition. But BOOL is defined as int
447 # and so the compiler thinks that there is a mistake.
448 add_compile_options(-Wno-constant-logical-operand)
449 # We use pshpack1/2/4/8.h and poppack.h headers to set and restore packing. However
450 # clang 6.0 complains when the packing change lifetime is not contained within
452 add_compile_options(-Wno-pragma-pack)
454 add_compile_options(-Wno-unknown-warning-option)
456 #These seem to indicate real issues
457 add_compile_options(-Wno-invalid-offsetof)
458 # The following warning indicates that an attribute __attribute__((__ms_struct__)) was applied
459 # to a struct or a class that has virtual members or a base class. In that case, clang
460 # may not generate the same object layout as MSVC.
461 add_compile_options(-Wno-incompatible-ms-struct)
463 # Some architectures (e.g., ARM) assume char type is unsigned while CoreCLR assumes char is signed
464 # as x64 does. It has been causing issues in ARM (https://github.com/dotnet/coreclr/issues/4746)
465 add_compile_options(-fsigned-char)
466 endif(CLR_CMAKE_PLATFORM_UNIX)
468 if(CLR_CMAKE_PLATFORM_UNIX_ARM)
469 # Because we don't use CMAKE_C_COMPILER/CMAKE_CXX_COMPILER to use clang
470 # we have to set the triple by adding a compiler argument
471 add_compile_options(-mthumb)
472 add_compile_options(-mfpu=vfpv3)
473 add_compile_options(-march=armv7-a)
475 add_definitions(-DARM_SOFTFP)
476 add_compile_options(-mfloat-abi=softfp)
478 endif(CLR_CMAKE_PLATFORM_UNIX_ARM)
481 # Compile options for targeting windows
483 # The following options are set by the razzle build
484 add_compile_options(/TP) # compile all files as C++
485 add_compile_options(/d2Zi+) # make optimized builds debugging easier
486 add_compile_options(/nologo) # Suppress Startup Banner
487 add_compile_options(/W3) # set warning level to 3
488 add_compile_options(/WX) # treat warnings as errors
489 add_compile_options(/Oi) # enable intrinsics
490 add_compile_options(/Oy-) # disable suppressing of the creation of frame pointers on the call stack for quicker function calls
491 add_compile_options(/U_MT) # undefine the predefined _MT macro
492 add_compile_options(/GF) # enable read-only string pooling
493 add_compile_options(/Gm-) # disable minimal rebuild
494 add_compile_options(/EHa) # enable C++ EH (w/ SEH exceptions)
495 add_compile_options(/Zp8) # pack structs on 8-byte boundary
496 add_compile_options(/Gy) # separate functions for linker
497 add_compile_options(/Zc:wchar_t-) # C++ language conformance: wchar_t is NOT the native type, but a typedef
498 add_compile_options(/Zc:forScope) # C++ language conformance: enforce Standard C++ for scoping rules
499 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /GR-") # disable C++ RTTI
500 add_compile_options(/FC) # use full pathnames in diagnostics
501 add_compile_options(/MP) # Build with Multiple Processes (number of processes equal to the number of processors)
502 add_compile_options(/GS) # Buffer Security Check
503 add_compile_options(/Zm200) # Specify Precompiled Header Memory Allocation Limit of 150MB
504 add_compile_options(/wd4960 /wd4961 /wd4603 /wd4627 /wd4838 /wd4456 /wd4457 /wd4458 /wd4459 /wd4091 /we4640)
505 add_compile_options(/Zi) # enable debugging information
506 add_compile_options(/ZH:SHA_256) # use SHA256 for generating hashes of compiler processed source files.
507 add_compile_options(/source-charset:utf-8) # Force MSVC to compile source as UTF-8.
509 if (CLR_CMAKE_PLATFORM_ARCH_I386)
510 add_compile_options(/Gz)
511 endif (CLR_CMAKE_PLATFORM_ARCH_I386)
513 add_compile_options($<$<OR:$<CONFIG:Release>,$<CONFIG:Relwithdebinfo>>:/GL>)
514 add_compile_options($<$<OR:$<OR:$<CONFIG:Release>,$<CONFIG:Relwithdebinfo>>,$<CONFIG:Checked>>:/O1>)
516 if (CLR_CMAKE_PLATFORM_ARCH_AMD64)
517 # The generator expression in the following command means that the /homeparams option is added only for debug builds
518 add_compile_options($<$<CONFIG:Debug>:/homeparams>) # Force parameters passed in registers to be written to the stack
519 endif (CLR_CMAKE_PLATFORM_ARCH_AMD64)
521 # enable control-flow-guard support for native components for non-Arm64 builds
522 # Added using variables instead of add_compile_options to let individual projects override it
523 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /guard:cf")
524 set(CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS} /guard:cf")
526 # Statically linked CRT (libcmt[d].lib, libvcruntime[d].lib and libucrt[d].lib) by default. This is done to avoid
527 # linking in VCRUNTIME140.DLL for a simplified xcopy experience by reducing the dependency on VC REDIST.
529 # For Release builds, we shall dynamically link into uCRT [ucrtbase.dll] (which is pushed down as a Windows Update on downlevel OS) but
530 # wont do the same for debug/checked builds since ucrtbased.dll is not redistributable and Debug/Checked builds are not
531 # production-time scenarios.
532 add_compile_options($<$<OR:$<CONFIG:Release>,$<CONFIG:Relwithdebinfo>>:/MT>)
533 add_compile_options($<$<OR:$<CONFIG:Debug>,$<CONFIG:Checked>>:/MTd>)
535 set(CMAKE_ASM_MASM_FLAGS "${CMAKE_ASM_MASM_FLAGS} /ZH:SHA_256")
539 if(CLR_CMAKE_ENABLE_CODE_COVERAGE)
541 if(CLR_CMAKE_PLATFORM_UNIX)
542 string(TOUPPER ${CMAKE_BUILD_TYPE} UPPERCASE_CMAKE_BUILD_TYPE)
543 if(NOT UPPERCASE_CMAKE_BUILD_TYPE STREQUAL DEBUG)
544 message( WARNING "Code coverage results with an optimised (non-Debug) build may be misleading" )
545 endif(NOT UPPERCASE_CMAKE_BUILD_TYPE STREQUAL DEBUG)
547 add_compile_options(-fprofile-arcs)
548 add_compile_options(-ftest-coverage)
549 set(CLANG_COVERAGE_LINK_FLAGS "--coverage")
550 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${CLANG_COVERAGE_LINK_FLAGS}")
551 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${CLANG_COVERAGE_LINK_FLAGS}")
553 message(FATAL_ERROR "Code coverage builds not supported on current platform")
554 endif(CLR_CMAKE_PLATFORM_UNIX)
556 endif(CLR_CMAKE_ENABLE_CODE_COVERAGE)