Class: Xcodeproj::Project::Object::AbstractTarget

Inherits:
AbstractObject show all
Defined in:
lib/xcodeproj/project/object/native_target.rb

Attributes collapse

Attributes inherited from AbstractObject

#isa, #project, #uuid

Attributes collapse

Helpers collapse

Build Phases Helpers collapse

System frameworks collapse

AbstractObject Hooks collapse

Methods inherited from AbstractObject

#<=>, #==, #ascii_plist_annotation, #display_name, #inspect, isa, #nested_object_for_hash, #remove_from_project, #sort, #sort_recursively, #to_ascii_plist, #to_hash

Instance Attribute Details

#build_configuration_listXCConfigurationList

Returns the list of the build configurations of the target. This list commonly include two configurations Debug and Release.

Returns:

  • (XCConfigurationList)

    the list of the build configurations of the target. This list commonly include two configurations Debug and Release.



25
# File 'lib/xcodeproj/project/object/native_target.rb', line 25

has_one :build_configuration_list, XCConfigurationList

#commentsString

Returns Comments associated with this target.

This is apparently no longer used by Xcode.

Returns:

  • (String)

    Comments associated with this target.

    This is apparently no longer used by Xcode.



19
# File 'lib/xcodeproj/project/object/native_target.rb', line 19

attribute :comments, String

#nameString

Returns The name of the Target.

Returns:

  • (String)

    The name of the Target.



9
# File 'lib/xcodeproj/project/object/native_target.rb', line 9

attribute :name, String

#product_nameString

Returns the name of the build product.

Returns:

  • (String)

    the name of the build product.



13
# File 'lib/xcodeproj/project/object/native_target.rb', line 13

attribute :product_name, String

Instance Method Details

#add_build_configuration(name, type) ⇒ XCBuildConfiguration

Note:

If a build configuration with the given name is already present no new build configuration is added.

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

Parameters:

  • name (String)

    The name of the build configuration.

  • type (Symbol)

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

Returns:

  • (XCBuildConfiguration)

    the created build configuration or the existing one with the same name.



185
186
187
188
189
190
191
192
193
194
195
196
# 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) ⇒ void

This method returns an undefined value.

Adds a dependency on the given target.

Parameters:

  • target (AbstractTarget)

    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.



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# 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) ⇒ Array<PBXFileReference> Also known as: add_system_frameworks

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.

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

Parameters:

  • names (Array<String>, String)

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

Returns:



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
# 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

#add_system_library(names) ⇒ void Also known as: add_system_libraries

This method returns an undefined value.

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

Parameters:

  • names (Array<String>, String)

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



378
379
380
# File 'lib/xcodeproj/project/object/native_target.rb', line 378

def add_system_library(names)
  add_system_library_extension(names, 'dylib')
end

#add_system_library_tbd(names) ⇒ void Also known as: add_system_libraries_tbd

This method returns an undefined value.

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

Parameters:

  • names (Array<String>, String)

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



404
405
406
# File 'lib/xcodeproj/project/object/native_target.rb', line 404

def add_system_library_tbd(names)
  add_system_library_extension(names, 'tbd')
end

#build_configurationsObjectList<XCBuildConfiguration>

Returns the build configurations of the target.

Returns:



164
165
166
# File 'lib/xcodeproj/project/object/native_target.rb', line 164

def build_configurations
  build_configuration_list.build_configurations
end

#build_settings(build_configuration_name) ⇒ Hash

Returns the build settings of the build configuration with the given name.

Parameters:

  • build_configuration_name (String)

    the name of a build configuration.

Returns:

  • (Hash)

    the build settings of the build configuration with the given name.



205
206
207
# 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) ⇒ String

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.

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

Parameters:

  • key (String)

    the key of the build setting.

  • resolve_against_xcconfig (Boolean) (defaults to: false)

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

Returns:

  • (String)

    The value of the build setting.

Raises:

  • If the build setting has multiple values.



90
91
92
93
94
95
96
97
# 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_phasesArray<PBXCopyFilesBuildPhase>

Returns the copy files build phases of the target.

Returns:



221
222
223
# File 'lib/xcodeproj/project/object/native_target.rb', line 221

def copy_files_build_phases
  build_phases.grep(PBXCopyFilesBuildPhase)
end

#dependenciesObjectList<PBXTargetDependency>

Returns the targets necessary to build this target.

Returns:



30
# File 'lib/xcodeproj/project/object/native_target.rb', line 30

has_many :dependencies, PBXTargetDependency

#dependency_for_target(target) ⇒ PBXTargetDependency

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

Parameters:

Returns:



272
273
274
275
276
277
278
279
280
281
282
# 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_targetString

Returns the deployment target of the target according to its platform.

Returns:

  • (String)

    the deployment target of the target according to its platform.



146
147
148
149
# 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) ⇒ Object

Parameters:

  • deployment_target (String)

    the deployment target to set for the target according to its platform.



154
155
156
157
158
159
# 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_phasesPBXFrameworksBuildPhase

Returns the frameworks build phases of the target.

Returns:



214
215
216
# 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) ⇒ PBXCopyFilesBuildPhase

Creates a new copy files build phase.

Parameters:

  • name (String) (defaults to: nil)

    an optional name for the phase.

Returns:



291
292
293
294
295
296
# 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) ⇒ PBXShellScriptBuildPhase

Creates a new shell script build phase.

Parameters:

  • name (String) (defaults to: nil)

    an optional name for the phase.

Returns:



304
305
306
307
308
309
# 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_nameSymbol

Returns the name of the platform of the target.

Returns:

  • (Symbol)

    the name of the platform of the target.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# 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_printHash{String => Hash}

Returns A hash suitable to display the object to the user.

Returns:

  • (Hash{String => Hash})

    A hash suitable to display the object to the user.



417
418
419
420
421
422
423
424
# 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) ⇒ Hash{String => String}

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.

TODO: Full support for this would require to take into account the default values for the platform.

Parameters:

  • key (String)

    the key of the build setting.

  • resolve_against_xcconfig (Bool) (defaults to: false)

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

Returns:

  • (Hash{String => String})

    The value of the build setting grouped by the name of the build configuration.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 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

#sdkString

Returns the SDK that the target should use.

Returns:

  • (String)

    the SDK that the target should use.



101
102
103
# File 'lib/xcodeproj/project/object/native_target.rb', line 101

def sdk
  common_resolved_build_setting('SDKROOT')
end

#sdk_versionString

Returns the version of the SDK.

Returns:

  • (String)

    the version of the SDK.



124
125
126
127
# 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_phasesArray<PBXShellScriptBuildPhase>

Returns the shell script build phases of the target.

Returns:



228
229
230
# File 'lib/xcodeproj/project/object/native_target.rb', line 228

def shell_script_build_phases
  build_phases.grep(PBXShellScriptBuildPhase)
end