Home | History | Annotate | Download | only in standalone
      1 # Copyright (C) 2017 The Android Open Source Project
      2 #
      3 # Licensed under the Apache License, Version 2.0 (the "License");
      4 # you may not use this file except in compliance with the License.
      5 # You may obtain a copy of the License at
      6 #
      7 #      http://www.apache.org/licenses/LICENSE-2.0
      8 #
      9 # Unless required by applicable law or agreed to in writing, software
     10 # distributed under the License is distributed on an "AS IS" BASIS,
     11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 # See the License for the specific language governing permissions and
     13 # limitations under the License.
     14 
     15 import("//gn/standalone/android.gni")
     16 import("//gn/standalone/wasm.gni")
     17 import("//gn/standalone/sanitizers/sanitizers.gni")
     18 
     19 config("extra_warnings") {
     20   cflags = [
     21     "-Wall",
     22     "-Wextra",
     23   ]
     24 
     25   # Disable Weverything on fuzzers to avoid breakages when new versions of clang
     26   # are rolled into OSS-fuzz.
     27   if (is_clang && !is_fuzzer) {
     28     cflags += [
     29       "-Weverything",
     30       "-Wno-c++98-compat-pedantic",
     31       "-Wno-c++98-compat",
     32       "-Wno-disabled-macro-expansion",
     33       "-Wno-gnu-include-next",
     34       "-Wno-gnu-statement-expression",
     35       "-Wno-gnu-zero-variadic-macro-arguments",
     36       "-Wno-padded",
     37       "-Wno-reserved-id-macro",
     38       "-Wno-unknown-sanitizers",
     39     ]
     40   }
     41 }
     42 
     43 config("no_exceptions") {
     44   cflags_cc = [ "-fno-exceptions" ]
     45 }
     46 
     47 config("no_rtti") {
     48   cflags_cc = [ "-fno-rtti" ]
     49 }
     50 
     51 config("c++11") {
     52   cflags_cc = [ "-std=c++11" ]
     53 }
     54 
     55 # This is needed to compile libunwindstack.
     56 config("c++17") {
     57   cflags_cc = [ "-std=c++17" ]
     58 }
     59 
     60 config("visibility_hidden") {
     61   cflags = [ "-fvisibility=hidden" ]
     62 }
     63 
     64 config("default") {
     65   asmflags = []
     66   cflags = []
     67   cflags_c = []
     68   cflags_cc = []
     69   defines = []
     70   ldflags = []
     71   libs = []
     72 
     73   cflags += [
     74     "-fstrict-aliasing",
     75     "-fstack-protector",
     76     "-fPIC",
     77     "-g",
     78     "-Wformat",
     79     "-Werror",
     80   ]
     81 
     82   if (!is_wasm && !is_lto) {
     83     cflags += [ "-Wa,--noexecstack" ]
     84   }
     85 
     86   if (is_clang) {
     87     cflags += [
     88       # Color compiler output, see https://github.com/ninja-build/ninja/wiki/FAQ
     89       "-fcolor-diagnostics",
     90       "-fdiagnostics-show-template-tree",
     91     ]
     92   }
     93 
     94   if (is_lto) {
     95     cflags += [ "-flto=full" ]
     96     ldflags += [ "-flto=full" ]
     97   }
     98 
     99   if (current_cpu == "arm") {
    100     cflags += [
    101       "-march=armv7-a",
    102       "-mfpu=neon",
    103       "-mthumb",
    104     ]
    105   } else if (current_cpu == "x86") {
    106     asmflags += [ "-m32" ]
    107     cflags += [
    108       "-m32",
    109       "-msse2",
    110       "-mfpmath=sse",
    111     ]
    112     ldflags += [ "-m32" ]
    113   } else if (current_cpu == "arm64") {
    114     cflags += [ "-fno-omit-frame-pointer" ]
    115   }
    116 
    117   if (is_linux) {
    118     libs += [
    119       "pthread",
    120       "rt",
    121     ]
    122   }
    123 
    124   if (is_debug) {
    125     libs += [ "dl" ]
    126   }
    127 
    128   if (is_android) {
    129     asmflags += [ "--target=$android_abi_target" ]
    130     cflags += [
    131       "--sysroot=$android_compile_sysroot",
    132       "-isystem$android_compile_sysroot/$android_compile_sysroot_subdir",
    133       "-isystem$android_compile_sysroot",
    134       "-DANDROID",
    135       "-D__ANDROID_API__=${android_api_level}",
    136       "--target=$android_abi_target",
    137     ]
    138     cflags_cc += [
    139       "-I$android_ndk_root/sources/cxx-stl/llvm-libc++/include",
    140       "-I$android_ndk_root/sources/android/support/include",
    141       "-I$android_ndk_root/sources/cxx-stl/llvm-libc++abi/include",
    142 
    143       # This is needed for NDK libc++. Using -isystem instead of -I above
    144       # results in build breakage related to cmath.h.
    145       "-Wno-format-nonliteral",
    146     ]
    147     ldflags += [
    148       "-Wl,-z,nocopyreloc",
    149       "-gcc-toolchain",
    150       "$android_toolchain_root",
    151       "--sysroot=$android_ndk_root/$android_link_sysroot_subdir",
    152       "--target=$android_abi_target",
    153       "-Wl,--exclude-libs,libunwind.a",
    154       "-Wl,--exclude-libs,libgcc.a",
    155       "-Wl,--exclude-libs,libc++_static.a",
    156       "-Wl,--build-id",
    157       "-Wl,--no-undefined",
    158       "-Wl,-z,noexecstack",
    159       "-Wl,-z,relro",
    160       "-Wl,-z,now",
    161       "-Wl,--warn-shared-textrel",
    162       "-Wl,--fatal-warnings",
    163     ]
    164     lib_dirs = [
    165       "$android_ndk_root/sources/cxx-stl/llvm-libc++/libs/$android_app_abi",
    166     ]
    167     libs += [
    168       "gcc",
    169       "c++_static",
    170       "c++abi",
    171     ]
    172 
    173     if (current_cpu == "arm") {
    174       # New NDKs don't have libandroid_support for arm64.
    175       libs += [ "android_support" ]
    176     }
    177   }
    178 }
    179 
    180 config("debug_symbols") {
    181   cflags = [ "-O0" ]
    182   if (is_android || is_linux) {
    183     cflags += [ "-funwind-tables" ]
    184   }
    185 }
    186 
    187 config("release") {
    188   cflags = [
    189     "-fdata-sections",
    190     "-ffunction-sections",
    191   ]
    192   if (is_android) {
    193     cflags += [ "-Oz" ]
    194   } else if (is_fuzzer) {
    195     cflags += [ "-O1" ]
    196   } else {
    197     cflags += [ "-O3" ]
    198   }
    199   if (is_mac) {
    200     ldflags = [ "-dead_strip" ]
    201   } else {
    202     ldflags = [
    203       "-fuse-ld=gold",
    204       "-Wl,--gc-sections",
    205       "-Wl,--icf=all",
    206       "-Wl,-O1",
    207     ]
    208   }
    209   defines = [ "NDEBUG" ]
    210 }
    211 
    212 config("shared_library") {
    213   if (is_android || is_linux) {
    214     ldflags = [ "-fPIC" ]
    215   }
    216 }
    217 
    218 config("executable") {
    219   ldflags = []
    220 
    221   # Android will refuse to run executables if they aren't position independent.
    222   # Instead on Linux there isn't any need and they break ASan (goo.gl/paFR6K).
    223   if (is_android) {
    224     asmflags = [ "-fPIE" ]
    225     cflags = [ "-fPIE" ]
    226     ldflags += [ "-pie" ]
    227   }
    228 
    229   # -rpath stores the path to the linked shared libraries into the binary, so
    230   # that they can be launched without passing any LD_LIBRARY_PATH. It's
    231   # supported only by Linux, not Android. But concretely we need this only when
    232   # use_custom_libcxx=true && custom_libcxx_is_static=false, which happens only
    233   # on Linux right now.
    234   if (is_linux) {
    235     ldflags += [
    236       "-Wl,-rpath=\$ORIGIN/.",
    237       "-Wl,-rpath-link=.",
    238     ]
    239   }
    240 }
    241 
    242 # This config is only added to certain leaf target types (see BUILDCONFIG.gn).
    243 # This allows us to remove the config (and thus the dependency) on a per-target
    244 # basis. If the config was applied to source_sets, then they would unavoidably
    245 # carry the dependency onto liblog to the final target.
    246 config("android_liblog") {
    247   if (is_android) {
    248     libs = [ "log" ]
    249   }
    250 }
    251 
    252 config("gen_include_path") {
    253   include_dirs = [ root_gen_dir ]
    254 }
    255 
    256 # This action generates a perfetto_version.gen.h which contains the git SHA1
    257 # of HEAD. The file can be included from C/C++ sources and exposes a
    258 # PERFETTO_GET_GIT_REVISION() macro that contains the git revision.
    259 action("gen_git_revision") {
    260   script = "gen_git_revision.py"
    261   generated_header = "${root_gen_dir}/perfetto_version.gen.h"
    262   args = [ rebase_path(generated_header, root_build_dir) ]
    263   inputs = []
    264   outputs = [
    265     generated_header,
    266   ]
    267   public_configs = [ ":gen_include_path" ]
    268 }
    269