Francois
Francois

Reputation: 2066

CMake: require building of files in addition to building targets

In this simple CMakefile, the first script list.sh outputs a list of 2 generated files file1.proto;file2.proto, instructing CMake that they can be built from source source.xml (using the second script gen.sh).

cmake_minimum_required(VERSION 3.13)

set(source "source.xml")

execute_process(
  COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/list.sh ${source}
  OUTPUT_VARIABLE protos
)
message("${protos}: ${source}")
add_custom_command(
  OUTPUT ${protos}
  COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/gen.sh ${source}
  DEPENDS ${source}
)
add_custom_target(my_target DEPENDS ${protos})

Everything works well if I run:

$ cmake ..
file1.proto;file2.proto: source.xml
-- Configuring done
-- Generating done
-- Build files have been written to: /build

$ make my_target
[100%] Generating file1.proto, file2.proto
[100%] Built target my_target

What should I add to be able to also run the code generation with:

$ make file1.proto

[EDIT] autocomplete suggests only the following for command make:

$ make  (TAB TAB)
all                       cmake_force               edit_cache/               preinstall                
clean                     default_target            help                      preinstall/               
clean/                    depend                    my_target                 rebuild_cache             
cmake_check_build_system  edit_cache                my_target/                rebuild_cache/  

Upvotes: 0

Views: 69

Answers (1)

Francois
Francois

Reputation: 2066

Solution from @KamilCuk :
Adding the following makes it possible to build each proto file individually
(it works, but then cmake complains about circular dependencies!)

foreach(p ${protos})
  add_custom_target(${p} DEPENDS {CMAKE_CURRENT_BINARY_DIR}/${p})
endforeach()

Upvotes: 1

Related Questions