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

Use of JSON_DIAGNOSTICS through CMake and find_package() #3106

Open
psalvaggio opened this issue Oct 26, 2021 · 9 comments
Open

Use of JSON_DIAGNOSTICS through CMake and find_package() #3106

psalvaggio opened this issue Oct 26, 2021 · 9 comments
Labels
kind: bug state: help needed the issue needs help to proceed

Comments

@psalvaggio
Copy link

I would like to use the JSON_DIAGNOSTICS feature to produce better error messages. I am using the library through Homebrew and in the Targets.cmake file, there is the following:

INTERFACE_COMPILE_DEFINITIONS "JSON_USE_IMPLICIT_CONVERSIONS=\$<BOOL:ON>;JSON_DIAGNOSTICS=\$<BOOL:OFF>"

so this is hard-coded to be off. I can fix this by undoing in in my code, but it would be nice if we could have control over these parameters via some CMake variables, similar to how you can pass hints to some other CMake packages.

I am using Version 3.10.4 installed through Homebrew.

@nlohmann
Copy link
Owner

I am not maintaining the formula myself, and I have no idea how to fix this.

@nlohmann nlohmann added the state: help needed the issue needs help to proceed label Oct 30, 2021
@nitul1991
Copy link

I'd like to take a crack at resolving this issue. I've begun to look at the cmake files for this project which is where I think we can resolve this issue.

@nitul1991
Copy link

The issue here does not seem to be related to Homebrew.
The library provides a cmake target import file, nlohmann_jsonTargets.cmake, which is generated by cmake.
This file defines default values for the build macros JSON_USE_IMPLICIT_CONVERSIONS and JSON_USE_DIAGNOSTICS.

Overriding these values in client code may be done as follows:

add_executable(foo main.cpp)
set_target_properties(nlohmann_json::nlohmann_json PROPERTIES INTERFACE_COMPILE_DEFINITIONS "JSON_DIAGNOSTICS=1;JSON_USE_IMPLICIT_CONVERSIONS=1")
target_link_libraries(foo PRIVATE nlohmann_json::nlohmann_json)

@psalvaggio this should resolve your issue and I believe it does not merit a code change. Please correct me if I'm wrong.

@nitul1991
Copy link

@nlohmann I believe this issue can be closed. Please advise.

@psalvaggio
Copy link
Author

That's essentially how I have solved the issue on my end. Overwriting properties on imported targets generally isn't idiomatic in CMake. I think probably the desired solution would be something like:

set(JSON_DIAGNOSTICS 1) # probably needs to be a cache variable
find_package(nlohmann_json)
# nlohmann_json::nlohmann_json now has the correct value in INTERFACE_COMPILE_DEFINITIONS

I could probably take a swing at this over the weekend.

@nitul1991
Copy link

@psalvaggio did you manage to come up with a fix for this as per your desired solution?

@psalvaggio
Copy link
Author

Not yet, I had some higher priority tasks come up over the weekend. It should be pretty easy, just a defining a cache variable with the current hardcoded value as the default and replacing the 1's with the cache variable.

@stale
Copy link

stale bot commented Jan 9, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Jan 9, 2022
@nlohmann nlohmann removed the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Jul 30, 2022
@nmoreaud
Copy link

nmoreaud commented Nov 3, 2023

The issue here does not seem to be related to Homebrew. The library provides a cmake target import file, nlohmann_jsonTargets.cmake, which is generated by cmake. This file defines default values for the build macros JSON_USE_IMPLICIT_CONVERSIONS and JSON_USE_DIAGNOSTICS.

Overriding these values in client code may be done as follows:

add_executable(foo main.cpp)
set_target_properties(nlohmann_json::nlohmann_json PROPERTIES INTERFACE_COMPILE_DEFINITIONS "JSON_DIAGNOSTICS=1;JSON_USE_IMPLICIT_CONVERSIONS=1")
target_link_libraries(foo PRIVATE nlohmann_json::nlohmann_json)

@psalvaggio this should resolve your issue and I believe it does not merit a code change. Please correct me if I'm wrong.

@nitul1991 I have tried your solution, but there is a build error:
/usr/bin/c++ -DFMT_LOCALE -DFMT_SHARED -DJSON_DIAGNOSTICS=0 -DJSON_DIAGNOSTICS=1 -DJSON_USE_IMPLICIT_CONVERSIONS=1 ...
[build] : error: "JSON_DIAGNOSTICS" redefined
[build] : note: this is the location of the previous definition

Okay, this is because I use the json library twice, once in a library, and once in a depending executable.
The lib has JSON_DIAGNOSTICS=0, but the executable uses JSON_DIAGNOSTICS=1.
However, CMake transitive dependencies make the 2 values appear in compile command line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: bug state: help needed the issue needs help to proceed
Projects
None yet
Development

No branches or pull requests

4 participants