1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259
  260
  261
  262
  263
  264
  265
  266
  267
  268
  269
  270
  271
  272
  273
  274
  275
  276
  277
  278
  279
  280
  281
  282
  283
  284
  285
  286
  287
  288
  289
  290
  291
  292
  293
  294
  295
  296
  297
  298
  299
  300
  301
  302
  303
  304
  305
  306
  307
  308
  309
  310
  311
  312
  313
  314
  315
  316
  317
  318
  319
  320
  321
  322
  323
  324
  325
  326
  327
  328
  329
  330
  331
  332
  333
  334
  335
  336
  337
  338
  339
  340
  341
  342
  343
  344
  345
  346
  347
  348
  349
  350
  351
  352
  353
  354
  355
  356
  357
  358
  359
  360
  361
  362
  363
  364
  365
  366
  367
  368
  369
  370
  371
  372
  373
  374
  375
  376
  377
  378
  379
  380
  381
  382
  383
  384
  385
  386
  387
  388
  389
  390
  391
  392
  393
  394
  395
  396
  397
  398
  399
  400
  401
  402
  403
  404
  405
  406
  407
  408
  409
  410
  411
  412
  413
  414
  415
  416
  417
  418
  419
  420
  421
  422
  423
  424
  425
  426
  427
  428
  429
  430
  431
  432
  433
  434
  435
  436
  437
  438
  439
  440
  441
  442
  443
  444
  445
  446
  447
  448
  449
  450
  451
  452
  453
  454
  455
  456
  457
  458
  459
  460
  461
  462
  463
  464
  465
  466
  467
  468
  469
  470
  471
  472
  473
  474
  475
  476
  477
  478
  479
  480
  481
  482
  483
  484
  485
  486
  487
  488
  489
  490
  491
  492
  493
  494
  495
  496
  497
  498
  499
  500
  501
  502
  503
  504
  505
  506
  507
  508
  509
  510
  511
  512
  513
  514
  515
  516
  517
  518
  519
  520
  521
  522
  523
  524
  525
  526
  527
  528
  529
  530
  531
  532
  533
  534
  535
  536
  537
  538
  539
  540
  541
  542
  543
  544
  545
  546
  547
  548
  549
  550
  551
  552
  553
  554
  555
  556
  557
  558
  559
  560
  561
  562
  563
  564
  565
  566
  567
  568
  569
  570
  571
  572
  573
  574
  575
  576
  577
  578
  579
  580
  581
  582
  583
  584
  585
  586
  587
  588
  589
  590
  591
  592
  593
  594
  595
  596
  597
  598
  599
  600
  601
  602
  603
  604
  605
  606
  607
  608
  609
  610
  611
  612
  613
  614
  615
  616
  617
  618

build / config / mac / rules.gni [blame]

# Copyright 2015 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import("//build/apple/apple_info_plist.gni")
import("//build/config/apple/symbols.gni")
import("//build/config/mac/mac_sdk.gni")

# Generates Info.plist files for Mac apps and frameworks.
#
# Arguments
#
#     info_plist:
#         (optional) string, path to the Info.plist file that will be used for
#         the bundle.
#
#     info_plist_target:
#         (optional) string, if the info_plist is generated from an action,
#         rather than a regular source file, specify the target name in lieu
#         of info_plist. The two arguments are mutually exclusive.
#
#     executable_name:
#         string, name of the generated target used for the product
#         and executable name as specified in the output Info.plist.
#
#     extra_substitutions:
#         (optional) string array, 'key=value' pairs for extra fields which are
#         specified in a source Info.plist template.
template("mac_info_plist") {
  assert(defined(invoker.info_plist) != defined(invoker.info_plist_target),
         "Only one of info_plist or info_plist_target may be specified in " +
             target_name)

  if (defined(invoker.info_plist)) {
    _info_plist = invoker.info_plist
  } else {
    _info_plist_target_output = get_target_outputs(invoker.info_plist_target)
    _info_plist = _info_plist_target_output[0]
  }

  apple_info_plist(target_name) {
    format = "xml1"
    extra_substitutions = [
      "MAC_SDK_BUILD=$mac_sdk_build_version",
      "MAC_SDK_NAME=$mac_sdk_name$mac_sdk_version",
      "MACOSX_DEPLOYMENT_TARGET=$mac_deployment_target",
      "CHROMIUM_MIN_SYSTEM_VERSION=$mac_min_system_version",
      "XCODE_BUILD=$xcode_build",
      "XCODE_VERSION=$xcode_version",
    ]
    if (defined(invoker.extra_substitutions)) {
      extra_substitutions += invoker.extra_substitutions
    }
    plist_templates = [
      "//build/config/mac/BuildInfo.plist",
      _info_plist,
    ]
    if (defined(invoker.info_plist_target)) {
      deps = [ invoker.info_plist_target ]
    }
    forward_variables_from(invoker,
                           [
                             "testonly",
                             "executable_name",
                           ])
  }
}

# Template to package a shared library into a Mac framework bundle.
#
# By default, the bundle target this template generates does not link the
# resulting framework into anything that depends on it. If a dependency wants
# a link-time (as well as build-time) dependency on the framework bundle,
# depend against "$target_name+link". If only the build-time dependency is
# required (e.g., for copying into another bundle), then use "$target_name".
#
# Arguments
#
#     framework_version:
#         string, version of the framework. Typically this is a
#         single letter, like "A".
#
#     framework_contents:
#         list of string, top-level items in the framework. This is
#         the list of symlinks to create in the .framework directory that link
#         into Versions/Current/.
#
#     info_plist:
#         (optional) string, path to the Info.plist file that will be used for
#         the bundle.
#
#     info_plist_target:
#         (optional) string, if the info_plist is generated from an action,
#         rather than a regular source file, specify the target name in lieu
#         of info_plist. The two arguments are mutually exclusive.
#
#     output_name:
#         (optional) string, name of the generated framework without the
#         .framework suffix. If omitted, defaults to target_name.
#
#     extra_substitutions:
#         (optional) string array, 'key=value' pairs for extra fields which are
#         specified in a source Info.plist template.
#
# This template provides three targets for the resulting framework bundle. The
# link-time behavior varies depending on which of the two targets below is
# added as a dependency:
#   - $target_name only adds a build-time dependency. Targets that depend on
#     it will not link against the framework.
#   - $target_name+link adds a build-time and link-time dependency. Targets
#     that depend on it will link against the framework.
#   - $target_name+link_nested adds a build-time and link-time dependency, but
#     only on the shared library and not the fully-assembled framework bundle.
#     This should only be used for other nested binary components of the
#     framework bundle (e.g. Helpers) that themselves depend on the main shared
#     library of the framework bundle.
#
# The build-time-only dependency is used for when a target needs to use the
# framework either only for resources, or because the target loads it at run-
# time, via dlopen() or NSBundle. The link-time dependency will cause the
# dependee to have the framework loaded by dyld at launch.
#
# Example of build-time only dependency:
#
#     mac_framework_bundle("CoreTeleportation") {
#       sources = [ ... ]
#     }
#
#     bundle_data("core_teleportation_bundle_data") {
#       deps = [ ":CoreTeleportation" ]
#       sources = [ "$root_out_dir/CoreTeleportation.framework" ]
#       outputs = [ "{{bundle_contents_dir}}/Frameworks/{{source_file_part}}" ]
#     }
#
#     app_bundle("GoatTeleporter") {
#       sources = [ ... ]
#       deps = [
#         ":core_teleportation_bundle_data",
#       ]
#     }
#
# The GoatTeleporter.app will not directly link against
# CoreTeleportation.framework, but it will be included in the bundle's
# Frameworks directory.
#
# Example of link-time dependency:
#
#     mac_framework_bundle("CoreTeleportation") {
#       sources = [ ... ]
#       ldflags = [
#         "-install_name",
#         "@executable_path/../Frameworks/$target_name.framework"
#       ]
#     }
#
#     bundle_data("core_teleportation_bundle_data") {
#       deps = [ ":CoreTeleportation+link" ]
#       sources = [ "$root_out_dir/CoreTeleportation.framework" ]
#       outputs = [ "{{bundle_contents_dir}}/Frameworks/{{source_file_part}}" ]
#     }
#
#     app_bundle("GoatTeleporter") {
#       sources = [ ... ]
#       deps = [
#         ":core_teleportation_bundle_data",
#       ]
#     }
#
# Note that the framework is still copied to the app's bundle, but dyld will
# load this library when the app is launched because it uses the "+link"
# target as a dependency. This also requires that the framework set its
# install_name so that dyld can locate it.
#
# See "gn help shared_library" for more information on arguments supported
# by shared library target.
template("mac_framework_bundle") {
  assert(defined(invoker.deps) || defined(invoker.public_deps),
         "Dependencies must be specified for $target_name")
  assert(invoker.framework_version != "", "framework_version is required")
  assert(defined(invoker.framework_contents), "framework_contents is required")

  _info_plist_target = target_name + "_info_plist"

  mac_info_plist(_info_plist_target) {
    executable_name = target_name
    if (defined(invoker.output_name)) {
      executable_name = invoker.output_name
    }
    forward_variables_from(invoker,
                           [
                             "extra_substitutions",
                             "info_plist",
                             "info_plist_target",
                             "testonly",
                           ])
  }

  _info_plist_bundle_data = _info_plist_target + "_bundle_data"

  bundle_data(_info_plist_bundle_data) {
    forward_variables_from(invoker, [ "testonly" ])
    sources = get_target_outputs(":$_info_plist_target")
    outputs = [ "{{bundle_resources_dir}}/Info.plist" ]
    public_deps = [ ":$_info_plist_target" ]
  }

  _target_name = target_name
  _output_name = target_name
  if (defined(invoker.output_name)) {
    _output_name = invoker.output_name
  }

  # Create a file to track the build dependency on the framework_version and
  # framework_contents variables.
  _framework_toc = [
                     "Version=" + invoker.framework_version,
                     _output_name,
                   ] + invoker.framework_contents
  _framework_contents = [ _output_name ] + invoker.framework_contents
  _framework_toc_file = "$target_out_dir/${target_name}.toc"
  write_file(_framework_toc_file, _framework_toc)

  # Create local variables for referencing different parts of the bundle.
  _framework_target = _target_name
  _framework_name = _output_name + ".framework"
  _framework_base_dir = "$root_out_dir/$_framework_name"
  _framework_root_dir =
      _framework_base_dir + "/Versions/${invoker.framework_version}"

  # Clean the entire framework if the framework_version changes.
  _version_file = "$target_out_dir/${target_name}_version"
  exec_script("//build/config/mac/prepare_framework_version.py",
              [
                rebase_path(_version_file),
                rebase_path(_framework_base_dir),
                invoker.framework_version,
              ])

  # Create the symlinks.
  _framework_package_target = target_name + "_package"
  action(_framework_package_target) {
    script = "//build/config/mac/package_framework.py"

    # The TOC file never needs to be read, since its contents are the values
    # of GN variables. It is only used to trigger this rule when the values
    # change.
    inputs = [ _framework_toc_file ]

    _stamp_file = "$target_out_dir/run_${_framework_package_target}.stamp"
    outputs = [ _stamp_file ]

    visibility = [ ":$_framework_target" ]

    args = [
             "--framework",
             rebase_path(_framework_base_dir, root_build_dir),
             "--stamp",
             rebase_path(_stamp_file, root_build_dir),
             "--version",
             invoker.framework_version,
             "--contents",
           ] + _framework_contents

    # It is not possible to list _framework_contents as outputs, since
    # ninja does not properly stat symbolic links.
    # https://github.com/ninja-build/ninja/issues/1186
  }

  _link_shared_library_target = target_name + "_shared_library"
  _shared_library_bundle_data = target_name + "_shared_library_bundle_data"

  shared_library(_link_shared_library_target) {
    forward_variables_from(invoker,
                           "*",
                           [
                             "assert_no_deps",
                             "bundle_deps",
                             "code_signing_enabled",
                             "data_deps",
                             "info_plist",
                             "info_plist_target",
                             "output_name",
                             "visibility",
                           ])
    visibility = [
      ":$_shared_library_bundle_data",
      ":${_framework_target}+link_nested",
    ]
    output_name = _output_name
    output_prefix_override = true
    output_extension = ""
    output_dir = "$target_out_dir/$_link_shared_library_target"
  }

  bundle_data(_shared_library_bundle_data) {
    visibility = [ ":$_framework_target" ]
    forward_variables_from(invoker, [ "testonly" ])
    sources = [ "$target_out_dir/$_link_shared_library_target/$_output_name" ]
    outputs = [ "{{bundle_executable_dir}}/$_output_name" ]
    public_deps = [ ":$_link_shared_library_target" ]
  }

  _framework_public_config = _target_name + "_public_config"
  config(_framework_public_config) {
    visibility = [ ":$_framework_target+link" ]
    framework_dirs = [ root_out_dir ]
    frameworks = [ _framework_name ]
  }

  create_bundle(_framework_target) {
    forward_variables_from(invoker,
                           [
                             "data_deps",
                             "deps",
                             "public_deps",
                             "testonly",
                           ])

    if (defined(invoker.visibility)) {
      visibility = invoker.visibility
      visibility += [ ":$_target_name+link" ]
    }

    if (!defined(deps)) {
      deps = []
    }
    deps += [ ":$_info_plist_bundle_data" ]

    if (defined(invoker.bundle_deps)) {
      deps += invoker.bundle_deps
    }

    if (!defined(public_deps)) {
      public_deps = []
    }
    public_deps += [
      ":$_framework_package_target",
      ":$_shared_library_bundle_data",
    ]

    if (enable_dsyms) {
      data = [
        "$root_out_dir/$_output_name.dSYM/Contents/Info.plist",
        "$root_out_dir/$_output_name.dSYM/Contents/Resources/DWARF/$_output_name",
      ]
    }

    bundle_root_dir = _framework_base_dir
    bundle_contents_dir = _framework_root_dir
    bundle_resources_dir = "$bundle_contents_dir/Resources"
    bundle_executable_dir = bundle_contents_dir
  }

  group(_target_name + "+link") {
    forward_variables_from(invoker,
                           [
                             "public_configs",
                             "testonly",
                             "visibility",
                           ])
    public_deps = [ ":$_target_name" ]
    if (!defined(public_configs)) {
      public_configs = []
    }
    public_configs += [ ":$_framework_public_config" ]
  }

  group(_target_name + "+link_nested") {
    forward_variables_from(invoker,
                           [
                             "public_configs",
                             "testonly",
                             "visibility",
                           ])

    # Depend only on the shared library. Nested code will be a dependency of
    # the create_bundle target, which would be cyclic with depending on the
    # framework itself. This is sufficient to link; for loading, a proper
    # install_name should be set.
    public_deps = [ ":$_link_shared_library_target" ]
  }
}

set_defaults("mac_framework_bundle") {
  configs = default_shared_library_configs
}

# Template to create a Mac executable application bundle.
#
# Arguments
#
#     package_type:
#         (optional) string, the product package type to create. Options are:
#             "app" to create a .app bundle (default)
#             "xpc" to create an .xpc service bundle
#
#     info_plist:
#         (optional) string, path to the Info.plist file that will be used for
#         the bundle.
#
#     info_plist_target:
#         (optional) string, if the info_plist is generated from an action,
#         rather than a regular source file, specify the target name in lieu
#         of info_plist. The two arguments are mutually exclusive.
#
#     output_name:
#         (optional) string, name of the generated app without the
#         .app suffix. If omitted, defaults to target_name.
#
#     extra_substitutions:
#         (optional) string array, 'key=value' pairs for extra fields which are
#         specified in a source Info.plist template.
template("mac_app_bundle") {
  _target_name = target_name
  _output_name = target_name
  if (defined(invoker.output_name)) {
    _output_name = invoker.output_name
  }

  _package_type = "app"
  if (defined(invoker.package_type)) {
    _package_type = invoker.package_type
  }

  if (_package_type == "app") {
    _output_extension = "app"
    _product_type = "com.apple.product-type.application"
    _write_pkg_info = true
  } else if (_package_type == "xpc") {
    _output_extension = "xpc"
    _product_type = "com.apple.product-type.xpc-service"
    _write_pkg_info = false
  } else if (_package_type == "bundle") {
    _output_extension = "bundle"
    _product_type = "com.apple.product-type.bundle"
    _write_pkg_info = false
  } else {
    assert(false, "Unsupported packge_type: " + packge_type)
  }

  _executable_target = target_name + "_executable"
  _executable_bundle_data = _executable_target + "_bundle_data"

  _info_plist_target = target_name + "_info_plist"

  mac_info_plist(_info_plist_target) {
    executable_name = _output_name
    forward_variables_from(invoker,
                           [
                             "extra_substitutions",
                             "info_plist",
                             "info_plist_target",
                             "testonly",
                           ])
  }

  if (_write_pkg_info) {
    _pkg_info_target = target_name + "_pkg_info"

    action(_pkg_info_target) {
      forward_variables_from(invoker, [ "testonly" ])
      script = "//build/apple/write_pkg_info.py"
      inputs = [ "//build/apple/plist_util.py" ]
      sources = get_target_outputs(":$_info_plist_target")
      outputs = [ "$target_gen_dir/$_pkg_info_target" ]
      args = [ "--plist" ] + rebase_path(sources, root_build_dir) +
             [ "--output" ] + rebase_path(outputs, root_build_dir)
      deps = [ ":$_info_plist_target" ]
    }
  }

  executable(_executable_target) {
    visibility = [ ":$_executable_bundle_data" ]
    forward_variables_from(invoker,
                           "*",
                           [
                             "assert_no_deps",
                             "data_deps",
                             "info_plist",
                             "output_name",
                             "visibility",
                           ])
    output_name = _output_name
    output_dir = "$target_out_dir/$_executable_target"
  }

  bundle_data(_executable_bundle_data) {
    visibility = [ ":$_target_name" ]
    forward_variables_from(invoker, [ "testonly" ])
    sources = [ "$target_out_dir/$_executable_target/$_output_name" ]
    outputs = [ "{{bundle_executable_dir}}/$_output_name" ]
    public_deps = [ ":$_executable_target" ]
  }

  _info_plist_bundle_data = _info_plist_target + "_bundle_data"

  bundle_data(_info_plist_bundle_data) {
    forward_variables_from(invoker, [ "testonly" ])
    visibility = [ ":$_target_name" ]
    sources = get_target_outputs(":$_info_plist_target")
    outputs = [ "{{bundle_contents_dir}}/Info.plist" ]
    public_deps = [ ":$_info_plist_target" ]
  }

  if (_write_pkg_info) {
    _pkg_info_bundle_data = _pkg_info_target + "_bundle_data"

    bundle_data(_pkg_info_bundle_data) {
      forward_variables_from(invoker, [ "testonly" ])
      visibility = [ ":$_target_name" ]
      sources = get_target_outputs(":$_pkg_info_target")
      outputs = [ "{{bundle_contents_dir}}/PkgInfo" ]
      public_deps = [ ":$_pkg_info_target" ]
    }
  }

  create_bundle(_target_name) {
    forward_variables_from(invoker,
                           [
                             "data_deps",
                             "deps",
                             "public_deps",
                             "testonly",
                           ])
    if (!defined(deps)) {
      deps = []
    }
    deps += [
      ":$_executable_bundle_data",
      ":$_info_plist_bundle_data",
    ]
    if (_write_pkg_info) {
      deps += [ ":$_pkg_info_bundle_data" ]
    }

    if (enable_dsyms) {
      data = [
        "$root_out_dir/$_output_name.dSYM/Contents/Info.plist",
        "$root_out_dir/$_output_name.dSYM/Contents/Resources/DWARF/$_output_name",
      ]
    }

    product_type = _product_type
    bundle_root_dir = "$root_out_dir/${_output_name}.${_output_extension}"
    bundle_contents_dir = "$bundle_root_dir/Contents"
    bundle_resources_dir = "$bundle_contents_dir/Resources"
    bundle_executable_dir = "$bundle_contents_dir/MacOS"
  }
}

set_defaults("mac_app_bundle") {
  configs = default_executable_configs
}

# Template to package a loadable_module into a .plugin bundle.
#
# This takes no extra arguments that differ from a loadable_module.
template("mac_plugin_bundle") {
  assert(defined(invoker.deps),
         "Dependencies must be specified for $target_name")

  _target_name = target_name
  _loadable_module_target = _target_name + "_loadable_module"
  _loadable_module_bundle_data = _loadable_module_target + "_bundle_data"

  _output_name = _target_name
  if (defined(invoker.output_name)) {
    _output_name = invoker.output_name
  }

  loadable_module(_loadable_module_target) {
    visibility = [ ":$_loadable_module_bundle_data" ]
    forward_variables_from(invoker,
                           "*",
                           [
                             "assert_no_deps",
                             "data_deps",
                             "output_name",
                             "visibility",
                           ])
    output_dir = "$target_out_dir"
    output_name = _output_name
  }

  bundle_data(_loadable_module_bundle_data) {
    forward_variables_from(invoker, [ "testonly" ])
    visibility = [ ":$_target_name" ]
    sources = [ "$target_out_dir/$_output_name.so" ]
    outputs = [ "{{bundle_executable_dir}}/$_output_name" ]
    public_deps = [ ":$_loadable_module_target" ]
  }

  create_bundle(_target_name) {
    forward_variables_from(invoker,
                           [
                             "data_deps",
                             "deps",
                             "public_deps",
                             "testonly",
                             "visibility",
                           ])
    if (!defined(deps)) {
      deps = []
    }
    deps += [ ":$_loadable_module_bundle_data" ]

    if (enable_dsyms) {
      data = [
        "$root_out_dir/$_output_name.so.dSYM/Contents/Info.plist",
        "$root_out_dir/$_output_name.so.dSYM/Contents/Resources/DWARF/$_output_name.so",
      ]
    }

    bundle_root_dir = "$root_out_dir/$_output_name.plugin"
    bundle_contents_dir = "$bundle_root_dir/Contents"
    bundle_executable_dir = "$bundle_contents_dir/MacOS"
  }
}