On 10/25/2011 12:09 AM, Daniel Dunbar wrote: > Hi all, > > I've been searching for an answer to the following problem, with no luck: > > I have a CMake build that reads in some data from an external > (non-CMake) file during configuration. > > I want to tell CMake that the configuration depends on this file, so > that the --check-build-system / check_cache steps will know to > automatically rebuild the project files / Makefiles / etc., whenever I > edit the external file. > > Is this possible? > > Here is an example test case to demonstrate the problem, I want the > project to know to rebuild when I change the version.txt file: > -- > # Here is the test CMakeLists file that uses an external file: > $ cat CMakeLists.txt > cmake_minimum_required(VERSION 2.6) > > file(STRINGS "version.txt" VERSION) > add_library(test t.cpp) > add_definitions("-DVERSION=@VERSION@") > > # And here is the external file: > $ cat version.txt > 1 > > # Do our first build. > $ make > [100%] Building CXX object CMakeFiles/test.dir/t.cpp.o > Linking CXX static library libtest.a > [100%] Built target test > > # Modify the external file. > $ echo 2 > version.txt > > # Now we rerun make, but nothing is rebuilt because CMake > # doesn't know the configuration depends on version.txt > $ make > [100%] Built target test > -- > > Thanks in advance, > - Daniel
>From the top of my head, the easiest solution would be to do something like this: configure_file(version.txt ${PROJECT_BINARY_DIR}/version.txt COPYONLY) Another possibility would be to create a CMake script (e.g. called createVersionHeader.cmake) which reads version.txt and creates a header file version.h defining VERSION which you then include everywhere where it is required. In your CMakeLists.txt file you first configure_file this script to contain the paths to PROJECT_SOURCE_DIR and PROJECT_BINARY_DIR and you run it using add_custom_command. This has the advantage that whenever version.txt changes, CMake doesn't have to reconfigure the whole project, only an additional rule will be run. HTH Michael -- Powered by www.kitware.com Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ Follow this link to subscribe/unsubscribe: http://www.cmake.org/mailman/listinfo/cmake