Veeg
Veeg

Reputation: 325

CMake: Generated input dependency to configure_file

I would like to run configure_file() whose input file is generated using add_custom_command, through a custom dependency, before installing the output of configure_file.

I'm in reality using a ruby script to read a cmake formatted file, to extract a few definitions which i can convert to Ruby constants in a separate library. Thus, i require the configure_file such that cmake can replace its internal variables in the ruby generated file, whose goal is to export these variables.

Thus far, I've attempted the following:

# Custom command to build the out.rb.in
add_custom_command (
  OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/output.rb.in"
  COMMAND ruby ${CMAKE_CURRENT_SOURCE_DIR}/build_ruby_output.rb
  -i ${CMAKE_CURRENT_SOURCE_DIR}/input.cmake
  -o ${CMAKE_CURRENT_BINARY_DIR}/output.rb.in
)

# Custom target is required to build it
add_custom_target (
  dummy_target_xxx ALL DEPENDS
  "${CMAKE_CURRENT_BINARY_DIR}/output.rb.in"
)

configure_file ( 
  "${CMAKE_CURRENT_BINARY_DIR}/output.rb.in"
  "${CMAKE_CURRENT_BINARY_DIR}/output.rb"
)

install (
  FILES "${CMAKE_CURRENT_BINARY_DIR}/output.rb"
  DESTINATION "${RUBY_VENDOR_LIBDIR}/myvendor"
)

CMake complains with the error

CMake Error: File /home/user/myproject/build/output.rb.in does not exist.
CMake Error at CMakeLists.txt:35 (configure_file):
  configure_file Problem configuring file

when running an out-of-tree build. Any ideas?

Upvotes: 4

Views: 9375

Answers (2)

Tsyvarev
Tsyvarev

Reputation: 65928

configure_file() is executed immediately at configuration step, while add_custom_command() add command to be executed(and dependency to be evaluated) at build step.

You may either replace add_custom_command with appropriate execute_process call, so your ruby script will be executed at configuration step, and its output file will be ready for configure_file.

Or you may replace configure_file with appropriate add_custom_command, so your file will be configured at build step, and will see dependencies. The problem here that you should explicitely pass CMake variables, used in "output.rb.in" file, to the configuration program, which can be, e.g.,

${CMAKE_COMMAND} [-D<var-definition>]+ -P <cmake-script-file-which-calls-configure_file>

Upvotes: 4

Veeg
Veeg

Reputation: 325

In this particular case, it was sufficient to reorder to configuration operation, by performing configure_file() first, and pipe the output of this into the custom command.. I was merely thinking of the ordering all wrong.

I still do not have an answer for adding generated dependency to the input of configure_file()

Complete code

configure_file ( 
  "${CMAKE_CURRENT_SOURCE_DIR}/input.cmake"
  "${CMAKE_CURRENT_BINARY_DIR}/output.rb.in"
)

# Custom command to build the output.rb
add_custom_command (
  OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/output.rb"
  COMMAND ruby ${CMAKE_CURRENT_SOURCE_DIR}/build_ruby_output.rb
  -i ${CMAKE_CURRENT_BINARY_DIR}/output.rb.in
  -o ${CMAKE_CURRENT_BINARY_DIR}/output.rb
)

# Custom target is required to build it
add_custom_target (
  dummy_target_xxx ALL DEPENDS
  "${CMAKE_CURRENT_BINARY_DIR}/output.rb"
)

install (
  FILES "${CMAKE_CURRENT_BINARY_DIR}/output.rb"
  DESTINATION "${RUBY_VENDOR_LIBDIR}/myvendor"
)

Upvotes: 1

Related Questions