Home > front end >  cmake add_custom_command pre_build
cmake add_custom_command pre_build

Time:11-26

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?

CodePudding user response:

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.

  • Related