The Makefiles subsystem is designed to optimize the local Makefile
with global Rules and Targets. There are two general Makefile fragments
that are included during generation of Makefile from Makefile.in. All Makefiles
must include these two general fragments that furnish general variables
and general target rules. Inclusion is done with the keywords @RULES@ and
@TARGETS@ in the Makefile.in. If you need to write a new Makefile, please
look at an existing Makefile for general information on how to organize
the Makefile. The fragments are in autoconf/Rules.mak.in and autoconf/Targets.mak.
For a complete understanding of the inner working, please look at these
two fragments and at the Makefiles.
There are some local variables to the Makefiles used by fragments. These
variables are:
The targets needed by each local Makefile are listed in the "all-targets"
directive.
This new design is made to ease Makefile.in management. To reach this objective all the common Makefile variables and rules are included from two common files at configure time.
To include these files, you must start your Makefile.in with the following fragment:
# Default variables @VARIABLES@ # TOP source directory. topdir = @top_srcdir@ top_builddir = $(topdir)
The directive @VARIABLES@ includes the file variables.mak, then, to define correctly the topdir and top_builddir variables, that are the root sources directory relative to the current path, the following two assignments must be done.
Now, to automate the traversing of subdirectories you must list all the subdirectories that contain a Makefile in the subdirs variable:
# # this is a list of all subdirectories that contain Makefiles # subdirs = @INTLSUB@ @POSUB@ drivers @WIN32@ src @CGI@
This to allow the Makefile subsystem to know where it must go to generate libraries and executables prior to make the current directory.
Following the subdirs directive, all the common targets must be included:
# Include the default make targets: to be put before the all-targets: rule. @TARGETS@
These @TARGETS@ include also the 'all:' target for convenience. To define all the targets to be made in the current directory you must specify them with the following directive:
all-targets: Makefile apcupsd apctest
After defining the all-targets you can define the make rules. Remembering that the generic '.c.o:' rule is already defined in targets.mak, we can simply define the rules to make the executables:
dobjs = daemonobj.o gobjs = genericobj.o apcupsd: $(dobjs) $(gobjs) $(LIBS) $(CC) $(LDFLAGS) $(dobjs) $(gobjs) $(LIBS) $(WINAPC) $(WINLIBS) \ -o apcupsd
In this example $(dobjs) and $(gobjs) are defined locally because they are local rules and dependancies for the apcupsd executable and not generic rules for all the package.
After that you must add the install and uninstall rules. Note that these rules are mandatory and if you have nothing to (un)install in the current directory you must define them as dummy targets.
install: uninstall:
Obviously this is a simple description on how to write a Makefile.in for apcupsd. If you follow these rules your Makefile.in files will be short, clean, readable and, I did my best for this, simple.
If one or more of your targets are executables, assign them to the $(allexe) variable and then add to all-targets: dependancy.
All the libraries are specified in the targets with their dependancies so any reference to a library will automatically generate a dependancy check.