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_cflags!(options, key, value) ⇒ Object



267
268
269
270
# File 'lib/cocoapods-xcremotecache/command/hooks.rb', line 267

def self.add_cflags!(options, key, value)
  return if options.fetch('OTHER_CFLAGS',[]).include?(value)
  options['OTHER_CFLAGS'] = remove_cflags!(options, key) << "#{key}=#{value}" 
end

.add_lldbinit_rewrite(lines_content, user_proj_directory, fake_src_root) ⇒ Object

Append source rewrite command to the lldbinit content



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

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) ⇒ Object



279
280
281
282
# File 'lib/cocoapods-xcremotecache/command/hooks.rb', line 279

def self.add_swiftflags!(options, key, value)
  return if options.fetch('OTHER_SWIFT_FLAGS','').include?(value)
  options['OTHER_SWIFT_FLAGS'] = remove_swiftflags!(options, key) + " #{key} #{value}" 
end

.clean_lldbinit_content(lldbinit_path) ⇒ Object

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



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/cocoapods-xcremotecache/command/hooks.rb', line 316

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

.disable_xcremotecache(user_project, pods_project = nil) ⇒ Object

Uninstall the XCRemoteCache



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/cocoapods-xcremotecache/command/hooks.rb', line 290

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



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/cocoapods-xcremotecache/command/hooks.rb', line 198

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('LD') if config.build_settings.key?('LD')
    # Remove Fake src root for ObjC & Swift
    config.build_settings.delete('XCREMOTE_CACHE_FAKE_SRCROOT')
    config.build_settings.delete('XCRC_PLATFORM_PREFERRED_ARCH')
    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



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/cocoapods-xcremotecache/command/hooks.rb', line 245

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



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/cocoapods-xcremotecache/command/hooks.rb', line 225

def self.download_xcrc_if_needed(local_location)
  required_binaries = ['xcld', 'xclibtool', '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) ⇒ 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



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
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
# File 'lib/cocoapods-xcremotecache/command/hooks.rb', line 110

def self.enable_xcremotecache(target, repo_distance, xc_location, xc_cc_path, mode, exclude_build_configurations, final_target, fake_src_root)
  srcroot_relative_xc_location = parent_dir(xc_location, repo_distance)

  target.build_configurations.each do |config|
    # apply only for relevant Configurations
    next if exclude_build_configurations.include?(config.name)
    if mode == 'consumer'
      config.build_settings['CC'] = ["$SRCROOT/#{parent_dir(xc_cc_path, repo_distance)}"]
    elsif mode == 'producer' || mode == 'producer-fast'
      config.build_settings.delete('CC') if config.build_settings.key?('CC')
    end
    config.build_settings['SWIFT_EXEC'] = ["$SRCROOT/#{srcroot_relative_xc_location}/xcswiftc"]
    config.build_settings['LIBTOOL'] = ["$SRCROOT/#{srcroot_relative_xc_location}/xclibtool"]
    config.build_settings['LD'] = ["$SRCROOT/#{srcroot_relative_xc_location}/xcld"]

    config.build_settings['XCREMOTE_CACHE_FAKE_SRCROOT'] = fake_src_root
    config.build_settings['XCRC_PLATFORM_PREFERRED_ARCH'] = ["$(LINK_FILE_LIST_$(CURRENT_VARIANT)_$(PLATFORM_PREFERRED_ARCH):dir:standardizepath:file:default=arm64)"]
    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)")
    add_swiftflags!(config.build_settings, '-debug-prefix-map', "#{debug_prefix_map_replacement}=$(XCREMOTE_CACHE_FAKE_SRCROOT)")
  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"

  # 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

.generate_rcinfoObject



95
96
97
# File 'lib/cocoapods-xcremotecache/command/hooks.rb', line 95

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

.parent_dir(path, parent_count) ⇒ Object



99
100
101
# File 'lib/cocoapods-xcremotecache/command/hooks.rb', line 99

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

.remove_cflags!(options, key) ⇒ Object



272
273
274
275
276
277
# File 'lib/cocoapods-xcremotecache/command/hooks.rb', line 272

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



284
285
286
287
# File 'lib/cocoapods-xcremotecache/command/hooks.rb', line 284

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

.save_lldbinit_rewrite(user_proj_directory, fake_src_root) ⇒ Object



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

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



221
222
223
# File 'lib/cocoapods-xcremotecache/command/hooks.rb', line 221

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
# 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
  }
  @@configuration.merge! default_values.select { |k, v| !@@configuration.key?(k) }
end

.validate_configurationObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/cocoapods-xcremotecache/command/hooks.rb', line 72

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