Building a library using autotools from cmake
Asked Answered
B

2

43

This is my first try with cmake and I would like to have, if possible, some feedbacks about what I did since some problems remain.

In the CMakeLists.txt of the library folder, I created two makefile targets: configure-antlr3c and antlr3c. The first target runs the autotools configuration shell script, the second one runs the make executable to build the library:

# CMakeLists.txt in libantlr3c-3.1.3 
add_custom_target(
  configure-antlr3c
  ${SHELL_EXECUTABLE} configure
  WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)

add_custom_target(
  antlr3c
   ${MAKE}
   DEPENDS configure-antlr3c
   WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)

The main problem is thatconfigure-antlr3c target is always "out of date", so it will always be executed even if no changes happened. Moreover, I necessarily need to generate my cmake makefiles in a separate directory (not in the root directory of my project) to avoid overriding the autotools Makefile of the library...

Has anyone had this problem (building autotools projects with cmake) ? And if so, what have been your solutions ?

Thank you.

EDIT : Solution In the root CMakeLists.txt:

include(ExternalProject)
ExternalProject_Add(
  libantlr3c
  SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/lib/libantlr3c-3.1.3
  CONFIGURE_COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/lib/libantlr3c-3.1.3/configure --prefix=${CMAKE_CURRENT_SOURCE_DIR}/lib/libantlr3c-3.1.3
  PREFIX ${CMAKE_CURRENT_SOURCE_DIR}/lib/libantlr3c-3.1.3
  BUILD_COMMAND make
  BUILD_IN_SOURCE 1
)
Bouffard answered 12/5, 2011 at 0:1 Comment(1)
You should add your solution to an Answer block; not in the question.Michigan
D
60

I think that you'd be better off using the ExternalProject feature of cmake. I guess you have your project and have libantrl in a sub directory?

project
      +- libantlr
      +- mysrc
  ---- etc ----

If that's the case, you can do something like this in the top level CMakeLists.txt:

cmake_minimum_required(VERSION 2.8)
project(test)
include(ExternalProject)
ExternalProject_Add(libantlr
    SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libantlr
    CONFIGURE_COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/libantlr/configure --prefix=<INSTALL_DIR>
    BUILD_COMMAND ${MAKE})

The <INSTALL_DIR> is expanded to something like libantlr-prefix, so things are installed in your build tree rather than in /usr/local, which is what autotools would do without a prefix.

Daveen answered 12/5, 2011 at 9:2 Comment(3)
Perfect ! And this keep track of the fact I just compiled it and I don't need to build it every time I have a dependency. Thanks a lot ;)Bouffard
Nice answer. Just take into account that "configure" needs an absolute path for "--prefix".Testimonial
It looks like wrong compiler is used when building's an external project. At least in the mbed tool chain.Euphonium
F
17

I posted the answer below almost four years ago and it seems that it is still occasionally being upvoted. The answer "works" but it problematic in several ways. Also, in the meantime CMake now has FetchContent which should replace ExternalProject in most if not all use cases. In fact, anyone reading this should consider using the CMake Package Manager (CPM) which wraps FetchContent and provides some very nice features while being a relatively minimal step up in complexity over FetchContent. The new example below uses FetchContent but could easily be modified to use CPM.

This code installs libgpiod but should be usable with only minor changes to for installing other packages built with autotools.


include(FetchContent)

# This integrates libgpiod into the CMake build process.
# libgpiod uses `autotools`, sometimes referred to as (autohell)[https://www.shlomifish.org/open-source/anti/autohell/].
# While the cmake code below is fairly clean, it was not so easy to discover.
# Here are some notes for things I figured out that may be useful to anyone
# trying to do something similar.
#
# 1. `autotools` typically is used to configure and build in the source directory.
#    This is not ideal for CMake, which prefers to build in a separate directory.
#    The pattern used below does the trick: set the WORKING_DIRECTORY to the
#    binary/build directory, but reference autgen.sh with the source directory path.
# 1b. `autotools` requires packages that we do not typically install in our build
#    images. I had to modify our docker image to include pkg-config & autoconf-archive.
#    The error messages before doing this were not very helpful.
# 2. `FetchContent` is nicer to use than `ExternalProject`. The structure below
#    is similar to how we use `FetchContent` elsewhere.
# 3. `autotools` uses the `CC` and `CXX` environment variables to determine the
#    compiler to use. We want to ensure that when cross-compiling these variables
#    are set to the correct compiler as defined by the toolchain. The code below
#    sets these variables in the environment for the `autogen.sh` script.
# 4. `autotools` uses the `--host` flag to determine the target architecture.
# 5. The INSTALL directory is simply the root of the build directory, e.g.
#    `builds/min-x86` in our build system. In that directory all of the built
#     library and header artifacts are placed into the `lib` and `include`
#     directories, respectively. This is the expected struture for CMake,
#     and we largely get it for free by using `FetchContent`.
# 6. We use `pkg-config` to find the installed library. This is nice but requires
#    that CMAKE_PREFIX_PATH include the INSTALL directory, which bizarrely is
#    not the default.
# 7. `pkg_check_modules` with the `IMPORTED_TARGET` option is able to create
#    a modern cmake target for the library. Such targets are always scoped with
#    `PkgConfig::`. In this case, the target is `PkgConfig::gpiod`.

set(INSTALL ${PROJECT_BINARY_DIR})

list(APPEND CMAKE_PREFIX_PATH ${INSTALL})
find_package(PkgConfig REQUIRED)

FetchContent_Declare(libgpiod
    GIT_REPOSITORY "https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git"
    GIT_TAG v2.0.1
    UPDATE_DISCONNECTED ON
)

# If libgpiod is already installed, we can use pkg-config to find it.
pkg_check_modules(gpiod IMPORTED_TARGET libgpiod)

if(NOT libgpiod_POPULATED AND NOT gpiod_FOUND)
    FetchContent_Populate(libgpiod)

    set(ENV${CC} ${CMAKE_C_COMPILER})
    set(ENV{CXX} ${CMAKE_CXX_COMPILER})

    # Configure
    execute_process(
        OUTPUT_QUIET
        COMMAND ${libgpiod_SOURCE_DIR}/autogen.sh --host=${CMAKE_SYSTEM_PROCESSOR} -enable-tools=yes --enable-bindings-cxx --prefix=${INSTALL}
        WORKING_DIRECTORY ${libgpiod_BINARY_DIR}
    )

    # Build
    execute_process(
        OUTPUT_QUIET
        COMMAND make
        WORKING_DIRECTORY ${libgpiod_BINARY_DIR}
    )

    # Install
    execute_process(
        OUTPUT_QUIET
        COMMAND make install
        WORKING_DIRECTORY ${libgpiod_BINARY_DIR}
    )

    pkg_check_modules(gpiod REQUIRED IMPORTED_TARGET libgpiod)

endif()

The 4-year old answer is below, which might be useful as reference.

I needed to do something similar but found it surprisingly difficult to get a working solution, despite the example provided here with the accepted answer, and code snippets provided in several other blog posts, the CMake support email listserv archives, etc. For the benefit of others who come across this question, here is my solution.

The external project we wanted to use is libmodbus, though I believe my solution is general enough to work with any project configured with the standard autoconf recipe of ./autoconf.sh && configure.sh && make && make install.

We wanted to add libmodbus as a submodule of our git repository. We added to our repository at the path <root>/opt/libmodbus. The CMake code to configure it is located in <root>/cmake/modbus.cmake, which is included from our root CMakeLists.txt using

# libmodbus
include(cmake/modbus.cmake)

The content of cmake/modbus.cmake is:

include(ExternalProject)

set(MODBUS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/opt/libmodbus)
set(MODBUS_BIN ${CMAKE_CURRENT_BINARY_DIR}/libmodbus)
set(MODBUS_STATIC_LIB ${MODBUS_BIN}/lib/libmodbus.a)
set(MODBUS_INCLUDES ${MODBUS_BIN}/include)

file(MAKE_DIRECTORY ${MODBUS_INCLUDES})

ExternalProject_Add(
    libmodbus
    PREFIX ${MODBUS_BIN}
    SOURCE_DIR ${MODBUS_DIR}
    DOWNLOAD_COMMAND cd ${MODBUS_DIR} && git clean -dfX && ${MODBUS_DIR}/autogen.sh
    CONFIGURE_COMMAND ${MODBUS_DIR}/configure --srcdir=${MODBUS_DIR} --prefix=${MODBUS_BIN} --enable-static=yes --disable-shared
    BUILD_COMMAND make
    INSTALL_COMMAND make install
    BUILD_BYPRODUCTS ${MODBUS_STATIC_LIB}
)

add_library(modbus STATIC IMPORTED GLOBAL)

add_dependencies(modbus libmodbus)

set_target_properties(modbus PROPERTIES IMPORTED_LOCATION ${MODBUS_STATIC_LIB})
set_target_properties(modbus PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${MODBUS_INCLUDES})

A component that uses libmodbus can declare its dependency as usual:

    add_executable(hello_modbus main.cpp)
    target_link_libraries(hello_modbus modbus)

A few notes:

  1. This abuses the DOWNLOAD_COMMAND to perform the autogen.sh step. The git clean -dfX is probably not necessary (it is a leftover from an earlier version that used the BUILD_IN_SOURCE option. If you really want to download the code instead of using a git submodule, you'll need to modify this line appropriately.
  2. We go to the trouble to force a static-only build of the library. Adjust your configure command line if you want shared libraries.
  3. The set_target_properties command to set the IMPORTED_LOCATION will fail without the BUILD_BYPRODUCTS ${MODBUS_STATIC_LIB} declaration.
  4. Likewise, the set_target_properties command to set the INTERFACE_INCLUDE_DIRECTORIES will fail without the file(MAKE_DIRECTORY ${MODBUS_INCLUDES}).
Filum answered 25/11, 2019 at 5:30 Comment(2)
I accidentally wrapped the line after CONFIGURE_COMMAND in quotes, and it didn't work. Took some time to figure that out.Ottillia
Note that it would be possible to pass additional commands after CONFIGURE_COMMAND by having COMMAND ls and similar after *_COMMAND.Octagon

© 2022 - 2024 — McMap. All rights reserved.