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, is_replaceMinVersionForMultiTarget = false) ⇒ PodPostInstaller

安装pod hook操作 进行环境修复 参数说明 lib 组件库 deployment_target 工程最低支持版本,默认unity3d 12.0 / 其它11.0, 976开始默认12.0 广告sdk要求 filter_targets 过滤工程target,多个使用[] is_matrix 是否矩阵产品,默认true,debug场景会进行配置替换操作 is_replaceMinVersionForMultiTarget 多target产品,是否需要替换最低版本支持,默认false



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/cocoapods-bb-PodAssistant/babybus/installer/post_install_hooks.rb', line 18

def initialize(lib, deployment_target=nil, filter_targets=["test"], is_matrix=true, is_replaceMinVersionForMultiTarget=false)
    @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
    @is_replaceMinVersionForMultiTarget = is_replaceMinVersionForMultiTarget
end

Instance Method Details

#fix_above_xcode16_no_poObject

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



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

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



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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
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
# File 'lib/cocoapods-bb-PodAssistant/babybus/installer/post_install_hooks.rb', line 129

def run
    env = BabybusInstallEnv.new()
    is_above_xcode26 = BB::PodUtils.above_xcode_26_version # 高于26
    # 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)}".yellow
    @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)
    should_save = false
    base_ldflags = generate_other_ldflags()
    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
                    end
                    env.copyCocosPackage
                end
                # xcode_version = `xcrun xcodebuild -version | grep Xcode | cut -d' ' -f2`.to_f   if xcode_version ≥ 15
                new_ldflags = base_ldflags.dup
                key = 'OTHER_LDFLAGS'
                # origin_flags = Array(config.build_settings[key]).join(' ').strip
                origin_flags = Array(config.build_settings[key]).dup
                # 确保 inherited 在
                origin_flags.unshift('$(inherited)') unless origin_flags.include?('$(inherited)')
                linkmap_flag = '-Wl,-map,$(CONFIGURATION_BUILD_DIR)/$(PRODUCT_NAME)-LinkMap-$(CURRENT_VARIANT)-$(CURRENT_ARCH).txt'
                if config.name.include?('Debug')
                  new_ldflags << linkmap_flag unless new_ldflags.include?(linkmap_flag)
                else
                  new_ldflags.delete(linkmap_flag)
                end
                # puts "====#{target.name}-#{config.name} beigin====="
                # puts "origin_flags:#{origin_flags}"
                # puts "new_ldflags:#{new_ldflags}"
                # puts "=====#{target.name}-#{config.name} end====="
                if origin_flags != new_ldflags
                    config.build_settings[key] = new_ldflags
                    should_save = true
                    puts "[Updated] #{target.name} #{config.name}: OTHER_LDFLAGS changed Value: #{new_ldflags}".yellow
                else
                    puts "[Skipped] #{target.name} #{config.name}: already up-to-date"
                end
                # 配置工程最低部署
                if @is_replaceMinVersionForMultiTarget
                    origin_value = config.build_settings['IPHONEOS_DEPLOYMENT_TARGET']
                    if origin_value.to_i != ios_deployment_target.to_i
                        should_save = true
                        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
    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
            # Only enable BUILD_LIBRARY_FOR_DISTRIBUTION for binary static libraries
            # if (target.name.include? 'BBNativeContainer') || (target.name.include? 'BBSchemeDispatcher') || (target.name.include? 'BBComponentServicesKit') || (target.name.include? 'BBSecurityProtectionKit')
            #     config.build_settings['BUILD_LIBRARY_FOR_DISTRIBUTION'] = 'YES'
            # end
            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
            # Xcode26写入自定义宏
            if (is_above_xcode26 == true) then
                config.build_settings['SWIFT_ACTIVE_COMPILATION_CONDITIONS'] = "$(inherited) USING_XCODE_26"
            else
                config.build_settings['SWIFT_ACTIVE_COMPILATION_CONDITIONS'] = "$(inherited)"
            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
    end
    project.save if should_save
    # 修正Xcode16不能po问题
    fix_above_xcode16_no_po()
end