Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I call another target after I call my own script in Makefile?

Tags:

makefile

gnu

I want to call another target in my makefile after I call custom commands something like this:

first_target:
        do_some_stuff

second_target:
        call some command
        first_target

When I do it like above, it complains that first_target command is not found. How can I go about doing this ?

like image 342
Muhammad Lukman Low Avatar asked Oct 20 '15 07:10

Muhammad Lukman Low


Video Answer


2 Answers

What I sought: 1. Do one target first, then do another target. 2. At the end of a target, do another target.

parent_1:
    @echo "parent 1"

parent_2:
    @echo "parent 2"

child_1: parent_1
    @echo "child 1"

child_2: parent_1
    @echo "child 2"
    $(MAKE) parent_2
    @echo "end child 2"

Avoiding another instance of make:

child_2_setup:
    @echo "child 2"

child_2_complex: parent_1 child_2_setup parent_2
    @echo "end child 2"

This is more complex, especially for multiple subtasks.

like image 95
Hunaphu Avatar answered Sep 28 '22 04:09

Hunaphu


Makefile recipes have the following syntax:

target: dependency1 dependency2 ... dependencyN
    command1
    command2
    ...
    command3

With command1, command2... being commands executable on the shell of your system. So therefore writing first_target in your example results in gmake trying to execute command first_target which fails since there is no such command.

In order to achieve your goal you have to work with the dependencies which are listed after the name of the recepie. The dependencies list is empty in your example. Each dependency has to be available before the recepie can be made. For each dependency there should be a recepie within your makefile so gmake is able to create all stuff needed for a recepie (or gmake will fail with no rule to make target xxx). Also remember that all dependecies are made before the recepie.

So in your case I suggest doing something like this:

first_target:
   do_some_stuff

second_target: first_target
   call some command

This will result in the following command sequence (note that the commands of first_target are executed before the ones of second_target):

do_some_stuff
call some command

I guess this is not really what you need so I suggest to create a new build target like the following:

first_target:
   do_some_stuff

second_target:
   call some command

all: second_target first_target

This will result in the following command sequence:

call some command
do_some_stuff
like image 26
Lukas Thomsen Avatar answered Sep 28 '22 03:09

Lukas Thomsen