Home | History | Annotate | Download | only in gn
      1 # Copyright 2016 Google Inc.
      2 #
      3 # Use of this source code is governed by a BSD-style license that can be
      4 # found in the LICENSE file.
      5 
      6 declare_args() {
      7   extra_asmflags = []
      8   extra_cflags = []
      9   extra_cflags_c = []
     10   extra_cflags_cc = []
     11   extra_ldflags = []
     12 
     13   malloc = ""
     14 }
     15 
     16 if (is_ios) {
     17   if (is_tvos) {
     18     sdk = "appletvos"
     19     if (target_cpu == "x86" || target_cpu == "x64") {
     20       sdk = "appletvsimulator"
     21     }
     22   } else {
     23     sdk = "iphoneos"
     24     if (target_cpu == "x86" || target_cpu == "x64") {
     25       sdk = "iphonesimulator"
     26     }
     27   }
     28   ios_sysroot = exec_script("find_ios_sysroot.py", [ sdk ], "trim string")
     29 }
     30 
     31 config("default") {
     32   asmflags = []
     33   cflags = []
     34   cflags_c = []
     35   cflags_cc = []
     36   defines = []
     37   ldflags = []
     38   libs = []
     39 
     40   if (is_win) {
     41     cflags += [
     42       "/bigobj",  # Some of our files are bigger than the regular limits.
     43       "/WX",  # Treat warnings as errors.
     44       "/utf-8",  # Set Source and Executable character sets to UTF-8.
     45     ]
     46     if (is_clang) {
     47       cflags += [ "-fms-compatibility-version=19" ]  # 2015
     48     }
     49     defines += [
     50       "_CRT_SECURE_NO_WARNINGS",  # Disables warnings about sscanf().
     51       "_HAS_EXCEPTIONS=0",  # Disables exceptions in MSVC STL.
     52       "WIN32_LEAN_AND_MEAN",
     53       "NOMINMAX",
     54     ]
     55 
     56     if (msvc == 2015) {
     57       _include_dirs = [ "$win_vc/include" ]
     58     } else {  # 2017
     59       _include_dirs = [ "$win_vc/Tools/MSVC/$win_toolchain_version/include" ]
     60     }
     61     _include_dirs += [
     62       "$win_sdk/Include/$win_sdk_version/shared",
     63       "$win_sdk/Include/$win_sdk_version/ucrt",
     64       "$win_sdk/Include/$win_sdk_version/um",
     65       "$win_sdk/Include/$win_sdk_version/winrt",
     66     ]
     67 
     68     if (is_clang) {
     69       foreach(dir, _include_dirs) {
     70         cflags += [
     71           "-imsvc",
     72           dir,
     73         ]
     74       }
     75     } else {
     76       include_dirs = _include_dirs
     77     }
     78 
     79     lib_dirs = [
     80       "$win_sdk/Lib/$win_sdk_version/ucrt/$target_cpu",
     81       "$win_sdk/Lib/$win_sdk_version/um/$target_cpu",
     82     ]
     83     if (msvc == 2015) {
     84       if (target_cpu == "x86") {
     85         lib_dirs += [ "$win_vc/lib" ]
     86       } else {
     87         lib_dirs += [ "$win_vc/lib/amd64" ]
     88       }
     89     } else {  # 2017
     90       lib_dirs +=
     91           [ "$win_vc/Tools/MSVC/$win_toolchain_version/lib/$target_cpu" ]
     92     }
     93   } else {
     94     cflags += [
     95       "-fstrict-aliasing",
     96       "-fPIC",
     97     ]
     98     cflags_cc += [ "-std=c++14" ]
     99 
    100     # The main idea is to slim the exported API, but these flags also improve link time on Mac.
    101     # These would make stack traces worse on Linux, so we don't just set them willy-nilly.
    102     if (is_component_build || is_ios || is_mac) {
    103       cflags += [ "-fvisibility=hidden" ]
    104       cflags_cc += [ "-fvisibility-inlines-hidden" ]
    105     }
    106   }
    107 
    108   if (current_cpu == "arm") {
    109     cflags += [
    110       "-march=armv7-a",
    111       "-mfpu=neon",
    112       "-mthumb",
    113     ]
    114   } else if (current_cpu == "loongson3a") {
    115     asmflags += [ "-march=loongson3a" ]
    116     cflags += [
    117       "-march=loongson3a",
    118 
    119       # Causes an internal compiler error.
    120       "-DSKCMS_PORTABLE",
    121     ]
    122   } else if (current_cpu == "mips64el") {
    123     asmflags += [ "-march=mips64" ]
    124     cflags += [ "-march=mips64" ]
    125   } else if (current_cpu == "x86" && !is_win) {
    126     asmflags += [ "-m32" ]
    127     cflags += [
    128       "-m32",
    129       "-msse2",
    130       "-mfpmath=sse",
    131     ]
    132     ldflags += [ "-m32" ]
    133   }
    134 
    135   if (malloc != "" && !is_win) {
    136     cflags += [
    137       "-fno-builtin-malloc",
    138       "-fno-builtin-calloc",
    139       "-fno-builtin-realloc",
    140       "-fno-builtin-free",
    141     ]
    142     libs += [ malloc ]
    143   }
    144 
    145   if (is_android) {
    146     asmflags += [ "--target=$ndk_target" ]
    147     cflags += [
    148       "--sysroot=$ndk/sysroot",
    149       "-isystem$ndk/sysroot/usr/include/$ndk_target",
    150       "-D__ANDROID_API__=$ndk_api",
    151       "--target=$ndk_target",
    152     ]
    153     cflags_cc += [
    154       "-isystem$ndk/sources/cxx-stl/llvm-libc++/include",
    155       "-isystem$ndk/sources/cxx-stl/llvm-libc++abi/include",
    156       "-isystem$ndk/sources/android/support/include",
    157     ]
    158     ldflags += [
    159       "--sysroot=$ndk/platforms/$ndk_platform",
    160       "--target=$ndk_target",
    161       "-B$ndk/toolchains/$ndk_gccdir-4.9/prebuilt/$ndk_host/$ndk_target/bin",
    162     ]
    163     lib_dirs = [
    164       "$ndk/sources/cxx-stl/llvm-libc++/libs/$ndk_stdlib",
    165       "$ndk/toolchains/$ndk_gccdir-4.9/prebuilt/$ndk_host/lib/gcc/$ndk_target/4.9.x",
    166     ]
    167 
    168     libs += [
    169       "c++_static",
    170       "c++abi",
    171     ]
    172     if (ndk_api < 21) {
    173       libs += [ "android_support" ]
    174     }
    175   }
    176 
    177   if (is_ios) {
    178     _target = target_cpu
    179     if (target_cpu == "arm") {
    180       _target = "armv7"
    181     } else if (target_cpu == "x86") {
    182       _target = "i386"
    183     } else if (target_cpu == "x64") {
    184       _target = "x86_64"
    185     }
    186     asmflags += [
    187       "-isysroot",
    188       ios_sysroot,
    189       "-arch",
    190       _target,
    191     ]
    192     cflags += [
    193       "-isysroot",
    194       ios_sysroot,
    195       "-arch",
    196       _target,
    197     ]
    198     cflags_cc += [ "-stdlib=libc++" ]
    199     ldflags += [
    200       "-isysroot",
    201       ios_sysroot,
    202       "-arch",
    203       _target,
    204       "-stdlib=libc++",
    205     ]
    206     libs += [ "objc" ]
    207   }
    208 
    209   if (is_linux) {
    210     libs += [ "pthread" ]
    211     if (is_debug && sanitize == "") {
    212       defines += [ "_GLIBCXX_DEBUG" ]
    213     }
    214   }
    215   if (is_mac) {
    216     # Disable linker warnings.  They're usually just annoyances like,
    217     #   ld: warning: text-based stub file
    218     #     /System/Library/Frameworks/foo.framework/foo.tbd and library file
    219     #     /System/Library/Frameworks/foo.framework/foo are out of sync.
    220     #     Falling back to library file for linking.
    221     ldflags += [ "-Wl,-w" ]
    222   }
    223 
    224   if (sanitize != "" && sanitize != "MSVC") {
    225     # You can either pass the sanitizers directly, e.g. "address,undefined",
    226     # or pass one of the couple common aliases used by the bots.
    227     sanitizers = sanitize
    228 
    229     if (sanitize == "ASAN" || sanitize == "UBSAN") {
    230       # ASAN implicitly runs all UBSAN checks also.
    231       sanitizers = "undefined"
    232       if (sanitize == "ASAN") {
    233         sanitizers += ",address"
    234       }
    235 
    236       if (is_android) {
    237         # TODO(mtklein): work out UBSAN link errors
    238         sanitizers = "address"
    239       }
    240     } else if (sanitize == "TSAN") {
    241       sanitizers = "thread"
    242     } else if (sanitize == "MSAN") {
    243       sanitizers = "memory"
    244     }
    245 
    246     _blacklist = rebase_path("../tools/xsan.blacklist")
    247 
    248     cflags += [
    249       "-fsanitize=$sanitizers",
    250       "-fno-sanitize-recover=$sanitizers",
    251       "-fsanitize-blacklist=$_blacklist",
    252     ]
    253     ldflags += [ "-fsanitize=$sanitizers" ]
    254 
    255     if (is_win) {
    256       cflags += [ "/FI$_blacklist" ]
    257     } else {
    258       cflags += [
    259         "-include$_blacklist",
    260         "-fno-omit-frame-pointer",
    261       ]
    262     }
    263 
    264     if (is_linux) {
    265       cflags_cc += [ "-stdlib=libc++" ]
    266       ldflags += [ "-stdlib=libc++" ]
    267     }
    268 
    269     if (sanitizers == "memory") {
    270       cflags += [ "-fsanitize-memory-track-origins" ]
    271     }
    272   }
    273 }
    274 
    275 config("no_exceptions") {
    276   # Exceptions are disabled by default on Windows.  (Use /EHsc to enable them.)
    277   if (!is_win) {
    278     cflags_cc = [ "-fno-exceptions" ]
    279   }
    280 }
    281 
    282 config("warnings") {
    283   cflags = []
    284   cflags_cc = []
    285   cflags_objc = []
    286   cflags_objcc = []
    287   if (is_win) {
    288     cflags += [
    289       "/W3",  # Turn on lots of warnings.
    290 
    291       # Disable a bunch of warnings:
    292       "/wd4244",  # conversion from 'float' to 'int', possible loss of data
    293       "/wd4267",  # conversion from 'size_t' to 'int', possible loss of data
    294       "/wd4800",  # forcing value to bool 'true' or 'false' (performance warning)
    295 
    296       # Probably only triggers when /EHsc is enabled.
    297       "/wd4291",  # no matching operator delete found;
    298                   # memory will not be freed if initialization throws an exception
    299     ]
    300   } else {
    301     cflags += [
    302       "-Werror",
    303       "-Wall",
    304       "-Wextra",
    305       "-Winit-self",
    306       "-Wpointer-arith",
    307       "-Wsign-compare",
    308       "-Wvla",
    309 
    310       "-Wno-deprecated-declarations",
    311       "-Wno-maybe-uninitialized",
    312     ]
    313     cflags_cc += [
    314       "-Wnon-virtual-dtor",
    315       "-Wno-noexcept-type",
    316     ]
    317   }
    318 
    319   if (is_clang) {
    320     cflags += [
    321       "-fcolor-diagnostics",
    322       "-Weverything",
    323       "-Wno-unknown-warning-option",  # Let older Clangs ignore newer Clangs' warnings.
    324     ]
    325 
    326     if (target_cpu == "arm" && is_ios) {
    327       # Clang seems to think new/malloc will only be 4-byte aligned on 32-bit iOS.
    328       # We're pretty sure it's actually 8-byte alignment.
    329       cflags += [ "-Wno-over-aligned" ]
    330     }
    331     if (target_cpu == "x86" && is_android) {
    332       # Clang seems to think new/malloc will only be 4-byte aligned on 32-bit x86 Android builds.
    333       # We're pretty sure it's actually 8-byte alignment.  See OverAlignedTest.cpp for more info.
    334       cflags += [ "-Wno-over-aligned" ]
    335     }
    336 
    337     # Shouldn't be necessary for local builds. With distributed Windows builds, files may lose
    338     # their case during copy, causing case-sensitivity mismatch on remote machines.
    339     cflags += [
    340       "-Wno-nonportable-include-path",
    341       "-Wno-nonportable-system-include-path",
    342     ]
    343 
    344     # TODO: These would all be really great warnings to turn on.
    345     cflags += [
    346       "-Wno-cast-align",
    347       "-Wno-cast-qual",
    348       "-Wno-conversion",
    349       "-Wno-disabled-macro-expansion",
    350       "-Wno-documentation",
    351       "-Wno-documentation-unknown-command",
    352       "-Wno-double-promotion",
    353       "-Wno-exit-time-destructors",  # TODO: OK outside libskia
    354       "-Wno-float-equal",
    355       "-Wno-format-nonliteral",
    356       "-Wno-global-constructors",  # TODO: OK outside libskia
    357       "-Wno-missing-prototypes",
    358       "-Wno-missing-variable-declarations",
    359       "-Wno-pedantic",
    360       "-Wno-reserved-id-macro",
    361       "-Wno-shadow",
    362       "-Wno-shift-sign-overflow",
    363       "-Wno-signed-enum-bitfield",
    364       "-Wno-switch-enum",
    365       "-Wno-undef",
    366       "-Wno-unreachable-code",
    367       "-Wno-unreachable-code-break",
    368       "-Wno-unreachable-code-return",
    369       "-Wno-unused-macros",
    370       "-Wno-unused-member-function",
    371       "-Wno-unused-template",
    372       "-Wno-zero-as-null-pointer-constant",
    373     ]
    374     cflags_cc += [
    375       "-Wno-abstract-vbase-init",
    376       "-Wno-weak-vtables",
    377     ]
    378 
    379     # We are unlikely to want to fix these.
    380     cflags += [
    381       "-Wno-bad-function-cast",
    382       "-Wno-covered-switch-default",
    383       "-Wno-deprecated",
    384       "-Wno-missing-noreturn",
    385       "-Wno-old-style-cast",
    386       "-Wno-padded",
    387       "-Wno-newline-eof",
    388     ]
    389     cflags_cc += [
    390       "-Wno-c++98-compat",
    391       "-Wno-c++98-compat-pedantic",
    392       "-Wno-undefined-func-template",
    393     ]
    394     cflags_objc += [
    395       "-Wno-direct-ivar-access",
    396       "-Wno-objc-interface-ivars",
    397     ]
    398     cflags_objcc += [
    399       "-Wno-direct-ivar-access",
    400       "-Wno-objcc-interface-ivars",
    401     ]
    402   }
    403   if (!is_win || is_clang) {
    404     cflags += [ "-Wno-implicit-fallthrough" ]
    405   }
    406 }
    407 config("warnings_except_public_headers") {
    408   if (!is_win || is_clang) {
    409     cflags = [ "-Wno-unused-parameter" ]
    410   }
    411 }
    412 
    413 config("extra_flags") {
    414   asmflags = extra_asmflags
    415   cflags = extra_cflags
    416   cflags_c = extra_cflags_c
    417   cflags_cc = extra_cflags_cc
    418   ldflags = extra_ldflags
    419 }
    420 
    421 config("debug_symbols") {
    422   # It's annoying to wait for full debug symbols to push over
    423   # to Android devices.  -gline-tables-only is a lot slimmer.
    424   if (is_android) {
    425     cflags = [
    426       "-gline-tables-only",
    427       "-funwind-tables",  # Helps make in-process backtraces fuller.
    428     ]
    429   } else if (is_win) {
    430     cflags = [ "/Z7" ]
    431     if (is_clang) {
    432       cflags += [ "-gcodeview-ghash" ]
    433       ldflags = [ "/DEBUG:GHASH" ]
    434     } else {
    435       ldflags = [ "/DEBUG:FASTLINK" ]
    436     }
    437   } else {
    438     cflags = [ "-g" ]
    439   }
    440 }
    441 
    442 config("no_rtti") {
    443   if (sanitize != "ASAN") {  # -fsanitize=vptr requires RTTI
    444     if (is_win) {
    445       cflags_cc = [ "/GR-" ]
    446     } else {
    447       cflags_cc = [ "-fno-rtti" ]
    448     }
    449   }
    450 }
    451 
    452 config("optimize") {
    453   if (is_win) {
    454     cflags = [
    455       "/O2",
    456       "/Zc:inline",
    457     ]
    458     ldflags = [
    459       "/OPT:ICF",
    460       "/OPT:REF",
    461     ]
    462   } else {
    463     cflags = [
    464       "-O3",
    465       "-fdata-sections",
    466       "-ffunction-sections",
    467     ]
    468     if (is_mac || is_ios) {
    469       ldflags = [ "-dead_strip" ]
    470     } else {
    471       ldflags = [ "-Wl,--gc-sections" ]
    472     }
    473     if (target_cpu == "wasm") {
    474       # The compiler asks us to add an optimization flag to both cflags
    475       # and ldflags to cut down on the local variables,
    476       # for performance reasons.
    477       # The "linking" step is the conversion to javascript.
    478       ldflags += [ "-O3" ]
    479     }
    480   }
    481 }
    482 
    483 config("NDEBUG") {
    484   defines = [ "NDEBUG" ]
    485 }
    486 
    487 config("msvc_rtc") {
    488   defines = [ "_ALLOW_RTCc_IN_STL" ]
    489   cflags = [
    490     "/RTCcsu",
    491     "/guard:cf",
    492   ]
    493   ldflags = [ "/guard:cf" ]
    494 }
    495 
    496 config("executable") {
    497   if (is_android) {
    498     ldflags = [
    499       "-pie",
    500       "-rdynamic",
    501     ]
    502   } else if (is_mac) {
    503     ldflags = [ "-Wl,-rpath,@loader_path/." ]
    504   } else if (is_linux) {
    505     ldflags = [
    506       "-rdynamic",
    507       "-Wl,-rpath,\$ORIGIN",
    508     ]
    509   } else if (is_win) {
    510     ldflags = [
    511       "/SUBSYSTEM:CONSOLE",  # Quiet "no subsystem specified; CONSOLE assumed".
    512       "/INCREMENTAL:NO",  # Quiet warnings about failing to incrementally link by never trying to.
    513     ]
    514   }
    515 }
    516