LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-07-2010, 07:03 AM   #1
axelfc
Member
 
Registered: Jan 2006
Distribution: ArchLinux
Posts: 175

Rep: Reputation: 34
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?
 
Old 10-08-2010, 05:16 AM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
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.

Last edited by neonsignal; 10-08-2010 at 05:17 AM.
 
Old 10-08-2010, 05:27 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
as neon shows you can have as many commands as you like
remember the blanks to start the lines are TABS.
 
Old 10-08-2010, 05:57 AM   #4
axelfc
Member
 
Registered: Jan 2006
Distribution: ArchLinux
Posts: 175

Original Poster
Rep: Reputation: 34
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.
 
Old 10-08-2010, 06:35 AM   #5
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Quote:
Originally Posted by axelfc View Post
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).
 
Old 10-08-2010, 06:40 AM   #6
axelfc
Member
 
Registered: Jan 2006
Distribution: ArchLinux
Posts: 175

Original Poster
Rep: Reputation: 34
Quote:
Originally Posted by neonsignal View Post
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. :/
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Makefile question orgcandman Programming 3 02-27-2008 12:04 PM
g++ makefile question zahadumy Programming 4 03-25-2007 04:18 PM
Makefile question ar1 Programming 3 01-26-2006 07:06 AM
Makefile.in question lucky6969b Programming 4 12-08-2005 03:32 AM
makefile question blackzone Programming 2 12-20-2004 07:03 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration