Intercepting environment variables set by xcodebuild and passing them to static analyzer

Hi,

I'm using scan-build to run xcodebuild and static analyses on the code being compiled. Some of my custom analyses need the information which is set by xcodebuild in environment variables before the source code is compiled. Is there any way to intercept those env variables set by xcodebuild and pass them to static analyzer?

- Nikita

All environment variables that xcodebuild sets should be observable by the clang process. Using getenv() within your analysis code should be a portable solution for accessing those values. Does that not work?

It doesn’t seem to work, or I’m doing something wrong :). For example, the following line, when inserted into static analyzer, prints “TARGET_NAME env variable is set: 0”:

fprintf(stderr, “TARGET_NAME env variable is set: %d\n”, getenv(“TARGET_NAME”) != NULL);

Although xcodebuild does set the TARGET_NAME when compiling (I dump all env variables from xcodebuild with a Run Script Phase). The only env variables related to compiling I see from the clang process are CCC_ANALYZER_ANALYSIS, LDPLUSPLUS, CCC_ANALYZER_HTML, CLANG and CC.

All environment variables that xcodebuild sets should be observable by the clang process. Using getenv() within your analysis code should be a portable solution for accessing those values. Does that not work?

It doesn’t seem to work, or I’m doing something wrong :). For example, the following line, when inserted into static analyzer, prints “TARGET_NAME env variable is set: 0”:

fprintf(stderr, “TARGET_NAME env variable is set: %d\n”, getenv(“TARGET_NAME”) != NULL);

Although xcodebuild does set the TARGET_NAME when compiling (I dump all env variables from xcodebuild with a Run Script Phase).

It’s entirely possible that xcodebuild only sets these environment variables for child processes that execute during the Run Script Phase, and not when the compiler is invoked. I honestly don’t know.

The only env variables related to compiling I see from the clang process are CCC_ANALYZER_ANALYSIS, LDPLUSPLUS, CCC_ANALYZER_HTML, CLANG and CC.

These environment variables are set by scan-build, which (when running the analyzer) is a parent process of xcodebuild. This is what makes me suspect that xcodebuild isn’t setting any special environment variables when invoking the compiler.

AFAK xcodebuild does not set any variable, except when it execute a runscript phase, and I guess it just pass these variable as parameter to exec when it execute the script subprocess and does not set them globaly.