The whole thing is not about automake, but rather about writing makefiles.
Actually, you can try the .gch, but there're restrictons:
- write one common header to include everything (like
stdafx.h
)
- It should be the 1st include in all your sources
- use same
CFLAGS
to compile all your sources
It's a new functionality. You'd want to detect it in your configure.ac
Write a rule in your Makefile.am
, name it like stdafx.gch:
. Make it empty, if gch
is not supported:
stdafx.gch: stdafx.h
$(OPTIONAL_COMPILE_GCH)
.PHONY: $(OPTIONAL_STDAFX_GCH)
Make your _OBJECTS
depend on stdafx.gch
:
$(foo_SOURCES:.cpp=.$(OBJEXT)): stdafx.gch
# or (is it documented var?)
$(foo_OBJECTS)): stdafx.gch
Youc can use the documented CXXCOMPILE command to be sure that all CXXFLAGS are the same.
Detect in your configure.ac
:
if ...; then
[OPTIONAL_COMPILE_GCH='$(CXXCOMPILE) -c -o $@ $<']
[OPTIONAL_STDAFX_GCH=]
else
[OPTIONAL_COMPILE_GCH=]
[OPTIONAL_STDAFX_GCH='stdafx.gch']
fi
AC_SUBST(OPTIONAL_COMPILE_GCH)
AC_SUBST(OPTIONAL_STDAFX_GCH)
Update
You want your .gch
to be recompiled, when any header file indirectly used by stdafx.h
is modified.
Then you could add stdafx.cpp
that does nothing, but includes stdagx.h
and make .gch
depend on stdafx.o
. This way the dependency tracking would be managed by automake
, but there's a problem:
stdafx.o
itself could make use of .gch
to compile faster, but if we add such dependency, it would be circular. It's not easy for me to find a solution.
Here's an example that uses status files to solve this: https://gist.github.com/basinilya/e00ea0055f74092b4790
Ideally, we would override the compilation command for stdafx.o
, so it first creates .gch
and then runs the standard automake compilation, but automake does not use $(CXXCOMPILE)
as is. It creates a complex recipe from it (and it depends on automake version):
Update 2
Another solution is to use gcc dependency tracking directly:
stdafx.h.gch: stdafx.h
g++ -MT stdafx.h.gch -MD -MP -MF .deps/stdafx.Tpo -c -o stdafx.h.gch stdafx.h
mv .deps/stdafx.Tpo .deps/stdafx.Po
-include .deps/stdafx.Po
By default, if precompiled header is used, gcc -MD
will not put the list of header files into the generated dependencies file, just .gch
. There's an option to fix this : -fpch-deps
(see bug), but perhaps the default behavior is not that bad: if only the .gch
depends on the headers, make will have less work to do.