1 # Verify minimum required version
2 cmake_minimum_required(VERSION 2.8.12)
4 if(CMAKE_VERSION VERSION_EQUAL 3.0 OR CMAKE_VERSION VERSION_GREATER 3.0)
5 cmake_policy(SET CMP0042 NEW)
11 # Include cmake functions
12 include(functions.cmake)
14 # Include global configure settings
15 include(configure.cmake)
18 message(STATUS "VS_PLATFORM_TOOLSET is ${CMAKE_VS_PLATFORM_TOOLSET}")
19 message(STATUS "VS_PLATFORM_NAME is ${CMAKE_VS_PLATFORM_NAME}")
22 # Set commonly used directory names
23 set(CLR_DIR ${CMAKE_CURRENT_SOURCE_DIR})
24 set(VM_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src/vm)
25 set(GENERATED_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/src/inc)
26 set(GENERATED_EVENTING_DIR ${CMAKE_CURRENT_BINARY_DIR}/eventing)
27 set(VERSION_FILE_PATH "${CMAKE_BINARY_DIR}/version.cpp")
29 set(CORECLR_SET_RPATH ON)
31 # Enable @rpath support for shared libraries.
33 endif(CORECLR_SET_RPATH)
35 OPTION(CLR_CMAKE_ENABLE_CODE_COVERAGE "Enable code coverage" OFF)
36 OPTION(CLR_CMAKE_WARNINGS_ARE_ERRORS "Warnings are errors" ON)
38 # Ensure that python is present
39 find_program(PYTHON NAMES python2.7 python2 python)
40 if (PYTHON STREQUAL "PYTHON-NOTFOUND")
41 message(FATAL_ERROR "PYTHON not found: Please install Python 2.7.9 or later from https://www.python.org/downloads/")
44 # Ensure other tools are present
46 if(CLR_CMAKE_HOST_ARCH STREQUAL arm)
48 # Confirm that Windows SDK is present
49 if(NOT DEFINED CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION OR CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION STREQUAL "" )
50 message(FATAL_ERROR "Windows SDK is required for the Arm32 build.")
52 message("Using Windows SDK version ${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}")
55 # Explicitly specify the assembler to be used for Arm32 compile
56 if($ENV{__VSVersion} STREQUAL "vs2015")
57 file(TO_CMAKE_PATH "$ENV{VCINSTALLDIR}\\bin\\x86_arm\\armasm.exe" CMAKE_ASM_COMPILER)
59 file(TO_CMAKE_PATH "$ENV{VCToolsInstallDir}\\bin\\HostX86\\arm\\armasm.exe" CMAKE_ASM_COMPILER)
62 set(CMAKE_ASM_MASM_COMPILER ${CMAKE_ASM_COMPILER})
63 message("CMAKE_ASM_MASM_COMPILER explicitly set to: ${CMAKE_ASM_MASM_COMPILER}")
65 # Enable generic assembly compilation to avoid CMake generate VS proj files that explicitly
66 # use ml[64].exe as the assembler.
69 enable_language(ASM_MASM)
72 # Ensure that MC is present
74 if (MC STREQUAL "MC-NOTFOUND")
75 message(FATAL_ERROR "MC not found")
78 if (CLR_CMAKE_HOST_ARCH STREQUAL arm64)
79 # CMAKE_CXX_COMPILER will default to the compiler installed with
80 # Visual studio. Overwrite it to the compiler on the path.
81 # TODO, remove when cmake generator supports Arm64 as a target.
82 find_program(PATH_CXX_COMPILER cl)
83 set(CMAKE_CXX_COMPILER ${PATH_CXX_COMPILER})
84 message("Overwriting the CMAKE_CXX_COMPILER.")
85 message(CMAKE_CXX_COMPILER found:${CMAKE_CXX_COMPILER})
91 # Ensure that awk is present
93 if (AWK STREQUAL "AWK-NOTFOUND")
94 message(FATAL_ERROR "AWK not found")
97 # Try to locate the paxctl tool. Failure to find it is not fatal,
98 # but the generated executables won't work on a system where PAX is set
99 # to prevent applications to create executable memory mappings.
100 find_program(PAXCTL paxctl)
102 if (CMAKE_SYSTEM_NAME STREQUAL Darwin)
104 # Ensure that dsymutil and strip are present
105 find_program(DSYMUTIL dsymutil)
106 if (DSYMUTIL STREQUAL "DSYMUTIL-NOTFOUND")
107 message(FATAL_ERROR "dsymutil not found")
110 find_program(STRIP strip)
111 if (STRIP STREQUAL "STRIP-NOTFOUND")
112 message(FATAL_ERROR "strip not found")
115 else (CMAKE_SYSTEM_NAME STREQUAL Darwin)
117 # Ensure that objcopy is present
118 if (CLR_UNIX_CROSS_BUILD AND NOT DEFINED CLR_CROSS_COMPONENTS_BUILD)
119 if (CMAKE_SYSTEM_PROCESSOR STREQUAL armv7l OR CMAKE_SYSTEM_PROCESSOR STREQUAL aarch64 OR CMAKE_SYSTEM_PROCESSOR STREQUAL arm)
120 find_program(OBJCOPY ${TOOLCHAIN}-objcopy)
121 elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL i686)
122 find_program(OBJCOPY objcopy)
127 find_program(OBJCOPY objcopy)
130 if (OBJCOPY STREQUAL "OBJCOPY-NOTFOUND")
131 message(FATAL_ERROR "objcopy not found")
134 endif (CMAKE_SYSTEM_NAME STREQUAL Darwin)
137 #----------------------------------------
138 # Detect and set platform variable names
139 # - for non-windows build platform & architecture is detected using inbuilt CMAKE variables and cross target component configure
140 # - for windows we use the passed in parameter to CMAKE to determine build arch
141 #----------------------------------------
142 if(CMAKE_SYSTEM_NAME STREQUAL Linux)
143 set(CLR_CMAKE_PLATFORM_UNIX 1)
144 if(CLR_CROSS_COMPONENTS_BUILD)
145 # CMAKE_HOST_SYSTEM_PROCESSOR returns the value of `uname -p` on host.
146 if(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL x86_64 OR CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL amd64)
147 if(CLR_CMAKE_TARGET_ARCH STREQUAL "arm")
148 set(CLR_CMAKE_PLATFORM_UNIX_X86 1)
150 set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
152 elseif(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL i686)
153 set(CLR_CMAKE_PLATFORM_UNIX_X86 1)
158 # CMAKE_SYSTEM_PROCESSOR returns the value of `uname -p` on target.
159 # For the AMD/Intel 64bit architecture two different strings are common.
160 # Linux and Darwin identify it as "x86_64" while FreeBSD and netbsd uses the
161 # "amd64" string. Accept either of the two here.
162 if(CMAKE_SYSTEM_PROCESSOR STREQUAL x86_64 OR CMAKE_SYSTEM_PROCESSOR STREQUAL amd64)
163 set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
164 elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL armv7l)
165 set(CLR_CMAKE_PLATFORM_UNIX_ARM 1)
166 elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL arm)
167 set(CLR_CMAKE_PLATFORM_UNIX_ARM 1)
168 elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL aarch64)
169 set(CLR_CMAKE_PLATFORM_UNIX_ARM64 1)
170 elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL i686)
171 set(CLR_CMAKE_PLATFORM_UNIX_X86 1)
176 set(CLR_CMAKE_PLATFORM_LINUX 1)
179 if(DEFINED CLR_CMAKE_LINUX_ID)
180 if(CLR_CMAKE_LINUX_ID STREQUAL ubuntu)
181 set(CLR_CMAKE_TARGET_UBUNTU_LINUX 1)
182 elseif(CLR_CMAKE_LINUX_ID STREQUAL tizen)
183 set(CLR_CMAKE_TARGET_TIZEN_LINUX 1)
184 elseif(CLR_CMAKE_LINUX_ID STREQUAL alpine)
185 set(CLR_CMAKE_PLATFORM_ALPINE_LINUX 1)
187 if(CLR_CMAKE_LINUX_ID STREQUAL ubuntu)
188 set(CLR_CMAKE_PLATFORM_UBUNTU_LINUX 1)
190 endif(DEFINED CLR_CMAKE_LINUX_ID)
191 endif(CMAKE_SYSTEM_NAME STREQUAL Linux)
193 if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
194 set(CLR_CMAKE_PLATFORM_UNIX 1)
195 set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
196 set(CLR_CMAKE_PLATFORM_DARWIN 1)
197 if(CMAKE_VERSION VERSION_LESS "3.4.0")
198 set(CMAKE_ASM_COMPILE_OBJECT "${CMAKE_C_COMPILER} <FLAGS> <DEFINES> -o <OBJECT> -c <SOURCE>")
200 set(CMAKE_ASM_COMPILE_OBJECT "${CMAKE_C_COMPILER} <FLAGS> <DEFINES> <INCLUDES> -o <OBJECT> -c <SOURCE>")
201 endif(CMAKE_VERSION VERSION_LESS "3.4.0")
202 endif(CMAKE_SYSTEM_NAME STREQUAL Darwin)
204 if(CMAKE_SYSTEM_NAME STREQUAL FreeBSD)
205 set(CLR_CMAKE_PLATFORM_UNIX 1)
206 set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
207 set(CLR_CMAKE_PLATFORM_FREEBSD 1)
208 endif(CMAKE_SYSTEM_NAME STREQUAL FreeBSD)
210 if(CMAKE_SYSTEM_NAME STREQUAL OpenBSD)
211 set(CLR_CMAKE_PLATFORM_UNIX 1)
212 set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
213 set(CLR_CMAKE_PLATFORM_OPENBSD 1)
214 endif(CMAKE_SYSTEM_NAME STREQUAL OpenBSD)
216 if(CMAKE_SYSTEM_NAME STREQUAL NetBSD)
217 set(CLR_CMAKE_PLATFORM_UNIX 1)
218 set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
219 set(CLR_CMAKE_PLATFORM_NETBSD 1)
220 endif(CMAKE_SYSTEM_NAME STREQUAL NetBSD)
222 if(CMAKE_SYSTEM_NAME STREQUAL SunOS)
223 set(CLR_CMAKE_PLATFORM_UNIX 1)
226 OUTPUT_VARIABLE SUNOS_NATIVE_INSTRUCTION_SET
228 if(SUNOS_NATIVE_INSTRUCTION_SET MATCHES "amd64")
229 set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
230 set(CMAKE_SYSTEM_PROCESSOR "amd64")
234 set(CLR_CMAKE_PLATFORM_SUNOS 1)
235 endif(CMAKE_SYSTEM_NAME STREQUAL SunOS)
237 #--------------------------------------------
238 # This repo builds two set of binaries
239 # 1. binaries which execute on target arch machine
240 # - for such binaries host architecture & target architecture are same
242 # 2. binaries which execute on host machine but target another architecture
243 # - host architecture is different from target architecture
244 # - eg. crossgen.exe - runs on x64 machine and generates nis targeting arm64
245 # - for complete list of such binaries refer to file crosscomponents.cmake
246 #-------------------------------------------------------------
247 # Set HOST architecture variables
248 if(CLR_CMAKE_PLATFORM_UNIX_ARM)
249 set(CLR_CMAKE_PLATFORM_ARCH_ARM 1)
250 set(CLR_CMAKE_HOST_ARCH "arm")
251 elseif(CLR_CMAKE_PLATFORM_UNIX_ARM64)
252 set(CLR_CMAKE_PLATFORM_ARCH_ARM64 1)
253 set(CLR_CMAKE_HOST_ARCH "arm64")
254 elseif(CLR_CMAKE_PLATFORM_UNIX_AMD64)
255 set(CLR_CMAKE_PLATFORM_ARCH_AMD64 1)
256 set(CLR_CMAKE_HOST_ARCH "x64")
257 elseif(CLR_CMAKE_PLATFORM_UNIX_X86)
258 set(CLR_CMAKE_PLATFORM_ARCH_I386 1)
259 set(CLR_CMAKE_HOST_ARCH "x86")
261 # CLR_CMAKE_HOST_ARCH is passed in as param to cmake
262 if (CLR_CMAKE_HOST_ARCH STREQUAL x64)
263 set(CLR_CMAKE_PLATFORM_ARCH_AMD64 1)
264 elseif(CLR_CMAKE_HOST_ARCH STREQUAL x86)
265 set(CLR_CMAKE_PLATFORM_ARCH_I386 1)
266 elseif(CLR_CMAKE_HOST_ARCH STREQUAL arm)
267 set(CLR_CMAKE_PLATFORM_ARCH_ARM 1)
268 elseif(CLR_CMAKE_HOST_ARCH STREQUAL arm64)
269 set(CLR_CMAKE_PLATFORM_ARCH_ARM64 1)
275 # Set TARGET architecture variables
276 # Target arch will be a cmake param (optional) for both windows as well as non-windows build
277 # if target arch is not specified then host & target are same
278 if(NOT DEFINED CLR_CMAKE_TARGET_ARCH OR CLR_CMAKE_TARGET_ARCH STREQUAL "" )
279 set(CLR_CMAKE_TARGET_ARCH ${CLR_CMAKE_HOST_ARCH})
282 # Set target architecture variables
283 if (CLR_CMAKE_TARGET_ARCH STREQUAL x64)
284 set(CLR_CMAKE_TARGET_ARCH_AMD64 1)
285 elseif(CLR_CMAKE_TARGET_ARCH STREQUAL x86)
286 set(CLR_CMAKE_TARGET_ARCH_I386 1)
287 elseif(CLR_CMAKE_TARGET_ARCH STREQUAL arm64)
288 set(CLR_CMAKE_TARGET_ARCH_ARM64 1)
289 elseif(CLR_CMAKE_TARGET_ARCH STREQUAL arm)
290 set(CLR_CMAKE_TARGET_ARCH_ARM 1)
295 # check if host & target arch combination are valid
296 if(NOT(CLR_CMAKE_TARGET_ARCH STREQUAL CLR_CMAKE_HOST_ARCH))
297 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)))
298 message(FATAL_ERROR "Invalid host and target arch combination")
302 #-----------------------------------------------------
303 # Initialize Cmake compiler flags and other variables
304 #-----------------------------------------------------
306 if (CMAKE_CONFIGURATION_TYPES) # multi-configuration generator?
307 set(CMAKE_CONFIGURATION_TYPES "Debug;Checked;Release;RelWithDebInfo" CACHE STRING "" FORCE)
308 endif (CMAKE_CONFIGURATION_TYPES)
310 set(CMAKE_C_FLAGS_CHECKED ${CLR_C_FLAGS_CHECKED_INIT} CACHE STRING "Flags used by the compiler during checked builds.")
311 set(CMAKE_CXX_FLAGS_CHECKED ${CLR_CXX_FLAGS_CHECKED_INIT} CACHE STRING "Flags used by the compiler during checked builds.")
312 set(CMAKE_EXE_LINKER_FLAGS_CHECKED "")
313 set(CMAKE_SHARED_LINKER_FLAGS_CHECKED "")
315 set(CMAKE_CXX_STANDARD_LIBRARIES "") # do not link against standard win32 libs i.e. kernel32, uuid, user32, etc.
318 # For multi-configuration toolset (as Visual Studio)
319 # set the different configuration defines.
320 foreach (Config DEBUG CHECKED RELEASE RELWITHDEBINFO)
321 foreach (Definition IN LISTS CLR_DEFINES_${Config}_INIT)
322 set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS $<$<CONFIG:${Config}>:${Definition}>)
323 endforeach (Definition)
326 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /GUARD:CF")
327 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /GUARD:CF")
331 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /MANIFEST:NO") #Do not create Side-by-Side Assembly Manifest
333 if (CLR_CMAKE_PLATFORM_ARCH_ARM)
334 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /SUBSYSTEM:WINDOWS,6.02") #windows subsystem - arm minimum is 6.02
335 elseif(CLR_CMAKE_PLATFORM_ARCH_ARM64)
336 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /SUBSYSTEM:WINDOWS,6.03") #windows subsystem - arm64 minimum is 6.03
338 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /SUBSYSTEM:WINDOWS,6.01") #windows subsystem
341 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /LARGEADDRESSAWARE") # can handle addresses larger than 2 gigabytes
342 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /NXCOMPAT") #Compatible with Data Execution Prevention
343 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /DYNAMICBASE") #Use address space layout randomization
344 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /PDBCOMPRESS") #shrink pdb size
345 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /DEBUG")
346 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /IGNORE:4197,4013,4254,4070,4221")
348 set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} /IGNORE:4221")
350 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /DEBUG /PDBCOMPRESS")
351 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /STACK:1572864")
353 # Temporarily disable incremental link due to incremental linking CFG bug crashing crossgen.
354 # See https://github.com/dotnet/coreclr/issues/12592
355 # This has been fixed in VS 2017 Update 5 but we're keeping this around until everyone is off
356 # the versions that have the bug. The bug manifests itself as a bad crash.
357 set(NO_INCREMENTAL_LINKER_FLAGS "/INCREMENTAL:NO")
359 # Debug build specific flags
360 set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "/NOVCFEATURE ${NO_INCREMENTAL_LINKER_FLAGS}")
361 set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${NO_INCREMENTAL_LINKER_FLAGS}")
363 # Checked build specific flags
364 set(CMAKE_SHARED_LINKER_FLAGS_CHECKED "${CMAKE_SHARED_LINKER_FLAGS_CHECKED} /OPT:REF /OPT:NOICF /NOVCFEATURE ${NO_INCREMENTAL_LINKER_FLAGS}")
365 set(CMAKE_STATIC_LINKER_FLAGS_CHECKED "${CMAKE_STATIC_LINKER_FLAGS_CHECKED}")
366 set(CMAKE_EXE_LINKER_FLAGS_CHECKED "${CMAKE_EXE_LINKER_FLAGS_CHECKED} /OPT:REF /OPT:NOICF ${NO_INCREMENTAL_LINKER_FLAGS}")
368 # Release build specific flags
369 set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /LTCG /OPT:REF /OPT:ICF ${NO_INCREMENTAL_LINKER_FLAGS}")
370 set(CMAKE_STATIC_LINKER_FLAGS_RELEASE "${CMAKE_STATIC_LINKER_FLAGS_RELEASE} /LTCG")
371 set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /LTCG /OPT:REF /OPT:ICF ${NO_INCREMENTAL_LINKER_FLAGS}")
373 # ReleaseWithDebugInfo build specific flags
374 set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO} /LTCG /OPT:REF /OPT:ICF ${NO_INCREMENTAL_LINKER_FLAGS}")
375 set(CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO} /LTCG")
376 set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} /LTCG /OPT:REF /OPT:ICF ${NO_INCREMENTAL_LINKER_FLAGS}")
378 # Temporary until cmake has VS generators for arm64
379 if(CLR_CMAKE_PLATFORM_ARCH_ARM64)
380 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /machine:arm64")
381 set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} /machine:arm64")
382 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /machine:arm64")
383 endif(CLR_CMAKE_PLATFORM_ARCH_ARM64)
385 # Force uCRT to be dynamically linked for Release build
386 set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /NODEFAULTLIB:libucrt.lib /DEFAULTLIB:ucrt.lib")
387 set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /NODEFAULTLIB:libucrt.lib /DEFAULTLIB:ucrt.lib")
388 set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO} /NODEFAULTLIB:libucrt.lib /DEFAULTLIB:ucrt.lib")
389 set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} /NODEFAULTLIB:libucrt.lib /DEFAULTLIB:ucrt.lib")
391 elseif (CLR_CMAKE_PLATFORM_UNIX)
392 # Set the values to display when interactively configuring CMAKE_BUILD_TYPE
393 set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "DEBUG;CHECKED;RELEASE;RELWITHDEBINFO")
395 # Use uppercase CMAKE_BUILD_TYPE for the string comparisons below
396 string(TOUPPER ${CMAKE_BUILD_TYPE} UPPERCASE_CMAKE_BUILD_TYPE)
398 # For single-configuration toolset
399 # set the different configuration defines.
400 if (UPPERCASE_CMAKE_BUILD_TYPE STREQUAL DEBUG)
402 set_property(DIRECTORY PROPERTY COMPILE_DEFINITIONS ${CLR_DEFINES_DEBUG_INIT})
403 elseif (UPPERCASE_CMAKE_BUILD_TYPE STREQUAL CHECKED)
405 set_property(DIRECTORY PROPERTY COMPILE_DEFINITIONS ${CLR_DEFINES_CHECKED_INIT})
406 elseif (UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELEASE)
408 set_property(DIRECTORY PROPERTY COMPILE_DEFINITIONS ${CLR_DEFINES_RELEASE_INIT})
409 elseif (UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELWITHDEBINFO)
410 # And then RELWITHDEBINFO
411 set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS ${CLR_DEFINES_RELWITHDEBINFO_INIT})
413 message(FATAL_ERROR "Unknown build type! Set CMAKE_BUILD_TYPE to DEBUG, CHECKED, RELEASE, or RELWITHDEBINFO!")
416 # set the CLANG sanitizer flags for debug build
417 if(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL DEBUG OR UPPERCASE_CMAKE_BUILD_TYPE STREQUAL CHECKED)
418 # obtain settings from running enablesanitizers.sh
419 string(FIND "$ENV{DEBUG_SANITIZERS}" "asan" __ASAN_POS)
420 string(FIND "$ENV{DEBUG_SANITIZERS}" "ubsan" __UBSAN_POS)
421 if ((${__ASAN_POS} GREATER -1) OR (${__UBSAN_POS} GREATER -1))
422 set(CLR_SANITIZE_CXX_FLAGS "${CLR_SANITIZE_CXX_FLAGS} -fsanitize-blacklist=${CMAKE_CURRENT_SOURCE_DIR}/sanitizerblacklist.txt -fsanitize=")
423 set(CLR_SANITIZE_LINK_FLAGS "${CLR_SANITIZE_LINK_FLAGS} -fsanitize=")
424 if (${__ASAN_POS} GREATER -1)
425 set(CLR_SANITIZE_CXX_FLAGS "${CLR_SANITIZE_CXX_FLAGS}address,")
426 set(CLR_SANITIZE_LINK_FLAGS "${CLR_SANITIZE_LINK_FLAGS}address,")
427 add_definitions(-DHAS_ASAN)
428 message("Address Sanitizer (asan) enabled")
430 if (${__UBSAN_POS} GREATER -1)
431 # all sanitizier flags are enabled except alignment (due to heavy use of __unaligned modifier)
432 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")
433 set(CLR_SANITIZE_LINK_FLAGS "${CLR_SANITIZE_LINK_FLAGS}undefined")
434 message("Undefined Behavior Sanitizer (ubsan) enabled")
437 # -fdata-sections -ffunction-sections: each function has own section instead of one per .o file (needed for --gc-sections)
438 # -fPIC: enable Position Independent Code normally just for shared libraries but required when linking with address sanitizer
439 # -O1: optimization level used instead of -O0 to avoid compile error "invalid operand for inline asm constraint"
440 set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${CLR_SANITIZE_CXX_FLAGS} -fdata-sections -ffunction-sections -fPIC -O1")
441 set(CMAKE_CXX_FLAGS_CHECKED "${CMAKE_CXX_FLAGS_CHECKED} ${CLR_SANITIZE_CXX_FLAGS} -fdata-sections -ffunction-sections -fPIC -O1")
443 set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} ${CLR_SANITIZE_LINK_FLAGS}")
444 set(CMAKE_EXE_LINKER_FLAGS_CHECKED "${CMAKE_EXE_LINKER_FLAGS_CHECKED} ${CLR_SANITIZE_LINK_FLAGS}")
446 # -Wl and --gc-sections: drop unused sections\functions (similar to Windows /Gy function-level-linking)
447 set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} ${CLR_SANITIZE_LINK_FLAGS} -Wl,--gc-sections")
448 set(CMAKE_SHARED_LINKER_FLAGS_CHECKED "${CMAKE_SHARED_LINKER_FLAGS_CHECKED} ${CLR_SANITIZE_LINK_FLAGS} -Wl,--gc-sections")
450 endif(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL DEBUG OR UPPERCASE_CMAKE_BUILD_TYPE STREQUAL CHECKED)
453 # CLR_ADDITIONAL_LINKER_FLAGS - used for passing additional arguments to linker
454 # CLR_ADDITIONAL_COMPILER_OPTIONS - used for passing additional arguments to compiler
457 # ./build-native.sh cmakeargs "-DCLR_ADDITIONAL_COMPILER_OPTIONS=<...>" cmakeargs "-DCLR_ADDITIONAL_LINKER_FLAGS=<...>"
459 if(CLR_CMAKE_PLATFORM_UNIX)
460 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${CLR_ADDITIONAL_LINKER_FLAGS}")
461 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${CLR_ADDITIONAL_LINKER_FLAGS}" )
462 add_compile_options(${CLR_ADDITIONAL_COMPILER_OPTIONS})
463 endif(CLR_CMAKE_PLATFORM_UNIX)
465 if(CLR_CMAKE_PLATFORM_LINUX)
466 set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -Wa,--noexecstack")
467 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--build-id=sha1")
468 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--build-id=sha1")
469 endif(CLR_CMAKE_PLATFORM_LINUX)
471 #------------------------------------
472 # Definitions (for platform)
473 #-----------------------------------
474 if (CLR_CMAKE_PLATFORM_ARCH_AMD64)
475 add_definitions(-D_AMD64_)
476 add_definitions(-D_WIN64)
477 add_definitions(-DAMD64)
478 add_definitions(-DBIT64=1)
479 elseif (CLR_CMAKE_PLATFORM_ARCH_I386)
480 add_definitions(-D_X86_)
481 elseif (CLR_CMAKE_PLATFORM_ARCH_ARM)
482 add_definitions(-D_ARM_)
483 add_definitions(-DARM)
484 elseif (CLR_CMAKE_PLATFORM_ARCH_ARM64)
485 add_definitions(-D_ARM64_)
486 add_definitions(-DARM64)
487 add_definitions(-D_WIN64)
488 add_definitions(-DBIT64=1)
493 if (CLR_CMAKE_PLATFORM_UNIX)
494 if(CLR_CMAKE_PLATFORM_LINUX)
495 if(CLR_CMAKE_PLATFORM_UNIX_AMD64)
496 message("Detected Linux x86_64")
497 add_definitions(-DLINUX64)
498 elseif(CLR_CMAKE_PLATFORM_UNIX_ARM)
499 message("Detected Linux ARM")
500 add_definitions(-DLINUX32)
501 elseif(CLR_CMAKE_PLATFORM_UNIX_ARM64)
502 message("Detected Linux ARM64")
503 add_definitions(-DLINUX64)
504 elseif(CLR_CMAKE_PLATFORM_UNIX_X86)
505 message("Detected Linux i686")
506 add_definitions(-DLINUX32)
510 endif(CLR_CMAKE_PLATFORM_LINUX)
511 endif(CLR_CMAKE_PLATFORM_UNIX)
513 if (CLR_CMAKE_PLATFORM_UNIX)
514 add_definitions(-DPLATFORM_UNIX=1)
516 if(CLR_CMAKE_PLATFORM_DARWIN)
517 message("Detected OSX x86_64")
518 endif(CLR_CMAKE_PLATFORM_DARWIN)
520 if(CLR_CMAKE_PLATFORM_FREEBSD)
521 message("Detected FreeBSD amd64")
522 endif(CLR_CMAKE_PLATFORM_FREEBSD)
524 if(CLR_CMAKE_PLATFORM_NETBSD)
525 message("Detected NetBSD amd64")
526 endif(CLR_CMAKE_PLATFORM_NETBSD)
527 endif(CLR_CMAKE_PLATFORM_UNIX)
530 # Define the CRT lib references that link into Desktop imports
531 set(STATIC_MT_CRT_LIB "libcmt$<$<OR:$<CONFIG:Debug>,$<CONFIG:Checked>>:d>.lib")
532 set(STATIC_MT_VCRT_LIB "libvcruntime$<$<OR:$<CONFIG:Debug>,$<CONFIG:Checked>>:d>.lib")
533 set(STATIC_MT_CPP_LIB "libcpmt$<$<OR:$<CONFIG:Debug>,$<CONFIG:Checked>>:d>.lib")
535 # Define the uCRT lib reference
536 set(STATIC_UCRT_LIB "libucrt$<$<OR:$<CONFIG:Debug>,$<CONFIG:Checked>>:d>.lib")
537 set(DYNAMIC_UCRT_LIB "ucrt$<$<OR:$<CONFIG:Debug>,$<CONFIG:Checked>>:d>.lib")
540 # Architecture specific files folder name
541 if (CLR_CMAKE_TARGET_ARCH_AMD64)
542 set(ARCH_SOURCES_DIR amd64)
543 elseif (CLR_CMAKE_TARGET_ARCH_ARM64)
544 set(ARCH_SOURCES_DIR arm64)
545 elseif (CLR_CMAKE_TARGET_ARCH_ARM)
546 set(ARCH_SOURCES_DIR arm)
547 elseif (CLR_CMAKE_TARGET_ARCH_I386)
548 set(ARCH_SOURCES_DIR i386)
553 #--------------------------------------
555 #--------------------------------------
556 include(compileoptions.cmake)
558 #----------------------------------------------------
559 # Cross target Component build specific configuration
560 #----------------------------------------------------
561 if(CLR_CROSS_COMPONENTS_BUILD)
562 include(crosscomponents.cmake)
563 endif(CLR_CROSS_COMPONENTS_BUILD)
568 include(pgosupport.cmake)
570 #-----------------------------------------
572 # - project which require platform header not clr's
573 # - do not depend on clr's compile definitions
574 #-----------------------------------------
575 if(CLR_CMAKE_PLATFORM_UNIX AND NOT DEFINED CLR_CROSS_COMPONENTS_BUILD)
576 add_subdirectory(src/corefx)
579 if(CLR_CMAKE_PLATFORM_UNIX)
580 add_subdirectory(src/ToolBox/SOS/lldbplugin)
581 add_subdirectory(src/pal)
582 add_subdirectory(src/coreclr/hosts)
583 add_subdirectory(src/ildasm/unixcoreclrloader)
584 endif(CLR_CMAKE_PLATFORM_UNIX)
586 # Add this subdir. We install the headers for the jit.
587 add_subdirectory(src/pal/prebuilt/inc)
589 add_subdirectory(src/debug/debug-pal)
592 add_subdirectory(src/gc/sample)
595 # Above projects do not build with these compile options
596 # All of the compiler options are specified in file compileoptions.cmake
597 # Do not add any new options here. They should be added in compileoptions.cmake
599 add_compile_options(/FIWarningControl.h) # force include of WarningControl.h
600 add_compile_options(/Zl) # omit default library name in .OBJ
603 #------------------------------
605 #------------------------------
606 if(CLR_CMAKE_BUILD_TESTS)
607 add_subdirectory(tests)
608 endif(CLR_CMAKE_BUILD_TESTS)
610 #--------------------------------
611 # Definition directives
612 # - all clr specific compile definitions should be included in this file
613 # - all clr specific feature variable should also be added in this file
614 #----------------------------------
615 include(clrdefinitions.cmake)
617 #-------------------------------------
618 # Include directory directives
619 #-------------------------------------
620 # Include the basic prebuilt headers - required for getting fileversion resource details.
621 include_directories("src/pal/prebuilt/inc")
622 include_directories("bin/obj")
624 if(FEATURE_STANDALONE_GC)
625 add_definitions(-DFEATURE_STANDALONE_GC)
626 add_subdirectory(src/gc)
627 endif(FEATURE_STANDALONE_GC)
629 if (CLR_CMAKE_PLATFORM_UNIX)
630 include_directories("src/pal/inc")
631 include_directories("src/pal/inc/rt")
632 include_directories("src/pal/src/safecrt")
633 endif (CLR_CMAKE_PLATFORM_UNIX)
635 # Microsoft.Dotnet.BuildTools.Coreclr version
636 set(BuildToolsVersion "1.0.4-prerelease")
637 set(BuildToolsDir "${CLR_CMAKE_PACKAGES_DIR}/Microsoft.DotNet.BuildTools.CoreCLR/${BuildToolsVersion}")
639 #------------------------------
640 # Add Product Directory
641 #------------------------------
642 add_subdirectory(src)
644 include(definitionsconsistencycheck.cmake)