Class: Pod::Installer::Xcode::PodsProjectGenerator::PodTargetInstaller

Inherits:
TargetInstaller
  • Object
show all
Defined in:
lib/cocoapods/installer/xcode/pods_project_generator/pod_target_installer.rb

Overview

Creates the target for the Pods libraries in the Pods project and the relative support files.

Constant Summary collapse

ENABLE_OBJECT_USE_OBJC_FROM =
{
  :ios => Version.new('6'),
  :osx => Version.new('10.8'),
  :watchos => Version.new('2.0'),
  :tvos => Version.new('9.0'),
}.freeze

Instance Attribute Summary collapse

Attributes inherited from TargetInstaller

#project, #sandbox

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from TargetInstaller

#add_file_to_support_group, #clean_support_files_temp_dir, #create_dummy_source, #create_info_plist_file, #create_support_files_dir, #deployment_target, #support_files_temp_dir

Methods included from TargetInstallerHelper

create_info_plist_file_with_sandbox, #create_info_plist_file_with_sandbox, #create_prefix_header, create_prefix_header, update_changed_file, #update_changed_file

Constructor Details

#initialize(sandbox, project, target, umbrella_header_paths = nil) ⇒ PodTargetInstaller

Initialize a new instance

Parameters:

  • sandbox (Sandbox)

    @see TargetInstaller#sandbox

  • project (Pod::Project)

    @see TargetInstaller#project

  • target (PodTarget)

    @see TargetInstaller#target

  • umbrella_header_paths (Array<Pathname>) (defaults to: nil)

    @see #umbrella_header_paths


30
31
32
33
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_installer.rb', line 30

def initialize(sandbox, project, target, umbrella_header_paths = nil)
  super(sandbox, project, target)
  @umbrella_header_paths = umbrella_header_paths
end

Instance Attribute Details

#targetPodTarget (readonly)

Returns @see TargetInstaller#target.

Returns:

  • (PodTarget)

    @see TargetInstaller#target


21
22
23
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_installer.rb', line 21

def target
  @target
end

#umbrella_header_pathsArray<Pathname> (readonly)

Returns Array of umbrella header paths in the headers directory.

Returns:

  • (Array<Pathname>)

    Array of umbrella header paths in the headers directory


17
18
19
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_installer.rb', line 17

def umbrella_header_paths
  @umbrella_header_paths
end

Class Method Details

.bcsymbolmap_paths(target) ⇒ Array<String>

Returns the bcsymbolmap paths for the given target.

Parameters:

  • target (PodTarget)

    the target to be installed

Returns:

  • (Array<String>)

    the bcsymbolmap paths for the given target


1197
1198
1199
1200
1201
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_installer.rb', line 1197

def bcsymbolmap_paths(target)
  target.framework_paths.values.flatten.reject do |fmwk_path|
    fmwk_path.bcsymbolmap_paths.nil?
  end.flat_map(&:bcsymbolmap_paths)
end

.dsym_paths(target) ⇒ Array<String>

Returns the dSYM paths for the given target.

Parameters:

  • target (PodTarget)

    the target to be installed

Returns:

  • (Array<String>)

    the dSYM paths for the given target


1187
1188
1189
1190
1191
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_installer.rb', line 1187

def dsym_paths(target)
  dsym_paths = target.framework_paths.values.flatten.reject { |fmwk_path| fmwk_path.dsym_path.nil? }.map(&:dsym_path)
  dsym_paths.concat(target.xcframeworks.values.flatten.flat_map { |xcframework| xcframework_dsyms(xcframework.path) })
  dsym_paths
end

.xcframework_dsyms(xcframework_path) ⇒ Array<Pathname>

Returns all found .dSYM paths.

Parameters:

  • xcframework_path (Pathname)

    the base path of the .xcframework bundle

Returns:

  • (Array<Pathname>)

    all found .dSYM paths


1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_installer.rb', line 1208

def xcframework_dsyms(xcframework_path)
  basename = File.basename(xcframework_path, '.xcframework')
  dsym_basename = basename + '.dSYMs'
  path = xcframework_path.dirname + dsym_basename
  if File.directory?(path)
    Dir.glob(path + '*.dSYM')
  else
    []
  end
end

Instance Method Details

#add_app_targetsHash{Specification => PBXNativeTarget} (private)

Adds the app targets for the library to the Pods project with the appropriate build configurations.

Returns:

  • (Hash{Specification => PBXNativeTarget})

    the app native targets created, keyed by their app spec


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
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_installer.rb', line 452

def add_app_targets
  target.app_specs.each_with_object({}) do |app_spec, hash|
    spec_consumer = app_spec.consumer(target.platform)
    spec_name = app_spec.parent.name
    subspec_name = target.subspec_label(app_spec)
    app_target_label = target.app_target_label(app_spec)
    platform = Platform.new(target.platform.symbolic_name, target.deployment_target_for_non_library_spec(app_spec))
    info_plist_entries = spec_consumer.info_plist
    resources = target.file_accessors.find { |fa| fa.spec == app_spec }.resources
    add_launchscreen_storyboard = resources.none? { |resource| resource.basename.to_s == 'LaunchScreen.storyboard' } && platform.name == :ios
    app_native_target = AppHostInstaller.new(sandbox, project, platform, subspec_name, spec_name,
                                             app_target_label, :add_main => false,
                                                               :add_launchscreen_storyboard => add_launchscreen_storyboard,
                                                               :info_plist_entries => info_plist_entries).install!

    app_native_target.product_reference.name = app_target_label
    target.user_build_configurations.each do |bc_name, type|
      app_native_target.add_build_configuration(bc_name, type)
    end

    app_native_target.build_configurations.each do |configuration|
      configuration.build_settings.merge!(custom_build_settings)

      # target_installer will automatically add an empty `OTHER_LDFLAGS`. For app
      # targets those are set via an app xcconfig file instead.
      configuration.build_settings.delete('OTHER_LDFLAGS')
      # target_installer will automatically set the product name to the module name if the target
      # requires frameworks. For apps we always use the app target name as the product name
      # irrelevant to whether we use frameworks or not.
      configuration.build_settings['PRODUCT_NAME'] = app_target_label
      # target_installer sets 'MACH_O_TYPE' for static frameworks ensure this does not propagate
      # to app target.
      configuration.build_settings.delete('MACH_O_TYPE')
      # Use xcode default product module name, which is $(PRODUCT_NAME:c99extidentifier)
      # this gives us always valid name that is distinct from the parent spec module name
      # which allow the app to use import to access the parent framework
      configuration.build_settings.delete('PRODUCT_MODULE_NAME')

      # We must codesign iOS app bundles that contain binary frameworks to allow them to be launchable in the simulator
      unless target.platform == :osx
        configuration.build_settings['CODE_SIGNING_REQUIRED'] = 'YES'
        configuration.build_settings['CODE_SIGNING_ALLOWED'] = 'YES'
      end
      # For macOS we do not code sign the appbundle because we do not code sign the frameworks either.
      configuration.build_settings['CODE_SIGN_IDENTITY'] = '' if target.platform == :osx
      # For iOS, we delete the target_installer empty values that get set for libraries since CocoaPods will
      # code sign the libraries manually but for apps this is not true.
      if target.platform == :ios
        configuration.build_settings.delete('CODE_SIGN_IDENTITY[sdk=appletvos*]')
        configuration.build_settings.delete('CODE_SIGN_IDENTITY[sdk=iphoneos*]')
        configuration.build_settings.delete('CODE_SIGN_IDENTITY[sdk=watchos*]')
      end
    end

    remove_pod_target_xcconfig_overrides_from_target(target.app_spec_build_settings_by_config[app_spec.name], app_native_target)

    create_app_target_embed_frameworks_script(app_spec)
    create_app_target_copy_resources_script(app_spec)
    add_resources_to_target(resources, app_native_target)

    hash[app_spec] = app_native_target
  end
end

#add_files_to_build_phases(native_target, test_native_targets, app_native_targets) ⇒ void (private)

Note:

The Frameworks are used only for presentation purposes as the xcconfig is the authoritative source about their information.

Note:

Core Data model directories (.xcdatamodeld) defined in the resources property are currently added to the Copy Resources build phase like all other resources. The Xcode UI adds these to the Compile Sources build phase, but they will compile correctly either way.

This method returns an undefined value.

Adds the build files of the pods to the target and adds a reference to the frameworks of the Pods.


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
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_installer.rb', line 310

def add_files_to_build_phases(native_target, test_native_targets, app_native_targets)
  target.file_accessors.each do |file_accessor|
    consumer = file_accessor.spec_consumer

    native_target =  case consumer.spec.spec_type
                     when :library
                       native_target
                     when :test
                       test_native_target_from_spec(consumer.spec, test_native_targets)
                     when :app
                       app_native_targets[consumer.spec]
                     end

    headers = file_accessor.headers
    public_headers = file_accessor.public_headers.map(&:realpath)
    private_headers = file_accessor.private_headers.map(&:realpath)
    other_source_files = file_accessor.other_source_files

    {
      true => file_accessor.arc_source_files,
      false => file_accessor.non_arc_source_files,
    }.each do |arc, source_files|
      next if source_files.empty?
      source_files = source_files - headers - other_source_files
      swift_source_files, non_swift_source_files = source_files.partition { |file| file.extname == '.swift' }
      {
        :objc => non_swift_source_files,
        :swift => swift_source_files,
      }.each do |language, files|
        compiler_flags = compiler_flags_for_consumer(consumer, arc, language)
        file_refs = project_file_references_array(files, 'source')
        native_target.add_file_references(file_refs, compiler_flags)
      end
    end

    header_file_refs = project_file_references_array(headers, 'header')
    native_target.add_file_references(header_file_refs) do |build_file|
      add_header(file_accessor, build_file, public_headers, private_headers, native_target)
    end

    other_file_refs = project_file_references_array(other_source_files, 'other source')
    native_target.add_file_references(other_file_refs, nil)

    next unless target.build_as_framework?

    filter_resource_file_references(file_accessor.resources.flatten) do |compile_phase_refs, resource_phase_refs|
      native_target.add_file_references(compile_phase_refs, nil)

      if target.build_as_static_framework? && consumer.spec.library_specification?
        resource_phase_refs = resource_phase_refs.select { |ref| Target.resource_extension_compilable?(File.extname(ref.path)) }
      end

      native_target.add_resources(resource_phase_refs)
    end
  end
end

#add_header(file_accessor, build_file, public_headers, private_headers, native_target) ⇒ Object (private)


1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_installer.rb', line 1029

def add_header(file_accessor, build_file, public_headers, private_headers, native_target)
  file_ref = build_file.file_ref
  acl = if !target.build_as_framework? # Headers are already rooted at ${PODS_ROOT}/Headers/P*/[pod]/...
          'Project'
        elsif public_headers.include?(file_ref.real_path)
          'Public'
        elsif private_headers.include?(file_ref.real_path)
          'Private'
        else
          'Project'
        end

  if target.build_as_framework? && !header_mappings_dir(file_accessor).nil? && acl != 'Project'
    relative_path = if mapping_dir = header_mappings_dir(file_accessor)
                      file_ref.real_path.relative_path_from(mapping_dir)
                    else
                      file_ref.real_path.relative_path_from(file_accessor.path_list.root)
                    end
    compile_build_phase_index = native_target.build_phases.index do |bp|
      bp.is_a?(Xcodeproj::Project::Object::PBXSourcesBuildPhase)
    end
    sub_dir = relative_path.dirname
    copy_phase_name = "Copy #{sub_dir} #{acl} Headers"
    copy_phase = native_target.copy_files_build_phases.find { |bp| bp.name == copy_phase_name } ||
      native_target.new_copy_files_build_phase(copy_phase_name)
    native_target.build_phases.move(copy_phase, compile_build_phase_index - 1) unless compile_build_phase_index.nil?
    copy_phase.symbol_dst_subfolder_spec = :products_directory
    copy_phase.dst_path = "$(#{acl.upcase}_HEADERS_FOLDER_PATH)/#{sub_dir}"
    copy_phase.add_file_reference(file_ref, true)
  else
    build_file.settings ||= {}
    build_file.settings['ATTRIBUTES'] = [acl]
  end
end

#add_placeholder_targetPBXAggregateTarget (private)

Adds a placeholder native target for the library to the Pods project with the appropriate build configurations.

Returns:

  • (PBXAggregateTarget)

    the native target that was added.


1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_installer.rb', line 1082

def add_placeholder_target
  native_target = project.new_aggregate_target(target.label, [], target.platform.name, deployment_target)
  target.user_build_configurations.each do |bc_name, type|
    native_target.add_build_configuration(bc_name, type)
  end
  unless target.archs.empty?
    native_target.build_configurations.each do |configuration|
      configuration.build_settings['ARCHS'] = target.archs
    end
  end
  native_target
end

#add_resources_bundle_targets(file_accessors) ⇒ Hash{String=>Array<PBXNativeTarget>} (private)

Note:

The source files are grouped by Pod and in turn by subspec (recursively) in the resources group.

Adds the resources of the Pods to the Pods project.

Parameters:

  • file_accessors (Array<Sandbox::FileAccessor>)

    the file accessors list to generate resource bundles for.

Returns:

  • (Hash{String=>Array<PBXNativeTarget>})

    the resource bundle native targets created.


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
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_installer.rb', line 543

def add_resources_bundle_targets(file_accessors)
  file_accessors.each_with_object({}) do |file_accessor, hash|
    hash[file_accessor.spec.name] = file_accessor.resource_bundles.map do |bundle_name, paths|
      label = target.resources_bundle_target_label(bundle_name)
      resource_bundle_target = project.new_resources_bundle(label, file_accessor.spec_consumer.platform_name)
      resource_bundle_target.product_reference.tap do |bundle_product|
        bundle_file_name = "#{bundle_name}.bundle"
        bundle_product.name = bundle_file_name
      end

      contains_compile_phase_refs = add_resources_to_target(paths, resource_bundle_target)

      target.user_build_configurations.each do |bc_name, type|
        resource_bundle_target.add_build_configuration(bc_name, type)
      end
      resource_bundle_target.deployment_target = if file_accessor.spec.non_library_specification?
                                                   target.deployment_target_for_non_library_spec(file_accessor.spec)
                                                 else
                                                   deployment_target
                                                 end
      # Create Info.plist file for bundle
      path = target.info_plist_path
      path.dirname.mkdir unless path.dirname.exist?
      info_plist_path = path.dirname + "ResourceBundle-#{bundle_name}-#{path.basename}"
      create_info_plist_file(info_plist_path, resource_bundle_target, target.version, target.platform, :bndl)

      resource_bundle_target.build_configurations.each do |configuration|
        configuration.build_settings['PRODUCT_NAME'] = bundle_name
        # Do not set the CONFIGURATION_BUILD_DIR for resource bundles that are only meant for test targets.
        # This is because the test target itself also does not set this configuration build dir and it expects
        # all bundles to be copied from the default path.
        unless file_accessor.spec.test_specification?
          configuration.build_settings['CONFIGURATION_BUILD_DIR'] = target.configuration_build_dir('$(BUILD_DIR)/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)')
        end

        # Set the 'IBSC_MODULE' build settings for resource bundles so that Storyboards and Xibs can load
        # classes from the parent module.
        configuration.build_settings['IBSC_MODULE'] = target.product_module_name

        # Set the `SWIFT_VERSION` build setting for resource bundles that could have resources that get
        # compiled such as an `xcdatamodeld` file which has 'Swift' as its code generation language.
        if contains_compile_phase_refs && file_accessors.any? { |fa| target.uses_swift_for_spec?(fa.spec) }
          configuration.build_settings['SWIFT_VERSION'] = target.swift_version
        end

        # Set the correct device family for this bundle, based on the platform
        device_family_by_platform = {
          :ios => '1,2',
          :tvos => '3',
          :watchos => '1,2,4',
        }

        if (family = device_family_by_platform[target.platform.name])
          configuration.build_settings['TARGETED_DEVICE_FAMILY'] = family
        end
      end

      remove_pod_target_xcconfig_overrides_from_target(target.build_settings_by_config_for_spec(file_accessor.spec), resource_bundle_target)

      resource_bundle_target
    end
  end
end

#add_resources_to_target(paths, target) ⇒ Boolean (private)

Adds the resources to the compile resources phase of the target.

Parameters:

  • paths (Array<Pathname>)

    the paths to add to the target.

  • target (PBXNativeTarget)

    the target resources are added to.

Returns:

  • (Boolean)

    whether any compile phase references were added.


524
525
526
527
528
529
530
531
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_installer.rb', line 524

def add_resources_to_target(paths, target)
  filter_resource_file_references(paths) do |compile_phase_refs, resource_phase_refs|
    # Resource bundles are only meant to have resources, so install everything
    # into the resources phase. See note in filter_resource_file_references.
    target.add_resources(resource_phase_refs + compile_phase_refs)
    !compile_phase_refs.empty?
  end
end

#add_swift_library_compatibility_header_phase(native_target) ⇒ Void (private)

Adds a shell script phase, intended only for library targets that contain swift, to copy the ObjC compatibility header (the -Swift.h file that the swift compiler generates) to the built products directory. Additionally, the script phase copies the module map, appending a .Swift submodule that references the (moved) compatibility header. Since the module map has been moved, the umbrella header is also copied, so that it is sitting next to the module map. This is necessary for a successful archive build.

Parameters:

  • native_target (PBXNativeTarget)

    the native target to add the Swift static library script phase into.

Returns:

  • (Void)

1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_installer.rb', line 1106

def add_swift_library_compatibility_header_phase(native_target)
  if custom_module_map
    raise Informative, 'Using Swift static libraries with custom module maps is currently not supported. ' \
                       "Please build `#{target.label}` as a framework or remove the custom module map."
  end

  build_phase = native_target.new_shell_script_build_phase('Copy generated compatibility header')

  relative_module_map_path = target.module_map_path.relative_path_from(target.sandbox.root)
  relative_umbrella_header_path = target.umbrella_header_path.relative_path_from(target.sandbox.root)

  build_phase.shell_script = <<-SH.strip_heredoc
    COMPATIBILITY_HEADER_PATH="${BUILT_PRODUCTS_DIR}/Swift Compatibility Header/${PRODUCT_MODULE_NAME}-Swift.h"
    MODULE_MAP_PATH="${BUILT_PRODUCTS_DIR}/${PRODUCT_MODULE_NAME}.modulemap"

    ditto "${DERIVED_SOURCES_DIR}/${PRODUCT_MODULE_NAME}-Swift.h" "${COMPATIBILITY_HEADER_PATH}"
    ditto "${PODS_ROOT}/#{relative_module_map_path}" "${MODULE_MAP_PATH}"
    ditto "${PODS_ROOT}/#{relative_umbrella_header_path}" "${BUILT_PRODUCTS_DIR}"
    printf "\\n\\nmodule ${PRODUCT_MODULE_NAME}.Swift {\\n  header \\"${COMPATIBILITY_HEADER_PATH}\\"\\n  requires objc\\n}\\n" >> "${MODULE_MAP_PATH}"
  SH
  build_phase.input_paths = %W(
    ${DERIVED_SOURCES_DIR}/${PRODUCT_MODULE_NAME}-Swift.h
    ${PODS_ROOT}/#{relative_module_map_path}
    ${PODS_ROOT}/#{relative_umbrella_header_path}
  )
  build_phase.output_paths = %W(
    ${BUILT_PRODUCTS_DIR}/${PRODUCT_MODULE_NAME}.modulemap
    ${BUILT_PRODUCTS_DIR}/#{relative_umbrella_header_path.basename}
    ${BUILT_PRODUCTS_DIR}/Swift\ Compatibility\ Header/${PRODUCT_MODULE_NAME}-Swift.h
  )
end

#add_targetPBXNativeTarget (private)

Note:

Overrides the superclass implementation to remove settings that are set in the pod target xcconfig

Adds the target for the library to the Pods project with the appropriate build configurations.

Returns:

  • (PBXNativeTarget)

    the native target that was added.


156
157
158
159
160
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_installer.rb', line 156

def add_target
  super.tap do |native_target|
    remove_pod_target_xcconfig_overrides_from_target(target.build_settings, native_target)
  end
end

#add_test_app_host_targetsArray<PBXNativeTarget> (private)

Adds the test app host targets for the library to the Pods project with the appropriate build configurations.

Returns:

  • (Array<PBXNativeTarget>)

    the app host targets created.


436
437
438
439
440
441
442
443
444
445
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_installer.rb', line 436

def add_test_app_host_targets
  target.test_spec_consumers.reject(&:requires_app_host?).select(&:app_host_name).each do |test_spec_consumer|
    raise Informative, "`#{target.label}-#{test_spec_consumer.test_type}-Tests` manually specifies an app host but has not specified `requires_app_host = true`."
  end

  target.test_spec_consumers.select(&:requires_app_host?).reject(&:app_host_name).group_by { |consumer| target.app_host_target_label(consumer.spec) }.
    map do |(_, target_name), _|
      AppHostInstaller.new(sandbox, project, target.platform, target_name, target.pod_name, target_name).install!
    end
end

#add_test_targetsArray<PBXNativeTarget> (private)

Adds the test targets for the library to the Pods project with the appropriate build configurations.

Returns:

  • (Array<PBXNativeTarget>)

    the test native targets created.


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
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_installer.rb', line 372

def add_test_targets
  target.test_specs.map do |test_spec|
    spec_consumer = test_spec.consumer(target.platform)
    test_type = spec_consumer.test_type
    product_type = target.product_type_for_test_type(test_type)
    name = target.test_target_label(test_spec)
    platform_name = target.platform.name
    language = target.uses_swift_for_spec?(test_spec) ? :swift : :objc
    test_native_target = project.new_target(product_type, name, platform_name,
                                            target.deployment_target_for_non_library_spec(test_spec), nil,
                                            language)
    test_native_target.product_reference.name = name

    target.user_build_configurations.each do |bc_name, type|
      test_native_target.add_build_configuration(bc_name, type)
    end

    test_native_target.build_configurations.each do |configuration|
      configuration.build_settings.merge!(custom_build_settings)
      # target_installer will automatically add an empty `OTHER_LDFLAGS`. For test
      # targets those are set via a test xcconfig file instead.
      configuration.build_settings.delete('OTHER_LDFLAGS')
      # target_installer will automatically set the product name to the module name if the target
      # requires frameworks. For tests we always use the test target name as the product name
      # irrelevant to whether we use frameworks or not.
      configuration.build_settings['PRODUCT_NAME'] = name
      # target_installer sets 'MACH_O_TYPE' for static frameworks ensure this does not propagate
      # to test target.
      configuration.build_settings.delete('MACH_O_TYPE')
      # Use xcode default product module name, which is $(PRODUCT_NAME:c99extidentifier)
      # this gives us always valid name that is distinct from the parent spec module name
      # which allow tests to use either import or @testable import to access the parent framework
      configuration.build_settings.delete('PRODUCT_MODULE_NAME')
      # We must codesign iOS XCTest bundles that contain binary frameworks to allow them to be launchable in the simulator
      unless target.platform == :osx
        configuration.build_settings['CODE_SIGNING_REQUIRED'] = 'YES'
        configuration.build_settings['CODE_SIGNING_ALLOWED'] = 'YES'
      end
      # For macOS we do not code sign the XCTest bundle because we do not code sign the frameworks either.
      configuration.build_settings['CODE_SIGN_IDENTITY'] = '' if target.platform == :osx
    end

    remove_pod_target_xcconfig_overrides_from_target(target.test_spec_build_settings_by_config[test_spec.name], test_native_target)

    # Test native targets also need frameworks and resources to be copied over to their xctest bundle.
    create_test_target_embed_frameworks_script(test_spec)
    create_test_target_copy_resources_script(test_spec)

    # Generate vanilla Info.plist for test target similar to the one Xcode generates for new test target.
    # This creates valid test bundle accessible at the runtime, allowing tests to load bundle resources
    # defined in podspec.
    additional_entries = spec_consumer.info_plist
    path = target.info_plist_path_for_spec(test_spec)
    create_info_plist_file(path, test_native_target, '1.0', target.platform, :bndl, :additional_entries => additional_entries)

    test_native_target
  end
end

#any_header_mapping_dirs?Boolean (private)

Returns:

  • (Boolean)

1016
1017
1018
1019
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_installer.rb', line 1016

def any_header_mapping_dirs?
  return @any_header_mapping_dirs if defined?(@any_header_mapping_dirs)
  @any_header_mapping_dirs = target.file_accessors.any? { |fa| fa.spec_consumer.header_mappings_dir }
end

#apply_xcconfig_file_ref_to_targets(targets, xcconfig_file_ref, configurations) ⇒ Object (private)


944
945
946
947
948
949
950
951
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_installer.rb', line 944

def apply_xcconfig_file_ref_to_targets(targets, xcconfig_file_ref, configurations)
  targets.each do |config_target|
    config_target.build_configurations.each do |configuration|
      next unless configurations.include?(configuration.name)
      configuration.base_configuration_reference = xcconfig_file_ref
    end
  end
end

#compiler_flags_for_consumer(consumer, arc, language) ⇒ String (private)

Returns the compiler flags for the source files of the given specification.

The following behavior is regarding the OS_OBJECT_USE_OBJC flag. When set to 0, it will allow code to use dispatch_release() on >= iOS 6.0 and OS X 10.8.

  • New libraries that do not require ARC don’t need to care about this issue at all.

  • New libraries that do require ARC and have a deployment target of

    = iOS 6.0 or OS X 10.8:

These no longer use dispatch_release() and should not have the OS_OBJECT_USE_OBJC flag set to 0.

Note: this means that these libraries have to specify the deployment target in order to function well.

  • New libraries that do require ARC, but have a deployment target of < iOS 6.0 or OS X 10.8:

These contain dispatch_release() calls and as such need the OS_OBJECT_USE_OBJC flag set to 1.

Note: libraries that do not specify a platform version are assumed to have a deployment target of < iOS 6.0 or OS X 10.8.

For more information, see: https://opensource.apple.com/source/libdispatch/libdispatch-228.18/os/object.h

Parameters:

  • consumer (Specification::Consumer)

    The consumer for the specification for which the compiler flags are needed.

  • arc (Boolean)

    Whether the arc is enabled or not.

  • language (Symbol)

    The language these compiler warnings are for. Can be either :objc or :swift.

Returns:

  • (String)

    The compiler flags.


927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_installer.rb', line 927

def compiler_flags_for_consumer(consumer, arc, language)
  flags = consumer.compiler_flags.dup
  if !arc && language == :objc
    flags << '-fno-objc-arc'
  else
    platform_name = consumer.platform_name
    spec_deployment_target = consumer.spec.deployment_target(platform_name)
    if spec_deployment_target.nil? || Version.new(spec_deployment_target) < ENABLE_OBJECT_USE_OBJC_FROM[platform_name]
      flags << '-DOS_OBJECT_USE_OBJC=0'
    end
  end
  if target.inhibit_warnings? && language == :objc
    flags << '-w -Xanalyzer -analyzer-disable-all-checks'
  end
  flags * ' '
end

#create_app_target_copy_resources_script(app_spec) ⇒ void (private)

This method returns an undefined value.

Creates a script that copies the resources to the bundle of the app target.

Parameters:

  • app_spec (Specification)

    The app spec to create the copy resources script for.


758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_installer.rb', line 758

def create_app_target_copy_resources_script(app_spec)
  path = target.copy_resources_script_path_for_spec(app_spec)
  resource_paths_by_config = target.user_build_configurations.each_with_object({}) do |(config_name, config), resources_by_config|
    pod_targets = target.dependent_targets_for_app_spec(app_spec, :configuration => config)
    resources_by_config[config_name] = pod_targets.flat_map do |pod_target|
      spec_paths_to_include = pod_target.library_specs.map(&:name)
      spec_paths_to_include << app_spec.name if pod_target == target
      pod_target.resource_paths.values_at(*spec_paths_to_include).flatten.compact
    end
  end
  unless resource_paths_by_config.each_value.all?(&:empty?)
    generator = Generator::CopyResourcesScript.new(resource_paths_by_config, target.platform)
    update_changed_file(generator, path)
    add_file_to_support_group(path)
  end
end

#create_app_target_embed_frameworks_script(app_spec) ⇒ void (private)

This method returns an undefined value.

Creates a script that embeds the frameworks to the bundle of the app target.

Parameters:

  • app_spec (Specification)

    The app spec to create the embed frameworks script for.


782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_installer.rb', line 782

def create_app_target_embed_frameworks_script(app_spec)
  path = target.embed_frameworks_script_path_for_spec(app_spec)
  framework_paths_by_config = target.user_build_configurations.each_with_object({}) do |(config_name, config), paths_by_config|
    paths_by_config[config_name] = target.dependent_targets_for_app_spec(app_spec, :configuration => config).flat_map do |pod_target|
      spec_paths_to_include = pod_target.library_specs.map(&:name)
      spec_paths_to_include << app_spec.name if pod_target == target
      pod_target.framework_paths.values_at(*spec_paths_to_include).flatten.compact.uniq
    end
  end
  xcframeworks_by_config = target.user_build_configurations.each_with_object({}) do |(config_name, config), paths_by_config|
    paths_by_config[config_name] = target.dependent_targets_for_app_spec(app_spec, :configuration => config).flat_map do |pod_target|
      spec_paths_to_include = pod_target.library_specs.map(&:name)
      spec_paths_to_include << app_spec.name if pod_target == target
      pod_target.xcframeworks.values_at(*spec_paths_to_include).flatten.compact.uniq
    end
  end

  unless framework_paths_by_config.each_value.all?(&:empty?) && xcframeworks_by_config.each_value.all?(&:empty?)
    generator = Generator::EmbedFrameworksScript.new(framework_paths_by_config, xcframeworks_by_config)
    update_changed_file(generator, path)
    add_file_to_support_group(path)
  end
end

#create_app_xcconfig_files(app_native_targets, app_resource_bundle_targets) ⇒ void (private)

This method returns an undefined value.

Generates the contents of the xcconfig file used for each app target type and saves it to disk.

Parameters:

  • app_native_targets (Hash{Specification => PBXNativeTarget})

    the app native targets to link the xcconfig file into.

  • app_resource_bundle_targets (Hash{String=>Array<PBXNativeTarget>})

    the additional app resource bundle targets to link the xcconfig file into.


733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_installer.rb', line 733

def create_app_xcconfig_files(app_native_targets, app_resource_bundle_targets)
  target.app_specs.each do |app_spec|
    spec_consumer = app_spec.consumer(target.platform)
    app_native_target = app_native_targets[spec_consumer.spec]

    target.user_config_names_by_config_type.each do |config, names|
      path = target.xcconfig_path("#{target.subspec_label(app_spec)}.#{config}")
      app_spec_build_settings = target.build_settings_for_spec(app_spec, :configuration => config)
      update_changed_file(app_spec_build_settings, path)
      app_xcconfig_file_ref = add_file_to_support_group(path)

      # also apply the private config to resource bundle app targets related to this app spec.
      scoped_app_resource_bundle_targets = app_resource_bundle_targets[app_spec.name]
      apply_xcconfig_file_ref_to_targets([app_native_target] + scoped_app_resource_bundle_targets, app_xcconfig_file_ref, names)
    end
  end
end

This method returns an undefined value.

Creates a build phase which links the versioned header folders of the OS X into the framework bundle's root root directory. This is only necessary because the way how headers are copied via custom copy file build phases in combination with header_mappings_dir interferes with xcodebuild's expectations about the existence of private or public headers.

Parameters:

  • native_target (PBXNativeTarget)

    the native target to add the script phase into.


860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_installer.rb', line 860

def create_build_phase_to_symlink_header_folders(native_target)
  return unless target.platform.name == :osx && any_header_mapping_dirs?

  build_phase = native_target.new_shell_script_build_phase('Create Symlinks to Header Folders')
  build_phase.shell_script = <<-eos.strip_heredoc
    cd "$CONFIGURATION_BUILD_DIR/$WRAPPER_NAME" || exit 1

    public_path="${PUBLIC_HEADERS_FOLDER_PATH\#\$CONTENTS_FOLDER_PATH/}"
    if [ ! -f "$public_path" ]; then
      ln -fs "${PUBLIC_HEADERS_FOLDER_PATH\#$WRAPPER_NAME/}" "$public_path"
    fi

    private_path="${PRIVATE_HEADERS_FOLDER_PATH\#\$CONTENTS_FOLDER_PATH/}"
    if [ ! -f "$private_path" ]; then
      ln -fs "${PRIVATE_HEADERS_FOLDER_PATH\#\$WRAPPER_NAME/}" "$private_path"
    fi
  eos
end

#create_copy_dsyms_scriptvoid (private)

This method returns an undefined value.

Creates a script that copies and strips vendored dSYMs and bcsymbolmaps.


810
811
812
813
814
815
816
817
818
819
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_installer.rb', line 810

def create_copy_dsyms_script
  dsym_paths = PodTargetInstaller.dsym_paths(target)
  bcsymbolmap_paths = PodTargetInstaller.bcsymbolmap_paths(target)
  path = target.copy_dsyms_script_path
  unless dsym_paths.empty? && bcsymbolmap_paths.empty?
    generator = Generator::CopydSYMsScript.new(dsym_paths, bcsymbolmap_paths)
    update_changed_file(generator, path)
    add_file_to_support_group(path)
  end
end

#create_copy_xcframeworks_scriptvoid (private)

Note:

We can't use Xcode default link libraries phase, because we need to ensure that we only copy the frameworks which are relevant for the current build configuration.

This method returns an undefined value.

Creates a script that copies the appropriate xcframework slice to the build dir.


829
830
831
832
833
834
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_installer.rb', line 829

def create_copy_xcframeworks_script
  path = target.copy_xcframeworks_script_path
  generator = Generator::CopyXCFrameworksScript.new(target.xcframeworks.values.flatten, sandbox.root, target.platform)
  update_changed_file(generator, path)
  add_file_to_support_group(path)
end

#create_module_map(native_target) ⇒ Object (private)


953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_installer.rb', line 953

def create_module_map(native_target)
  return super(native_target) unless custom_module_map

  path = target.module_map_path_to_write
  UI.message "- Copying module map file to #{UI.path(path)}" do
    contents = custom_module_map.read
    unless target.build_as_framework?
      contents.gsub!(/^(\s*)framework\s+(module[^{}]+){/, '\1\2{')
    end
    generator = Generator::Constant.new(contents)
    update_changed_file(generator, path)
    add_file_to_support_group(path)

    linked_path = target.module_map_path
    if path != linked_path
      linked_path.dirname.mkpath
      source = path.relative_path_from(linked_path.dirname)
      FileUtils.ln_sf(source, linked_path)
    end

    relative_path = target.module_map_path.relative_path_from(sandbox.root).to_s
    native_target.build_configurations.each do |c|
      c.build_settings['MODULEMAP_FILE'] = relative_path.to_s
    end
  end
end

#create_test_target_copy_resources_script(test_spec) ⇒ void (private)

This method returns an undefined value.

Creates a script that copies the resources to the bundle of the test target.

Parameters:

  • test_spec (Specification)

    The test spec to create the copy resources script for.


668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_installer.rb', line 668

def create_test_target_copy_resources_script(test_spec)
  path = target.copy_resources_script_path_for_spec(test_spec)
  host_target_spec_names = target.app_host_dependent_targets_for_spec(test_spec).flat_map do |pt|
    pt.specs.map(&:name)
  end.uniq
  resource_paths_by_config = target.user_build_configurations.each_with_object({}) do |(config_name, config), resources_by_config|
    resources_by_config[config_name] = target.dependent_targets_for_test_spec(test_spec, :configuration => config).flat_map do |pod_target|
      spec_paths_to_include = pod_target.library_specs.map(&:name)
      spec_paths_to_include -= host_target_spec_names
      spec_paths_to_include << test_spec.name if pod_target == target
      pod_target.resource_paths.values_at(*spec_paths_to_include).flatten.compact
    end
  end
  unless resource_paths_by_config.each_value.all?(&:empty?)
    generator = Generator::CopyResourcesScript.new(resource_paths_by_config, target.platform)
    update_changed_file(generator, path)
    add_file_to_support_group(path)
  end
end

#create_test_target_embed_frameworks_script(test_spec) ⇒ void (private)

This method returns an undefined value.

Creates a script that embeds the frameworks to the bundle of the test target.

Parameters:

  • test_spec (Specification)

    The test spec to create the embed frameworks script for.


695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_installer.rb', line 695

def create_test_target_embed_frameworks_script(test_spec)
  path = target.embed_frameworks_script_path_for_spec(test_spec)
  host_target_spec_names = target.app_host_dependent_targets_for_spec(test_spec).flat_map do |pt|
    pt.specs.map(&:name)
  end.uniq
  framework_paths_by_config = target.user_build_configurations.each_with_object({}) do |(config_name, config), paths_by_config|
    paths_by_config[config_name] = target.dependent_targets_for_test_spec(test_spec, :configuration => config).flat_map do |pod_target|
      spec_paths_to_include = pod_target.library_specs.map(&:name)
      spec_paths_to_include -= host_target_spec_names
      spec_paths_to_include << test_spec.name if pod_target == target
      pod_target.framework_paths.values_at(*spec_paths_to_include).flatten.compact.uniq
    end
  end
  xcframeworks_by_config = target.user_build_configurations.each_with_object({}) do |(config_name, config), paths_by_config|
    paths_by_config[config_name] = target.dependent_targets_for_test_spec(test_spec, :configuration => config).flat_map do |pod_target|
      spec_paths_to_include = pod_target.library_specs.map(&:name)
      spec_paths_to_include -= host_target_spec_names
      spec_paths_to_include << test_spec.name if pod_target == target
      pod_target.xcframeworks.values_at(*spec_paths_to_include).flatten.compact.uniq
    end
  end
  unless framework_paths_by_config.each_value.all?(&:empty?) && xcframeworks_by_config.each_value.all?(&:empty?)
    generator = Generator::EmbedFrameworksScript.new(framework_paths_by_config, xcframeworks_by_config)
    update_changed_file(generator, path)
    add_file_to_support_group(path)
  end
end

#create_test_xcconfig_files(test_native_targets, test_resource_bundle_targets) ⇒ void (private)

This method returns an undefined value.

Generates the contents of the xcconfig file used for each test target type and saves it to disk.

Parameters:

  • test_native_targets (Array<PBXNativeTarget>)

    the test native target to link the xcconfig file into.

  • test_resource_bundle_targets (Hash{String=>Array<PBXNativeTarget>})

    the additional test resource bundle targets to link the xcconfig file into.


638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_installer.rb', line 638

def create_test_xcconfig_files(test_native_targets, test_resource_bundle_targets)
  target.test_specs.each do |test_spec|
    spec_consumer = test_spec.consumer(target.platform)
    test_type = spec_consumer.test_type
    test_native_target = test_native_target_from_spec(spec_consumer.spec, test_native_targets)

    target.user_config_names_by_config_type.each do |config, names|
      path = target.xcconfig_path("#{test_type.capitalize}-#{target.subspec_label(test_spec)}.#{config}")
      test_spec_build_settings = target.build_settings_for_spec(test_spec, :configuration => config)
      update_changed_file(test_spec_build_settings, path)
      test_xcconfig_file_ref = add_file_to_support_group(path)

      # also apply the private config to resource bundle test targets related to this test spec.
      scoped_test_resource_bundle_targets = test_resource_bundle_targets[test_spec.name]
      apply_xcconfig_file_ref_to_targets([test_native_target] + scoped_test_resource_bundle_targets, test_xcconfig_file_ref, names)
    end

    test_native_target.build_configurations.each do |test_native_target_bc|
      test_target_swift_debug_hack(test_spec, test_native_target_bc)
    end
  end
end

#create_umbrella_header(native_target) ⇒ Object (private)


991
992
993
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_installer.rb', line 991

def create_umbrella_header(native_target)
  return super(native_target) unless custom_module_map
end

#create_xcconfig_file(native_target, resource_bundle_targets) ⇒ void (private)

This method returns an undefined value.

Generates the contents of the xcconfig file and saves it to disk.

Parameters:

  • native_target (PBXNativeTarget)

    the native target to link the xcconfig file into.

  • resource_bundle_targets (Array<PBXNativeTarget>)

    the additional resource bundle targets to link the xcconfig file into.


617
618
619
620
621
622
623
624
625
626
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_installer.rb', line 617

def create_xcconfig_file(native_target, resource_bundle_targets)
  target.user_config_names_by_config_type.each do |config, names|
    path = target.xcconfig_path(config)
    update_changed_file(target.build_settings[config], path)
    xcconfig_file_ref = add_file_to_support_group(path)

    # also apply the private config to resource bundle targets.
    apply_xcconfig_file_ref_to_targets([native_target] + resource_bundle_targets, xcconfig_file_ref, names)
  end
end

#custom_build_settingsHash{String => String} (private)

Remove the default headers folder path settings for static library pod targets.

Returns:

  • (Hash{String => String})

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
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_installer.rb', line 210

def custom_build_settings
  settings = super
  unless target.build_as_framework?
    settings['PRIVATE_HEADERS_FOLDER_PATH'] = ''
    settings['PUBLIC_HEADERS_FOLDER_PATH'] = ''
  end

  settings['PRODUCT_NAME'] = target.product_basename
  settings['PRODUCT_MODULE_NAME'] = target.product_module_name

  settings['CODE_SIGN_IDENTITY[sdk=appletvos*]'] = ''
  settings['CODE_SIGN_IDENTITY[sdk=iphoneos*]'] = ''
  settings['CODE_SIGN_IDENTITY[sdk=watchos*]'] = ''

  settings['SWIFT_ACTIVE_COMPILATION_CONDITIONS'] = '$(inherited) '

  if target.swift_version
    settings['SWIFT_VERSION'] = target.swift_version
  end

  if info_plist_bundle_id
    settings['PRODUCT_BUNDLE_IDENTIFIER'] = info_plist_bundle_id
  end

  settings
end

#custom_module_mapObject (private)


995
996
997
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_installer.rb', line 995

def custom_module_map
  @custom_module_map ||= target.file_accessors.first.module_map
end

#filter_resource_file_references(resource_file_references) {|compile_phase_refs, resources_phase_refs| ... } ⇒ Object (private)

Note:

Core Data model directories (.xcdatamodeld) used to be added to the Copy Resources build phase like all other resources, since they would compile correctly in either the resources or compile phase. In recent versions of xcode, there's an exception for data models that generate headers. These need to be added to the compile sources phase of a real target for the headers to be built in time for code in the target to use them. These kinds of models generally break when added to resource bundles.

Filters the given resource file references discarding empty paths which are added by their parent directory. This will also include references to the parent [PBXVariantGroup] for all resources underneath it.

Parameters:

  • resource_file_references (Array<Pathname>)

    The array of all resource file references to filter.

Yields:

  • (compile_phase_refs, resources_phase_refs)

276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_installer.rb', line 276

def filter_resource_file_references(resource_file_references)
  file_references = resource_file_references.map do |resource_file_reference|
    ref = project.reference_for_path(resource_file_reference)

    # Some nested files are not directly present in the Xcode project, such as the contents
    # of an .xcdatamodeld directory. These files are implicitly included by including their
    # parent directory.
    next if ref.nil?

    # For variant groups, the variant group itself is added, not its members.
    next ref.parent if ref.parent.is_a?(Xcodeproj::Project::Object::PBXVariantGroup)

    ref
  end.compact.uniq
  compile_phase_matcher = lambda { |ref| !(ref.path =~ /.*\.xcdatamodeld/i).nil? }
  compile_phase_refs, resources_phase_refs = file_references.partition(&compile_phase_matcher)
  yield compile_phase_refs, resources_phase_refs
end

#header_mappings_dir(file_accessor) ⇒ Object (private)


1021
1022
1023
1024
1025
1026
1027
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_installer.rb', line 1021

def header_mappings_dir(file_accessor)
  @header_mappings_dirs ||= {}
  return @header_mappings_dirs[file_accessor] if @header_mappings_dirs.key?(file_accessor)
  @header_mappings_dirs[file_accessor] = if dir = file_accessor.spec_consumer.header_mappings_dir
                                           file_accessor.path_list.root + dir
                                         end
end

#info_plist_bundle_idString (private)

Returns Bundle Identifier found in the custom Info.plist entries.

Returns:

  • (String)

    Bundle Identifier found in the custom Info.plist entries


239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_installer.rb', line 239

def info_plist_bundle_id
  return @plist_bundle_id if defined?(@plist_bundle_id)
  unless target.info_plist_entries.nil?
    @plist_bundle_id = target.info_plist_entries['CFBundleIdentifier']
    unless @plist_bundle_id.nil?
      message = "The `#{target.name}` target " \
    "sets a Bundle Identifier of `#{@plist_bundle_id}` in it's info.plist file. " \
    'The Bundle Identifier should be set using pod_target_xcconfig: ' \
    "s.pod_target_xcconfig = { 'PRODUCT_BUNDLE_IDENTIFIER': '#{@plist_bundle_id}' }`."
      UI.warn message
    end
    @plist_bundle_id
  end
end

#install!TargetInstallationResult

Creates the target in the Pods project and the relative support files.

Returns:


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
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_installer.rb', line 39

def install!
  UI.message "- Installing target `#{target.name}` #{target.platform}" do
    create_support_files_dir
    library_file_accessors = target.file_accessors.select { |fa| fa.spec.library_specification? }
    test_file_accessors = target.file_accessors.select { |fa| fa.spec.test_specification? }
    app_file_accessors = target.file_accessors.select { |fa| fa.spec.app_specification? }

    unless target.should_build?
      # For targets that should not be built (e.g. pre-built vendored frameworks etc), we add a placeholder
      # PBXAggregateTarget that will be used to wire up dependencies later.
      native_target = add_placeholder_target
      resource_bundle_targets = add_resources_bundle_targets(library_file_accessors).values.flatten
      create_copy_dsyms_script
      create_copy_xcframeworks_script unless target.xcframeworks.values.all?(&:empty?)
      create_xcconfig_file(native_target, resource_bundle_targets)
      return TargetInstallationResult.new(target, native_target, resource_bundle_targets)
    end

    native_target = add_target
    resource_bundle_targets = add_resources_bundle_targets(library_file_accessors).values.flatten

    test_native_targets = add_test_targets
    test_app_host_targets = add_test_app_host_targets
    test_resource_bundle_targets = add_resources_bundle_targets(test_file_accessors)

    app_native_targets = add_app_targets
    app_resource_bundle_targets = add_resources_bundle_targets(app_file_accessors)

    add_files_to_build_phases(native_target, test_native_targets, app_native_targets)
    validate_targets_contain_sources(test_native_targets + app_native_targets.values + [native_target])
    validate_xcframeworks

    create_copy_xcframeworks_script unless target.xcframeworks.values.all?(&:empty?)

    create_xcconfig_file(native_target, resource_bundle_targets)
    create_test_xcconfig_files(test_native_targets, test_resource_bundle_targets)
    create_app_xcconfig_files(app_native_targets, app_resource_bundle_targets)

    if target.defines_module?
      create_module_map(native_target) do |generator|
        generator.headers.concat module_map_additional_headers
      end
      create_umbrella_header(native_target) do |generator|
        generator.imports += library_file_accessors.flat_map do |file_accessor|
          header_dir = if !target.build_as_framework? && dir = file_accessor.spec_consumer.header_dir
                         Pathname.new(dir)
                       end

          file_accessor.public_headers.map do |public_header|
            public_header = if header_mappings_dir(file_accessor)
                              public_header.relative_path_from(header_mappings_dir(file_accessor))
                            else
                              public_header.basename
                            end
            if header_dir
              public_header = header_dir.join(public_header)
            end
            public_header
          end
        end
      end
    end

    if target.build_as_framework?
      unless skip_info_plist?(native_target)
        create_info_plist_file(target.info_plist_path, native_target, target.version, target.platform, :additional_entries => target.info_plist_entries)
      end
      create_build_phase_to_symlink_header_folders(native_target)
    end

    if target.build_as_library? && target.uses_swift?
      add_swift_library_compatibility_header_phase(native_target)
    end

    project_directory = project.path.dirname

    unless skip_pch?(target.library_specs)
      path = target.prefix_header_path
      create_prefix_header(path, library_file_accessors, target.platform, native_target, project_directory)
      add_file_to_support_group(path)
    end
    unless skip_pch?(target.test_specs)
      target.test_specs.each do |test_spec|
        path = target.prefix_header_path_for_spec(test_spec)
        test_spec_consumer = test_spec.consumer(target.platform)
        test_native_target = test_native_target_from_spec(test_spec_consumer.spec, test_native_targets)
        create_prefix_header(path, test_file_accessors, target.platform, test_native_target, project_directory)
        add_file_to_support_group(path)
      end
    end
    unless skip_pch?(target.app_specs)
      target.app_specs.each do |app_spec|
        path = target.prefix_header_path_for_spec(app_spec)
        app_spec_consumer = app_spec.consumer(target.platform)
        app_native_target = app_native_targets[app_spec_consumer.spec]
        create_prefix_header(path, app_file_accessors, target.platform, app_native_target, project_directory)
        add_file_to_support_group(path)
      end
    end
    create_dummy_source(native_target)
    create_copy_dsyms_script
    clean_support_files_temp_dir
    TargetInstallationResult.new(target, native_target, resource_bundle_targets,
                                 test_native_targets, test_resource_bundle_targets, test_app_host_targets,
                                 app_native_targets, app_resource_bundle_targets)
  end
end

#module_map_additional_headersObject (private)


980
981
982
983
984
985
986
987
988
989
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_installer.rb', line 980

def module_map_additional_headers
  return [] unless umbrella_header_paths

  other_paths = umbrella_header_paths - [target.umbrella_header_path]
  other_paths.map do |module_map_path|
    # exclude other targets umbrella headers, to avoid
    # incomplete umbrella warnings
    Generator::ModuleMap::Header.new(module_map_path.basename, nil, nil, nil, true)
  end
end

#project_file_references_array(files, file_type) ⇒ Object (private)


999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_installer.rb', line 999

def project_file_references_array(files, file_type)
  error_message_for_missing_reference = lambda do |sf, target|
    "Unable to find #{file_type} ref for `#{sf.basename}` for target `#{target.name}`."
  end
  files.map do |sf|
    begin
      project.reference_for_path(sf).tap do |ref|
        raise Informative, error_message_for_missing_reference.call(sf, target) unless ref
      end
    rescue Errno::ENOENT
      # Normalize the error for Ruby < 2.7. Ruby 2.7 can crash on a different call of real path compared
      # to older versions. This ensures that the error message is consistent.
      raise Informative, error_message_for_missing_reference.call(sf, target)
    end
  end
end

#remove_pod_target_xcconfig_overrides_from_target(build_settings_by_config, native_target) ⇒ Void (private)

Removes overrides of the pod_target_xcconfig settings from the target's build configurations.

Parameters:

  • build_settings_by_config (Hash{Symbol => Pod::Target::BuildSettings})

    the build settings by config of the target.

  • native_target (PBXNativeTarget)

    the native target to remove pod target xcconfig overrides from.

Returns:

  • (Void)

174
175
176
177
178
179
180
181
182
183
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_installer.rb', line 174

def remove_pod_target_xcconfig_overrides_from_target(build_settings_by_config, native_target)
  native_target.build_configurations.each do |configuration|
    build_settings = build_settings_by_config[target.user_build_configurations[configuration.name]]
    unless build_settings.nil?
      build_settings.merged_pod_target_xcconfigs.each_key do |setting|
        configuration.build_settings.delete(setting)
      end
    end
  end
end

#skip_info_plist?(native_target) ⇒ Boolean (private)

True if info.plist generation should be skipped

Parameters:

  • native_target (PXNativeTarget)

Returns:

  • (Boolean)

    Whether the target should build an Info.plist file


200
201
202
203
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_installer.rb', line 200

def skip_info_plist?(native_target)
  existing_setting = native_target.resolved_build_setting('INFOPLIST_FILE', true).values.compact
  !existing_setting.empty?
end

#skip_pch?(specs) ⇒ Boolean (private)

Returns Whether the target should build a pch file.

Parameters:

  • specs (Array<Specification>)

    the specs to check against whether .pch generation should be skipped or not.

Returns:

  • (Boolean)

    Whether the target should build a pch file.


190
191
192
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_installer.rb', line 190

def skip_pch?(specs)
  specs.any? { |spec| spec.root.prefix_header_file.is_a?(FalseClass) }
end

#support_files_groupObject (private)


1064
1065
1066
1067
1068
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_installer.rb', line 1064

def support_files_group
  pod_name = target.pod_name
  dir = target.support_files_dir
  project.pod_support_files_group(pod_name, dir)
end

#test_native_target_from_spec(spec, test_native_targets) ⇒ Object (private)


1070
1071
1072
1073
1074
1075
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_installer.rb', line 1070

def test_native_target_from_spec(spec, test_native_targets)
  test_target_label = target.test_target_label(spec)
  test_native_targets.find do |test_native_target|
    test_native_target.name == test_target_label
  end
end

#test_target_swift_debug_hack(test_spec, test_target_bc) ⇒ void (private)

This method returns an undefined value.

Manually add libswiftSwiftOnoneSupport.dylib as it seems there is an issue with tests that do not include it for Debug configurations. Possibly related to Swift module optimization.


841
842
843
844
845
846
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_installer.rb', line 841

def test_target_swift_debug_hack(test_spec, test_target_bc)
  return unless test_target_bc.debug?
  return unless target.dependent_targets_for_test_spec(test_spec).any?(&:uses_swift?)
  ldflags = test_target_bc.build_settings['OTHER_LDFLAGS'] ||= '$(inherited)'
  ldflags << ' -lswiftSwiftOnoneSupport'
end

#validate_targets_contain_sources(native_targets) ⇒ Object (private)


1138
1139
1140
1141
1142
1143
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_installer.rb', line 1138

def validate_targets_contain_sources(native_targets)
  native_targets.each do |native_target|
    next unless native_target.source_build_phase.files.empty?
    raise Informative, "Unable to install the `#{target.label}` pod, because the `#{native_target}` target in Xcode would have no sources to compile."
  end
end

#validate_xcframeworksObject (private)

Raises if a vendored xcframework contains frameworks of mixed linkage or mixed packaging


1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
# File 'lib/cocoapods/installer/xcode/pods_project_generator/pod_target_installer.rb', line 1147

def validate_xcframeworks
  target.xcframeworks.each_value do |xcframeworks|
    xcframeworks.each do |xcframework|
      if xcframework.slices.empty?
        raise Informative, "Unable to install vendored xcframework `#{xcframework.name}` for Pod `#{target.label}` because it does not contain any binaries."
      end
      if xcframework.build_type.dynamic_library?
        raise Informative, <<-MSG.strip_heredoc
          Unable to install vendored xcframework `#{xcframework.name}` for Pod `#{target.label}` because it contains dynamic libraries which are not supported.
          Use dynamic frameworks for dynamic linking instead.
        MSG
      end
      if xcframework.build_type.static_library?
        binary_names = xcframework.slices.map { |slice| File.basename(slice.binary_path, File.extname(slice.binary_path)) }.uniq
        if binary_names.size > 1
          raise Informative, <<-MSG.strip_heredoc
            Unable to install vendored xcframework `#{xcframework.name}` for Pod `#{target.label}` because it contains static libraries
            with differing binary names: #{binary_names.to_sentence}.
          MSG
        end
      end
      dynamic_slices, static_slices = xcframework.slices.partition(&:dynamic?)
      if !dynamic_slices.empty? && !static_slices.empty?
        raise Informative, "Unable to install vendored xcframework `#{xcframework.name}` for Pod `#{target.label}`, because it contains both static and dynamic frameworks."
      end
      library_slices, framework_slices = xcframework.slices.partition(&:library?)
      if !library_slices.empty? && !framework_slices.empty?
        raise Informative, "Unable to install vendored xcframework `#{xcframework.name}` for Pod `#{target.label}`, because it contains both libraries and frameworks."
      end
    end
  end
end