Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate a source file when building using autotools

With Make, I do something like

generated.c: input.txt generator
    ./generator input.txt > generated.c

How would I get equivalent functionality out of autotools? (removing generated.c on cleaning would also be a bonus).

like image 403
Jeremy Salwen Avatar asked Jun 08 '11 20:06

Jeremy Salwen


1 Answers

I'm assuming that input.txt is a source file that is distributed, that you do not want to distribute generated.c (i.e., it must be built by each user), and that generator is a program built by your package too.

EXTRA_DIST = input.txt
bin_PROGRAMS = prog
noinst_PROGRAMS = generator

# Source files for the generator
generator_SOURCES = generator.c ...

# Source files for prog
prog_SOURCES = prog.c ...
nodist_prog_SOURCES = generated.c 

# generated.c is a built source that must be cleaned
CLEANFILES = generated.c

# Build rule for generated.c
generated.c: $(srcdir)/input.txt generator$(EXEEXT)
        ./generator$(EXEEXT) $(srcdir)/input.txt

In some situations, like when generating header files, it is important to generate the file before the rest of the sources are compiled, so it can be included. In that case you should also list the generated file in the variable BUILT_SOURCES (The Automake manual has many example in its Built sources section. In the above case it is not necessary.

EDIT: I have fixed the above Makefile.am example to declare generator as a non-installed program.

like image 51
adl Avatar answered Sep 18 '22 13:09

adl