Class: PodPostInstaller

Inherits:
Object
  • Object
show all
Defined in:
lib/cocoapods-bb-PodAssistant/babybus/installer/post_install_hooks.rb

Instance Method Summary collapse

Constructor Details

#initialize(lib, deployment_target = nil, filter_targets = ["test"], is_matrix = true) ⇒ PodPostInstaller

Returns a new instance of PodPostInstaller.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/cocoapods-bb-PodAssistant/babybus/installer/post_install_hooks.rb', line 11

def initialize(lib, deployment_target=nil, filter_targets=["test"], is_matrix=true)
    @lib = lib
    if deployment_target.nil?
        is_xcode16 = BB::PodUtils.above_xcode_16_version
        if (is_xcode16 == true) then
            @deployment_target = "13.0" # iOS18最低支持13系统
        else
            @deployment_target = isUnity3DApp ? "12.0" : "11.0" # Unity2022引擎最低支持iOS12.0、unity2d iOS11 、cocos iOS10、超A产品 iOS11 (968广告厂商升级最低支持iOS11)
        end
    else
        @deployment_target = deployment_target
    end
    if filter_targets.is_a? Array
        @filter_targets = filter_targets # 过滤target
    elsif filter_targets.is_a? String
        @filter_targets = [filter_targets] # 过滤target
    end
    @is_matrix = is_matrix
end

Instance Method Details

#fix_above_xcode16_no_poObject

修正Xcode16静态库不能po问题(打印swift参数、全局变量)



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
# File 'lib/cocoapods-bb-PodAssistant/babybus/installer/post_install_hooks.rb', line 40

def fix_above_xcode16_no_po()
    is_above_xcode16 = BB::PodUtils.above_xcode_16_version # 高于16
    if (is_above_xcode16 == true) then
        puts "� CocoaPods: Modifying OTHER_LDFLAGS for Xcode16+ LLDB debugging..."
        @lib.aggregate_targets.each do |target|
            xcconfig_path = File.join(@lib.sandbox.target_support_files_root.to_s, target.name, "#{target.name}.debug.xcconfig")
        
            # 确保 xcconfig 文件存在
            next unless File.exist?(xcconfig_path)
        
            # 读取 xcconfig 内容
            xcconfig_content = File.read(xcconfig_path)
        
            # 查找 EXISTING `OTHER_LDFLAGS`
            match = xcconfig_content.match(/OTHER_LDFLAGS = ([^\n]+)\n/)
            next unless match # 避免 nil 错误
        
            xcconfig_new_ld_flags = match[1] # 提取原来的 OTHER_LDFLAGS
        
            # 遍历所有 Pod 组件
            target.pod_targets.each do |pod_target|
              if pod_target.build_as_static? 
                pod_target_name = pod_target.name
                swiftmodule_path = "$(TARGET_BUILD_DIR)/#{pod_target_name}/#{pod_target_name}.framework/Modules/#{pod_target_name}.swiftmodule/$(NATIVE_ARCH_ACTUAL)-apple-$(SHALLOW_BUNDLE_TRIPLE).swiftmodule"
                
                # 避免重复添加相同路径
                unless xcconfig_new_ld_flags.include?(swiftmodule_path)
                  xcconfig_new_ld_flags += " -Wl,-add_ast_path,#{swiftmodule_path}"
                end
              end
            end
        
            # 替换原有 OTHER_LDFLAGS
            xcconfig_content.gsub!(/OTHER_LDFLAGS = ([^\n]+)\n/, "OTHER_LDFLAGS = #{xcconfig_new_ld_flags}\n")
        
            # 写回文件(确保不破坏原有格式)
            FileUtils.cp(xcconfig_path, "#{xcconfig_path}.backup") # 备份原文件
            File.write(xcconfig_path, xcconfig_content)
        
            puts "✅ 已添加 SwiftModule 路径到 xcconfig_path: #{xcconfig_path}"
        end
        # @lib.aggregate_targets.each do |target|
        #     xcconfig_path = @lib.sandbox.target_support_files_root.to_s + "/#{target.name}/#{target.name}.debug.xcconfig"
        #     xcconfig_content = File.read xcconfig_path
        #     xcconfig_new_ld_flags = xcconfig_content.match(/OTHER_LDFLAGS = ([^\n]+)\n/)[1]
        #     target.pod_targets.each do |pod_target|
        #         if pod_target.build_as_static?
        #             pod_targetName = pod_target.name
        #             xcconfig_new_ld_flags += " -Wl,-add_ast_path,$(TARGET_BUILD_DIR)/#{pod_targetName}/#{pod_targetName}.framework/Modules/#{pod_targetName}.swiftmodule/$(NATIVE_ARCH_ACTUAL)-apple-$(SHALLOW_BUNDLE_TRIPLE).swiftmodule"
        #         end
        #     end
        #     xcconfig_content.gsub! /OTHER_LDFLAGS = ([^\n]+)\n/, "OTHER_LDFLAGS =#{xcconfig_new_ld_flags}\n"
        #     File.open(xcconfig_path, 'w') do |f|
        #         f.puts xcconfig_content
        #     end
        #     puts "✅ 已添加 SwiftModule 路径到 xcconfig_path: #{xcconfig_path}"
        # end
    end
end

#runObject



100
101
102
103
104
105
106
107
108
109
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/cocoapods-bb-PodAssistant/babybus/installer/post_install_hooks.rb', line 100

def run
    env = BabybusInstallEnv.new()
    is_above_xcode15 = BB::PodUtils.compare_xcode_15_version # 高于15
    is_below_xcode15 = BB::PodUtils.below_xcode_15_version # 低于15
    # 是否Untify项目
    isUnityProject = isUnityApp
    # Untify项目需要移除cocos项目res/src目录
    if (isUnityProject) then
        env.createCocosResource
    end
    ios_deployment_target = @deployment_target
    puts "pod组件配置工程最低支持iOS系统 ===> #{ios_deployment_target.to_s.send(:red)}".green
    @lib.aggregate_targets.first.user_project.save # 解决Xcode13 pod update操作出现failed to save pods.xcodeproj问题 by hm 21/11/8
    project = Xcodeproj::Project.open(BB::PodUtils.getXcodeprojPath)
    project.targets.each do |target|
        target.build_configurations.each do |config|
            # if !target.name.include? 'test'
            if !filterTargetName(target.name) # 过滤target test 工程
                if (@is_matrix == true) && (config.name.include? 'Debug') # 只有debug环境才进行替换操作,其它环境交由打包机处理
                    puts "[#{target.name}/#{config.name}] 开发者debug环境环境替换[PlistMacro/cocos/unity]文件操作,其它环境交由打包机处理".red
                    env.copyPlistMacro # 开发自行打开不能提交到版本控制,避免test包出现配置不正确情况 by hm 22/5/17
                    if (isUnityProject) then
                        env.copyUnityFramework
                    else
                        env.copyCocosPackage
                    end
                end
                # xcode_version = `xcrun xcodebuild -version | grep Xcode | cut -d' ' -f2`.to_f   if xcode_version ≥ 15
                # iOS17适配添加ld64 https://developer.apple.com/documentation/xcode-release-notes/xcode-15-release-notes
                if (is_above_xcode15 == true) then
                    # c++ weak 标识 xcode15 -ld64 is deprecated, use -ld_classic instead
                    config.build_settings['OTHER_LDFLAGS'] = "$(inherited) -lxml2 -lz -ObjC -lstdc++ -Wl -ld_classic -weak_framework SwiftUI"
                else
                    config.build_settings['OTHER_LDFLAGS'] = "$(inherited) -lxml2 -lz -ObjC -lstdc++"
                end
                otherLink_setting_value = config.build_settings['OTHER_LDFLAGS']
                puts "#{target.name}-#{config.name} Build Setting 'OTHER_LDFLAGS' Value: #{otherLink_setting_value}"

                # 配置工程最低部署
                origin_value = config.build_settings['IPHONEOS_DEPLOYMENT_TARGET']
                if origin_value.to_i != ios_deployment_target.to_i
                    config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = ios_deployment_target.to_s
                    new_value = config.build_settings['IPHONEOS_DEPLOYMENT_TARGET']
                    puts "#{target.name}-#{config.name} Build Setting 'IPHONEOS_DEPLOYMENT_TARGET' deployment target: #{origin_value} => #{new_value}"
                end
            end
        end
    end
    # projects.config
    @lib.pod_target_subprojects.each do |project|
        project.build_configurations.each do |config|
            config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = ios_deployment_target
            config.build_settings['ARCHS'] = "$(ARCHS_STANDARD)"
        end
    end
    # project.targets.config
    @lib.pod_target_subprojects.flat_map { |p| p.targets }.each do |target|
        target.build_configurations.each do |config|
            # pod 1.13.0兼容故去除,避免
            # 适配xcode 15 动态库报错: Error 'DT_TOOLCHAIN_DIR cannot be used to evaluate LIBRARY_SEARCH_PATHS, use TOOLCHAIN_DIR instead'
            # if (is_above_xcode15 == true) then
            #     xcconfig_path = config.base_configuration_reference.real_path
            #     xcconfig = File.read(xcconfig_path)
            #     xcconfig_mod = xcconfig.gsub(/DT_TOOLCHAIN_DIR/, "TOOLCHAIN_DIR")
            #     File.open(xcconfig_path, "w") { |file| file << xcconfig_mod }
            # end
            
            config.build_settings['DEAD_CODE_STRIPPING'] = 'YES'
            config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = ios_deployment_target
            config.build_settings['ARCHS'] = "$(ARCHS_STANDARD)"
            # fix xcode14手动签名问题 https://github.com/CocoaPods/CocoaPods/issues/11402
            # config.build_settings["DEVELOPMENT_TEAM"] = "#{teamid}"
            # Fix Xcode14 bundle need sign
            # https://github.com/CocoaPods/CocoaPods/issues/11402#issuecomment-1201464693
            if target.respond_to?(:product_type) and target.product_type == "com.apple.product-type.bundle"
                config.build_settings['CODE_SIGNING_ALLOWED'] = 'NO'
            end
            # 编译器优化
            config.build_settings['WARNING_CFLAGS'] = '-Wno-documentation -Wno-nullability-completeness -Wno-macro-redefined -Wno-property-attribute-mismatch -Wno-strict-prototypes -Wno-incompatible-pointer-types'
            config.build_settings['ASSETCATALOG_COMPILER_OPTIMIZATION'] = 'space'
            config.build_settings['ENABLE_BITCODE'] = 'NO'
            config.build_settings['DEAD_CODE_STRIPPING'] = 'YES'
            if config.name.include? 'Debug'
                config.build_settings['SWIFT_COMPILATION_MODE'] = 'Incremental'
                config.build_settings['SWIFT_OPTIMIZATION_LEVEL'] = '-Onone'
                config.build_settings['GCC_OPTIMIZATION_LEVEL'] = '0'
            else
                config.build_settings['SWIFT_COMPILATION_MODE'] = 'wholemodule'
                config.build_settings['SWIFT_OPTIMIZATION_LEVEL'] = '-Osize'
                config.build_settings['GCC_OPTIMIZATION_LEVEL'] = 'z'
            end
            # C++支持@import (工程中带mm类需要进行控制)
            if (target.name.include? 'matrix-wechat') || (target.name.include? 'Sentry')
                # 三方库不支持modulemap
                config.build_settings['OTHER_CPLUSPLUSFLAGS'] = "$(inherited) $(OTHER_CFLAGS)"
            else
                config.build_settings['OTHER_CPLUSPLUSFLAGS'] = "$(inherited) $(OTHER_CFLAGS) -fmodules -fcxx-modules"
            end
        end
        # 修复警告 Run script build phase '[CP] Copy Pods Resources' will be run during every build because it does not specify any outputs
        fix_phases = target.shell_script_build_phases.find { |x| x.name == '[CP] Copy XCFrameworks' || 'Create Symlinks to Header Folders' }
        if fix_phases
            fix_phases.always_out_of_date = "1"
        end
        if (is_below_xcode15 == true) then
            project.save
        end
    end
    # 修正Xcode16不能po问题
    fix_above_xcode16_no_po()
end