15f9996aaSopenharmony_ci# Copyright 2014 The Chromium Authors. All rights reserved.
25f9996aaSopenharmony_ci# Use of this source code is governed by a BSD-style license that can be
35f9996aaSopenharmony_ci# found in the LICENSE file.
45f9996aaSopenharmony_ci
55f9996aaSopenharmony_ciimport("//build/config/c++/c++.gni")
65f9996aaSopenharmony_ciimport("//build/config/compiler/compiler.gni")
75f9996aaSopenharmony_ciimport("//build/config/sanitizers/sanitizers.gni")
85f9996aaSopenharmony_ciimport("//build/config/sysroot.gni")
95f9996aaSopenharmony_ciimport("//build/toolchain/toolchain.gni")
105f9996aaSopenharmony_ci
115f9996aaSopenharmony_cideclare_args() {
125f9996aaSopenharmony_ci  # When non empty, overrides the target rpath value. This allows a user to
135f9996aaSopenharmony_ci  # make a Chromium build where binaries and shared libraries are meant to be
145f9996aaSopenharmony_ci  # installed into separate directories, like /usr/bin/chromium and
155f9996aaSopenharmony_ci  # /usr/lib/chromium for instance. It is useful when a build system that
165f9996aaSopenharmony_ci  # generates a whole target root filesystem (like Yocto) is used on top of gn,
175f9996aaSopenharmony_ci  # especially when cross-compiling.
185f9996aaSopenharmony_ci  # Note: this gn arg is similar to gyp target_rpath generator flag.
195f9996aaSopenharmony_ci  gcc_target_rpath = ""
205f9996aaSopenharmony_ci  ldso_path = ""
215f9996aaSopenharmony_ci}
225f9996aaSopenharmony_ci
235f9996aaSopenharmony_ci# This config causes functions not to be automatically exported from shared
245f9996aaSopenharmony_ci# libraries. By default, all symbols are exported but this means there are
255f9996aaSopenharmony_ci# lots of exports that slow everything down. In general we explicitly mark
265f9996aaSopenharmony_ci# which functions we want to export from components.
275f9996aaSopenharmony_ci#
285f9996aaSopenharmony_ci# Some third_party code assumes all functions are exported so this is separated
295f9996aaSopenharmony_ci# into its own config so such libraries can remove this config to make symbols
305f9996aaSopenharmony_ci# public again.
315f9996aaSopenharmony_ci#
325f9996aaSopenharmony_ci# See http://gcc.gnu.org/wiki/Visibility
335f9996aaSopenharmony_ciconfig("symbol_visibility_hidden") {
345f9996aaSopenharmony_ci  cflags = [ "-fvisibility=hidden" ]
355f9996aaSopenharmony_ci
365f9996aaSopenharmony_ci  # Visibility attribute is not supported on AIX.
375f9996aaSopenharmony_ci  if (current_os != "aix") {
385f9996aaSopenharmony_ci    cflags_cc = [ "-fvisibility-inlines-hidden" ]
395f9996aaSopenharmony_ci    cflags_objcc = cflags_cc
405f9996aaSopenharmony_ci  }
415f9996aaSopenharmony_ci}
425f9996aaSopenharmony_ci
435f9996aaSopenharmony_ciconfig("symbol_visibility_inline_hidden") {
445f9996aaSopenharmony_ci  cflags_cc = [ "-fvisibility-inlines-hidden" ]
455f9996aaSopenharmony_ci}
465f9996aaSopenharmony_ci
475f9996aaSopenharmony_ci# This config is usually set when :symbol_visibility_hidden is removed.
485f9996aaSopenharmony_ci# It's often a good idea to set visibility explicitly, as there're flags
495f9996aaSopenharmony_ci# which would error out otherwise (e.g. -fsanitize=cfi-unrelated-cast)
505f9996aaSopenharmony_ciconfig("symbol_visibility_default") {
515f9996aaSopenharmony_ci  cflags = [ "-fvisibility=default" ]
525f9996aaSopenharmony_ci}
535f9996aaSopenharmony_ci
545f9996aaSopenharmony_ci# The rpath is the dynamic library search path. Setting this config on a link
555f9996aaSopenharmony_ci# step will put the directory where the build generates shared libraries into
565f9996aaSopenharmony_ci# the rpath.
575f9996aaSopenharmony_ci#
585f9996aaSopenharmony_ci# This is required for component builds since the build generates many shared
595f9996aaSopenharmony_ci# libraries in the build directory that we expect to be automatically loaded.
605f9996aaSopenharmony_ci# It will be automatically applied in this case by :executable_ldconfig.
615f9996aaSopenharmony_ci#
625f9996aaSopenharmony_ci# In non-component builds, certain test binaries may expect to load dynamic
635f9996aaSopenharmony_ci# libraries from the current directory. As long as these aren't distributed,
645f9996aaSopenharmony_ci# this is OK. For these cases use something like this:
655f9996aaSopenharmony_ci#
665f9996aaSopenharmony_ci#  if (is_linux && !is_component_build) {
675f9996aaSopenharmony_ci#    configs += [ "//build/config/gcc:rpath_for_built_shared_libraries" ]
685f9996aaSopenharmony_ci#  }
695f9996aaSopenharmony_ciconfig("rpath_for_built_shared_libraries") {
705f9996aaSopenharmony_ci  if (!is_ohos) {
715f9996aaSopenharmony_ci    # Note: ohos doesn't support rpath.
725f9996aaSopenharmony_ci    rpath_link = "."
735f9996aaSopenharmony_ci    if (current_toolchain != default_toolchain || gcc_target_rpath == "") {
745f9996aaSopenharmony_ci      ldflags = [
755f9996aaSopenharmony_ci        # Want to pass "\$". GN will re-escape as required for ninja.
765f9996aaSopenharmony_ci        "-Wl,-rpath=\$ORIGIN/${rpath_link}",
775f9996aaSopenharmony_ci        "-Wl,-rpath-link=${rpath_link}",
785f9996aaSopenharmony_ci      ]
795f9996aaSopenharmony_ci    } else {
805f9996aaSopenharmony_ci      ldflags = [
815f9996aaSopenharmony_ci        "-Wl,-rpath=${gcc_target_rpath}",
825f9996aaSopenharmony_ci        "-Wl,-rpath-link=${rpath_link}",
835f9996aaSopenharmony_ci      ]
845f9996aaSopenharmony_ci    }
855f9996aaSopenharmony_ci  }
865f9996aaSopenharmony_ci
875f9996aaSopenharmony_ci  if (!is_ohos) {
885f9996aaSopenharmony_ci    if (!defined(ldflags)) {
895f9996aaSopenharmony_ci      ldflags = []
905f9996aaSopenharmony_ci    }
915f9996aaSopenharmony_ci    if (current_toolchain == default_toolchain && ldso_path != "") {
925f9996aaSopenharmony_ci      ldflags += [ "-Wl,--dynamic-linker=${ldso_path}" ]
935f9996aaSopenharmony_ci    }
945f9996aaSopenharmony_ci  }
955f9996aaSopenharmony_ci}
965f9996aaSopenharmony_ci
975f9996aaSopenharmony_ci# Settings for executables.
985f9996aaSopenharmony_ciconfig("executable_ldconfig") {
995f9996aaSopenharmony_ci  ldflags = []
1005f9996aaSopenharmony_ci
1015f9996aaSopenharmony_ci  if (is_ohos) {
1025f9996aaSopenharmony_ci    ldflags += [
1035f9996aaSopenharmony_ci      "-Bdynamic",
1045f9996aaSopenharmony_ci      "-Wl,-z,nocopyreloc",
1055f9996aaSopenharmony_ci    ]
1065f9996aaSopenharmony_ci  }
1075f9996aaSopenharmony_ci
1085f9996aaSopenharmony_ci  if (!is_ohos) {
1095f9996aaSopenharmony_ci    # See the rpath_for... config above for why this is necessary for component
1105f9996aaSopenharmony_ci    # builds.
1115f9996aaSopenharmony_ci    if (is_component_build) {
1125f9996aaSopenharmony_ci      configs = [ ":rpath_for_built_shared_libraries" ]
1135f9996aaSopenharmony_ci    }
1145f9996aaSopenharmony_ci  }
1155f9996aaSopenharmony_ci
1165f9996aaSopenharmony_ci  if (!is_ohos && current_os != "aix") {
1175f9996aaSopenharmony_ci    # Find the path containing shared libraries for this toolchain
1185f9996aaSopenharmony_ci    # relative to the build directory. ${root_out_dir} will be a
1195f9996aaSopenharmony_ci    # subdirectory of ${root_build_dir} when cross compiling.
1205f9996aaSopenharmony_ci    _rpath_link = rebase_path(root_out_dir, root_build_dir)
1215f9996aaSopenharmony_ci    ldflags += [
1225f9996aaSopenharmony_ci      "-Wl,-rpath-link=$_rpath_link",
1235f9996aaSopenharmony_ci
1245f9996aaSopenharmony_ci      # Newer binutils don't set DT_RPATH unless you disable "new" dtags
1255f9996aaSopenharmony_ci      # and the new DT_RUNPATH doesn't work without --no-as-needed flag.
1265f9996aaSopenharmony_ci      "-Wl,--disable-new-dtags",
1275f9996aaSopenharmony_ci    ]
1285f9996aaSopenharmony_ci  }
1295f9996aaSopenharmony_ci}
130