configurecompiler.cmake 25.5 KB
Newer Older
1 2
include(${CMAKE_CURRENT_LIST_DIR}/configuretools.cmake)

3 4 5
# Set initial flags for each configuration

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
6 7 8 9
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
10

11 12
include(CheckCXXCompilerFlag)

13
# "configureoptimization.cmake" must be included after CLR_CMAKE_HOST_UNIX has been set.
14 15
include(${CMAKE_CURRENT_LIST_DIR}/configureoptimization.cmake)

16 17 18 19
#-----------------------------------------------------
# Initialize Cmake compiler flags and other variables
#-----------------------------------------------------

20
if (CLR_CMAKE_HOST_UNIX)
21
    add_compile_options(-g)
22 23
    add_compile_options(-Wall)
    if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
24 25
        add_compile_options(-Wno-null-conversion)
    else()
26
        add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-Werror=conversion-null>)
27 28 29
    endif()
endif()

30 31 32 33
if (CMAKE_CONFIGURATION_TYPES) # multi-configuration generator?
    set(CMAKE_CONFIGURATION_TYPES "Debug;Checked;Release;RelWithDebInfo" CACHE STRING "" FORCE)
endif (CMAKE_CONFIGURATION_TYPES)

34 35
set(CMAKE_C_FLAGS_CHECKED "")
set(CMAKE_CXX_FLAGS_CHECKED "")
36 37 38
set(CMAKE_EXE_LINKER_FLAGS_CHECKED "")
set(CMAKE_SHARED_LINKER_FLAGS_CHECKED "")

39 40 41 42 43 44 45
set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "")
set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "")
set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "")
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "")
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "")
set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "")

46 47
add_compile_definitions("$<$<OR:$<CONFIG:DEBUG>,$<CONFIG:CHECKED>>:DEBUG;_DEBUG;_DBG;URTBLDENV_FRIENDLY=Checked;BUILDENV_CHECKED=1>")
add_compile_definitions("$<$<OR:$<CONFIG:RELEASE>,$<CONFIG:RELWITHDEBINFO>>:NDEBUG;URTBLDENV_FRIENDLY=Retail>")
48

49
if (MSVC)
50
  add_linker_flag(/GUARD:CF)
51 52 53

  # Linker flags
  #
54
  set (WINDOWS_SUBSYSTEM_VERSION 6.01)
55

56
  if (CLR_CMAKE_HOST_ARCH_ARM)
57
    set(WINDOWS_SUBSYSTEM_VERSION 6.02) #windows subsystem - arm minimum is 6.02
58
  elseif(CLR_CMAKE_HOST_ARCH_ARM64)
59
    set(WINDOWS_SUBSYSTEM_VERSION 6.03) #windows subsystem - arm64 minimum is 6.03
60 61
  endif ()

62
  #Do not create Side-by-Side Assembly Manifest
63
  set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /MANIFEST:NO")
64
  # can handle addresses larger than 2 gigabytes
65
  set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /LARGEADDRESSAWARE")
66
  #Compatible with Data Execution Prevention
67
  set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /NXCOMPAT")
68
  #Use address space layout randomization
69
  set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /DYNAMICBASE")
70
  #shrink pdb size
71
  set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /PDBCOMPRESS")
72

73 74 75
  set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /DEBUG")
  set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /IGNORE:4197,4013,4254,4070,4221")
  set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /SUBSYSTEM:WINDOWS,${WINDOWS_SUBSYSTEM_VERSION}")
76 77

  set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} /IGNORE:4221")
78

79 80 81
  set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /DEBUG")
  set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /PDBCOMPRESS")
  set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /STACK:1572864")
82 83

  # Debug build specific flags
84 85
  set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} /NOVCFEATURE")
  set(CMAKE_SHARED_LINKER_FLAGS_CHECKED "${CMAKE_SHARED_LINKER_FLAGS_CHECKED} /NOVCFEATURE")
86 87

  # Checked build specific flags
88 89 90
  add_linker_flag(/INCREMENTAL:NO CHECKED) # prevent "warning LNK4075: ignoring '/INCREMENTAL' due to '/OPT:REF' specification"
  add_linker_flag(/OPT:REF CHECKED)
  add_linker_flag(/OPT:NOICF CHECKED)
91 92

  # Release build specific flags
93 94 95 96
  add_linker_flag(/LTCG RELEASE)
  add_linker_flag(/OPT:REF RELEASE)
  add_linker_flag(/OPT:ICF RELEASE)
  add_linker_flag(/INCREMENTAL:NO RELEASE)
97 98 99
  set(CMAKE_STATIC_LINKER_FLAGS_RELEASE "${CMAKE_STATIC_LINKER_FLAGS_RELEASE} /LTCG")

  # ReleaseWithDebugInfo build specific flags
100 101 102
  add_linker_flag(/LTCG RELWITHDEBINFO)
  add_linker_flag(/OPT:REF RELWITHDEBINFO)
  add_linker_flag(/OPT:ICF RELWITHDEBINFO)
103 104 105
  set(CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO} /LTCG")

  # Force uCRT to be dynamically linked for Release build
106 107
  add_linker_flag(/NODEFAULTLIB:libucrt.lib RELEASE)
  add_linker_flag(/DEFAULTLIB:ucrt.lib RELEASE)
108

109
elseif (CLR_CMAKE_HOST_UNIX)
110 111 112 113 114 115
  # Set the values to display when interactively configuring CMAKE_BUILD_TYPE
  set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "DEBUG;CHECKED;RELEASE;RELWITHDEBINFO")

  # Use uppercase CMAKE_BUILD_TYPE for the string comparisons below
  string(TOUPPER ${CMAKE_BUILD_TYPE} UPPERCASE_CMAKE_BUILD_TYPE)

116 117
  set(CLR_SANITIZE_CXX_OPTIONS "")
  set(CLR_SANITIZE_LINK_OPTIONS "")
118 119 120 121 122 123 124

  # set the CLANG sanitizer flags for debug build
  if(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL DEBUG OR UPPERCASE_CMAKE_BUILD_TYPE STREQUAL CHECKED)
    # obtain settings from running enablesanitizers.sh
    string(FIND "$ENV{DEBUG_SANITIZERS}" "asan" __ASAN_POS)
    string(FIND "$ENV{DEBUG_SANITIZERS}" "ubsan" __UBSAN_POS)
    if ((${__ASAN_POS} GREATER -1) OR (${__UBSAN_POS} GREATER -1))
125 126 127
      list(APPEND CLR_SANITIZE_CXX_OPTIONS -fsanitize-blacklist=${CMAKE_CURRENT_SOURCE_DIR}/sanitizerblacklist.txt)
      set (CLR_CXX_SANITIZERS "")
      set (CLR_LINK_SANITIZERS "")
128
      if (${__ASAN_POS} GREATER -1)
129 130
        list(APPEND CLR_CXX_SANITIZERS address)
        list(APPEND CLR_LINK_SANITIZERS address)
131 132 133 134 135 136 137
        set(CLR_SANITIZE_CXX_FLAGS "${CLR_SANITIZE_CXX_FLAGS}address,")
        set(CLR_SANITIZE_LINK_FLAGS "${CLR_SANITIZE_LINK_FLAGS}address,")
        add_definitions(-DHAS_ASAN)
        message("Address Sanitizer (asan) enabled")
      endif ()
      if (${__UBSAN_POS} GREATER -1)
        # all sanitizier flags are enabled except alignment (due to heavy use of __unaligned modifier)
138
        list(APPEND CLR_CXX_SANITIZERS
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
          "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)
        list(APPEND CLR_LINK_SANITIZERS
          undefined)
157 158
        message("Undefined Behavior Sanitizer (ubsan) enabled")
      endif ()
159 160 161 162
      list(JOIN CLR_CXX_SANITIZERS "," CLR_CXX_SANITIZERS_OPTIONS)
      list(APPEND CLR_SANITIZE_CXX_OPTIONS "-fsanitize=${CLR_CXX_SANITIZERS_OPTIONS}")
      list(JOIN CLR_LINK_SANITIZERS "," CLR_LINK_SANITIZERS_OPTIONS)
      list(APPEND CLR_SANITIZE_LINK_OPTIONS "-fsanitize=${CLR_LINK_SANITIZERS_OPTIONS}")
163 164 165

      # -fdata-sections -ffunction-sections: each function has own section instead of one per .o file (needed for --gc-sections)
      # -O1: optimization level used instead of -O0 to avoid compile error "invalid operand for inline asm constraint"
166 167
      add_compile_options("$<$<OR:$<CONFIG:DEBUG>,$<CONFIG:CHECKED>>:${CLR_SANITIZE_CXX_OPTIONS};-fdata-sections;--ffunction-sections;-O1>")
      add_linker_flag("${CLR_SANITIZE_LINK_OPTIONS}" DEBUG CHECKED)
168
      # -Wl and --gc-sections: drop unused sections\functions (similar to Windows /Gy function-level-linking)
169
      add_linker_flag("-Wl,--gc-sections" DEBUG CHECKED)
170 171
    endif ()
  endif(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL DEBUG OR UPPERCASE_CMAKE_BUILD_TYPE STREQUAL CHECKED)
172
endif(MSVC)
173 174 175 176 177 178 179

# CLR_ADDITIONAL_LINKER_FLAGS - used for passing additional arguments to linker
# CLR_ADDITIONAL_COMPILER_OPTIONS - used for passing additional arguments to compiler
#
# For example:
#       ./build-native.sh cmakeargs "-DCLR_ADDITIONAL_COMPILER_OPTIONS=<...>" cmakeargs "-DCLR_ADDITIONAL_LINKER_FLAGS=<...>"
#
180
if(CLR_CMAKE_HOST_UNIX)
181 182 183
  foreach(ADDTL_LINKER_FLAG ${CLR_ADDITIONAL_LINKER_FLAGS})
    add_linker_flag(${ADDTL_LINKER_FLAG})
  endforeach()
184
endif(CLR_CMAKE_HOST_UNIX)
185

186
if(CLR_CMAKE_HOST_LINUX)
187
  add_compile_options($<$<COMPILE_LANGUAGE:ASM>:-Wa,--noexecstack>)
188 189
  add_linker_flag(-Wl,--build-id=sha1)
  add_linker_flag(-Wl,-z,relro,-z,now)
190
elseif(CLR_CMAKE_HOST_FREEBSD)
191
  add_compile_options($<$<COMPILE_LANGUAGE:ASM>:-Wa,--noexecstack>)
192
  add_linker_flag("-Wl,--build-id=sha1")
193
elseif(CLR_CMAKE_HOST_SUNOS)
A
Adeel 已提交
194
  add_compile_options($<$<COMPILE_LANGUAGE:ASM>:-Wa,--noexecstack>)
195 196
  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fstack-protector")
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fstack-protector")
A
Adeel 已提交
197
  add_definitions(-D__EXTENSIONS__)
198
endif()
199 200 201 202

#------------------------------------
# Definitions (for platform)
#-----------------------------------
203
if (CLR_CMAKE_HOST_ARCH_AMD64)
204
  set(ARCH_HOST_NAME x64)
205 206
  add_definitions(-DHOST_AMD64)
  add_definitions(-DHOST_64BIT)
207
elseif (CLR_CMAKE_HOST_ARCH_I386)
208
  set(ARCH_HOST_NAME x86)
209
  add_definitions(-DHOST_X86)
210
elseif (CLR_CMAKE_HOST_ARCH_ARM)
211
  set(ARCH_HOST_NAME arm)
212
  add_definitions(-DHOST_ARM)
213
elseif (CLR_CMAKE_HOST_ARCH_ARM64)
214
  set(ARCH_HOST_NAME arm64)
215 216
  add_definitions(-DHOST_ARM64)
  add_definitions(-DHOST_64BIT)
217 218 219 220
else ()
  clr_unknown_arch()
endif ()

221 222 223
if (CLR_CMAKE_HOST_UNIX)
  if(CLR_CMAKE_HOST_LINUX)
    if(CLR_CMAKE_HOST_UNIX_AMD64)
224
      message("Detected Linux x86_64")
225
    elseif(CLR_CMAKE_HOST_UNIX_ARM)
226
      message("Detected Linux ARM")
227
    elseif(CLR_CMAKE_HOST_UNIX_ARM64)
228
      message("Detected Linux ARM64")
229
    elseif(CLR_CMAKE_HOST_UNIX_X86)
230 231 232 233
      message("Detected Linux i686")
    else()
      clr_unknown_arch()
    endif()
234 235
  endif(CLR_CMAKE_HOST_LINUX)
endif(CLR_CMAKE_HOST_UNIX)
236

237
if (CLR_CMAKE_HOST_UNIX)
238
  add_definitions(-DHOST_UNIX)
239

240
  if(CLR_CMAKE_HOST_OSX)
241 242 243 244 245 246 247
    if(CLR_CMAKE_HOST_UNIX_AMD64)
      message("Detected OSX x86_64")
    elseif(CLR_CMAKE_HOST_UNIX_ARM64)
      message("Detected OSX ARM64")
    else()
      clr_unknown_arch()
    endif()
248
  elseif(CLR_CMAKE_HOST_FREEBSD)
249
    message("Detected FreeBSD amd64")
250
  elseif(CLR_CMAKE_HOST_NETBSD)
251
    message("Detected NetBSD amd64")
252 253
  elseif(CLR_CMAKE_HOST_SUNOS)
    message("Detected SunOS amd64")
254
  endif(CLR_CMAKE_HOST_OSX)
255
endif(CLR_CMAKE_HOST_UNIX)
256

257
if (CLR_CMAKE_HOST_WIN32)
258
  add_definitions(-DHOST_WINDOWS)
259

260 261 262 263
  # Define the CRT lib references that link into Desktop imports
  set(STATIC_MT_CRT_LIB  "libcmt$<$<OR:$<CONFIG:Debug>,$<CONFIG:Checked>>:d>.lib")
  set(STATIC_MT_VCRT_LIB  "libvcruntime$<$<OR:$<CONFIG:Debug>,$<CONFIG:Checked>>:d>.lib")
  set(STATIC_MT_CPP_LIB  "libcpmt$<$<OR:$<CONFIG:Debug>,$<CONFIG:Checked>>:d>.lib")
264
endif(CLR_CMAKE_HOST_WIN32)
265 266 267 268

# Architecture specific files folder name
if (CLR_CMAKE_TARGET_ARCH_AMD64)
    set(ARCH_SOURCES_DIR amd64)
269 270 271
    set(ARCH_TARGET_NAME x64)
    add_compile_definitions($<$<NOT:$<BOOL:$<TARGET_PROPERTY:IGNORE_DEFAULT_TARGET_ARCH>>>:TARGET_AMD64>)
    add_compile_definitions($<$<NOT:$<BOOL:$<TARGET_PROPERTY:IGNORE_DEFAULT_TARGET_ARCH>>>:TARGET_64BIT>)
272 273
elseif (CLR_CMAKE_TARGET_ARCH_ARM64)
    set(ARCH_SOURCES_DIR arm64)
274 275 276
    set(ARCH_TARGET_NAME arm64)
    add_compile_definitions($<$<NOT:$<BOOL:$<TARGET_PROPERTY:IGNORE_DEFAULT_TARGET_ARCH>>>:TARGET_ARM64>)
    add_compile_definitions($<$<NOT:$<BOOL:$<TARGET_PROPERTY:IGNORE_DEFAULT_TARGET_ARCH>>>:TARGET_64BIT>)
277 278
elseif (CLR_CMAKE_TARGET_ARCH_ARM)
    set(ARCH_SOURCES_DIR arm)
279 280
    set(ARCH_TARGET_NAME arm)
    add_compile_definitions($<$<NOT:$<BOOL:$<TARGET_PROPERTY:IGNORE_DEFAULT_TARGET_ARCH>>>:TARGET_ARM>)
281
elseif (CLR_CMAKE_TARGET_ARCH_I386)
282
    set(ARCH_TARGET_NAME x86)
283
    set(ARCH_SOURCES_DIR i386)
284
    add_compile_definitions($<$<NOT:$<BOOL:$<TARGET_PROPERTY:IGNORE_DEFAULT_TARGET_ARCH>>>:TARGET_X86>)
285 286 287 288 289 290 291
else ()
    clr_unknown_arch()
endif ()

#--------------------------------------
# Compile Options
#--------------------------------------
292
if (CLR_CMAKE_HOST_UNIX)
293 294 295 296
  # Disable frame pointer optimizations so profilers can get better call stacks
  add_compile_options(-fno-omit-frame-pointer)

  # The -fms-extensions enable the stuff like __if_exists, __declspec(uuid()), etc.
J
Jan Kotas 已提交
297
  add_compile_options(-fms-extensions)
298 299 300 301 302 303 304
  #-fms-compatibility      Enable full Microsoft Visual C++ compatibility
  #-fms-extensions         Accept some non-standard constructs supported by the Microsoft compiler

  # Make signed arithmetic overflow of addition, subtraction, and multiplication wrap around
  # using twos-complement representation (this is normally undefined according to the C++ spec).
  add_compile_options(-fwrapv)

305
  if(CLR_CMAKE_HOST_OSX)
306 307 308
    # We cannot enable "stack-protector-strong" on OS X due to a bug in clang compiler (current version 7.0.2)
    add_compile_options(-fstack-protector)
  else()
309 310 311 312
    check_cxx_compiler_flag(-fstack-protector-strong COMPILER_SUPPORTS_F_STACK_PROTECTOR_STRONG)
    if (COMPILER_SUPPORTS_F_STACK_PROTECTOR_STRONG)
      add_compile_options(-fstack-protector-strong)
    endif()
313
  endif(CLR_CMAKE_HOST_OSX)
314

315 316
  # Suppress warnings-as-errors in release branches to reduce servicing churn
  if (PRERELEASE)
317
    add_compile_options(-Werror)
318
  endif(PRERELEASE)
319

320 321 322 323 324 325
  # Disabled common warnings
  add_compile_options(-Wno-unused-variable)
  add_compile_options(-Wno-unused-value)
  add_compile_options(-Wno-unused-function)

  #These seem to indicate real issues
326
  add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-Wno-invalid-offsetof>)
327

328
  if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
329 330
    # The -ferror-limit is helpful during the porting, it makes sure the compiler doesn't stop
    # after hitting just about 20 errors.
331 332 333 334 335 336 337 338 339 340 341 342
    add_compile_options(-ferror-limit=4096)

    # Disabled warnings
    add_compile_options(-Wno-unused-private-field)
    # Explicit constructor calls are not supported by clang (this->ClassName::ClassName())
    add_compile_options(-Wno-microsoft)
    # This warning is caused by comparing 'this' to NULL
    add_compile_options(-Wno-tautological-compare)
    # There are constants of type BOOL used in a condition. But BOOL is defined as int
    # and so the compiler thinks that there is a mistake.
    add_compile_options(-Wno-constant-logical-operand)
    # We use pshpack1/2/4/8.h and poppack.h headers to set and restore packing. However
343
    # clang 6.0 complains when the packing change lifetime is not contained within
344 345 346 347 348 349 350 351 352 353 354
    # a header file.
    add_compile_options(-Wno-pragma-pack)

    add_compile_options(-Wno-unknown-warning-option)

    # The following warning indicates that an attribute __attribute__((__ms_struct__)) was applied
    # to a struct or a class that has virtual members or a base class. In that case, clang
    # may not generate the same object layout as MSVC.
    add_compile_options(-Wno-incompatible-ms-struct)
  else()
    add_compile_options(-Wno-unused-but-set-variable)
S
Sinan Kaya 已提交
355
    add_compile_options(-Wno-unknown-pragmas)
356 357 358 359 360 361 362
    add_compile_options(-Wno-uninitialized)
    add_compile_options(-Wno-strict-aliasing)
    add_compile_options(-Wno-array-bounds)
    check_cxx_compiler_flag(-Wclass-memaccess COMPILER_SUPPORTS_W_CLASS_MEMACCESS)
    if (COMPILER_SUPPORTS_W_CLASS_MEMACCESS)
      add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-Wno-class-memaccess>)
    endif()
363
    check_cxx_compiler_flag(-faligned-new COMPILER_SUPPORTS_F_ALIGNED_NEW)
A
Adeel 已提交
364
    if (COMPILER_SUPPORTS_F_ALIGNED_NEW)
365
      add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-faligned-new>)
A
Adeel 已提交
366
    endif()
367
  endif()
368 369

  # Some architectures (e.g., ARM) assume char type is unsigned while CoreCLR assumes char is signed
370
  # as x64 does. It has been causing issues in ARM (https://github.com/dotnet/runtime/issues/5778)
371
  add_compile_options(-fsigned-char)
372

373 374 375
  # We mark the function which needs exporting with DLLEXPORT
  add_compile_options(-fvisibility=hidden)

376
  # Specify the minimum supported version of macOS
377
  if(CLR_CMAKE_HOST_OSX)
378
    set(MACOS_VERSION_MIN_FLAGS -mmacosx-version-min=10.13)
379
    add_compile_options(${MACOS_VERSION_MIN_FLAGS})
380
    add_linker_flag(${MACOS_VERSION_MIN_FLAGS})
381
  endif(CLR_CMAKE_HOST_OSX)
382
endif(CLR_CMAKE_HOST_UNIX)
383

384
if(CLR_CMAKE_TARGET_UNIX)
385
  add_compile_definitions($<$<NOT:$<BOOL:$<TARGET_PROPERTY:IGNORE_DEFAULT_TARGET_OS>>>:TARGET_UNIX>)
386 387
  # Contracts are disabled on UNIX.
  add_definitions(-DDISABLE_CONTRACTS)
388
  if(CLR_CMAKE_TARGET_OSX)
389
    add_compile_definitions($<$<NOT:$<BOOL:$<TARGET_PROPERTY:IGNORE_DEFAULT_TARGET_OS>>>:TARGET_OSX>)
A
Adeel 已提交
390
  elseif(CLR_CMAKE_TARGET_FREEBSD)
391
    add_compile_definitions($<$<NOT:$<BOOL:$<TARGET_PROPERTY:IGNORE_DEFAULT_TARGET_OS>>>:TARGET_FREEBSD>)
A
Adeel 已提交
392
  elseif(CLR_CMAKE_TARGET_LINUX)
393
    add_compile_definitions($<$<NOT:$<BOOL:$<TARGET_PROPERTY:IGNORE_DEFAULT_TARGET_OS>>>:TARGET_LINUX>)
A
Adeel 已提交
394
  elseif(CLR_CMAKE_TARGET_NETBSD)
395
    add_compile_definitions($<$<NOT:$<BOOL:$<TARGET_PROPERTY:IGNORE_DEFAULT_TARGET_OS>>>:TARGET_NETBSD>)
396
  elseif(CLR_CMAKE_TARGET_SUNOS)
397
    add_compile_definitions($<$<NOT:$<BOOL:$<TARGET_PROPERTY:IGNORE_DEFAULT_TARGET_OS>>>:TARGET_SUNOS>)
A
Adeel 已提交
398
  elseif(CLR_CMAKE_TARGET_ANDROID)
399
    add_compile_definitions($<$<NOT:$<BOOL:$<TARGET_PROPERTY:IGNORE_DEFAULT_TARGET_OS>>>:TARGET_ANDROID>)
400
  endif()
401
else(CLR_CMAKE_TARGET_UNIX)
402
  add_compile_definitions($<$<NOT:$<BOOL:$<TARGET_PROPERTY:IGNORE_DEFAULT_TARGET_OS>>>:TARGET_WINDOWS>)
403 404
endif(CLR_CMAKE_TARGET_UNIX)

405
if(CLR_CMAKE_HOST_UNIX_ARM)
S
Sinan Kaya 已提交
406 407 408 409
   if (NOT DEFINED CLR_ARM_FPU_TYPE)
     set(CLR_ARM_FPU_TYPE vfpv3)
   endif(NOT DEFINED CLR_ARM_FPU_TYPE)

410 411 412
   # Because we don't use CMAKE_C_COMPILER/CMAKE_CXX_COMPILER to use clang
   # we have to set the triple by adding a compiler argument
   add_compile_options(-mthumb)
S
Sinan Kaya 已提交
413 414 415 416 417
   add_compile_options(-mfpu=${CLR_ARM_FPU_TYPE})
   if (NOT DEFINED CLR_ARM_FPU_CAPABILITY)
     set(CLR_ARM_FPU_CAPABILITY 0x7)
   endif(NOT DEFINED CLR_ARM_FPU_CAPABILITY)
   add_definitions(-DCLR_ARM_FPU_CAPABILITY=${CLR_ARM_FPU_CAPABILITY})
418 419 420 421 422
   add_compile_options(-march=armv7-a)
   if(ARM_SOFTFP)
     add_definitions(-DARM_SOFTFP)
     add_compile_options(-mfloat-abi=softfp)
   endif(ARM_SOFTFP)
423
endif(CLR_CMAKE_HOST_UNIX_ARM)
424

N
Next Turn 已提交
425 426 427 428
if(CLR_CMAKE_HOST_UNIX_X86)
  add_compile_options(-msse2)
endif()

429
if(CLR_CMAKE_HOST_UNIX)
430
  add_compile_options(${CLR_ADDITIONAL_COMPILER_OPTIONS})
431
endif(CLR_CMAKE_HOST_UNIX)
432

433
if (MSVC)
434 435 436 437 438 439 440 441 442 443 444
  # Compile options for targeting windows

  add_compile_options(/TP) # compile all files as C++
  add_compile_options(/nologo) # Suppress Startup Banner
  add_compile_options(/W3) # set warning level to 3
  add_compile_options(/WX) # treat warnings as errors
  add_compile_options(/Oi) # enable intrinsics
  add_compile_options(/Oy-) # disable suppressing of the creation of frame pointers on the call stack for quicker function calls
  add_compile_options(/Gm-) # disable minimal rebuild
  add_compile_options(/Zp8) # pack structs on 8-byte boundary
  add_compile_options(/Gy) # separate functions for linker
445
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /GR-") # disable C++ RTTI
446 447 448
  add_compile_options(/FC) # use full pathnames in diagnostics
  add_compile_options(/MP) # Build with Multiple Processes (number of processes equal to the number of processors)
  add_compile_options(/Zm200) # Specify Precompiled Header Memory Allocation Limit of 150MB
449
  add_compile_options(/Zc:strictStrings) # Disable string-literal to char* or wchar_t* conversion
450

451
  add_compile_options(/wd4960 /wd4961 /wd4603 /wd4627 /wd4838 /wd4456 /wd4457 /wd4458 /wd4459 /wd4091 /we4640)
452 453 454

  # Disable Warnings:
  # 4291: Delete not defined for new, c++ exception may cause leak.
455 456
  # 5105: Windows SDK headers use 'defined' operator in some macros
  add_compile_options(/wd4291 /wd5105)
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474

  # Treat Warnings as Errors:
  # 4007: 'main' : must be __cdecl.
  # 4013: 'function' undefined - assuming extern returning int.
  # 4102: "'%$S' : unreferenced label".
  # 4551: Function call missing argument list.
  # 4700: Local used w/o being initialized.
  # 4806: Unsafe operation involving type 'bool'.
  add_compile_options(/we4007 /we4013 /we4102 /we4551 /we4700 /we4806)

  # Set Warning Level 3:
  # 4092: Sizeof returns 'unsigned long'.
  # 4121: Structure is sensitive to alignment.
  # 4125: Decimal digit in octal sequence.
  # 4130: Logical operation on address of string constant.
  # 4132: Const object should be initialized.
  # 4212: Function declaration used ellipsis.
  # 4530: C++ exception handler used, but unwind semantics are not enabled. Specify -GX.
475 476
  # 35038: data member 'member1' will be initialized after data member 'member2'.
  add_compile_options(/w34092 /w34121 /w34125 /w34130 /w34132 /w34212 /w34530 /w35038)
477 478 479 480 481

  # Set Warning Level 4:
  # 4177: Pragma data_seg s/b at global scope.
  add_compile_options(/w44177)

482 483 484 485
  add_compile_options(/Zi) # enable debugging information
  add_compile_options(/ZH:SHA_256) # use SHA256 for generating hashes of compiler processed source files.
  add_compile_options(/source-charset:utf-8) # Force MSVC to compile source as UTF-8.

486
  if (CLR_CMAKE_HOST_ARCH_I386)
487
    add_compile_options(/Gz)
488
  endif (CLR_CMAKE_HOST_ARCH_I386)
489 490 491 492

  add_compile_options($<$<OR:$<CONFIG:Release>,$<CONFIG:Relwithdebinfo>>:/GL>)
  add_compile_options($<$<OR:$<OR:$<CONFIG:Release>,$<CONFIG:Relwithdebinfo>>,$<CONFIG:Checked>>:/O1>)

493
  if (CLR_CMAKE_HOST_ARCH_AMD64)
494 495
  # The generator expression in the following command means that the /homeparams option is added only for debug builds
  add_compile_options($<$<CONFIG:Debug>:/homeparams>) # Force parameters passed in registers to be written to the stack
496
  endif (CLR_CMAKE_HOST_ARCH_AMD64)
497 498

  # enable control-flow-guard support for native components for non-Arm64 builds
499 500
  # Added using variables instead of add_compile_options to let individual projects override it
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /guard:cf")
501
  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /guard:cf")
502

503 504 505 506 507 508
  # Statically linked CRT (libcmt[d].lib, libvcruntime[d].lib and libucrt[d].lib) by default. This is done to avoid
  # linking in VCRUNTIME140.DLL for a simplified xcopy experience by reducing the dependency on VC REDIST.
  #
  # For Release builds, we shall dynamically link into uCRT [ucrtbase.dll] (which is pushed down as a Windows Update on downlevel OS) but
  # wont do the same for debug/checked builds since ucrtbased.dll is not redistributable and Debug/Checked builds are not
  # production-time scenarios.
509

510 511 512 513
  add_compile_options($<$<OR:$<OR:$<CONFIG:Release>,$<CONFIG:Relwithdebinfo>>,$<BOOL:$<TARGET_PROPERTY:DAC_COMPONENT>>>:/MT>)
  add_compile_options($<$<AND:$<OR:$<CONFIG:Debug>,$<CONFIG:Checked>>,$<NOT:$<BOOL:$<TARGET_PROPERTY:DAC_COMPONENT>>>>:/MTd>)

  add_compile_options($<$<COMPILE_LANGUAGE:ASM_MASM>:/ZH:SHA_256>)
514

515 516 517
  if (CLR_CMAKE_TARGET_ARCH_ARM OR CLR_CMAKE_TARGET_ARCH_ARM64)
    # Contracts work too slow on ARM/ARM64 DEBUG/CHECKED.
    add_definitions(-DDISABLE_CONTRACTS)
518 519
  endif (CLR_CMAKE_TARGET_ARCH_ARM OR CLR_CMAKE_TARGET_ARCH_ARM64)

520
endif (MSVC)
521 522 523

if(CLR_CMAKE_ENABLE_CODE_COVERAGE)

524
  if(CLR_CMAKE_HOST_UNIX)
525 526 527 528 529 530 531
    string(TOUPPER ${CMAKE_BUILD_TYPE} UPPERCASE_CMAKE_BUILD_TYPE)
    if(NOT UPPERCASE_CMAKE_BUILD_TYPE STREQUAL DEBUG)
      message( WARNING "Code coverage results with an optimised (non-Debug) build may be misleading" )
    endif(NOT UPPERCASE_CMAKE_BUILD_TYPE STREQUAL DEBUG)

    add_compile_options(-fprofile-arcs)
    add_compile_options(-ftest-coverage)
532
    add_linker_flag(--coverage)
533 534
  else()
    message(FATAL_ERROR "Code coverage builds not supported on current platform")
535
  endif(CLR_CMAKE_HOST_UNIX)
536 537

endif(CLR_CMAKE_ENABLE_CODE_COVERAGE)
538 539 540

if (CMAKE_BUILD_TOOL STREQUAL nmake)
  set(CMAKE_RC_CREATE_SHARED_LIBRARY "${CMAKE_CXX_CREATE_SHARED_LIBRARY}")
541
endif(CMAKE_BUILD_TOOL STREQUAL nmake)
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598

# Ensure other tools are present
if (CLR_CMAKE_HOST_WIN32)
    if(CLR_CMAKE_HOST_ARCH_ARM)

      # Confirm that Windows SDK is present
      if(NOT DEFINED CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION OR CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION STREQUAL "" )
              message(FATAL_ERROR "Windows SDK is required for the Arm32 build.")
      else()
              message("Using Windows SDK version ${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}")
      endif()

      # Explicitly specify the assembler to be used for Arm32 compile
      file(TO_CMAKE_PATH "$ENV{VCToolsInstallDir}\\bin\\HostX86\\arm\\armasm.exe" CMAKE_ASM_COMPILER)

      set(CMAKE_ASM_MASM_COMPILER ${CMAKE_ASM_COMPILER})
      message("CMAKE_ASM_MASM_COMPILER explicitly set to: ${CMAKE_ASM_MASM_COMPILER}")

      # Enable generic assembly compilation to avoid CMake generate VS proj files that explicitly
      # use ml[64].exe as the assembler.
      enable_language(ASM)
    elseif(CLR_CMAKE_HOST_ARCH_ARM64)

      # Confirm that Windows SDK is present
      if(NOT DEFINED CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION OR CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION STREQUAL "" )
              message(FATAL_ERROR "Windows SDK is required for the ARM64 build.")
      else()
              message("Using Windows SDK version ${CMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION}")
      endif()

      # Explicitly specify the assembler to be used for Arm64 compile
      file(TO_CMAKE_PATH "$ENV{VCToolsInstallDir}\\bin\\HostX86\\arm64\\armasm64.exe" CMAKE_ASM_COMPILER)

      set(CMAKE_ASM_MASM_COMPILER ${CMAKE_ASM_COMPILER})
      message("CMAKE_ASM_MASM_COMPILER explicitly set to: ${CMAKE_ASM_MASM_COMPILER}")

      # Enable generic assembly compilation to avoid CMake generate VS proj files that explicitly
      # use ml[64].exe as the assembler.
      enable_language(ASM)
    else()
      enable_language(ASM_MASM)
    endif()

    # Ensure that MC is present
    find_program(MC mc)
    if (MC STREQUAL "MC-NOTFOUND")
        message(FATAL_ERROR "MC not found")
    endif()

else (CLR_CMAKE_HOST_WIN32)
    enable_language(ASM)

    # Ensure that awk is present
    find_program(AWK awk)
    if (AWK STREQUAL "AWK-NOTFOUND")
        message(FATAL_ERROR "AWK not found")
    endif()
599 600 601 602 603 604 605 606 607 608 609 610 611 612

    # detect linker
    set(ldVersion ${CMAKE_C_COMPILER};-Wl,--version)
    execute_process(COMMAND ${ldVersion}
        ERROR_QUIET
        OUTPUT_VARIABLE ldVersionOutput)

    if("${ldVersionOutput}" MATCHES "GNU ld" OR "${ldVersionOutput}" MATCHES "GNU gold")
        set(LD_GNU 1)
    elseif("${ldVersionOutput}" MATCHES "Solaris Link")
        set(LD_SOLARIS 1)
    else(CLR_CMAKE_HOST_OSX)
        set(LD_OSX 1)
    endif()
613
endif(CLR_CMAKE_HOST_WIN32)