# Copyright (c) 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import("//build/config/chrome_build.gni")
import("//build/config/c++/c++.gni")
import("//build/config/clang/clang.gni")
import("//build/config/compiler/compiler.gni")
import("//build/config/sanitizers/sanitizers.gni")
import("//build/config/win/visual_studio_version.gni")
import("//build/toolchain/goma.gni")
import("//build/toolchain/toolchain.gni")

assert(is_win)

declare_args() {
  # Set this to true to enable static analysis through Visual Studio's
  # /analyze. This dramatically slows compiles and reports thousands of
  # warnings, so normally this is done on a build machine and only the new
  # warnings are examined.
  use_vs_code_analysis = false

  # Turn this on to have the linker output extra timing information.
  win_linker_timing = false

  use_incremental_linking = (is_debug || is_component_build)

  # possible values for target_winuwp_version:
  #   "10" - Windows UWP 10
  #   "8.1" - Windows RT 8.1
  #   "8.0" - Windows RT 8.0
  target_winuwp_version = "10"

  # possible values:
  #   "app" - Windows Store Applications
  #   "phone" - Windows Phone Applications
  #   "system" - Windows Drivers and Tools
  #   "server" - Windows Server Applications
  #   "desktop" - Windows Desktop Applications
  target_winuwp_family = "app"
}

# This is included by reference in the //build/config/compiler config that
# is applied to all targets. It is here to separate out the logic that is
# Windows-only.
config("compiler") {
  if (current_cpu == "x86") {
    asmflags = [
      # When /safeseh is specified, the linker will only produce an image if it
      # can also produce a table of the image's safe exception handlers. This
      # table specifies for the operating system which exception handlers are
      # valid for the image. Note that /SAFESEH isn't accepted on the command
      # line, only /safeseh. This is only accepted by ml.exe, not ml64.exe.
      "/safeseh",
    ]
  }

  cflags = [
    "/Gy",  # Enable function-level linking.
    "/FS",  # Preserve previous PDB behavior.
    "/bigobj",  # Some of our files are bigger than the regular limits.
  ]

  # Force C/C++ mode for the given GN detected file type. This is necessary
  # for precompiled headers where the same source file is compiled in both
  # modes.
  cflags_c = [ "/TC" ]
  cflags_cc = [ "/TP" ]

  cflags += [
    # Tell the compiler to crash on failures. This is undocumented
    # and unsupported but very handy.
    "/d2FastFail",

    # Work around crbug.com/526851, bug in VS 2015 RTM compiler.
    "/Zc:sizedDealloc-",
  ]

  if (is_clang) {
    cflags += [ "-fmsc-version=1911" ]

    if (current_cpu == "x86") {
      cflags += [ "-m32" ]
    } else {
      cflags += [ "-m64" ]
    }

    if (exec_script("//build/win/use_ansi_codes.py", [], "trim string") ==
        "True") {
      cflags += [
        # cmd.exe doesn't understand ANSI escape codes by default,
        # so only enable them if something emulating them is around.
        "-fansi-escape-codes",
      ]
    }

    # Clang runtime libraries, such as the sanitizer runtimes, live here.
    lib_dirs = [ "$clang_base_path/lib/clang/$clang_version/lib/windows" ]
  }

  if (use_lld && !use_thin_lto && (is_clang || !use_goma)) {
    # /Brepro lets the compiler not write the mtime field in the .obj output.
    # link.exe /incremental relies on this field to work correctly, but lld
    # never looks at this timestamp, so it's safe to pass this flag with
    # lld and get more deterministic compiler output in return.
    # In LTO builds, the compiler doesn't write .obj files containing mtimes,
    # so /Brepro is ignored there.
    cflags += [ "/Brepro" ]
  }

  if (!is_debug && !is_component_build) {
    # Enable standard linker optimizations like GC (/OPT:REF) and ICF in static
    # release builds. These are implied by /PROFILE below, but /PROFILE is
    # incompatible with /debug:fastlink and LLD ignores it as of this writing.
    # Release builds always want these optimizations, so enable them explicitly.
    ldflags = [
      "/OPT:REF",
      "/OPT:ICF",
      "/INCREMENTAL:NO",
      "/FIXED:NO",
    ]

    if (use_lld) {
      # String tail merging leads to smaller binaries, but they don't compress
      # as well, leading to increased mini_installer size (crbug.com/838449).
      ldflags += [ "/OPT:NOLLDTAILMERGE" ]
    }

    # TODO(siggi): Is this of any use anymore?
    # /PROFILE ensures that the PDB file contains FIXUP information (growing the
    # PDB file by about 5%) but does not otherwise alter the output binary. It
    # is enabled opportunistically for builds where it is not prohibited (not
    # supported when incrementally linking, or using /debug:fastlink).
    if (!is_win_fastlink) {
      ldflags += [ "/PROFILE" ]
    }
  }

  # arflags apply only to static_libraries. The normal linker configs are only
  # set for executable and shared library targets so arflags must be set
  # elsewhere. Since this is relatively contained, we just apply them in this
  # more general config and they will only have an effect on static libraries.
  arflags = [
    # "No public symbols found; archive member will be inaccessible." This
    # means that one or more object files in the library can never be
    # pulled in to targets that link to this library. It's just a warning that
    # the source file is a no-op.
    "/ignore:4221",
  ]
}

config("vs_code_analysis") {
  if (use_vs_code_analysis && !is_clang) {
    # When use_vs_code_analysis is specified add the /analyze switch to enable
    # static analysis. Specifying /analyze:WX- says that /analyze warnings
    # should not be treated as errors.
    cflags = [ "/analyze:WX-" ]

    # Also, disable various noisy warnings that have low value.
    cflags += [
      "/wd6011",  # Dereferencing NULL pointer

      # C6285 is ~16% of raw warnings and has low value
      "/wd6285",  # non-zero constant || non-zero constant
      "/wd6308",  # realloc might return null pointer

      # Possible infinite loop: use of the constant
      # EXCEPTION_CONTINUE_EXECUTION in the exception-filter
      "/wd6312",

      "/wd6322",  # Empty _except block
      "/wd6330",  # 'char' used instead of 'unsigned char' for istype() call

      # C6334 is ~80% of raw warnings and has low value
      "/wd6334",  # sizeof applied to an expression with an operator
      "/wd6326",  # Potential comparison of constant with constant
      "/wd6340",  # Sign mismatch in function parameter
      "/wd28159",  # Consider using 'GetTickCount64'
      "/wd28196",  # The precondition is not satisfied
      "/wd28204",  # Inconsistent SAL annotations
      "/wd28251",  # Inconsistent SAL annotations
      "/wd28252",  # Inconsistent SAL annotations
      "/wd28253",  # Inconsistent SAL annotations
      "/wd28278",  # Function appears with no prototype in scope
      "/wd28285",  # syntax error in SAL annotation (in algorithm)
      "/wd28301",  # Inconsistent SAL annotations
      "/wd28182",  # Dereferencing NULL pointer
    ]
  }
}

# This is included by reference in the //build/config/compiler:runtime_library
# config that is applied to all targets. It is here to separate out the logic
# that is Windows-only. Please see that target for advice on what should go in
# :runtime_library vs. :compiler.
config("runtime_library") {
  cflags = []
  cflags_cc = []

  # Defines that set up the CRT.
  defines = [
    "__STD_C",
    "_CRT_RAND_S",
    "_CRT_SECURE_NO_DEPRECATE",
    "_SCL_SECURE_NO_DEPRECATE",
  ]

  # Defines that set up the Windows SDK.
  defines += [
    "_ATL_NO_OPENGL",
    "_WINDOWS",
    "CERT_CHAIN_PARA_HAS_EXTRA_FIELDS",
    "PSAPI_VERSION=1",
    "WIN32",
    "_SECURE_ATL",
  ]

  if (!use_vs_code_analysis) {
    # This is required for ATL to use XP-safe versions of its functions.
    # However it is prohibited when using /analyze
    defines += [ "_USING_V110_SDK71_" ]
  }

  if (use_custom_libcxx) {
    cflags_cc +=
        [ "-I" + rebase_path("$libcxx_prefix/include", root_build_dir) ]
    if (libcpp_is_static) {
      defines += [ "_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS" ]
    }

    # Prevent libc++ from embedding linker flags to try to automatically link
    # against its runtime library. This is unnecessary with our build system,
    # and can also result in build failures if libc++'s name for a library does
    # not match ours.
    defines += [ "_LIBCPP_NO_AUTO_LINK" ]
  }

  if (current_os == "winuwp") {
    # When targeting Windows Runtime, certain compiler/linker flags are
    # necessary.
    defines += [
      "WINUWP",
      "__WRL_NO_DEFAULT_LIB__",
    ]
    if (target_winuwp_family == "app") {
      defines += [ "WINAPI_FAMILY=WINAPI_FAMILY_PC_APP" ]
    } else if (target_winuwp_family == "phone") {
      defines += [ "WINAPI_FAMILY=WINAPI_FAMILY_PHONE_APP" ]
    } else if (target_winuwp_family == "system") {
      defines += [ "WINAPI_FAMILY=WINAPI_FAMILY_SYSTEM" ]
    } else if (target_winuwp_family == "server") {
      defines += [ "WINAPI_FAMILY=WINAPI_FAMILY_SERVER" ]
    } else {
      defines += [ "WINAPI_FAMILY=WINAPI_FAMILY_DESKTOP_APP" ]
    }
    cflags_cc += [
      "/ZW",
      "/EHsc",
    ]

    # This warning is given because the linker cannot tell the difference
    # between consuming WinRT APIs versus authoring WinRT within static
    # libraries as such this warning is always given by the linker. Since
    # consuming WinRT APIs within a library is legitimate but authoring
    # WinRT APis is not allowed, this warning is disabled to ignore the
    # legitimate consumption of WinRT APIs within static library builds.
    arflags = [ "/IGNORE:4264" ]

    if (target_winuwp_version == "10") {
      defines += [ "WIN10=_WIN32_WINNT_WIN10" ]
    } else if (target_winuwp_version == "8.1") {
      defines += [ "WIN8_1=_WIN32_WINNT_WINBLUE" ]
    } else if (target_winuwp_version == "8.0") {
      defines += [ "WIN8=_WIN32_WINNT_WIN8" ]
    }
  } else {
    # When not targeting Windows Runtime, make sure the WINAPI family is set
    # to desktop.
    defines += [ "WINAPI_FAMILY=WINAPI_FAMILY_DESKTOP_APP" ]
  }
}

# Sets the default Windows build version. This is separated because some
# targets need to manually override it for their compiles.
config("winver") {
  defines = [
    "NTDDI_VERSION=0x0A000002",
    "_WIN32_WINNT=0x0A00",
    "WINVER=0x0A00",
  ]
}

# Linker flags for Windows SDK setup, this is applied only to EXEs and DLLs.
config("sdk_link") {
  assert(current_cpu == "x64" || current_cpu == "x86" || current_cpu == "arm" ||
             current_cpu == "arm64",
         "Only supports x64, x86, arm and arm64 CPUs")
  if (current_cpu == "x64") {
    ldflags = [ "/MACHINE:X64" ]
  } else if (current_cpu == "x86") {
    ldflags = [
      "/MACHINE:X86",
      "/SAFESEH",  # Not compatible with x64 so use only for x86.
      "/largeaddressaware",
    ]
  } else if (current_cpu == "arm") {
    ldflags = [ "/MACHINE:ARM" ]
  } else if (current_cpu == "arm64") {
    ldflags = [ "/MACHINE:ARM64" ]
  }

  if (ninja_use_custom_environment_files) {
    vcvars_toolchain_data = exec_script("../../toolchain/win/setup_toolchain.py",
                                        [
                                          visual_studio_path,
                                          windows_sdk_path,
                                          visual_studio_runtime_dirs,
                                          current_os,
                                          current_cpu,
                                          "none"
                                        ],
                                        "scope")
  } else {
    vcvars_toolchain_data = exec_script("../../toolchain/win/setup_toolchain.py",
                                        [
                                          current_os,
                                          current_cpu,
                                          "none",
                                        ],
                                        "scope")
  }

  vc_lib_path = vcvars_toolchain_data.vc_lib_path
  if (defined(vcvars_toolchain_data.vc_lib_atlmfc_path)) {
    vc_lib_atlmfc_path = vcvars_toolchain_data.vc_lib_atlmfc_path
  }
  vc_lib_um_path = vcvars_toolchain_data.vc_lib_um_path

  lib_dirs = [
    "$vc_lib_um_path",
    "$vc_lib_path",
  ]
  if (defined(vc_lib_atlmfc_path)) {
    lib_dirs += [ "$vc_lib_atlmfc_path" ]
  }
}

# This default linker setup is provided separately from the SDK setup so
# targets who want different library configurations can remove this and specify
# their own.
config("common_linker_setup") {
  ldflags = [
    "/fastfail",
    "/FIXED:NO",
    "/ignore:4199",
    "/ignore:4221",
    "/NXCOMPAT",
    "/DYNAMICBASE",
  ]

  if (win_linker_timing) {
    ldflags += [
      "/time",
      "/verbose:incr",
    ]
  }
}

config("cfi_linker") {
  # Control Flow Guard (CFG)
  # https://msdn.microsoft.com/en-us/library/windows/desktop/mt637065.aspx
  # /DYNAMICBASE (ASLR) is turned off in debug builds, therefore CFG can’t be
  # turned on either.
  # CFG seems to lead to random corruption with incremental linking so turn off
  # CFG in component builds. https://crbug.com/812421
  # ASan and CFG leads to slow process startup. Chromium's test runner uses
  # lots of child processes, so this means things are really slow. Disable CFG
  # for now. https://crbug.com/846966
  if (!is_debug && !is_component_build && !is_asan) {
    # Turn on CFG in msvc linker, regardless of compiler used. Turn off CFG for
    # longjmp (new in VS 2017) because it relies on compiler support which we do
    # not have enabled.
    ldflags = [ "/guard:cf,nolongjmp" ]
  }
}

# CRT --------------------------------------------------------------------------

# Configures how the runtime library (CRT) is going to be used.
# See https://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx for a reference of
# what each value does.
config("default_crt") {
  if (is_shared) {
    # Component mode: dynamic CRT. Since the library is shared, it requires
    # exceptions or will give errors about things not matching, so keep
    # exceptions on.
    configs = [ ":dynamic_crt" ]
  } else {
    if (current_os == "winuwp") {
      # https://blogs.msdn.microsoft.com/vcblog/2014/06/10/the-great-c-runtime-crt-refactoring/
      # contains a details explanation of what is happening with the Windows
      # CRT in Visual Studio releases related to Windows store applications.
      configs = [ ":dynamic_crt" ]
    } else {
      # Desktop Windows: static CRT.
      configs = [ ":static_crt" ]
    }
  }
}

# Use this to force the debug CRT for when building perf-critical build tools
# that need to be fully optimized even in debug builds, for those times when the
# debug CRT is part of the bottleneck. This also avoids *implicitly* defining
# _DEBUG.
config("release_crt") {
  if (is_component_build) {
    cflags = [ "/MD" ]
  } else {
    cflags = [ "/MT" ]
  }
}

config("dynamic_crt") {
  if (is_debug) {
    # This pulls in the DLL debug CRT and defines _DEBUG
    cflags = [ "/MDd" ]
  } else {
    cflags = [ "/MD" ]
  }
}

config("static_crt") {
  if (is_debug) {
    # This pulls in the static debug CRT and defines _DEBUG
    cflags = [ "/MTd" ]
  } else {
    cflags = [ "/MT" ]
  }
}

# Subsystem --------------------------------------------------------------------

# This is appended to the subsystem to specify a minimum version.
if (current_cpu == "x64") {
  # The number after the comma is the minimum required OS version.
  # 5.02 = Windows Server 2003.
  subsystem_version_suffix = ",5.02"
} else {
  # 5.01 = Windows XP.
  subsystem_version_suffix = ",5.01"
}

config("console") {
  ldflags = [ "/SUBSYSTEM:CONSOLE$subsystem_version_suffix" ]
}
config("windowed") {
  ldflags = [ "/SUBSYSTEM:WINDOWS$subsystem_version_suffix" ]
}

# Incremental linking ----------------------------------------------------------

incremental_linking_on_switch = [ "/INCREMENTAL" ]
if ((!is_debug && !is_component_build) || !use_lld) {
  incremental_linking_off_switch = [ "/INCREMENTAL:NO" ]
}
if (use_lld) {
  incremental_linking_on_switch += [ "/OPT:NOREF" ]
}

# Enable incremental linking for debug builds and all component builds - any
# builds where performance is not job one.
if (use_incremental_linking) {
  default_incremental_linking_switch = incremental_linking_on_switch
} else {
  default_incremental_linking_switch = incremental_linking_off_switch
}

# Applies incremental linking or not depending on the current configuration.
config("default_incremental_linking") {
  ldflags = default_incremental_linking_switch
}

# Explicitly on or off incremental linking
config("incremental_linking") {
  ldflags = incremental_linking_on_switch
}
config("no_incremental_linking") {
  # Projects disable incremental linking to work around ilk file issues with
  # link.exe. lld doesn't use ilk files and doesn't really have an incremental
  # link mode; the only effect of the flag is that the .lib file timestamp isn't
  # updated if the .lib doesn't change.
  if (!use_lld) {
    ldflags = incremental_linking_off_switch
  }
}

# Some large modules can't handle incremental linking in some situations. This
# config should be applied to large modules to turn off incremental linking
# when it won't work.
config("default_large_module_incremental_linking") {
  if (use_lld || symbol_level == 0 ||
      (current_cpu == "x86" && is_component_build)) {
    # In these configurations, ilk file sizes stay low enough that we can
    # link incrementally.
    ldflags = default_incremental_linking_switch
  } else {
    ldflags = incremental_linking_off_switch
  }
}

# Character set ----------------------------------------------------------------

# Not including this config means "ansi" (8-bit system codepage).
config("unicode") {
  defines = [
    "_UNICODE",
    "UNICODE",
  ]
}

# Lean and mean ----------------------------------------------------------------

# Some third party code might not compile with WIN32_LEAN_AND_MEAN so we have
# to have a separate config for it. Remove this config from your target to
# get the "bloaty and accommodating" version of windows.h.
config("lean_and_mean") {
  defines = [ "WIN32_LEAN_AND_MEAN" ]
}

# Nominmax --------------------------------------------------------------------

# Some third party code defines NOMINMAX before including windows.h, which
# then causes warnings when it's been previously defined on the command line.
# For such targets, this config can be removed.

config("nominmax") {
  defines = [ "NOMINMAX" ]
}

# Generating order files -------------------------------------------------------

config("default_cygprofile_instrumentation") {
  if (generate_order_files) {
    assert(is_clang, "cygprofile instrumentation only works with clang")
    assert(is_official_build, "order files should be made w/ official builds")
    assert(!is_chrome_branded, "order files could leak internal symbol names")
    configs = [ ":cygprofile_instrumentation" ]
  }
}

config("cygprofile_instrumentation") {
  cflags = [
    "-Xclang",
    "-finstrument-functions-after-inlining",
  ]
}
