尽管有评论,但我最终还是这样做了。我在问题中没有提到的是这种自动生成已经完成,但是以一种非常特别的方式(cat
将各种文件组合在一起,其中一些是动态生成的,以创建 configure.ac),我只是想把它清理干净。
在构建脚本中,我们有:
confdir="config/configure.ac_scripts"
# Generate a sorted list of all the makefiles in the project, wrap it into
# an autoconfigure command and put it into a file.
makefile_list="$(find -type f -name 'Makefile.am' \
| sed -e 's:Makefile\.am:Makefile:' -e 's:^./::' \
| sort)"
# A bit more explanation of the above command:
# First we find all Makefile.ams in the project.
# Then we remove the .am using sed to get a list of Makefiles to create. We
# also remove the "./" here because the autotools don't like it.
# Finally we sort the output so that the order of the resulting list is
# deterministic.
# Create the file containing the list of Makefiles
cat > "$confdir/new_makefile_list" <<EOF
# GENERATED FILE, DO NOT MODIFY.
AC_CONFIG_FILES([
$makefile_list
])
EOF
# In case you haven't seen it before: this writes the lines between <<EOF
# and EOF into the file $confdir/new_makefile_list. Variables are
# substituted as normal.
# If we found some new dirs then write it into the list file that is
# included in configure.ac and tell the user. The fact that we have
# modified a file included in configure.ac will cause make to rerun
# autoconf and configure.
touch "$confdir/makefile_list"
if ! diff -q "$confdir/new_makefile_list" "$confdir/makefile_list" > /dev/null 2>&1;
then
echo "New/removed directories detected and $confdir/makefile_list updated,"
echo "./configure will be rerun automatically by make."
mv "$confdir/new_makefile_list" "$confdir/makefile_list"
fi
然后在 configure.ac 我们有:
# Include the file containing the constructed AC_CONFIG_FILES([....]) command
m4_include([config/configure.ac_scripts/makefile_list])
而不是直接写入 Makefile 列表。
完整的源代码在这里和这里。