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

Expose spdlog option to use std::format instead of internal fmt lib #1336

Merged
merged 5 commits into from
Dec 17, 2023

Conversation

hdoc
Copy link
Contributor

@hdoc hdoc commented Dec 14, 2023

Now that std::format is available in recent compiler versions (Clang 17, GCC 13), it's possible to use the compiler-provided std::format instead of fmtlib.

spdlog already provides this functionality upstream and exposes it in CMake, but it's not an option with Meson.

This change adds support for that functionality to our wrap.

@hdoc hdoc force-pushed the spdlog-std-format-support branch from 23ffc2a to 9cb5e31 Compare December 14, 2023 20:49
@neheb
Copy link
Collaborator

neheb commented Dec 14, 2023

Might want to add a check if std::format can be used.

@hdoc
Copy link
Contributor Author

hdoc commented Dec 14, 2023

@neheb added a check for the format header, let me know if that's what you had in mind.

@@ -24,7 +24,9 @@ if get_option('external_fmt').enabled()
endif

if get_option('std_format').enabled()
spdlog_compile_args += '-DSPDLOG_USE_STD_FORMAT'
if meson.get_compiler('cpp').has_header('format', required: true)
Copy link
Collaborator

Choose a reason for hiding this comment

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

required can take a feature option directly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm aware that required can take get_option('std_format')) directly but this whole branch is gated by the if get_option('std_format').enabled() anyway so the header check is always required.
If the option isn't specified in the first if statement, the has_header() check will never execute.
Setting required to true is a logical optimization.

This is what I think you're proposing, please clarify if my assumption is wrong:

if get_option('std_format').enabled()
  if meson.get_compiler('cpp').has_header('format', required: get_option('std_format'))
    spdlog_compile_args += '-DSPDLOG_USE_STD_FORMAT'
  endif
endif

@neheb
Copy link
Collaborator

neheb commented Dec 14, 2023

The check is not enough actually.

If compiling in a C++ mode lower than 17, it will still succeed. I haven't tested if check_header works. That would be the cleanest solution.

In exiv2, I currently use

if not cpp.has_header_symbol('format', '__cpp_lib_format')
  deps += dependency('fmt')
endif

@hdoc
Copy link
Contributor Author

hdoc commented Dec 14, 2023

Updated the check to use the new C++20 library feature test macros like you proposed, and verified it worked locally with GCC 13 on macOS.

One question: do we need to do any feature gating for the C++ standard version here? If a user is using C++17 and using the compiled_lib option I think this will fail. Perhaps we should hide the std_format option behind a is_in_cpp20_mode check like here. Please let me know what you think is best for the end-user.

@neheb
Copy link
Collaborator

neheb commented Dec 14, 2023

I think it's best to modify the build file as such:

if meson.get_compiler('cpp').has_header_symbol('format', '__cpp_lib_format', required: get_option('std_format'))
  spdlog_compile_args += '-DSPDLOG_USE_STD_FORMAT'
else
  fmt_dep = dependency('fmt', version: '>=8.1', required: get_option('external_fmt').enabled())
  if fmt_dep.found()
    spdlog_dependencies += fmt_dep
    spdlog_compile_args += '-DSPDLOG_FMT_EXTERNAL'
  endif
endif

@hdoc
Copy link
Contributor Author

hdoc commented Dec 14, 2023

Looks reasonable and passed tests locally. I didn't realize get_option() could be used in this way. Thank you

if meson.get_compiler('cpp').has_header_symbol('format', '__cpp_lib_format', required: get_option('std_format'))
spdlog_compile_args += '-DSPDLOG_USE_STD_FORMAT'
else
fmt_dep = dependency('fmt', version: '>=8.1', required: get_option('external_fmt').enabled())
Copy link
Collaborator

Choose a reason for hiding this comment

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

you don't need enabled() here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

@neheb neheb merged commit 3b7b8ee into mesonbuild:master Dec 17, 2023
8 checks passed
@hdoc hdoc deleted the spdlog-std-format-support branch December 18, 2023 18:46
willwray pushed a commit to willwray/wrapdb that referenced this pull request Apr 22, 2024
…esonbuild#1336)

* Expose spdlog option to use std::format instead of internal fmt lib
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