xref: /aosp_15_r20/external/cronet/build/config/gcc/BUILD.gn (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1# Copyright 2014 The Chromium Authors
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/compiler/compiler.gni")
7import("//build/config/sanitizers/sanitizers.gni")
8import("//build/config/sysroot.gni")
9import("//build/toolchain/toolchain.gni")
10
11declare_args() {
12  # When non empty, overrides the target rpath value. This allows a user to
13  # make a Chromium build where binaries and shared libraries are meant to be
14  # installed into separate directories, like /usr/bin/chromium and
15  # /usr/lib/chromium for instance. It is useful when a build system that
16  # generates a whole target root filesystem (like Yocto) is used on top of gn,
17  # especially when cross-compiling.
18  # Note: this gn arg is similar to gyp target_rpath generator flag.
19  gcc_target_rpath = ""
20  ldso_path = ""
21}
22
23# This config causes functions not to be automatically exported from shared
24# libraries. By default, all symbols are exported but this means there are
25# lots of exports that slow everything down. In general we explicitly mark
26# which functions we want to export from components.
27#
28# Some third_party code assumes all functions are exported so this is separated
29# into its own config so such libraries can remove this config to make symbols
30# public again.
31#
32# See http://gcc.gnu.org/wiki/Visibility
33config("symbol_visibility_hidden") {
34  cflags = [ "-fvisibility=hidden" ]
35
36  # Visibility attribute is not supported on AIX.
37  if (current_os != "aix") {
38    cflags_cc = [ "-fvisibility-inlines-hidden" ]
39    cflags_objcc = cflags_cc
40  }
41}
42
43# This config is usually set when :symbol_visibility_hidden is removed.
44# It's often a good idea to set visibility explicitly, as there're flags
45# which would error out otherwise (e.g. -fsanitize=cfi-unrelated-cast)
46config("symbol_visibility_default") {
47  cflags = [ "-fvisibility=default" ]
48}
49
50# The rpath is the dynamic library search path. Setting this config on a link
51# step will put the directory where the build generates shared libraries into
52# the rpath.
53#
54# This is required for component builds since the build generates many shared
55# libraries in the build directory that we expect to be automatically loaded.
56# It will be automatically applied in this case by :executable_config.
57#
58# In non-component builds, certain test binaries may expect to load dynamic
59# libraries from the current directory. As long as these aren't distributed,
60# this is OK. For these cases use something like this:
61#
62#  if ((is_linux || is_chromeos) && !is_component_build) {
63#    configs += [ "//build/config/gcc:rpath_for_built_shared_libraries" ]
64#  }
65config("rpath_for_built_shared_libraries") {
66  if (!is_android && current_os != "aix" && !is_castos) {
67    # Note: Android, Aix don't support rpath. Chromecast has its own logic for
68    # setting the rpath in //build/config/chromecast.
69    if (current_toolchain != default_toolchain || gcc_target_rpath == "") {
70      ldflags = [
71        # Want to pass "\$". GN will re-escape as required for ninja.
72        "-Wl,-rpath=\$ORIGIN",
73      ]
74    } else {
75      ldflags = [ "-Wl,-rpath=${gcc_target_rpath}" ]
76    }
77    if (current_toolchain == default_toolchain && ldso_path != "") {
78      ldflags += [ "-Wl,--dynamic-linker=${ldso_path}" ]
79    }
80  }
81}
82
83if (is_component_build && !is_android) {
84  # See the rpath_for... config above for why this is necessary for component
85  # builds.
86  executable_and_shared_library_configs_ =
87      [ ":rpath_for_built_shared_libraries" ]
88} else {
89  executable_and_shared_library_configs_ = []
90}
91
92# Settings for executables.
93config("executable_config") {
94  configs = executable_and_shared_library_configs_
95  ldflags = [ "-pie" ]
96  if (is_android) {
97    ldflags += [
98      "-Bdynamic",
99      "-Wl,-z,nocopyreloc",
100    ]
101  }
102
103  if (!is_android && current_os != "aix") {
104    ldflags += [
105      # TODO(GYP): Do we need a check on the binutils version here?
106      #
107      # Newer binutils don't set DT_RPATH unless you disable "new" dtags
108      # and the new DT_RUNPATH doesn't work without --no-as-needed flag.
109      "-Wl,--disable-new-dtags",
110    ]
111  }
112}
113
114# Settings for shared libraries.
115config("shared_library_config") {
116  configs = executable_and_shared_library_configs_
117}
118