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

Export package to allow builds without installing #1202

Merged
merged 1 commit into from
Aug 25, 2018

Conversation

dennisfischer
Copy link
Contributor

@dennisfischer dennisfischer commented Aug 20, 2018

This pull requests modifies the CMakeLists.txt to export the package.

Problem:

As of right now, the repository needs to be installed such that find_package(nlohmann_json) can be used. This creates a problem if this project is used as a git submodule or if it is used without installing. Example: This repository is cloned to a folder named project/json/. In the root directory of the project the CMakeLists.txt is changed to include the json folder by adding add_subdirectory(json). However, because the json project does not export the package the call to find_package(..) fails.

Solution:

Export the package export(PACKAGE ${PROJECT_NAME}).


Pull request checklist

Read the Contribution Guidelines for detailed information.

  • Changes are described in the pull request, or an existing issue is referenced.
  • The test suite compiles and runs without error.
  • Code coverage is 100%. Test cases can be added by editing the test suite.
  • The source code is amalgamated; that is, after making changes to the sources in the include/nlohmann directory, run make amalgamate to create the single-header file single_include/nlohmann/json.hpp. The whole process is described here.

@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling dfe607c on dennisfischer:develop into 7608a64 on nlohmann:develop.

@nlohmann
Copy link
Owner

I have little experience with CMake, so I do not just want to merge this PR without understanding it. However, I fail to reproduce the issue.

Could you provide a minimal CMakeLists.txt file that demonstrates the issue with the current develop branch?

@dennisfischer
Copy link
Contributor Author

dennisfischer commented Aug 25, 2018

Note: If you fail to reproduce the example, make sure that the json package is not present in the local registry. At least remove .cmake/packages/nlohmann_json if present. It might be required to remove /usr/local/include/nlohmann_json and /usr/local/lib/cmake/nlohmann_json too. Otherwise CMake might find an existing exported package.

I've created a minimal example. It's at https://github.com/dennisfischer/example-json

In general, a call to find_package(nlohmann_json) should make sure that the dependency exists.

However, in certain cases you do not want or cannot have dependencies installed, i.e. the entries for /usr/local/include/nlohmann_json and /usr/local/lib/cmake/nlohmann_json are missing and instead, you want to use libraries from another directory. A common use case could be to use git submodules.
Further, a dependent project can now just make its call to find_package. It does not need to know if the package is installed or provided in the buildtree.

Master branch creates following error during configuration:

-- The C compiler identification is Clang 6.0.1
-- The CXX compiler identification is Clang 6.0.1
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Using the single-header code from [...]/json/json/single_include/
CMake Error at CMakeLists.txt:6 (find_package):
  By not providing "Findnlohmann_json.cmake" in CMAKE_MODULE_PATH this
  project has asked CMake to find a package configuration file provided by
  "nlohmann_json", but CMake did not find one.

  Could not find a package configuration file provided by "nlohmann_json"
  with any of the following names:

    nlohmann_jsonConfig.cmake
    nlohmann_json-config.cmake

  Add the installation prefix of "nlohmann_json" to CMAKE_PREFIX_PATH or set
  "nlohmann_json_DIR" to a directory containing one of the above files.  If
  "nlohmann_json" provides a separate development package or SDK, be sure it
  has been installed.

We see that cmake finds the uses the add_subdirectory call but even though this directory provides the correct package, the requirement set by find_package is not fulfilled. The reason is how cmake manages packages internally. A package is added to the package registry if the project is installed properly (this exists in the current CMakeLists.txt) or if it is exported (this is what the pull request changes).

Note that the add_subdirectory call in this CMakeLists.txt is a bad example. Usually this call would occur in a parent directories CMakeLists.txt as you'd have for superprojects.

In contrast to the main branch, the subdirectory branch contains the json repository with my commit included, i.e. export(PACKAGE...) is added to the CMakeLists.txt file. This adds the package to the user package registry. Therefore, find_package now resolves the dependency.

-- The C compiler identification is Clang 6.0.1
-- The CXX compiler identification is Clang 6.0.1
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Using the single-header code from [...]/json/single_include/
-- Configuring done
-- Generating done
-- Build files have been written to: [...]

@nlohmann
Copy link
Owner

Thanks so much for this comprehensive answer! Indeed, there were leftovers in my .cmake folder. After removing them, I could reproduce the issue and confirm your fix.

@nlohmann nlohmann self-assigned this Aug 25, 2018
Copy link
Owner

@nlohmann nlohmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

@nlohmann nlohmann added this to the Release 3.2.1 milestone Aug 25, 2018
@nlohmann nlohmann merged commit d713727 into nlohmann:develop Aug 25, 2018
@nlohmann
Copy link
Owner

Thanks a lot!

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.

3 participants