How to replace a line in a file with the value of the current directory using CMake

I am trying to make another study code for children reproducible, so that others do not have the same problem as now. But for this I do not have enough experience with cmake. Here is what I could do after reading the documents:
In the same folder as mine CMakeLists.txt, I have a file with a name io_utils.hwith a variable ROOT_PATHwhose value VALUE_ROOT_PATH. I want to replace this line with the value of the current file directory. So I tried to add the following to CMakeLists.txt:

# Set ROOT_PATH in io_utils.h
FIND_PATH(BUILD_PATH CMakeLists.txt . )
FILE(READ ${BUILD_PATH}io_utils.h IO_UTILS)
STRING(REGEX REPLACE "VALUE_ROOT_PATH" "${BUILD_PATH}" MOD_IO_UTILS "${IO_UTILS}" )
FILE(WRITE ${BUILD_PATH}io_utils.h "${MOD_IO_UTILS}")

I tried to do and install this, but it does not work, the file is not changed. What happened to what I did?

+4
source share
2 answers

, configure_file cmake. -, , , cmake, . , , io_utils.h.in, :

const char* ROOT_PATH = "${BUILD_PATH}";

CMakeLists.txt - :

configure_file( io_utils.h.in io_utils.h )

cmake, (io_utils.h.in ), ( io_utils.h) , :

const char* ROOT_PATH = "/path/to/CMakeLists.txt";

, CMake , CMakeLists.txt, CMAKE_SOURCE_DIR. BUILD_PATH .

+8

:

add_definition(ROOT_PATH ${VALUE_ROOT_PATH})

+1

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


All Articles