jansson icon indicating copy to clipboard operation
jansson copied to clipboard

Target settings do not automatically populate when using CMake FetchContent

Open Andrew-Au opened this issue 2 years ago • 1 comments

MacOS using XCode

Create FetchContent import as follows:

include( FetchContent )
FetchContent_Declare(
	jansson
	GIT_REPOSITORY https://github.com/akheron/jansson.git
	GIT_TAG 684e18c927e89615c2d501737e90018f4930d6c5 # v2.14
)

FetchContent_MakeAvailable(
	jansson
)

What works:

  • automatic download of Jansson repository
  • automatic build of Jansson repository
  • inclusion of test targets into generated XCode project

What fails:

  • search path to "jansson.h" not included in build (<jansson.h> also fails)
  • Jansson compile options not added to build

I enquired on the CMake forums and the FetchContent experts explained that the problem was the Jansson project not using per-target options. Because these are missing, the options are not exported along with the target and the build fails.

Proposed fixes:

  • add_definitions(-DJANSSON_USING_CMAKE) and add_definitions(-DHAVE_CONFIG_H) become target_compile_definitions( jansson PUBLIC JANSSON_USING_CMAKE) and target_compile_definitions( jansson PRIVATE HAVE_CONFIG_H) respectively.
  • include_directories( ${CMAKE_CURRENT_BINARY_DIR}/include ) becomes target_include_directories(jansson PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include> INTERFACE $<INSTALL_INTERFACE:include> )
  • Add library alias Jansson::jansson
  • Update minimum CMake to 3.5. As of 3.27 minimums below 3.5 are deprecated.

Testing on OS X with CMake 3.26

  • Jansson download and build works
  • App build using FetchContent now works correctly
    • Can define Jansson::jansson as dependency
    • "jansson.h" header correctly found
    • JANSSON_USING_CMAKE correctly added to compile options

Andrew-Au avatar Sep 28 '23 23:09 Andrew-Au

I am interested in working on implementing the proposed solution. Edit: I see this is already being done.

JosiahWI avatar Jul 09 '24 19:07 JosiahWI

Merged as https://github.com/akheron/jansson/commit/96d160df90016066d04d493d1d69639474ba4f20.

Andrew-Au avatar Apr 04 '25 04:04 Andrew-Au