bibi
bibi

Reputation: 3765

makefile parallel clean+compile issue

I have a simple makefile with 3 build rules:

  1. clean (that cleans the .o)
  2. debug (compiles my code with debgging stuff)
  3. release (compiles my code with optimization stuff)

sometimes I want to switch between debug mode and release so I would issue this

make clean debug -j8

or

make clean release -j8

that has a drawback because while it's doing the clean stuff, the -j8 allows make to jump some command since the .o are still there Then those .o are removed by the clean rule and the compiler complains because it can't find those .o

I could do something like

make clean; make debug -j8

but since I use an odd makefile in another dir, the command becomes

make -C ../src -f nMakefile clean ; make -C ../src -f nMakefile -j8 release

that is more annoying. I was wondering if there was an hiddedn-guru-mode-rule that allows me to do it in one line

Hope it's clear enough...

Upvotes: 4

Views: 1716

Answers (2)

payo
payo

Reputation: 4561

I needed to solve this very same problem, and the solution I came up was to parse the MAKECMDGOALS for clean, and dispatch a shell command to do the actual cleaning work; RATHER than clean the build as a target. This way, any MAKECMDGOALS that include "clean" will clean the build as part of that build, first, sequentially, rather than clean running asynchronously as its own target.

-include $(deps)
bin/%.o : %.cpp
    @mkdir -p $@D
    g++ $(flags) $(includes) -MMD -c $< -o $@

.PHONY : clean
clean:
    @echo rm -rf bin/

ifneq ($(filter clean,$(MAKECMDGOALS)),)
    $(shell rm -rf bin/)
endif

Upvotes: 2

user2672165
user2672165

Reputation: 3049

As I stated above, the normal practice is to have different sub directories for the object files. As you are running in parallel I would think you need to enforce serial execution so that clean is completed before release. One way of doing it could be:

clean_release: clean
    +@$(MAKE) -s --no-print-directory release

or if you prefer

clean_release:
    +@$(MAKE) -s --no-print-directory clean && $(MAKE) -s --no-print-directory release

Upvotes: 1

Related Questions