Fedora Linux Support Community & Resources Center
  #1  
Old 8th October 2010, 08:21 AM
axel Offline
Registered User
 
Join Date: Apr 2005
Location: Thessaloniki, Greece
Posts: 290
linuxfirefox
Question about a makefile

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)?
Reply With Quote
  #2  
Old 8th October 2010, 02:42 PM
stevea Online
Registered User
 
Join Date: Apr 2006
Location: Ohio, USA
Posts: 8,798
linuxfedorafirefox
Re: Question about a makefile

That's a difficult requirement. Common makefile dependency rules can only compare dates on files that exist with those of target objects.

When you delete a file the modification date of the directory changes, and that could be used by putting the diretory onto the dependency list BUT that directory may change for other reasons - like if the results from 'command' are written there.

You could make a script to update a 'MANIFEST' file (say the ouptut of "ls -1', create a copy in /tmp/MANIFEST, diff with $DIR/MANIFEST, copy if they differ) but only update the MANIFEST if it changes. Adding $(DIR)/MANIFEST to the dependency list would cause the command to be executed whenever MANIFEST is updated.
__________________
None are more hopelessly enslaved than those who falsely believe they are free.
Johann Wolfgang von Goethe
Reply With Quote
  #3  
Old 10th October 2010, 11:42 AM
axel Offline
Registered User
 
Join Date: Apr 2005
Location: Thessaloniki, Greece
Posts: 290
linuxchrome
Re: Question about a makefile

Hi stevea, thanks for your reply.

My first thought was indeed to use the timestamp of the directory but as you also mention it could change for another reason as well.

The next solution I thought of was using an extra file like your MANIFEST. I asked here just in case there was another solution available since a user who adds or removes a file in $(DIR) should know the existence of MANIFEST.

It seems there is no other way to do it though. Thanks for your time.
Reply With Quote
  #4  
Old 10th October 2010, 01:58 PM
SaGS Offline
Registered User
 
Join Date: Nov 2006
Posts: 135
windows_xp_2003opera
Re: Question about a makefile

Quote:
Originally Posted by axel View Post
…a user who adds or removes a file in $(DIR) should know the existence of MANIFEST…
No, users don’t kneed to know of the manifest file (= don’t need to update it themselves). Just apply stevea’s solution as follows:
Code:
ALL_FILES = $(DIR)*.txt

all : checkmanifest target

checkmanifest :
        ... create MANIFEST if it does not exist
    OR  ... update it if the list of files changed
    OR  ... do nothing if MANIFEST exists and the list of files didn’t change

target : MANIFEST $(ALL_FILES)
        command -o $@
(and you’ll make the all target, and not the target target (!), of course).
Reply With Quote
  #5  
Old 10th October 2010, 02:04 PM
axel Offline
Registered User
 
Join Date: Apr 2005
Location: Thessaloniki, Greece
Posts: 290
linuxchrome
Re: Question about a makefile

SaGS, I've already tried what you written in the above post. This makefile is part of a larger project and is executed through some other makefile. My problem was that I couldn't correctly call make all from the other makefiles and although the compile of this makefile was completed successfully and just like I wanted I couldn't make the rest of the compile to be completed.

I'll check it tomorrow again though. Thanks.
Reply With Quote
  #6  
Old 10th October 2010, 02:09 PM
jpollard Online
Registered User
 
Join Date: Aug 2009
Location: Waldorf, Maryland
Posts: 6,820
linuxfedorafirefox
Re: Question about a makefile

What is usually done is to implement a "clean" target. The command for this target
usually just deletes things that can be derived.

This can be done before updating DIR.

In the makefiles I usually use, I am generating object files and executables.

A make clean then just removes all object files (*.o), This works as all derived
files have extensions of .o

If I want to also remove the executables, then I put a "realclean" target that
also removes the executables - which can be in a list.
Reply With Quote
  #7  
Old 10th October 2010, 02:12 PM
axel Offline
Registered User
 
Join Date: Apr 2005
Location: Thessaloniki, Greece
Posts: 290
linuxchrome
Re: Question about a makefile

jpollard I know that make clean would solve the problem but if I recompile that object a lot of other object files will need to be recompiled. And I don't want that... Recompile should occur only if a file is deleted/added in the directory. Thanks though.
Reply With Quote
  #8  
Old 10th October 2010, 02:23 PM
jpollard Online
Registered User
 
Join Date: Aug 2009
Location: Waldorf, Maryland
Posts: 6,820
linuxfedorafirefox
Re: Question about a makefile

One other technique I have seen in makefiles is something like:
Code:
target: ...
       for i in *.whatever; do
                     ....
       done
Usually this is in a top level make file though.

You don't usually need to recompile just because a file was removed (unless that file
is an object file). Deleting source files can leave dangling object files, but a make
clean (before final integration build test) usually sufficient.

Normally, a manifest doesn't include such files anyway. If you delete a source file
you CAN get complex with using ls -C1 *.o, ls -C1 *.src, strip off the suffix and match
source to objects. Any left over object files are deleted. But event that doesn't necessarily
remove the object file - it may exist in a project library. Deleting from a library is
possible, but it leaves the library file in a bit of a fragmented form, and if you are
creating shared libraries, the only way to remove it is to rebuild the shared library.

But this is usually more effort than it is worth to try and remove single intermediate
files. It's just easier (and usually faster) to just make clean, make. Besides testing
that all files required are present.

In the projects I've worked with (Kerberos, X, GCC, Perl...) there are always some
files that are not involved with creating the application. These are usually readme(s)
documentation files, todo lists, suggestions, goals, plans... Nothing to do with
the program as far as make goes, but are important with keeping track of the project
or subproject.

Last edited by jpollard; 10th October 2010 at 02:31 PM.
Reply With Quote
  #9  
Old 11th October 2010, 03:03 PM
axel Offline
Registered User
 
Join Date: Apr 2005
Location: Thessaloniki, Greece
Posts: 290
linuxfirefox
Re: Question about a makefile

Here is something else I've tried. make target.cpp produces a cpp file which contains among others a list of the files inside files directory. So I thought to compare those file names with the ones that currently exist in the directory. If there is a difference I remove target.cpp and I expect $target.cpp: $(wildcard $(ALL_FILES)) to rebuild it. However I ended up with an infinite loop in the make process. Any ideas about that?

Code:
$ cat target.cpp
files/file1.txt
files/file2.txt
files/file2.txt
Code:
check_files_cmd = \
grep "files/" files/target.cpp > z1.txt; \
ls -1 files/*.txt > z2.txt; \
if [ -f files/target.cpp ]; then if [ "cmp z1.txt z2.txt" ]; then rm -f files/target.cpp; fi; fi
$(shell $(check_files_cmd))

$target.cpp: $(wildcard $(ALL_FILES))
        command -o $@
Reply With Quote
Reply

Tags
makefile, question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
makefile question daviddoria Using Fedora 2 24th November 2009 11:16 PM
% in makefile daviddoria Using Fedora 3 23rd April 2008 01:37 AM
Makefile??? bradley Using Fedora 15 23rd January 2008 11:12 AM
Makefile Elbryan Using Fedora 13 11th January 2006 04:45 PM
Question about makefile Stevenisme Programming & Packaging 13 29th September 2005 12:56 PM


Current GMT-time: 01:16 (Friday, 29-08-2014)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat