Pod子模块工程编译宏设置

在Swift项目的模块化项目开发中,有个独立的网络子模块,采用了宏定义的方式来隔离服务器环境。在主工程(后续均称为A工程)的Configurations设置了多个config,分别是Debug、Alpha、Beta、Release。 A工程配Target配置如下图:


截屏2022-03-15 下午5.41.09.png

但是,现在遇到了一个问题,在采用不同config进行编译,项目始终读取的是DEBUG状态下的网络地址。开始时,以为是子项目中没有相关的配置,于是在Podfile中添加了如下脚本:

  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      if config.name == 'Alpha'
        config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)','ALPHA=2']
      elsif config.name == 'Beta'
        config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)','BETA=3']
      elsif config.name == 'Release'
        config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)','RELEASE=4']
      end
    end
  end
end

但是,在做了此工作之后,才发现改方法仍然不起作用。后来查询资料说是Swift是不支持宏的,需要在Other Swift Flags中添加定义。于是修改如下:

post_install do |installer|
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      if config.name == 'Alpha'
        config.build_settings['OTHER_SWIFT_FLAGS'] = '$(inherited) -D ALPHA'
        config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)','ALPHA=2']
      elsif config.name == 'Beta'
        config.build_settings['OTHER_SWIFT_FLAGS'] = '$(inherited) -D BETA'
        config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)','BETA=3']
      elsif config.name == 'Release' 
        config.build_settings['OTHER_SWIFT_FLAGS'] = '$(inherited) -D RELEASE'
        config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)','RELEASE=4']
      else        
        config.build_settings['OTHER_SWIFT_FLAGS'] = '$(inherited) -D DEBUG'
      end
    end
  end
end

但是,运行到最后发现仍不生效。而在网络溜达了一圈,发现没有其他跟后的思路,索性回到项目本省的主工程。于是想着,主工程保持与主工程相同的配置方式应该可以吧。于是,有了如下的配置修改:

post_install do |installer|
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      if config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'].to_f < 11.0
        config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '11.0'
      end
      if config.name == 'Alpha'
        config.build_settings['SWIFT_ACTIVE_COMPILATION_CONDITIONS'] = 'ALPHA'
        config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)','ALPHA=2']
      elsif config.name == 'Beta'
        config.build_settings['SWIFT_ACTIVE_COMPILATION_CONDITIONS'] = 'BETA'
        config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)','BETA=3']
      elsif config.name == 'Release'
        config.build_settings['SWIFT_ACTIVE_COMPILATION_CONDITIONS'] = 'RELEASE'
        config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)','RELEASE=4']
      else
        config.build_settings['SWIFT_ACTIVE_COMPILATION_CONDITIONS'] = 'DEBUG'
      end
    end
  end
end

好家伙,成功了。回过头来,看看配置。
SWIFT_ACTIVE_COMPILATION_CONDITIONS本身表示的是激活SWIFT条件编译。所以,改方法能成功也是预料之中的。

你可能感兴趣的:(Pod子模块工程编译宏设置)