Back to the main page

(g)make and makefile

The make program is tool that helps automation in software building process.
It can also be used to automate any task which uses files, like having some framework and want to distribute config files to other machines. And this is my case, pushing config file to other systems.

The make uses makefile in order to learn what to do. By default, when gmake looks for the makefile, it tries the following order: GNUmakefile, makefile and Makefile. Use GNUmakefile only if you use gmake, other versions of make don't understand it.

The Makefile consists of rules, that looks like:
target ...: prerequisites ...
	command
	...
A target is name of action to be performed. If a rule does not have a prerequisite and its purpose is to run a command only, target is such rule is called phony target.
A prerequisite is a file that has some kind of input.
A command is an action to be performed. IMPORTANT: A tab character must be used at the beginning of every command line!

Here in this quick/short example, the Blastwave's CSWgmake is used (the binary is /opt/csw/bin/gmake). It's little framework for updating Logcheck problem/ignore files to remote central log servers.

> cat GNUmakefile
# two log servers
LOGHOST=        log-office-1
LOGHOST+=       log-office-2

# define commands
RSYNC=/opt/csw/bin/rsync -aHbessh --backup-dir=/usr/local/etc/logcheck/BAK --suffix=.`date +'%Y-%m-%dT%H:%M'`
SSH=/usr/bin/ssh
DESTDIR= /usr/local/etc/logcheck

# files to rsync
FILES=  global.ignore
FILES+= hw.ignore
FILES+= hw.problem
FILES+= security.ignore
FILES+= security.problem
FILES+= sw.ignore
FILES+= sw.problem
FILES+= unusual.ignore

# phony target
push: sync

sync: $(FILES)
        for host in $(LOGHOST) ; \
        do \
        echo "Rsync to system : $$host" ; \
                for files in ${FILES} ; \
                do \
                echo "Rsyncing ${DESTDIR}/$$files : \c" ; \
                $(RSYNC) $$files $$host:$(DESTDIR) ; \
                        if [ "$$?" -ne "0" ] ; then \
                        echo "PROBLEM?" ; \
                        exit 1 ; \
                        fi ; \
                        echo "OKAY" ; \
                        done ; \
        echo "Done with $$host \n" ; \
        done
Working screen output:
> gmake -s
Rsync to system : log-office-1
Rsyncing /usr/local/etc/logcheck/global.ignore : OKAY
Rsyncing /usr/local/etc/logcheck/hw.ignore : OKAY
Rsyncing /usr/local/etc/logcheck/hw.problem : OKAY
Rsyncing /usr/local/etc/logcheck/security.ignore : OKAY
Rsyncing /usr/local/etc/logcheck/security.problem : OKAY
Rsyncing /usr/local/etc/logcheck/sw.ignore : OKAY
Rsyncing /usr/local/etc/logcheck/sw.problem : OKAY
Rsyncing /usr/local/etc/logcheck/unusual.ignore : OKAY
Done with log-office-1

Rsync to system : log-office-2
Rsyncing /usr/local/etc/logcheck/global.ignore : OKAY
Rsyncing /usr/local/etc/logcheck/hw.ignore : OKAY
Rsyncing /usr/local/etc/logcheck/hw.problem : OKAY
Rsyncing /usr/local/etc/logcheck/security.ignore : OKAY
Rsyncing /usr/local/etc/logcheck/security.problem : OKAY
Rsyncing /usr/local/etc/logcheck/sw.ignore : OKAY
Rsyncing /usr/local/etc/logcheck/sw.problem : OKAY
Rsyncing /usr/local/etc/logcheck/unusual.ignore : OKAY
Done with log-office-2

Back to the main page