Adding a custom command with the file name as a target

前端 未结 1 1669
盖世英雄少女心
盖世英雄少女心 2020-12-18 02:30

I\'d like to do something like add_custom_command, with the output file name as a target in the generated makefile. Is there an elegant way of doing this?

1条回答
  •  时光说笑
    2020-12-18 02:42

    You could do it by generating your hello.bin as a side effect of a target. Instead of generating hello.bin from objcopy, you generate hello.tmp. Then as a side effect you also copy hello.tmp to hello.bin. Finally, you create the phony target hello.bin that depends on your hello.tmp. In code:

    add_executable (hello hello.c)
    add_custom_command(OUTPUT hello.tmp
                       COMMAND objcopy --output-format=binary hello hello.tmp
                       COMMAND ${CMAKE_COMMAND} -E copy hello.tmp hello.bin
                       DEPENDS hello
                       COMMENT "objcopying hello to hello.bin")
    add_custom_target(hello.bin ALL DEPENDS hello.tmp)
    

    The problem with that is that hello.bin is not cleaned when you run clean. To get that working, add:

    set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES hello.bin)
    

    0 讨论(0)
提交回复
热议问题