1 # Require at least version 2.8.12 of CMake
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)
15 message(STATUS "VS_PLATFORM_TOOLSET is ${CMAKE_VS_PLATFORM_TOOLSET}")
16 message(STATUS "VS_PLATFORM_NAME is ${CMAKE_VS_PLATFORM_NAME}")
19 # Set commonly used directory names
20 set(CLR_DIR ${CMAKE_CURRENT_SOURCE_DIR})
21 set(VM_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src/vm)
22 set(GENERATED_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/src/inc)
23 set(VERSION_FILE_PATH "${CMAKE_BINARY_DIR}/version.cpp")
25 set(CORECLR_SET_RPATH ON)
27 # Enable @rpath support for shared libraries.
29 endif(CORECLR_SET_RPATH)
31 OPTION(CMAKE_ENABLE_CODE_COVERAGE "Enable code coverage" OFF)
33 # Ensure that python is present
34 find_program(PYTHON python)
35 if (PYTHON STREQUAL "PYTHON-NOTFOUND")
36 message(FATAL_ERROR "PYTHON not found: Please install Python 2.7.9 or later from https://www.python.org/downloads/")
39 # Ensure other tools are present
41 if(CLR_CMAKE_HOST_ARCH STREQUAL arm)
43 # Confirm that Windows SDK is present
44 if(NOT DEFINED CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION OR CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION STREQUAL "" )
45 message(FATAL_ERROR "Windows SDK is required for the Arm32 build.")
47 message("Using Windows SDK version ${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}")
50 # Explicitly specify the assembler to be used for Arm32 compile
51 file(TO_CMAKE_PATH "$ENV{VCINSTALLDIR}\\bin\\x86_arm\\armasm.exe" CMAKE_ASM_COMPILER)
53 set(CMAKE_ASM_MASM_COMPILER ${CMAKE_ASM_COMPILER})
54 message("CMAKE_ASM_MASM_COMPILER explicitly set to: ${CMAKE_ASM_MASM_COMPILER}")
56 # Enable generic assembly compilation to avoid CMake generate VS proj files that explicitly
57 # use ml[64].exe as the assembler.
60 enable_language(ASM_MASM)
63 # Ensure that MC is present
65 if (MC STREQUAL "MC-NOTFOUND")
66 message(FATAL_ERROR "MC not found")
69 if (CLR_CMAKE_HOST_ARCH STREQUAL arm64)
70 # CMAKE_CXX_COMPILER will default to the compiler installed with
71 # Visual studio. Overwrite it to the compiler on the path.
72 # TODO, remove when cmake generator supports Arm64 as a target.
73 find_program(PATH_CXX_COMPILER cl)
74 set(CMAKE_CXX_COMPILER ${PATH_CXX_COMPILER})
75 message("Overwriting the CMAKE_CXX_COMPILER.")
76 message(CMAKE_CXX_COMPILER found:${CMAKE_CXX_COMPILER})
82 # Ensure that awk is present
84 if (AWK STREQUAL "AWK-NOTFOUND")
85 message(FATAL_ERROR "AWK not found")
88 if (CMAKE_SYSTEM_NAME STREQUAL Darwin)
90 # Ensure that dsymutil and strip are present
91 find_program(DSYMUTIL dsymutil)
92 if (DSYMUTIL STREQUAL "DSYMUTIL-NOTFOUND")
93 message(FATAL_ERROR "dsymutil not found")
96 find_program(STRIP strip)
97 if (STRIP STREQUAL "STRIP-NOTFOUND")
98 message(FATAL_ERROR "strip not found")
101 else (CMAKE_SYSTEM_NAME STREQUAL Darwin)
103 # Ensure that objcopy is present
104 if (DEFINED ENV{CROSSCOMPILE} AND NOT DEFINED CLR_CROSS_COMPONENTS_BUILD)
105 if (CMAKE_SYSTEM_PROCESSOR STREQUAL armv7l OR CMAKE_SYSTEM_PROCESSOR STREQUAL aarch64 OR CMAKE_SYSTEM_PROCESSOR STREQUAL arm)
106 find_program(OBJCOPY ${TOOLCHAIN}-objcopy)
107 elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL i686)
108 find_program(OBJCOPY objcopy)
113 find_program(OBJCOPY objcopy)
116 if (OBJCOPY STREQUAL "OBJCOPY-NOTFOUND")
117 message(FATAL_ERROR "objcopy not found")
120 endif (CMAKE_SYSTEM_NAME STREQUAL Darwin)
123 #----------------------------------------
124 # Detect and set platform variable names
125 # - for non-windows build platform & architecture is detected using inbuilt CMAKE variables and cross target component configure
126 # - for windows we use the passed in parameter to CMAKE to determine build arch
127 #----------------------------------------
128 if(CMAKE_SYSTEM_NAME STREQUAL Linux)
129 set(CLR_CMAKE_PLATFORM_UNIX 1)
130 if(CLR_CROSS_COMPONENTS_BUILD)
131 # CMAKE_HOST_SYSTEM_PROCESSOR returns the value of `uname -p` on host.
132 if(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL x86_64 OR CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL amd64)
133 if(CLR_CMAKE_TARGET_ARCH STREQUAL "arm")
134 set(CLR_CMAKE_PLATFORM_UNIX_X86 1)
136 set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
138 elseif(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL i686)
139 set(CLR_CMAKE_PLATFORM_UNIX_X86 1)
144 # CMAKE_SYSTEM_PROCESSOR returns the value of `uname -p` on target.
145 # For the AMD/Intel 64bit architecure two different strings are common.
146 # Linux and Darwin identify it as "x86_64" while FreeBSD and netbsd uses the
147 # "amd64" string. Accept either of the two here.
148 if(CMAKE_SYSTEM_PROCESSOR STREQUAL x86_64 OR CMAKE_SYSTEM_PROCESSOR STREQUAL amd64)
149 set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
150 elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL armv7l)
151 set(CLR_CMAKE_PLATFORM_UNIX_ARM 1)
152 elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL arm)
153 set(CLR_CMAKE_PLATFORM_UNIX_ARM 1)
154 elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL aarch64)
155 set(CLR_CMAKE_PLATFORM_UNIX_ARM64 1)
156 elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL i686)
157 set(CLR_CMAKE_PLATFORM_UNIX_X86 1)
162 set(CLR_CMAKE_PLATFORM_LINUX 1)
164 # Detect Alpine Linux
165 SET(OS_RELEASE_FILENAME "/etc/os-release")
166 if (EXISTS ${OS_RELEASE_FILENAME})
167 file(READ ${OS_RELEASE_FILENAME} OS_RELEASE)
168 string(FIND "${OS_RELEASE}" "ID=alpine" CLR_CMAKE_PLATFORM_ALPINE_LINUX)
169 if(CLR_CMAKE_PLATFORM_ALPINE_LINUX EQUAL -1)
170 unset(CLR_CMAKE_PLATFORM_ALPINE_LINUX)
171 endif(CLR_CMAKE_PLATFORM_ALPINE_LINUX EQUAL -1)
172 endif(EXISTS ${OS_RELEASE_FILENAME})
173 endif(CMAKE_SYSTEM_NAME STREQUAL Linux)
175 if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
176 set(CLR_CMAKE_PLATFORM_UNIX 1)
177 set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
178 set(CLR_CMAKE_PLATFORM_DARWIN 1)
179 if(CMAKE_VERSION VERSION_LESS "3.4.0")
180 set(CMAKE_ASM_COMPILE_OBJECT "${CMAKE_C_COMPILER} <FLAGS> <DEFINES> -o <OBJECT> -c <SOURCE>")
182 set(CMAKE_ASM_COMPILE_OBJECT "${CMAKE_C_COMPILER} <FLAGS> <DEFINES> <INCLUDES> -o <OBJECT> -c <SOURCE>")
183 endif(CMAKE_VERSION VERSION_LESS "3.4.0")
184 endif(CMAKE_SYSTEM_NAME STREQUAL Darwin)
186 if(CMAKE_SYSTEM_NAME STREQUAL FreeBSD)
187 set(CLR_CMAKE_PLATFORM_UNIX 1)
188 set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
189 set(CLR_CMAKE_PLATFORM_FREEBSD 1)
190 endif(CMAKE_SYSTEM_NAME STREQUAL FreeBSD)
192 if(CMAKE_SYSTEM_NAME STREQUAL OpenBSD)
193 set(CLR_CMAKE_PLATFORM_UNIX 1)
194 set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
195 set(CLR_CMAKE_PLATFORM_OPENBSD 1)
196 endif(CMAKE_SYSTEM_NAME STREQUAL OpenBSD)
198 if(CMAKE_SYSTEM_NAME STREQUAL NetBSD)
199 set(CLR_CMAKE_PLATFORM_UNIX 1)
200 set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
201 set(CLR_CMAKE_PLATFORM_NETBSD 1)
202 endif(CMAKE_SYSTEM_NAME STREQUAL NetBSD)
204 if(CMAKE_SYSTEM_NAME STREQUAL SunOS)
205 set(CLR_CMAKE_PLATFORM_UNIX 1)
208 OUTPUT_VARIABLE SUNOS_NATIVE_INSTRUCTION_SET
210 if(SUNOS_NATIVE_INSTRUCTION_SET MATCHES "amd64")
211 set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
212 set(CMAKE_SYSTEM_PROCESSOR "amd64")
216 set(CLR_CMAKE_PLATFORM_SUNOS 1)
217 endif(CMAKE_SYSTEM_NAME STREQUAL SunOS)
219 #--------------------------------------------
220 # This repo builds two set of binaries
221 # 1. binaries which execute on target arch machine
222 # - for such binaries host architecture & target architecture are same
224 # 2. binaries which execute on host machine but target another architecture
225 # - host architecture is different from target architecture
226 # - eg. crossgen.exe - runs on x64 machine and generates nis targeting arm64
227 # - for complete list of such binaries refer to file crosscomponents.cmake
228 #-------------------------------------------------------------
229 # Set HOST architecture variables
230 if(CLR_CMAKE_PLATFORM_UNIX_ARM)
231 set(CLR_CMAKE_PLATFORM_ARCH_ARM 1)
232 set(CLR_CMAKE_HOST_ARCH "arm")
233 elseif(CLR_CMAKE_PLATFORM_UNIX_ARM64)
234 set(CLR_CMAKE_PLATFORM_ARCH_ARM64 1)
235 set(CLR_CMAKE_HOST_ARCH "arm64")
236 elseif(CLR_CMAKE_PLATFORM_UNIX_AMD64)
237 set(CLR_CMAKE_PLATFORM_ARCH_AMD64 1)
238 set(CLR_CMAKE_HOST_ARCH "x64")
239 elseif(CLR_CMAKE_PLATFORM_UNIX_X86)
240 set(CLR_CMAKE_PLATFORM_ARCH_I386 1)
241 set(CLR_CMAKE_HOST_ARCH "x86")
243 # CLR_CMAKE_HOST_ARCH is passed in as param to cmake
244 if (CLR_CMAKE_HOST_ARCH STREQUAL x64)
245 set(CLR_CMAKE_PLATFORM_ARCH_AMD64 1)
246 elseif(CLR_CMAKE_HOST_ARCH STREQUAL x86)
247 set(CLR_CMAKE_PLATFORM_ARCH_I386 1)
248 elseif(CLR_CMAKE_HOST_ARCH STREQUAL arm)
249 set(CLR_CMAKE_PLATFORM_ARCH_ARM 1)
250 elseif(CLR_CMAKE_HOST_ARCH STREQUAL arm64)
251 set(CLR_CMAKE_PLATFORM_ARCH_ARM64 1)
257 # Set TARGET architecture variables
258 # Target arch will be a cmake param (optional) for both windows as well as non-windows build
259 # if target arch is not specified then host & target are same
260 if(NOT DEFINED CLR_CMAKE_TARGET_ARCH OR CLR_CMAKE_TARGET_ARCH STREQUAL "" )
261 set(CLR_CMAKE_TARGET_ARCH ${CLR_CMAKE_HOST_ARCH})
264 # Set target architecture variables
265 if (CLR_CMAKE_TARGET_ARCH STREQUAL x64)
266 set(CLR_CMAKE_TARGET_ARCH_AMD64 1)
267 elseif(CLR_CMAKE_TARGET_ARCH STREQUAL x86)
268 set(CLR_CMAKE_TARGET_ARCH_I386 1)
269 elseif(CLR_CMAKE_TARGET_ARCH STREQUAL arm64)
270 set(CLR_CMAKE_TARGET_ARCH_ARM64 1)
271 elseif(CLR_CMAKE_TARGET_ARCH STREQUAL arm)
272 set(CLR_CMAKE_TARGET_ARCH_ARM 1)
277 # check if host & target arch combination are valid
278 if(NOT(CLR_CMAKE_TARGET_ARCH STREQUAL CLR_CMAKE_HOST_ARCH))
279 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)))
280 message(FATAL_ERROR "Invalid host and target arch combination")
284 #-----------------------------------------------------
285 # Initialize Cmake compiler flags and other variables
286 #-----------------------------------------------------
288 if (CMAKE_CONFIGURATION_TYPES) # multi-configuration generator?
289 set(CMAKE_CONFIGURATION_TYPES "Debug;Checked;Release;RelWithDebInfo" CACHE STRING "" FORCE)
290 endif (CMAKE_CONFIGURATION_TYPES)
292 set(CMAKE_C_FLAGS_CHECKED ${CLR_C_FLAGS_CHECKED_INIT} CACHE STRING "Flags used by the compiler during checked builds.")
293 set(CMAKE_CXX_FLAGS_CHECKED ${CLR_CXX_FLAGS_CHECKED_INIT} CACHE STRING "Flags used by the compiler during checked builds.")
294 set(CMAKE_EXE_LINKER_FLAGS_CHECKED "")
295 set(CMAKE_SHARED_LINKER_FLAGS_CHECKED "")
297 # Disable the following line for UNIX altjit on Windows
298 set(CMAKE_CXX_STANDARD_LIBRARIES "") # do not link against standard win32 libs i.e. kernel32, uuid, user32, etc.
301 # For multi-configuration toolset (as Visual Studio)
302 # set the different configuration defines.
303 foreach (Config DEBUG CHECKED RELEASE RELWITHDEBINFO)
304 foreach (Definition IN LISTS CLR_DEFINES_${Config}_INIT)
305 set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS $<$<CONFIG:${Config}>:${Definition}>)
306 endforeach (Definition)
309 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /guard:cf")
310 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /guard:cf")
312 # Incremental linking with CFG is broken until next VS release.
313 # This needs to be appended to the last for each build type to override the default flag.
314 set(NO_INCREMENTAL_LINKER_FLAGS "/INCREMENTAL:NO")
318 # Disable the following line for UNIX altjit on Windows
319 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /MANIFEST:NO") #Do not create Side-by-Side Assembly Manifest
321 if (CLR_CMAKE_PLATFORM_ARCH_ARM)
322 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /SUBSYSTEM:WINDOWS,6.02") #windows subsystem - arm minimum is 6.02
323 elseif(CLR_CMAKE_PLATFORM_ARCH_ARM64)
324 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /SUBSYSTEM:WINDOWS,6.03") #windows subsystem - arm64 minimum is 6.03
326 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /SUBSYSTEM:WINDOWS,6.01") #windows subsystem
329 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /LARGEADDRESSAWARE") # can handle addresses larger than 2 gigabytes
330 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /RELEASE") #sets the checksum in the header
331 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /NXCOMPAT") #Compatible with Data Execution Prevention
332 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /DYNAMICBASE") #Use address space layout randomization
333 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /DEBUGTYPE:cv,fixup") #debugging format
334 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /PDBCOMPRESS") #shrink pdb size
335 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /DEBUG")
336 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /IGNORE:4197,4013,4254,4070,4221")
338 set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} /IGNORE:4221")
340 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /DEBUG /PDBCOMPRESS")
341 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /STACK:1572864")
343 # Debug build specific flags
344 set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "/NOVCFEATURE ${NO_INCREMENTAL_LINKER_FLAGS}")
345 set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${NO_INCREMENTAL_LINKER_FLAGS}")
347 # Checked build specific flags
348 set(CMAKE_SHARED_LINKER_FLAGS_CHECKED "${CMAKE_SHARED_LINKER_FLAGS_CHECKED} /OPT:REF /OPT:NOICF /NOVCFEATURE ${NO_INCREMENTAL_LINKER_FLAGS}")
349 set(CMAKE_STATIC_LINKER_FLAGS_CHECKED "${CMAKE_STATIC_LINKER_FLAGS_CHECKED}")
350 set(CMAKE_EXE_LINKER_FLAGS_CHECKED "${CMAKE_EXE_LINKER_FLAGS_CHECKED} /OPT:REF /OPT:NOICF ${NO_INCREMENTAL_LINKER_FLAGS}")
352 # Release build specific flags
353 set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /LTCG /OPT:REF /OPT:ICF ${NO_INCREMENTAL_LINKER_FLAGS}")
354 set(CMAKE_STATIC_LINKER_FLAGS_RELEASE "${CMAKE_STATIC_LINKER_FLAGS_RELEASE} /LTCG")
355 set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /LTCG /OPT:REF /OPT:ICF ${NO_INCREMENTAL_LINKER_FLAGS}")
357 # ReleaseWithDebugInfo build specific flags
358 set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO} /LTCG /OPT:REF /OPT:ICF ${NO_INCREMENTAL_LINKER_FLAGS}")
359 set(CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO} /LTCG")
360 set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} /LTCG /OPT:REF /OPT:ICF ${NO_INCREMENTAL_LINKER_FLAGS}")
362 # Temporary until cmake has VS generators for arm64
363 if(CLR_CMAKE_PLATFORM_ARCH_ARM64)
364 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /machine:arm64")
365 set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} /machine:arm64")
366 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /machine:arm64")
367 endif(CLR_CMAKE_PLATFORM_ARCH_ARM64)
369 # Force uCRT to be dynamically linked for Release build
370 set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /NODEFAULTLIB:libucrt.lib /DEFAULTLIB:ucrt.lib")
371 set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /NODEFAULTLIB:libucrt.lib /DEFAULTLIB:ucrt.lib")
372 set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO} /NODEFAULTLIB:libucrt.lib /DEFAULTLIB:ucrt.lib")
373 set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} /NODEFAULTLIB:libucrt.lib /DEFAULTLIB:ucrt.lib")
375 elseif (CLR_CMAKE_PLATFORM_UNIX)
376 # Set the values to display when interactively configuring CMAKE_BUILD_TYPE
377 set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "DEBUG;CHECKED;RELEASE;RELWITHDEBINFO")
379 # Use uppercase CMAKE_BUILD_TYPE for the string comparisons below
380 string(TOUPPER ${CMAKE_BUILD_TYPE} UPPERCASE_CMAKE_BUILD_TYPE)
382 # For single-configuration toolset
383 # set the different configuration defines.
384 if (UPPERCASE_CMAKE_BUILD_TYPE STREQUAL DEBUG)
386 set_property(DIRECTORY PROPERTY COMPILE_DEFINITIONS ${CLR_DEFINES_DEBUG_INIT})
387 elseif (UPPERCASE_CMAKE_BUILD_TYPE STREQUAL CHECKED)
389 set_property(DIRECTORY PROPERTY COMPILE_DEFINITIONS ${CLR_DEFINES_CHECKED_INIT})
390 elseif (UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELEASE)
392 set_property(DIRECTORY PROPERTY COMPILE_DEFINITIONS ${CLR_DEFINES_RELEASE_INIT})
393 elseif (UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELWITHDEBINFO)
394 # And then RELWITHDEBINFO
395 set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS ${CLR_DEFINES_RELWITHDEBINFO_INIT})
397 message(FATAL_ERROR "Unknown build type! Set CMAKE_BUILD_TYPE to DEBUG, CHECKED, RELEASE, or RELWITHDEBINFO!")
400 # set the CLANG sanitizer flags for debug build
401 if(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL DEBUG OR UPPERCASE_CMAKE_BUILD_TYPE STREQUAL CHECKED)
402 # obtain settings from running enablesanitizers.sh
403 string(FIND "$ENV{DEBUG_SANITIZERS}" "asan" __ASAN_POS)
404 string(FIND "$ENV{DEBUG_SANITIZERS}" "ubsan" __UBSAN_POS)
405 if ((${__ASAN_POS} GREATER -1) OR (${__UBSAN_POS} GREATER -1))
406 set(CLR_SANITIZE_CXX_FLAGS "${CLR_SANITIZE_CXX_FLAGS} -fsanitize-blacklist=${CMAKE_CURRENT_SOURCE_DIR}/sanitizerblacklist.txt -fsanitize=")
407 set(CLR_SANITIZE_LINK_FLAGS "${CLR_SANITIZE_LINK_FLAGS} -fsanitize=")
408 if (${__ASAN_POS} GREATER -1)
409 set(CLR_SANITIZE_CXX_FLAGS "${CLR_SANITIZE_CXX_FLAGS}address,")
410 set(CLR_SANITIZE_LINK_FLAGS "${CLR_SANITIZE_LINK_FLAGS}address,")
411 message("Address Sanitizer (asan) enabled")
413 if (${__UBSAN_POS} GREATER -1)
414 # all sanitizier flags are enabled except alignment (due to heavy use of __unaligned modifier)
415 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")
416 set(CLR_SANITIZE_LINK_FLAGS "${CLR_SANITIZE_LINK_FLAGS}undefined")
417 message("Undefined Behavior Sanitizer (ubsan) enabled")
420 # -fdata-sections -ffunction-sections: each function has own section instead of one per .o file (needed for --gc-sections)
421 # -fPIC: enable Position Independent Code normally just for shared libraries but required when linking with address sanitizer
422 # -O1: optimization level used instead of -O0 to avoid compile error "invalid operand for inline asm constraint"
423 set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${CLR_SANITIZE_CXX_FLAGS} -fdata-sections -ffunction-sections -fPIC -O1")
424 set(CMAKE_CXX_FLAGS_CHECKED "${CMAKE_CXX_FLAGS_CHECKED} ${CLR_SANITIZE_CXX_FLAGS} -fdata-sections -ffunction-sections -fPIC -O1")
426 set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} ${CLR_SANITIZE_LINK_FLAGS}")
427 set(CMAKE_EXE_LINKER_FLAGS_CHECKED "${CMAKE_EXE_LINKER_FLAGS_CHECKED} ${CLR_SANITIZE_LINK_FLAGS}")
429 # -Wl and --gc-sections: drop unused sections\functions (similar to Windows /Gy function-level-linking)
430 set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} ${CLR_SANITIZE_LINK_FLAGS} -Wl,--gc-sections")
431 set(CMAKE_SHARED_LINKER_FLAGS_CHECKED "${CMAKE_SHARED_LINKER_FLAGS_CHECKED} ${CLR_SANITIZE_LINK_FLAGS} -Wl,--gc-sections")
433 endif(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL DEBUG OR UPPERCASE_CMAKE_BUILD_TYPE STREQUAL CHECKED)
436 if(CLR_CMAKE_PLATFORM_LINUX)
437 set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -Wa,--noexecstack")
438 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--build-id=sha1")
439 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--build-id=sha1")
440 endif(CLR_CMAKE_PLATFORM_LINUX)
442 #------------------------------------
443 # Definitions (for platform)
444 #-----------------------------------
445 if (CLR_CMAKE_PLATFORM_ARCH_AMD64)
446 add_definitions(-D_AMD64_)
447 add_definitions(-D_WIN64)
448 add_definitions(-DAMD64)
449 add_definitions(-DBIT64=1)
450 elseif (CLR_CMAKE_PLATFORM_ARCH_I386)
451 add_definitions(-D_X86_)
452 elseif (CLR_CMAKE_PLATFORM_ARCH_ARM)
453 add_definitions(-D_ARM_)
454 add_definitions(-DARM)
455 elseif (CLR_CMAKE_PLATFORM_ARCH_ARM64)
456 add_definitions(-D_ARM64_)
457 add_definitions(-DARM64)
458 add_definitions(-D_WIN64)
459 add_definitions(-DBIT64=1)
464 if (CLR_CMAKE_PLATFORM_UNIX)
465 if(CLR_CMAKE_PLATFORM_LINUX)
466 if(CLR_CMAKE_PLATFORM_UNIX_AMD64)
467 message("Detected Linux x86_64")
468 add_definitions(-DLINUX64)
469 elseif(CLR_CMAKE_PLATFORM_UNIX_ARM)
470 message("Detected Linux ARM")
471 add_definitions(-DLINUX32)
472 elseif(CLR_CMAKE_PLATFORM_UNIX_ARM64)
473 message("Detected Linux ARM64")
474 add_definitions(-DLINUX64)
475 elseif(CLR_CMAKE_PLATFORM_UNIX_X86)
476 message("Detected Linux i686")
477 add_definitions(-DLINUX32)
481 endif(CLR_CMAKE_PLATFORM_LINUX)
482 endif(CLR_CMAKE_PLATFORM_UNIX)
484 if (CLR_CMAKE_PLATFORM_UNIX)
485 add_definitions(-DPLATFORM_UNIX=1)
487 if(CLR_CMAKE_PLATFORM_DARWIN)
488 message("Detected OSX x86_64")
489 endif(CLR_CMAKE_PLATFORM_DARWIN)
491 if(CLR_CMAKE_PLATFORM_FREEBSD)
492 message("Detected FreeBSD amd64")
493 endif(CLR_CMAKE_PLATFORM_FREEBSD)
495 if(CLR_CMAKE_PLATFORM_NETBSD)
496 message("Detected NetBSD amd64")
497 endif(CLR_CMAKE_PLATFORM_NETBSD)
498 endif(CLR_CMAKE_PLATFORM_UNIX)
501 # Define the CRT lib references that link into Desktop imports
502 set(STATIC_MT_CRT_LIB "libcmt$<$<OR:$<CONFIG:Debug>,$<CONFIG:Checked>>:d>.lib")
503 set(STATIC_MT_VCRT_LIB "libvcruntime$<$<OR:$<CONFIG:Debug>,$<CONFIG:Checked>>:d>.lib")
504 set(STATIC_MT_CPP_LIB "libcpmt$<$<OR:$<CONFIG:Debug>,$<CONFIG:Checked>>:d>.lib")
506 # Define the uCRT lib reference
507 set(STATIC_UCRT_LIB "libucrt$<$<OR:$<CONFIG:Debug>,$<CONFIG:Checked>>:d>.lib")
508 set(DYNAMIC_UCRT_LIB "ucrt$<$<OR:$<CONFIG:Debug>,$<CONFIG:Checked>>:d>.lib")
511 # Architecture specific files folder name
512 if (CLR_CMAKE_TARGET_ARCH_AMD64)
513 set(ARCH_SOURCES_DIR amd64)
514 elseif (CLR_CMAKE_TARGET_ARCH_ARM64)
515 set(ARCH_SOURCES_DIR arm64)
516 elseif (CLR_CMAKE_TARGET_ARCH_ARM)
517 set(ARCH_SOURCES_DIR arm)
518 elseif (CLR_CMAKE_TARGET_ARCH_I386)
519 set(ARCH_SOURCES_DIR i386)
524 # Enable for UNIX altjit on Windows - set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
525 # Enable for UNIX altjit on Windows - add_definitions(-DCLR_CMAKE_PLATFORM_UNIX=1)
527 #--------------------------------------
529 #--------------------------------------
530 include(compileoptions.cmake)
532 #----------------------------------------------------
533 # Cross target Component build specific configuration
534 #----------------------------------------------------
535 if(CLR_CROSS_COMPONENTS_BUILD)
536 include(crosscomponents.cmake)
537 endif(CLR_CROSS_COMPONENTS_BUILD)
542 include(pgosupport.cmake)
544 #-----------------------------------------
546 # - project which require platform header not clr's
547 # - do not depend on clr's compile definitions
548 #-----------------------------------------
549 if(CLR_CMAKE_PLATFORM_UNIX AND NOT DEFINED CLR_CROSS_COMPONENTS_BUILD)
550 add_subdirectory(src/corefx)
553 if(CLR_CMAKE_PLATFORM_UNIX)
554 add_subdirectory(src/ToolBox/SOS/lldbplugin)
555 add_subdirectory(src/pal)
556 add_subdirectory(src/coreclr/hosts)
557 add_subdirectory(src/ildasm/unixcoreclrloader)
558 endif(CLR_CMAKE_PLATFORM_UNIX)
560 # Add this subdir. We install the headers for the jit.
561 add_subdirectory(src/pal/prebuilt/inc)
563 add_subdirectory(src/debug/debug-pal)
566 add_subdirectory(src/gc/sample)
569 # Above projects do not build with these compile options
570 # All of the compiler options are specified in file compileoptions.cmake
571 # Do not add any new options here. They shoul be added in compileoptions.cmake
573 add_compile_options(/FIWarningControl.h) # force include of WarningControl.h
574 add_compile_options(/Zl) # omit default library name in .OBJ
577 #-------------------------------------
578 # Include directory directives
579 #-------------------------------------
580 # Include the basic prebuilt headers - required for getting fileversion resource details.
581 include_directories("src/pal/prebuilt/inc")
582 include_directories("bin/obj")
584 if(FEATURE_STANDALONE_GC)
585 add_definitions(-DFEATURE_STANDALONE_GC)
587 if(CLR_CMAKE_PLATFORM_UNIX)
588 add_subdirectory(src/gc/unix)
589 endif(CLR_CMAKE_PLATFORM_UNIX)
590 endif(FEATURE_STANDALONE_GC)
592 if (CLR_CMAKE_PLATFORM_UNIX)
593 include_directories("src/pal/inc")
594 include_directories("src/pal/inc/rt")
595 include_directories("src/pal/src/safecrt")
596 endif (CLR_CMAKE_PLATFORM_UNIX)
598 #--------------------------------
599 # Definition directives
600 # - all clr specific compile definitions should be included in this file
601 # - all clr specific feature variable should also be added in this file
602 #----------------------------------
603 include(clrdefinitions.cmake)
605 # Microsoft.Dotnet.BuildTools.Coreclr version
606 set(BuildToolsVersion "1.0.4-prerelease")
607 set(BuildToolsDir "${CLR_DIR}/packages/Microsoft.DotNet.BuildTools.CoreCLR/${BuildToolsVersion}")
609 #------------------------------
610 # Add Product Directory
611 #------------------------------
612 add_subdirectory(src)
614 #------------------------------
616 #------------------------------
617 if(CLR_CMAKE_BUILD_TESTS)
618 # remove some definitions for test build
619 remove_definitions(-D_SECURE_SCL=0)
620 remove_definitions(-DUNICODE)
621 remove_definitions(-D_UNICODE)
623 add_subdirectory(tests)
624 endif(CLR_CMAKE_BUILD_TESTS)
626 include(definitionsconsistencycheck.cmake)