如何在 Xcode 中根据构建配置有条件地包含文件? [英] How can I conditionally include a file based on build configuration in Xcode?

查看:24
本文介绍了如何在 Xcode 中根据构建配置有条件地包含文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含大量目标的 Xcode 项目,我想在其中包含在 Ad-hoc 和 Debug 配置下构建的应用程序的设置包,但不在发布配置下.

I have an Xcode project with a large number of targets where I would like to include a settings bundle for apps built under the Ad-hoc and Debug configurations, but not under the Release configuration.

构建阶段似乎不允许让自己以配置为条件(它们显然可以以目标为条件,但项目中的目标数量加倍会使其完全无法使用).

Build Phases don't seem to allow for making themselves conditional on configuration (they can obviously be conditional on target, but doubling the number of targets in the project would make it completely unusable).

剩下的就是编写自定义构建规则了.我的计划是从所有目标中排除 Settings.bundle,并创建一个构建规则,有条件地将其复制到产品包中,但适用的示例确实很难找到.

That leaves writing a custom Build Rule. My plan is to exclude the Settings.bundle from all targets, and create a build rule that conditionally copies it into the product package, but applicable examples are really hard to find.

我开始的构建规则将 Process 设置设置为名称匹配的源文件:"和 Settings.bundle 作为名称.使用设置是自定义脚本:".

The build rule I've started has the Process setting set to "Source files with names matching:" and Settings.bundle as the name. The Using setting is "Custom script:".

我的自定义脚本如下(需要注意的是,我的 bash 脚本处于货物崇拜级别):

My custom script is as follows (with the caveat that my bash scripting is on a cargo cult level):

if [${CONFIGURATION} = 'Debug'] then
    cp -r ${INPUT_FILE_PATH} ${DERIVED_FILES_DIR}/.
fi

最后,我将 ${DERIVED_FILES_DIR}/Settings.bundle 列为输出文件.

Finally, I have ${DERIVED_FILES_DIR}/Settings.bundle listed as an output file.

既然我在这里,很明显它不起作用.我的第一个问题是,是否可以在某处将构建规则的输出视为执行,以确保 1)它实际上正在执行,并且 2)我在某处没有愚蠢的语法错误.

Since I'm here, it should be obvious that it's not working. My first question is whether there is somewhere I can view the output of the build rules as the execute to make sure that 1) it's actually being executed and that 2) I don't have a stupid syntax error somewhere.

另外,将输出复制到的正确位置(以环境变量的形式)是什么?

Also, what's the proper location (in the form of an environment variable) to copy the output to?

推荐答案

我终于想通了.

对于您想要有条件地包含设置包的每个目标,从源列表中选择其项目,选择目标,然后切换到构建阶段"选项卡.

For each target for which you want to conditionally include the settings bundle, choose its Project from the source list, choose the target, and switch to the "Build Phases" tab.

单击添加构建阶段"按钮并选择添加运行脚本".

Click the "Add Build Phase" button and choose "Add Run Script".

然后为脚本输入以下内容:

Then enter the following for the script:

if [ "${CONFIGURATION}" == "Debug" ]; then
    cp -r "${PROJECT_DIR}/Settings.bundle" "${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app"
fi

这篇关于如何在 Xcode 中根据构建配置有条件地包含文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆