TransWikia.com

CMake: Best practices for differing lib paths with ExternalProject_Add

Stack Overflow Asked by 0x5453 on August 20, 2020

I have a small project whose CMakeLists.txt looks something like this:

include(ExternalProject)
ExternalProject_Add(gainput_project
  PREFIX gainput
  GIT_REPOSITORY https://github.com/jkuhlmann/gainput.git
  GIT_TAG e21b15f0bc3dd3f1a745fe89a966a2457e940142
  INSTALL_COMMAND ""
)
ExternalProject_Get_Property(gainput_project SOURCE_DIR)
ExternalProject_Get_Property(gainput_project BINARY_DIR)
set(GAINPUT_SOURCE_DIR "${SOURCE_DIR}")
set(GAINPUT_BINARY_DIR "${BINARY_DIR}")

add_executable(demo main.cpp)
add_dependencies(demo gainput_project)

target_include_directories(demo PUBLIC
  SYSTEM ${GAINPUT_SOURCE_DIR}/lib/include
)

# TODO: this is bad
if(WIN32)
  target_link_libraries(demo PUBLIC
    debug ${GAINPUT_BINARY_DIR}/lib/Debug/gainput-d.lib
    optimized ${GAINPUT_BINARY_DIR}/lib/Release/gainput.lib
  )
else()
  target_link_libraries(demo PUBLIC
    debug ${GAINPUT_BINARY_DIR}/lib/gainput-d.so
    optimized ${GAINPUT_BINARY_DIR}/lib/gainput.so
  )
endif()

Obviously, those calls to target_link_libraries are not ideal; they are very repetitive and I am explicitly writing paths and filenames that the build system should already know. What is the best way to handle the different output directories and filenames from the external project?

Is there a way I can query the external project to get the actual output dir? (e.g. via ExternalProject_Get_Property?) Is there some magical generator expression I can use? (ExternalProject_Get_Property(gainput_project RUNTIME_OUTPUT_DIRECTORY) does not work.)

It seems like find_library would work, except that the output library does not exist at configure time, so that fails.

Maybe I should set the external project’s INSTALL_DIR to some hard-coded directory that I control and always use that? (Would it still get the Debug and Release subdirectories on Windows?)

Would the FetchContent module give me better access to the outputs of the included project? I don’t see anything obvious in the docs there.

Idk, I’ve already tried quite a few ideas and nothing has worked so far. I feel like I may be missing something fundemental – this is the first time I’ve used ExternalProject. Any tips?

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP