AOG Poston
a software engineer

Migrating from EXEC_PROGRAM in CMake
by AOGPoston on 03/28/24

While working on a project involving the Drogon C++ library, I encountered EXEC_PROGRAM. In Drogon's document it describes adding an few lines to simplify the process of generating views. The lines are:

FILE(GLOB SCP_LIST ${CMAKE_CURRENT_SOURCE_DIR}/views/*.csp)
foreach(cspFile ${SCP_LIST})
    message(STATUS "cspFile:" ${cspFile})
    EXEC_PROGRAM(basename ARGS "-s .csp ${cspFile}" OUTPUT_VARIABLE classname)
    message(STATUS "view classname:" ${classname})
    add_custom_command(OUTPUT ${classname}.h ${classname}.cc
        COMMAND drogon_ctl
        ARGS create view ${cspFile}
        DEPENDS ${cspFile}
        VERBATIM )
   set(VIEWSRC ${VIEWSRC} ${classname}.cc)
endforeach()

This chunck of code threw an error when building with cmake because the EXEC_PROGRAM function has been completely deprecated. So to get this to work, I had to use the more mmodern function, execute_process(). Its important that the format of the function and its arguments have changed and thus execute_process() isnt just a drop in replacement.

For me, I had to add the COMMAND keyword in order to maintain the codeblock's function.

The final, working code looks like:

FILE(GLOB SCP_LIST ${CMAKE_CURRENT_SOURCE_DIR}/views/*.csp)
foreach(cspFile ${SCP_LIST})
  message(STATUS "cspFile:" ${cspFile})

  execute_process(
    COMMAND basename ARGS
    "-s .csp ${cspFile}"
    OUTPUT_VARIABLE classname)

  message(STATUS "view classname:" ${classname})

  add_custom_command(
    OUTPUT ${classname}.h ${classname}.cc
    COMMAND drogon_ctl ARGS create view ${cspFile}
    DEPENDS ${cspFile}
    VERBATIM)
  set(VIEWSRC ${VIEWSRC} ${classname}.cc)
endforeach()

-Amon (@aogposton)