Merge pull request #5773 from Maoni0/grow_card_2
[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 # Set commonly used directory names
15 set(CLR_DIR ${CMAKE_CURRENT_SOURCE_DIR})
16 set(VM_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src/vm)
17 set(GENERATED_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/src/inc)
18 set(VERSION_FILE_PATH "${CMAKE_BINARY_DIR}/version.cpp")
19
20 set(CORECLR_SET_RPATH ON)
21 if(CORECLR_SET_RPATH)
22     # Enable @rpath support for shared libraries.
23     set(MACOSX_RPATH ON)
24 endif(CORECLR_SET_RPATH)
25
26 OPTION(CMAKE_ENABLE_CODE_COVERAGE "Enable code coverage" OFF)
27
28 # Ensure that python is present
29 find_program(PYTHON python)
30 if (PYTHON STREQUAL "PYTHON-NOTFOUND")
31     message(FATAL_ERROR "PYTHON not found: Please install Python 2.7.9 or later from https://www.python.org/downloads/")
32 endif()
33
34 # Ensure other tools are present
35 if(WIN32)
36     enable_language(ASM_MASM)
37
38     # Ensure that MC is present
39     find_program(MC mc)
40     if (MC STREQUAL "MC-NOTFOUND")
41         message(FATAL_ERROR "MC not found")
42     endif()
43
44     if(CLR_CMAKE_HOST_ARCH STREQUAL arm64)
45       # CMAKE_CXX_COMPILER will default to the compiler installed with 
46       # Visual studio. Overwrite it to the compiler on the path. 
47       # TODO, remove when cmake generator supports Arm64 as a target.  
48       find_program(PATH_CXX_COMPILER cl) 
49       set(CMAKE_CXX_COMPILER ${PATH_CXX_COMPILER})  
50       message("Overwriting the CMAKE_CXX_COMPILER.") 
51       message(CMAKE_CXX_COMPILER found:${CMAKE_CXX_COMPILER}) 
52     endif()
53 else()
54     enable_language(ASM)
55
56     # Ensure that awk is present
57     find_program(AWK awk)
58     if (AWK STREQUAL "AWK-NOTFOUND")
59         message(FATAL_ERROR "AWK not found")
60     endif()
61  
62     if (CMAKE_SYSTEM_NAME STREQUAL Darwin)
63
64       # Ensure that dsymutil and strip is present
65       find_program(DSYMUTIL dsymutil)
66       if (DSYMUTIL STREQUAL "DSYMUTIL-NOTFOUND")
67           message(FATAL_ERROR "dsymutil not found")
68       endif()
69       find_program(STRIP strip)
70       if (STRIP STREQUAL "STRIP-NOTFOUND")
71           message(FATAL_ERROR "strip not found")
72       endif()
73     elseif (CMAKE_SYSTEM_NAME STREQUAL Linux)
74       # Ensure that objcopy is present
75       if(DEFINED ENV{CROSSCOMPILE})
76         if(CMAKE_SYSTEM_PROCESSOR STREQUAL armv7l)
77           find_program(OBJCOPY ${TOOLCHAIN}-objcopy) 
78         elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL aarch64)
79           find_program(OBJCOPY ${TOOLCHAIN}-objcopy)
80         else()
81           clr_unknown_arch()
82         endif()
83       else()
84         find_program(OBJCOPY objcopy)
85       endif()
86       if (OBJCOPY STREQUAL "OBJCOPY-NOTFOUND")
87           message(FATAL_ERROR "objcopy not found")
88       endif()
89     endif ()
90 endif(WIN32)
91
92 #----------------------------------------
93 # Detect and set platform variable names
94 #     - for non-windows build platform & architecture is detected using inbuilt CMAKE variables
95 #     - for windows we use the passed in parameter to CMAKE to determine build arch
96 #----------------------------------------
97 if(CMAKE_SYSTEM_NAME STREQUAL Linux)
98     set(CLR_CMAKE_PLATFORM_UNIX 1)
99     # CMAKE_SYSTEM_PROCESSOR returns the value of `uname -p`.
100     # For the AMD/Intel 64bit architecure two different strings are common.
101     # Linux and Darwin identify it as "x86_64" while FreeBSD and netbsd uses the
102     # "amd64" string. Accept either of the two here.
103     if(CMAKE_SYSTEM_PROCESSOR STREQUAL x86_64 OR CMAKE_SYSTEM_PROCESSOR STREQUAL amd64)
104         set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
105     elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL armv7l)
106         set(CLR_CMAKE_PLATFORM_UNIX_ARM 1)
107     elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL aarch64)
108         set(CLR_CMAKE_PLATFORM_UNIX_ARM64 1)
109     else()
110         clr_unknown_arch()
111     endif()
112     set(CLR_CMAKE_PLATFORM_LINUX 1)
113 endif(CMAKE_SYSTEM_NAME STREQUAL Linux)
114
115 if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
116   set(CLR_CMAKE_PLATFORM_UNIX 1)
117   set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
118   set(CLR_CMAKE_PLATFORM_DARWIN 1)
119   if(CMAKE_VERSION VERSION_LESS "3.4.0")
120     set(CMAKE_ASM_COMPILE_OBJECT "${CMAKE_C_COMPILER} <FLAGS> <DEFINES> -o <OBJECT> -c <SOURCE>")
121   else()
122     set(CMAKE_ASM_COMPILE_OBJECT "${CMAKE_C_COMPILER} <FLAGS> <DEFINES> <INCLUDES> -o <OBJECT> -c <SOURCE>")
123   endif(CMAKE_VERSION VERSION_LESS "3.4.0")
124 endif(CMAKE_SYSTEM_NAME STREQUAL Darwin)
125
126 if(CMAKE_SYSTEM_NAME STREQUAL FreeBSD)
127   set(CLR_CMAKE_PLATFORM_UNIX 1)
128   set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
129   set(CLR_CMAKE_PLATFORM_FREEBSD 1)
130 endif(CMAKE_SYSTEM_NAME STREQUAL FreeBSD)
131
132 if(CMAKE_SYSTEM_NAME STREQUAL OpenBSD)
133   set(CLR_CMAKE_PLATFORM_UNIX 1)
134   set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
135   set(CLR_CMAKE_PLATFORM_OPENBSD 1)
136 endif(CMAKE_SYSTEM_NAME STREQUAL OpenBSD)
137
138 if(CMAKE_SYSTEM_NAME STREQUAL NetBSD)
139   set(CLR_CMAKE_PLATFORM_UNIX 1)
140   set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
141   set(CLR_CMAKE_PLATFORM_NETBSD 1)
142 endif(CMAKE_SYSTEM_NAME STREQUAL NetBSD)
143
144 if(CMAKE_SYSTEM_NAME STREQUAL SunOS)
145   set(CLR_CMAKE_PLATFORM_UNIX 1)
146   EXECUTE_PROCESS(
147     COMMAND isainfo -n
148     OUTPUT_VARIABLE SUNOS_NATIVE_INSTRUCTION_SET
149     )
150   if(SUNOS_NATIVE_INSTRUCTION_SET MATCHES "amd64")
151     set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
152     set(CMAKE_SYSTEM_PROCESSOR "amd64")
153   else()
154     clr_unknown_arch()
155   endif()
156   set(CLR_CMAKE_PLATFORM_SUNOS 1)
157 endif(CMAKE_SYSTEM_NAME STREQUAL SunOS)
158
159 #--------------------------------------------
160 # This repo builds two set of binaries
161 # 1. binaries which execute on target arch machine
162 #        - for such binaries host architecture & target architecture are same
163 #        - eg. coreclr.dll
164 # 2. binaries which execute on host machine but target another architecture
165 #        - host architecture is different from target architecture
166 #        - eg. crossgen.exe - runs on x64 machine and generates nis targeting arm64
167 #        - for complete list of such binaries refer to file crosscomponents.cmake
168 #-------------------------------------------------------------
169 # Set HOST architecture variables
170 if(CLR_CMAKE_PLATFORM_UNIX_ARM)
171   set(CLR_CMAKE_PLATFORM_ARCH_ARM 1)
172   set(CLR_CMAKE_HOST_ARCH "arm")
173 elseif(CLR_CMAKE_PLATFORM_UNIX_ARM64)
174   set(CLR_CMAKE_PLATFORM_ARCH_ARM64 1) 
175   set(CLR_CMAKE_HOST_ARCH "arm64")
176 elseif(CLR_CMAKE_PLATFORM_UNIX_AMD64)
177   set(CLR_CMAKE_PLATFORM_ARCH_AMD64 1)
178   set(CLR_CMAKE_HOST_ARCH "x64")
179 elseif(WIN32)
180   # CLR_CMAKE_HOST_ARCH is passed in as param to cmake
181   if (CLR_CMAKE_HOST_ARCH STREQUAL x64)
182     set(CLR_CMAKE_PLATFORM_ARCH_AMD64 1)
183   elseif(CLR_CMAKE_HOST_ARCH STREQUAL x86)
184     set(CLR_CMAKE_PLATFORM_ARCH_I386 1)
185   elseif(CLR_CMAKE_HOST_ARCH STREQUAL arm64)
186     set(CLR_CMAKE_PLATFORM_ARCH_ARM64 1)
187   else()
188     clr_unknown_arch()
189   endif()
190 endif()
191
192 # Set TARGET architecture variables
193 # Target arch will be a cmake param (optional) for both windows as well as non-windows build
194 # if target arch is not specified then host & target are same
195 if(NOT DEFINED CLR_CMAKE_TARGET_ARCH OR CLR_CMAKE_TARGET_ARCH STREQUAL "" )
196   set(CLR_CMAKE_TARGET_ARCH ${CLR_CMAKE_HOST_ARCH})
197 endif()
198
199 # Set target architecture variables
200 if (CLR_CMAKE_TARGET_ARCH STREQUAL x64)
201     set(CLR_CMAKE_TARGET_ARCH_AMD64 1)
202   elseif(CLR_CMAKE_TARGET_ARCH STREQUAL x86)
203     set(CLR_CMAKE_TARGET_ARCH_I386 1)
204   elseif(CLR_CMAKE_TARGET_ARCH STREQUAL arm64)
205     set(CLR_CMAKE_TARGET_ARCH_ARM64 1)
206   elseif(CLR_CMAKE_TARGET_ARCH STREQUAL arm)
207     set(CLR_CMAKE_TARGET_ARCH_ARM 1)
208   else()
209     clr_unknown_arch()
210 endif()
211
212 # check if host & target arch combination are valid
213 if(NOT(CLR_CMAKE_TARGET_ARCH STREQUAL CLR_CMAKE_HOST_ARCH))
214   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)))
215     message(FATAL_ERROR "Invalid host and target arch combination")
216   endif()
217 endif()
218
219 #-----------------------------------------------------
220 # Initialize Cmake compiler flags and other variables
221 #-----------------------------------------------------
222
223 if (CMAKE_CONFIGURATION_TYPES) # multi-configuration generator?
224     set(CMAKE_CONFIGURATION_TYPES "Debug;Checked;Release;RelWithDebInfo" CACHE STRING "" FORCE)
225 endif (CMAKE_CONFIGURATION_TYPES)
226
227 set(CMAKE_C_FLAGS_CHECKED ${CLR_C_FLAGS_CHECKED_INIT} CACHE STRING "Flags used by the compiler during checked builds.")
228 set(CMAKE_CXX_FLAGS_CHECKED ${CLR_CXX_FLAGS_CHECKED_INIT} CACHE STRING "Flags used by the compiler during checked builds.")
229 set(CMAKE_EXE_LINKER_FLAGS_CHECKED "")
230 set(CMAKE_SHARED_LINKER_FLAGS_CHECKED "")
231
232 # Disable the following line for UNIX altjit on Windows
233 set(CMAKE_CXX_STANDARD_LIBRARIES "") # do not link against standard win32 libs i.e. kernel32, uuid, user32, etc.
234
235 if (WIN32)
236   # For multi-configuration toolset (as Visual Studio)
237   # set the different configuration defines.
238   foreach (Config DEBUG CHECKED RELEASE RELWITHDEBINFO)
239     foreach (Definition IN LISTS CLR_DEFINES_${Config}_INIT)
240       set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS $<$<CONFIG:${Config}>:${Definition}>)
241     endforeach (Definition)
242   endforeach (Config)
243
244   if(NOT CLR_CMAKE_PLATFORM_ARCH_ARM64)
245     set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /guard:cf")
246     set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /guard:cf")
247   endif (NOT CLR_CMAKE_PLATFORM_ARCH_ARM64)
248
249   # Incremental linking with CFG is broken until next VS release.
250   # This needs to be appended to the last for each build type to override the default flag.
251   set(NO_INCREMENTAL_LINKER_FLAGS "/INCREMENTAL:NO")
252
253   # Linker flags
254   #
255   # Disable the following line for UNIX altjit on Windows
256   set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /MANIFEST:NO") #Do not create Side-by-Side Assembly Manifest
257   set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /SUBSYSTEM:WINDOWS,6.00") #windows subsystem
258   set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /LARGEADDRESSAWARE") # can handle addresses larger than 2 gigabytes
259   set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /RELEASE") #sets the checksum in the header
260   set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /NXCOMPAT") #Compatible with Data Execution Prevention
261   set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /DYNAMICBASE") #Use address space layout randomization
262   set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /DEBUGTYPE:cv,fixup") #debugging format
263   set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /PDBCOMPRESS") #shrink pdb size
264   set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /DEBUG")
265   set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /IGNORE:4197,4013,4254,4070,4221")
266
267   set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} /IGNORE:4221")
268
269   set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /DEBUG /PDBCOMPRESS")
270   set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /STACK:1572864")
271
272   # Debug build specific flags
273   set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "/NOVCFEATURE ${NO_INCREMENTAL_LINKER_FLAGS}")
274   set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${NO_INCREMENTAL_LINKER_FLAGS}")
275
276   # Checked build specific flags
277   set(CMAKE_SHARED_LINKER_FLAGS_CHECKED "${CMAKE_SHARED_LINKER_FLAGS_CHECKED} /OPT:REF /OPT:NOICF /NOVCFEATURE ${NO_INCREMENTAL_LINKER_FLAGS}")
278   set(CMAKE_STATIC_LINKER_FLAGS_CHECKED "${CMAKE_STATIC_LINKER_FLAGS_CHECKED}")
279   set(CMAKE_EXE_LINKER_FLAGS_CHECKED "${CMAKE_EXE_LINKER_FLAGS_CHECKED} /OPT:REF /OPT:NOICF ${NO_INCREMENTAL_LINKER_FLAGS}")
280
281   # Release build specific flags
282   set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /LTCG /OPT:REF /OPT:ICF ${NO_INCREMENTAL_LINKER_FLAGS}")
283   set(CMAKE_STATIC_LINKER_FLAGS_RELEASE "${CMAKE_STATIC_LINKER_FLAGS_RELEASE} /LTCG")
284   set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /LTCG /OPT:REF /OPT:ICF ${NO_INCREMENTAL_LINKER_FLAGS}")
285
286   # ReleaseWithDebugInfo build specific flags
287   set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO} /LTCG /OPT:REF /OPT:ICF ${NO_INCREMENTAL_LINKER_FLAGS}")
288   set(CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO} /LTCG")
289   set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} /LTCG /OPT:REF /OPT:ICF ${NO_INCREMENTAL_LINKER_FLAGS}")
290
291   # Temporary until cmake has VS generators for arm64
292   if(CLR_CMAKE_PLATFORM_ARCH_ARM64)
293     set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /machine:arm64")
294     set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} /machine:arm64")
295     set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /machine:arm64")
296   endif(CLR_CMAKE_PLATFORM_ARCH_ARM64)
297
298   # Force uCRT to be dynamically linked for Release build  
299   set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /NODEFAULTLIB:libucrt.lib /DEFAULTLIB:ucrt.lib")  
300   set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /NODEFAULTLIB:libucrt.lib /DEFAULTLIB:ucrt.lib")  
301   set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO} /NODEFAULTLIB:libucrt.lib /DEFAULTLIB:ucrt.lib")  
302   set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} /NODEFAULTLIB:libucrt.lib /DEFAULTLIB:ucrt.lib")  
303
304 elseif (CLR_CMAKE_PLATFORM_UNIX)
305   # Set the values to display when interactively configuring CMAKE_BUILD_TYPE
306   set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "DEBUG;CHECKED;RELEASE;RELWITHDEBINFO")
307
308   # Use uppercase CMAKE_BUILD_TYPE for the string comparisons below
309   string(TOUPPER ${CMAKE_BUILD_TYPE} UPPERCASE_CMAKE_BUILD_TYPE)
310
311   # For single-configuration toolset
312   # set the different configuration defines.
313   if (UPPERCASE_CMAKE_BUILD_TYPE STREQUAL DEBUG)
314     # First DEBUG
315     set_property(DIRECTORY  PROPERTY COMPILE_DEFINITIONS ${CLR_DEFINES_DEBUG_INIT})
316   elseif (UPPERCASE_CMAKE_BUILD_TYPE STREQUAL CHECKED)
317     # Then CHECKED
318     set_property(DIRECTORY PROPERTY COMPILE_DEFINITIONS ${CLR_DEFINES_CHECKED_INIT})
319   elseif (UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELEASE)
320     # Then RELEASE
321     set_property(DIRECTORY PROPERTY COMPILE_DEFINITIONS ${CLR_DEFINES_RELEASE_INIT})
322   elseif (UPPERCASE_CMAKE_BUILD_TYPE STREQUAL RELWITHDEBINFO)
323     # And then RELWITHDEBINFO
324     set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS ${CLR_DEFINES_RELWITHDEBINFO_INIT})
325   else ()
326     message(FATAL_ERROR "Unknown build type! Set CMAKE_BUILD_TYPE to DEBUG, CHECKED, RELEASE, or RELWITHDEBINFO!")
327   endif ()
328
329   # set the CLANG sanitizer flags for debug build
330   if(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL DEBUG OR UPPERCASE_CMAKE_BUILD_TYPE STREQUAL CHECKED)
331     # obtain settings from running enablesanitizers.sh
332     string(FIND "$ENV{DEBUG_SANITIZERS}" "asan" __ASAN_POS)
333     string(FIND "$ENV{DEBUG_SANITIZERS}" "ubsan" __UBSAN_POS)
334     if ((${__ASAN_POS} GREATER -1) OR (${__UBSAN_POS} GREATER -1))
335       set(CLR_SANITIZE_CXX_FLAGS "${CLR_SANITIZE_CXX_FLAGS} -fsanitize-blacklist=${CMAKE_CURRENT_SOURCE_DIR}/sanitizerblacklist.txt -fsanitize=")
336       set(CLR_SANITIZE_LINK_FLAGS "${CLR_SANITIZE_LINK_FLAGS} -fsanitize=")
337       if (${__ASAN_POS} GREATER -1)
338         set(CLR_SANITIZE_CXX_FLAGS "${CLR_SANITIZE_CXX_FLAGS}address,")
339         set(CLR_SANITIZE_LINK_FLAGS "${CLR_SANITIZE_LINK_FLAGS}address,")
340         message("Address Sanitizer (asan) enabled")
341       endif ()
342       if (${__UBSAN_POS} GREATER -1)
343         # all sanitizier flags are enabled except alignment (due to heavy use of __unaligned modifier)
344         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")
345         set(CLR_SANITIZE_LINK_FLAGS "${CLR_SANITIZE_LINK_FLAGS}undefined")
346         message("Undefined Behavior Sanitizer (ubsan) enabled")
347       endif ()
348
349       # -fdata-sections -ffunction-sections: each function has own section instead of one per .o file (needed for --gc-sections)
350       # -fPIC: enable Position Independent Code normally just for shared libraries but required when linking with address sanitizer
351       # -O1: optimization level used instead of -O0 to avoid compile error "invalid operand for inline asm constraint"
352       set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${CLR_SANITIZE_CXX_FLAGS} -fdata-sections -ffunction-sections -fPIC -O1")
353       set(CMAKE_CXX_FLAGS_CHECKED "${CMAKE_CXX_FLAGS_CHECKED} ${CLR_SANITIZE_CXX_FLAGS} -fdata-sections -ffunction-sections -fPIC -O1")
354
355       set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} ${CLR_SANITIZE_LINK_FLAGS}")
356       set(CMAKE_EXE_LINKER_FLAGS_CHECKED "${CMAKE_EXE_LINKER_FLAGS_CHECKED} ${CLR_SANITIZE_LINK_FLAGS}")
357
358       # -Wl and --gc-sections: drop unused sections\functions (similar to Windows /Gy function-level-linking)
359       set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} ${CLR_SANITIZE_LINK_FLAGS} -Wl,--gc-sections")
360       set(CMAKE_SHARED_LINKER_FLAGS_CHECKED "${CMAKE_SHARED_LINKER_FLAGS_CHECKED} ${CLR_SANITIZE_LINK_FLAGS} -Wl,--gc-sections")
361     endif ()
362   endif(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL DEBUG OR UPPERCASE_CMAKE_BUILD_TYPE STREQUAL CHECKED)
363 endif(WIN32)
364
365 if(CLR_CMAKE_PLATFORM_LINUX)  
366   set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -Wa,--noexecstack")  
367 endif(CLR_CMAKE_PLATFORM_LINUX)  
368
369 #------------------------------------
370 # Definitions (for platform)
371 #-----------------------------------
372 if (CLR_CMAKE_PLATFORM_ARCH_AMD64)
373   add_definitions(-D_AMD64_)
374   add_definitions(-D_WIN64)
375   add_definitions(-DAMD64)
376   add_definitions(-DBIT64=1)
377 elseif (CLR_CMAKE_PLATFORM_ARCH_I386)
378   add_definitions(-D_X86_)
379 elseif (CLR_CMAKE_PLATFORM_ARCH_ARM)
380   add_definitions(-D_ARM_)
381   add_definitions(-DARM)
382 elseif (CLR_CMAKE_PLATFORM_ARCH_ARM64)
383   add_definitions(-D_ARM64_)
384   add_definitions(-DARM64)
385   add_definitions(-D_WIN64)
386   add_definitions(-DBIT64=1)
387 else ()
388   clr_unknown_arch()
389 endif ()
390
391 if (CLR_CMAKE_PLATFORM_UNIX)
392   if(CLR_CMAKE_PLATFORM_LINUX)
393     if(CLR_CMAKE_PLATFORM_UNIX_AMD64)
394       message("Detected Linux x86_64")
395       add_definitions(-DLINUX64)
396     elseif(CLR_CMAKE_PLATFORM_UNIX_ARM)
397       message("Detected Linux ARM")
398       add_definitions(-DLINUX32)
399     elseif(CLR_CMAKE_PLATFORM_UNIX_ARM64)
400       message("Detected Linux ARM64")
401       add_definitions(-DLINUX64)
402     else()
403       clr_unknown_arch()
404     endif()
405   endif(CLR_CMAKE_PLATFORM_LINUX)
406 endif(CLR_CMAKE_PLATFORM_UNIX)
407
408 if (CLR_CMAKE_PLATFORM_UNIX)
409   add_definitions(-DPLATFORM_UNIX=1)
410
411   if(CLR_CMAKE_PLATFORM_DARWIN)
412     message("Detected OSX x86_64")
413   endif(CLR_CMAKE_PLATFORM_DARWIN)
414
415   if(CLR_CMAKE_PLATFORM_FREEBSD)
416     message("Detected FreeBSD amd64")
417   endif(CLR_CMAKE_PLATFORM_FREEBSD)
418
419   if(CLR_CMAKE_PLATFORM_NETBSD)  
420     message("Detected NetBSD amd64")  
421   endif(CLR_CMAKE_PLATFORM_NETBSD)  
422 endif(CLR_CMAKE_PLATFORM_UNIX)
423
424 if (WIN32)
425   # Define the CRT lib references that link into Desktop imports
426   set(STATIC_MT_CRT_LIB  "libcmt$<$<OR:$<CONFIG:Debug>,$<CONFIG:Checked>>:d>.lib")
427   set(STATIC_MT_VCRT_LIB  "libvcruntime$<$<OR:$<CONFIG:Debug>,$<CONFIG:Checked>>:d>.lib")
428   set(STATIC_MT_CPP_LIB  "libcpmt$<$<OR:$<CONFIG:Debug>,$<CONFIG:Checked>>:d>.lib")
429
430   # Define the uCRT lib reference
431   set(STATIC_UCRT_LIB  "libucrt$<$<OR:$<CONFIG:Debug>,$<CONFIG:Checked>>:d>.lib")
432   set(DYNAMIC_UCRT_LIB  "ucrt$<$<OR:$<CONFIG:Debug>,$<CONFIG:Checked>>:d>.lib")
433 endif(WIN32)
434
435 # Architecture specific files folder name
436 if (CLR_CMAKE_TARGET_ARCH_AMD64)
437     set(ARCH_SOURCES_DIR amd64)
438 elseif (CLR_CMAKE_TARGET_ARCH_ARM64)
439     set(ARCH_SOURCES_DIR arm64)
440 elseif (CLR_CMAKE_TARGET_ARCH_ARM)
441     set(ARCH_SOURCES_DIR arm)
442 elseif (CLR_CMAKE_TARGET_ARCH_I386)
443     set(ARCH_SOURCES_DIR i386)
444 else ()
445     clr_unknown_arch()
446 endif ()
447
448 # Enable for UNIX altjit on Windows - set(CLR_CMAKE_PLATFORM_UNIX_AMD64 1)
449 # Enable for UNIX altjit on Windows - add_definitions(-DCLR_CMAKE_PLATFORM_UNIX=1)
450
451 #--------------------------------------
452 # Compile Options
453 #--------------------------------------
454 include(compileoptions.cmake)
455
456 #----------------------------------------------------
457 # Cross target Component build specific configuration
458 #----------------------------------------------------
459 if(CLR_CROSS_COMPONENTS_BUILD)
460   include(crosscomponents.cmake)
461 endif(CLR_CROSS_COMPONENTS_BUILD)
462
463 #-----------------------------------------
464 # Add Projects
465 #     - project which require platform header not clr's
466 #     - do not depend on clr's compile definitions
467 #-----------------------------------------
468 if(CLR_CMAKE_PLATFORM_UNIX)
469   add_subdirectory(src/corefx)
470 endif(CLR_CMAKE_PLATFORM_UNIX)
471
472 if(CLR_CMAKE_PLATFORM_UNIX)
473   add_subdirectory(src/ToolBox/SOS/lldbplugin)
474   if(NOT DEFINED CLR_CROSS_COMPONENTS_BUILD)
475     add_subdirectory(src/pal)
476   endif(NOT DEFINED CLR_CROSS_COMPONENTS_BUILD)
477   add_subdirectory(src/coreclr/hosts)
478   add_subdirectory(src/ildasm/unixcoreclrloader)
479 endif(CLR_CMAKE_PLATFORM_UNIX)
480
481 # Add this subdir. We install the headers for the jit.
482 add_subdirectory(src/pal/prebuilt/inc)
483
484 add_subdirectory(src/debug/debug-pal)
485
486 if(WIN32)
487   add_subdirectory(src/gc/sample)
488 endif()
489
490 # Above projects do not build with these compile options
491 # All of the compiler options are specified in file compileoptions.cmake
492 # Do not add any new options here. They shoul be added in compileoptions.cmake
493 if(WIN32)
494   add_compile_options(/FIWarningControl.h) # force include of WarningControl.h
495   add_compile_options(/Zl) # omit default library name in .OBJ
496 endif(WIN32)
497
498 #-------------------------------------
499 # Include directory directives
500 #-------------------------------------
501 # Include the basic prebuilt headers - required for getting fileversion resource details.
502 include_directories("src/pal/prebuilt/inc")
503 include_directories("bin/obj")
504
505 if (CLR_CMAKE_PLATFORM_UNIX)
506   include_directories("src/pal/inc")
507   include_directories("src/pal/inc/rt")
508   include_directories("src/pal/src/safecrt")
509 endif (CLR_CMAKE_PLATFORM_UNIX)
510
511 #--------------------------------
512 # Definition directives
513 #  - all clr specific compile definitions should be included in this file
514 #  - all clr specific feature variable should also be added in this file
515 #----------------------------------
516 include(clrdefinitions.cmake)
517
518 # Microsoft.Dotnet.BuildTools.Coreclr version
519 set(BuildToolsVersion "1.0.4-prerelease")
520 set(BuildToolsDir "${CLR_DIR}/packages/Microsoft.DotNet.BuildTools.CoreCLR/${BuildToolsVersion}")
521
522 #------------------------------
523 # Add Product Directory
524 #------------------------------
525 add_subdirectory(src)
526
527 #------------------------------
528 # Add Test Directory
529 #------------------------------
530 if(CLR_CMAKE_BUILD_TESTS)
531   # remove some definitions for test build
532   remove_definitions(-D_SECURE_SCL=0)
533   remove_definitions(-DUNICODE)
534   remove_definitions(-D_UNICODE)
535
536   add_subdirectory(tests)
537 endif(CLR_CMAKE_BUILD_TESTS)
538
539 #----------------------------------------------------
540 # Build the project again for cross target components
541 #    - intermediates will be placed at %__IntermediatesDir%\crosscomponents
542 #    - final binaries will be placed at %__CMakeBinDir%\<hostArch>
543 #----------------------------------------------------
544
545 if(CLR_CMAKE_PLATFORM_ARCH_ARM64 AND WIN32)
546   # Cross target component build only enabled for win arm64
547   set(CLR_CROSS_COMPONENTS_BUILD_ENABLED 1)
548 endif()
549
550 # To avoid recursion when building cross target components
551 if(NOT DEFINED CLR_CROSS_COMPONENTS_BUILD AND CLR_CROSS_COMPONENTS_BUILD_ENABLED)
552
553   # Set host arch for cross target components
554   if(CLR_CMAKE_PLATFORM_ARCH_ARM64)
555     set(CLR_CROSS_BUILD_HOST_ARCH x64)
556   elseif(CLR_CMAKE_PLATFORM_ARCH_ARM)
557     set(CLR_CROSS_BUILD_HOST_ARCH x86)
558   endif()
559
560   include(ExternalProject)
561
562   # Add the source root again as external project but with CLR_CROSS_COMPONENTS_BUILD flag set
563   ExternalProject_Add(
564     crosscomponents
565     SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}
566     # Arm64 builds currently pollute the env by setting private toolset dirs. Get rid of that.
567     CMAKE_COMMAND "$ENV{__VSToolsRoot}\\..\\..\\VC\\vcvarsall.bat" COMMAND cmake
568     CMAKE_ARGS -DCLR_CROSS_COMPONENTS_BUILD=1
569                -DCMAKE_INSTALL_PREFIX:PATH=$ENV{__CMakeBinDir}/${CLR_CROSS_BUILD_HOST_ARCH}
570                -DCMAKE_USER_MAKE_RULES_OVERRIDE=${CLR_DIR}/src/pal/tools/windows-compiler-override.txt
571                -DCLR_CMAKE_HOST_ARCH=${CLR_CROSS_BUILD_HOST_ARCH}
572                -DCLR_CMAKE_TARGET_ARCH=${CLR_CMAKE_HOST_ARCH}
573     BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/crosscomponents
574     INSTALL_DIR $ENV{__CMakeBinDir}/${CLR_CROSS_BUILD_HOST_ARCH}
575   )
576 endif()