Clean IOS xcode target in command line - ios

I am building/running an IOS app from command line with following commands:
xcodebuild -sdk "${TARGET_SDK}" -xcconfig "${CONFIG_FILE_PATH}" -configuration Release
/usr/bin/xcrun -sdk "${TARGET_SDK}" PackageApplication -v "${PROJECT_BUILD_DIR}/${APPLICATION_NAME}.app" -o "${OUTPUT_DIR}/${APPLICATION_NAME}.ipa"
Is there any command to clean the targets. or these commands take care of cleaning themselves.

From xcodebuild manual page:
xcodebuild [-project projectname] [-target targetname ...] [-configuration configurationname] [-sdk [sdkfullpath | sdkname]] [buildaction ...] [setting=value ...] [-userdefault=value ...]
and build action could take following values (UPD 13.08.2018):
action ...
Specify one or more actions to perform. Available actions are:
build Build the target in the build root (SYMROOT). This is the default action, and
is used if no action is given.
build-for-testing Build the target and associated tests in the build root (SYMROOT). This will
also produce an xctestrun file in the build root. This requires specifying a
scheme.
analyze Build and analyze a target or scheme from the build root (SYMROOT). This
requires specifying a scheme.
archive Archive a scheme from the build root (SYMROOT). This requires specifying a
scheme.
test Test a scheme from the build root (SYMROOT). This requires specifying a
scheme and optionally a destination.
test-without-building Test compiled bundles. If a scheme is provided with -scheme then the command
finds bundles in the build root (SRCROOT). If an xctestrun file is provided
with -xctestrun then the command finds bundles at paths specified in the
xctestrun file.
install-src Copy the source of the project to the source root (SRCROOT).
install Build the target and install it into the target's installation directory in
the distribution root (DSTROOT).
clean Remove build products and intermediate files from the build root (SYMROOT).
In your case
xcodebuild -sdk "${TARGET_SDK}" -xcconfig "${CONFIG_FILE_PATH}" -configuration Release clean build
There are two build actions in this command line: 'clean' and 'build'. The 'clean' action is performed first, then the 'build'. As the documentation states, you can specify multiple build actions in a command, and doing so rather than using separate commands ensures that the other options are the same for all the build actions.

You can just delete build folder:
rm -rf ./build

Related

What could cause code coverage enabled builds to fail?

Summary
I'm trying to get a code coverage report from my project. However when I try to build and run tests, there is no CodeCoverage folder created in the derived data folder.
How am I trying to do this?
I am running xcodebuild as follows:
xcodebuild \
-workspace <some_workspace> \
-scheme <some_scheme> \
-destination <some_destination> \
-derivedDataPath <some_path> \
-enableCodeCoverage YES \
build test
What is the problem?
For my workspace/project it fails at the very end with a line:
xcodebuild: error: Failed to build workspace <some_workspace> with scheme <some_scheme>.
Reason: Could not determine bundle identifier for <some_test_target>'s TEST_HOST: "<some_path>/Build/Intermediates/CodeCoverage/Products/Debug-iphonesimulator/<some_product>.app"
At first it seemed directly linked to the TEST_HOST issue, but that's not the case here.
There is simply no CodeCoverage folder:
<some_path>/Build/Intermediates/CodeCoverage
What have I tried?
I tried the same with a clean new project, same running the same xcodebuild command, which succeeds. Within the Build/Intermediates/ folder exists the CodeCoverage folder.
However, in my own project, which is more complex, the Build/Intermediates/ folder contains a bunch of *.build folders (related to the app and the various frameworks, HockeySDK.build for example) and PrecompiledHeaders but no CodeCoverage folder.
When looking through the output of xcodebuild I never see a reference to the CodeCoverage folder for my project. However, for the test project, the first mention is at:
Write auxiliary files
/bin/mkdir -p /<some_path>/Build/Intermediates/CodeCoverage/Intermediates/<some_project>.build/Debug-iphonesimulator/<some_project>.build/Objects-normal/x86_64
For my project I see:
Write auxiliary files
/bin/mkdir -p /<some_path>/Build/Intermediates/<some_project>.build/Debug-iphonesimulator/<some_project>.build/Objects-normal/x86_64
Workaround
Something else that seemed to 'trick' it into working is to set the Host Application to None. After doing this it builds and tests, but tests fail due to the lack of host. However, this causes the CodeCoverage folder to be created, and if the Host Application is correctly set again, running the build and tests works fine, with a code coverage report produced.
You have to assure the package name is equal in all configurations. Xcode modifies it in case you are using unsupported characters. For instance Xcode replaces "-" with "_".
In all configurations, go to:
Project -> Build Settings -> Product Module
and set the exact same name without spaces.

xctool build failure: Test bundle not found under `Debug-iphonesimulator`

I'm experiencing failures while running xctool build. My command is as follows:
xctool clean build run-tests -only 'MyApp KIF Tests' -workspace MyApp.xcworkspace/ -scheme 'Debug' -destination "platform=iOS Simulator,name=iPhone 6,OS=latest" -sdk iphonesimulator -reporter junit -reporter plain
The build fails with the following error:
Failed to query the list of test cases in the test bundle: Test bundle not found at: /Users/myusername/Library/Developer/Xcode/DerivedData/MyApp-blbvnisblktossbxwcmnxafkngts/Build/Products/Debug-iphonesimulator/MyApp KIF Tests.xctest
I looked up the MyApp KIF Tests.xctest in the path specified in the error message, and indeed I couldn't find that file. However, this file does exist under a different path:
/Users/myusername/Library/Developer/Xcode/DerivedData/MyApp-blbvnisblktossbxwcmnxafkngts/Build/Products/Debug-iphoneos/MyApp KIF Tests.xctest
The only difference between these two paths is that one is under Debug-iphonesimulator, and the other is under Debug-iphoneos.
I also noticed that under target -> Build Settings -> Build Locations -> Per-configuration Build Products Path -> Debug, the value is $(BUILD_DIR)/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME), which was evaluating to build/OktaVerify.build/Debug-iphoneos. So the first question is: why does $(EFFECTIVE_PLATFORM_NAME) evaluate to Debug-iphoneos? What config do I need to change to make it evaluate to Debug-iphonesimulator so that xctool can find it?
Next, I hardcoded the value of $(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME) to Debug-iphonesimulator, wishing to see it working. However, I got the same error. My second question is, why does the xctest file appear under Debug-iphoneos folder even the build products path specifies Debug-iphonesimulator? What setting is specifying Debug-iphoneos?
Environment is Xcode 7.2 and xctool version 0.2.8.
I solved the issue by removing "-sdk iphonesimulator" from the xctool command. This way it'll automatically choose the appropriate sdk depending on the target type.

xcodebuild archives all targets instead of one

I need to archive a specific scheme from my xcodeproj. To do this I run
xcodebuild archive \
-scheme $SCHEME \
-target $TARGET \
-archivePath $ARCHIVE_PATH
Although I explicitly define a scheme and target, the resulting xcarchive contains both targets and the follow-up IPA export call complains with
error: the archive at path '<path>.xcarchive' is not a single-bundle archive
** EXPORT FAILED **
Why does xcodebuild produce an archive with multiple bundles?
It was a problem with the project's schemes. I created new Schemes by duplicating the original one instead of adding a new one. The resulting scheme then listed 2 targets in the build section instead of one (only god knows why) and Xcode would not let me remove the other target.
Once I removed all schemes and created them from scratch everything worked just fine.
One more scenario when this happens is when your sub-project(Target dependency) has Skip Install set to No in build settings.
To find which sub-project, check the content inside the archive. Products folder should have only one product.
In my case a sub-project was a dynamic library and was added in the archive under /<path to archive>/Products/usr/lib/<library> path.

xcodebuild workspace and scheme

I am a little confused as to what happens with the xcodebuild command line tool when you specify a workspace and scheme.
I understand how a configured scheme works in the XCode IDE GUI. The build action lists the targets to build and for each action (Analyze, Test, Run, Profile, Archive), you select which one you want the build action to execute for.
So if I have each action (Analyze, Test, Run, Profile, Archive) selected in the build action for building, what happens when I execute the below command.
xcodebuild clean install -workspace MyWorkspace.xcworkspace -scheme MyScheme
-configuration AdHoc SYMROOT=PATH DSTROOT=PATH...
It searches for MyScheme.xcscheme in the main xcodeproj which has all this configuration specified when editing the scheme in XCode.
What does xcodebuild read in from this file? Does it just build the configured target with the AdHoc configuration and disregard everything else?
You're almost there, but your syntax is a bit off.
According to the man page:
xcodebuild -workspace workspacename -scheme schemename [-configuration configurationname]
[-sdk [sdkfullpath | sdkname]] [buildaction ...] [setting=value ...]
[-userdefault=value ...]
Where buildaction is one of the following:
buildaction ...
Specify a build action (or actions) to perform on the target. Available build actions are:
build Build the target in the build root (SYMROOT). This is the default build action.
archive Archive a scheme from the build root (SYMROOT). This requires specifying a scheme.
test Test a scheme from the build root (SYMROOT). This requires specifying a scheme.
installsrc Copy the source of the project to the source root (SRCROOT).
install Build the target and install it into the target's installation directory in the dis-
tribution root (DSTROOT).
clean Remove build products and intermediate files from the build root (SYMROOT).
In the Xcode IDE, you choose the buildaction to run through the Product menu, or by clicking and holding the round button on the top left of the IDE (Run = Play triangle, Test = wrench icon, etc).
Also, be careful to note where xcodebuild is looking for your build scheme - it can either be in your .xcproj OR your .xcworkspace file, depending on which one you created.
(If you didn't manually create a workspace, you will have a .xcproj file).
You can also ascertain which schemes you have via your 'Manage Schemes' setting in Xcode.

Xcode 4 - command line target with initial setup

I am implementing an app which has a dependency to a command line tool.
This is because there are some presets to do.
The command line tool is responsible to create a sqlite file with all initial information needed by the app (clearly speaking: its just a tool to add some initial data to the app).
Now the problem is, that during debugging everything works fine but if I'll do a release build, some errors occur.
The first error was
target specifies product type 'com.apple.product-type.tool', but there's no such
product type for the 'iphoneos' platform"
This was because my main app had the command line tool as a target dependency.
I solved this by removing the target dependency and added the build to a run script phase:
#Build the initial setup target
xcodebuild -target InitialCoreDataSetup -sdk macosx -configuration $CONFIGURATION
#Run initializing data target to get current sqlite file
cd "$CONFIGURATION_BUILD_DIR"
cd ..
cd "$CONFIGURATION"
current_dir=$(pwd)
./InitialCoreDataSetup "$current_dir" "$SRCROOT" "$CONFIGURATION"
Now the problem is, that I get the following error:
=== BUILD NATIVE TARGET InitialCoreDataSetup OF PROJECT XY WITH CONFIGURATION AdHoc ===
Check dependencies
SDK Configuration Error: no wrapper for product type #
I have absolutely no idea how to fix this.
Any suggestions?
How do you provide your app with an initial sqlite file?
Thanks in advance for any help!
EDIT
At the moment the build phases of my app target are the following:
BTW: I finally got that working by the following line:
#Build the initial setup target
export DYLD_FRAMEWORK_PATH="$SYMROOT/Debug"
/usr/bin/env -i xcodebuild -target InitialCoreDataSetup -sdk "macosx" -configuration Debug OBJROOT="$SYMROOT/Debug" SYMROOT="$SYMROOT"
echo "$SYMROOT/Debug"
#Run initializing data target to get current sqlite file
cd "$SYMROOT/Debug"
./InitialCoreDataSetup "$SYMROOT/Debug" "$SRCROOT" "$CONFIGURATION"
The trick is to start the initialCoreDataSetup always in DEBUG mode
I would suggest letting Xcode do the build in this situation since you should be able to generate the sqllite file by using a build rule to run a script before the other build rules run.

Resources