Using makefile to create tar archive

Programming, for all ages and all languages.
Post Reply
FlashBurn
Member
Member
Posts: 313
Joined: Fri Oct 20, 2006 10:14 am

Using makefile to create tar archive

Post 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.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: Using makefile to create tar archive

Post 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
User avatar
JackScott
Member
Member
Posts: 1031
Joined: Thu Dec 21, 2006 3:03 am
Location: Hobart, Australia
Contact:

Re: Using makefile to create tar archive

Post 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.
Post Reply