xcodebuild manually building framework in run-script and follow same destination - ios

I have a framework that I need to build manually as a part of my run-script in xcode.
How can I copy the build setting from the current project when building it?
xcodebuild
-project "$PROJECT_DIR/myProject/MyProject.xcodeproj"
-scheme MyProject
-configuration "$MY_PROJECT_CONFIGURATION"
CONFIGURATION_BUILD_DIR="$APP_BUILD_DIR"
build
I'm specifically having problem matching -destination. Is there a way to get the -destination used to build the current project and pass it to xcodebuild

Xcode offers this as a Environmental Variable:
build_dir=${BUILD_DIR}
//use it
... -destination $build_dir ...
This answer shows how to get all Environmental Variables.

Related

How can I build the Xcode-generated Swift Package Workspace with xcodebuild?

When you open a Package.swift file, Xcode will generate a workspace with the path: .swiftpm/xcode/package.xcworkspace.
I'd like to use xcodebuild to build this but it does not seem to work. The error messages are not helpful, just tells me that clang failed.
The actual command I'm running is:
xcodebuild -workspace .swiftpm/xcode/package.xcworkspace -scheme MyLibrary-Package -sdk iphonesimulator OTHER_SWIFT_FLAGS="-D SWIFT_PACKAGE"
Funny enough, trying to build a scheme that describes an individual target works:
xcodebuild -workspace .swiftpm/xcode/package.xcworkspace -scheme SpecificTarget -sdk iphonesimulator OTHER_SWIFT_FLAGS="-D SWIFT_PACKAGE"
Am I missing some build flags needed to build the combination scheme? Has anyone else struggled with this and have any advice?
Note:
The ultimate goal here is to be able to verify that my Swift Package builds in CI. If there is an easier way to achieve this goal then I'm happy to go another route. (The easier might just be multiple commands to build the individual targets but this is less robust)

Build all targets (or schemes) in Xcode project

I have an Xcode project with many targets. Six of them are aggregates which build final release products (static libraries, frameworks) using Run Scripts under Build Phases. I can build them each individually fine, but I can't find any way to hit "one button" to build them all.
Approach #1
First I tried using -alltargets from the command line, like so:
xcodebuild -project MyProject.xcodeproj -alltargets
With that I get errors on my test targets, claiming that they aren't built for testing. I don't know what that means because they normally "test" correctly. Something is different when attempted this way. But technically it's including targets I'm not interested in. I wouldn't mind much if it worked.
Approach #2
Next I tried making an aggregate which had a run script that individually built each aggregate target, like so:
xcodebuild -project MyProject.xcodeproj -target FirstAggregateTarget
xcodebuild -project MyProject.xcodeproj -target SecondAggregateTarget
xcodebuild -project MyProject.xcodeproj -target ThirdAggregateTarget
xcodebuild -project MyProject.xcodeproj -target FourthAggregateTarget
xcodebuild -project MyProject.xcodeproj -target FifthAggregateTarget
xcodebuild -project MyProject.xcodeproj -target SixthAggregateTarget
It doesn't get any errors from Xcode's point of view, but several of the aggregates just don't build properly. Somehow the run scripts in the individual aggregates were influenced by the top-level aggregate, I guess.
Approach #3
Next I then tried making a new "RELEASE_PRODUCTS" scheme that in the build section had the six aggregates listed. With that I got errors like this:
There were also other obscure errors about build products not being found where they were expected to be.
Approach #4
Next I created a script that I run completely outside of Xcode, like so:
#!/bin/bash
# Builds all release products
xcodebuild -project MyProject.xcodeproj -target FirstAggregateTarget
xcodebuild -project MyProject.xcodeproj -target SecondAggregateTarget
xcodebuild -project MyProject.xcodeproj -target ThirdAggregateTarget
xcodebuild -project MyProject.xcodeproj -target FourthAggregateTarget
xcodebuild -project MyProject.xcodeproj -target FifthAggregateTarget
xcodebuild -project MyProject.xcodeproj -target SixthAggregateTarget
That seems to be the only thing that works. But I wish I could get this to work from within Xcode, preferably as something I could hit from the command line if I wanted to, because then I wouldn't have to leave the IDE and it could report to be success or fail.
You could make a dummy target that depends on the other six. Use Target Dependencies in the Build Phases tab
Inspired by sansumbrella's answer in another forum.
Create a new target in your project (I chose to use a CLI tool since my project is CLI based).
According to sansumbrella, you can create an application and delete its plist. My approach does not rely on any special (or non-existent) plists.
Give your target a name, such as ALL, satisfy the remaining fields, and press "Finish".
Click on the new target's Build Phases link and:
Delete the entry in the Compile Sources, which will be main.c if you're doing it how I did.
Open the Target Dependencies and add all the other targets by clicking the + or dragging targets into this space.
You can now build and clean your whole project when you have this target selected as the active schema.

xcodebuild - how to use build settings from xcode

I'm trying to build several targets and I already have Build Settings per target in Xcode. I also have a scheme per target. What I'm trying to do is issue one command line instruction to build each target, something like:
xcodebuild -project MyProject.xcodeproj -scheme PRODScheme archive
-archivePath /Users/myUser/Documents/PRODApps/archives/"${PRODUCT_NAME}".xcarchive
-configuration Release
Here, ${PRODUCT_NAME} is the Build Setting variable defined by XCode. I'm thinking on how xcodebuild can make use of the bunch of already defined settings.
Is there a way of reusing those settings in xcodebuild?
Typically you would use the scheme, which would contain the other information you are trying to include:
xcodebuild -scheme PRODScheme build
To use with a configuration file you would use:
xcodebuild -target MyProject.xcodeproj -xcconfig configuration.xcconfig
To build all targets use -alltargets
Command Line tech notes: https://developer.apple.com/library/ios/technotes/tn2339/_index.html
Man xcodebuild: https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/xcodebuild.1.html

Choose or get xcodebuild destination directory for app

Is there a way I can choose the destination folder (directory) for when I build an iOS app via command line using xcodebuild?
Right now I am using a command like
xcodebuild -sdk iphonesimulator -workspace /<path_to_project>/ios-app/CompanyName-iOS.xcworkspace -scheme AppName -configuration Debug RUN_APPLICATION_TESTS_WITH_IOS_SIM=YES ONLY_ACTIVE_ARCH=NO clean build 2>&1
In the output I see something like
GenerateDSYMFile /Users/<username>/Library/Developer/Xcode/DerivedData/CompanyName-iOS-fcvhojqctgtmvgdaavahmjutbfyy/Build/Products/Debug-iphonesimulator/AppNAme.app.dSYM
Is there a way I could get this location where the app is output to or even specify where I would rather have the app be sent to? I need access to the app to run Appium selenium tests and just having a build execute and run tests is not helpful. Also trying to incorporate jenkins into the mix and need to have commands to fully automate the process
Set the CONFIGURATION_BUILD_DIR Build Setting to be the directory you want.
https://developer.apple.com/library/mac/documentation/DeveloperTools/Reference/XcodeBuildSettingRef/1-Build_Setting_Reference/build_setting_ref.html#//apple_ref/doc/uid/TP40003931-CH3-SW40
So in your example:
xcodebuild ...cut... CONFIGURATION_BUILD_DIR=<desired dir> ...cut...
use -derivedDataPath path to get compiled output onto specific folder.
xcodebuild -scheme xxx -workspace xxx.xcworkspace ONLY_ACTIVE_ARCH=NO -sdk iphonesimulator -configuration Debug -derivedDataPath ./build build
https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/xcodebuild.1.html

Building/running iOS app from command line

I am trying to build/run my iOS app from command line. To build the app, the command which I give in shell script is as follows:
PROJECT_DIR="/Users/ingenyous/Desktop/splistV11_test_sh_build/code/app/ios"
TARGET_NAME="splistapp2"
TARGET_SDK="iphoneos5.1"
PROJECT_BUILD_DIR="${PROJECT_DIR}/build/Release-iphoneos"
OUTPUT_DIR="/Users/ingenyous/Desktop/ipa"
# compile project
echo Building Project
cd "${PROJECT_DIR}"
xcodebuild -target "${TARGET_NAME}" -sdk "${TARGET_SDK}" -configuration Release
Here I specify the target name and other parameters. Is it possible to give the target properties like product name and other options in this script itself, which can overwrite the properties given actually in the editor target. or be able to create the target itself from shell script and not needing to create targets from Xcode editor.
You can certainly set the product name using xcodebuild:
xcodebuild -target "${TARGET_NAME}" -configuration Release build PRODUCT_NAME=MyProduct
There can be issues with multiple targets, see this existing SO question
You can set any build setting in this way. A full list of build settings is available on Apple's developer site, and you can also check out the man for xcodebuild.

Resources