I am looking through the source-code for ZeroMQ, which I want to build from source. Inside I found platform.hpp.in, which contains:
...
#cmakedefine ZMQ_HAVE_SO_PEERCRED
#cmakedefine ZMQ_HAVE_LOCAL_PEERCRED
#cmakedefine ZMQ_HAVE_SOCK_CLOEXEC
#cmakedefine ZMQ_HAVE_SO_KEEPALIVE
#cmakedefine ZMQ_HAVE_TCP_KEEPCNT
...
I assume these cmakedefine macros are used as templates to generate a header file, but how exactly do they work in CMake? How can I determine what are valid values? How are values set by the user when building the project?
It's part of a file that's processed by CMake's configure_file command. When configure_file is called for the file, #cmakedefine FOO is replaced by:
#define FOO - if the CMake variable FOO is set to ON or TRUE.
/* #undef FOO */ - otherwise.
And that is one way to pass values from CMake into C or C++ source code: The result of configure_file() is a C/C++ header file, which is included by the code you want to respect the CMake variable values.
Related
Background
I'm running through the CMake tutorial and found the solution for TODO #10, i.e:
TutorialConfig.h.in
#define Tutorial_VERSION_MAJOR #Tutorial_VERSION_MAJOR#
#define Tutorial_VERSION_MINOR #Tutorial_VERSION_MINOR#
seems to conflict with the manual for the configure_file command
configure_file(
[NO_SOURCE_PERMISSIONS | USE_SOURCE_PERMISSIONS |
FILE_PERMISSIONS ...]
[COPYONLY] [ESCAPE_QUOTES] [#ONLY]
[NEWLINE_STYLE [UNIX|DOS|WIN32|LF|CRLF] ])
Copies an file to an file and substitutes variable
values referenced as #VAR# or ${VAR} in the input file content. Each
variable reference will be replaced with the current value of the
variable, or the empty string if the variable is not defined.
Furthermore, input lines of the form
#cmakedefine VAR ...
will be replaced with either
#define VAR ...
or
/* #undef VAR */
depending on whether VAR is set in CMake to any value not considered a
false constant by the if() command. The "..." content on the line
after the variable name, if any, is processed as above.
in that the #cmakedefine preprocessor directive should be used in the input file an turned into a #define directive in the output file rather than what is done in the solution (using the #define directive in the input file). I tried using the #cmakedefine directive before looking at the solution and it resulted in an undefined _VERSION_MINOR i.e. here is the generated header file:
TutorialConfig.h
#define Tutorial_VERSION_MAJOR 1
/* #undef Tutorial_VERSION_MINOR */
whereas when I use the #define directive within the input file (TutorialConfig.h.in), both Tutorial_VERSION_MAJOR and Tutorial_VERSION_MINOR are defined in the generated header file. i.e.
TutorialConfig.h
#define Tutorial_VERSION_MAJOR 1
#define Tutorial_VERSION_MINOR 0
So what is the difference here and why would one properly grab the defined Tutorial_VERSION_MINOR and another not? Is there specific documentation regarding the #cmakedefine and #cmakedefine01 directives other than what is shown in the configure_file documentation?
Based on a comment received highlighting the following text from the configure_file documentation:
"[...] depending on whether VAR is set in CMake to any value not considered a false constant by the if() command".
I'm wondering if the only difference between using the #cmakedefine and #define directives is just that the #cmakedefine one goes through an if() statement and thus any value of 0 would result in the output header file showing an undefined variable rather than a string of 0 as intended.
cmake version 3.22.1
Minimal Working Example
A full working minimal example below or alternatively can be found here:
tutorial.cxx
#include <cstdlib>
#include <iostream>
#include <string>
#include "TutorialConfig.h"
int main()
{
std::cout << "Version: " << Tutorial_VERSION_MAJOR << "." << Tutorial_VERSION_MINOR << std::endl;
return 0;
}
TutorialConfig.h.in
#define Tutorial_VERSION_MAJOR #Tutorial_VERSION_MAJOR#
#define Tutorial_VERSION_MINOR #Tutorial_VERSION_MINOR#
CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(Tutorial
VERSION 1.0)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
configure_file(TutorialConfig.h.in
TutorialConfig.h
#ONLY)
add_executable(Tutorial
tutorial.cxx)
target_include_directories(Tutorial
PUBLIC
${PROJECT_BINARY_DIR})
Steps to reproduce this:
from the directory containing the above 3 files (e.g. Step1) make a build directory with mkdir build
cd build
cmake ..
make
./Tutorial and note it prints Version: 1.0 as expected
cd ..
change the #define directives in TutorialConfig.h.in to #cmakedefine, i.e.
TutorialConfig.h.in:
#cmakedefine Tutorial_VERSION_MAJOR #Tutorial_VERSION_MAJOR#
#cmakedefine Tutorial_VERSION_MINOR #Tutorial_VERSION_MINOR#
repeat steps 2-4 and note the compile error:
Step1/tutorial.cxx:8:64: error: ‘Tutorial_VERSION_MINOR’ was not declared in this scope; did
you mean ‘Tutorial_VERSION_MAJOR’?
8 | std::cout << "Version: " << Tutorial_VERSION_MAJOR << "." << Tutorial_VERSION_MINOR << std::endl;
| ^~~~~~~~~~~~~~~~~~~~~~
| Tutorial_VERSION_MAJOR
As Friedrich already mentioned in the comments, CMake is doing exactly what it's documented to do.
You wrote:
project(Tutorial
VERSION 1.0)
, so in the following
#cmakedefine Tutorial_VERSION_MINOR #Tutorial_VERSION_MINOR#
, for the first "Tutorial_VERSION_MINOR", CMake looks at what the value of that CMake variable is, and sees that it is a value considered false by the if() command. Yes- Tutorial_VERSION_MINOR is a CMake variable. See the <PROJECT_NAME>_VERSION_MINOR docs. The specific docs that say 0 is considered as a falsy value in the if() command docs is under the "basic expressions" section:
False if the constant is 0, OFF, NO, FALSE, N, IGNORE, NOTFOUND, the empty string, or ends in the suffix -NOTFOUND. Named boolean constants are case-insensitive.
And as the documentation you linked states, #cmakedefine VAR ... is replaced with /* #undef VAR */ when "VAR is set in CMake to any value considered a false constant by the if() command."
seems to conflict with the manual for the configure_file command [...] in that the #cmakedefine preprocessor directive should be used in the input file an turned into a #define directive in the output file rather than what is done in the solution (using the #define directive in the input file)
uhh no. The tutorial just uses the approach that will actually work here. This kind of scenario where you want to create a macro definition with the same name as a CMake variable where the CMake variable might be a CMake-falsy value is the weakness scenario of #cmakedefine that one can avoid by instead using #define and configure_file's variable substitution facilities- namely #VAR# and ${VAR}.
There's nothing special about #define with respect to configure_file. It just treats it like any other text. #cmakedefine, #cmakedefine01 and variable substitution with #VAR# and ${VAR} are the only things configure_file does special things for.
What is the easiest way to get the value of a C/C++ macro into a CMake variable?
Given I check for a library libfoo with the header foo.h. I know foo.h contains the macro #define FOO_VERSION_MAJOR <version> where version is an integer or string value. To extract the major version of the found library, I want to use the value from this macro.
As a bonus, if the macro is not found, this could indicate a version older then a specific version introducing the version macro.
I'd go with file(READ ...) to read the header followed by string(REGEX ...) to extract desired define.
Example code:
file(READ "foo.h" header)
string(REGEX MATCH "#define FOO_MAJOR_VERSION [0-9]+" macrodef "${header}")
string(REGEX MATCH "[0-9]+" FooMajorVersion "${macrodef}")
With try_compile and the right pragma it is possible to output the value of a pre-processor macro during compile time. CMake can parse the output to get the desired value.
CMake snippet:
try_compile(result "${CMAKE_BINARY_DIR}"
SOURCES "${CMAKE_SOURCE_DIR}/foo-version.cpp"
OUTPUT_VARIABLE fooversion)
string(REGEX MATCH ": [0-9]+" fooversionshort "${fooversion}")
string(REGEX MATCH "[0-9]+" FooMajorVersion "${fooversionshort}")
foo-version.cpp:
#include "foo.h"
/* definition to expand macro then apply to pragma message */
#define VALUE_TO_STRING(x) #x
#define VALUE(x) VALUE_TO_STRING(x)
#pragma message(VALUE(FOO_MAJOR_VERSION))
int main()
{
return 0;
}
Good:
Actual value from the variable, which might be calculated.
Bad:
Output of macros is only support by some newer compilers.
Parsing of output might break for untested compilers, as the format changes from compiler version to compiler version.
Kind of complicated code, verbose code which is difficult to read.
The macro expansion can be extracted by using the C preprocessor.
I used this method to extract specific typedef's without needing to know the exact location of the define in the file hierarchy.
Let say that we have this macro defined somewhere in foo.h
#define FOO_VERSION_MAJOR 666
You need to create a helper file helper.c with this content
#include "foo.h"
int __READ_FOO_MAJOR__ = FOO_VERSION_MAJOR ;
Note that I used a specific pattern __READ_FOO_MAJOR__ that I will use later as the pattern for a grep command
And from CMakeLists.txt you have to call the C (C++, etc..) preprocessor and filter its output like this
cmake_minimum_required(VERSION 3.0)
execute_process(
COMMAND bash "-c" "${CMAKE_C_COMPILER} -E ${CMAKE_CURRENT_SOURCE_DIR}/helper.cpp | grep __READ_FOO_MAJOR__ | awk '{ print $4}'"
OUTPUT_VARIABLE FOO_VERSION_MAJOR )
message("From CMake: FOO_VERSION_MAJOR=${FOO_VERSION_MAJOR}")
Note that awk '{ print $4}' extract the 4th word on the selected line.
When running cmake we get this result
From CMake: FOO_VERSION_MAJOR=666
The short shel pipeline used is built with Unix system V base commands and should run everywhere.
I can pass defined variable for cmake like below(for example when I want to set PYTHON_INCLUDE_PATH=dir1).
cmake -DPYTHON_INCLUDE_PATH=dir1 ..
But what if I want to set multiple paths for this PYTHON_INCLUDE_PATH? I tried
cmake -DPYTHON_INCLUDE_PATH='dir1 dir2 dir3'
or should it be
cmake -DPYTHON_INCLUDE_PATH='dir1:dir2:dir3' (or , instead of :)
But I'm not sure it's valid. (see some other error so I am not sure yet if it's correct or not.)
I saw I can also set these defines in .cmake file like set(VARIABLE,VALUE) like below.
set(OpenCV_CUDA_VERSION 7.5)
Then what's the corresponding syntax for this set(..) form when the variable has multiple elements?
use the following syntax:
cmake -DLIST_VAR="one;two;three" ...
you can play w/ the following CMakeLists.txt:
cmake_minimum_required(VERSION 3.5)
foreach(v IN LISTS LIST_VAR)
message(STATUS "${v}")
endforeach()
Suppose I have two libraries ABC, XYZ and two variables ABC_FOUND XYZ_FOUND in CMakeLists.txt. In a config.h.in file, I have the following lines
#define __USE_ABC__ #ABC_FOUND#
#define __USE_XYZ__ #XYZ_FOUND#
If I do configure_file(config.h.in config.h), the output config.h file will be changed to
#define __USE_ABC__ ON
#define __USE_XYZ__ OFF
My question is how can I convert the ON/OFF options into 1/0, in other word, my desired output config.h should be
#define __USE_ABC__ 1
#define __USE_XYZ__ 0
Define ABC_FOUND to either 0 or 1 with
#cmakedefine01 #ABC_FOUND#
Use an intermediate variable in either environment to change it to USE_ABC.
Likely, you should use #cmakedefine instead of hard defines (see examples e.g. here)
But please remember that in the case of #cmakedefine you get a configuration header where "enabled features" (in terms of CMake if() command) are #define-d to a supplied value, while "disabled features" are #undef-ined. So you should test them not with #if <something> but rather with #ifdef <something>.
I try to use cmake determine whether exist inttypes.h header file for generate project of visual c++ 11.
Initially, i wrote the following sentence in CMakeLists.txt
FIND_FILE(HAVE_INTTYPES_H "inttypes.h" DOC "Does the inttypes.h exist?")
Unfortunately, the HAVE_INTTYPES_H variable is HAVE_INTTYPES_H-NOTFOUND.
Afterwards, i looked up cmake documentation about find_file, which mentioned the need to some search path. But i can not get the c standard header files in any place in cmake?
Thanks.
Your find_file call is correct. The problem is there's no inttypes.h on Visual Studio. So keep your test the same, but when it's not found include another headers, for instance provided by: http://code.google.com/p/msinttypes/
Something like:
FIND_FILE(HAVE_INTTYPES_H "inttypes.h" DOC "Does the inttypes.h exist?")
if (HAVE_INTTYPES_H)
add_definitions(-DHAVE_INTTYPES_H=1)
endif()
and in your code:
#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#else
#include "path/to/inttypes.h"
#endif
Now, to detect headers, you may also want to try using the CheckIncludeFile standard CMake module, it's trying to detect include files using your target compiler instead of searching the file system:
include(CheckIncludeFile)
check_include_file("stdint.h" STDINT_H_FOUND)
if (STDINT_H_FOUND)
add_definitions(-DHAVE_STDINT_H=1)
endif()