Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separate build directory using xcodebuild

The man page for xcodebuild reads:

Run xcodebuild from the directory containing your project (i.e. the directory containing the projectname.xcodeproj package).

I would like to keep my source directory (which is a Subversion external) clean and unmodified, and build my object files and executables to a location completely outside of the source directory.

Is there any way to build into a separate build directory from a terminal using xcodebuild like you can do with make tools or even msbuild on Windows?

like image 956
Dave Mateer Avatar asked Feb 11 '11 14:02

Dave Mateer


People also ask

Where are IOS builds stored?

It should by located in: ~/Library/Developer/Xcode/DerivedData .

How do I change my build settings in Xcode?

Choose the project in the Project Navigator on the left. Select the Configurations target from the Targets section and click the Build Settings tab at the top. The Build Settings tab shows the build settings for the Configurations target. It's possible to expand this list with build settings that you define.

What does Xcodebuild archive do?

It archives your Xcode project by running the xcodebuild archive command and exports the archive into an . ipa file with the xcodebuild -exportArchive command. This . ipa file can be shared and installed on test devices, or uploaded to App Store Connect.


2 Answers

You can set build settings from the command line. The CONFIGURATION_BUILD_DIR will set the build directory. For example:

xcodebuild -project YourProject.xcodeproj -configuration Debug -target All ARCHS=x86_64 ONLY_ACTIVE_ARCH=YES CONFIGURATION_BUILD_DIR=../some/other/dir

A reference is found on Apple's site:

  • Build Setting Reference
like image 95
Dave Mateer Avatar answered Nov 15 '22 23:11

Dave Mateer


To achieve complete separation the following build settings have to be changed (more than just CONFIGURATION_BUILD_DIR like accepted answer suggests), otherwise Xcode still builds some of its artifacts under its default build folder.

On my machine Xcode normally builds everything to ./Build and ./DerivedData where current directory is one of my project.

I wanted xcodebuild to build everything under Build-command-line folder so I used xcodebuild -scheme MyScheme -showBuildSettings | grep Build\/ to find all build settings that correspond to all build paths and by trial-end-error I found "the generative" build settings that are enough to redirect all build artefacts produced by xcodebuild to custom folder.

I ended up using the following command:

BUILD_DIR=./Build-command-line
DERIVED_DATA_DIR=$(BUILD_DIR)/DerivedData

xcodebuild -project MyProject.xcodeproj \
         -IDEBuildOperationMaxNumberOfConcurrentCompileTasks=`sysctl -n hw.ncpu` \
         -scheme MyScheme \
         -sdk iphonesimulator \
         -destination 'platform=iOS Simulator,name=iPhone 6S Plus,OS=latest' \
         -xcconfig command-line-build.xcconfig \
         -derivedDataPath $(DERIVED_DATA_DIR) \
         test

Where command-line-build.xcconfig is:

HERE_BUILD=$(SRCROOT)/Build-command-line
HERE_INTERMEDIATES=$(HERE_BUILD)/Intermediates

// Paths
// the following paths are enough to redirect everything to $HERE_BUILD
MODULE_CACHE_DIR    = $(HERE_BUILD)/DerivedData/ModuleCache
OBJROOT             = $(HERE_INTERMEDIATES)
SHARED_PRECOMPS_DIR = $(HERE_INTERMEDIATES)/PrecompiledHeaders
SYMROOT

Note: Make sure you use absolute paths in your xcconfig, otherwise you may have error: Xcode crashing on startup “parentPath must be nil but it is not.

I have written a post about this solution with a bit of background: xcodebuild: how to really change its build path.

P.S. Of course this information is subject to change but as of Xcode Version 7.3.1 (7D1014) it works perfectly for me.

like image 31
Stanislav Pankevich Avatar answered Nov 15 '22 23:11

Stanislav Pankevich