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