Class: CocoapodsXCRemoteCacheModifier::Hooks::XCRemoteCache

Inherits:
Object
  • Object
show all
Defined in:
lib/cocoapods-xcremotecache/command/hooks.rb

Constant Summary collapse

@@configuration =
nil

Class Method Summary collapse

Class Method Details

.add_build_setting(build_settings, key, value, exclude_sdks_configurations) ⇒ Object



325
326
327
328
329
330
# File 'lib/cocoapods-xcremotecache/command/hooks.rb', line 325

def self.add_build_setting(build_settings, key, value, exclude_sdks_configurations)
  build_settings[key] = value
  for exclude_sdks_configuration in exclude_sdks_configurations
    build_settings["#{key}[sdk=#{exclude_sdks_configuration}]"] = [""]
  end
end

.add_build_setting_for_sdks(build_settings, key, value, sdk_configurations) ⇒ Object

Sets value for a key only for a subset of sdk configurations



347
348
349
350
351
# File 'lib/cocoapods-xcremotecache/command/hooks.rb', line 347

def self.add_build_setting_for_sdks(build_settings, key, value, sdk_configurations)
  for sdk_configuration in sdk_configurations
    build_settings["#{key}[sdk=#{sdk_configuration}]"] = value
  end
end

.add_cflags!(options, key, value, exclude_sdks_configurations) ⇒ Object



305
306
307
# File 'lib/cocoapods-xcremotecache/command/hooks.rb', line 305

def self.add_cflags!(options, key, value, exclude_sdks_configurations)
  reset_build_setting(options, 'OTHER_CFLAGS', remove_cflags!(options, key) << "#{key}=#{value}", exclude_sdks_configurations)
end

.add_lldbinit_rewrite(lines_content, user_proj_directory, fake_src_root) ⇒ Object

Append source rewrite command to the lldbinit content



398
399
400
401
402
403
404
# File 'lib/cocoapods-xcremotecache/command/hooks.rb', line 398

def self.add_lldbinit_rewrite(lines_content, user_proj_directory,fake_src_root)
  all_lines = lines_content.clone
  all_lines << LLDB_INIT_COMMENT
  all_lines << "settings set target.source-map #{fake_src_root} #{user_proj_directory}"
  all_lines << ""
  all_lines
end

.add_swiftflags!(options, key, value, exclude_sdks_configurations) ⇒ Object



316
317
318
# File 'lib/cocoapods-xcremotecache/command/hooks.rb', line 316

def self.add_swiftflags!(options, key, value, exclude_sdks_configurations)
  reset_build_setting(options, 'OTHER_SWIFT_FLAGS', remove_swiftflags!(options, key) + " #{key} #{value}", exclude_sdks_configurations)
end

.clean_lldbinit_content(lldbinit_path) ⇒ Object

Returns the content (array of lines) of the lldbinit with stripped XCRemoteCache rewrite



380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/cocoapods-xcremotecache/command/hooks.rb', line 380

def self.clean_lldbinit_content(lldbinit_path)
  all_lines = []
  return all_lines unless File.exist?(lldbinit_path)
  File.open(lldbinit_path) { |file|
    while(line = file.gets) != nil
      line = line.strip
      if line == LLDB_INIT_COMMENT
        # skip current and next lines
        file.gets
        next
      end
      all_lines << line
    end
  }
  all_lines
end

.configure(c) ⇒ Object



49
50
51
# File 'lib/cocoapods-xcremotecache/command/hooks.rb', line 49

def self.configure(c)
  @@configuration = c
end

.delete_build_setting(build_settings, key) ⇒ Object

Delete all build setting for a key, including settings like “[skd=*,arch=*]”



340
341
342
343
344
# File 'lib/cocoapods-xcremotecache/command/hooks.rb', line 340

def self.delete_build_setting(build_settings, key)
  for build_setting_key in build_settings.keys
    build_settings.delete(build_setting_key) if build_setting_key == key || build_setting_key.start_with?("#{key}[")
  end
end

.disable_xcremotecache(user_project, pods_project = nil) ⇒ Object

Uninstall the XCRemoteCache



354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'lib/cocoapods-xcremotecache/command/hooks.rb', line 354

def self.disable_xcremotecache(user_project, pods_project = nil)
  user_project.targets.each do |target|
    disable_xcremotecache_for_target(target)
  end
  user_project.save()

  unless pods_project.nil?
    pods_project.native_targets.each do |target|
      disable_xcremotecache_for_target(target)
    end
    pods_proj_directory = pods_project.project_dir
    pods_project.root_object.project_references.each do |subproj_ref|
      generated_project = Xcodeproj::Project.open("#{pods_proj_directory}/#{subproj_ref[:project_ref].path}")
      generated_project.native_targets.each do |target|
        disable_xcremotecache_for_target(target)
      end
      generated_project.save()
    end
    pods_project.save()
  end

  # Remove .lldbinit rewrite
  save_lldbinit_rewrite(nil,nil) unless !@@configuration['modify_lldb_init']
end

.disable_xcremotecache_for_target(target) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/cocoapods-xcremotecache/command/hooks.rb', line 232

def self.disable_xcremotecache_for_target(target)
  target.build_configurations.each do |config|
    config.build_settings.delete('CC') if config.build_settings.key?('CC')
    config.build_settings.delete('SWIFT_EXEC') if config.build_settings.key?('SWIFT_EXEC')
    config.build_settings.delete('LIBTOOL') if config.build_settings.key?('LIBTOOL')
    config.build_settings.delete('LIPO') if config.build_settings.key?('LIPO')
    config.build_settings.delete('LD') if config.build_settings.key?('LD')
    config.build_settings.delete('LDPLUSPLUS') if config.build_settings.key?('LDPLUSPLUS')
    config.build_settings.delete('SWIFT_USE_INTEGRATED_DRIVER') if config.build_settings.key?('SWIFT_USE_INTEGRATED_DRIVER')
    # Remove Fake src root for ObjC & Swift
    config.build_settings.delete('XCREMOTE_CACHE_FAKE_SRCROOT')
    config.build_settings.delete('XCRC_PLATFORM_PREFERRED_ARCH')
    config.build_settings.delete(XCRC_COOCAPODS_ROOT_KEY)
    remove_cflags!(config.build_settings, '-fdebug-prefix-map')
    remove_swiftflags!(config.build_settings, '-debug-prefix-map')
  end

  # User project is not generated from scratch (contrary to `Pods`), delete all previous XCRemoteCache phases
  target.build_phases.delete_if {|phase|
    # Some phases (e.g. PBXSourcesBuildPhase) don't have strict name check respond_to?
    if phase.respond_to?(:name)
        phase.name != nil && phase.name.start_with?("[XCRC]")
    end
  }
end

.download_latest_xcrc_release(local_package_location) ⇒ Object



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/cocoapods-xcremotecache/command/hooks.rb', line 283

def self.download_latest_xcrc_release(local_package_location)
  release_url = 'https://api.github.com/repos/spotify/XCRemoteCache/releases/latest'
  asset_url = nil

  URI.open(release_url) do |f|
    assets_array = JSON.parse(f.read)['assets']
    # Pick fat archive
    asset_array = assets_array.detect{|arr| arr['name'].include?(FAT_ARCHIVE_NAME_INFIX)}
    asset_url = asset_array['url']
  end

  if asset_url.nil?
    throw "Downloading XCRemoteCache failed"
  end

  URI.open(asset_url, "accept" => 'application/octet-stream') do |f|
    File.open(local_package_location, "wb") do |file|
      file.puts f.read
    end
  end
end

.download_xcrc_if_needed(local_location) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/cocoapods-xcremotecache/command/hooks.rb', line 263

def self.download_xcrc_if_needed(local_location)
  required_binaries = ['xcld', 'xcldplusplus', 'xclibtool', 'xclipo', 'xcpostbuild', 'xcprebuild', 'xcprepare', 'xcswiftc']
  binaries_exist = required_binaries.reduce(true) do |exists, filename|
    file_path = File.join(local_location, filename)
    exists = exists && File.exist?(file_path)
  end

  # Don't download XCRemoteCache if provided directory already contains it
  return if binaries_exist

  Dir.mkdir(local_location) unless File.exist?(local_location)
  local_package_location = File.join(local_location, 'package.zip')

  download_latest_xcrc_release(local_package_location)

  if !system("unzip #{local_package_location} -d #{local_location}")
    throw "Unzipping XCRemoteCache failed"
  end
end

.enable_xcremotecache(target, repo_distance, xc_location, xc_cc_path, mode, exclude_build_configurations, final_target, fake_src_root, exclude_sdks_configurations, enable_swift_driver_integration) ⇒ Object

Parameters:

  • target (Target)

    target to apply XCRemoteCache

  • repo_distance (Integer)

    distance from the git repo root to the target’s $SRCROOT

  • xc_location (String)

    path to the dir with all XCRemoteCache binaries, relative to the repo root

  • xc_cc_path (String)

    path to the XCRemoteCache clang wrapper, relative to the repo root

  • mode (String)

    mode name (‘consumer’, ‘producer’, ‘producer-fast’ etc.)

  • exclude_build_configurations (String[])

    list of targets that should have disabled remote cache

  • final_target (String)

    name of target that should trigger marking

  • exclude_sdks_configurations (String[])

    list of sdks that should have disabled remote cache



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/cocoapods-xcremotecache/command/hooks.rb', line 117

def self.enable_xcremotecache(
  target,
  repo_distance,
  xc_location,
  xc_cc_path,
  mode,
  exclude_build_configurations,
  final_target,
  fake_src_root,
  exclude_sdks_configurations,
  enable_swift_driver_integration
)
  srcroot_relative_xc_location = parent_dir(xc_location, repo_distance)
  # location of the entrite CocoaPods project, relative to SRCROOT
  srcroot_relative_project_location = parent_dir('', repo_distance)

  target.build_configurations.each do |config|
    # apply only for relevant Configurations
    next if exclude_build_configurations.include?(config.name)
    if mode == 'consumer'
      reset_build_setting(config.build_settings, 'CC', "$SRCROOT/#{parent_dir(xc_cc_path, repo_distance)}", exclude_sdks_configurations)
    elsif mode == 'producer' || mode == 'producer-fast'
      config.build_settings.delete('CC') if config.build_settings.key?('CC')
    end
    swiftc_name = enable_swift_driver_integration ? 'swiftc' : 'xcswiftc'
    reset_build_setting(config.build_settings, 'SWIFT_EXEC', "$SRCROOT/#{srcroot_relative_xc_location}/#{swiftc_name}", exclude_sdks_configurations)
    reset_build_setting(config.build_settings, 'LIBTOOL', "$SRCROOT/#{srcroot_relative_xc_location}/xclibtool", exclude_sdks_configurations)
    # Setting LIBTOOL to '' breaks SwiftDriver intengration so resetting it to the original value 'libtool' for all excluded configurations
    add_build_setting_for_sdks(config.build_settings, 'LIBTOOL', 'libtool', exclude_sdks_configurations)
    reset_build_setting(config.build_settings, 'LD', "$SRCROOT/#{srcroot_relative_xc_location}/xcld", exclude_sdks_configurations)
    reset_build_setting(config.build_settings, 'LDPLUSPLUS', "$SRCROOT/#{srcroot_relative_xc_location}/xcldplusplus", exclude_sdks_configurations)
    reset_build_setting(config.build_settings, 'LIPO', "$SRCROOT/#{srcroot_relative_xc_location}/xclipo", exclude_sdks_configurations)
    reset_build_setting(config.build_settings, 'SWIFT_USE_INTEGRATED_DRIVER', 'NO', exclude_sdks_configurations) unless enable_swift_driver_integration

    reset_build_setting(config.build_settings, 'XCREMOTE_CACHE_FAKE_SRCROOT', fake_src_root, exclude_sdks_configurations)
    reset_build_setting(config.build_settings, 'XCRC_PLATFORM_PREFERRED_ARCH', "$(LINK_FILE_LIST_$(CURRENT_VARIANT)_$(PLATFORM_PREFERRED_ARCH):dir:standardizepath:file:default=arm64)", exclude_sdks_configurations)
    reset_build_setting(config.build_settings, XCRC_COOCAPODS_ROOT_KEY, "$SRCROOT/#{srcroot_relative_project_location}", exclude_sdks_configurations)
    debug_prefix_map_replacement = '$(SRCROOT' + ':dir:standardizepath' * repo_distance + ')'
    add_cflags!(config.build_settings, '-fdebug-prefix-map', "#{debug_prefix_map_replacement}=$(XCREMOTE_CACHE_FAKE_SRCROOT)", exclude_sdks_configurations)
    add_swiftflags!(config.build_settings, '-debug-prefix-map', "#{debug_prefix_map_replacement}=$(XCREMOTE_CACHE_FAKE_SRCROOT)", exclude_sdks_configurations)
    delete_build_setting(config.build_settings, 'XCRC_DISABLED')
    add_build_setting_for_sdks(config.build_settings, 'XCRC_DISABLED', 'YES', exclude_sdks_configurations)
  end

  # Prebuild
  if mode == 'consumer'
    existing_prebuild_script = target.build_phases.detect do |phase|
      if phase.respond_to?(:name)
        phase.name != nil && phase.name.start_with?("[XCRC] Prebuild")
      end
    end

    prebuild_script = existing_prebuild_script || target.new_shell_script_build_phase("[XCRC] Prebuild #{target.name}")
    prebuild_script.shell_script = "\"$SCRIPT_INPUT_FILE_0\""
    prebuild_script.input_paths = ["$SRCROOT/#{srcroot_relative_xc_location}/xcprebuild"]
    prebuild_script.output_paths = [
      "$(TARGET_TEMP_DIR)/rc.enabled",
      "$(DWARF_DSYM_FOLDER_PATH)/$(DWARF_DSYM_FILE_NAME)"
    ]
    prebuild_script.dependency_file = "$(TARGET_TEMP_DIR)/prebuild.d"

    # Move prebuild (last element) to the position before compile sources phase (to make it real 'prebuild')
    if !existing_prebuild_script
      compile_phase_index = target.build_phases.index(target.source_build_phase)
      target.build_phases.insert(compile_phase_index, target.build_phases.delete(prebuild_script))
    end
  elsif mode == 'producer' || mode == 'producer-fast'
    # Delete existing prebuild build phase (to support switching between modes)
    target.build_phases.delete_if do |phase|
      if phase.respond_to?(:name)
        phase.name != nil && phase.name.start_with?("[XCRC] Prebuild")
      end
    end
  end

  # Postbuild
  existing_postbuild_script = target.build_phases.detect do |phase|
    if phase.respond_to?(:name)
      phase.name != nil && phase.name.start_with?("[XCRC] Postbuild")
    end
  end
  postbuild_script = existing_postbuild_script || target.new_shell_script_build_phase("[XCRC] Postbuild #{target.name}")
  postbuild_script.shell_script = "\"$SCRIPT_INPUT_FILE_0\""
  postbuild_script.input_paths = ["$SRCROOT/#{srcroot_relative_xc_location}/xcpostbuild"]
  postbuild_script.output_paths = [
    "$(TARGET_BUILD_DIR)/$(MODULES_FOLDER_PATH)/$(PRODUCT_MODULE_NAME).swiftmodule/$(XCRC_PLATFORM_PREFERRED_ARCH).swiftmodule.md5",
    "$(TARGET_BUILD_DIR)/$(MODULES_FOLDER_PATH)/$(PRODUCT_MODULE_NAME).swiftmodule/$(XCRC_PLATFORM_PREFERRED_ARCH)-$(LLVM_TARGET_TRIPLE_VENDOR)-$(SWIFT_PLATFORM_TARGET_PREFIX)$(LLVM_TARGET_TRIPLE_SUFFIX).swiftmodule.md5"
  ]
  postbuild_script.dependency_file = "$(TARGET_TEMP_DIR)/postbuild.d"
  # Move postbuild (last element) to the position after compile sources phase (to make it real 'postbuild')
  if !existing_postbuild_script
    compile_phase_index = target.build_phases.index(target.source_build_phase)
    target.build_phases.insert(compile_phase_index + 1, target.build_phases.delete(postbuild_script))
  end

  # Mark a sha as ready for a given platform and configuration when building the final_target
  if (mode == 'producer' || mode == 'producer-fast') && target.name == final_target
    existing_mark_script = target.build_phases.detect do |phase|
      if phase.respond_to?(:name)
        phase.name != nil && phase.name.start_with?("[XCRC] Mark")
      end
    end
    mark_script = existing_mark_script || target.new_shell_script_build_phase("[XCRC] Mark")
    mark_script.shell_script = "\"$SCRIPT_INPUT_FILE_0\" mark --configuration \"$CONFIGURATION\" --platform $PLATFORM_NAME"
    mark_script.input_paths = ["$SRCROOT/#{srcroot_relative_xc_location}/xcprepare"]
  else
    # Delete existing mark build phase (to support switching between modes or changing the final target)
    target.build_phases.delete_if do |phase|
      if phase.respond_to?(:name)
        phase.name != nil && phase.name.start_with?("[XCRC] Mark")
      end
    end
  end
end

.find_source_build_phase(target) ⇒ Object

Contrary to AbstractTarget.source_build_phase, it only finds a build phase, without creating one if it doesn’t exist



413
414
415
# File 'lib/cocoapods-xcremotecache/command/hooks.rb', line 413

def self.find_source_build_phase(target)
  target.build_phases.find { |bp| bp.class == Xcodeproj::Project::Object::PBXSourcesBuildPhase }
end

.generate_rcinfoObject



101
102
103
# File 'lib/cocoapods-xcremotecache/command/hooks.rb', line 101

def self.generate_rcinfo()
  @@configuration.select { |key, value| !CUSTOM_CONFIGURATION_KEYS.include?(key) }
end

.parent_dir(path, parent_count) ⇒ Object



105
106
107
# File 'lib/cocoapods-xcremotecache/command/hooks.rb', line 105

def self.parent_dir(path, parent_count)
  "../" * parent_count + path
end

.remove_cflags!(options, key) ⇒ Object



309
310
311
312
313
314
# File 'lib/cocoapods-xcremotecache/command/hooks.rb', line 309

def self.remove_cflags!(options, key)
  cflags_arr = options.fetch('OTHER_CFLAGS', ['$(inherited)'])
  cflags_arr = [cflags_arr] if cflags_arr.kind_of? String
  options['OTHER_CFLAGS'] = cflags_arr.delete_if {|flag| flag.include?("#{key}=") }
  options['OTHER_CFLAGS']
end

.remove_swiftflags!(options, key) ⇒ Object



320
321
322
323
# File 'lib/cocoapods-xcremotecache/command/hooks.rb', line 320

def self.remove_swiftflags!(options, key)
  options['OTHER_SWIFT_FLAGS'] = options.fetch('OTHER_SWIFT_FLAGS', '$(inherited)').gsub(/\s+#{Regexp.escape(key)}\s+\S+/, '')
  options['OTHER_SWIFT_FLAGS']
end

.reset_build_setting(build_settings, key, value, exclude_sdks_configurations) ⇒ Object

Deletes all previous build settings for a key, and sets a new value to all configurations but the sdks in exclude_sdks_configurations



334
335
336
337
# File 'lib/cocoapods-xcremotecache/command/hooks.rb', line 334

def self.reset_build_setting(build_settings, key, value, exclude_sdks_configurations)
  delete_build_setting(build_settings, key)
  add_build_setting(build_settings, key, value, exclude_sdks_configurations)
end

.save_lldbinit_rewrite(user_proj_directory, fake_src_root) ⇒ Object



406
407
408
409
410
# File 'lib/cocoapods-xcremotecache/command/hooks.rb', line 406

def self.save_lldbinit_rewrite(user_proj_directory,fake_src_root)
  lldbinit_lines = clean_lldbinit_content(LLDB_INIT_PATH)
  lldbinit_lines = add_lldbinit_rewrite(lldbinit_lines, user_proj_directory,fake_src_root) unless user_proj_directory.nil?
  File.write(LLDB_INIT_PATH, lldbinit_lines.join("\n"), mode: "w")
end

.save_rcinfo(info, directory) ⇒ Object

Writes XCRemoteCache info in the specified directory location



259
260
261
# File 'lib/cocoapods-xcremotecache/command/hooks.rb', line 259

def self.save_rcinfo(info, directory)
    File.open(File.join(directory, '.rcinfo'), 'w') { |file| file.write info.to_yaml }
end

.set_configuration_default_valuesObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cocoapods-xcremotecache/command/hooks.rb', line 53

def self.set_configuration_default_values
  default_values = {
    'mode' => 'consumer',
    'enabled' => true,
    'xcrc_location' => "XCRC",
    'exclude_build_configurations' => [],
    'check_build_configuration' => 'Debug',
    'check_platform' => 'iphonesimulator',
    'modify_lldb_init' => true,
    'xccc_file' => "#{BIN_DIR}/xccc",
    'remote_commit_file' => "#{BIN_DIR}/arc.rc",
    'exclude_targets' => [],
    'prettify_meta_files' => false,
    'fake_src_root' => "/#{'x' * 10 }",
    'disable_certificate_verification' => false,
    'custom_rewrite_envs' => [],
    'exclude_sdks_configurations' => []
  }
  @@configuration.merge! default_values.select { |k, v| !@@configuration.key?(k) }
  # Always include XCRC_COOCAPODS_ROOT_KEY in custom_rewrite_envs
  unless @@configuration['custom_rewrite_envs'].include?(XCRC_COOCAPODS_ROOT_KEY)
    @@configuration['custom_rewrite_envs'] << XCRC_COOCAPODS_ROOT_KEY
  end
end

.validate_configurationObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/cocoapods-xcremotecache/command/hooks.rb', line 78

def self.validate_configuration()
  required_values = [
    'cache_addresses',
    'primary_repo',
    'check_build_configuration',
    'check_platform'
  ]

  missing_configuration_values = required_values.select { |v| !@@configuration.key?(v) }
  unless missing_configuration_values.empty?
    throw "XCRemoteCache not fully configured. Make sure all required fields are provided. Missing fields are: #{missing_configuration_values.join(', ')}."
  end

  mode = @@configuration['mode']
  unless mode == 'consumer' || mode == 'producer' || mode == 'producer-fast'
    throw "Incorrect 'mode' value. Allowed values are ['consumer', 'producer', 'producer-fast'], but you provided '#{mode}'. A typo?"
  end

  unless mode == 'consumer' || @@configuration.key?('final_target')
    throw "Missing 'final_target' value in the Pod configuration."
  end
end