Module: Pod::XBuilder::XcodeXBuilder

Included in:
Pod::XBuilder
Defined in:
lib/cocoapods-xcframework/xbuilder/xcode_xbuild.rb

Instance Method Summary collapse

Instance Method Details

#compare_xcode_14_versionObject

xcode14以上,14以后不再支持armv7(32位设备)



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/cocoapods-xcframework/xbuilder/xcode_xbuild.rb', line 20

def compare_xcode_14_version
  current_version = xcode_version
  if current_version.nil?
    UI.puts("未找到安装的Xcode版本。")
  else
    UI.puts("当前Xcode版本:#{current_version}")
    num_ver = current_version.to_i
    return  num_ver >= 14
  end
  return false
end

#xcode_versionObject

调用方法来获取当前Xcode版本



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/cocoapods-xcframework/xbuilder/xcode_xbuild.rb', line 5

def xcode_version
  xcode_version_output = `xcode-select -p`
  return nil if xcode_version_output.empty?
  
  xcode_path = xcode_version_output.chomp
  version_output = `xcodebuild -version`
  
  # Extract the Xcode version number
  version_match = version_output.match(/Xcode (\d+(\.\d+)+)/)
  return nil if version_match.nil?
  
  xcode_version = version_match[1]
  return xcode_version
end

#xcode_xbuild(defines, configuration, work_dir, build_dir = 'export', platform = 'iOS', project = nil, scheme = nil, skip_install = false) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
103
104
105
106
107
108
# File 'lib/cocoapods-xcframework/xbuilder/xcode_xbuild.rb', line 32

def xcode_xbuild(defines, configuration, work_dir, build_dir = 'export', platform = 'iOS', project = nil, scheme = nil, skip_install = false)
  if defined?(Pod::DONT_CODESIGN)
    defines = "#{defines} CODE_SIGN_IDENTITY=\"\" CODE_SIGNING_REQUIRED=NO"
  end
  pwd = Pathname.pwd
  Dir.chdir work_dir
  # 判断当前Xcode版本是否大于14
  above_ver_14 = compare_xcode_14_version
  
  if platform == 'macOS'
    destination = 'generic/platform=macOS,variant=Mac Catalyst,name=Any Mac'
    archs = 'x86_64 arm64'
  elsif platform == 'iOS Simulator'
    destination = 'generic/platform=iOS Simulator'
    if above_ver_14 # 14以后不支持32位设备
      archs = 'x86_64 arm64'
    else
      archs = 'x86_64 i386 arm64' # swift库针对M1芯片移除x86架构,故模拟器添加i386,解决maccatalyst能正常生成
    end
  else
    destination = 'generic/platform=iOS'
    if above_ver_14 # 14以后不支持32位设备
      archs = 'arm64'
    else
      archs = 'arm64 armv7'
    end
  end
  if skip_install
    skip_install = 'YES'
  else
    skip_install = 'NO'
  end
  if not project.nil?
    # if not scheme.nil?
    #   command = "xcodebuild #{defines} BUILD_DIR=#{build_dir} BUILD_LIBRARY_FOR_DISTRIBUTION=YES clean build -configuration #{configuration} -project '#{project}' -scheme '#{scheme}' -destination '#{destination}' ARCHS='#{archs}' SKIP_INSTALL=#{skip_install} | xcpretty 2>&1"
    # else
    #   command = "xcodebuild #{defines} BUILD_DIR=#{build_dir} BUILD_LIBRARY_FOR_DISTRIBUTION=YES clean build -configuration #{configuration} -alltargets -scheme '#{scheme}' -destination '#{destination}' ARCHS='#{archs}' SKIP_INSTALL=#{skip_install} | xcpretty 2>&1"
    # end
    command = "xcodebuild #{defines} BUILD_DIR=#{build_dir} BUILD_LIBRARY_FOR_DISTRIBUTION=YES clean build -configuration #{configuration} -alltargets -scheme '#{scheme}' -destination '#{destination}' ARCHS='#{archs}' SKIP_INSTALL=#{skip_install} | xcpretty 2>&1"
  else
    command = "xcodebuild #{defines} BUILD_DIR=#{build_dir} BUILD_LIBRARY_FOR_DISTRIBUTION=YES clean build -configuration #{configuration} -alltargets -destination '#{destination}' ARCHS='#{archs}' SKIP_INSTALL=#{skip_install} | xcpretty 2>&1"
  end
  # UI.puts("XBuilder command:#{command}")
  # output = `#{command}`.lines.to_a
  # Dir.chdir pwd
  # if $?.exitstatus != 0
  #   Pod::ErrorUtil.error_report command,output
  #   Process.exit -1
  # end
  
  # --- enhanced logging & reliable failure surfacing ---
  log_dir  = File.join(work_dir, 'logs')
  FileUtils.mkdir_p(log_dir)
  scheme_name = (scheme || @spec&.name || 'Pods').to_s
  plat_name   = platform.to_s.gsub(/\s+/, '_')
  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