How to retrieve a user environment variable in CMake (Windows) - windows

I know how to retrieve a normal machine wide environment variable in CMAKE using
$ENV{EnvironmentVariableName}
but I can not retrieve a user specific environment variable. Is it possible and how?

Getting variables into your CMake script
You can pass a variable on the line with the cmake invocation:
FOO=1 cmake
or by exporting a variable in BASH:
export FOO=1
Then you can pick it up in a cmake script using:
$ENV{FOO}

You can also invoke cmake itself to do this in a cross-platform way:
cmake -E env EnvironmentVariableName="Hello World" cmake ..
env [--unset=NAME]... [NAME=VALUE]... COMMAND [ARG]...
Run command in a modified environment.
Just be aware that this may only work the first time. If CMake re-configures with one of the consecutive builds (you just call e.g. make, one CMakeLists.txt was changed and CMake runs through the generation process again), the user defined environment variable may not be there anymore (in comparison to system wide environment variables).
So I transfer those user defined environment variables in my projects into a CMake cached variable:
cmake_minimum_required(VERSION 2.6)
project(PrintEnv NONE)
if (NOT "$ENV{EnvironmentVariableName}" STREQUAL "")
set(EnvironmentVariableName "$ENV{EnvironmentVariableName}" CACHE INTERNAL "Copied from environment variable")
endif()
message("EnvironmentVariableName = ${EnvironmentVariableName}")
Reference
CMake - Command Line Tool Mode

You need to have your variables exported. So for example in Linux:
export EnvironmentVariableName=foo
Unexported variables are empty in CMAKE.

Environment variables (that you modify using the System Properties) are only propagated to subshells when you create a new subshell.
If you had a command line prompt (DOS or cygwin) open when you changed the User env vars, then they won't show up.
You need to open a new command line prompt after you change the user settings.
The equivalent in Unix/Linux is adding a line to your .bash_rc: you need to start a new shell to get the values.

Related

The keyword export in command line?

What does 'export' do when used in a command line.
For example, and this is only one example, I build a number of C++ libraries and for a library such as zlib-1.2.8 I need to specify the install directories.
To do this I need to do the following in MSYS command line interface. This is just one example
export LIBRARY_PATH="c/libraries/libs;$LIBRARY_PATH"
Would anyone know what the command 'export' actually does in this instance?
Does it permanently install a record for MSYS to user later on when looking for dependencies such as ZLIB . My using make install the zlib library file is placed in this directory.
OR, when I close MSYS is this LIBRARY_PATH lost from MSYS memory?
Thanks
This is the bash syntax to set an environment variable. Using export allows the variable to be seen outside the script in which it's defined.
Environment variables only affect the msys process and any child processes started from that shell. If you want it to persist after you close the command line and start a new one, you will need to put it into a script such as .bashrc

How to compile multiple languages from command prompt?

If I wanted to compile code in java, I go to environment variables and set the PATH variable to the bin of the jdk on my computer. Now my command prompt recognizes commands like "javac" and "java" and I can compile/run code without any issues.
But if I wanted to compile code in C/C++, suddenly commands such as "gcc" or "g++" are no longer recognized by my command prompt because the PATH variable was overwritten to the java location. I could change it back to the location of my C/C++ compilers, but then my command prompt would no longer recognize the java commands.
How can you make the command prompt recognize all commands? There must be a better way than changing environment variables every time.
You can append all needed paths to your PATH variable. You will want to put them in order of priority, in case there are matches that may potentially be found on multiple path entries.
For example, for Windows:
set PATH=%JAVA_PATH%;%PATH%
set PATH=%CPP_PATH%;%PATH%
...
Or, as a single line:
set PATH=%JAVA_PATH%;%CPP_PATH%;...;%PATH%
(Hypothetical entries - substitute as appropriate.)

View windows environment variables in Cygwin

Is there a way to sync windows environment variables with Cygwin?
For example, in CMD echo %inetroot% gives the path to a project build. Is it possible to transfer this env variable to Cygwin such that echo $inetroot provides the same path?
Thanks!
Use the env program to so that ... or echo "$inetroot". I think the variable names are case-sensitive, though (in Bash and thus MinGW).
Those variables are all available in MinGW from Windows (user profile and global). Again, use env to list them or for example env|grep -i inetroot to find the properly capitalized version of the variable name.

Setting an environment variable in Cygwin

I have been trying to setup a environment variable in Cygwin using the command export PRIMOSBASE=/directory/for/primosfiles.
And when i check the variable using the command echo $PRIMOSBASE it shows the /directory/for/primosfiles. hopeful this means the environment variable is set.
But when i try to run a shell script(primos) for the /directory/for/primosfiles, it shows
./primos: line 8: /prilaunch.pl: No such file or directory
chmod: failed to get attributes of `step1.sh': No such file or directory
which means i have not set the PRIMOSBASE environment. could anyone please tell me where i am going wrong...
Thanks ...
Run
echo "export PRIMOSBASE=/directory/for/primosfiles" >> ~/.bashrc
to append the command to the end of your .bashrc file, so that the variable is set each time you use Cygwin. Then run
source ~/.bashrc
to make it take effect immediately.
NOTE: Make sure you use double brackets (>>) to append. It might be a good idea to make a backup of .bashrc just in case. If you're not comfortable with I/O redirection, an alternative is to edit .bashrc with an editor. I think vim is among the default tools in Cygwin.
I had a similar issue trying to get ANDROID_HOME to work in a Cygwin window. When I used the linux path separators, as follows
ANDROID_HOME=/cygdrive/c/Users/User/AppData/Local/Android/sdk my gradlew build script complained it couldn't find the sdk in ANDROID_HOME.
I eventually discovered that I had to set my environment variable in the Windows format, including Windows path separators '\', as follows
ANDROID_HOME=C:\Users\User\AppData\Local\Android\sdk
Note: the PATH and several other environment variables set in Windows are converted into Linux format. I hope this helps others who want/need to use Cygwin + Windows + essentially Windows programs that need environment variables.

How to print result of shell script in CMake?

If I want to check currently exported environment variables, I do this in shell.
export
In CMake, I do this to print something.
MESSAGE ("This is message.")
How can I print former one with CMake?
I know that CMake is stand for cross-platform building, anyway when debugging something I need to check raw values. So I need this.
If you want to know the value of a specific variable, you can use $ENV{varname}:
message(STATUS $ENV{PATH})
If you want to see all variables, you probably need to resort to invoking an external command such as env (on Unix) or set (on Windows):
# Windows
execute_process(COMMAND cmd /c set OUTPUT_VARIABLE output)
message(${output})
I don't know how to get cmake to show output to a console, but if you don't mind just getting it out of a file later, you can add:
env > /tmp/environment
in the appropriate place, and then read the /tmp/environment file later.

Resources