A CMake script I am using expects an environment variable XXX_HOME to be set, but I don't have this and don't want to add it.
Is there a way to trick CMake into thinking this variable exists, with a value I set, in the CMake cache - ideally using the CMake GUI - so I don't have to edit the CMake scripts? I tried adding a setting in the GUI XXX_HOME = ... but it didn't work.
Can't you run cmake-gui from an environment with the environment variable set? I do this all the time on windows using batch files and shortcuts to the visual studio command prompt so that I can build for different compilers / different bit depths in totally different build trees with the same source.
In linux you also should be able to set the variable on the same line as cmake-gui. Like this XXX_HOME="/usr/src/xxx" cmake-gui
Related
I am trying to move from an own written tool to cmake for Windows to simplify our build environment.
We have a tool in the build process that requires to have set environment variables. I would like to set them with cmake but don't know how. What I have tried:
set(ENV{MYENVVAR} "My-Content")
I would expect that I can read this environment variable again with
message($ENV{MYENVVAR})
But this does not work. Even setting a variable before invoking cmake (instead of setting it in cmake) does not help, I cant read the variable in cmake.
What do I do wrong?
Edit:
I do invoke cmake again after changing CMakeLists.exe.
I do set environment variables with set MYENVVAR=My-Content in Windows cmd.exe and then invoke cmake from exactly this console.
cmake is version 3.20.0 for Windows.
You're going to need to produce a minimal, reproducible example. Here's my best attempt based on your question and unfortunately, the best I can say is "works for me":
alex#alex-ubuntu:~$ export MYENVVAR=FooBarBaz
alex#alex-ubuntu:~$ cat test.cmake
cmake_minimum_required(VERSION 3.20)
message($ENV{MYENVVAR})
set(ENV{MYENVVAR} "My-Content")
message($ENV{MYENVVAR})
alex#alex-ubuntu:~$ cmake -P test.cmake
FooBarBaz
My-Content
alex#alex-ubuntu:~$ echo $MYENVVAR
FooBarBaz
So CMake can both read the environment variable and write to it in-process, but it does not affect the controlling process's (in this case bash; in your case cmd.exe) environment.
I create usual hello-world Qt executable via cmake+msvc.
But I cant launch or debug it - the launсh fails by the reason of the absence needed Qt dll.
I found that if I add the
D:\libs\Qt\Qt5.14.2\5.14.2\msvc2017_64\bin;
D:\libs\Qt\Qt5.14.2\5.14.2\msvc2017_64\plugins\platforms;
to the PATH environment variable - the issue gone.
But I dont want that paths in the global environment variables (I plan to have few different versions of the Qt libraries, so all time switching global environment variables will be unusable).
How I could pass the paths to Qt librariles to the cmake for debug/launch executable without modifying the global environment variables?
here is my solution.
open Tools > Options > Debugging > Symbols > add new location.
the location is your qt MSVC bin floder, in my case is "D:\Qt\6.2.2\msvc2019_64\bin".
don't forget select new location, now you can use vs debug qt exe.
You can add the path to your local debugging environment to your CMake generated Visual Studio Solution, either by adding it in the GUI under Configuration Properties -> Debugging -> Environment
or (since CMake 3.13.0) in your CMakeLists.txt by setting the appropriate property on the target.
set_target_properties(exe_target PROPERTIES VS_DEBUGGER_ENVIRONMENT "PATH=D:\libs\Qt\Qt5.14.2\5.14.2\msvc2017_64\bin;D:\libs\Qt\Qt5.14.2\5.14.2\msvc2017_64\plugins\platforms")
Although these properties are prefixed with VS_DEBUGGER_ they apply to both the Debug and Release versions.
Other VS_DEBUGGER_ properties might be of interest too.
See here for documentation.
I have a library with a bunch of different configuration options. We usually configure the build with cmake-gui and ticking a few checkboxes.
I want to automate this into a .sh script using just cmake.
e.g.
In GUI -> selects a bunch of different options
equivalent cmake command -> cmake -D CMAKE_XXX=X -D CMAKE_XXY=XXY [a bunch of options here] ..
How can I find the "equivalent" cmake command-line command to any arbitrary configuration I choose from the GUI?
The equivalent cmake command to cache a variable is explained here (-D option). Note that previous documentation was ambiguous, so take care of always checking the latest one.
Basically:
-D<var>:<type>=<value>
You have to specify also the type to have the variable cached in the same way as through your cmake-gui procedure. Note that variable definition is necessary only the first time: if not specified anymore, the cached value will be used.
cmake-gui generates CMakeVars.txt and CMakeCache.txt files in the build directory once you click "Configure" button. They cache all variables you configured through the GUI.
Had the same question ... and as you asked I looking up some of the options in the menu and found it. Menu Tools -> Show My Changes
Bringing up an Dialog with an edit field with content for command line options or cache file options.
yeah
p.s. I used cmake 3.11.1
just read file named like CMakeCache.txt (iirc) in the root of build directory and see variable names there
You can write a file containing all variables you want to set with set(<var_name> <value>) and pass this file to the CMake call via -C:
cmake -C <fileWithInitialValues> <pathToSrcDir>
Documentation:
https://cmake.org/cmake/help/v3.3/manual/cmake.1.html
This should would similar with cmake-gui and ccmake, but it is not a pure solution with the graphic interface.
My UNIX Makefile is as follows:
param=0
run:
./foo -r "fun($(param))"
So if I do make run, I get ./foo - r "fun(0)" and
for make run param=10, I get ./foo -r "fun(10)".
Now I want to generate similar Makefile using cmake.
add_custom_target(
run
./foo -r "\"fun($(param))\""
)
How do I set the default value for param within cmake configuration file?
The concept in CMake is a bit different. You can define "cache variables" (basically variables that are remembered for subsequent builds in the same build dir, and can be customized by users) that come with default values and documentation strings and such. These can then be changed either by passing -D name:type=value options to cmake, or using one of the friendlier frontends (e.g. ccmake, the curses UI for CMake).
Example based on your question:
SET(param 0 CACHE STRING "Test variable defaulting to '0'")
# ...
add_custom_target(run ./foo -r "\"fun(${param})\"")
You'll find more details in the exhaustive docs for CMake.
PS. this is for variables inside CMake and specifically CMakeLists.txt itself; the possibility to change the value is not carried over into the generated Makefile as far as I can tell. I'm not sure that's possible in the first place because it probably wouldn't be compatiable with all of the targets supported by CMake (e.g. Visual Studio projects and what not). In any case, CMake doesn't seem to have been designed for generating build files used independently of CMake.
Use
set (projectname_param 0)
to set it.
I'm using CMake for my build system and in the project, we will have a bunch of configuration files. Some of them will just need to be copied over, some will need to be modified per computer. I'm currently using CMake's "configure_file" command to copy/replace parts of the file. This works great, and I love how I can use any variable from CMake in the configure routine.
But if you change the original file, CMake will not pick this up and you have to rerun cmake for it to configure the directory. When I run "make", I want it to pick up that I've changed the file and rerun configure.
It will also reconfigure files always, even if the file it is overwriting is newer. I want it to act like a custom target.
I think I can do this with add_custom_command, but I don't think I can run a CMake command from add_custom_command. So is there anyway to duplicate the behaviour that configure_file does in CMake?
I recently upgraded to CMake 2.8. It seems like it automatically has the exact behavior I wanted.
I do not think this has an easy answer. I see two options:
To trigger a re-run of cmake if an input changes, you might be able to make your input file depend on CMakeLists.txt.
To run a cmake command as part of and add_custom_command, there is the variable ${CMAKE_COMMAND}, which will give you the path to the running cmake. You could, as part of the configure step, generate a fragment of CMake code (that calls configure_file) that is invoked using the -P option. Either pass substitutions on the command line using -D, or write them to the CMake fragment.