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.
INSTALL_COMMAND
- Note that “-j” option is NOT used to avoid race conditions in install scripts that might intermittently fail
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_property(DIRECTORY PROPERTY EP_UPDATE_DISCONNECTED true)
# don't recheck for updates to the Git repo at subsequent CMake reconfigure
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
URL https://github.invalid/username/archive.tar.bz2
CONFIGURE_HANDLED_BY_BUILD true
CONFIGURE_COMMAND <SOURCE_DIR>/configure ${config_flags}
BUILD_COMMAND ${MAKE_EXECUTABLE} -j
INSTALL_COMMAND ${MAKE_EXECUTABLE} 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)
For Ninja BUILD_BYPRODUCTS
is necessary to avoid “ninja: error: “lib” needed by “target”, missing and no known rule to make it”
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
)