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_apple) { 373 default_compiler_configs += [ "//build/config/compiler:enable_arc2" ] 374} 375 376if (is_posix) { 377 if (current_os != "aix") { 378 default_compiler_configs += 379 [ "//build/config/gcc:symbol_visibility_hidden" ] 380 } 381} 382 383if (is_fuchsia) { 384 default_compiler_configs += [ "//build/config/gcc:symbol_visibility_hidden" ] 385} 386 387if (is_android) { 388 default_compiler_configs += 389 [ "//build/config/android:default_orderfile_instrumentation" ] 390} 391 392if (is_clang && !is_nacl) { 393 default_compiler_configs += [ 394 "//build/config/clang:find_bad_constructs", 395 "//build/config/clang:extra_warnings", 396 ] 397} 398 399# Debug/release-related defines. 400if (is_debug) { 401 default_compiler_configs += [ "//build/config:debug" ] 402} else { 403 default_compiler_configs += [ "//build/config:release" ] 404} 405 406# ============================================================================= 407# Begin PDFIUM MODIFICATIONS 408# ============================================================================= 409import("//pdfium.gni") 410if (!pdf_use_cxx20) { 411 if (is_win && !is_clang) { 412 msvc_use_cxx17 = true 413 } else { 414 default_compiler_configs += [ "//build_overrides/compiler:force_cxx17" ] 415 } 416} 417 418# ============================================================================= 419# End PDFIUM MODIFICATIONS 420# ============================================================================= 421 422# Static libraries and source sets use only the compiler ones. 423set_defaults("static_library") { 424 configs = default_compiler_configs 425} 426set_defaults("source_set") { 427 configs = default_compiler_configs 428} 429set_defaults("rust_library") { 430 configs = default_compiler_configs 431} 432set_defaults("rust_proc_macro") { 433 configs = default_compiler_configs 434} 435 436# Compute the set of configs common to all linked targets (shared libraries, 437# loadable modules, executables) to avoid duplication below. 438if (is_win) { 439 # Many targets remove these configs, so they are not contained within 440 # //build/config:executable_config for easy removal. 441 _linker_configs = [ 442 "//build/config/win:default_incremental_linking", 443 444 # Default to console-mode apps. Most of our targets are tests and such 445 # that shouldn't use the windows subsystem. 446 "//build/config/win:console", 447 ] 448} else if (is_mac) { 449 _linker_configs = [ "//build/config/apple:strip_all" ] 450} else { 451 _linker_configs = [] 452} 453 454# Executable defaults. 455default_executable_configs = default_compiler_configs + [ 456 "//build/config:default_libs", 457 "//build/config:executable_config", 458 ] + _linker_configs 459 460if (is_win) { 461 # Turn on linker CFI for executables, and position it so it can be removed 462 # if needed. 463 default_executable_configs += [ "//build/config/win:cfi_linker" ] 464} 465 466set_defaults("executable") { 467 configs = default_executable_configs 468} 469 470# Shared library and loadable module defaults (also for components in component 471# mode). 472default_shared_library_configs = default_compiler_configs + [ 473 "//build/config:default_libs", 474 "//build/config:shared_library_config", 475 ] + _linker_configs 476if (is_win) { 477 # Turn on linker CFI for DLLs, and position it so it can be removed if needed. 478 default_shared_library_configs += [ "//build/config/win:cfi_linker" ] 479} 480 481if (is_android) { 482 # Strip native JNI exports from shared libraries by default. Binaries that 483 # want this can remove this config. 484 default_shared_library_configs += 485 [ "//build/config/android:hide_all_but_jni_onload" ] 486} 487set_defaults("shared_library") { 488 configs = default_shared_library_configs 489} 490set_defaults("loadable_module") { 491 configs = default_shared_library_configs 492 493 # loadable_modules are generally used by other libs, not just via JNI. 494 if (is_android) { 495 configs -= [ "//build/config/android:hide_all_but_jni_onload" ] 496 } 497} 498 499# A helper for forwarding testonly and visibility. 500# Forwarding "*" does not include variables from outer scopes (to avoid copying 501# all globals into each template invocation), so it will not pick up 502# file-scoped or outer-template-scoped variables. Normally this behavior is 503# desired, but "visibility" and "testonly" are commonly defined in outer scopes. 504# Explicitly forwarding them in forward_variables_from() works around this 505# nuance. See //build/docs/writing_gn_templates.md#using-forward_variables_from 506TESTONLY_AND_VISIBILITY = [ 507 "testonly", 508 "visibility", 509] 510 511# Sets default dependencies for executable and shared_library targets. 512# 513# Variables 514# no_default_deps: If true, no standard dependencies will be added. 515# Targets that set this usually also want to remove 516# "//build/config/compiler:runtime_library" from configs (to remove 517# its subconfig "//build/config/c++:runtime_library"). 518foreach(_target_type, 519 [ 520 "executable", 521 "loadable_module", 522 "shared_library", 523 ]) { 524 template(_target_type) { 525 # Alias "target_name" because it is clobbered by forward_variables_from(). 526 _target_name = target_name 527 target(_target_type, _target_name) { 528 forward_variables_from(invoker, 529 "*", 530 TESTONLY_AND_VISIBILITY + [ "no_default_deps" ]) 531 forward_variables_from(invoker, TESTONLY_AND_VISIBILITY) 532 if (!defined(deps)) { 533 deps = [] 534 } 535 if (!defined(invoker.no_default_deps) || !invoker.no_default_deps) { 536 # This pulls in one of: 537 # //build/config:executable_deps 538 # //build/config:loadable_module_deps 539 # //build/config:shared_library_deps 540 # (This explicit list is so that grepping for these configs finds where 541 # they are used.) 542 deps += [ "//build/config:${_target_type}_deps" ] 543 } 544 545 # On Android, write shared library output file to metadata. We will use 546 # this information to, for instance, collect all shared libraries that 547 # should be packaged into an APK. 548 if (!defined(invoker.metadata) && (is_android || is_robolectric) && 549 (_target_type == "shared_library" || 550 _target_type == "loadable_module")) { 551 _output_name = _target_name 552 if (defined(invoker.output_name)) { 553 _output_name = invoker.output_name 554 } 555 556 # Remove 'lib' prefix from output name if it exists. 557 _magic_prefix = "$0x01$0x01" 558 _output_name = string_replace("${_magic_prefix}${_output_name}", 559 "${_magic_prefix}lib", 560 _magic_prefix, 561 1) 562 _output_name = string_replace(_output_name, _magic_prefix, "", 1) 563 564 if (defined(output_extension)) { 565 _shlib_extension = ".$output_extension" 566 } else if (is_component_build && _target_type != "loadable_module") { 567 _shlib_extension = ".cr.so" 568 } else { 569 _shlib_extension = ".so" 570 } 571 572 metadata = { 573 shared_libraries = 574 [ "$root_out_dir/lib${_output_name}${_shlib_extension}" ] 575 } 576 } 577 } 578 } 579} 580 581# ============================================================================== 582# COMPONENT SETUP 583# ============================================================================== 584 585# Defines a component, which equates to a shared_library when 586# is_component_build == true and a static_library otherwise. 587# 588# Use static libraries for the static build rather than source sets because 589# many of of our test binaries link many large dependencies but often don't 590# use large portions of them. The static libraries are much more efficient to 591# link in this situation since only the necessary object files are linked. 592# 593# The invoker can override the type of the target in the non-component-build 594# case by setting static_component_type to either "source_set" or 595# "static_library". If unset, the default will be used. 596template("component") { 597 if (is_component_build) { 598 _component_mode = "shared_library" 599 } else if (defined(invoker.static_component_type)) { 600 assert(invoker.static_component_type == "static_library" || 601 invoker.static_component_type == "source_set") 602 _component_mode = invoker.static_component_type 603 } else if (!defined(invoker.sources) || invoker.sources == []) { 604 # When there are no sources defined, use a source set to avoid creating 605 # an empty static library (which generally don't work). 606 _component_mode = "source_set" 607 } else { 608 _component_mode = "static_library" 609 } 610 target(_component_mode, target_name) { 611 forward_variables_from(invoker, TESTONLY_AND_VISIBILITY) 612 forward_variables_from(invoker, "*", TESTONLY_AND_VISIBILITY) 613 } 614} 615 616# Component defaults 617# Set a variable since we also want to make this available 618# to mixed_component.gni 619if (is_component_build) { 620 default_component_configs = default_shared_library_configs 621 if (is_android) { 622 default_component_configs -= 623 [ "//build/config/android:hide_all_but_jni_onload" ] 624 } 625} else { 626 default_component_configs = default_compiler_configs 627} 628 629set_defaults("component") { 630 configs = default_component_configs 631} 632