How to write a Run Script in Xcode - xcode

In my project i have Frameworks different for iPhone Simulator and iPhone Device for example like in Rest kit "libRestKit_simulator.a" for simulator and "libRestKit.a" for device,
My requirement is to write a "Run Script" in Xcode to include the frameworks based on the Simulator or device.

Not sure if this is the best solution but it appears to work for me.
If you add a build script below target dependancies then add in the following code. This assumes you have 2 libraries
library.a
library_simulator.a
The project would be set up to use library.a and if you are using the simulator the script will remove library.a and replace it with library_simulator.a. You would need to add the simulator to your build scheme to make sure it is built when doing a build and both libraries would need the same interface.
if [[ "${SDKROOT}" == *Simulator* ]]
then
if [[ -f "${BUILT_PRODUCTS_DIR}/library.a"]]
then
rm -rf "${BUILT_PRODUCTS_DIR}/library.a"
fi
mv "${BUILT_PRODUCTS_DIR}/library_simulator.a" "${BUILT_PRODUCTS_DIR}/library.a"
fi
Not sure how much use this is to you, I only tested it briefly. Hope it helps.

Related

Xcode 11.2.1 - Command CodeSign failed with a nonzero exit code

I'm working on a SFML app for macOS with the latest Xcode version.
I got a "Command CodeSign failed with a nonzero exit code" error when it tries to sign the SFML frameworks. I added these under "Link Binary with Libraries" and "Copy Files" in Target, under Build Phases. Before, I was working with my local frameworks under /Library/Frameworks, and it was working very well.
Now I need to create an archive .app.
I've tried a lot of things seen on other posts here (like lock and unlock my keychains), but nothing worked. Still got that error.
My SFML Frameworks in "Link Binary with Libraries"
/Users/lounesksouri/Library/Developer/Xcode/DerivedData/Squadro-gqwjbvsooypqaifxxyzhdrahkdpo/Build/Intermediates.noindex/ArchiveIntermediates/Squadro/InstallationBuildProductsLocation/Applications/Squadro.app/Contents/Frameworks/sfml-system.framework/Versions/A: bundle format unrecognized, invalid, or unsuitable
Command CodeSign failed with a nonzero exit code
Ok, I solved my problem, after long hours.
It's a lot more simple to use the script already present in Build Phases tab, generated if you use the SFML App Xcode template (see here the original script). I had tried it at first, but it didn't work so I let it down.
But, in this script, the first three lines are not the good ones, for a normal SFML installation on macOS. We need to modify these lines with the good paths to the Frameworks and lib folders, as follows :
SFML_DEPENDENCIES_INSTALL_PREFIX="/Library/Frameworks"
CMAKE_INSTALL_FRAMEWORK_PREFIX="/Library/Frameworks"
CMAKE_INSTALL_LIB_PREFIX="/usr/local/lib"
FRAMEWORKS_FULL_PATH="$BUILT_PRODUCTS_DIR/$FRAMEWORKS_FOLDER_PATH/"
Secondly, there is still the code signing problem : the archive will created but we will get an error if we start the .app. The solution to this problem is to check Disable Library Validation under Target/Signing & Capabilities to allow the application to load plug-ins or frameworks signed by other developers.
Finally, if we create the archive and launch the application, everything goes normally, as planned.
I faced this problem with the new update of Xcode. I was reading your discussion, and did some accidental modifications. I am definetely not an expert but what I did fixed the problem so I did not receive tgis error anymore. So I added the sfml libraries (5 libraries), whose paths are written in build settings -> framework swarch paths. And this adding automatically added them in Embedded Frameworks under the Build phases section. What I did is unchecking the checkboxes of all 5 libraries "Code sign On copy". Thank you guys for giving me the path for "solving" the problem :))))
For the Code-signing issue:
Open a Terminal,
Go to the Framework folder (cd /Library/Frameworks) and sing them.
choose your identity
security find-identity -vp codesigning
sign them Frameworks!
codesign -s AD90FAFAFAFAFAFAFAF-YOURHASH-9D6 ogg.framework
codesign ... all the others
run and enjoy!

xcode 6 beta 7:A signed resource has been added modified or deleted.

When I'm running the application on device aftercleaning, removing derived data, first time it runs without any issues.
Second time when I'm trying to run, it say's "A signed resource has been added modified or deleted."
On the simulator app run's without problem.
I think it's because of extensions, I have editing, share and today extensions, when I'm deleting this extensions, then I can run second time without this message.
The problem has been since from xcode 6 beta 5.
Maybe issue related to provisioning profiles and signing ? I has created different provisioning profiles for each extension target.
So my question is, how can I fix this ? How can I run on the device second time without cleaning project?
It is still not fixed in XCode 6 RTM. But I found an easy walk around. Simply delete the ShareExtension.appex folder under your build. Then rebuild again. You don't need to clean project, so rebuild is quite fast.
A actually alias the command to do the cleaning
rm -rf ~/Library/Developer/Xcode/DerivedData/<your_app>-*/Build/Products/Debug-iphoneos/ShareExtension.appex/
I used a slight modification of Cloud Xu's script to delete both the .appex and .appex.dSYM
rm -rf ~/Library/Developer/Xcode/DerivedData/YourAppName-*/Build/Products/Debug-iphoneos/com.yourcompany.Name.extension.*
You can put this in your scheme so that it executes with every build:
Edit scheme... > Expand the Run mode in the sidebar > Pre-actions > Click '+' > New Run Script Action.
Edit:
There is an another workaround: for each extension target containing .swift file, add build pre-action in project running scheme configuration:
touch "${PROJECT_DIR}/SOME SWIFT FILE IN EXTENSION.SWIFT"
So I've found a workaround for this issue. For now we can't have swift code in extensions. As mentioned in the comment
When removed all extension targets that contains swift code it's started working normally. I think it's a bug in xcode, for now if we have extension with swift code, don't know why, but second time run gives "A signed resource has been added modified or deleted." error.

How to clean project before each build?

Is there a way (possibly using schemes) in Xcode to specify that a clean is automatically done before doing a new build.?
I have a project that sometimes fails to build unless I do a clean first, currently I am doing it by hand.
Press ⌥⌘R, expand the selected scheme, select Pre-actions, click +, select New Run Script Action, set Provide Build Settings from to your target. In the box below type rm -rf ${BUILT_PRODUCTS_DIR}. Note: it is BUILT not BUILD as seen in the Xcode dialog. You can type echo ${BUILT_PRODUCTS_DIR} > ~/Desktop/log.txt to see what's going to be deleted.
The selected answer did not work for me, it caused my build to fail (Xcode 4.6.3) when trying to run on the simulator.
Based on Jano's answer and on this link in the Pre-action script instead of writing
rm -rf ${BUILT_PRODUCTS_DIR}
I wrote
touch ${BUILT_PRODUCTS_DIR}
This should have the same effect and it doesn't cause my build to fail

Automate OSX Mac App Store builds

I'm looking for a way to automate the "Build and Archive" and "Organizer->Share.." for my OSX Mac App Store builds. I have read through "Xcode “Build and Archive” from command line" which is specific to iPhone development, and I'm hoping something similar can be done for my builds.
question from long ago.
It looks like now you can use Application Loader to upload binaries in addition to using XCode. Buried in the (PDF!) documentation for Application Loader is reference to a Unix build tool embedded deep in the package.
It means I can add some code to my build Makefile:
ALTOOL:="/Applications/Xcode.app/Contents/Applications/Application Loader.app/Contents/Frameworks/ITunesSoftwareService.framework/Versions/A/Support/altool"
$ALTOOL --validate-app -f Surfwriter.app.zip -u $APPSTORE_USERNAME -p $APPSTORE_PASSWORD
$ALTOOL --upload-app -f Surfwriter.app.zip -u $APPSTORE_USERNAME -p $APPSTORE_PASSWORD
Of course, before you submit Surfwriter.app.zip you have to build the binary and sign it (should be able to use xcodebuild for that)
This should theoretically do the trick for you.

Is it possible to automatically generate Xcode projects?

Simple question. Are there any tools for generating Xcode projects from the command line? We use SCons to build our cross-platform application, but that doesn't support intrinsic Xcode project generation. We'd like to avoid creating the project manually, since this would involve maintaining multiple file lists.
Look at CMake. You can generate XCode projects from it automatically. I found a previous StackOverflow question about its usage here. To get it to generate an XCode project, you use it as such:
CMake -G xcode
You can use premake (http://industriousone.com/premake) to generate Xcode projects. It can also generate Visual Studio projects.
For the benefit of anyone who lands on this question, I’ve actually just pushed an Xcode project file generator for SCons up to Bitbucket.
I think that your question should be "Is there a way to generate an XCode project from a SCons one?". I suppose, by your asking and by reading the others, that the answer is 'no'.
SCons people should know it better. I think they will be happy if you contribute a SCons Xcode project generator.
In the meantime you may choose to switch to CMake or to create your XCode project by hand that, given a good source tree organization, may be the best pragmatic solution.
qmake in the Qt toolchain generates Xcode projects. You can at least download it and take a look at its source here (LGPL).
You can generate a XCode project using the python based build system called waf. You need to download and install waf with the xcode6 extension:
$ curl -o waf-1.9.7.tar.bz2 https://waf.io/waf-1.9.7.tar.bz2
$ tar xjvf waf-1.9.7.tar.bz2
$ cd waf-1.9.7
$ ./waf-light --tools=xcode6
That will create a waf executable which can build your project. You need to configure how to generate your XCode project inside a file called wscript that should reside in your project folder. The wscript file uses Python syntax. Here's an example of how you could configure your project:
def configure(conf):
# Use environment variables to set default project configuration
# settings
conf.env.FRAMEWORK_VERSION = '1.0'
conf.env.ARCHS = 'x86_64'
# This must be called at the end of configure()
conf.load('xcode6')
# This will build a XCode project with one target with type 'framework'
def build(bld):
bld.load('xcode6')
bld.framework(
includes='include',
# Specify source files.
# This will become the groups (folders) inside XCode.
# Pass a dictionary to group by name. Use a list to add everything in one
source_files={
'MyLibSource': bld.path.ant_glob('src/MyLib/*.cpp|*.m|*.mm'),
'Include': bld.path.ant_glob(incl=['include/MyLib/*.h', 'include'], dir=True)
},
# export_headers will put the files in the
# 'Header Build Phase' in Xcode - i.e tell XCode to ship them with your .framework
export_headers=bld.path.ant_glob(incl=['include/MyLib/*.h', 'include/MyLib/SupportLib'], dir=True),
target='MyLib',
install='~/Library/Frameworks'
)
There are a bunch of settings you can use to configure it for your project.
Then to actually generate the XCode project, cd into your project folder where the wscript is and run your waf executable like
$ ./waf configure xcode6
A promising alternative to CMake which can generate Xcode projects is xmake. I haven’t tried it yet, but it looks good from the documentation.
Install xmake, create a simple project file (xmake.lua):
target("test")
add_files("src/*.cpp")
Then you can either do a command-line build:
xmake
or create an Xcode project:
xmake project -k xcode
Note that currently xmake seems to invoke CMake to generate the Xcode project, although they say they plan to add native Xcode project generation at some point.
You could use Automator to generate them for you.
I checked and there is no prebuilt action.
Therefore you would have to record your actions with Automator to do this.

Resources