1# Copyright 2015 The Chromium Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5import("//build/config/c++/c++.gni") 6import("//build/config/clang/clang.gni") 7import("//build/config/compiler/compiler.gni") 8import("//build/config/deps_revisions.gni") 9import("//build/config/sanitizers/sanitizers.gni") 10import("//build/config/sysroot.gni") 11import("//build/toolchain/toolchain.gni") 12 13assert(is_posix) 14 15group("posix") { 16 visibility = [ "//:optimize_gn_gen" ] 17} 18 19# This is included by reference in the //build/config/compiler:runtime_library 20# config that is applied to all targets. It is here to separate out the logic 21# that is Posix-only. Please see that target for advice on what should go in 22# :runtime_library vs. :compiler. 23config("runtime_library") { 24 asmflags = [] 25 cflags = [] 26 cflags_c = [] 27 cflags_cc = [] 28 cflags_objc = [] 29 cflags_objcc = [] 30 defines = [] 31 ldflags = [] 32 lib_dirs = [] 33 libs = [] 34 35 if (use_custom_libcxx) { 36 if (!is_component_build) { 37 # Don't leak any symbols on a static build. 38 defines += [ "_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS" ] 39 if (!export_libcxxabi_from_executables) { 40 defines += [ "_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS" ] 41 } 42 } 43 cflags_cc += [ 44 "-nostdinc++", 45 "-isystem" + rebase_path("$libcxx_prefix/include", root_build_dir), 46 "-isystem" + rebase_path("$libcxxabi_prefix/include", root_build_dir), 47 ] 48 defines += [ 49 "CR_LIBCXX_REVISION=$libcxx_svn_revision", 50 "CR_LIBCXXABI_REVISION=$libcxxabi_svn_revision", 51 "_LIBCPP_ENABLE_NODISCARD", 52 ] 53 54 # Make sure we don't link against libc++ or libstdc++. 55 if (is_clang) { 56 # //build/config/ohos:runtime_library adds -nostdlib, which suppresses 57 # linking against all system libraries. -nostdlib++ would be redundant, 58 # and would generate an unused warning in this case. 59 if (!is_ohos) { 60 ldflags += [ "-nostdlib++" ] 61 } 62 } else { 63 # Gcc has a built-in abs() definition with default visibility. 64 # If it was not disabled, it would conflict with libc++'s abs() 65 # with hidden visibility. 66 cflags += [ "-fno-builtin-abs" ] 67 68 ldflags += [ "-nodefaultlibs" ] 69 70 # Unfortunately, there's no way to disable linking against just libc++ 71 # (gcc doesn't have -notstdlib++: 72 # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83931); -nodefaultlibs 73 # removes all of the default libraries, so add back the ones that we need. 74 libs += [ 75 "c", 76 "gcc_s", 77 "m", 78 "rt", 79 ] 80 } 81 } 82 83 if (!is_mac && sysroot != "") { 84 # Pass the sysroot to all C compiler variants, the assembler, and linker. 85 sysroot_flags = [ "--sysroot=" + rebase_path(sysroot, root_build_dir) ] 86 if (is_linux) { 87 # This is here so that all files get recompiled after a sysroot roll and 88 # when turning the sysroot on or off. (defines are passed via the command 89 # line, and build system rebuilds things when their commandline 90 # changes). Nothing should ever read this define. 91 sysroot_hash = 92 exec_script("//build/linux/sysroot_scripts/install-sysroot.py", 93 [ "--print-hash=$current_cpu" ], 94 "trim string", 95 [ "//build/linux/sysroot_scripts/sysroots.json" ]) 96 defines += [ "CR_SYSROOT_HASH=$sysroot_hash" ] 97 } 98 asmflags += sysroot_flags 99 100 link_sysroot_flags = 101 [ "--sysroot=" + rebase_path(link_sysroot, root_build_dir) ] 102 ldflags += link_sysroot_flags 103 104 # When use_custom_libcxx=true, some -isystem flags get passed to 105 # cflags_cc to set up libc++ include paths. We want to make sure 106 # the sysroot includes take lower precedence than the libc++ 107 # ones, so they must appear later in the command line. However, 108 # the gn reference states "These variant-specific versions of 109 # cflags* will be appended on the compiler command line after 110 # 'cflags'." Because of this, we must set the sysroot flags for 111 # all cflags variants instead of using 'cflags' directly. 112 cflags_c += sysroot_flags 113 cflags_cc += sysroot_flags 114 cflags_objc += sysroot_flags 115 cflags_objcc += sysroot_flags 116 117 # Need to get some linker flags out of the sysroot. 118 ld_paths = 119 exec_script("sysroot_ld_path.py", 120 [ 121 rebase_path("//build/misc/linux/sysroot_ld_path.sh", 122 root_build_dir), 123 rebase_path(link_sysroot), 124 ], 125 "list lines") 126 foreach(ld_path, ld_paths) { 127 ld_path = rebase_path(ld_path, root_build_dir) 128 ldflags += [ 129 "-L" + ld_path, 130 "-Wl,-rpath-link=" + ld_path, 131 ] 132 } 133 } 134} 135