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