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