egrunin
egrunin

Reputation: 25083

Xcodebuild failing to pick up environment values from project file?

I'm using Xcode 3.2.6, MacOSX.

I have a globally visible environment setting:

ICU_SRC=~/Documents/icu/source

This really is an environment setting, it's set at login time. When I open up Terminal, it's there.

In my project, under Header Search Paths I've added this:

$(ICU_SRC)/i18n
$(ICU_SRC)/common

These expand correctly when I compile inside the IDE. When I look at the build results, I see this:

-I/Users/eric.grunin/Documents/icu/source/i18n
-I/Users/eric.grunin/Documents/icu/source/common

When I build from the command line, however, it fails. What I see is this:

-I/i18n
-I/common

Here's the command I'm using to compile:

/usr/bin/env -i xcodebuild -project my_project.xcodeproj -target "my_program" -configuration Release -sdk macosx10.6 build

What am I doing wrong?

Edited to add:

Apple explains Setting environment variables for user processes

Upvotes: 3

Views: 3756

Answers (5)

Z. Ziv
Z. Ziv

Reputation: 115

You need to "connect" xcode build to Xcode using your scheme.

From xcodebuild side : xcodebuild ... options ... yourVAR=yourValue

From Xcode -> Product -> Scheme -> Edit Scheme... -> Arguments -> Environment Variables (name) someKey (Value) $yourVAR

The key is that the name of yourVAR in xcodebuild will be referred from Xcode arguments, by using '$'.

In order to access the environment variable later from code, you need to use

ProcessInfo.processInfo.environment["someKey"]

Xcode Environment Variables from your Scheme

Upvotes: 2

user2246302
user2246302

Reputation: 396

I know this question is 2 years old but it is still relevant. I just spent a day tracking down the fact that xcodebuild looks at the CC environmental variable (if it is set) and will probably do the wrong thing. I am using xcodebuild version 6.1.1 (build version 6A2008a).

The error I get if CC is set is:

PBXTargetBuildContext.mm:1690

Details: commandPath should be a string, bit it is nil

Hints: none

I hope this helps somebody else.

Upvotes: 1

Joel
Joel

Reputation: 2295

Sometimes it works better if you specify the env vars before the xcodebuild command, as in

# NOT this
xcodebuild ... options ... SOMEVAR=somevalue

# But this instead works better
SOMEVAR=somevalue xcodebuild ... options ...

This works especially well for PRODUCT_NAME when working with cocoapods.

Upvotes: 2

Fuzhou Chen
Fuzhou Chen

Reputation: 91

As per my experiments, xcodebuild does not really pick up environment variable from Shell. To make xcode honor environment variables, we have to pass them explicitly:

  export ICU_SRC_ENV=~/Documents/icu/source 
  /usr/bin/env -i xcodebuild -project my_project.xcodeproj \
                             -target "my_program" \
                             -configuration Release \
                             -sdk macosx10.6 \
                             build \
                             ICU_SRC=$ICU_SRC_ENV

The command above passes system environment variable to xcodebuild environment variable. You may reference it in your xcodeproj by $(ICU_SRC) notation. BTW, I used different names so we can distinguish two variables, but you can also use the same variable name.

The ~/.MacOSX/environment.plist is indeed equivalant with the export line in my example. It's for Shell only.

Hope that helps.

Upvotes: 0

user529758
user529758

Reputation:

The "environment" settings set using Xcode are not really environment variables; they're only known to Xcode. If you want them to be really environment variables, visible from the command line, you have to use the following shell command:

user@host ~ $ export ICU_SRC="~/Documents/icu/source"

Upvotes: -1

Related Questions