Alex Brooks
Alex Brooks

Reputation: 1151

Automake: Run a script before traversing subdirectories

I have a build system setup for a library using the GNU autotools. The library has subdirectories for source and header files. It also contains a script in the top directory which auto-generates a source and header file (not the same as the config file) in the appropriate subdirectories. It is necessary that these files are generated before make is performed in the subdirectories.

What is the simplest way to have the script run before subdirectories are traversed (i.e. when user calls make, the script is ran before traversing SUBDIRS)? I have tried adding rules like all-local with no success.

EDIT:

Top-level directory:

configure.ac    Makefile.am    src    include    myscript.sh

Makefile.am:

EXTRA_DIST = myscript.sh
ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS} -I m4

SUBDIRS = src include

.PHONY: gen-script

gen-script:
  ./myscript.sh

src/Makefile.am:

AM_CPPFLAGS = -I$(top_srcdir)/include
lib_LTLIBRARIES = libmylib.la
libmylib_la_SOURCES = \
  file1.cxx \
  file2.cxx \
  autogen-file.cxx

clean-local:
  rm -f autogen-file.cxx

include/Makefile.am:

nobase_include_HEADERS = \
  file1.h \
  file2.h \
  autogen-file.h

clean-local:
  rm -f autogen-file.h

Upvotes: 1

Views: 1142

Answers (2)

Diego Elio Pettenò
Diego Elio Pettenò

Reputation: 3240

What you can do is force the current directory to run before the rest of the subdirectories with

SUBDIRS = . src include

More seriously, though, you should not use recursive automake. Particularly if your structure is relatively simple as it seems to be. If you have interests in looking up how to do that you can see my documentation on Autotools Mythbuser.

Upvotes: 1

5gon12eder
5gon12eder

Reputation: 25419

I think that the best solution would be to get rid of the recursive make and only have the top-level Makefile.am. Then you'd simply add a rule

include/autogen-file.h src/autogen-file.cxx: myscript.sh
    ${SHELL} $<

and list include/autogen-file.h in BUILT_SOURCES.

If you want to keep your recursive structure (even if this is considered harmful), you could place the rule to generate the files into the Makefile.ams in the sub-directories.

In src/Makefile.am:

autogen-file.cxx: ../myscript.sh
    cd ${top_srcdir} && ${SHELL} myscript.sh

And in include/Makefile.am:

autogen-file.h: ../myscript.sh
    cd ${top_srcdir} && ${SHELL} myscript.sh

By the way, how often do you need to re-build those? If the generated files only depend on the project configuration, you could simply run myscript.sh at the end of your configure.ac and delete the generated files only on make distclean.

Upvotes: 3

Related Questions