简体   繁体   中英

Targets in Makefile that can not be invoked in shell

I got multiple different targets in Makefile but I do not want users to be able to invoke some of them from shell, they are just prerequisites for other targets. For example:

target1: pre1 pre2
    ...

target2: target3
    ...

target3:
    ...

I would like to disable calling target3 in bash, in a way that would even prevent bash from displaying make target3 when make target is typed and then tab key is pressed.

In short words I do not want people who invoke make with my Makefile to know that target3 exists. Is it possible to achieve?

No expert in make , bit of looking around the GNU make pages

By default, make starts with the first target (not targets whose names start with '.').

So the target names that start with a period are not considered when make identifies the default goal. You could try adding the dot ( . ) before your target as

target1: pre1 pre2
    ...

target2: .target3
    ...

.target3:
    ...

to avoid target3 from shown default when you tab is pressed.

A way to achieve this is to parse the target given to the make command by using the variable MAKECMDGOALS .

Here is how look like the Makefile :

ifeq ($(MAKECMDGOALS),)
ALLOWED=1
endif

target1: target2 target3
ifdef ALLOWED 
    echo "target1"
endif

target2:
ifdef ALLOWED 
    echo "target2"
endif

target3: 
ifdef ALLOWED 
    echo "target3"
endif

If the make has no target, it will execute the echo . The make target3 will not output anything.

If you want to allow a specific target you can add the following:

ifeq ($(filter target1,$(MAKECMDGOALS)),target1)
ALLOWED=1
endif

Disclaimer: This answer would completely depend on the linux distro you are using. This is tested on Ubuntu 16.04:

Mark that as a hidden target. Rename it to something that starts with a non-alphanumeric string.

Common option would be .target3

From declare -f _make_target_extract_script :

  /^[^a-zA-Z0-9]/             d             # convention for hidden tgt

This line above is a part of the sed script that prints the target list for make , via the bash-completion function _make .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM