0

I am writing cmake example for the first time.

Here is a part of CMakeFiles.txt:

add_custom_command(
   OUTPUT ${CODEGEN_SRC}
   PRE_BUILD
   COMMAND ${CODEGEN_CMD} ${SERVICE_XML} --generate-cpp- code=/home/hello/include/gen/testGenCode
   COMMENT "Generate gdbus code"
)

add_custom_target(${CODEGEN_TARGET}
    DEPENDS ${CODEGEN_SRC}
)

Generate code using gdbus-codegen-glibmm in command syntax using add_custom_command.

However, contrary to my expectations, when I actually do cmake and make, it looks like this:

cmake ..
CMake Error at Server/CMakeLists.txt:1 (ADD_EXECUTABLE):
  Cannot find source file:
           #### generate File ####
CMake Error at Client/CMakeLists.txt:36 (ADD_EXECUTABLE):
  Cannot find source file:
           #### generate File ####

Then, if you proceed with make, the contents of COMMANT in add_custom_command are output, and codes are actually generated.

After checking the generated code, proceed with cmake .. and make again to build normally.

Server/CMakeLists.txt, Client/CMakeLists.txt I set the dependency of ${CODEGEN_TARGET} using ADD_DEPENDENCIES in , but it works differently than I expected.

How can I get the gdbus-codegen-glibmm command to run first?

einpoklum
  • 118,144
  • 57
  • 340
  • 684
JungWoo
  • 15
  • 1
  • 4
  • 1
    In the title you write "post_build", in the code you use "PRE_BUILD" but actually you custom command is of ["Generating files" signature](https://cmake.org/cmake/help/latest/command/add_custom_command.html#generating-files) and you use it in that manner. Please, be more careful when formulate a problem and write a code. – Tsyvarev Nov 25 '21 at 08:21
  • "I set the dependency of `${CODEGEN_TARGET}` using ADD_DEPENDENCIES ..." - Do not *describe* your **code**, instead **add** it to the question post "as is". There is something wrong with your code, but by having just a vague description of it we cannot help you. You should be able to prepare [mcve] which demonstrates your problem. Please, do that. – Tsyvarev Nov 25 '21 at 08:25
  • It's my first time posting a question on stackoverflow, so I'm very inexperienced. We will pay attention to the comments you have left. Thank you. Tsyvarev ! – JungWoo Nov 25 '21 at 09:01
  • This might be worth a look - it seems your intention is to actually to add a _pre-generate_ step... https://stackoverflow.com/questions/19898502/cmake-pregenerate-makefile-for-non-existing-sources – Den-Jason Nov 25 '21 at 09:18

1 Answers1

1

add_custom_command will run the command during build phase (when running make). Since it generate the files required by the next target, it will fail if the file have never been generated.

You can configure the file when running cmake too, using execute_process() in addition of add_custom_command(). You can also use configure_file() to create a placeholder for the target before you erase it later with gdbus-codegen-glibmm when running make.

Laurent Jospin
  • 604
  • 5
  • 9