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