1

We are supporting two modes of builds (32 & 64 bit). For that we are having two targets:

For 32 bit we have make32

For 64 bit we have make64

Whenever user try to run make command with make32, It will run 32bit build and same way for with make64 it will run 64bit build. We have another target called makeboth which runs both 32 & 64 bit builds sequentially.

makeboth:
         $(MAKE) make32 
         $(MAKE) make64

Above make snippet is in top level GNUmakefile, where we are running several makefiles inside its child directories. I want to run both 32 & 64 bit builds in parallel when user passes target makeboth.Is it possible to do that ? Can anyone one please help here.

Naga
  • 59
  • 8

3 Answers3

0

Arrange it so that makeboth has the other two as prerequisites.

makeboth: make32 make64
    @echo makeboth done

Then make -jN makeboth should do the parallel build.

kaylum
  • 13,833
  • 2
  • 22
  • 31
  • Kayulum, can we have any number with -j or we have any restriction based on some criteria/load? – Naga Jul 07 '20 at 06:58
  • 1
    @naga I usually use a little less than the number of cores on my system. See also: [What's best value for make -j](https://stackoverflow.com/questions/3025587/whats-best-value-for-make-j) – kaylum Jul 07 '20 at 07:21
  • Thanks Kaylim, I have one doubt , Here if we pass -j ,parallel execution should be applied for all targets in our GNUmakefile? or only with the target that we pass i.e. makeboth (only pre-reqs of makeboth run parallel.) – Naga Jul 07 '20 at 08:07
  • 1
    It will run as much of the build as possible in parallel. That is, `make` will work out which targets are independent and will run those in parallel. – kaylum Jul 07 '20 at 08:11
  • Nice...Then it is applicable for all the targets. – Naga Jul 07 '20 at 08:16
0

You can use -j N option to run targets in parallel:

makeboth: make32wrapper make64wrapper;
    @echo makeboth done

# We need wrappers here to prevent parallel execution deeper in the
# make32 and make64 targets
make32wrapper:
    @$(MAKE) -s make32
make64wrapper:
    @$(MAKE) -s make64

make32:
    echo make32 started
    sleep 3
    echo make32 done

make64:
    echo make64 started
    sleep 3
    echo make64 done

Example:

$ make -j 10 makeboth
make32 started
make64 started
make64 done
make32 done
makeboth done
Doj
  • 1,244
  • 6
  • 13
  • 19
  • Doj, Thanks for your response. Could you please elaborate why do we need wrappers here? And also what does -s option do? How can we add checks in GNUmakefile that user has passed with -j and proceed with parallel build? – Naga Jul 07 '20 at 06:51
  • 1. Necessity of the wrappers depends on how make32 and make64 and their prerequisites implemented. If they are implemented in a way that they allow parallel execution, we don't need the wrappers here. In general case they may work incorrect in parralel, so its just for safity. – Doj Jul 07 '20 at 07:06
  • 1
    2. `-s` means 'silent' for submakes and I added it here just for nice-looking output in the example code :) – Doj Jul 07 '20 at 07:08
  • 1
    3. `-j N` is native option for GNU make. To check inside Makefile code if it was specified you can try to use `$(MAKEFLAGS)` variable. – Doj Jul 07 '20 at 07:13
  • okay as per your 1st point, i understood that we run only make32 , make64 targets parallely and not the targets that depends on them right ? In our implementation the pre reqs which are dependent on make32/64 is not running parallel. So i guess we need to have wrappers right... Below answer posted by kaylum also works for me right? Is there any drawbacks with the same? – Naga Jul 07 '20 at 07:59
  • You understood it right. See also this answer for another possible approach: [In recursive make, prevent -j from cascading down](https://stackoverflow.com/a/62270487/11695625) – Doj Jul 07 '20 at 08:12
  • Actually i wanted to run as much of the build as possible in parallel. if we have wrappers it wont work for all targets. so I guess kaylum's answer will suit my requirement. – Naga Jul 07 '20 at 08:22
0

GNUmake enables you to run two commands concurrently, using the -j argument of make:

you can dod this manulay via command line:

where N is the process numbers to be spawned

make -j <N> make32 make64

This command will run the two commands/targets simultaneously. you can even do better by creating a new make target that handles this. Back in the Makefile:

supply number in the N section !

makeboth:
    make -j <N> make32 make64
Adam
  • 2,820
  • 1
  • 13
  • 33