Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add simple target to cmake generated makefile

TL;DR: how can I make cmake to output the following rule in the generated makefile?

.PHONY: target-I-want
target-I-want:
  echo "Hello World"

Hi,

I'm used to setting projects with their own makefiles to run any type of script it may need.

The project I'm working now is being built using cmake, so I can't just make my own makefile because it will be overwritten by the one generated by cmake.

I'm setting up my own makefile with a different name, say makefile.dev, but since I don't want to run make -f makefile.dev target-I-want every time, I thought add_custom_target could help me.

So this is what I added to my CMakeLists.txt:

ADD_CUSTOM_TARGET(target-I-want
  COMMAND make -f ${CMAKE_CURRENT_SOURCE_DIR}/makefile.dev target-I-want
)

And it works. BUT when I run it, it does like a lot more that just running the command I want (make -f ${CMAKE_CURRENT_SOURCE_DIR}/makefile.dev target-I-want), like checking the cmake build system and going through a lot of automatically generated targets.

I want to know if there is a way to add the command I want just as I wrote it, with no extras.

like image 891
Erick Sepúlveda Avatar asked Feb 03 '26 19:02

Erick Sepúlveda


1 Answers

There is no command that I have seen to insert arbitrary text into the Makefiles that are generated.

Based on your comments you need to have CMake generate a custom target that does not also include the use the CMake executable itself. While Makefile regeneration and detailed status messages can be suppressed the custom target still uses the CMake executable in the overall framework of the generated Makefiles.

If you really don't want to type make -f makefile.dev target-I-want then you'll have to find a alternative method like setting up an alias, or a script that invokes the command, or append the arbitrary text to the top-level Makefile that is generated by CMake.

like image 58
fdk1342 Avatar answered Feb 06 '26 10:02

fdk1342



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!