[CCPPETMR/SIRF] Run gmi_x* after relevant file has been updated (#372)

0 views
Skip to first unread message

Richard Brown

unread,
Apr 26, 2019, 12:05:36 PM4/26/19
to CCPPETMR/SIRF, Subscribed

If we alter cstir.h, we need to run gmi_xstir (and similar for other packages).

It would be good if CMake detected changes to the relevant files (cstir.h, cgadgetron.h, creg.h, etc.) and run the relevant executably (gmi_xstir, gmi_xgadgetron, gmi_xreg, etc.).

I'm alsmot 100% sure this is possible in CMake, I just can't remember how to do it.


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or mute the thread.

Edoardo Pasca

unread,
Apr 29, 2019, 4:40:17 AM4/29/19
to CCPPETMR/SIRF, Subscribed

You'll have to create a custom target depending on gmi_xstir and cstir.h which will run the specific command you need.

Richard Brown

unread,
Apr 29, 2019, 5:26:05 AM4/29/19
to CCPPETMR/SIRF, Subscribed
# Re-run gmi_xreg if cReg.h has changed
ADD_CUSTOM_COMMAND(
    OUTPUT ""
    COMMAND "gmi_xreg" 
    DEPENDS "../../cReg/cReg.h"
)

I tried this, but it's not correct. Any ideas?

Edoardo Pasca

unread,
Apr 29, 2019, 6:18:44 AM4/29/19
to CCPPETMR/SIRF, Subscribed

What do you mean "is not correct"?
I normally set the OUTPUT but it may be that the relative path to cReg.h is not resolved properly.

ADD_CUSTOM_COMMAND(
    OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/build/timestamp"
    COMMAND "gmi_xreg" 
    DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/../../cReg/cReg.h"
)

Richard Brown

unread,
Apr 29, 2019, 6:52:12 AM4/29/19
to CCPPETMR/SIRF, Subscribed

Using your example everything compiles, but when I change cReg.h, mReg.c isn't updated accordingly. (which is what I meant by "not correct").

Edoardo Pasca

unread,
Apr 29, 2019, 7:21:11 AM4/29/19
to CCPPETMR/SIRF, Subscribed

Is the command gmi_xreg issued at any time if you update cReg.h? Or the change of cReg.h is not picked by CMake?

Richard Brown

unread,
Apr 29, 2019, 8:48:22 AM4/29/19
to CCPPETMR/SIRF, Subscribed

I'm not sure whether the change of cReg.h isn't being detected (seems unlikely), or if I'm not using the command properly (more likely). At any rate, gmi_xreg isn't being run during the make stage.

Edoardo Pasca

unread,
Apr 29, 2019, 9:46:07 AM4/29/19
to CCPPETMR/SIRF, Subscribed

I tried on a simplified example. Adding a target works

add_custom_target(
    run_gmi 
    COMMAND gmi_xreg 
    DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/head.h"
)

but you'll run it with make run_gmi

Edoardo Pasca

unread,
Apr 29, 2019, 10:44:17 AM4/29/19
to CCPPETMR/SIRF, Subscribed

There are 2 signatures of the add_custom_command.

You are trying to use the first and the documentation is less than clear here. In practice the add_custom_command is not run at all unless you attach it to a target. Therefore one has to add a target. In the following I have created a hello target which is a hello-world program.

The custom target depends on the OUTPUT of the custom command and it will output a file named timestamp in the ${CMAKE_CURRENT_BINARY_DIR} with the content of the output of command hello

I hope this clarifies.

set(OUTPUT timestamp)
add_custom_command(
    OUTPUT ${OUTPUT}
    COMMAND hello  > ${OUTPUT}
    DEPENDS hello 
    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
)
add_custom_target(custom DEPENDS ${OUTPUT})

Additionally be careful as

Do not list the output in more than one independent target that may build in parallel or the two instances of the rule may conflict (instead use the add_custom_target() command to drive the command and make the other targets depend on that one).

The second signature

adds a custom command to a target such as a library or executable. This defines a new command that will be associated with building the specified <target>.

Richard Brown

unread,
Apr 30, 2019, 6:53:33 AM4/30/19
to CCPPETMR/SIRF, Subscribed

Sorry, I can't follow your example. What's head.h and what's the custom target custom?

From my side, this is as I've got: rijobro@3ab7954

It's not great because:

  1. the command gmi_xreg is run every time cmake is run.
  2. I had to create a dummy executable test1.

If this is easy for you (because I'm struggling!), feel free to commit directly to my branch.

Edoardo Pasca

unread,
May 1, 2019, 5:46:45 AM5/1/19
to CCPPETMR/SIRF, Subscribed

I think you did right. The custom command is only run if you specifically attach to a target, which is what you did by create_custom_target. Now, this target must depend on the source you want to monitor.

The additional problem would be that you need to make your new target become a dependency of install, probably.

Kris Thielemans

unread,
May 1, 2019, 6:24:51 AM5/1/19
to CCPPETMR/SIRF, Subscribed

there's some target/dependency stuff in UCL/STIR#280 (where I resolved it for doxygen)

Kris Thielemans

unread,
Jun 14, 2019, 5:58:53 PM6/14/19
to CCPPETMR/SIRF, Subscribed

Some doc on this is at https://crascit.com/2017/04/18/generated-sources-in-cmake-builds/

As gmi_xreg creates both mreg.h, mreg.c, it seems to me that you need to say

add_executable(gmi_xreg gmi_xreg.cpp)

# Re-run gmi_xreg if cReg.h has changed
ADD_CUSTOM_COMMAND
(
    OUTPUT mreg.h mreg.c
    COMMAND "gmi_xreg" 
    DEPENDS "../../cReg/cReg.h" "gmi_xreg"
)

add_executable(mreg ${CMAKE_CURRENT_BINARY_DIR}/mreg.c ${CMAKE_CURRENT_BINARY_DIR}/mreg.h)

INSTALL(TARGETS mreg DESTINATION ${MATLAB_DEST})

A problem then is that gmi_xreg relies on the SIRF_PATH env variable
https://github.com/CCPPETMR/SIRF/blob/51d131ef1b945560e131f5585df108ba744f6c2f/src/Registration/mReg/gmi/gmi_xreg.cpp#L56
I'm not 100% sure why this is as I haven't checked the output. Presumably, this path could/should be passed as an argument to gmi_xreg. @evgueni-ovtchinnikov could help with that.

Reply all
Reply to author
Forward
0 new messages