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

#<=>, #==, #display_name, #inspect, isa, #remove_from_project, #sort, #sort_recursively, #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.



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/xcodeproj/project/object/native_target.rb', line 147

def add_build_configuration(name, type)
  if existing = build_configuration_list[name]
    existing
  else
    build_configuration = project.new(XCBuildConfiguration)
    build_configuration.name = name
    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.



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/xcodeproj/project/object/native_target.rb', line 203

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 got target that belongs to a project 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) ⇒ void 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.

This method returns an undefined value.

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

Parameters:

  • name (Array<String>, String)

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



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/xcodeproj/project/object/native_target.rb', line 291

def add_system_framework(names)
  Array(names).each 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 :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 libraries to the project if needed and adds them to the Frameworks build phases.

Parameters:

  • name (Array<String>, String)

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



332
333
334
335
336
337
338
339
340
341
342
# File 'lib/xcodeproj/project/object/native_target.rb', line 332

def add_system_library(names)
  Array(names).each do |name|
    path = "usr/lib/lib#{name}.dylib"
    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

#build_configurationsObjectList<XCBuildConfiguration>

Returns the build configurations of the target.

Returns:



126
127
128
# File 'lib/xcodeproj/project/object/native_target.rb', line 126

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.



166
167
168
# File 'lib/xcodeproj/project/object/native_target.rb', line 166

def build_settings(build_configuration_name)
  build_configuration_list.build_settings(build_configuration_name)
end

#common_resolved_build_setting(key) ⇒ 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.

Returns:

  • (String)

    The value of the build setting.

Raises:

  • If the build setting has multiple values.



74
75
76
77
78
79
80
81
# File 'lib/xcodeproj/project/object/native_target.rb', line 74

def common_resolved_build_setting(key)
  values = resolved_build_setting(key).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:



182
183
184
# File 'lib/xcodeproj/project/object/native_target.rb', line 182

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:



233
234
235
236
237
238
239
240
241
# File 'lib/xcodeproj/project/object/native_target.rb', line 233

def dependency_for_target(target)
  dependencies.find do |dep|
    if dep.target_proxy.remote?
      dep.target_proxy.remote_global_id_string == target.uuid
    else
      dep.target == target
    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.



114
115
116
117
118
119
120
121
# File 'lib/xcodeproj/project/object/native_target.rb', line 114

def deployment_target
  case platform_name
  when :ios then common_resolved_build_setting('IPHONEOS_DEPLOYMENT_TARGET')
  when :osx then common_resolved_build_setting('MACOSX_DEPLOYMENT_TARGET')
  when :tvos then common_resolved_build_setting('TVOS_DEPLOYMENT_TARGET')
  when :watchos then common_resolved_build_setting('WATCHOS_DEPLOYMENT_TARGET')
  end
end

#frameworks_build_phasesPBXFrameworksBuildPhase

Returns the copy files build phases of the target.

Returns:



175
176
177
# File 'lib/xcodeproj/project/object/native_target.rb', line 175

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:



250
251
252
253
254
255
# File 'lib/xcodeproj/project/object/native_target.rb', line 250

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:



263
264
265
266
267
268
# File 'lib/xcodeproj/project/object/native_target.rb', line 263

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.



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/xcodeproj/project/object/native_target.rb', line 91

def platform_name
  return unless sdk
  if sdk.include? 'iphoneos'
    :ios
  elsif sdk.include? 'macosx'
    :osx
  elsif sdk.include? 'appletvos'
    :tvos
  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.



353
354
355
356
357
358
359
360
# File 'lib/xcodeproj/project/object/native_target.rb', line 353

def pretty_print
  {
    display_name => {
      'Build Phases' => build_phases.map(&:pretty_print),
      'Build Configurations' => build_configurations.map(&:pretty_print),
    },
  }
end

#resolved_build_setting(key) ⇒ 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

any associated xcconfig and the default values for the
platform.

Parameters:

  • key (String)

    the key of the build setting.

Returns:

  • (Hash{String => String})

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



51
52
53
54
55
56
57
# File 'lib/xcodeproj/project/object/native_target.rb', line 51

def resolved_build_setting(key)
  target_settings = build_configuration_list.get_setting(key)
  project_settings = project.build_configuration_list.get_setting(key)
  target_settings.merge(project_settings) do |_key, target_val, proj_val|
    target_val || proj_val
  end
end

#sdkString

Returns the SDK that the target should use.

Returns:

  • (String)

    the SDK that the target should use.



85
86
87
# File 'lib/xcodeproj/project/object/native_target.rb', line 85

def sdk
  common_resolved_build_setting('SDKROOT')
end

#sdk_versionString

Returns the version of the SDK.

Returns:

  • (String)

    the version of the SDK.



106
107
108
109
# File 'lib/xcodeproj/project/object/native_target.rb', line 106

def sdk_version
  return unless sdk
  sdk.scan(/[0-9.]+/).first
end

#shell_script_build_phasesArray<PBXShellScriptBuildPhase>

Returns the copy files build phases of the target.

Returns:



189
190
191
# File 'lib/xcodeproj/project/object/native_target.rb', line 189

def shell_script_build_phases
  build_phases.grep(PBXShellScriptBuildPhase)
end