简体   繁体   中英

Run makefile with command in order

I've the following Makefile which run all the target when you enter make in the CLI, I know that you can run the make with specific target but is there a way to run the make with sevral of targets ?

for example via the cli run module1 pack cleanup

with this order

  1. module1
  2. pack
  3. cleanup

(for this just module2 will not run) and I dont want to create new target for the 3 steps above This is the make for example

all: module1 module2

.PHONY: module1
module1:
    @echo "run module 1"
    DIR=$(PWD)
    @echo $(DIR)

.PHONY: module2
module2:
    @echo "run module2”

pack:
    pack $(DIR)

cleanup: 
    gbt clean $(DIR)

You can use MAKECMDGOALS for this. Something like:

PACK := $(filter pack,$(MAKECMDGOALS))
CLEANUP := $(filter cleanup,$(MAKECMDGOALS))

module1: $(PACK) $(CLEANUP)
        ...

module2: $(PACK) $(CLEANUP)
        ...

pack:
        pack $(DIR)
cleanup:
        git clean $(DIR)

If you don't give the pack or cleanup targets on the command line then the associated variable PACK and CLEANUP will be the empty string and won't be listed as a prerequisite of module1 or module2 etc.

You do not want to run them "in order". The entire purpose of make is for it to decide what to do and in what order. A Makefile is for listing the rules and dependencies so make can work properly.

If you want command run in order, write a script.

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