Saving environment variables in CMake

I am working on what requires some header files from another source tree. For various reasons, I would like to keep these headers outside of my project and reference them during the make process.

I have a CMake build script that generates my makefiles, but I would like to be able to generate makefiles with references to the environment variables in them so that the generated makefile can be run as follows:

HEADERS=/somewhere/on/the/filesystem make

Is this possible using CMake? Otherwise, is there a way to get what I got after using only CMake?

+3
source share
4 answers

It seems to have worked for me:

set(${PROJECT_NAME}_PORT "$(TARGET_SERIAL_PORT)")

, , . , Makefile.

+1

$ENV{VARIABLE}, , cmake, make.

0

You should look at add_custom_command using the TARGET and PRE_LINK parameters.

0
source

To pass an environment variable, you can:

CMAKE_POLICY(PUSH)
CMAKE_POLICY(SET CMP0005 NEW)
ADD_DEFINITIONS(-DHEADER=$ENV{HEADER})
CMAKE_POLICY(POP)

Replace HEADERwith any name for your variable.

The cmake policy setting CMP0005is for cmake to create the right output for you.

0
source

Source: https://habr.com/ru/post/1769344/


All Articles