Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake: Read build number from file to set a variable

I'm working on a project where the build number is stored in a file called 'BuildNumber.txt' at the root of the project. What I'd like to do is have CMake read the number from this file and set a variable that can be applied to a header file.

setup.h.in

#define build_number "@BUILD_NUMBER@";

Using configure_file, it's possible to replace placeholders in an .in file like above with a CMake variable. Is it possible to get CMake to read in the number from BuildNumber.txt into a variable?

like image 920
Grant Limberg Avatar asked Apr 20 '11 22:04

Grant Limberg


People also ask

How do you pass arguments in CMake?

If you create the cache variable in the CMakeLists. txt file and then pass the argument via calling cmake, won't the CMakeList. txt file keep overwriting the argument value? No, the cache is populated on the first run with either the default value, or the value supplied on the command line if it is provided.

How do you set a variable value in CMake?

You can use the command line to set entries in the Cache with the syntax cmake -D var:type=value , just cmake -D var=value or with cmake -C CMakeInitialCache. cmake . You can unset entries in the Cache with unset(... CACHE) .

What does Add_subdirectory do in CMake?

Add a subdirectory to the build. Adds a subdirectory to the build. The source_dir specifies the directory in which the source CMakeLists.

What is ${} in CMake?

Local Variables You access a variable by using ${} , such as ${MY_VARIABLE} . 1. CMake has the concept of scope; you can access the value of the variable after you set it as long as you are in the same scope. If you leave a function or a file in a sub directory, the variable will no longer be defined.


1 Answers

You can use the CMake command file (STRINGS ...) for that purpose. Assuming the build number is located in the file BuildNumber.txt in a single line, the following command will read it into the CMake variable BUILD_NUMBER:

file (STRINGS "BuildNumber.txt" BUILD_NUMBER)

Also see the file command reference.

like image 189
sakra Avatar answered Oct 05 '22 12:10

sakra