How to specify environment variables for cmake invocation

Running on OSX 10.8, XCode developer tools 5.1.1.

In pointing CLion at our legacy CMakeList.txt file (which references several others), I'm getting a failure upon opening the project, due to the fact that cmake, via CLion, is seeing the wrong compilers (cc instead of clang).  In our build system, we actually invoke cmake via a rake script that sets the CC and CXX environment variables prior to running cmake.  Doing this from the shell does the right thing:

  env CXX=/usr/bin/clang++ CC=/usr/bin/clang cmake -DCMAKE_BUILD_TYPE=Debug

But I can't figure out how to get CLion to use those values (instead of the default /usr/bin/cc usr/bin/c++).  I tried the following, and none worked:

- putting the env directive into the Prefs->Build, Excecution & Deployment->Toolchains->CMake executable
- creating a shell script with the env variables set that invokes cmake and pointing the same preference
- setting Environment variables->CC/CXX in CLion EAP.app/Contents/Info.plist.

Ideas?

14 comments
Comment actions Permalink

How about set env var before running CLion?

0
Comment actions Permalink

OSX apps don't inherit environment from the shell they're launched from, as far as I know.

0
Comment actions Permalink

This is not yet implemented (http://youtrack.jetbrains.com/issue/CPP-188). But you can set proper variables in the CMakeLists.txt files.
If you'd like to add you custom parameters, now you can't add them to CMakeCache from the IDE (http://youtrack.jetbrains.com/issue/CPP-428), but there is a workaround here.

0
Comment actions Permalink

Thanks.  I actually managed to get it working late last night another way.  Perhaps related to the unsupported feature you referenced, I found the CMake cache tab in the UI and tried to edit the values of CMAKE_CXX_COMPILER and CMAKE_C_COMPILER, but when I tried to save them, I got the error:

  Error:Cannot read /Users/stebro/Library/Caches/clion10/cmake/generated/e68903e4/e68903e4/__default__/CMakeCache.txt

The file in question didn't exist, but when I clicked the "Reload CMake Project" button, it reappeared (with the values that I didn't want for the above variables).  I then just updated the values in that file with a text editor and it worked.  Good enough for now, but not really a long term solution.  Am I even supposed to be editing this file?

EDIT:  My solution above doesn't really work - the next time I opened CLion, it regenerated that cache file & the changes were gone.  See my next comment to see what seems to have finally worked consistently.

0
Comment actions Permalink

As for setting variables in CMakeLists.txt, I'm putting this at the top of my CMakeLists.txt file for the project, but it seems to have no effect:

 
if (APPLE)
   set(CMAKE_CXX_COMPILER "/usr/bin/clang++")
   set(CMAKE_C_COMPILER "/usr/bin/clang")
endif (APPLE)


Also, this page ( http://www.cmake.org/Wiki/CMake_FAQ#How_do_I_use_a_different_compiler.3F ) says to avoid specifying compilers in the CMakeLists.txt file.

EDIT:  This solution seems to have finally worked:

if (APPLE)
   set(CMAKE_CXX_COMPILER /usr/bin/clang++ CACHE FILEPATH "c++ compiler" FORCE)
   set(CMAKE_C_COMPILER /usr/bin/clang CACHE FILEPATH "c compiler" FORCE)
endif (APPLE)

Obviously, not really cool to hard-code the path to the compiler in my CMakeLists.txt file, but at least it's repeatable.  I'm still interested in knowing the "right" way to do this.
0
Comment actions Permalink

The proper way:
- change variable in the CMakeCache editor in CLion
- press Enter
- click Apply Changes and Reload button in this tool window
you are done

0
Comment actions Permalink

That produces the error:

  Error:Cannot read /Users/stebro/Library/Caches/clion10/cmake/generated/e68903e4/e68903e4/__default__/CMakeCache.txt


0
Comment actions Permalink

May be you've changed some access rights while fixing the file manually?
Try dropping the cmake directory (/Users/stebro/Library/Caches/clion10/cmake) and call Reload CMake Project action.

0
Comment actions Permalink

We have a big cross platform project were we cannot rely on evironment variables or the other workarounds. Within the original cmake we have to edit quite a few own variables and reconfigure everytime after changing these kind of settings. I see no chance in CLion to do this.
In addition the cmakelists.txt is inside a subdirectory and referencing other sub scripts from there. How can I specify the place where the cmake cache will be generated because this will be also the place for the generated object file and binaries and this should not be the same place as cmakelists.txt?

0
Comment actions Permalink

You can't pass the env variable to CMake now (http://youtrack.jetbrains.com/issue/CPP-188). We hope to fix this in the next updates. So now only the option to set them in CMakeCache is available.

As for the other questions - you can set CMAKE_RUNTIME_OUTPUT_DIRECTORY in CMakeLists.txt to solve this.

0
Comment actions Permalink

I got this error before I ever started editing the file (the error text was how I located the file that needed editing).  In any case, it's not a permissions issue - if I check the file immediately after this error appears, I see that it doesn't exist.  Invoking Reload CMake Project brings the file back (with the settings I don't want), and trying to edit it again via the IDE results in the same error.

0
Comment actions Permalink

This is really strange. May be you can record some short screencast for us to see what's happening?

0
Comment actions Permalink

Stephen, thanks for the screencast, could you please now do the folowing:
* delete Caches/clion10/cmake folder
* open the project
* when you notice the error 'this build requres precompiled headers", zip clion10/cmake folder
* change cmake cache variables as you did before
* when you see the error "Cannot read CMakeCache.txt", zip clion10/cmake folder

File a bug report in the tracker and attach both zip files. Also, if possible, attach your CMakeLists.txt files (it may help us to understand that causes this problem).

Thanks for your help!

0

Please sign in to leave a comment.