user459723
user459723

Reputation: 53

Find source directory from build directory in cmake

I want to create a bash helper script to diff a generated file against its source. The directory structure of the output matches the source, so I should be able to find the name of the source file if I had this data:

My plan to get this is:

How do I do that? Preferably without having to configure/generate(as that introduces a bit of delay). Should I instead grep a specific file in the build folder? Which one?

Upvotes: 4

Views: 1613

Answers (3)

jobor
jobor

Reputation: 369

Another way of doing is is to let CMake load the cache and print the desired variable.

Create a little CMake script print-source-dir.cmake

load_cache("${BUILD_DIR}" READ_WITH_PREFIX cache_ CMAKE_HOME_DIRECTORY)
message("${cache_CMAKE_HOME_DIRECTORY}")

and run it like this

$ cmake -DBUILD_DIR=/home/wuff/projects/whatever/build -P print-source-dir.cmake 
/home/wuff/projects/whatever

Upvotes: 0

archie2x
archie2x

Reputation: 21

You can grep for home directory in CMakeCache.txt:

grep ^CMAKE_HOME_DIRECTORY CMakeCache.txt | cut -d = -f2-

My cmake version:

$ cmake --version
cmake version 3.28.1

Upvotes: 2

sakra
sakra

Reputation: 65981

Once you have located the CMakeCache.txt you can grep for the project source dir in it in the following way:

cat CMakeCache.txt | grep -E '.*_SOURCE_DIR:STATIC=.*' | grep -E -o '/.*'

This outputs the value of the _SOURCE_DIR variable.

Upvotes: 3

Related Questions