xref: /aosp_15_r20/external/pdfium/build_overrides/BUILDCONFIG.gn (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1*3ac0a46fSAndroid Build Coastguard Worker# Copyright 2022 The PDFium Authors
2*3ac0a46fSAndroid Build Coastguard Worker# Use of this source code is governed by a BSD-style license that can be
3*3ac0a46fSAndroid Build Coastguard Worker# found in the LICENSE file.
4*3ac0a46fSAndroid Build Coastguard Worker
5*3ac0a46fSAndroid Build Coastguard Worker# =============================================================================
6*3ac0a46fSAndroid Build Coastguard Worker# WHAT IS THIS FILE?
7*3ac0a46fSAndroid Build Coastguard Worker# =============================================================================
8*3ac0a46fSAndroid Build Coastguard Worker#
9*3ac0a46fSAndroid Build Coastguard Worker# This is a copy of //build/config/BUILDCONFIG.gn. The difference is it adds an
10*3ac0a46fSAndroid Build Coastguard Worker# extra default_compiler_configs to use PDFium's desired default compiler
11*3ac0a46fSAndroid Build Coastguard Worker# config. See "PDFIUM MODIFICATIONS" below.
12*3ac0a46fSAndroid Build Coastguard Worker#
13*3ac0a46fSAndroid Build Coastguard Worker# This is the main GN build configuration. This file is loaded after the
14*3ac0a46fSAndroid Build Coastguard Worker# build args (args.gn) for the build directory and after the toplevel ".gn"
15*3ac0a46fSAndroid Build Coastguard Worker# file (which points to this file as the build configuration).
16*3ac0a46fSAndroid Build Coastguard Worker#
17*3ac0a46fSAndroid Build Coastguard Worker# This file will be executed and the resulting context will be used to execute
18*3ac0a46fSAndroid Build Coastguard Worker# every other file in the build. So variables declared here (that don't start
19*3ac0a46fSAndroid Build Coastguard Worker# with an underscore) will be implicitly global.
20*3ac0a46fSAndroid Build Coastguard Worker
21*3ac0a46fSAndroid Build Coastguard Worker# =============================================================================
22*3ac0a46fSAndroid Build Coastguard Worker# PLATFORM SELECTION
23*3ac0a46fSAndroid Build Coastguard Worker# =============================================================================
24*3ac0a46fSAndroid Build Coastguard Worker#
25*3ac0a46fSAndroid Build Coastguard Worker# There are two main things to set: "os" and "cpu". The "toolchain" is the name
26*3ac0a46fSAndroid Build Coastguard Worker# of the GN thing that encodes combinations of these things.
27*3ac0a46fSAndroid Build Coastguard Worker#
28*3ac0a46fSAndroid Build Coastguard Worker# Users typically only set the variables "target_os" and "target_cpu" in "gn
29*3ac0a46fSAndroid Build Coastguard Worker# args", the rest are set up by our build and internal to GN.
30*3ac0a46fSAndroid Build Coastguard Worker#
31*3ac0a46fSAndroid Build Coastguard Worker# There are three different types of each of these things: The "host"
32*3ac0a46fSAndroid Build Coastguard Worker# represents the computer doing the compile and never changes. The "target"
33*3ac0a46fSAndroid Build Coastguard Worker# represents the main thing we're trying to build. The "current" represents
34*3ac0a46fSAndroid Build Coastguard Worker# which configuration is currently being defined, which can be either the
35*3ac0a46fSAndroid Build Coastguard Worker# host, the target, or something completely different (like nacl). GN will
36*3ac0a46fSAndroid Build Coastguard Worker# run the same build file multiple times for the different required
37*3ac0a46fSAndroid Build Coastguard Worker# configuration in the same build.
38*3ac0a46fSAndroid Build Coastguard Worker#
39*3ac0a46fSAndroid Build Coastguard Worker# This gives the following variables:
40*3ac0a46fSAndroid Build Coastguard Worker#  - host_os, host_cpu, host_toolchain
41*3ac0a46fSAndroid Build Coastguard Worker#  - target_os, target_cpu, default_toolchain
42*3ac0a46fSAndroid Build Coastguard Worker#  - current_os, current_cpu, current_toolchain.
43*3ac0a46fSAndroid Build Coastguard Worker#
44*3ac0a46fSAndroid Build Coastguard Worker# Note the default_toolchain isn't symmetrical (you would expect
45*3ac0a46fSAndroid Build Coastguard Worker# target_toolchain). This is because the "default" toolchain is a GN built-in
46*3ac0a46fSAndroid Build Coastguard Worker# concept, and "target" is something our build sets up that's symmetrical with
47*3ac0a46fSAndroid Build Coastguard Worker# its GYP counterpart. Potentially the built-in default_toolchain variable
48*3ac0a46fSAndroid Build Coastguard Worker# could be renamed in the future.
49*3ac0a46fSAndroid Build Coastguard Worker#
50*3ac0a46fSAndroid Build Coastguard Worker# When writing build files, to do something only for the host:
51*3ac0a46fSAndroid Build Coastguard Worker#   if (current_toolchain == host_toolchain) { ...
52*3ac0a46fSAndroid Build Coastguard Worker
53*3ac0a46fSAndroid Build Coastguard Workerif (target_os == "") {
54*3ac0a46fSAndroid Build Coastguard Worker  target_os = host_os
55*3ac0a46fSAndroid Build Coastguard Worker}
56*3ac0a46fSAndroid Build Coastguard Worker
57*3ac0a46fSAndroid Build Coastguard Workerif (target_cpu == "") {
58*3ac0a46fSAndroid Build Coastguard Worker  if (target_os == "android") {
59*3ac0a46fSAndroid Build Coastguard Worker    # If we're building for Android, we should assume that we want to
60*3ac0a46fSAndroid Build Coastguard Worker    # build for ARM by default, not the host_cpu (which is likely x64).
61*3ac0a46fSAndroid Build Coastguard Worker    # This allows us to not have to specify both target_os and target_cpu
62*3ac0a46fSAndroid Build Coastguard Worker    # on the command line.
63*3ac0a46fSAndroid Build Coastguard Worker    target_cpu = "arm"
64*3ac0a46fSAndroid Build Coastguard Worker  } else {
65*3ac0a46fSAndroid Build Coastguard Worker    target_cpu = host_cpu
66*3ac0a46fSAndroid Build Coastguard Worker  }
67*3ac0a46fSAndroid Build Coastguard Worker}
68*3ac0a46fSAndroid Build Coastguard Worker
69*3ac0a46fSAndroid Build Coastguard Workerif (current_cpu == "") {
70*3ac0a46fSAndroid Build Coastguard Worker  current_cpu = target_cpu
71*3ac0a46fSAndroid Build Coastguard Worker}
72*3ac0a46fSAndroid Build Coastguard Workerif (current_os == "") {
73*3ac0a46fSAndroid Build Coastguard Worker  current_os = target_os
74*3ac0a46fSAndroid Build Coastguard Worker}
75*3ac0a46fSAndroid Build Coastguard Worker
76*3ac0a46fSAndroid Build Coastguard Worker# =============================================================================
77*3ac0a46fSAndroid Build Coastguard Worker# BUILD FLAGS
78*3ac0a46fSAndroid Build Coastguard Worker# =============================================================================
79*3ac0a46fSAndroid Build Coastguard Worker#
80*3ac0a46fSAndroid Build Coastguard Worker# This block lists input arguments to the build, along with their default
81*3ac0a46fSAndroid Build Coastguard Worker# values.
82*3ac0a46fSAndroid Build Coastguard Worker#
83*3ac0a46fSAndroid Build Coastguard Worker# If a value is specified on the command line, it will overwrite the defaults
84*3ac0a46fSAndroid Build Coastguard Worker# given in a declare_args block, otherwise the default will be used.
85*3ac0a46fSAndroid Build Coastguard Worker#
86*3ac0a46fSAndroid Build Coastguard Worker# YOU SHOULD ALMOST NEVER NEED TO ADD FLAGS TO THIS FILE. GN allows any file in
87*3ac0a46fSAndroid Build Coastguard Worker# the build to declare build flags. If you need a flag for a single component,
88*3ac0a46fSAndroid Build Coastguard Worker# you can just declare it in the corresponding BUILD.gn file.
89*3ac0a46fSAndroid Build Coastguard Worker#
90*3ac0a46fSAndroid Build Coastguard Worker# - If your feature is a single target, say //components/foo, you can put
91*3ac0a46fSAndroid Build Coastguard Worker#   a declare_args() block in //components/foo/BUILD.gn and use it there.
92*3ac0a46fSAndroid Build Coastguard Worker#   Nobody else in the build needs to see the flag.
93*3ac0a46fSAndroid Build Coastguard Worker#
94*3ac0a46fSAndroid Build Coastguard Worker# - Defines based on build variables should be implemented via the generated
95*3ac0a46fSAndroid Build Coastguard Worker#   build flag header system. See //build/buildflag_header.gni. You can put
96*3ac0a46fSAndroid Build Coastguard Worker#   the buildflag_header target in the same file as the build flag itself. You
97*3ac0a46fSAndroid Build Coastguard Worker#   should almost never set "defines" directly.
98*3ac0a46fSAndroid Build Coastguard Worker#
99*3ac0a46fSAndroid Build Coastguard Worker# - If your flag toggles a target on and off or toggles between different
100*3ac0a46fSAndroid Build Coastguard Worker#   versions of similar things, write a "group" target that forwards to the
101*3ac0a46fSAndroid Build Coastguard Worker#   right target (or no target) depending on the value of the build flag. This
102*3ac0a46fSAndroid Build Coastguard Worker#   group can be in the same BUILD.gn file as the build flag, and targets can
103*3ac0a46fSAndroid Build Coastguard Worker#   depend unconditionally on the group rather than duplicating flag checks
104*3ac0a46fSAndroid Build Coastguard Worker#   across many targets.
105*3ac0a46fSAndroid Build Coastguard Worker#
106*3ac0a46fSAndroid Build Coastguard Worker# - If a semi-random set of build files REALLY needs to know about a define and
107*3ac0a46fSAndroid Build Coastguard Worker#   the above pattern for isolating the build logic in a forwarding group
108*3ac0a46fSAndroid Build Coastguard Worker#   doesn't work, you can put the argument in a .gni file. This should be put
109*3ac0a46fSAndroid Build Coastguard Worker#   in the lowest level of the build that knows about this feature (which should
110*3ac0a46fSAndroid Build Coastguard Worker#   almost always be outside of the //build directory!).
111*3ac0a46fSAndroid Build Coastguard Worker#
112*3ac0a46fSAndroid Build Coastguard Worker# Other flag advice:
113*3ac0a46fSAndroid Build Coastguard Worker#
114*3ac0a46fSAndroid Build Coastguard Worker# - Use boolean values when possible. If you need a default value that expands
115*3ac0a46fSAndroid Build Coastguard Worker#   to some complex thing in the default case (like the location of the
116*3ac0a46fSAndroid Build Coastguard Worker#   compiler which would be computed by a script), use a default value of -1 or
117*3ac0a46fSAndroid Build Coastguard Worker#   the empty string. Outside of the declare_args block, conditionally expand
118*3ac0a46fSAndroid Build Coastguard Worker#   the default value as necessary.
119*3ac0a46fSAndroid Build Coastguard Worker#
120*3ac0a46fSAndroid Build Coastguard Worker# - Use a name like "use_foo" or "is_foo" (whatever is more appropriate for
121*3ac0a46fSAndroid Build Coastguard Worker#   your feature) rather than just "foo".
122*3ac0a46fSAndroid Build Coastguard Worker#
123*3ac0a46fSAndroid Build Coastguard Worker# - Write good comments directly above the declaration with no blank line.
124*3ac0a46fSAndroid Build Coastguard Worker#   These comments will appear as documentation in "gn args --list".
125*3ac0a46fSAndroid Build Coastguard Worker#
126*3ac0a46fSAndroid Build Coastguard Worker# - Don't call exec_script inside declare_args. This will execute the script
127*3ac0a46fSAndroid Build Coastguard Worker#   even if the value is overridden, which is wasteful. See first bullet.
128*3ac0a46fSAndroid Build Coastguard Worker
129*3ac0a46fSAndroid Build Coastguard Workerdeclare_args() {
130*3ac0a46fSAndroid Build Coastguard Worker  # Set to enable the official build level of optimization. This has nothing
131*3ac0a46fSAndroid Build Coastguard Worker  # to do with branding, but enables an additional level of optimization above
132*3ac0a46fSAndroid Build Coastguard Worker  # release (!is_debug). This might be better expressed as a tri-state
133*3ac0a46fSAndroid Build Coastguard Worker  # (debug, release, official) but for historical reasons there are two
134*3ac0a46fSAndroid Build Coastguard Worker  # separate flags.
135*3ac0a46fSAndroid Build Coastguard Worker  #
136*3ac0a46fSAndroid Build Coastguard Worker  # IMPORTANT NOTE: (!is_debug) is *not* sufficient to get satisfying
137*3ac0a46fSAndroid Build Coastguard Worker  # performance. In particular, DCHECK()s are still enabled for release builds,
138*3ac0a46fSAndroid Build Coastguard Worker  # which can halve overall performance, and do increase memory usage. Always
139*3ac0a46fSAndroid Build Coastguard Worker  # set "is_official_build" to true for any build intended to ship to end-users.
140*3ac0a46fSAndroid Build Coastguard Worker  is_official_build = false
141*3ac0a46fSAndroid Build Coastguard Worker
142*3ac0a46fSAndroid Build Coastguard Worker  # Set to true when compiling with the Clang compiler.
143*3ac0a46fSAndroid Build Coastguard Worker  is_clang = current_os != "linux" ||
144*3ac0a46fSAndroid Build Coastguard Worker             (current_cpu != "s390x" && current_cpu != "s390" &&
145*3ac0a46fSAndroid Build Coastguard Worker              current_cpu != "ppc64" && current_cpu != "ppc" &&
146*3ac0a46fSAndroid Build Coastguard Worker              current_cpu != "mips" && current_cpu != "mips64" &&
147*3ac0a46fSAndroid Build Coastguard Worker              current_cpu != "riscv64")
148*3ac0a46fSAndroid Build Coastguard Worker
149*3ac0a46fSAndroid Build Coastguard Worker  # Allows the path to a custom target toolchain to be injected as a single
150*3ac0a46fSAndroid Build Coastguard Worker  # argument, and set as the default toolchain.
151*3ac0a46fSAndroid Build Coastguard Worker  custom_toolchain = ""
152*3ac0a46fSAndroid Build Coastguard Worker
153*3ac0a46fSAndroid Build Coastguard Worker  # This should not normally be set as a build argument.  It's here so that
154*3ac0a46fSAndroid Build Coastguard Worker  # every toolchain can pass through the "global" value via toolchain_args().
155*3ac0a46fSAndroid Build Coastguard Worker  host_toolchain = ""
156*3ac0a46fSAndroid Build Coastguard Worker
157*3ac0a46fSAndroid Build Coastguard Worker  # Do not set this directly.
158*3ac0a46fSAndroid Build Coastguard Worker  # It should be set only by //build/toolchains/android:robolectric_x64.
159*3ac0a46fSAndroid Build Coastguard Worker  # True when compiling native code for use with robolectric_binary().
160*3ac0a46fSAndroid Build Coastguard Worker  is_robolectric = false
161*3ac0a46fSAndroid Build Coastguard Worker
162*3ac0a46fSAndroid Build Coastguard Worker  # DON'T ADD MORE FLAGS HERE. Read the comment above.
163*3ac0a46fSAndroid Build Coastguard Worker}
164*3ac0a46fSAndroid Build Coastguard Worker
165*3ac0a46fSAndroid Build Coastguard Workerdeclare_args() {
166*3ac0a46fSAndroid Build Coastguard Worker  # Debug build. Enabling official builds automatically sets is_debug to false.
167*3ac0a46fSAndroid Build Coastguard Worker  is_debug = !is_official_build
168*3ac0a46fSAndroid Build Coastguard Worker}
169*3ac0a46fSAndroid Build Coastguard Worker
170*3ac0a46fSAndroid Build Coastguard Workerdeclare_args() {
171*3ac0a46fSAndroid Build Coastguard Worker  # Component build. Setting to true compiles targets declared as "components"
172*3ac0a46fSAndroid Build Coastguard Worker  # as shared libraries loaded dynamically. This speeds up development time.
173*3ac0a46fSAndroid Build Coastguard Worker  # When false, components will be linked statically.
174*3ac0a46fSAndroid Build Coastguard Worker  #
175*3ac0a46fSAndroid Build Coastguard Worker  # For more information see
176*3ac0a46fSAndroid Build Coastguard Worker  # https://chromium.googlesource.com/chromium/src/+/main/docs/component_build.md
177*3ac0a46fSAndroid Build Coastguard Worker  is_component_build = is_debug && current_os != "ios"
178*3ac0a46fSAndroid Build Coastguard Worker}
179*3ac0a46fSAndroid Build Coastguard Worker
180*3ac0a46fSAndroid Build Coastguard Workerassert(!(is_debug && is_official_build), "Can't do official debug builds")
181*3ac0a46fSAndroid Build Coastguard Workerassert(!(current_os == "ios" && is_component_build),
182*3ac0a46fSAndroid Build Coastguard Worker       "Can't use component build on iOS")
183*3ac0a46fSAndroid Build Coastguard Worker
184*3ac0a46fSAndroid Build Coastguard Worker# ==============================================================================
185*3ac0a46fSAndroid Build Coastguard Worker# TOOLCHAIN SETUP
186*3ac0a46fSAndroid Build Coastguard Worker# ==============================================================================
187*3ac0a46fSAndroid Build Coastguard Worker#
188*3ac0a46fSAndroid Build Coastguard Worker# Here we set the default toolchain, as well as the variable host_toolchain
189*3ac0a46fSAndroid Build Coastguard Worker# which will identify the toolchain corresponding to the local system when
190*3ac0a46fSAndroid Build Coastguard Worker# doing cross-compiles. When not cross-compiling, this will be the same as the
191*3ac0a46fSAndroid Build Coastguard Worker# default toolchain.
192*3ac0a46fSAndroid Build Coastguard Worker#
193*3ac0a46fSAndroid Build Coastguard Worker# We do this before anything else to make sure we complain about any
194*3ac0a46fSAndroid Build Coastguard Worker# unsupported os/cpu combinations as early as possible.
195*3ac0a46fSAndroid Build Coastguard Worker
196*3ac0a46fSAndroid Build Coastguard Workerif (host_toolchain == "") {
197*3ac0a46fSAndroid Build Coastguard Worker  # This should only happen in the top-level context.
198*3ac0a46fSAndroid Build Coastguard Worker  # In a specific toolchain context, the toolchain_args()
199*3ac0a46fSAndroid Build Coastguard Worker  # block should have propagated a value down.
200*3ac0a46fSAndroid Build Coastguard Worker  # TODO(dpranke): Add some sort of assert here that verifies that
201*3ac0a46fSAndroid Build Coastguard Worker  # no toolchain omitted host_toolchain from its toolchain_args().
202*3ac0a46fSAndroid Build Coastguard Worker
203*3ac0a46fSAndroid Build Coastguard Worker  if (host_os == "linux") {
204*3ac0a46fSAndroid Build Coastguard Worker    if (target_os != "linux") {
205*3ac0a46fSAndroid Build Coastguard Worker      host_toolchain = "//build/toolchain/linux:clang_$host_cpu"
206*3ac0a46fSAndroid Build Coastguard Worker    } else if (is_clang) {
207*3ac0a46fSAndroid Build Coastguard Worker      host_toolchain = "//build/toolchain/linux:clang_$host_cpu"
208*3ac0a46fSAndroid Build Coastguard Worker    } else {
209*3ac0a46fSAndroid Build Coastguard Worker      host_toolchain = "//build/toolchain/linux:$host_cpu"
210*3ac0a46fSAndroid Build Coastguard Worker    }
211*3ac0a46fSAndroid Build Coastguard Worker  } else if (host_os == "mac") {
212*3ac0a46fSAndroid Build Coastguard Worker    host_toolchain = "//build/toolchain/mac:clang_$host_cpu"
213*3ac0a46fSAndroid Build Coastguard Worker  } else if (host_os == "win") {
214*3ac0a46fSAndroid Build Coastguard Worker    # On Windows always use the target CPU for host builds for x86/x64. On the
215*3ac0a46fSAndroid Build Coastguard Worker    # configurations we support this will always work and it saves build steps.
216*3ac0a46fSAndroid Build Coastguard Worker    # Windows ARM64 targets require an x64 host for cross build.
217*3ac0a46fSAndroid Build Coastguard Worker    if (target_cpu == "x86" || target_cpu == "x64") {
218*3ac0a46fSAndroid Build Coastguard Worker      if (is_clang) {
219*3ac0a46fSAndroid Build Coastguard Worker        host_toolchain = "//build/toolchain/win:win_clang_$target_cpu"
220*3ac0a46fSAndroid Build Coastguard Worker      } else {
221*3ac0a46fSAndroid Build Coastguard Worker        host_toolchain = "//build/toolchain/win:$target_cpu"
222*3ac0a46fSAndroid Build Coastguard Worker      }
223*3ac0a46fSAndroid Build Coastguard Worker    } else if (is_clang) {
224*3ac0a46fSAndroid Build Coastguard Worker      host_toolchain = "//build/toolchain/win:win_clang_$host_cpu"
225*3ac0a46fSAndroid Build Coastguard Worker    } else {
226*3ac0a46fSAndroid Build Coastguard Worker      host_toolchain = "//build/toolchain/win:$host_cpu"
227*3ac0a46fSAndroid Build Coastguard Worker    }
228*3ac0a46fSAndroid Build Coastguard Worker  } else if (host_os == "aix") {
229*3ac0a46fSAndroid Build Coastguard Worker    host_toolchain = "//build/toolchain/aix:$host_cpu"
230*3ac0a46fSAndroid Build Coastguard Worker  } else if (host_os == "zos") {
231*3ac0a46fSAndroid Build Coastguard Worker    host_toolchain = "//build/toolchain/zos:$host_cpu"
232*3ac0a46fSAndroid Build Coastguard Worker  } else {
233*3ac0a46fSAndroid Build Coastguard Worker    assert(false, "Unsupported host_os: $host_os")
234*3ac0a46fSAndroid Build Coastguard Worker  }
235*3ac0a46fSAndroid Build Coastguard Worker}
236*3ac0a46fSAndroid Build Coastguard Worker
237*3ac0a46fSAndroid Build Coastguard Worker_default_toolchain = ""
238*3ac0a46fSAndroid Build Coastguard Worker
239*3ac0a46fSAndroid Build Coastguard Workerif (target_os == "android") {
240*3ac0a46fSAndroid Build Coastguard Worker  assert(host_os == "linux", "Android builds are only supported on Linux.")
241*3ac0a46fSAndroid Build Coastguard Worker  _default_toolchain = "//build/toolchain/android:android_clang_$target_cpu"
242*3ac0a46fSAndroid Build Coastguard Worker} else if (target_os == "chromeos" || target_os == "linux") {
243*3ac0a46fSAndroid Build Coastguard Worker  # See comments in build/toolchain/cros/BUILD.gn about board compiles.
244*3ac0a46fSAndroid Build Coastguard Worker  if (is_clang) {
245*3ac0a46fSAndroid Build Coastguard Worker    _default_toolchain = "//build/toolchain/linux:clang_$target_cpu"
246*3ac0a46fSAndroid Build Coastguard Worker  } else {
247*3ac0a46fSAndroid Build Coastguard Worker    _default_toolchain = "//build/toolchain/linux:$target_cpu"
248*3ac0a46fSAndroid Build Coastguard Worker  }
249*3ac0a46fSAndroid Build Coastguard Worker} else if (target_os == "fuchsia") {
250*3ac0a46fSAndroid Build Coastguard Worker  _default_toolchain = "//build/toolchain/fuchsia:$target_cpu"
251*3ac0a46fSAndroid Build Coastguard Worker} else if (target_os == "ios") {
252*3ac0a46fSAndroid Build Coastguard Worker  _default_toolchain = "//build/toolchain/ios:ios_clang_$target_cpu"
253*3ac0a46fSAndroid Build Coastguard Worker} else if (target_os == "mac") {
254*3ac0a46fSAndroid Build Coastguard Worker  assert(host_os == "mac" || host_os == "linux",
255*3ac0a46fSAndroid Build Coastguard Worker         "Mac cross-compiles are unsupported.")
256*3ac0a46fSAndroid Build Coastguard Worker  _default_toolchain = "//build/toolchain/mac:clang_$target_cpu"
257*3ac0a46fSAndroid Build Coastguard Worker} else if (target_os == "win") {
258*3ac0a46fSAndroid Build Coastguard Worker  # On Windows, we use the same toolchain for host and target by default.
259*3ac0a46fSAndroid Build Coastguard Worker  # Beware, win cross builds have some caveats, see docs/win_cross.md
260*3ac0a46fSAndroid Build Coastguard Worker  if (is_clang) {
261*3ac0a46fSAndroid Build Coastguard Worker    _default_toolchain = "//build/toolchain/win:win_clang_$target_cpu"
262*3ac0a46fSAndroid Build Coastguard Worker  } else {
263*3ac0a46fSAndroid Build Coastguard Worker    _default_toolchain = "//build/toolchain/win:$target_cpu"
264*3ac0a46fSAndroid Build Coastguard Worker  }
265*3ac0a46fSAndroid Build Coastguard Worker} else if (target_os == "winuwp") {
266*3ac0a46fSAndroid Build Coastguard Worker  # Only target WinUWP on for a Windows store application and only
267*3ac0a46fSAndroid Build Coastguard Worker  # x86, x64 and arm are supported target CPUs.
268*3ac0a46fSAndroid Build Coastguard Worker  assert(target_cpu == "x86" || target_cpu == "x64" || target_cpu == "arm" ||
269*3ac0a46fSAndroid Build Coastguard Worker         target_cpu == "arm64")
270*3ac0a46fSAndroid Build Coastguard Worker  _default_toolchain = "//build/toolchain/win:uwp_$target_cpu"
271*3ac0a46fSAndroid Build Coastguard Worker} else if (target_os == "aix") {
272*3ac0a46fSAndroid Build Coastguard Worker  _default_toolchain = "//build/toolchain/aix:$target_cpu"
273*3ac0a46fSAndroid Build Coastguard Worker} else if (target_os == "zos") {
274*3ac0a46fSAndroid Build Coastguard Worker  _default_toolchain = "//build/toolchain/zos:$target_cpu"
275*3ac0a46fSAndroid Build Coastguard Worker} else {
276*3ac0a46fSAndroid Build Coastguard Worker  assert(false, "Unsupported target_os: $target_os")
277*3ac0a46fSAndroid Build Coastguard Worker}
278*3ac0a46fSAndroid Build Coastguard Worker
279*3ac0a46fSAndroid Build Coastguard Worker# If a custom toolchain has been set in the args, set it as default. Otherwise,
280*3ac0a46fSAndroid Build Coastguard Worker# set the default toolchain for the platform (if any).
281*3ac0a46fSAndroid Build Coastguard Workerif (custom_toolchain != "") {
282*3ac0a46fSAndroid Build Coastguard Worker  set_default_toolchain(custom_toolchain)
283*3ac0a46fSAndroid Build Coastguard Worker} else if (_default_toolchain != "") {
284*3ac0a46fSAndroid Build Coastguard Worker  set_default_toolchain(_default_toolchain)
285*3ac0a46fSAndroid Build Coastguard Worker}
286*3ac0a46fSAndroid Build Coastguard Worker
287*3ac0a46fSAndroid Build Coastguard Worker# =============================================================================
288*3ac0a46fSAndroid Build Coastguard Worker# OS DEFINITIONS
289*3ac0a46fSAndroid Build Coastguard Worker# =============================================================================
290*3ac0a46fSAndroid Build Coastguard Worker#
291*3ac0a46fSAndroid Build Coastguard Worker# We set these various is_FOO booleans for convenience in writing OS-based
292*3ac0a46fSAndroid Build Coastguard Worker# conditions.
293*3ac0a46fSAndroid Build Coastguard Worker#
294*3ac0a46fSAndroid Build Coastguard Worker# - is_android, is_chromeos, is_ios, and is_win should be obvious.
295*3ac0a46fSAndroid Build Coastguard Worker# - is_mac is set only for desktop Mac. It is not set on iOS.
296*3ac0a46fSAndroid Build Coastguard Worker# - is_posix is true for mac and any Unix-like system (basically everything
297*3ac0a46fSAndroid Build Coastguard Worker#   except Fuchsia and Windows).
298*3ac0a46fSAndroid Build Coastguard Worker# - is_linux is true for desktop Linux, but not for ChromeOS nor Android (which
299*3ac0a46fSAndroid Build Coastguard Worker#   is generally too different despite being based on the Linux kernel).
300*3ac0a46fSAndroid Build Coastguard Worker#
301*3ac0a46fSAndroid Build Coastguard Worker# Do not add more is_* variants here for random lesser-used Unix systems like
302*3ac0a46fSAndroid Build Coastguard Worker# aix or one of the BSDs. If you need to check these, just check the
303*3ac0a46fSAndroid Build Coastguard Worker# current_os value directly.
304*3ac0a46fSAndroid Build Coastguard Worker
305*3ac0a46fSAndroid Build Coastguard Workeris_android = current_os == "android"
306*3ac0a46fSAndroid Build Coastguard Workeris_chromeos = current_os == "chromeos"
307*3ac0a46fSAndroid Build Coastguard Workeris_fuchsia = current_os == "fuchsia"
308*3ac0a46fSAndroid Build Coastguard Workeris_ios = current_os == "ios"
309*3ac0a46fSAndroid Build Coastguard Workeris_linux = current_os == "linux"
310*3ac0a46fSAndroid Build Coastguard Workeris_mac = current_os == "mac"
311*3ac0a46fSAndroid Build Coastguard Workeris_nacl = current_os == "nacl"
312*3ac0a46fSAndroid Build Coastguard Workeris_win = current_os == "win" || current_os == "winuwp"
313*3ac0a46fSAndroid Build Coastguard Worker
314*3ac0a46fSAndroid Build Coastguard Workeris_apple = is_ios || is_mac
315*3ac0a46fSAndroid Build Coastguard Workeris_posix = !is_win && !is_fuchsia
316*3ac0a46fSAndroid Build Coastguard Worker
317*3ac0a46fSAndroid Build Coastguard Worker# =============================================================================
318*3ac0a46fSAndroid Build Coastguard Worker# TARGET DEFAULTS
319*3ac0a46fSAndroid Build Coastguard Worker# =============================================================================
320*3ac0a46fSAndroid Build Coastguard Worker#
321*3ac0a46fSAndroid Build Coastguard Worker# Set up the default configuration for every build target of the given type.
322*3ac0a46fSAndroid Build Coastguard Worker# The values configured here will be automatically set on the scope of the
323*3ac0a46fSAndroid Build Coastguard Worker# corresponding target. Target definitions can add or remove to the settings
324*3ac0a46fSAndroid Build Coastguard Worker# here as needed.
325*3ac0a46fSAndroid Build Coastguard Worker#
326*3ac0a46fSAndroid Build Coastguard Worker# WHAT GOES HERE?
327*3ac0a46fSAndroid Build Coastguard Worker#
328*3ac0a46fSAndroid Build Coastguard Worker# Other than the main compiler and linker configs, the only reason for a config
329*3ac0a46fSAndroid Build Coastguard Worker# to be in this list is if some targets need to explicitly override that config
330*3ac0a46fSAndroid Build Coastguard Worker# by removing it. This is how targets opt-out of flags. If you don't have that
331*3ac0a46fSAndroid Build Coastguard Worker# requirement and just need to add a config everywhere, reference it as a
332*3ac0a46fSAndroid Build Coastguard Worker# sub-config of an existing one, most commonly the main "compiler" one.
333*3ac0a46fSAndroid Build Coastguard Worker
334*3ac0a46fSAndroid Build Coastguard Worker# Holds all configs used for running the compiler.
335*3ac0a46fSAndroid Build Coastguard Workerdefault_compiler_configs = [
336*3ac0a46fSAndroid Build Coastguard Worker  "//build/config:feature_flags",
337*3ac0a46fSAndroid Build Coastguard Worker  "//build/config/compiler:afdo",
338*3ac0a46fSAndroid Build Coastguard Worker  "//build/config/compiler:afdo_optimize_size",
339*3ac0a46fSAndroid Build Coastguard Worker  "//build/config/compiler:cet_shadow_stack",
340*3ac0a46fSAndroid Build Coastguard Worker  "//build/config/compiler:chromium_code",
341*3ac0a46fSAndroid Build Coastguard Worker  "//build/config/compiler:compiler",
342*3ac0a46fSAndroid Build Coastguard Worker  "//build/config/compiler:compiler_arm_fpu",
343*3ac0a46fSAndroid Build Coastguard Worker  "//build/config/compiler:compiler_arm_thumb",
344*3ac0a46fSAndroid Build Coastguard Worker  "//build/config/compiler:default_include_dirs",
345*3ac0a46fSAndroid Build Coastguard Worker  "//build/config/compiler:default_init_stack_vars",
346*3ac0a46fSAndroid Build Coastguard Worker  "//build/config/compiler:default_optimization",
347*3ac0a46fSAndroid Build Coastguard Worker  "//build/config/compiler:default_stack_frames",
348*3ac0a46fSAndroid Build Coastguard Worker  "//build/config/compiler:default_symbols",
349*3ac0a46fSAndroid Build Coastguard Worker  "//build/config/compiler:export_dynamic",
350*3ac0a46fSAndroid Build Coastguard Worker  "//build/config/compiler:no_exceptions",
351*3ac0a46fSAndroid Build Coastguard Worker  "//build/config/compiler:no_rtti",
352*3ac0a46fSAndroid Build Coastguard Worker  "//build/config/compiler:no_unresolved_symbols",
353*3ac0a46fSAndroid Build Coastguard Worker  "//build/config/compiler:runtime_library",
354*3ac0a46fSAndroid Build Coastguard Worker  "//build/config/compiler:thin_archive",
355*3ac0a46fSAndroid Build Coastguard Worker  "//build/config/compiler:thinlto_optimize_default",
356*3ac0a46fSAndroid Build Coastguard Worker  "//build/config/compiler/pgo:default_pgo_flags",
357*3ac0a46fSAndroid Build Coastguard Worker  "//build/config/coverage:default_coverage",
358*3ac0a46fSAndroid Build Coastguard Worker  "//build/config/sanitizers:default_sanitizer_flags",
359*3ac0a46fSAndroid Build Coastguard Worker]
360*3ac0a46fSAndroid Build Coastguard Worker
361*3ac0a46fSAndroid Build Coastguard Workerif (is_win) {
362*3ac0a46fSAndroid Build Coastguard Worker  default_compiler_configs += [
363*3ac0a46fSAndroid Build Coastguard Worker    "//build/config/win:default_cfg_compiler",
364*3ac0a46fSAndroid Build Coastguard Worker    "//build/config/win:default_crt",
365*3ac0a46fSAndroid Build Coastguard Worker    "//build/config/win:lean_and_mean",
366*3ac0a46fSAndroid Build Coastguard Worker    "//build/config/win:nominmax",
367*3ac0a46fSAndroid Build Coastguard Worker    "//build/config/win:unicode",
368*3ac0a46fSAndroid Build Coastguard Worker    "//build/config/win:winver",
369*3ac0a46fSAndroid Build Coastguard Worker  ]
370*3ac0a46fSAndroid Build Coastguard Worker}
371*3ac0a46fSAndroid Build Coastguard Worker
372*3ac0a46fSAndroid Build Coastguard Workerif (is_apple) {
373*3ac0a46fSAndroid Build Coastguard Worker  default_compiler_configs += [ "//build/config/compiler:enable_arc2" ]
374*3ac0a46fSAndroid Build Coastguard Worker}
375*3ac0a46fSAndroid Build Coastguard Worker
376*3ac0a46fSAndroid Build Coastguard Workerif (is_posix) {
377*3ac0a46fSAndroid Build Coastguard Worker  if (current_os != "aix") {
378*3ac0a46fSAndroid Build Coastguard Worker    default_compiler_configs +=
379*3ac0a46fSAndroid Build Coastguard Worker        [ "//build/config/gcc:symbol_visibility_hidden" ]
380*3ac0a46fSAndroid Build Coastguard Worker  }
381*3ac0a46fSAndroid Build Coastguard Worker}
382*3ac0a46fSAndroid Build Coastguard Worker
383*3ac0a46fSAndroid Build Coastguard Workerif (is_fuchsia) {
384*3ac0a46fSAndroid Build Coastguard Worker  default_compiler_configs += [ "//build/config/gcc:symbol_visibility_hidden" ]
385*3ac0a46fSAndroid Build Coastguard Worker}
386*3ac0a46fSAndroid Build Coastguard Worker
387*3ac0a46fSAndroid Build Coastguard Workerif (is_android) {
388*3ac0a46fSAndroid Build Coastguard Worker  default_compiler_configs +=
389*3ac0a46fSAndroid Build Coastguard Worker      [ "//build/config/android:default_orderfile_instrumentation" ]
390*3ac0a46fSAndroid Build Coastguard Worker}
391*3ac0a46fSAndroid Build Coastguard Worker
392*3ac0a46fSAndroid Build Coastguard Workerif (is_clang && !is_nacl) {
393*3ac0a46fSAndroid Build Coastguard Worker  default_compiler_configs += [
394*3ac0a46fSAndroid Build Coastguard Worker    "//build/config/clang:find_bad_constructs",
395*3ac0a46fSAndroid Build Coastguard Worker    "//build/config/clang:extra_warnings",
396*3ac0a46fSAndroid Build Coastguard Worker  ]
397*3ac0a46fSAndroid Build Coastguard Worker}
398*3ac0a46fSAndroid Build Coastguard Worker
399*3ac0a46fSAndroid Build Coastguard Worker# Debug/release-related defines.
400*3ac0a46fSAndroid Build Coastguard Workerif (is_debug) {
401*3ac0a46fSAndroid Build Coastguard Worker  default_compiler_configs += [ "//build/config:debug" ]
402*3ac0a46fSAndroid Build Coastguard Worker} else {
403*3ac0a46fSAndroid Build Coastguard Worker  default_compiler_configs += [ "//build/config:release" ]
404*3ac0a46fSAndroid Build Coastguard Worker}
405*3ac0a46fSAndroid Build Coastguard Worker
406*3ac0a46fSAndroid Build Coastguard Worker# =============================================================================
407*3ac0a46fSAndroid Build Coastguard Worker# Begin PDFIUM MODIFICATIONS
408*3ac0a46fSAndroid Build Coastguard Worker# =============================================================================
409*3ac0a46fSAndroid Build Coastguard Workerimport("//pdfium.gni")
410*3ac0a46fSAndroid Build Coastguard Workerif (!pdf_use_cxx20) {
411*3ac0a46fSAndroid Build Coastguard Worker  if (is_win && !is_clang) {
412*3ac0a46fSAndroid Build Coastguard Worker    msvc_use_cxx17 = true
413*3ac0a46fSAndroid Build Coastguard Worker  } else {
414*3ac0a46fSAndroid Build Coastguard Worker    default_compiler_configs += [ "//build_overrides/compiler:force_cxx17" ]
415*3ac0a46fSAndroid Build Coastguard Worker  }
416*3ac0a46fSAndroid Build Coastguard Worker}
417*3ac0a46fSAndroid Build Coastguard Worker
418*3ac0a46fSAndroid Build Coastguard Worker# =============================================================================
419*3ac0a46fSAndroid Build Coastguard Worker# End PDFIUM MODIFICATIONS
420*3ac0a46fSAndroid Build Coastguard Worker# =============================================================================
421*3ac0a46fSAndroid Build Coastguard Worker
422*3ac0a46fSAndroid Build Coastguard Worker# Static libraries and source sets use only the compiler ones.
423*3ac0a46fSAndroid Build Coastguard Workerset_defaults("static_library") {
424*3ac0a46fSAndroid Build Coastguard Worker  configs = default_compiler_configs
425*3ac0a46fSAndroid Build Coastguard Worker}
426*3ac0a46fSAndroid Build Coastguard Workerset_defaults("source_set") {
427*3ac0a46fSAndroid Build Coastguard Worker  configs = default_compiler_configs
428*3ac0a46fSAndroid Build Coastguard Worker}
429*3ac0a46fSAndroid Build Coastguard Workerset_defaults("rust_library") {
430*3ac0a46fSAndroid Build Coastguard Worker  configs = default_compiler_configs
431*3ac0a46fSAndroid Build Coastguard Worker}
432*3ac0a46fSAndroid Build Coastguard Workerset_defaults("rust_proc_macro") {
433*3ac0a46fSAndroid Build Coastguard Worker  configs = default_compiler_configs
434*3ac0a46fSAndroid Build Coastguard Worker}
435*3ac0a46fSAndroid Build Coastguard Worker
436*3ac0a46fSAndroid Build Coastguard Worker# Compute the set of configs common to all linked targets (shared libraries,
437*3ac0a46fSAndroid Build Coastguard Worker# loadable modules, executables) to avoid duplication below.
438*3ac0a46fSAndroid Build Coastguard Workerif (is_win) {
439*3ac0a46fSAndroid Build Coastguard Worker  # Many targets remove these configs, so they are not contained within
440*3ac0a46fSAndroid Build Coastguard Worker  # //build/config:executable_config for easy removal.
441*3ac0a46fSAndroid Build Coastguard Worker  _linker_configs = [
442*3ac0a46fSAndroid Build Coastguard Worker    "//build/config/win:default_incremental_linking",
443*3ac0a46fSAndroid Build Coastguard Worker
444*3ac0a46fSAndroid Build Coastguard Worker    # Default to console-mode apps. Most of our targets are tests and such
445*3ac0a46fSAndroid Build Coastguard Worker    # that shouldn't use the windows subsystem.
446*3ac0a46fSAndroid Build Coastguard Worker    "//build/config/win:console",
447*3ac0a46fSAndroid Build Coastguard Worker  ]
448*3ac0a46fSAndroid Build Coastguard Worker} else if (is_mac) {
449*3ac0a46fSAndroid Build Coastguard Worker  _linker_configs = [ "//build/config/apple:strip_all" ]
450*3ac0a46fSAndroid Build Coastguard Worker} else {
451*3ac0a46fSAndroid Build Coastguard Worker  _linker_configs = []
452*3ac0a46fSAndroid Build Coastguard Worker}
453*3ac0a46fSAndroid Build Coastguard Worker
454*3ac0a46fSAndroid Build Coastguard Worker# Executable defaults.
455*3ac0a46fSAndroid Build Coastguard Workerdefault_executable_configs = default_compiler_configs + [
456*3ac0a46fSAndroid Build Coastguard Worker                               "//build/config:default_libs",
457*3ac0a46fSAndroid Build Coastguard Worker                               "//build/config:executable_config",
458*3ac0a46fSAndroid Build Coastguard Worker                             ] + _linker_configs
459*3ac0a46fSAndroid Build Coastguard Worker
460*3ac0a46fSAndroid Build Coastguard Workerif (is_win) {
461*3ac0a46fSAndroid Build Coastguard Worker  # Turn on linker CFI for executables, and position it so it can be removed
462*3ac0a46fSAndroid Build Coastguard Worker  # if needed.
463*3ac0a46fSAndroid Build Coastguard Worker  default_executable_configs += [ "//build/config/win:cfi_linker" ]
464*3ac0a46fSAndroid Build Coastguard Worker}
465*3ac0a46fSAndroid Build Coastguard Worker
466*3ac0a46fSAndroid Build Coastguard Workerset_defaults("executable") {
467*3ac0a46fSAndroid Build Coastguard Worker  configs = default_executable_configs
468*3ac0a46fSAndroid Build Coastguard Worker}
469*3ac0a46fSAndroid Build Coastguard Worker
470*3ac0a46fSAndroid Build Coastguard Worker# Shared library and loadable module defaults (also for components in component
471*3ac0a46fSAndroid Build Coastguard Worker# mode).
472*3ac0a46fSAndroid Build Coastguard Workerdefault_shared_library_configs = default_compiler_configs + [
473*3ac0a46fSAndroid Build Coastguard Worker                                   "//build/config:default_libs",
474*3ac0a46fSAndroid Build Coastguard Worker                                   "//build/config:shared_library_config",
475*3ac0a46fSAndroid Build Coastguard Worker                                 ] + _linker_configs
476*3ac0a46fSAndroid Build Coastguard Workerif (is_win) {
477*3ac0a46fSAndroid Build Coastguard Worker  # Turn on linker CFI for DLLs, and position it so it can be removed if needed.
478*3ac0a46fSAndroid Build Coastguard Worker  default_shared_library_configs += [ "//build/config/win:cfi_linker" ]
479*3ac0a46fSAndroid Build Coastguard Worker}
480*3ac0a46fSAndroid Build Coastguard Worker
481*3ac0a46fSAndroid Build Coastguard Workerif (is_android) {
482*3ac0a46fSAndroid Build Coastguard Worker  # Strip native JNI exports from shared libraries by default. Binaries that
483*3ac0a46fSAndroid Build Coastguard Worker  # want this can remove this config.
484*3ac0a46fSAndroid Build Coastguard Worker  default_shared_library_configs +=
485*3ac0a46fSAndroid Build Coastguard Worker      [ "//build/config/android:hide_all_but_jni_onload" ]
486*3ac0a46fSAndroid Build Coastguard Worker}
487*3ac0a46fSAndroid Build Coastguard Workerset_defaults("shared_library") {
488*3ac0a46fSAndroid Build Coastguard Worker  configs = default_shared_library_configs
489*3ac0a46fSAndroid Build Coastguard Worker}
490*3ac0a46fSAndroid Build Coastguard Workerset_defaults("loadable_module") {
491*3ac0a46fSAndroid Build Coastguard Worker  configs = default_shared_library_configs
492*3ac0a46fSAndroid Build Coastguard Worker
493*3ac0a46fSAndroid Build Coastguard Worker  # loadable_modules are generally used by other libs, not just via JNI.
494*3ac0a46fSAndroid Build Coastguard Worker  if (is_android) {
495*3ac0a46fSAndroid Build Coastguard Worker    configs -= [ "//build/config/android:hide_all_but_jni_onload" ]
496*3ac0a46fSAndroid Build Coastguard Worker  }
497*3ac0a46fSAndroid Build Coastguard Worker}
498*3ac0a46fSAndroid Build Coastguard Worker
499*3ac0a46fSAndroid Build Coastguard Worker# A helper for forwarding testonly and visibility.
500*3ac0a46fSAndroid Build Coastguard Worker# Forwarding "*" does not include variables from outer scopes (to avoid copying
501*3ac0a46fSAndroid Build Coastguard Worker# all globals into each template invocation), so it will not pick up
502*3ac0a46fSAndroid Build Coastguard Worker# file-scoped or outer-template-scoped variables. Normally this behavior is
503*3ac0a46fSAndroid Build Coastguard Worker# desired, but "visibility" and "testonly" are commonly defined in outer scopes.
504*3ac0a46fSAndroid Build Coastguard Worker# Explicitly forwarding them in forward_variables_from() works around this
505*3ac0a46fSAndroid Build Coastguard Worker# nuance. See //build/docs/writing_gn_templates.md#using-forward_variables_from
506*3ac0a46fSAndroid Build Coastguard WorkerTESTONLY_AND_VISIBILITY = [
507*3ac0a46fSAndroid Build Coastguard Worker  "testonly",
508*3ac0a46fSAndroid Build Coastguard Worker  "visibility",
509*3ac0a46fSAndroid Build Coastguard Worker]
510*3ac0a46fSAndroid Build Coastguard Worker
511*3ac0a46fSAndroid Build Coastguard Worker# Sets default dependencies for executable and shared_library targets.
512*3ac0a46fSAndroid Build Coastguard Worker#
513*3ac0a46fSAndroid Build Coastguard Worker# Variables
514*3ac0a46fSAndroid Build Coastguard Worker#   no_default_deps: If true, no standard dependencies will be added.
515*3ac0a46fSAndroid Build Coastguard Worker#       Targets that set this usually also want to remove
516*3ac0a46fSAndroid Build Coastguard Worker#       "//build/config/compiler:runtime_library" from configs (to remove
517*3ac0a46fSAndroid Build Coastguard Worker#       its subconfig "//build/config/c++:runtime_library").
518*3ac0a46fSAndroid Build Coastguard Workerforeach(_target_type,
519*3ac0a46fSAndroid Build Coastguard Worker        [
520*3ac0a46fSAndroid Build Coastguard Worker          "executable",
521*3ac0a46fSAndroid Build Coastguard Worker          "loadable_module",
522*3ac0a46fSAndroid Build Coastguard Worker          "shared_library",
523*3ac0a46fSAndroid Build Coastguard Worker        ]) {
524*3ac0a46fSAndroid Build Coastguard Worker  template(_target_type) {
525*3ac0a46fSAndroid Build Coastguard Worker    # Alias "target_name" because it is clobbered by forward_variables_from().
526*3ac0a46fSAndroid Build Coastguard Worker    _target_name = target_name
527*3ac0a46fSAndroid Build Coastguard Worker    target(_target_type, _target_name) {
528*3ac0a46fSAndroid Build Coastguard Worker      forward_variables_from(invoker,
529*3ac0a46fSAndroid Build Coastguard Worker                             "*",
530*3ac0a46fSAndroid Build Coastguard Worker                             TESTONLY_AND_VISIBILITY + [ "no_default_deps" ])
531*3ac0a46fSAndroid Build Coastguard Worker      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
532*3ac0a46fSAndroid Build Coastguard Worker      if (!defined(deps)) {
533*3ac0a46fSAndroid Build Coastguard Worker        deps = []
534*3ac0a46fSAndroid Build Coastguard Worker      }
535*3ac0a46fSAndroid Build Coastguard Worker      if (!defined(invoker.no_default_deps) || !invoker.no_default_deps) {
536*3ac0a46fSAndroid Build Coastguard Worker        # This pulls in one of:
537*3ac0a46fSAndroid Build Coastguard Worker        # //build/config:executable_deps
538*3ac0a46fSAndroid Build Coastguard Worker        # //build/config:loadable_module_deps
539*3ac0a46fSAndroid Build Coastguard Worker        # //build/config:shared_library_deps
540*3ac0a46fSAndroid Build Coastguard Worker        # (This explicit list is so that grepping for these configs finds where
541*3ac0a46fSAndroid Build Coastguard Worker        # they are used.)
542*3ac0a46fSAndroid Build Coastguard Worker        deps += [ "//build/config:${_target_type}_deps" ]
543*3ac0a46fSAndroid Build Coastguard Worker      }
544*3ac0a46fSAndroid Build Coastguard Worker
545*3ac0a46fSAndroid Build Coastguard Worker      # On Android, write shared library output file to metadata. We will use
546*3ac0a46fSAndroid Build Coastguard Worker      # this information to, for instance, collect all shared libraries that
547*3ac0a46fSAndroid Build Coastguard Worker      # should be packaged into an APK.
548*3ac0a46fSAndroid Build Coastguard Worker      if (!defined(invoker.metadata) && (is_android || is_robolectric) &&
549*3ac0a46fSAndroid Build Coastguard Worker          (_target_type == "shared_library" ||
550*3ac0a46fSAndroid Build Coastguard Worker           _target_type == "loadable_module")) {
551*3ac0a46fSAndroid Build Coastguard Worker        _output_name = _target_name
552*3ac0a46fSAndroid Build Coastguard Worker        if (defined(invoker.output_name)) {
553*3ac0a46fSAndroid Build Coastguard Worker          _output_name = invoker.output_name
554*3ac0a46fSAndroid Build Coastguard Worker        }
555*3ac0a46fSAndroid Build Coastguard Worker
556*3ac0a46fSAndroid Build Coastguard Worker        # Remove 'lib' prefix from output name if it exists.
557*3ac0a46fSAndroid Build Coastguard Worker        _magic_prefix = "$0x01$0x01"
558*3ac0a46fSAndroid Build Coastguard Worker        _output_name = string_replace("${_magic_prefix}${_output_name}",
559*3ac0a46fSAndroid Build Coastguard Worker                                      "${_magic_prefix}lib",
560*3ac0a46fSAndroid Build Coastguard Worker                                      _magic_prefix,
561*3ac0a46fSAndroid Build Coastguard Worker                                      1)
562*3ac0a46fSAndroid Build Coastguard Worker        _output_name = string_replace(_output_name, _magic_prefix, "", 1)
563*3ac0a46fSAndroid Build Coastguard Worker
564*3ac0a46fSAndroid Build Coastguard Worker        if (defined(output_extension)) {
565*3ac0a46fSAndroid Build Coastguard Worker          _shlib_extension = ".$output_extension"
566*3ac0a46fSAndroid Build Coastguard Worker        } else if (is_component_build && _target_type != "loadable_module") {
567*3ac0a46fSAndroid Build Coastguard Worker          _shlib_extension = ".cr.so"
568*3ac0a46fSAndroid Build Coastguard Worker        } else {
569*3ac0a46fSAndroid Build Coastguard Worker          _shlib_extension = ".so"
570*3ac0a46fSAndroid Build Coastguard Worker        }
571*3ac0a46fSAndroid Build Coastguard Worker
572*3ac0a46fSAndroid Build Coastguard Worker        metadata = {
573*3ac0a46fSAndroid Build Coastguard Worker          shared_libraries =
574*3ac0a46fSAndroid Build Coastguard Worker              [ "$root_out_dir/lib${_output_name}${_shlib_extension}" ]
575*3ac0a46fSAndroid Build Coastguard Worker        }
576*3ac0a46fSAndroid Build Coastguard Worker      }
577*3ac0a46fSAndroid Build Coastguard Worker    }
578*3ac0a46fSAndroid Build Coastguard Worker  }
579*3ac0a46fSAndroid Build Coastguard Worker}
580*3ac0a46fSAndroid Build Coastguard Worker
581*3ac0a46fSAndroid Build Coastguard Worker# ==============================================================================
582*3ac0a46fSAndroid Build Coastguard Worker# COMPONENT SETUP
583*3ac0a46fSAndroid Build Coastguard Worker# ==============================================================================
584*3ac0a46fSAndroid Build Coastguard Worker
585*3ac0a46fSAndroid Build Coastguard Worker# Defines a component, which equates to a shared_library when
586*3ac0a46fSAndroid Build Coastguard Worker# is_component_build == true and a static_library otherwise.
587*3ac0a46fSAndroid Build Coastguard Worker#
588*3ac0a46fSAndroid Build Coastguard Worker# Use static libraries for the static build rather than source sets because
589*3ac0a46fSAndroid Build Coastguard Worker# many of of our test binaries link many large dependencies but often don't
590*3ac0a46fSAndroid Build Coastguard Worker# use large portions of them. The static libraries are much more efficient to
591*3ac0a46fSAndroid Build Coastguard Worker# link in this situation since only the necessary object files are linked.
592*3ac0a46fSAndroid Build Coastguard Worker#
593*3ac0a46fSAndroid Build Coastguard Worker# The invoker can override the type of the target in the non-component-build
594*3ac0a46fSAndroid Build Coastguard Worker# case by setting static_component_type to either "source_set" or
595*3ac0a46fSAndroid Build Coastguard Worker# "static_library". If unset, the default will be used.
596*3ac0a46fSAndroid Build Coastguard Workertemplate("component") {
597*3ac0a46fSAndroid Build Coastguard Worker  if (is_component_build) {
598*3ac0a46fSAndroid Build Coastguard Worker    _component_mode = "shared_library"
599*3ac0a46fSAndroid Build Coastguard Worker  } else if (defined(invoker.static_component_type)) {
600*3ac0a46fSAndroid Build Coastguard Worker    assert(invoker.static_component_type == "static_library" ||
601*3ac0a46fSAndroid Build Coastguard Worker           invoker.static_component_type == "source_set")
602*3ac0a46fSAndroid Build Coastguard Worker    _component_mode = invoker.static_component_type
603*3ac0a46fSAndroid Build Coastguard Worker  } else if (!defined(invoker.sources) || invoker.sources == []) {
604*3ac0a46fSAndroid Build Coastguard Worker    # When there are no sources defined, use a source set to avoid creating
605*3ac0a46fSAndroid Build Coastguard Worker    # an empty static library (which generally don't work).
606*3ac0a46fSAndroid Build Coastguard Worker    _component_mode = "source_set"
607*3ac0a46fSAndroid Build Coastguard Worker  } else {
608*3ac0a46fSAndroid Build Coastguard Worker    _component_mode = "static_library"
609*3ac0a46fSAndroid Build Coastguard Worker  }
610*3ac0a46fSAndroid Build Coastguard Worker  target(_component_mode, target_name) {
611*3ac0a46fSAndroid Build Coastguard Worker    forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
612*3ac0a46fSAndroid Build Coastguard Worker    forward_variables_from(invoker, "*", TESTONLY_AND_VISIBILITY)
613*3ac0a46fSAndroid Build Coastguard Worker  }
614*3ac0a46fSAndroid Build Coastguard Worker}
615*3ac0a46fSAndroid Build Coastguard Worker
616*3ac0a46fSAndroid Build Coastguard Worker# Component defaults
617*3ac0a46fSAndroid Build Coastguard Worker# Set a variable since we also want to make this available
618*3ac0a46fSAndroid Build Coastguard Worker# to mixed_component.gni
619*3ac0a46fSAndroid Build Coastguard Workerif (is_component_build) {
620*3ac0a46fSAndroid Build Coastguard Worker  default_component_configs = default_shared_library_configs
621*3ac0a46fSAndroid Build Coastguard Worker  if (is_android) {
622*3ac0a46fSAndroid Build Coastguard Worker    default_component_configs -=
623*3ac0a46fSAndroid Build Coastguard Worker        [ "//build/config/android:hide_all_but_jni_onload" ]
624*3ac0a46fSAndroid Build Coastguard Worker  }
625*3ac0a46fSAndroid Build Coastguard Worker} else {
626*3ac0a46fSAndroid Build Coastguard Worker  default_component_configs = default_compiler_configs
627*3ac0a46fSAndroid Build Coastguard Worker}
628*3ac0a46fSAndroid Build Coastguard Worker
629*3ac0a46fSAndroid Build Coastguard Workerset_defaults("component") {
630*3ac0a46fSAndroid Build Coastguard Worker  configs = default_component_configs
631*3ac0a46fSAndroid Build Coastguard Worker}
632