1
votes

I've got an Xcode project with a build phase that copies some files around. Say I want to copy all rtf files from [project directory]/foo/bar to [project directory]/bar/foo. I would use a command like this:

cp -v foo/bar/*.rtf bar/foo/

Executing this in Terminal does exactly what I expect: all rtf files in foo/bar are copied to bar/foo. But when building, I get a build error from cp:

Cp: foo/bar/*.rtf: No such file or directory

cp can't seem to find the files. It's as if bash isn't expanding the wildcard. I know I'm in the right directory when the script runs, because I've run pwd before cp and it gives what I expect. And I've set the Shell of the script phase to /bin/bash. What am I doing wrong? Can I not use globs in Run Script build phases? If so, why?

Update: I found out that you can turn off globbing in bash like this:

set -o noglob

And turn it back on like this:

set +o noglob

And see all bash options and their status like this:

set -o

I tried using the latter in my Run Script phase and it shows that noglob is disabled. So noglob is not the issue here.

2
The immediate problem is that foo/bar/*.rtf isn't matching anything, so it is passed as a literal string to cp. This is either because the current working directory is not [project directory], or because there are no RTF files in foo/bar. - chepner
@chepner I have verified that I am in the project directory as I expect (via pwd) and that there are RTF files in foo/bar. Executing the same command from the project directory in Terminal does what I expect. - Tom Hamming
@chepner I have also tried using a full path (~/Documents/Code/project/foo/bar/*.rtf). Same result. - Tom Hamming

2 Answers

2
votes

The issue can be revealed by looking at the build script execution from the build log (press the icon on the right of the build script line) and you will see that Xcode runs the script as:

/bin/sh -c \"cp -v foo/bar/*.rtf bar/foo/\"

and globbing doesn't work when enclosed in quotes or double-quotes.

I normally write a script to do that kind of stuff and run that instead. So create a tools folder, next to the .xcodeproj, and write the copy instructions in that script (tools/copy_pdf_files.sh):

#!/bin/sh
cp -v foo/bar/*.rtf bar/foo/

Make it executable from the command prompt:

cd /path/to/project
chmod +x tools/copy_pdf_files.sh

Then run that instead:

${PROJECT_DIR}/tools/copy_pdf_files.sh
0
votes

I had the paths for the cp arguments inside double quotes without realizing it, and globbing doesn't work in quotes. Problem solved.