class Xcodeproj::Project::Object::AbstractTarget

Constants

DEPLOYMENT_TARGET_SETTING_BY_PLATFORM_NAME

@visibility private

@return [Hash<Symbol, String>]

The name of the setting for the deployment target by platform
name.

Public Instance Methods

add_build_configuration(name, type) click to toggle source

Adds a new build configuration to the target and populates its with default settings according to the provided type if one doesn’t exists.

@note If a build configuration with the given name is already

present no new build configuration is added.

@param [String] name

The name of the build configuration.

@param [Symbol] type

The type of the build configuration used to populate the build
settings, must be :debug or :release.

@return [XCBuildConfiguration] the created build configuration or the

existing one with the same name.
# File lib/xcodeproj/project/object/native_target.rb, line 185
def add_build_configuration(name, type)
  if existing = build_configuration_list[name]
    existing
  else
    build_configuration = project.new(XCBuildConfiguration)
    build_configuration.name = name
    product_type = self.product_type if respond_to?(:product_type)
    build_configuration.build_settings = ProjectHelper.common_build_settings(type, platform_name, deployment_target, product_type)
    build_configuration_list.build_configurations << build_configuration
    build_configuration
  end
end
add_dependency(target) click to toggle source

Adds a dependency on the given target.

@param [AbstractTarget] target

the target which should be added to the dependencies list of
the receiver. The target may be a target of this target's
project or of a subproject of this project. Note that the
subproject must already be added to this target's project.

@return [void]

# File lib/xcodeproj/project/object/native_target.rb, line 242
def add_dependency(target)
  unless dependency_for_target(target)
    container_proxy = project.new(Xcodeproj::Project::PBXContainerItemProxy)
    if target.project == project
      container_proxy.container_portal = project.root_object.uuid
    else
      subproject_reference = project.reference_for_path(target.project.path)
      raise ArgumentError, 'add_dependency received target that belongs to a project that is not this project and is not a subproject of this project' unless subproject_reference
      container_proxy.container_portal = subproject_reference.uuid
    end
    container_proxy.proxy_type = Constants::PROXY_TYPES[:native_target]
    container_proxy.remote_global_id_string = target.uuid
    container_proxy.remote_info = target.name

    dependency = project.new(Xcodeproj::Project::PBXTargetDependency)
    dependency.name = target.name
    dependency.target = target if target.project == project
    dependency.target_proxy = container_proxy

    dependencies << dependency
  end
end
add_system_framework(names) click to toggle source

Adds a file reference for one or more system framework to the project if needed and adds them to the Frameworks build phases.

@param [Array<String>, String] names

The name or the list of the names of the framework.

@note Xcode behaviour is following: if the target has the same SDK

of the project it adds the reference relative to the SDK root
otherwise the reference is added relative to the Developer
directory. This can create confusion or duplication of the
references of frameworks linked by iOS and OS X targets. For
this reason the new Xcodeproj behaviour is to add the
frameworks in a subgroup according to the platform.

@return [Array<PBXFileReference>] An array of the newly created file

references.
# File lib/xcodeproj/project/object/native_target.rb, line 333
def add_system_framework(names)
  Array(names).map do |name|
    case platform_name
    when :ios
      group = project.frameworks_group['iOS'] || project.frameworks_group.new_group('iOS')
      path_sdk_name = 'iPhoneOS'
      path_sdk_version = sdk_version || Constants::LAST_KNOWN_IOS_SDK
    when :osx
      group = project.frameworks_group['OS X'] || project.frameworks_group.new_group('OS X')
      path_sdk_name = 'MacOSX'
      path_sdk_version = sdk_version || Constants::LAST_KNOWN_OSX_SDK
    when :tvos
      group = project.frameworks_group['tvOS'] || project.frameworks_group.new_group('tvOS')
      path_sdk_name = 'AppleTVOS'
      path_sdk_version = sdk_version || Constants::LAST_KNOWN_TVOS_SDK
    when :visionos
      group = project.frameworks_group['visionOS'] || project.frameworks_group.new_group('visionOS')
      path_sdk_name = 'XROS'
      path_sdk_version = sdk_version || Constants::LAST_KNOWN_VISIONOS_SDK
    when :watchos
      group = project.frameworks_group['watchOS'] || project.frameworks_group.new_group('watchOS')
      path_sdk_name = 'WatchOS'
      path_sdk_version = sdk_version || Constants::LAST_KNOWN_WATCHOS_SDK
    else
      raise 'Unknown platform for target'
    end

    path = "Platforms/#{path_sdk_name}.platform/Developer/SDKs/#{path_sdk_name}#{path_sdk_version}.sdk/System/Library/Frameworks/#{name}.framework"
    unless ref = group.find_file_by_path(path)
      ref = group.new_file(path, :developer_dir)
    end
    frameworks_build_phase.add_file_reference(ref, true)
    ref
  end
end
Also aliased as: add_system_frameworks
add_system_frameworks(names)
add_system_libraries(names)
Alias for: add_system_library
add_system_libraries_tbd(names)
add_system_library(names) click to toggle source

Adds a file reference for one or more system dylib libraries to the project if needed and adds them to the Frameworks build phases.

@param [Array<String>, String] names

The name or the list of the names of the libraries.

@return [void]

# File lib/xcodeproj/project/object/native_target.rb, line 378
def add_system_library(names)
  add_system_library_extension(names, 'dylib')
end
Also aliased as: add_system_libraries
add_system_library_tbd(names) click to toggle source

Adds a file reference for one or more system tbd libraries to the project if needed and adds them to the Frameworks build phases.

@param [Array<String>, String] names

The name or the list of the names of the libraries.

@return [void]

# File lib/xcodeproj/project/object/native_target.rb, line 404
def add_system_library_tbd(names)
  add_system_library_extension(names, 'tbd')
end
Also aliased as: add_system_libraries_tbd
build_configurations() click to toggle source

@return [ObjectList<XCBuildConfiguration>] the build

configurations of the target.
# File lib/xcodeproj/project/object/native_target.rb, line 164
def build_configurations
  build_configuration_list.build_configurations
end
build_settings(build_configuration_name) click to toggle source

@param [String] build_configuration_name

the name of a build configuration.

@return [Hash] the build settings of the build configuration with the

given name.
# File lib/xcodeproj/project/object/native_target.rb, line 205
def build_settings(build_configuration_name)
  build_configuration_list.build_settings(build_configuration_name)
end
common_resolved_build_setting(key, resolve_against_xcconfig: false) click to toggle source

Gets the value for the given build setting, properly inherited if need, if shared across the build configurations.

@param [String] key

the key of the build setting.

@param [Boolean] resolve_against_xcconfig

whether the resolved setting should take in consideration any
configuration file present.

@raise If the build setting has multiple values.

@note As it is common not to have a setting with no value for

custom build configurations nil keys are not considered to
determine if the setting is unique. This is an heuristic
which might not closely match Xcode behaviour.

@return [String] The value of the build setting.

# File lib/xcodeproj/project/object/native_target.rb, line 90
def common_resolved_build_setting(key, resolve_against_xcconfig: false)
  values = resolved_build_setting(key, resolve_against_xcconfig).values.compact.uniq
  if values.count <= 1
    values.first
  else
    raise "[Xcodeproj] Consistency issue: build setting `#{key}` has multiple values: `#{resolved_build_setting(key)}`"
  end
end
copy_files_build_phases() click to toggle source

@return [Array<PBXCopyFilesBuildPhase>]

the copy files build phases of the target.
# File lib/xcodeproj/project/object/native_target.rb, line 221
def copy_files_build_phases
  build_phases.grep(PBXCopyFilesBuildPhase)
end
dependency_for_target(target) click to toggle source

Checks whether this target has a dependency on the given target.

@param [AbstractTarget] target

the target to search for.

@return [PBXTargetDependency]

# File lib/xcodeproj/project/object/native_target.rb, line 272
def dependency_for_target(target)
  dependencies.find do |dep|
    if dep.target_proxy.remote?
      subproject_reference = project.reference_for_path(target.project.path)
      uuid = subproject_reference.uuid if subproject_reference
      dep.target_proxy.remote_global_id_string == target.uuid && dep.target_proxy.container_portal == uuid
    else
      dep.target.uuid == target.uuid
    end
  end
end
deployment_target() click to toggle source

@return [String] the deployment target of the target according to its

platform.
# File lib/xcodeproj/project/object/native_target.rb, line 146
def deployment_target
  return unless setting = DEPLOYMENT_TARGET_SETTING_BY_PLATFORM_NAME[platform_name]
  common_resolved_build_setting(setting)
end
deployment_target=(deployment_target) click to toggle source

@param [String] deployment_target the deployment target to set for

the target according to its platform.
# File lib/xcodeproj/project/object/native_target.rb, line 154
def deployment_target=(deployment_target)
  return unless setting = DEPLOYMENT_TARGET_SETTING_BY_PLATFORM_NAME[platform_name]
  build_configurations.each do |config|
    config.build_settings[setting] = deployment_target
  end
end
frameworks_build_phases() click to toggle source

@return [PBXFrameworksBuildPhase]

the frameworks build phases of the target.
# File lib/xcodeproj/project/object/native_target.rb, line 214
def frameworks_build_phases
  build_phases.find { |bp| bp.class == PBXFrameworksBuildPhase }
end
new_copy_files_build_phase(name = nil) click to toggle source

Creates a new copy files build phase.

@param [String] name

an optional name for the phase.

@return [PBXCopyFilesBuildPhase] the new phase.

# File lib/xcodeproj/project/object/native_target.rb, line 291
def new_copy_files_build_phase(name = nil)
  phase = project.new(PBXCopyFilesBuildPhase)
  phase.name = name
  build_phases << phase
  phase
end
new_shell_script_build_phase(name = nil) click to toggle source

Creates a new shell script build phase.

@param (see new_copy_files_build_phase)

@return [PBXShellScriptBuildPhase] the new phase.

# File lib/xcodeproj/project/object/native_target.rb, line 304
def new_shell_script_build_phase(name = nil)
  phase = project.new(PBXShellScriptBuildPhase)
  phase.name = name
  build_phases << phase
  phase
end
platform_name() click to toggle source

@return [Symbol] the name of the platform of the target.

# File lib/xcodeproj/project/object/native_target.rb, line 107
def platform_name
  return unless sdk
  if sdk.include? 'iphoneos'
    :ios
  elsif sdk.include? 'macosx'
    :osx
  elsif sdk.include? 'appletvos'
    :tvos
  elsif sdk.include? 'xros'
    :visionos
  elsif sdk.include? 'watchos'
    :watchos
  end
end
pretty_print() click to toggle source

@return [Hash{String => Hash}] A hash suitable to display the object

to the user.
# File lib/xcodeproj/project/object/native_target.rb, line 417
def pretty_print
  {
    display_name => {
      'Build Phases' => build_phases.map(&:pretty_print),
      'Build Configurations' => build_configurations.map(&:pretty_print),
    },
  }
end
resolved_build_setting(key, resolve_against_xcconfig = false) click to toggle source

Gets the value for the given build setting in all the build configurations or the value inheriting the value from the project ones if needed.

@param [String] key

the key of the build setting.

@param [Bool] resolve_against_xcconfig

whether the resolved setting should take in consideration any
configuration file present.

@return [Hash{String => String}] The value of the build setting

grouped by the name of the build configuration.

TODO: Full support for this would require to take into account

the default values for the platform.
# File lib/xcodeproj/project/object/native_target.rb, line 54
def resolved_build_setting(key, resolve_against_xcconfig = false)
  target_settings = build_configuration_list.get_setting(key, resolve_against_xcconfig, self)
  project_settings = project.build_configuration_list.get_setting(key, resolve_against_xcconfig)
  target_settings.merge(project_settings) do |_key, target_val, proj_val|
    target_includes_inherited = Constants::INHERITED_KEYWORDS.any? { |keyword| target_val.include?(keyword) } if target_val
    if target_includes_inherited && proj_val
      if target_val.is_a? String
        target_val.gsub(Regexp.union(Constants::INHERITED_KEYWORDS), proj_val)
      else
        target_val.flat_map { |value| Constants::INHERITED_KEYWORDS.include?(value) ? proj_val : value }
      end
    else
      target_val || proj_val
    end
  end
end
sdk() click to toggle source

@return [String] the SDK that the target should use.

# File lib/xcodeproj/project/object/native_target.rb, line 101
def sdk
  common_resolved_build_setting('SDKROOT')
end
sdk_version() click to toggle source

@return [String] the version of the SDK.

# File lib/xcodeproj/project/object/native_target.rb, line 124
def sdk_version
  return unless sdk
  sdk.scan(/[0-9.]+/).first
end
shell_script_build_phases() click to toggle source

@return [Array<PBXShellScriptBuildPhase>]

the shell script build phases of the target.
# File lib/xcodeproj/project/object/native_target.rb, line 228
def shell_script_build_phases
  build_phases.grep(PBXShellScriptBuildPhase)
end

Private Instance Methods

add_system_library_extension(names, extension) click to toggle source
# File lib/xcodeproj/project/object/native_target.rb, line 383
def add_system_library_extension(names, extension)
  Array(names).each do |name|
    path = "usr/lib/lib#{name}.#{extension}"
    files = project.frameworks_group.files
    unless reference = files.find { |ref| ref.path == path }
      reference = project.frameworks_group.new_file(path, :sdk_root)
    end
    frameworks_build_phase.add_file_reference(reference, true)
    reference
  end
end