1 .\" -*- mode: text; coding: utf-8; -*-
3 .\" Copyright ©2014, 2017 Free Software Foundation
4 .\" 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
6 .\" Permission is hereby granted, free of charge, to any person
7 .\" obtaining a copy of this software and associated documentation
8 .\" files (the "Software"), to deal in the Software without restriction,
9 .\" including, without limitation, the rights to use, copy, modify,
10 .\" merge, publish, distribute, sublicense, and sell copies of
11 .\" the Software, and to permit persons to whom the Software is
12 .\" furnished to do so, subject to the following conditions:
14 .\" The above copyright notice and this permission notice shall be
15 .\" included in all copies, or substantial portions, of the Software;
17 .\" THE SOFTWARE IS PROVIDED "AS IS," WITHOUT WARRANTY OF ANY KIND,
18 .\" EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19 .\" OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
20 .\" NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 .\" HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY,
22 .\" WHETHER IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING
23 .\" FROM, OUT OF, OR IN CONNECTION WITH, THE SOFTWARE, OR THE USE OF,
24 .\" OR OTHER DEALINGS IN, THE SOFTWARE.
26 .\" Formatted with the mom macros
27 .\" .RW (reduce) and .EW (expand) control track kerning
28 .\" .WS controls word spacing
29 .\" Hanging punctuation and hyphens are inserted manually
31 .TITLE "Using Automake in the Groff project"
32 .AUTHOR "Bertrand Garrigues"
33 .COPYRIGHT "2014, 2017 Free Software Foundation"
34 .COVER TITLE AUTHOR DOCTYPE COPYRIGHT
39 .HEADING_STYLE 1 NUMBER
40 .HEADING_STYLE 2 NUMBER
41 .HEADING_STYLE 3 NUMBER
42 .HEADING_STYLE 4 NUMBER
51 .TOC_ENTRY_STYLE 2 FONT I
54 .NO_SHIM \" Flex-spaced
59 This is a quick overview of how to use `automake' in the groff
60 project, and is intended to help the developers and contributors
61 find their way when they have to make changes to the sources files
62 or to the data that are installed. If you need more details on
63 `automake', here are some reading suggestions:
73 .PDF_WWW_LINK https://www.gnu.org/software/automake/manual/automake.html
76 A book by John Calcote, with good practical examples:
78 .PDF_WWW_LINK http://fsmsh.com/2753
81 This site, by Diego Petteno, with good practical examples too:
83 .PDF_WWW_LINK https://autotools.io/index.html
89 .HEADING 1 "Overview, the initial build"
91 .HEADING 2 "First build"
94 Groff integrates the `gnulib' and uses its `bootstrap' script. When
95 compiling from the git repository, you should first invoke this
111 Clone the gnulib repository as a git submodule in 'gnulib',
112 add the needed gnulib sources files in `lib',
113 add the needed gnulib m4 macros in `gnulib_m4'.
116 Invoke autoreconf that will call all the `GNU autotools' (`aclocal',
117 `autoheader', `autoconf', `automake') in the right order for
118 creating the following files:
123 INSTALL (a symlink to gnulib's INSTALL file)
131 build-aux/ (that contains all the helper scripts)
135 src/include/config.hin
145 Note that aclocal.m4 is generated and the groff m4 macros are
146 included via the acinclude.m4 file.
151 At this point you can invoke the `configure' script and call `make'
152 to build the groff project. You can do it in the source tree:
159 You can also build groff in an out-of-source build tree, which is
169 Note that parallel build is also supported and `make' can be invoked
170 with the -j option, which will greatly speed up the build.
172 .HEADING 2 "Automake in the autotools process"
175 Automake's main job is to generate a Makefile.in file (this file is
176 maintained manually on projects using only autoconf). The main file
177 processed by `automake' is the Makefile.am file, which eventually
178 generates a Makefile. The (simplified) process is:
187 `aclocal' generates the `aclocal.m4' file from `configure.ac' and
188 the user-defined macros in `acinclude.m4'.
190 `autoheader' generates config.h.in.
192 `autoconf' generates the `configure' script from `aclocal.m4' and `configure.ac'
194 `automake' generates Makefile.in from Makefile.am and the
195 `configure.ac' file. It also generates some helper scripts, on the
196 groff project they are located in build-aux.
198 `configure' generates `config.status'
200 `config.status' generates the Makefile and config.h.
209 Finally, `autoreconf' is the program that can be used to call these
210 various tools in the correct order.
215 Automake defines a set of special variables that are used to
216 generate various build rules in the final Makefile. Note however
217 that if Automake's pre-defined rules are not enough, you still have
218 the possibility of adding handwritten standard `make' rules in a
219 Makefile.am; these rules will be copied verbatim in the Makefile.in
220 and then in the final Makefile.
222 .HEADING 2 "Modification of autotools files"
225 Previously, when groff used `autoconf' only and not `automake',
226 you had to invoke manually the autotools, depending on what you
227 modified. For example, to change the file `aclocal.m4', you had
228 to run the shell command 'aclocal -I m4'; to recreate the files
229 `configure' and `Makefile', you had to use the command 'autoreconf
232 Now, as groff uses `automake', you don't need to run `autoreconf'.
233 If you make some changes in Makefile.am or configure.ac, all the
234 files that need to be updated will be regenerated when you execute
237 .HEADING 1 "Building a program"
239 .HEADING 2 "A program and its source files"
242 Generally speaking, when using `automake' you will have to write a
243 Makefile.am file and use the variable \*[CODE]bin_PROGRAMS\*[CODE OFF]
244 to declare a program that should be built, and then list the
245 sources of this program in a variable that starts with the name of
246 your program and ends with \*[CODE]_SOURCES\*[CODE OFF]\&. In the
247 groff project we have only 1 top-level Makefile.am that includes
250 Take for example the build of grolbp, in src/devices/grolbp/grolbp.am.
251 The file starts with:
254 bin_PROGRAMS += grolbp
257 This says that a program named `grolbp' is added to the list of the
258 programs that should be built. Note that \*[CODE]bin_PROGRAMS\*[CODE OFF]
259 is initialized to an empty string in the top-level Makefile.am,
260 which includes grolbp.am. (We will see later why we don't write
262 \*[CODE]bin_PROGRAMS\~=\~grolbp\*[CODE OFF] in a Makefile.am in the
265 Then, we list the sources of grolbp like this:
271 src/devices/grolbp/lbp.cpp \\
272 src/devices/grolbp/lbp.h \\
273 src/devices/grolbp/charset.h
277 As you added `grolbp' to \*[CODE]bin_PROGRAMS\*[CODE OFF],
278 you need to define the sources of grolbp in the variable
279 \*[CODE]grolbp_SOURCES\*[CODE OFF]\&. If you write in another file
280 \*[CODE]bin_PROGRAMS += foo\*[CODE OFF] you will list the sources
281 of `foo' in \*[CODE]foo_SOURCES\*[CODE OFF]\&.
283 With these two statements, the resulting generated Makefile
284 will contain everything that is needed to build, clean,
285 install and uninstall the `grolbp' binary when invoking the
286 adequate `make' command. Also, the source files listed in
287 \*[CODE]grolbp_SOURCES\*[CODE OFF] will automatically be included in
288 the distribution tarball. That is why the headers are also listed
289 in \*[CODE]grolbp_SOURCES\*[CODE OFF]: it is not necessary to add
290 them in order to correctly build `grolbp', but this way the headers
291 will be distributed. Note that:
300 The path to the files are relative to the top-level directory.
302 The binaries are generated in the top-level build directory.
304 The .o files are generated in the directory where the source files
305 are located, or, in the case of an out-of-source build tree, in a
306 directory that is the replication of the source tree directory.
307 For example if you built groff in a `build' directory, lbp.o
308 (object file from src/devices/grolbp/lbp.cpp) will be located in
309 build/src/devices/grolbp/lbp.o.
316 We will also see later the reasons; this is due to the non-recursive
319 .HEADING 2 "Linking against a library"
322 To list which libraries grolbp needs to link against, we just write:
327 grolbp_LDADD = $(LIBM) \\
334 Again, we use the variable \*[CODE]grolbp_LDADD\*[CODE OFF] because
335 we added a program named `grolbp'. This will also automatically
336 set build dependencies between `grolbp' and the libraries it needs:
337 `libdriver.a' and `libgroff.a', that are convenience libraries built
338 within the groff project, will be compiled before grolbp.
340 .HEADING 2 "Preprocessor flags"
343 Preprocessor flags that are common to all the binaries are listed
344 in the variable \*[CODE]AM_CPPFLAGS\*[CODE OFF] in the top-level
345 Makefile.am. If a `foo' binary needs specific preprocessor
346 flags, use \*[CODE]foo_CPPFLAGS\*[CODE OFF], for example, in
347 src/devices/xditview/xditview.am, extra flags are needed to build
348 gxditview and are added like this:
353 gxditview_CPPFLAGS = $(AM_CPPFLAGS) $(X_CFLAGS) -Dlint \\
354 -I$(top_builddir)/src/devices/xditview
359 The use of specific CPPFLAGS changes the name of the generated objects:
360 the .o object files are prefixed with the name of the program.
361 For example, the .o file corresponding to src/devices/xditview/device.c
362 will be src/devices/xditview/gxditview-device.o.
364 .HEADING 2 "Cleaning"
367 You don't need to write rules to clean the programs listed in
368 \*[CODE]bin_PROGRAMS\*[CODE OFF], `automake' will write them for
369 you. However, some programs might have generated sources that
370 should be cleaned. In this case, you have mainly two special
371 variables to list extra files that should be cleaned:
380 \*[CODE]MOSTLYCLEANFILES\*[CODE OFF] for files that should be
381 cleaned by `make mostlyclean'
383 \*[CODE]CLEANFILES\*[CODE OFF ] for files that should be cleaned by
391 There is also the possibility of writing custom rules. We will see
394 .HEADING 2 "Dependencies"
397 We have already seen that when linking against a convenience
398 library, the dependencies are already created by `automake'.
399 However, some dependencies still need to be manually added, for
400 example when a source file includes a generated header. In this
401 case, the easiest way is to add a plain-make dependency. For
402 example, src/roff/groff/groff.cpp includes defs.h, which is a
403 generated header. We just add in src/roff/groff/groff.am:
406 src/roff/groff/groff.$(OBJEXT): defs.h
413 Apart from \*[CODE]bin_PROGRAMS\*[CODE OFF], there is another
414 similar special variable for scripts: \*[CODE]bin_SCRIPTS\*[CODE OFF]\&.
415 The scripts listed in this variable will automatically be
416 built (of course you have to provide your custom rule to build the
417 script), installed and uninstalled when invoking 'make', 'make
418 install' and 'make uninstall'. The main difference is that unlike
419 the programs listed in \*[CODE]bin_PROGRAMS\*[CODE OFF], the scripts
420 will not be cleaned by default. They are not distributed by default
421 either. In the groff project, \*[CODE]bin_SCRIPTS\*[CODE OFF] are
422 cleaned because they are added to \*[CODE]MOSTLYCLEANFILES\*[CODE OFF]
423 in the top-level Makefile.am.
425 A simple example are the gropdf and pdfmom scripts in
426 src/devices/gropdf/gropdf.am:
431 bin_SCRIPTS += gropdf pdfmom
433 gropdf: $(gropdf_dir)/gropdf.pl $(SH_DEPS_SED_SCRIPT)
435 sed -f $(SH_DEPS_SED_SCRIPT) \\
436 -e "s|[@]VERSION[@]|$(VERSION)|" \\
437 -e "s|[@]PERL[@]|$(PERL)|" \\
438 -e "s|[@]GROFF_FONT_DIR[@]|$(fontpath)|" \\
439 -e "s|[@]RT_SEP[@]|$(RT_SEP)|" $(gropdf_dir)/gropdf.pl >$@
442 pdfmom: $(gropdf_dir)/pdfmom.pl $(SH_DEPS_SED_SCRIPT)
444 sed -f $(SH_DEPS_SED_SCRIPT) \\
445 -e "s|[@]VERSION[@]|$(VERSION)|" \\
446 -e "s|[@]PERL[@]|$(PERL)|" $(gropdf_dir)/pdfmom.pl >$@
451 Note that in this example the '@' symbol is protected by square
452 brackets to prevent the substitution of the variable by `automake'.
454 .HEADING 1 "Non-recursive make schema"
457 There are two possibilities for organizing the Makefile.am of a
458 large project, using a recusive or a non-recursive `make'.
460 .HEADING 2 "1st possibility: make recursion"
463 A top level Makefile.am includes another Makefile.am, using the
464 \*[CODE]SUBDIRS\*[CODE OFF] directive, and the Makefile.am of each
465 sub-directory lists the programs that should be built. If we had
466 chosen this type of organization, we would have a Makefile.am in
467 src/devices/grolbp and in each directory that contain sources to
468 build a program (tbl, eqn, troff etc ...). We would write in the
469 top-level Makefile.am:
474 SUBDIRS = src/devices/grolbp \\
475 \&... (and all the dir that build a program or a script)
478 and in src/devices/grolbp, we would have a file Makefile.am that
482 bin_PROGRAMS = grolbp
483 grolbp_SOURCES = lbp.cpp lbp.h charset.h
487 Only `grolbp' is affected to the variable \*[CODE]bin_PROGRAMS\*[CODE OFF]\&.
488 It would be the same in, say, src/roff/troff: you would have a Makefile.am
489 with \*[CODE]bin_PROGRAMS = troff\*[CODE OFF]\&. We would have
490 one generated Makefile per Makefile.am file: in the build tree
491 you will have the top-level Makefile, grolbp's Makefile in
492 src/devices/grolbp, troff's Makefile in src/roff/troff, and so on.
493 When calling `make' to build everything, `make' will be recursively
494 called in all the directories that have a Makefile. Thus, the
495 paths are logically relative to the directory that contains the
498 This approach has the disadvantage of making dependencies harder
499 to resolve: each Makefile does not know the targets of the other
500 Makfiles. It also makes the build slower.
502 .HEADING 2 "Non-recursive make used by the Groff project"
505 The second possibility, which was chosen for the groff project, is to use
506 a non-recursive make schema. It is described in paragraph 7.3 of
507 the Automake manual ("An Alternative Approach to Subdirectories"),
508 based on the following paper from Peter Miller:
509 .PDF_WWW_LINK http://miller.emu.id.au/pmiller/books/rmch/ \
510 SUFFIX . "\*[IT]Recursive Make Considered Harmful\*[PREV]"
512 The idea is to have a single Makefile that contains all the rules.
513 That is why we have only a single Makefile.am in the top-level
514 directory which includes all the .am files that define rules
515 to build the various programs. The inclusion is done with the
516 \*[CODE]include\*[CODE OFF] directive, not \*[CODE]SUBDIRS\*[CODE OFF]\&.
517 Using 'include' is like copying the contents of the included
518 file into the top-level Makefile.am, and will not generate other
521 We first say in this top-level Makefile.am:
527 and then all the .am files that define a program to be built (e.g.
528 src/devices/grolbp/grolbp.am, src/roff/troff/troff.am, and so on)
529 overload this variable, so that at the end, all the programs that
530 should be built are listed in this \*[CODE]bin_PROGRAMS\*[CODE OFF]
531 variable. This is the reason why all the paths in the various .am
532 files are relative to the top-level directory: at the end we will
533 have only one Makefile in the top-level directory of the build tree.
535 As the resulting single Makefile knows all the targets, the
536 dependencies are easier to manage. The build is also faster,
537 particularly when compiling a single file: `make' is called once only
538 and the file will be instantly rebuilt, while on a recursive make
539 system, `make' will have to be invoked in all the sub-directories.
541 Note also that in order to make `gnulib' work with this
542 non-recursive schema, the `non-recursive-gnulib-prefix-hack'
543 configuration should be selected in bootstrap.conf.
545 .HEADING 1 "Installing data"
548 Variables that end with \*[CODE]_DATA\*[CODE OFF] are special
549 variables used to list files that should be installed in a
550 particular location. The prefix of the variables should refer to
551 another previously defined variable that ends with a `dir' suffix.
552 This varibale that ends with `dir' defines where the files should be
555 .HEADING 2 "A simple case"
558 For example, in font/devX100/devX100.am, we can see this:
562 devX100fontdir = $(fontdir)/devX100
563 devX100font_DATA = $(DEVX100FONTS)
566 EXTRA_DIST += $(DEVX100FONTS)
570 \*[CODE]DEVX100FONTS\*[CODE OFF] is just a list of font files,
571 defined at the begining of devX100.am. \*[CODE]fontdir\*[CODE OFF]
572 is where all the font directories are installed, it is defined
573 in the top-level Makefile.am. The conditional
574 \*[CODE]if\~!WITHOUT_X11\*[CODE OFF]
575 is used to prevent the installation of
576 these files if X11 is not available.
579 We first define where we wants to install the devX100 fonts with:
582 devX100fontdir = $(fontdir)/devX100
585 Because we declared a variable ending with `dir', we are allowed
586 to define \*[CODE]devX100font_DATA\*[CODE OFF] (you remove the
587 `dir' suffix and add \*[CODE]_DATA\*[CODE OFF]). Note that
588 wildcards are not supported in the special variable that end with
589 \*[CODE]_DATA\*[CODE OFF]\&.
591 With these two lines, `make install' will install the files
592 listed in \*[CODE]DEVX100FONTS\*[CODE OFF] and `make uninstall'
593 will uninstall them. \*[CODE]devX100fontdir\*[CODE OFF] will be
594 automatically created if missing during the installation
595 process, but not removed during the uninstall. The complete
596 \*[CODE]fontdir\*[CODE OFF] is removed by a custom uninstall rule
597 (uninstall_groffdirs in Makefile.am).
599 Because the files listed in \*[CODE]devX100font_DATA\*[CODE OFF]
600 are not distributed by default, we explicitely added them to the
601 \*[CODE]EXTRA_DIST\*[CODE OFF] variable, which lists all the files
602 that should be distributed and that are not taken into account by
603 the default automake rules.
606 EXTRA_DIST += $(DEVX100FONTS)
609 Another possibility would have been to add a `dist' prefix to the
610 \*[CODE]devX100font_DATA\*[CODE OFF] variable, in this case the use
611 of \*[CODE]EXTRA_DIST\*[CODE OFF] is useless (except of course if
612 \*[CODE]WITHOUT_X11\*[CODE OFF] is true, in this case we don't
613 install the files but we still have to distribute them):
617 devX100fontdir = $(fontdir)/devX100
618 dist_devX100font_DATA = $(DEVX100FONTS)
620 EXTRA_DIST += $(DEVX100FONTS)
625 .HEADING 2 "Dealing with generated files"
628 In the previous example, all the font files that must be installed
629 were already present in the source tree. But in some cases,
630 you need to generate the files you intend to install. In this
631 case, the files should be installed but not distributed. A
632 simple way to deal with this is to add a `nodist' prefix to your
633 \*[CODE]xxx_DATA\*[CODE OFF] variable.
635 For example in font/devps/devps.am, we have a list of
636 font files already present in the source tree, defined
637 by \*[CODE]DEVPSFONTFILES\*[CODE OFF], and another list
638 of font files that are generated, listed in the variable
639 \*[CODE]DEVPSFONTFILES_GENERATED\*[CODE OFF]\&. They should all
640 by installed in a `devps' directory under the fontdir. Thus
641 the following three lines, where we use the `dist' and `nodist'
645 devpsfontdir = $(fontdir)/devps
646 dist_devpsfont_DATA = $(DEVPSFONTFILES)
647 nodist_devpsfont_DATA = $(DEVPSFONTFILES_GENERATED)
650 The generated files are not cleaned by default, thus we add:
653 MOSTLYCLEANFILES += $(DEVPSFONTFILES_GENERATED)
657 .HEADING 1 "Extending Automake's rules"
659 .HEADING 2 "Local clean rules"
662 In most of the cases, the files that need to be cleaned are
663 automatically determined by `automake', or were added to the
664 \*[CODE]MOSTCLEANFILES\*[CODE OFF] or \*[CODE]CLEANFILES\*[CODE OFF]
665 variables. However, you might need to define a specific rule
666 to clean some files that were not added to any list. Automake
667 defines a set of targets to extend the clean targets with your
668 own rules: clean-local, mostlyclean-local, distclean-local or
669 maintainerclean-local. An example of such extension exists in
670 font/devpdf/devpdf.am: because some fonts are not explicitely listed
671 in a \*[CODE]xxx_DATA\*[CODE OFF] variable but generated by a custom
672 rule, we define an extra rule to extend the `mostlyclean' target:
676 mostlyclean-local: mostlyclean_devpdf_extra
677 mostlyclean_devpdf_extra:
678 @echo Cleaning font/devpdf
679 rm -rf $(top_builddir)/font/devpdf/enc \\
680 $(top_builddir)/font/devpdf/map;
681 if test -d $(top_builddir)/font/devpdf; then \\
682 for f in $(GROFF_FONT_FILES); do \\
683 rm -f $(top_builddir)/font/devpdf/$$f; \\
689 .NO_FLEX OFF \" Prevent upcoming NEWPAGE from disabling flex-spacing.
690 .HEADING 2 "Local install/uninstall rules and hooks"
693 Similarly to the clean rules, there are extensions to install and
694 uninstall rules. They come with two flavous, local rules and hooks.
703 There are 2 rules to extend install commands: `install-exec-local'
704 for binaries and `install-data-local' for data.
706 There is 1 uninstall local rule: `uninstall-local'.
713 There are no garantees on the order of execution of these local
714 rules. An example of local rule is the installation of GXditview.ad
715 and GXditview-color.ad files in src/devices/xditview/xditview.am: if
716 theses files are already installed, the old files are first saved.
717 Also, the final file that is installed is stripped from its .ad
718 suffix. Thus the usage of a custom rule rather than the definition
719 of a \*[CODE]xxx_DATA\*[CODE OFF] variable:
723 # Custom installation of GXditview.ad and GXditview-color.ad
724 install-data-local: install_xditview
725 uninstall-local: uninstall_xditview
728 install_xditview: $(xditview_srcdir)/GXditview.ad
729 -test -d $(DESTDIR)$(appresdir) \\
730 || $(mkinstalldirs) $(DESTDIR)$(appresdir)
731 if test -f $(DESTDIR)$(appresdir)/GXditview; then \\
732 mv $(DESTDIR)$(appresdir)/GXditview \\
733 $(DESTDIR)$(appresdir)/GXditview.old; \\
736 $(INSTALL_DATA) $(xditview_srcdir)/GXditview.ad \\
737 $(DESTDIR)$(appresdir)/GXditview
741 Hooks, on the other hand, are garanteed to be executed after all the
742 standard targets have been executed.
752 There are 2 install hooks: `install-exec-hook' and
755 There is 1 uninstall hook: `unintall-hook'
763 An example of hook is the `uninstall_groffdirs' rule in the
764 top-level Makefile.am. This hook is used to remove all the
765 directories specific to groff introduced by the installation
766 process. Obviously it could not be a local extension of `uninstall'
767 because the order of execution is not guaranteed.
770 # directories specific to groff
771 uninstall-hook: uninstall_groffdirs
773 if test -d $(DESTDIR)$(datasubdir); then \\
774 rm -rf $(DESTDIR)$(fontdir); \\
775 rm -rf $(DESTDIR)$(oldfontdir); \\
776 rmdir $(DESTDIR)$(datasubdir); \\
785 .\" vim: filetype=groff: