[CMake] Adding a custom line to CMake's makefiles?

Michael Hertling mhertling at online.de
Wed Feb 2 16:32:48 EST 2011


On 02/02/2011 03:34 PM, Clifford Yapp wrote:
> Is there any way to customize the Makefile output from CMake to
> include user-defined lines (say, something like "#include
> Makefile.inc") at the end of each Make file?

With GNU Make, you might do the following:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
PROJECT(INCLUDEMAKEFILE C)
FILE(WRITE ${CMAKE_BINARY_DIR}/main.c "int main(void){return 0;}\n")
FILE(WRITE ${CMAKE_BINARY_DIR}/Makefile.inc "# Makefile.inc\n")
ADD_CUSTOM_COMMAND(
    OUTPUT includemarker
    COMMAND ${CMAKE_COMMAND} -E touch includemarker
    COMMAND ${CMAKE_COMMAND} -E echo
    include ${CMAKE_BINARY_DIR}/Makefile.inc
    >> \$\(firstword \$\(MAKEFILE_LIST\)\)
)
ADD_EXECUTABLE(main main.c includemarker)

The $(firstword $(MAKEFILE_LIST)) provides access to the currently
processed Makefile, so you can plant additional lines from within a
custom command. However, this approach is neither portable nor clean
nor <your-expectation-here>, so I would ask Eric's questions, too. ;)

Regards,

Michael


More information about the CMake mailing list