Adding actions to rules in generated Makefile

I’d like to know how to modify CMakeLists.rtxt to cause the following to happen in the generated Makefile, to create a dummy file to be used as a flag…

all:
rm -f build.ok # add this line to delete flag
$(CMAKE_COMMAND) -E … # this line already created via cmake
touch build.ok # add this line to create flag

Can anyone help me do this, or something that is equivalent?

Thank you very much.

You might be able to use PRE_BUILD and POST_BUILD custom commands for libraries, but that doesn’t support the all target. I think it’d be easier to wrap such things around the build itself:

$ rm -f foo && cmake --build . && touch foo

Thanks for responding, I was considering doing what you suggested, putting the deletion and creation of the flag file in the script that calls cmake, but wanted to consolidate the flagging within the Makefile itself (and learn to use cmake better along the way). Also, I know the Makefile way works solidly (tested it manually).

I tried using the flagging outside of cmake and this didn’t work for me. Somehow, the cmake is not propagating back the exit return code of the make(s) it runs. For whatever reason, the cmake rules/files created are not passing back return codes (if that was per cmake design). I did some Web search on this topic, and it seems others have the same or similar issues, and the cmake exit code passing mechanism is not quite clear. Maybe there are some cmake directives that can perform the (failing) return code correctly.

I’ll keep digging on getting cmake to generate the right Makefile to do flaggng. Hopefully, this is somehow possible. It seems this should be a very basic capability.

Thanks.

Alas, the kind of “just drop this code in” mechanism is very easy to break with CMake’s backwards compatibility guarantees. Basically anything that the code could “see” would be subject to being frozen unless we document what, exactly, the snippets would be allowed to do and that anything beyond that is subject to breakage if/when CMake changes something in the Makefiles generators.