Search code examples
c++cmakefetchcontent

Does FetchContent build with optimisations?


I am moving from including my dependencies using VCPKG to using FetchContent to make building my docker images slightly easer. I now get my dependencies in a file called get_dependencies.cmake like:

include(FetchContent)

message(STATUS "Getting iGraph")
FetchContent_Declare(
    igraph
    URL https://github.com/igraph/igraph/releases/download/0.10.4/igraph-0.10.4.tar.gz
    URL_HASH MD5=10a3f325425970c75a7ba8359376e208
    FIND_PACKAGE_ARGS NAMES igraph
    SYSTEM
)

FetchContent_MakeAvailable(igraph)

message(STATUS "Getting GEOS")
FetchContent_Declare(
    GEOS
    URL https://download.osgeo.org/geos/geos-3.11.1.tar.bz2
    URL_HASH MD5=5732ec96b391ecddc35bda9795b654ea
    FIND_PACKAGE_ARGS NAMES GEOS
    SYSTEM
)

FetchContent_MakeAvaiable(GEOS)

FetchContent_Declare(
  grpc
  GIT_REPOSITORY  https://github.com/grpc/grpc.git
  GIT_TAG         v1.52.1
  GIT_SHALLOW     ON
  SYSTEM
)
FetchContent_MakeAvailable(grpc)


etc..

However my code is now running really slowly. I cant tell if this is because it is some network issue (as my office is currently experiencing them) or if my dependencies (grpc) have been built without optimizations.

My library is being built like:

file(GLOB_RECURSE INCLUDE_FILES CONFIGURE_DEPENDS LIST_DIRECTORIES false ${CMAKE_CURRENT_SOURCE_DIR} *.h)
file(GLOB_RECURSE SRC_FILES CONFIGURE_DEPENDS LIST_DIRECTORIES false ${CMAKE_CURRENT_SOURCE_DIR} *.cpp *.c)

set(DEPENDENCIES
    igraph
    geos
    GribManagerClient
    Eigen3::Eigen
    Boost::functional
)

add_library(
    RoutingLib
    ${INCLUDE_FILES}
    ${SRC_FILES}
)
add_dependencies(RoutingLib ${DEPENDENCIES})
target_link_libraries(RoutingLib PUBLIC ${DEPENDENCIES})
target_compile_options(RoutingLib PRIVATE ${RoutingLib_FLAGS_LIST})

And the value for RoutingLib_FLAGS_LIST is in a CMakePresets.json file as:

"RoutingLib_FLAGS": "-std=c++17 -pthread -O3 -fPIC -Wall -Wextra -pedantic"

Does FetchContent automatically build my external libraries with the optimisation flags, or do I need to add the line:

add_compile_options(-O3)

to the top of my cmake file that gets my external dependencies?


Solution

  • However my code is now running really slowly. I cant tell if this is because it is some network issue (as my office is currently experiencing them) or if my dependencies (grpc) have been built without optimizations.

    FetchContent fetches the specified resource, and then just add_subdirectorys it. So the first order of what specifies the optimization flags will be the same as how it works for any other targets: CXX_<LANG>_FLAGS, CMAKE_<LANG>_FLAGS_<CONFIG>, CMAKE_<LANG>_FLAGS_<CONFIG>_INIT, which CMake default-initializes to things appropriate for whatever build type you select (Ex. "Debug", "Release", "MinSizeRel", "RelWithDebInfo") and what compiler you're using, add_compile_options (which adds to the directory's COMPILE_OPTIONS), target_compile_options (which adds to the target's COMPILE_OPTIONS), and the source file COMPILE_OPTIONS property, which are all valid mechansisms to modify the compile options for things.

    If you actually want to inspect the flags that get used to compile source files, you can either dig through the generated build system's config files, or if supported for your generator, use CMAKE_EXPORT_COMPILE_COMMANDS to generate a compile_commands.json file.

    I'd first check that what you think is happening is actually happening. Check what flags are being used to compile your fetched-dependencies' source files. Then, if actually needed, modify those using your mechanism of choice. Ex. target_compile_options or add_compile_options (if you want it to only apply to the dependencies and no other targets in the scope that does the FetchContent_MakeAvailable, you might need to artificially add a subdirectory level where you do that).