Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefix PUBLIC_HEADER with CMAKE_CURRENT_SOURCE_DIR #332

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

avaliente-evs
Copy link

This is required to install the project when using cmake FetchContent, or it will complain about the file json-schema.hpp not found (because it will use a relative path to the project that called FetchContent instead of json-schema-validator path

This is required to install the project when using cmake FetchContent,
or it will complain about the file json-schema.hpp not found (because it
will use a relative path to the project that called FetchContent
instead of json-schema-validator path
@LecrisUT
Copy link
Collaborator

Could you share an example build? CMAKE_CURRENT_SOURCE_DIR should be implicitly included. It might be a CMake version issue or cmake_policy instead.

@avaliente-evs
Copy link
Author

Sure, here is a minimal example:
CMakeLists.txt

cmake_minimum_required(VERSION 3.3)
project(Example)

include(FetchContent)
set(JSON_SCHEMA_VALIDATOR_VERSION 2.3.0)
FetchContent_Declare(JSON_SCHEMA_VALIDATOR
                     URL https://github.com/pboettch/json-schema-validator/archive/refs/tags/${JSON_SCHEMA_VALIDATOR_VERSION}.tar.gz)
FetchContent_MakeAvailable(JSON_SCHEMA_VALIDATOR)

add_library(example main.cpp)

target_link_libraries(example PUBLIC nlohmann_json_schema_validator)

install(TARGETS
          example
          nlohmann_json
          nlohmann_json_schema_validator
        EXPORT
          exampleConfig
)
install(EXPORT exampleConfig DESTINATION cmake)

main.cpp

#include <nlohmann/json-schema.hpp>

int main()
{
  return 0;
}

Run cmake . -B build && make -C build && sudo make -C build install gives the following error:

Install the project...
-- Install configuration: ""
-- Installing: /usr/local/lib64/libexample.a
-- Installing: /usr/local/lib64/libnlohmann_json_schema_validator.a
CMake Error at cmake_install.cmake:54 (file):
  file INSTALL cannot find
  "/home/[email protected]/dev/example_fetch_content/nlohmann/json-schema.hpp": No
  such file or directory.

@avaliente-evs
Copy link
Author

I am using cmake 3.26.5 on Rocky Linux 9 (RHEL 9)

@LecrisUT
Copy link
Collaborator

LecrisUT commented Oct 17, 2024

Ok I did some debugging. Try this CMakeLists.txt instead:

cmake_minimum_required(VERSION 3.25...3.30)
project(Example)

include(FetchContent)

option(JSON_VALIDATOR_INSTALL "example: Override" ON)
option(JSON_Install "example: Override" ON)

FetchContent_Declare(JSON_SCHEMA_VALIDATOR
  GIT_REPOSITORY https://github.com/pboettch/json-schema-validator
  GIT_TAG        2.3.0
  )
FetchContent_MakeAvailable(JSON_SCHEMA_VALIDATOR)

add_library(example main.cpp)

target_link_libraries(example PRIVATE nlohmann_json_schema_validator)

install(TARGETS
          example
        EXPORT
          exampleConfig
)
install(EXPORT exampleConfig DESTINATION cmake)

Explanation:

  • install instructions are already defined in each upstream project. If you really want to install the projects override the options of the projects, i.e. set an option() before the FetchContent commands. Beware that it would possibly override any pre-installed versions, particularly the Config.cmake files.
  • try to link everything as PRIVATE if you do not need to propagate the #include. If that is the case, you do not really need the nlohmann_json_schema_validator header files to be installed
  • alternatively make sure example is SHARED library and you can disable the installation for the dependencies

You reminded me of the weird edge-case that when you have a static library foo that depends on bar, then the install fails even though the linkage is PRIVATE. I still don't know why exactly that is the case, but if you would know anyone who might know, would be awesome if you could direct them to: LecrisUT/CMake-Template#15

Edit: I've tried to revisit the problem and made MWE and I am afraid things can get quite messy if you want to support both static and shared library build

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants