Page 1 of 1

Using makefile to create tar archive

Posted: Sat Apr 04, 2009 4:14 pm
by FlashBurn
I want to use a makefile to create a tar archive. 1st problem I have is that I need a rules for things where I can´t make one:

Code: Select all

archive.tar: file1.txt file2.txt
   tar cvf archive.tar file1.txt file2.txt
So here I can´t have rules for file1.txt and file2.txt. So what can I do here?

2nd problem is that I have directories which go into the tar archive. So I first have to collect all the files which are in the sub-directories and then use this list as dependency for the archive.tar. So I get the same problem as above and the problem that I don´t know how to collect all files of given sub-directories.

Is this possible with a makefile, if not I have to delve into shell programming.

Re: Using makefile to create tar archive

Posted: Sat Apr 04, 2009 4:28 pm
by JamesM
Hi,
FlashBurn wrote:I want to use a makefile to create a tar archive. 1st problem I have is that I need a rules for things where I can´t make one:

Code: Select all

archive.tar: file1.txt file2.txt
   tar cvf archive.tar file1.txt file2.txt
So here I can´t have rules for file1.txt and file2.txt. So what can I do here?
I'm not quite certain what you're asking here - if file1.txt and file2.txt are not automatically generated (by other parts of the makefile for example), they don't need to be in the dependency list.
2nd problem is that I have directories which go into the tar archive. So I first have to collect all the files which are in the sub-directories and then use this list as dependency for the archive.tar. So I get the same problem as above and the problem that I don´t know how to collect all files of given sub-directories.

Is this possible with a makefile, if not I have to delve into shell programming.
Likewise, the only files that need to be specified in the dependency list are those that are generated by other parts of the Makefile. Obviously all your files won't be, so there's no need to add them as dependencies. Just use "tar -cf directory/*", and let tar do everything recursively automatically (it defaults to that).

Cheers,

James

Re: Using makefile to create tar archive

Posted: Sat Apr 04, 2009 4:37 pm
by JackScott
First, I suggest having a look at Solar's magnificent Makefile tutorial on the wiki: Makefile. It can be hard to understand, but once you wrap your head around it, the ideas in it help a lot. Your question is answered.

Secondly, here is an excerpt from my own Makefile:

Code: Select all

backup:
        @cd ../ && tar czf ~/backups/synergy/sources/synergy-`date +%Y.%m.%d`.tar.gz synergy/
This rule basically goes up one level in the directory tree (from ~/projects/synergy to ~/projects), and from outside my kernel's source tree runs tar over the whole lot. It spits out a dated (YYYY-MM-DD) tar archive and puts it in a backup directory automatically. You're quite welcome to use this if you like. It doesn't cope well with Subversion subdirectories (you would have to add a clause to tar to omit them), but I don't use Subversion so I don't worry.