Xcode C / C ++ Bash Script Flags

I am trying to run the following command in Xcode, adding it as a C / C ++ flag so that I can get the build number coming from the shell script that is executed at the script stage of my project.

This works fine with GCC on another Unix system:

-D__BUILD_VERSION = $ (cat build_number)

And well, Xcode Im trying to use the following:

-D__BUILD_VERSION = $ (cat $ PROJECT_DIR / build_number)

But this does not work, what am I doing wrong? In Xcode, how can I assign the result of cat_type_type_number to a specific __BUILD_VERSION variable?

+4
source share
3 answers

if you try to set this value in the build phase of the Xcode compilation, you may have problems, since I do not know that any interpreting operation is performed with the settings that you are trying to configure as you are trying to configure them.

to automatically set the version number, I have a much more complicated scheme with a semi-automatic version and automatic numbering, so I don’t need to remember the change, or I can give the version number that I want but always increase the build number, and in both cases it will put build number in the "About" field in the application settings, which are displayed in the iOS system settings.

you may not need most of this, but there are a few tricks to getting and recording information that may be useful and may lead to a solution to your problem.


The following scenarios were inspired by the response to a stack overflow, how to do this, which I cannot find at the moment. I added a little more work, because (a) I want the version number to be displayed in the settings displayed in the system settings; and (b) Xcode caches the contents of the Info.plist file, so this is not as easy as I expected.

in the build phase that appears before compilation, I run the following (since running the script only during installation is unchecked)

sh xolawareStashProductSettings.sh 

the contents of xolawareStashProductSettings.sh checks the status of the git file of the info.plist file, and if it is not clean, temporarily set it aside for later recovery.

 #!/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 

script # 2 (with running the script only during installation is not checked):

 sh xolawareStashSettingsBundleRootPlist.sh 

the contents of xolawareStashSettingsBundleRootPlist.sh are similar to the contents of script 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 

script # 3 (with Run script only during installation )

 sh xolawareIncrementProductSettingsBuildNumber.sh 

the contents of xolawareIncrementProductSettingsBuildNumber uses plistbuddy to see what it is and raise it by one:

 #!/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}" 

script # 4 (since running the script only during installation is not installed)

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

the contents of xolawareProductSettingsShortVersion-from- git relies a bit on the fact that I mark my branch in git accordingly, but if I forget, it will use the number of commits since the last commit to automatically version my build for me.

 #!/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}" 

contents xolawareAboutInfoVersionInfoInSettings.sh put the contents in the "O" field in my Root.plist as I want. it relies on the “O” field, which is the first of your settings in your Root.plist. bundle:

 #!/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} 

There are also several cleanup scripts that will run after the compilation, linking, and copying resource phases

 sh xolawareStashRestoreSettingsBundleRootPlist.sh 

it may not be necessary, but I'm setting up other elements in Root.plist to create releases, so it restores these settings for debugging collections.

 #!/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 

and finally, the step of the automatic git repo tag, if I haven’t marked the element myself now:

 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
source

Try:

 -D__BUILD_VERSION=`cat $PROJECT_DIR/build_number` 

Note the "backticks" - they are not regular single quote characters.

0
source

One possibility could be for the first script to put the build number in an environment variable? Not sure if this will work, but it can.

0
source

All Articles