Class: Pod::XCFrameworkBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/cocoapods-util/command/xcframework/xcframework_build.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, source_dir) ⇒ XCFrameworkBuilder

Returns a new instance of XCFrameworkBuilder.

[View source]

3
4
5
6
# File 'lib/cocoapods-util/command/xcframework/xcframework_build.rb', line 3

def initialize(name, source_dir)
    @name = name
    @source_dir = source_dir
end

Instance Method Details

#build_static_xcframeworkObject

[View source]

8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
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
# File 'lib/cocoapods-util/command/xcframework/xcframework_build.rb', line 8

def build_static_xcframework
    framework_path = "#{@source_dir}/#{@name}.framework"
    # 可执行文件名称
    lib_file = "#{@name}"
    # 可执行文件完整路径
    lib_path = "#{framework_path}/#{lib_file}"
    # 可执行文件不存在,退出
    unless File.exist? lib_path
        UI.puts("没有找到可执行文件,请检查输入的framework")
        return nil
    end
    # 如果可执行文件为软链接类型,获取realpath
    if File.ftype(lib_path) == 'link'
        lib_file = File.readlink(lib_path) 
        lib_path = "#{framework_path}/#{lib_file}"
    end

    framework_paths = Array.new
    # 获取可执行文件的支持架构
    archs = `lipo -archs #{lib_path}`.split
    os_archs = sim_archs = []
    if archs.empty?
        UI.puts "framework文件中没有检查到任何编译架构,请使用`lipo -info`或`lipo -archs`检查文件支持的架构。"
        return
    elsif archs.count == 1
        framework_paths += [framework_path]
    else
        platform = `strings #{lib_path} | grep -E -i '/Platforms/.*\.platform/' | head -n 1`.chomp!
        if platform =~ /iPhone[^\.]*\.platform/ # iphoneos iphonesimulator
            os_archs = archs & ['arm64', 'armv7', 'armv7s']
            sim_archs = archs & ['i386', 'x86_64']
        elsif platform =~ /MacOSX.platform/ # macosx
            os_archs = ['arm64', 'x86_64']
        elsif platform =~ /Watch[^\.]*\.platform/ # watchos watchsimulator
            os_archs = archs & ['armv7k', 'arm64_32']
            sim_archs = archs & ['arm64', 'i386', 'x86_64']
        elsif platform =~ /AppleTV[^\.]*\.platform/ # appletvos appletvsimulator
            os_archs = archs & ['arm64']
            sim_archs = archs & ['x86_64'] # 'arm64' 'x86_64'
        else
            os_archs = archs & ['arm64', 'armv7', 'armv7s']
            sim_archs = archs & ['i386', 'x86_64']
        end
    end
    
    # 1. remove os/simulator paths
    clean_intermediate_path

    # 2. copy os framework
    if os_archs.count > 0
        path = Pathname.new("#{@source_dir}/#{os_target_path}")
        FileUtils.mkdir_p(path) unless path.exist?
        `cp -a #{framework_path} #{path}/`

        fwk_path = "#{path}/#{@name}.framework"
        framework_paths += ["#{fwk_path}"]
        `lipo -extract #{os_archs.join(' -extract ')} "#{fwk_path}/#{lib_file}" -output "#{fwk_path}/#{lib_file}"`
    end
    # 3. copy simulation framework
    if sim_archs.count > 0
        path = Pathname.new("#{@source_dir}/#{simulator_target_path}")
        FileUtils.mkdir_p(path) unless path.exist?
        `cp -a #{framework_path} #{path}/`

        fwk_path = "#{path}/#{@name}.framework"
        framework_paths += ["#{fwk_path}"]
        `lipo -extract #{sim_archs.join(' -extract ')} "#{fwk_path}/#{lib_file}" -output "#{fwk_path}/#{lib_file}"`
    end

    # 4. generate xcframework
    begin
        generate_xcframework(framework_paths)
    ensure # in case the create-xcframework fails; remove the temp directory.
        clean_intermediate_path
    end
end

#generate_xcframework(framework_paths) ⇒ Object

[View source]

85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/cocoapods-util/command/xcframework/xcframework_build.rb', line 85

def generate_xcframework(framework_paths)
    UI.puts("Generate #{@name}.xcframework")

    # create xcframework
    command = "xcodebuild -create-xcframework -allow-internal-distribution -framework #{framework_paths.join(' -framework ')} -output #{@source_dir}/#{@name}.xcframework 2>&1"
    output = `#{command}`.lines.to_a
    result = $?
    # show error
    if result.exitstatus != 0
        puts UI::BuildFailedReport.report(command, output)
        Process.exit
    end
    UI.puts("Generate #{@name}.xcframework succees")
end