How to generate a source file when building using autotools
Asked Answered
P

1

11

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).

Pogrom answered 8/6, 2011 at 20:35 Comment(0)
F
19

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.

Facient answered 9/6, 2011 at 5:30 Comment(3)
Thank you, exactly what I was looking for, and references to read more, too!Pogrom
I have a follow-on question for this, regarding how to make this work for VPATH builds.Skyline
@Craig Answered. HTH.Facient

© 2022 - 2024 — McMap. All rights reserved.