Autotools CMake ExternalProject

Building an Autotools project as a CMake ExternalProject saves the time of converting that other project to CMake. This technique makes it easy to automatically build that other project when it’s not easily installable otherwise, or you wish to build it optimized. This technique does not needlessly rebuild the ExternalProject each time the main CMake project is rebuilt.

It is useful to first check that Autotools is available to avoid emitting errors at build time. The goal is to emit errors about missing packages during build system configuration instead of during the build. We do this by including the file FindAutotools.cmake.

The UPDATE_DISCONNECTED true and CONFIGURE_HANDLED_BY_BUILD true avoid constant reconfigure / rebuild. “my_LIBRARY” is the known library file(s) built by the Autotools project.

include(GNUInstallDirs)
include(ExternalProject)

set(config_flags)  # parameters desired for ./configure of Autotools

set(my_LIBRARY ${CMAKE_INSTALL_FULL_LIBDIR}/${CMAKE_STATIC_LIBRARY_PREFIX}my${CMAKE_STATIC_LIBRARY_SUFFIX})

find_program(MAKE_EXECUTABLE NAMES gmake make mingw32-make REQUIRED)

ExternalProject_Add(my
GIT_REPOSITORY https://github.com/username/my.git
GIT_TAG main
UPDATE_DISCONNECTED true
CONFIGURE_HANDLED_BY_BUILD true
CONFIGURE_COMMAND <SOURCE_DIR>/configure ${config_flags}
BUILD_COMMAND ${MAKE_EXECUTABLE} -j
INSTALL_COMMAND ${MAKE_EXECUTABLE} -j install
TEST_COMMAND ""
BUILD_BYPRODUCTS ${my_LIBRARY}
)

add_library(my::my INTERFACE IMPORTED GLOBAL)
target_include_directories(my::my INTERFACE ${CMAKE_INSTALL_FULL_INCLUDEDIR})
target_link_libraries(my::my INTERFACE "${my_LIBRARY}")  # need the quotes to expand list
add_dependencies(my::my p4est)

Some Autotools projects may need a “bootstrap” before “configure”. Add this script if needed:

ExternalProject_Add_Step(my
bootstrap
COMMAND <SOURCE_DIR>/bootstrap
DEPENDEES download
DEPENDERS configure
)