Merge pull request #9654 from sdmaclea/PR-ARM64-JITGC-WIP2
[platform/upstream/coreclr.git] / CMakeLists.txt
1 # Require at least version 2.8.12 of CMake
2 cmake_minimum_required(VERSION 2.8.12)
3
4 if(CMAKE_VERSION VERSION_EQUAL 3.0 OR CMAKE_VERSION VERSION_GREATER 3.0)
5     cmake_policy(SET CMP0042 NEW)
6 endif()
7
8 # Set the project name
9 project(CoreCLR)
10
11 # Include cmake functions
12 include(functions.cmake)
13
14 if (WIN32)
15   message(STATUS "VS_PLATFORM_TOOLSET is ${CMAKE_VS_PLATFORM_TOOLSET}")
16   message(STATUS "VS_PLATFORM_NAME is ${CMAKE_VS_PLATFORM_NAME}")
17 endif (WIN32)
18
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")
24
25 set(CORECLR_SET_RPATH ON)
26 if(CORECLR_SET_RPATH)
27     # Enable @rpath support for shared libraries.
28     set(MACOSX_RPATH ON)
29 endif(CORECLR_SET_RPATH)
30
31 OPTION(CMAKE_ENABLE_CODE_COVERAGE "Enable code coverage" OFF)
32
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/")
37 endif()
38
39 # Ensure other tools are present
40 if (WIN32)
41     if(CLR_CMAKE_HOST_ARCH STREQUAL arm)
42       
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.")
46       else()
47               message("Using Windows SDK version ${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}")
48       endif()
49
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)
52
53       set(CMAKE_ASM_MASM_COMPILER ${CMAKE_ASM_COMPILER})
54       message("CMAKE_ASM_MASM_COMPILER explicitly set to: ${CMAKE_ASM_MASM_COMPILER}") 
55       
56       # Enable generic assembly compilation to avoid CMake generate VS proj files that explicitly
57       # use ml[64].exe as the assembler.
58       enable_language(ASM)
59     else()
60       enable_language(ASM_MASM)
61     endif()
62
63     # Ensure that MC is present
64     find_program(MC mc)
65     if (MC STREQUAL "MC-NOTFOUND")
66         message(FATAL_ERROR "MC not found")
67     endif()
68
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}) 
77     endif()
78
79 else (WIN32)
80     enable_language(ASM)
81
82     # Ensure that awk is present
83     find_program(AWK awk)
84     if (AWK STREQUAL "AWK-NOTFOUND")
85         message(FATAL_ERROR "AWK not found")
86     endif()
87  
88     if (CMAKE_SYSTEM_NAME STREQUAL Darwin)
89
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")
94       endif()
95
96       find_program(STRIP strip)
97       if (STRIP STREQUAL "STRIP-NOTFOUND")
98           message(FATAL_ERROR "strip not found")
99       endif()
100
101     else (CMAKE_SYSTEM_NAME STREQUAL Darwin)
102
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)
106           find_program(OBJCOPY ${TOOLCHAIN}-objcopy)
107         elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL i686)
108           find_program(OBJCOPY objcopy)
109         else()
110           clr_unknown_arch()
111         endif()
112       else()
113         find_program(OBJCOPY objcopy)
114       endif()
115
116       if (OBJCOPY STREQUAL "OBJCOPY-NOTFOUND")
117           message(FATAL_ERROR "objcopy not found")
118       endif()
119
120     endif (CMAKE_SYSTEM_NAME STREQUAL Darwin)
121 endif(WIN32)
122
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)
135             else()
136                 set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
137             endif()
138         elseif(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL i686)
139             set(CLR_CMAKE_PLATFORM_UNIX_X86 1)
140         else()
141             clr_unknown_arch()
142         endif()
143     else()
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 aarch64)
153             set(CLR_CMAKE_PLATFORM_UNIX_ARM64 1)
154         elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL i686)
155             set(CLR_CMAKE_PLATFORM_UNIX_X86 1)
156         else()
157             clr_unknown_arch()
158         endif()
159     endif()
160     set(CLR_CMAKE_PLATFORM_LINUX 1)
161
162     # Detect Alpine Linux
163     SET(OS_RELEASE_FILENAME "/etc/os-release")
164     if (EXISTS ${OS_RELEASE_FILENAME})
165         file(READ ${OS_RELEASE_FILENAME} OS_RELEASE)
166         string(FIND "${OS_RELEASE}" "ID=alpine" CLR_CMAKE_PLATFORM_ALPINE_LINUX)
167         if(CLR_CMAKE_PLATFORM_ALPINE_LINUX EQUAL -1)
168             unset(CLR_CMAKE_PLATFORM_ALPINE_LINUX)
169         endif(CLR_CMAKE_PLATFORM_ALPINE_LINUX EQUAL -1)
170     endif(EXISTS ${OS_RELEASE_FILENAME})
171 endif(CMAKE_SYSTEM_NAME STREQUAL Linux)
172
173 if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
174   set(CLR_CMAKE_PLATFORM_UNIX 1)
175   set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
176   set(CLR_CMAKE_PLATFORM_DARWIN 1)
177   if(CMAKE_VERSION VERSION_LESS "3.4.0")
178     set(CMAKE_ASM_COMPILE_OBJECT "${CMAKE_C_COMPILER} <FLAGS> <DEFINES> -o <OBJECT> -c <SOURCE>")
179   else()
180     set(CMAKE_ASM_COMPILE_OBJECT "${CMAKE_C_COMPILER} <FLAGS> <DEFINES> <INCLUDES> -o <OBJECT> -c <SOURCE>")
181   endif(CMAKE_VERSION VERSION_LESS "3.4.0")
182 endif(CMAKE_SYSTEM_NAME STREQUAL Darwin)
183
184 if(CMAKE_SYSTEM_NAME STREQUAL FreeBSD)
185   set(CLR_CMAKE_PLATFORM_UNIX 1)
186   set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
187   set(CLR_CMAKE_PLATFORM_FREEBSD 1)
188 endif(CMAKE_SYSTEM_NAME STREQUAL FreeBSD)
189
190 if(CMAKE_SYSTEM_NAME STREQUAL OpenBSD)
191   set(CLR_CMAKE_PLATFORM_UNIX 1)
192   set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
193   set(CLR_CMAKE_PLATFORM_OPENBSD 1)
194 endif(CMAKE_SYSTEM_NAME STREQUAL OpenBSD)
195
196 if(CMAKE_SYSTEM_NAME STREQUAL NetBSD)
197   set(CLR_CMAKE_PLATFORM_UNIX 1)
198   set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
199   set(CLR_CMAKE_PLATFORM_NETBSD 1)
200 endif(CMAKE_SYSTEM_NAME STREQUAL NetBSD)
201
202 if(CMAKE_SYSTEM_NAME STREQUAL SunOS)
203   set(CLR_CMAKE_PLATFORM_UNIX 1)
204   EXECUTE_PROCESS(
205     COMMAND isainfo -n
206     OUTPUT_VARIABLE SUNOS_NATIVE_INSTRUCTION_SET
207     )
208   if(SUNOS_NATIVE_INSTRUCTION_SET MATCHES "amd64")
209     set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
210     set(CMAKE_SYSTEM_PROCESSOR "amd64")
211   else()
212     clr_unknown_arch()
213   endif()
214   set(CLR_CMAKE_PLATFORM_SUNOS 1)
215 endif(CMAKE_SYSTEM_NAME STREQUAL SunOS)
216
217 #--------------------------------------------
218 # This repo builds two set of binaries
219 # 1. binaries which execute on target arch machine
220 #        - for such binaries host architecture & target architecture are same
221 #        - eg. coreclr.dll
222 # 2. binaries which execute on host machine but target another architecture
223 #        - host architecture is different from target architecture
224 #        - eg. crossgen.exe - runs on x64 machine and generates nis targeting arm64
225 #        - for complete list of such binaries refer to file crosscomponents.cmake
226 #-------------------------------------------------------------
227 # Set HOST architecture variables
228 if(CLR_CMAKE_PLATFORM_UNIX_ARM)
229     set(CLR_CMAKE_PLATFORM_ARCH_ARM 1)
230     set(CLR_CMAKE_HOST_ARCH "arm")
231 elseif(CLR_CMAKE_PLATFORM_UNIX_ARM64)
232     set(CLR_CMAKE_PLATFORM_ARCH_ARM64 1)
233     set(CLR_CMAKE_HOST_ARCH "arm64")
234 elseif(CLR_CMAKE_PLATFORM_UNIX_AMD64)
235     set(CLR_CMAKE_PLATFORM_ARCH_AMD64 1)
236     set(CLR_CMAKE_HOST_ARCH "x64")
237 elseif(CLR_CMAKE_PLATFORM_UNIX_X86)
238     set(CLR_CMAKE_PLATFORM_ARCH_I386 1)
239     set(CLR_CMAKE_HOST_ARCH "x86")
240 elseif(WIN32)
241     # CLR_CMAKE_HOST_ARCH is passed in as param to cmake
242     if (CLR_CMAKE_HOST_ARCH STREQUAL x64)
243         set(CLR_CMAKE_PLATFORM_ARCH_AMD64 1)
244     elseif(CLR_CMAKE_HOST_ARCH STREQUAL x86)
245         set(CLR_CMAKE_PLATFORM_ARCH_I386 1)
246     elseif(CLR_CMAKE_HOST_ARCH STREQUAL arm)
247         set(CLR_CMAKE_PLATFORM_ARCH_ARM 1)
248     elseif(CLR_CMAKE_HOST_ARCH STREQUAL arm64)
249         set(CLR_CMAKE_PLATFORM_ARCH_ARM64 1)
250     else()
251         clr_unknown_arch()
252     endif()
253 endif()
254
255 # Set TARGET architecture variables
256 # Target arch will be a cmake param (optional) for both windows as well as non-windows build
257 # if target arch is not specified then host & target are same
258 if(NOT DEFINED CLR_CMAKE_TARGET_ARCH OR CLR_CMAKE_TARGET_ARCH STREQUAL "" )
259   set(CLR_CMAKE_TARGET_ARCH ${CLR_CMAKE_HOST_ARCH})
260 endif()
261
262 # Set target architecture variables
263 if (CLR_CMAKE_TARGET_ARCH STREQUAL x64)
264     set(CLR_CMAKE_TARGET_ARCH_AMD64 1)
265   elseif(CLR_CMAKE_TARGET_ARCH STREQUAL x86)
266     set(CLR_CMAKE_TARGET_ARCH_I386 1)
267   elseif(CLR_CMAKE_TARGET_ARCH STREQUAL arm64)
268     set(CLR_CMAKE_TARGET_ARCH_ARM64 1)
269   elseif(CLR_CMAKE_TARGET_ARCH STREQUAL arm)
270     set(CLR_CMAKE_TARGET_ARCH_ARM 1)
271   else()
272     clr_unknown_arch()
273 endif()
274
275 # check if host & target arch combination are valid
276 if(NOT(CLR_CMAKE_TARGET_ARCH STREQUAL CLR_CMAKE_HOST_ARCH))
277     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)))
278         message(FATAL_ERROR "Invalid host and target arch combination")
279     endif()
280 endif()
281
282 #-----------------------------------------------------
283 # Initialize Cmake compiler flags and other variables
284 #-----------------------------------------------------
285
286 if (CMAKE_CONFIGURATION_TYPES) # multi-configuration generator?
287     set(CMAKE_CONFIGURATION_TYPES "Debug;Checked;Release;RelWithDebInfo" CACHE STRING "" FORCE)
288 endif (CMAKE_CONFIGURATION_TYPES)
289
290 set(CMAKE_C_FLAGS_CHECKED ${CLR_C_FLAGS_CHECKED_INIT} CACHE STRING "Flags used by the compiler during checked builds.")
291 set(CMAKE_CXX_FLAGS_CHECKED ${CLR_CXX_FLAGS_CHECKED_INIT} CACHE STRING "Flags used by the compiler during checked builds.")
292 set(CMAKE_EXE_LINKER_FLAGS_CHECKED "")
293 set(CMAKE_SHARED_LINKER_FLAGS_CHECKED "")
294
295 # Disable the following line for UNIX altjit on Windows
296 set(CMAKE_CXX_STANDARD_LIBRARIES "") # do not link against standard win32 libs i.e. kernel32, uuid, user32, etc.
297
298 if (WIN32)
299   # For multi-configuration toolset (as Visual Studio)
300   # set the different configuration defines.
301   foreach (Config DEBUG CHECKED RELEASE RELWITHDEBINFO)
302     foreach (Definition IN LISTS CLR_DEFINES_${Config}_INIT)
303       set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS $<$<CONFIG:${Config}>:${Definition}>)
304     endforeach (Definition)
305   endforeach (Config)
306
307   set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /guard:cf")
308   set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /guard:cf")
309
310   # Incremental linking with CFG is broken until next VS release.
311   # This needs to be appended to the last for each build type to override the default flag.
312   set(NO_INCREMENTAL_LINKER_FLAGS "/INCREMENTAL:NO")
313
314   # Linker flags
315   #
316   # Disable the following line for UNIX altjit on Windows
317   set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /MANIFEST:NO") #Do not create Side-by-Side Assembly Manifest
318
319   if (CLR_CMAKE_PLATFORM_ARCH_ARM)
320     set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /SUBSYSTEM:WINDOWS,6.02") #windows subsystem - arm minimum is 6.02  
321   elseif(CLR_CMAKE_PLATFORM_ARCH_ARM64)
322     set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /SUBSYSTEM:WINDOWS,6.03") #windows subsystem - arm64 minimum is 6.03  
323   else ()
324     set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /SUBSYSTEM:WINDOWS,6.01") #windows subsystem
325   endif ()
326     
327   set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /LARGEADDRESSAWARE") # can handle addresses larger than 2 gigabytes
328   set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /RELEASE") #sets the checksum in the header
329   set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /NXCOMPAT") #Compatible with Data Execution Prevention
330   set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /DYNAMICBASE") #Use address space layout randomization
331   set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /DEBUGTYPE:cv,fixup") #debugging format
332   set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /PDBCOMPRESS") #shrink pdb size
333   set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /DEBUG")
334   set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /IGNORE:4197,4013,4254,4070,4221")
335
336   set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} /IGNORE:4221")
337
338   set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /DEBUG /PDBCOMPRESS")
339   set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /STACK:1572864")
340
341   # Debug build specific flags
342   set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "/NOVCFEATURE ${NO_INCREMENTAL_LINKER_FLAGS}")
343   set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${NO_INCREMENTAL_LINKER_FLAGS}")
344
345   # Checked build specific flags
346   set(CMAKE_SHARED_LINKER_FLAGS_CHECKED "${CMAKE_SHARED_LINKER_FLAGS_CHECKED} /OPT:REF /OPT:NOICF /NOVCFEATURE ${NO_INCREMENTAL_LINKER_FLAGS}")
347   set(CMAKE_STATIC_LINKER_FLAGS_CHECKED "${CMAKE_STATIC_LINKER_FLAGS_CHECKED}")
348   set(CMAKE_EXE_LINKER_FLAGS_CHECKED "${CMAKE_EXE_LINKER_FLAGS_CHECKED} /OPT:REF /OPT:NOICF ${NO_INCREMENTAL_LINKER_FLAGS}")
349
350   # Release build specific flags
351   set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /LTCG /OPT:REF /OPT:ICF ${NO_INCREMENTAL_LINKER_FLAGS}")
352   set(CMAKE_STATIC_LINKER_FLAGS_RELEASE "${CMAKE_STATIC_LINKER_FLAGS_RELEASE} /LTCG")
353   set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /LTCG /OPT:REF /OPT:ICF ${NO_INCREMENTAL_LINKER_FLAGS}")
354
355   # ReleaseWithDebugInfo build specific flags
356   set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO} /LTCG /OPT:REF /OPT:ICF ${NO_INCREMENTAL_LINKER_FLAGS}")
357   set(CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO} /LTCG")
358   set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} /LTCG /OPT:REF /OPT:ICF ${NO_INCREMENTAL_LINKER_FLAGS}")
359
360   # Temporary until cmake has VS generators for arm64
361   if(CLR_CMAKE_PLATFORM_ARCH_ARM64)
362     set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /machine:arm64")
363     set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} /machine:arm64")
364     set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /machine:arm64")
365   endif(CLR_CMAKE_PLATFORM_ARCH_ARM64)
366
367   # Force uCRT to be dynamically linked for Release build  
368   set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /NODEFAULTLIB:libucrt.lib /DEFAULTLIB:ucrt.lib")  
369   set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /NODEFAULTLIB:libucrt.lib /DEFAULTLIB:ucrt.lib")  
370   set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO} /NODEFAULTLIB:libucrt.lib /DEFAULTLIB:ucrt.lib")  
371   set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} /NODEFAULTLIB:libucrt.lib /DEFAULTLIB:ucrt.lib")  
372
373 elseif (CLR_CMAKE_PLATFORM_UNIX)
374   # Set the values to display when interactively configuring CMAKE_BUILD_TYPE
375   set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "DEBUG;CHECKED;RELEASE;RELWITHDEBINFO")
376
377   # Use uppercase CMAKE_BUILD_TYPE for the string comparisons below
378   string(TOUPPER ${CMAKE_BUILD_TYPE} UPPERCASE_CMAKE_BUILD_TYPE)
379
380   # For single-configuration toolset
381   # set the different configuration defines.
382   if (UPPERCASE_CMAKE_BUILD_TYPE STREQUAL DEBUG)
383     # First DEBUG
384     set_property(DIRECTORY  PROPERTY COMPILE_DEFINITIONS ${CLR_DEFINES_DEBUG_INIT})
385   elseif (UPPERCASE_CMAKE_BUILD_TYPE STREQUAL CHECKED)
386     # Then CHECKED
387     set_property(DIRECTORY PROPERTY COMPILE_DEFINITIONS ${CLR_DEFINES_CHECKED_INIT})
388   elseif (UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELEASE)
389     # Then RELEASE
390     set_property(DIRECTORY PROPERTY COMPILE_DEFINITIONS ${CLR_DEFINES_RELEASE_INIT})
391   elseif (UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELWITHDEBINFO)
392     # And then RELWITHDEBINFO
393     set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS ${CLR_DEFINES_RELWITHDEBINFO_INIT})
394   else ()
395     message(FATAL_ERROR "Unknown build type! Set CMAKE_BUILD_TYPE to DEBUG, CHECKED, RELEASE, or RELWITHDEBINFO!")
396   endif ()
397
398   # set the CLANG sanitizer flags for debug build
399   if(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL DEBUG OR UPPERCASE_CMAKE_BUILD_TYPE STREQUAL CHECKED)
400     # obtain settings from running enablesanitizers.sh
401     string(FIND "$ENV{DEBUG_SANITIZERS}" "asan" __ASAN_POS)
402     string(FIND "$ENV{DEBUG_SANITIZERS}" "ubsan" __UBSAN_POS)
403     if ((${__ASAN_POS} GREATER -1) OR (${__UBSAN_POS} GREATER -1))
404       set(CLR_SANITIZE_CXX_FLAGS "${CLR_SANITIZE_CXX_FLAGS} -fsanitize-blacklist=${CMAKE_CURRENT_SOURCE_DIR}/sanitizerblacklist.txt -fsanitize=")
405       set(CLR_SANITIZE_LINK_FLAGS "${CLR_SANITIZE_LINK_FLAGS} -fsanitize=")
406       if (${__ASAN_POS} GREATER -1)
407         set(CLR_SANITIZE_CXX_FLAGS "${CLR_SANITIZE_CXX_FLAGS}address,")
408         set(CLR_SANITIZE_LINK_FLAGS "${CLR_SANITIZE_LINK_FLAGS}address,")
409         message("Address Sanitizer (asan) enabled")
410       endif ()
411       if (${__UBSAN_POS} GREATER -1)
412         # all sanitizier flags are enabled except alignment (due to heavy use of __unaligned modifier)
413         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")
414         set(CLR_SANITIZE_LINK_FLAGS "${CLR_SANITIZE_LINK_FLAGS}undefined")
415         message("Undefined Behavior Sanitizer (ubsan) enabled")
416       endif ()
417
418       # -fdata-sections -ffunction-sections: each function has own section instead of one per .o file (needed for --gc-sections)
419       # -fPIC: enable Position Independent Code normally just for shared libraries but required when linking with address sanitizer
420       # -O1: optimization level used instead of -O0 to avoid compile error "invalid operand for inline asm constraint"
421       set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${CLR_SANITIZE_CXX_FLAGS} -fdata-sections -ffunction-sections -fPIC -O1")
422       set(CMAKE_CXX_FLAGS_CHECKED "${CMAKE_CXX_FLAGS_CHECKED} ${CLR_SANITIZE_CXX_FLAGS} -fdata-sections -ffunction-sections -fPIC -O1")
423
424       set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} ${CLR_SANITIZE_LINK_FLAGS}")
425       set(CMAKE_EXE_LINKER_FLAGS_CHECKED "${CMAKE_EXE_LINKER_FLAGS_CHECKED} ${CLR_SANITIZE_LINK_FLAGS}")
426
427       # -Wl and --gc-sections: drop unused sections\functions (similar to Windows /Gy function-level-linking)
428       set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} ${CLR_SANITIZE_LINK_FLAGS} -Wl,--gc-sections")
429       set(CMAKE_SHARED_LINKER_FLAGS_CHECKED "${CMAKE_SHARED_LINKER_FLAGS_CHECKED} ${CLR_SANITIZE_LINK_FLAGS} -Wl,--gc-sections")
430     endif ()
431   endif(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL DEBUG OR UPPERCASE_CMAKE_BUILD_TYPE STREQUAL CHECKED)
432 endif(WIN32)
433
434 if(CLR_CMAKE_PLATFORM_LINUX)  
435   set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -Wa,--noexecstack")
436   set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--build-id=sha1")
437   set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--build-id=sha1")
438 endif(CLR_CMAKE_PLATFORM_LINUX)  
439
440 #------------------------------------
441 # Definitions (for platform)
442 #-----------------------------------
443 if (CLR_CMAKE_PLATFORM_ARCH_AMD64)
444   add_definitions(-D_AMD64_)
445   add_definitions(-D_WIN64)
446   add_definitions(-DAMD64)
447   add_definitions(-DBIT64=1)
448 elseif (CLR_CMAKE_PLATFORM_ARCH_I386)
449   add_definitions(-D_X86_)
450 elseif (CLR_CMAKE_PLATFORM_ARCH_ARM)
451   add_definitions(-D_ARM_)
452   add_definitions(-DARM)
453 elseif (CLR_CMAKE_PLATFORM_ARCH_ARM64)
454   add_definitions(-D_ARM64_)
455   add_definitions(-DARM64)
456   add_definitions(-D_WIN64)
457   add_definitions(-DBIT64=1)
458 else ()
459   clr_unknown_arch()
460 endif ()
461
462 if (CLR_CMAKE_PLATFORM_UNIX)
463   if(CLR_CMAKE_PLATFORM_LINUX)
464     if(CLR_CMAKE_PLATFORM_UNIX_AMD64)
465       message("Detected Linux x86_64")
466       add_definitions(-DLINUX64)
467     elseif(CLR_CMAKE_PLATFORM_UNIX_ARM)
468       message("Detected Linux ARM")
469       add_definitions(-DLINUX32)
470     elseif(CLR_CMAKE_PLATFORM_UNIX_ARM64)
471       message("Detected Linux ARM64")
472       add_definitions(-DLINUX64)
473     elseif(CLR_CMAKE_PLATFORM_UNIX_X86)
474       message("Detected Linux i686")
475       add_definitions(-DLINUX32)
476     else()
477       clr_unknown_arch()
478     endif()
479   endif(CLR_CMAKE_PLATFORM_LINUX)
480 endif(CLR_CMAKE_PLATFORM_UNIX)
481
482 if (CLR_CMAKE_PLATFORM_UNIX)
483   add_definitions(-DPLATFORM_UNIX=1)
484
485   if(CLR_CMAKE_PLATFORM_DARWIN)
486     message("Detected OSX x86_64")
487   endif(CLR_CMAKE_PLATFORM_DARWIN)
488
489   if(CLR_CMAKE_PLATFORM_FREEBSD)
490     message("Detected FreeBSD amd64")
491   endif(CLR_CMAKE_PLATFORM_FREEBSD)
492
493   if(CLR_CMAKE_PLATFORM_NETBSD)  
494     message("Detected NetBSD amd64")  
495   endif(CLR_CMAKE_PLATFORM_NETBSD)  
496 endif(CLR_CMAKE_PLATFORM_UNIX)
497
498 if (WIN32)
499   # Define the CRT lib references that link into Desktop imports
500   set(STATIC_MT_CRT_LIB  "libcmt$<$<OR:$<CONFIG:Debug>,$<CONFIG:Checked>>:d>.lib")
501   set(STATIC_MT_VCRT_LIB  "libvcruntime$<$<OR:$<CONFIG:Debug>,$<CONFIG:Checked>>:d>.lib")
502   set(STATIC_MT_CPP_LIB  "libcpmt$<$<OR:$<CONFIG:Debug>,$<CONFIG:Checked>>:d>.lib")
503
504   # Define the uCRT lib reference
505   set(STATIC_UCRT_LIB  "libucrt$<$<OR:$<CONFIG:Debug>,$<CONFIG:Checked>>:d>.lib")
506   set(DYNAMIC_UCRT_LIB  "ucrt$<$<OR:$<CONFIG:Debug>,$<CONFIG:Checked>>:d>.lib")
507 endif(WIN32)
508
509 # Architecture specific files folder name
510 if (CLR_CMAKE_TARGET_ARCH_AMD64)
511     set(ARCH_SOURCES_DIR amd64)
512 elseif (CLR_CMAKE_TARGET_ARCH_ARM64)
513     set(ARCH_SOURCES_DIR arm64)
514 elseif (CLR_CMAKE_TARGET_ARCH_ARM)
515     set(ARCH_SOURCES_DIR arm)
516 elseif (CLR_CMAKE_TARGET_ARCH_I386)
517     set(ARCH_SOURCES_DIR i386)
518 else ()
519     clr_unknown_arch()
520 endif ()
521
522 # Enable for UNIX altjit on Windows - set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
523 # Enable for UNIX altjit on Windows - add_definitions(-DCLR_CMAKE_PLATFORM_UNIX=1)
524
525 #--------------------------------------
526 # Compile Options
527 #--------------------------------------
528 include(compileoptions.cmake)
529
530 #----------------------------------------------------
531 # Cross target Component build specific configuration
532 #----------------------------------------------------
533 if(CLR_CROSS_COMPONENTS_BUILD)
534   include(crosscomponents.cmake)
535 endif(CLR_CROSS_COMPONENTS_BUILD)
536
537 #-------------------
538 # Enable PGO support
539 #-------------------
540 include(pgosupport.cmake)
541
542 #-----------------------------------------
543 # Add Projects
544 #     - project which require platform header not clr's
545 #     - do not depend on clr's compile definitions
546 #-----------------------------------------
547 if(CLR_CMAKE_PLATFORM_UNIX AND NOT DEFINED CLR_CROSS_COMPONENTS_BUILD)
548   add_subdirectory(src/corefx)
549 endif()
550
551 if(CLR_CMAKE_PLATFORM_UNIX)
552     add_subdirectory(src/ToolBox/SOS/lldbplugin)
553     add_subdirectory(src/pal)
554     add_subdirectory(src/coreclr/hosts)
555     add_subdirectory(src/ildasm/unixcoreclrloader)
556 endif(CLR_CMAKE_PLATFORM_UNIX)
557
558 # Add this subdir. We install the headers for the jit.
559 add_subdirectory(src/pal/prebuilt/inc)
560
561 add_subdirectory(src/debug/debug-pal)
562
563 if(WIN32)
564   add_subdirectory(src/gc/sample)
565 endif()
566
567 # Above projects do not build with these compile options
568 # All of the compiler options are specified in file compileoptions.cmake
569 # Do not add any new options here. They shoul be added in compileoptions.cmake
570 if(WIN32)
571   add_compile_options(/FIWarningControl.h) # force include of WarningControl.h
572   add_compile_options(/Zl) # omit default library name in .OBJ
573 endif(WIN32)
574
575 #-------------------------------------
576 # Include directory directives
577 #-------------------------------------
578 # Include the basic prebuilt headers - required for getting fileversion resource details.
579 include_directories("src/pal/prebuilt/inc")
580 include_directories("bin/obj")
581
582 if(FEATURE_STANDALONE_GC)
583   add_definitions(-DFEATURE_STANDALONE_GC)
584
585   if(CLR_CMAKE_PLATFORM_UNIX)
586     add_subdirectory(src/gc/unix)
587   endif(CLR_CMAKE_PLATFORM_UNIX)
588 endif(FEATURE_STANDALONE_GC)
589
590 if (CLR_CMAKE_PLATFORM_UNIX)
591   include_directories("src/pal/inc")
592   include_directories("src/pal/inc/rt")
593   include_directories("src/pal/src/safecrt")
594 endif (CLR_CMAKE_PLATFORM_UNIX)
595
596 #--------------------------------
597 # Definition directives
598 #  - all clr specific compile definitions should be included in this file
599 #  - all clr specific feature variable should also be added in this file
600 #----------------------------------
601 include(clrdefinitions.cmake)
602
603 # Microsoft.Dotnet.BuildTools.Coreclr version
604 set(BuildToolsVersion "1.0.4-prerelease")
605 set(BuildToolsDir "${CLR_DIR}/packages/Microsoft.DotNet.BuildTools.CoreCLR/${BuildToolsVersion}")
606
607 #------------------------------
608 # Add Product Directory
609 #------------------------------
610 add_subdirectory(src)
611
612 #------------------------------
613 # Add Test Directory
614 #------------------------------
615 if(CLR_CMAKE_BUILD_TESTS)
616   # remove some definitions for test build
617   remove_definitions(-D_SECURE_SCL=0)
618   remove_definitions(-DUNICODE)
619   remove_definitions(-D_UNICODE)
620
621   add_subdirectory(tests)
622 endif(CLR_CMAKE_BUILD_TESTS)
623
624 include(definitionsconsistencycheck.cmake)