I have a file called adhoc.cpp.proto
in ${SRC_DIR}
. I need to copy it to a file named adhoc.cpp
in ${SRC_DIR}
in preparation for some other tasks. I prefer to create the file if it does not exists, but I'll take an unconditional copy to keep things simple.
According to Copy file from source directory to binary directory using CMake I should be able to use configure_file(old new COPYONLY)
or file(COPY old DESTINATION new)
. Neither work for me. configure_file(old new COPYONLY)
does nothing, and file(COPY old DESTINATION new)
creates a directory called new
and puts the file old
in it.
According to the file
command docs GENERATE
may also work. The problem is, I can't figure out how to use it from the docs and I can't find examples of it.
How do I unconditionally copy ${SRC_DIR}/old
to ${SRC_DIR}/new
?
In case it is needed, testing is happening on Fedora 28 with:
$ cmake --version
cmake version 3.11.2
But I need this to work back to CMake 2.8, which is what is found in the field for current distros.
$ cat CMakeLists.txt
# <head notes ...>
cmake_minimum_required(VERSION 2.8.6)
if (${CMAKE_VERSION} VERSION_LESS "3.0.0")
project(cryptopp)
set(cryptopp_VERSION_MAJOR 7)
set(cryptopp_VERSION_MINOR 0)
set(cryptopp_VERSION_PATCH 0)
else ()
cmake_policy(SET CMP0048 NEW)
project(cryptopp VERSION 7.0.0)
if (NOT ${CMAKE_VERSION} VERSION_LESS "3.1.0")
cmake_policy(SET CMP0054 NEW)
endif ()
endif ()
set(SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR})
include(GNUInstallDirs)
include(CheckCXXCompilerFlag)
# Create adhoc.cpp from adhoc.cpp.proto.
# configure_file(adhoc.cpp.proto adhoc.cpp COPYONLY)
# file(COPY ${SRC_DIR}/adhoc.cpp.proto DESTINATION/${SRC_DIR} adhoc.cpp)
file(COPY ${SRC_DIR}/adhoc.cpp.proto DESTINATION ${SRC_DIR} adhoc.cpp)
...
configure_file(adhoc.cpp.proto adhoc.cpp COPYONLY)
produces:
cmake_build$ rm -rf * && rm -rf ../adhoc.cpp && cmake ..
-- The C compiler identification is GNU 8.2.1
...
# Nothing is copied
cmake_build$ ls ../adhoc*
../adhoc.cpp.proto
file(COPY ${SRC_DIR}/adhoc.cpp.proto DESTINATION ${SRC_DIR}/adhoc.cpp)
creates directories:
cmake_build$ rm -rf * && rm -rf ../adhoc.cpp && cmake ..
-- The C compiler identification is GNU 8.2.1
...
# Puts old file in directory
cmake_build$ ls ../adhoc*
../adhoc.cpp.proto
../adhoc.cpp:
adhoc.cpp.proto
file(COPY ${SRC_DIR}/adhoc.cpp.proto ${SRC_DIR}/adhoc.cpp DESTINATION ${SRC_DIR})
fails:
cmake_build$ rm -rf * && rm -rf ../adhoc.cpp && cmake ..
-- The C compiler identification is GNU 8.2.1
...
CMake Error at CMakeLists.txt:65 (file):
file COPY cannot find "/home/test/cryptopp/adhoc.cpp".
file(GENERATE OUTPUT ${SRC_DIR}/adhoc.cpp ${SRC_DIR}/adhoc.cpp.proto CONDITION true)
fails, too (as does CONDITION 1
):
cmake_build$ rm -rf * && rm -rf ../adhoc.cpp && cmake ..
-- The C compiler identification is GNU 8.2.1
...
CMake Error at CMakeLists.txt:66 (file):
file Incorrect arguments to GENERATE subcommand.
file(WRITE ${SRC_DIR}/adhoc.cpp file(READ ${SRC_DIR}/adhoc.cpp.proto))
creates the file but it does not evaluate the file(READ ...)
expression:
cmake_build$ rm -rf * && rm -rf ../adhoc.cpp && cmake ..
-- The C compiler identification is GNU 8.2.1
...
$ cat ../adhoc.cpp
file(READ/home/test/cryptopp/adhoc.cpp.proto)