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