Class: Pod::LocalBuilder

Inherits:
Object
  • Object
show all
Includes:
Config::Mixin, DirUtil, PodUtil
Defined in:
lib/cocoapods-xcframework/local_build.rb

Instance Method Summary collapse

Methods included from DirUtil

#create_target_directory_path_by_name, #create_target_directory_path_by_spec, #create_working_directory_by_name, #create_working_directory_by_spec

Methods included from PodUtil

#build_static_sandbox, #fix_bundle_file, #fix_header_file, #generic_new_podspec_hash, #installation_root, #installation_root_muti, #muti_config_with_file, #podfile_from_muti_configs, #podfile_from_spec, #spec_with_name, #spec_with_path, #to_native_platform

Constructor Details

#initialize(name, source, spec_sources, subspecs, configuration, force, use_modular_headers = true, enable_bitcode = false, symbols = true, support_maccatalyst = true, support_dynamic = false, source_dir = Dir.pwd) ⇒ LocalBuilder

Returns a new instance of LocalBuilder.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/cocoapods-xcframework/local_build.rb', line 6

def initialize(name, source, spec_sources, subspecs, configuration, force, use_modular_headers=true, enable_bitcode=false, symbols=true, support_maccatalyst=true, support_dynamic=false, source_dir=Dir.pwd)
  @name = name
  @source = source
  @spec_sources = spec_sources
  @subspecs = subspecs
  @configuration = configuration
  @force = force
  @use_modular_headers = use_modular_headers
  @enable_bitcode = enable_bitcode
  @symbols = symbols
  @support_maccatalyst = support_maccatalyst
  @support_dynamic = support_dynamic
  @spec = spec_with_path @name
  @source_dir = source_dir
  @outputs = Hash.new
end

Instance Method Details

#buildObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/cocoapods-xcframework/local_build.rb', line 26

def build
  spec = spec_with_path @name
  UI.puts "正在生成Local XCFramework #{spec.name}(#{spec.version}) with configuration #{@configuration} isSymbols:#{@symbols}".yellow
  @is_spec_from_path = true if spec
  spec ||= spec_with_name @name
  target_dir, work_dir = create_working_directory_by_spec spec, @force
  build_dir = "#{work_dir}/build"
  archive_path = "#{build_dir}/archives"
  defines = ""
  if @configuration == 'Debug'
    defines << 'GCC_GENERATE_DEBUGGING_SYMBOLS=YES ONLY_ACTIVE_ARCH=NO'
  else
    if @symbols
      defines << "GCC_GENERATE_DEBUGGING_SYMBOLS=YES" # Release模式需要符号表应于问题排查(二进制切源码操作) by hm 21/10/13
    else
      defines << "GCC_GENERATE_DEBUGGING_SYMBOLS=NO" # 去除符号表
    end
  end
  FileUtils.rm_rf(build_dir)

  build_all_device(defines, build_dir, archive_path)
  makeXCFramework(archive_path, build_dir)
  exportdSYMs(archive_path, build_dir)
  outputs(target_dir)
end

#build_all_device(defines, build_dir, archive_path) ⇒ Object



104
105
106
107
108
109
# File 'lib/cocoapods-xcframework/local_build.rb', line 104

def build_all_device(defines, build_dir, archive_path)
  # build general first because simulator will exchange SDKROOT to simulat sdk
  build_general_device defines, build_dir, archive_path
  build_simulator_device defines, build_dir, archive_path
  build_MacCatalyst_device defines, build_dir, archive_path if @support_maccatalyst
end

#build_general_device(defines, build_dir, archive_path) ⇒ Object



111
112
113
114
115
116
# File 'lib/cocoapods-xcframework/local_build.rb', line 111

def build_general_device defines, build_dir, archive_path
    UI.puts("--- Building framework #{@spec} with general device")
    destination = 'generic/platform=iOS'
    archs = 'arm64'
    xcode_xbuild(destination, archs, "iOS", defines, build_dir, archive_path)
end

#build_MacCatalyst_device(defines, build_dir, archive_path) ⇒ Object



125
126
127
128
129
130
# File 'lib/cocoapods-xcframework/local_build.rb', line 125

def build_MacCatalyst_device defines, build_dir, archive_path
    UI.puts("--- Building framework #{@spec} with MacCatalyst device")
    destination = 'generic/platform=macOS,variant=Mac Catalyst,name=Any Mac'
    archs = 'x86_64 arm64'
    xcode_xbuild(destination, archs, "MacCatalyst", defines, build_dir, archive_path)
end

#build_simulator_device(defines, build_dir, archive_path) ⇒ Object



118
119
120
121
122
123
# File 'lib/cocoapods-xcframework/local_build.rb', line 118

def build_simulator_device defines, build_dir, archive_path
    UI.puts("--- Building framework #{@spec} with simulator device")
    destination = 'generic/platform=iOS Simulator'
    archs = 'x86_64 arm64'
    xcode_xbuild(destination, archs, "Simulator", defines, build_dir, archive_path)
end

#exportdSYMs(archive_path, build_dir) ⇒ Object

导出符号表



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
231
# File 'lib/cocoapods-xcframework/local_build.rb', line 191

def exportdSYMs(archive_path, build_dir)
  spec = spec_with_path @name
  spec ||= spec_with_name @name
  framework_name = spec.name

  dsym_output_dir = File.join(build_dir, 'dSYMs')
  FileUtils.mkdir_p(dsym_output_dir)

  archives = Dir.glob(File.join(archive_path, '*.xcarchive'))
  if archives.empty?
    UI.puts("⚠️ 未在 #{archive_path} 下找到 xcarchive,跳过 dSYM 导出").yellow
    return
  end

  exported = false

  archives.each do |archive|
    dsym_path = File.join(
      archive,
      'dSYMs',
      "#{framework_name}.framework.dSYM"
    )

    if File.exist?(dsym_path)
      archive_name = File.basename(archive, '.xcarchive')
      target_path = File.join(dsym_output_dir, "#{archive_name}.framework.dSYM")
      FileUtils.rm_rf(target_path)
      FileUtils.cp_r(dsym_path, target_path)
      UI.puts "� Exported dSYM: #{target_path}".green
      exported = true
    else
      UI.puts "⚠️ 未在 #{archive} 中找到 dSYM,已跳过".yellow
    end
  end

  unless exported
    UI.puts("⚠️ 未导出任何 dSYM,请确认 DEBUG_INFORMATION_FORMAT=dwarf-with-dsym").yellow
  else
    UI.puts "✅ dSYM 导出完成,目录:#{dsym_output_dir}".green
  end
end

#find_bundles(target_dir) ⇒ Object



293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/cocoapods-xcframework/local_build.rb', line 293

def find_bundles target_dir
  bundle_root = "#{target_dir}/bundle/"
  pattern = "#{bundle_root}*"
  result = {}
  Pathname.glob(pattern).each do |bundle|
    bundle_relative_path = bundle.to_s.gsub(bundle_root, "")
    plat = bundle_relative_path
    result[plat] = {
      "#{@spec.name}" => "bundle/" + bundle_relative_path + "/*"
    }
  end
  result
end

#fix_xcframework_error(target_dir, name) ⇒ Object

解决项目是Swift和Objc混合开发,有一个Swift类名和ModuleName相同,当打包成xcframework引入到项目时, 会报错… is not a member type of …



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/cocoapods-xcframework/local_build.rb', line 268

def fix_xcframework_error target_dir, name
  # 全量替换代价:Swift module identity 被抹掉,--- 调试时能看不清来自哪个组件
  # command = "find #{target_dir} -name '*.swiftinterface' -exec sed -i -e 's/#{name}\\.//g' {} \\; && find #{target_dir} -name '*.swiftinterface-e' | xargs rm -rf" 
  # UI.puts "fix swift error command :#{command}"
  
  # 安全版修复: by hm 26/1/30
  # 仅修复 Swift 类名 == ModuleName 时产生的 `ModuleName.ModuleName` 冲突,
  # 避免全量移除 ModuleName. 造成 Swift module identity 丢失。
  UI.puts "fix xcframework-is-not-a-member-type-of-error (safe mode): #{target_dir}"
  # 只替换 `ModuleName.ModuleName` -> `ModuleName`
  pattern = "s/#{name}\\.#{name}/#{name}/g"
  # command = <<~CMD
  #   find #{target_dir} -name '*.swiftinterface' \
  #   -exec sed -i -e '#{pattern}' {} \; \
  #   && find #{target_dir} -name '*.swiftinterface-e' | xargs rm -rf
  # CMD
  command = "find #{target_dir} -name '*.swiftinterface' -exec sed -i -e 's/#{name}\\.#{name}/#{name}//g' {} \\; && find #{target_dir} -name '*.swiftinterface-e' | xargs rm -rf" 
  UI.puts "fix swift error command (safe): #{command}"
  output = `#{command}`.lines.to_a
  if $?.exitstatus != 0
    Pod::ErrorUtil.error_report command, output
    Process.exit -1
  end
end

#makeXCFramework(archive_path, output_dir) ⇒ Object



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
# File 'lib/cocoapods-xcframework/local_build.rb', line 132

def makeXCFramework(archive_path, output_dir)
  spec = spec_with_path @name
  spec ||= spec_with_name @name
  framework_name = spec.name

  # scan xcarchive directories
  archives = Dir.glob(File.join(archive_path, '*.xcarchive'))
  if archives.empty?
    UI.puts("❌ 未在 #{archive_path} 下找到 xcarchive 产物,无法生成 xcframework").red
    Process.exit 1
  end

  framework_args = []

  archives.each do |archive|
    framework_path = File.join(
      archive,
      'Products',
      'Library',
      'Frameworks',
      "#{framework_name}.framework"
    )
    if File.exist?(framework_path)
      framework_args << "-framework '#{framework_path}'"
      UI.puts "� Found framework: #{framework_path}".green
    else
      UI.puts "⚠️ 未在 #{archive} 中找到 #{framework_name}.framework,已跳过".yellow
    end
  end

  if framework_args.empty?
    UI.puts("❌ 未找到任何可用 Framework,xcframework 生成失败").red
    Process.exit 1
  end

  xcframework_path = File.join(output_dir, "#{framework_name}.xcframework")
  FileUtils.rm_rf(xcframework_path)
  @outputs[:xcframework] = xcframework_path

  command = [
    'xcodebuild -create-xcframework',
    framework_args.join(' '),
    "-output '#{xcframework_path}'"
  ].join(' ')

  UI.puts "� Creating XCFramework:"
  UI.puts command.cyan

  success = system(command)
  unless success
    UI.puts("❌ xcodebuild -create-xcframework 执行失败").red
    Process.exit $?.exitstatus.nonzero? || 1
  end

  UI.puts "✅ XCFramework 生成成功:"
  UI.puts xcframework_path.green
end

#outputs(target_dir) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/cocoapods-xcframework/local_build.rb', line 233

def outputs target_dir
  puts "outputs:#{target_dir}"
  if not File.exist? target_dir
    Pathname.new(target_dir).mkdir
  end
  outputs_xcframework target_dir
  outputs_bundle target_dir
  new_spec_hash = generic_new_podspec_hash @spec
  new_spec_hash[:vendored_frameworks] = "#{@spec.name}.xcframework"
  xcframework_path = "#{target_dir}/#{@spec.name}.xcframework"
  # new_spec_hash = fix_header_file new_spec_hash, xcframework_path
  # new_spec_hash = fix_bundle_file(@spec, new_spec_hash, @source_dir, xcframework_path)
  # find_bundles(target_dir).each do |plat, value| 
  #   if new_spec_hash[plat]
  #     new_spec_hash[plat]["resource_bundles"] = value
  #   else
  #     new_spec_hash[plat] = {
  #       "resource_bundles" => value
  #     }
  #   end
  # end
  require 'json'
  spec_json = JSON.pretty_generate(new_spec_hash) << "\n"
  File.open("#{target_dir}/#{@spec.name}.podspec.json",'wb+') do |f|
    f.write(spec_json)
  end
  # fix xcframework-is-not-a-member-type-of-error https://v3.bbcloud.babybus.com/team/184/wiki/13820
  # UI.puts "fix xcframework-is-not-a-member-type-of-error:#{target_dir}"
  # fix_xcframework_error(target_dir, @spec.name)

  UI.puts "result export at :#{target_dir} 生成成功".green
  target_dir
end

#outputs_bundle(target_dir) ⇒ Object



316
317
318
319
320
# File 'lib/cocoapods-xcframework/local_build.rb', line 316

def outputs_bundle target_dir
  if @outputs[:bundle]
    FileUtils.cp_r(Dir[@outputs[:bundle]],target_dir)
  end
end

#outputs_xcframework(target_dir) ⇒ Object



307
308
309
310
311
312
313
314
# File 'lib/cocoapods-xcframework/local_build.rb', line 307

def outputs_xcframework target_dir
  command = "cp -rp #{@outputs[:xcframework]} #{target_dir} 2>&1"
  output = `#{command}`.lines.to_a
  if $?.exitstatus != 0
    Pod::ErrorUtil.error_report command,output
    Process.exit -1
  end
end

#runObject



23
24
# File 'lib/cocoapods-xcframework/local_build.rb', line 23

def run
end

#xcode_xbuild(destination, archs, platform, defines, build_dir, archive_path) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/cocoapods-xcframework/local_build.rb', line 52

def xcode_xbuild(destination, archs, platform, defines, build_dir, archive_path)
    # xcodebuild archive requires -scheme when -archivePath is specified
    spec = spec_with_path @name
    spec ||= spec_with_name @name
    
    scheme_name = @spec&.name
    configuration = @configuration
    skip_install = 'NO'
    plat_name   = platform.to_s.gsub(/\s+/, '_')
    archivePath = "#{archive_path}/#{scheme_name}-#{plat_name}.xcarchive"
    UI.puts "archivePath:#{archivePath}"
    if scheme_name.nil?
      UI.puts("❌ archive 构建缺少 scheme,请在调用 xcode_xbuild 时显式传入 scheme").red
      Process.exit 1
    end
    command = "xcodebuild archive " \
              "-scheme '#{scheme_name}' " \
              "-archivePath '#{archivePath}' " \
              "-destination '#{destination}' " \
              "-configuration #{configuration} " \
              "ARCHS='#{archs}' " \
              "SKIP_INSTALL=#{skip_install} " \
              "#{defines} " \
              "SWIFT_OPTIMIZATION_LEVEL=-Onone DEBUG_INFORMATION_FORMAT=dwarf-with-dsym " \
              "BUILD_LIBRARY_FOR_DISTRIBUTION=YES  clean build" \

    # --- enhanced logging & reliable failure surfacing ---
    log_dir  = File.join(archivePath, 'logs')
    FileUtils.mkdir_p(log_dir)
    log_raw = File.join(log_dir, "xcodebuild-#{scheme_name}-#{plat_name}-#{configuration}.raw.log")

    # Allow disabling xcpretty via env
    use_pretty = ENV['XBUILDER_NOPRETTY'] != '1'
    wrapped = if use_pretty
      "set -o pipefail; #{command} | tee '#{log_raw}' | xcpretty"
    else
      # no pretty: just tee to log and console
      "set -o pipefail; #{command} | tee '#{log_raw}'"
    end
    UI.puts "XBuilder command: #{command}"
    success = system('bash', '-lc', wrapped)
    unless success
      UI.puts "\n** xcodebuild failed — showing last 200 lines of raw log: #{log_raw}".red
      if File.exist?(log_raw)
        tail = `tail -n 200 '#{log_raw}'`
        UI.puts tail
      end
      Pod::ErrorUtil.error_report(command, []) if defined?(Pod::ErrorUtil)
      Process.exit $?.exitstatus.nonzero? || 1
    end
end