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