XCode C / C ++标志Bash脚本

时间:2013-02-04 03:40:26

标签: xcode xcode4

我试图在XCode上运行以下命令,将其添加为C / C ++标志,我可以从我在项目的运行脚本阶段执行的shell脚本获取内部版本号。

这与其他Unix系统上的GCC一起工作正常:

-D__BUILD_VERSION = $(cat build_number)

好的XCode我试图使用以下内容:

-D__BUILD_VERSION = $(cat $ PROJECT_DIR / build_number)

但它不起作用,我做错了什么?在XCode中,如何将cat build_number的结果分配给__BUILD_VERSION定义的变量?

3 个答案:

答案 0 :(得分:1)

如果你试图在Xcode编译构建阶段设置该值,你可能会遇到麻烦,因为我不知道任何解释操作是在你尝试设置你的方式的设置下进行的试图设置它们。

自动设置版本号,我有一个更复杂的半自动版本和自动编号方案,所以我不必记得改变,或者我可以给我想要的版本号但总是递增内部版本号,在这两种情况下,它都会将内部版本号放在iOS系统设置中显示的应用程序设置的“关于”框中。

你可能不需要太多,但是有一些技巧可以获取和编写你可能觉得有用的信息,并可能为你的问题找到解决方案。


以下脚本受到堆栈溢出答案的启发,如何执行此操作,目前我无法找到。我投入了更多的工作,因为(a)我希望版本号显示在系统设置中显示的设置中; (b)Xcode缓存Info.plist文件的内容,因此这样做并不像我预期的那样简单。

在编译之前的构建阶段,我运行以下内容(仅在安装时运行脚本 取消选中

sh xolawareStashProductSettings.sh

xolawareStashProductSettings.sh的内容检查info.plist文件的git状态,如果不清理,暂时将其存放在一边以备日后恢复。

#!/bin/sh

#
# should be run prior to the Copy Bundle Resources step
# and prior to any version information modifier scripts

INFOPLIST_GIT_PATH=${PROJECT}/`basename ${INFOPLIST_FILE}`
echo "-- Temp Hold ${INFOPLIST_GIT_PATH} Script --"

set -e

# a fallback in case the user has made changes to the file
if [ `git status --porcelain ${INFOPLIST_GIT_PATH} ]|wc -l` -gt 0 ]; then
    echo cp -p ${INFOPLIST_GIT_PATH} ${TARGET_TEMP_DIR}
    cp -p ${INFOPLIST_GIT_PATH} ${TARGET_TEMP_DIR}
fi

脚本#2(仅在安装时运行脚本 取消选中):

sh xolawareStashSettingsBundleRootPlist.sh

xolawareStashSettingsBundleRootPlist.sh的内容类似于脚本1的内容。

#!/bin/sh
#
# should be run prior to the Copy Bundle Resources step
# and prior to any version information modifier scripts

echo '-- Temp Hold Settings.bundle/Root.plist Script --'

ROOT_PLIST=${PROJECT}/Resources/Settings.bundle/Root.plist

set -e

# a fallback in case the user has made changes to the file
if [ `git status --porcelain ${ROOT_PLIST} ]|wc -l` -gt 0 ]; then
    echo cp -p ${ROOT_PLIST} ${TARGET_TEMP_DIR}
    cp -p ${ROOT_PLIST} ${TARGET_TEMP_DIR}
fi

脚本#3(仅在安装时运行脚本 选中

sh xolawareIncrementProductSettingsBuildNumber.sh

xolawareIncrementProductSettingsBuildNumber的内容使用plistbuddy查看它是什么并将其碰到一个:

#!/bin/sh
#
# this should be prior to xolawareAboutInfoVersionInfoInSettings.sh

echo "-- Auto-Increment ${INFOPLIST_FILE} Build Version Install Script --"

PLISTBUDDYCMD="/usr/libexec/PlistBuddy -c"
CONFIGURATION_BUILD_SETTINGS_PATH=${CONFIGURATION_BUILD_DIR}/${INFOPLIST_PATH}

CFBV=$(${PLISTBUDDYCMD} "Print :CFBundleVersion" ${PRODUCT_SETTINGS_PATH})
if [[ "${CFBV}" == "" ]]; then
    echo "No build number in ${PRODUCT_SETTINGS_PATH}"
    exit 2
fi

CFBV=$(expr $CFBV + 1)

set -e
echo ${PLISTBUDDYCMD} "Set :CFBundleVersion $CFBV" "${PRODUCT_SETTINGS_PATH}"
${PLISTBUDDYCMD} "Set :CFBundleVersion $CFBV" "${PRODUCT_SETTINGS_PATH}"
echo ${PLISTBUDDYCMD} "Set :CFBundleVersion $CFBV" "${CONFIGURATION_BUILD_SETTINGS_PATH}"
${PLISTBUDDYCMD} "Set :CFBundleVersion $CFBV" "${CONFIGURATION_BUILD_SETTINGS_PATH}"

脚本#4(仅在安装时运行脚本 取消选中

sh xolawareProductSettingsShortVersion-from-git.sh
sh xolawareAboutInfoVersionInfoInSettings.sh

xolawareProductSettingsShortVersion-from-git的内容依赖于我在git中正确标记我的分支,但是如果我忘记了,它将使用自上次提交以来为我自动编译我的构建的提交数量。

#!/bin/sh
#
# this should be run after xolawareStashSettingsBundleRootPlist.sh
# and prior to xolawareAboutInfoVersionInfoInSettings.sh

echo '-- Get Product Settings Short Version String from git describe --'

PLISTBUDDYCMD="/usr/libexec/PlistBuddy -c"
CONFIGURATION_BUILD_SETTINGS_PATH=${CONFIGURATION_BUILD_DIR}/${INFOPLIST_PATH}

CFBVS=`git describe|awk '{split($0,a,"-"); print a[1]}'`
CFBVSI=`git describe|awk '{split($0,a,"-"); print a[2]}'`
if [[ "$CFBVSI" != "" ]]; then
    CFBVS=${CFBVS}.${CFBVSI}
fi

set -e
echo ${PLISTBUDDYCMD} "Set :CFBundleShortVersionString $CFBVS" "${PRODUCT_SETTINGS_PATH}"
${PLISTBUDDYCMD} "Set :CFBundleShortVersionString $CFBVS" "${PRODUCT_SETTINGS_PATH}"
echo ${PLISTBUDDYCMD} "Set :CFBundleShortVersionString $CFBVS" "${CONFIGURATION_BUILD_SETTINGS_PATH}"
${PLISTBUDDYCMD} "Set :CFBundleShortVersionString $CFBVS" "${CONFIGURATION_BUILD_SETTINGS_PATH}"

xolawareAboutInfoVersionInfoInSettings.sh的内容将内容放在我想要的Root.plist的About框中。它依赖于About框,这是您的settings.bundle的Root.plist中的第一个内容:

#!/bin/sh
#
# this should be invoked after xolawareStashInfoAndRootPlist.sh,
# xolawareIncrementProductSettingsBuildNumber.sh and 
# xolawareProductSettingsShortVersion-from-git.sh, and before
# the regular Copy Bundle Resources Build Phase

echo '-- Auto-Insert Version Info In System Settings Script --'

PLISTBUDDYCMD="/usr/libexec/PlistBuddy -c"
ROOT_PLIST=${PROJECT_DIR}/${PROJECT}/Resources/Settings.bundle/Root.plist

CFBSVS=`exec -c ${PLISTBUDDYCMD} "Print :CFBundleShortVersionString" ${PRODUCT_SETTINGS_PATH}`
CFBV=`exec -c ${PLISTBUDDYCMD} "Print :CFBundleVersion" ${PRODUCT_SETTINGS_PATH}`

set -e
echo ${PLISTBUDDYCMD} "Set :PreferenceSpecifiers:1:DefaultValue '${CFBSVS} (b${CFBV})'" ${ROOT_PLIST}
${PLISTBUDDYCMD} "Set :PreferenceSpecifiers:1:DefaultValue '${CFBSVS} (b${CFBV})'" ${ROOT_PLIST}

还有一些清理脚本在Compile,Link& amp;之后运行。复制捆绑资源阶段

sh xolawareStashRestoreSettingsBundleRootPlist.sh

这可能没有必要,但我调整了Root.plist中的其他项目以用于发布版本,因此这将恢复调试版本的这些设置。

#!/bin/sh
#
# should be run as the second to last script in Build Phases, after the Copy Bundle Resources Phase

echo "-- Manual Restore $INFOPLIST_FILE Script --"

ROOT_PLIST=${PROJECT}/Resources/Settings.bundle/Root.plist

set -e

# first, see if it was stashed earlier due to uncommitted changes
if [ -e ${TARGET_TEMP_DIR}/Root.plist ]; then
    echo mv ${TARGET_TEMP_DIR}/Root.plist ${ROOT_PLIST}
    mv ${TARGET_TEMP_DIR}/Root.plist ${ROOT_PLIST}

# the better option when available: restore to the pristine state
elif [ `git status --porcelain ${ROOT_PLIST}|wc -l` -gt 0 ]; then
    echo git checkout -- ${ROOT_PLIST}
    git checkout -- ${ROOT_PLIST}
fi

最后,如果我现在还没有标记该项目,则自动标记git repo的步骤:

sh xolawareProductSettings-git-commit-and-tag.sh


#!/bin/sh
#
# this should be run after xolawareAboutInfoVersionInfoInSettings.sh
# and xolawareProductSettingsShortVersion-from-git.sh

echo "-- ${INFOPLIST_FILE} git commit & tag Install Script --"

SCRIPT_VERSION=`/usr/libexec/PlistBuddy -c 'Print :CFBundleShortVersionString' ${INFOPLIST_FILE}`
SCRIPT_BUILD_NUMBER=`/usr/libexec/Plistbuddy -c 'Print :CFBundleVersion' ${INFOPLIST_FILE}`
if [ `git status --porcelain ${SCRIPT_INFO_PLIST}|wc -l` -gt 0 ]; then
    echo git commit -m '"'version ${SCRIPT_VERSION} build ${SCRIPT_BUILD_NUMBER}'"' ${INFOPLIST_FILE}
    git commit -m "version ${SCRIPT_VERSION} build ${SCRIPT_BUILD_NUMBER}" ${INFOPLIST_FILE}
fi
echo git tag -f ${SCRIPT_VERSION}
git tag -f -F /dev/null ${SCRIPT_VERSION}

答案 1 :(得分:0)

尝试:

-D__BUILD_VERSION=`cat $PROJECT_DIR/build_number`

请注意“反引号” - 它们不是常规的单引号字符。

答案 2 :(得分:0)

一种可能性是让第一个脚本将内部版本号放入环境变量中?不确定这是否有效但可能会有效。