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       "-Werror",
     98     ]
     99     cflags_cc += [ "-std=c++11" ]
    100 
    101     # The main idea is to slim the exported API, but these flags also improve link time on Mac.
    102     # These would make stack traces worse on Linux, so we don't just set them willy-nilly.
    103     if (is_component_build || is_ios || is_mac) {
    104       cflags += [ "-fvisibility=hidden" ]
    105       cflags_cc += [ "-fvisibility-inlines-hidden" ]
    106     }
    107   }
    108 
    109   if (current_cpu == "arm") {
    110     cflags += [
    111       "-march=armv7-a",
    112       "-mfpu=neon",
    113       "-mthumb",
    114     ]
    115   } else if (current_cpu == "x86" && is_win) {
    116     cflags += [ "/arch:SSE2" ]
    117   } else if (current_cpu == "x86") {
    118     asmflags += [ "-m32" ]
    119     cflags += [
    120       "-m32",
    121       "-msse2",
    122       "-mfpmath=sse",
    123     ]
    124     ldflags += [ "-m32" ]
    125   }
    126 
    127   if (malloc != "" && !is_win) {
    128     cflags += [
    129       "-fno-builtin-malloc",
    130       "-fno-builtin-calloc",
    131       "-fno-builtin-realloc",
    132       "-fno-builtin-free",
    133     ]
    134     libs += [ malloc ]
    135   }
    136 
    137   if (is_android) {
    138     asmflags += [ "--target=$ndk_target" ]
    139     cflags += [
    140       "--sysroot=$ndk/sysroot",
    141       "-isystem$ndk/sysroot/usr/include/$ndk_target",
    142       "-D__ANDROID_API__=$ndk_api",
    143       "--target=$ndk_target",
    144     ]
    145     cflags_cc += [
    146       "-isystem$ndk/sources/cxx-stl/llvm-libc++/include",
    147       "-isystem$ndk/sources/cxx-stl/llvm-libc++abi/include",
    148       "-isystem$ndk/sources/android/support/include",
    149     ]
    150     ldflags += [
    151       "--sysroot=$ndk/platforms/$ndk_platform",
    152       "--target=$ndk_target",
    153       "-B$ndk/toolchains/$ndk_gccdir-4.9/prebuilt/$ndk_host/$ndk_target/bin",
    154     ]
    155     lib_dirs = [
    156       "$ndk/sources/cxx-stl/llvm-libc++/libs/$ndk_stdlib",
    157       "$ndk/toolchains/$ndk_gccdir-4.9/prebuilt/$ndk_host/lib/gcc/$ndk_target/4.9.x",
    158     ]
    159 
    160     libs += [
    161       "c++_static",
    162       "c++abi",
    163       "android_support",
    164     ]
    165   }
    166 
    167   if (is_ios) {
    168     _target = target_cpu
    169     if (target_cpu == "arm") {
    170       _target = "armv7"
    171     } else if (target_cpu == "x86") {
    172       _target = "i386"
    173     } else if (target_cpu == "x64") {
    174       _target = "x86_64"
    175     }
    176     asmflags += [
    177       "-isysroot",
    178       ios_sysroot,
    179       "-arch",
    180       _target,
    181     ]
    182     cflags += [
    183       "-isysroot",
    184       ios_sysroot,
    185       "-arch",
    186       _target,
    187     ]
    188     cflags_cc += [ "-stdlib=libc++" ]
    189     ldflags += [
    190       "-isysroot",
    191       ios_sysroot,
    192       "-arch",
    193       _target,
    194       "-stdlib=libc++",
    195     ]
    196     libs += [ "objc" ]
    197   }
    198 
    199   if (is_linux) {
    200     libs += [ "pthread" ]
    201   }
    202   if (is_mac) {
    203     # Disable linker warnings.  They're usually just annoyances like,
    204     #   ld: warning: text-based stub file
    205     #     /System/Library/Frameworks/foo.framework/foo.tbd and library file
    206     #     /System/Library/Frameworks/foo.framework/foo are out of sync.
    207     #     Falling back to library file for linking.
    208     ldflags += [ "-Wl,-w" ]
    209   }
    210 
    211   if (sanitize != "") {
    212     # You can either pass the sanitizers directly, e.g. "address,undefined",
    213     # or pass one of the couple common aliases used by the bots.
    214     sanitizers = sanitize
    215     if (sanitize == "ASAN" || sanitize == "UBSAN") {
    216       # ASAN implicitly runs all UBSAN checks also.
    217       sanitizers = "bool,float-cast-overflow,integer-divide-by-zero,nonnull-attribute,null,return,returns-nonnull-attribute,shift,signed-integer-overflow,unreachable,vla-bound"
    218       if (!is_mac && !is_win) {
    219         sanitizers += ",function"  # Not supported on Mac or Win.
    220       }
    221       if (!is_win) {
    222         sanitizers += ",vptr"  # Not supported on Win.
    223       }
    224       if (!is_debug && !is_win) {
    225         # No-op with somewhat annoying warning at -O0.
    226         # Seems to be broken on Win.
    227         sanitizers += ",object-size"
    228       }
    229       if (sanitize == "ASAN") {
    230         sanitizers += ",address"
    231       }
    232       if (is_android) {
    233         # Android only easily supports address for now
    234         # UBSAN runs into linking errors
    235         sanitizers = "address"
    236 
    237         # recommended by
    238         # https://github.com/google/sanitizers/wiki/AddressSanitizerOnAndroid
    239         cflags += [ "-fno-omit-frame-pointer" ]
    240       }
    241     } else if (sanitize == "TSAN") {
    242       sanitizers = "thread"
    243     } else if (sanitize == "MSAN") {
    244       sanitizers = "memory"
    245     }
    246 
    247     cflags += [
    248       "-fsanitize=$sanitizers",
    249       "-fno-sanitize-recover=$sanitizers",
    250       "-fsanitize-blacklist=" + rebase_path("../tools/xsan.blacklist"),
    251     ]
    252     if (!is_win) {
    253       cflags += [ "-fno-omit-frame-pointer" ]
    254     }
    255     ldflags += [ "-fsanitize=$sanitizers" ]
    256     if (sanitizers == "memory") {
    257       cflags += [ "-fsanitize-memory-track-origins" ]
    258       cflags_cc += [ "-stdlib=libc++" ]
    259       ldflags += [ "-stdlib=libc++" ]
    260     }
    261   }
    262 }
    263 
    264 config("no_exceptions") {
    265   # Exceptions are disabled by default on Windows.  (Use /EHsc to enable them.)
    266   if (!is_win) {
    267     cflags_cc = [ "-fno-exceptions" ]
    268   }
    269 }
    270 
    271 config("warnings") {
    272   cflags = []
    273   cflags_cc = []
    274   cflags_objc = []
    275   cflags_objcc = []
    276   if (is_win) {
    277     cflags += [
    278       "/W3",  # Turn on lots of warnings.
    279 
    280       # Disable a bunch of warnings:
    281       "/wd4244",  # conversion from 'float' to 'int', possible loss of data
    282       "/wd4267",  # conversion from 'size_t' to 'int', possible loss of data
    283       "/wd4800",  # forcing value to bool 'true' or 'false' (performance warning)
    284 
    285       # Probably only triggers when /EHsc is enabled.
    286       "/wd4291",  # no matching operator delete found;
    287                   # memory will not be freed if initialization throws an exception
    288     ]
    289   } else {
    290     cflags += [
    291       "-Wall",
    292       "-Wextra",
    293       "-Winit-self",
    294       "-Wpointer-arith",
    295       "-Wsign-compare",
    296       "-Wvla",
    297 
    298       "-Wno-deprecated-declarations",
    299       "-Wno-maybe-uninitialized",
    300     ]
    301     cflags_cc += [ "-Wnon-virtual-dtor" ]
    302   }
    303 
    304   if (is_clang) {
    305     cflags += [
    306       "-Weverything",
    307       "-Wno-unknown-warning-option",  # Let older Clangs ignore newer Clangs' warnings.
    308     ]
    309 
    310     if (target_cpu == "arm" && is_ios) {
    311       # Clang seems to think new/malloc will only be 4-byte aligned on 32-bit iOS.
    312       # We're pretty sure it's actually 8-byte alignment.
    313       cflags += [ "-Wno-over-aligned" ]
    314     }
    315     if (target_cpu == "x86" && is_android) {
    316       # Clang seems to think new/malloc will only be 4-byte aligned on 32-bit x86 Android builds.
    317       # We're pretty sure it's actually 8-byte alignment.  See OverAlignedTest.cpp for more info.
    318       cflags += [ "-Wno-over-aligned" ]
    319     }
    320 
    321     # Shouldn't be necessary for local builds. With distributed Windows builds, files may lose
    322     # their case during copy, causing case-sensitivity mismatch on remote machines.
    323     cflags += [
    324       "-Wno-nonportable-include-path",
    325       "-Wno-nonportable-system-include-path",
    326     ]
    327 
    328     # TODO: These would all be really great warnings to turn on.
    329     cflags += [
    330       "-Wno-cast-align",
    331       "-Wno-cast-qual",
    332       "-Wno-conversion",
    333       "-Wno-disabled-macro-expansion",
    334       "-Wno-documentation",
    335       "-Wno-documentation-unknown-command",
    336       "-Wno-double-promotion",
    337       "-Wno-exit-time-destructors",  # TODO: OK outside libskia
    338       "-Wno-float-equal",
    339       "-Wno-format-nonliteral",
    340       "-Wno-global-constructors",  # TODO: OK outside libskia
    341       "-Wno-missing-prototypes",
    342       "-Wno-missing-variable-declarations",
    343       "-Wno-pedantic",
    344       "-Wno-reserved-id-macro",
    345       "-Wno-shadow",
    346       "-Wno-shift-sign-overflow",
    347       "-Wno-signed-enum-bitfield",
    348       "-Wno-switch-enum",
    349       "-Wno-undef",
    350       "-Wno-unreachable-code",
    351       "-Wno-unreachable-code-break",
    352       "-Wno-unreachable-code-return",
    353       "-Wno-unused-macros",
    354       "-Wno-unused-member-function",
    355       "-Wno-unused-template",
    356       "-Wno-zero-as-null-pointer-constant",
    357     ]
    358     cflags_cc += [
    359       "-Wno-abstract-vbase-init",
    360       "-Wno-weak-vtables",
    361     ]
    362 
    363     # We are unlikely to want to fix these.
    364     cflags += [
    365       "-Wno-covered-switch-default",
    366       "-Wno-deprecated",
    367       "-Wno-implicit-fallthrough",
    368       "-Wno-missing-noreturn",
    369       "-Wno-old-style-cast",
    370       "-Wno-padded",
    371       "-Wno-newline-eof",
    372     ]
    373     cflags_cc += [
    374       "-Wno-c++98-compat",
    375       "-Wno-c++98-compat-pedantic",
    376       "-Wno-undefined-func-template",
    377     ]
    378     cflags_objc += [
    379       "-Wno-direct-ivar-access",
    380       "-Wno-objc-interface-ivars",
    381     ]
    382     cflags_objcc += [
    383       "-Wno-direct-ivar-access",
    384       "-Wno-objcc-interface-ivars",
    385     ]
    386   }
    387 }
    388 config("warnings_except_public_headers") {
    389   if (!is_win || is_clang) {
    390     cflags = [ "-Wno-unused-parameter" ]
    391   }
    392 }
    393 
    394 config("extra_flags") {
    395   asmflags = extra_asmflags
    396   cflags = extra_cflags
    397   cflags_c = extra_cflags_c
    398   cflags_cc = extra_cflags_cc
    399   ldflags = extra_ldflags
    400 }
    401 
    402 config("debug_symbols") {
    403   # It's annoying to wait for full debug symbols to push over
    404   # to Android devices.  -gline-tables-only is a lot slimmer.
    405   if (is_android) {
    406     cflags = [
    407       "-gline-tables-only",
    408       "-funwind-tables",  # Helps make in-process backtraces fuller.
    409     ]
    410   } else if (is_win) {
    411     cflags = [ "/Z7" ]
    412     ldflags = [ "/DEBUG:FASTLINK" ]
    413   } else {
    414     cflags = [ "-g" ]
    415   }
    416 }
    417 
    418 config("no_rtti") {
    419   if (sanitize != "ASAN") {  # -fsanitize=vptr requires RTTI
    420     if (is_win) {
    421       cflags_cc = [ "/GR-" ]
    422     } else {
    423       cflags_cc = [ "-fno-rtti" ]
    424     }
    425   }
    426 }
    427 
    428 config("release") {
    429   if (is_win) {
    430     cflags = [
    431       "/O2",
    432       "/Zc:inline",
    433     ]
    434     ldflags = [
    435       "/OPT:ICF",
    436       "/OPT:REF",
    437     ]
    438   } else {
    439     cflags = [
    440       "-O3",
    441       "-fdata-sections",
    442       "-ffunction-sections",
    443     ]
    444     if (is_mac || is_ios) {
    445       ldflags = [ "-dead_strip" ]
    446     } else {
    447       ldflags = [ "-Wl,--gc-sections" ]
    448     }
    449     if (target_cpu == "wasm") {
    450       # The compiler asks us to add an optimization flag to both cflags
    451       # and ldflags to cut down on the local variables,
    452       # for performance reasons.
    453       # The "linking" step is the conversion to javascript.
    454       ldflags += [ "-O3" ]
    455     }
    456   }
    457   defines = [ "NDEBUG" ]
    458 }
    459 
    460 config("executable") {
    461   if (is_android) {
    462     ldflags = [
    463       "-pie",
    464       "-rdynamic",
    465     ]
    466   } else if (is_mac) {
    467     ldflags = [ "-Wl,-rpath,@loader_path/." ]
    468   } else if (is_linux) {
    469     ldflags = [
    470       "-rdynamic",
    471       "-Wl,-rpath,\$ORIGIN",
    472     ]
    473   } else if (is_win) {
    474     ldflags = [
    475       "/SUBSYSTEM:CONSOLE",  # Quiet "no subsystem specified; CONSOLE assumed".
    476       "/INCREMENTAL:NO",  # Quiet warnings about failing to incrementally link by never trying to.
    477     ]
    478   }
    479 }
    480