LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Question about a makefile (https://www.linuxquestions.org/questions/programming-9/question-about-a-makefile-836728/)

axelfc 10-07-2010 07:03 AM

Question about a makefile
 
I am trying to modify a makefile which currently is like this:

target: file1
some commands

When I type make the makefile checks the timestamp of file1 and if it is newer than the target it executes the commands.

What I want to do is to execute a few shell commands first which will update file1 or leave it the same. If file1 is updated and only then I want to run the commands of target1. Something like this:

target2:
if condition then touch file1

target1: file1
some commands

I've played with target dependencies, phony targets but I either end up with infinite loop in the makefile or I can't execute the commands of target2 before those of target1.

I don't know if I was clear enough... Does anyone have any solution?

neonsignal 10-08-2010 05:16 AM

I'm not clear why you cannot put the conditional directly into the command sequence for target1, but if you can't, you can use either multiple makefiles or recursion to achieve this:

Code:

target2:
        if [ -e condition.dat ] ; then touch file1; fi
        make target1
target1: file1
        echo 'building target1'
        touch target1

In some circumstances (for example, autodependency generation), you can also make use of the include directive to achieve similar effects.

The reason you can't do it with simple dependencies is that you are changing the dependencies after the make has evaluated them.

bigearsbilly 10-08-2010 05:27 AM

as neon shows you can have as many commands as you like
remember the blanks to start the lines are TABS.

axelfc 10-08-2010 05:57 AM

First of all thank you both for your replies.

The original problem I was trying to solve was the following.

I have a makefile like this:

Code:

ALL_FILES = $(DIR)*.txt

target: $(ALL_FILES)
        command -o $@

If I run make the makefile checks all the txt files in $(DIR) and if it finds a file with timestamp newer than target it executes the command. If I add a new txt file in $(DIR) make runs the command and does the compile. However if I remove a txt file from $(DIR) there is no file with timestamp newer than target and therefore make won't do the compile. Is there a way to make it run when I delete a txt file from $(DIR)?

I came up to a code like the one in my first post but I'm not sure if I am in the right way or if there is any other solution.

neonsignal as you say my problem is that I am changing the dependencies after the make has evaluated them.

neonsignal 10-08-2010 06:35 AM

Quote:

Originally Posted by axelfc (Post 4121358)
If I add a new txt file in $(DIR) make runs the command and does the compile. However if I remove a txt file from $(DIR) there is no file with timestamp newer than target and therefore make won't do the compile. Is there a way to make it run when I delete a txt file from $(DIR)?

Yes, by making it depend on the containing directory itself (since this is the only thing that changes when you delete a file).

axelfc 10-08-2010 06:40 AM

Quote:

Originally Posted by neonsignal (Post 4121381)
Yes, by making it depend on the containing directory itself (since this is the only thing that changes when you delete a file).

I've already proposed this as a solution but it's not accepted. Therefore I am trying to find something else. :/


All times are GMT -5. The time now is 04:11 AM.