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