Skip to content

Add Separate CMake Target for C++20 Modules#4685

Merged
vitaut merged 3 commits intofmtlib:masterfrom
MathewBensonCode:installmodules
Mar 2, 2026
Merged

Add Separate CMake Target for C++20 Modules#4685
vitaut merged 3 commits intofmtlib:masterfrom
MathewBensonCode:installmodules

Conversation

@MathewBensonCode
Copy link
Contributor

In the same vein as there is the fmt::fmt-header-only, fmt::fmt and fmt::fmt_c targets, I propose the addition of a new target fmt::fmt-module which will be for the compilation of the FMT_MODULE library option.

The new target will have the properties requried for Compiling, Installing and using the C++20 functionality

The add_module_library CMake function is factored out in favour of simply specifying the targets specifically in a cleaner and simpler CMake syntax.

The FMT_MODULE option is defaulted to true for versions of CMake newer than 3.28.

Closes #4684

@MathewBensonCode MathewBensonCode marked this pull request as draft February 24, 2026 12:06
@MathewBensonCode MathewBensonCode force-pushed the installmodules branch 2 times, most recently from 9b4e055 to 8c91cc2 Compare February 25, 2026 11:17
@MathewBensonCode MathewBensonCode marked this pull request as ready for review February 25, 2026 11:40
Copy link
Contributor

@vitaut vitaut left a comment

Choose a reason for hiding this comment

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

Adding a module target is a good idea but there are too many unrelated refactors in this PR. Please keep add_module_library and avoid unrelated changes (you can submit separate PR(s) for those).

@MathewBensonCode MathewBensonCode marked this pull request as draft February 26, 2026 12:01
MathewBensonCode pushed a commit to MathewBensonCode/fmt that referenced this pull request Feb 26, 2026
- Fixed the gramatical error in the documents changed in the pull
  request.
- Fix formatting, particularly on the newly added code
@MathewBensonCode MathewBensonCode force-pushed the installmodules branch 2 times, most recently from 834161d to b09d295 Compare February 27, 2026 22:43
@MathewBensonCode MathewBensonCode marked this pull request as ready for review February 27, 2026 23:15
@MathewBensonCode
Copy link
Contributor Author

I have tried to make the requested changes. I have restored the Removed functions to the CMakeLists.txt file.

However, I marked the add_module_library as deprecated and unused.

Kindly advise if the change is agreeable.

@MathewBensonCode MathewBensonCode marked this pull request as ready for review March 1, 2026 03:53
@ClausKlein
Copy link
Contributor

bash-5.3$ cmake -G Ninja -S . -B build --fresh -D CMAKE_CXX_STANDARD=17
-- CMake version: 4.2.1
-- The CXX compiler identification is AppleClang 16.0.0.16000026
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Ninja Version: 1.13.0.git.kitware.jobserver-pipe-1

-- Using CXX Modules by Default with Ninja                           <<<<<<<<<<<<<< he?
-- {fmt} version: 12.1.1
-- Build type: Release
-- Performing Test HAS_NULLPTR_WARNING
-- Performing Test HAS_NULLPTR_WARNING - Success
-- Target 'doc' disabled because mkdocs not found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Success
-- Found Threads: TRUE
-- FMT_PEDANTIC: OFF
-- The C compiler identification is AppleClang 16.0.0.16000026
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Configuring done (2.7s)
CMake Error in CMakeLists.txt:
  The target named "fmt-module" has C++ sources that may use modules, but the
  compiler does not provide a way to discover the import graph dependencies.
  See the cmake-cxxmodules(7) manual for details.  Use the
  CMAKE_CXX_SCAN_FOR_MODULES variable to enable or disable scanning.


-- Generating done (0.0s)
CMake Generate step failed.  Build files cannot be regenerated correctly.
bash-5.3$ 

@ClausKlein
Copy link
Contributor

you may have a look at ClausKlein#1

@MathewBensonCode MathewBensonCode force-pushed the installmodules branch 2 times, most recently from 23fb90f to eda54d5 Compare March 1, 2026 12:59
@MathewBensonCode
Copy link
Contributor Author

you may have a look at ClausKlein#1

@ClausKlein I have just seen this and noted the changes needed in the test/CMakeLists.txt file with regards to the FMT_MODULE flag. Thanks.

@MathewBensonCode MathewBensonCode marked this pull request as draft March 1, 2026 13:20
@ClausKlein
Copy link
Contributor

you may have a look at ClausKlein#1

@ClausKlein I have just seen this and noted the changes needed in the test/CMakeLists.txt file with regards to the FMT_MODULE flag. Thanks.

I was wondering, I could not open a PR against your repo?

@MathewBensonCode MathewBensonCode marked this pull request as ready for review March 1, 2026 17:55
@MathewBensonCode
Copy link
Contributor Author

you may have a look at ClausKlein#1

@ClausKlein I have just seen this and noted the changes needed in the test/CMakeLists.txt file with regards to the FMT_MODULE flag. Thanks.

I was wondering, I could not open a PR against your repo?

I don't know why this didn't work, but I have incorporated the commit in the workflow.

the tests for FMT_MODULE are still excluded, but the ordinary tests should now work. This can be solved in another pull request and issue.

MathewBenson and others added 2 commits March 2, 2026 03:17
In the same vein as there is the `fmt::fmt-header-only`, `fmt::fmt` and
`fmt::fmt_c` targets, I propose the addition of a new target
`fmt::fmt-module` which will be for the compilation of the FMT_MODULE
library option.

The new target will have the properties requried for Compiling,
Installing and using the C++20 functionality in CMake

The `add_module_library` function is marked as deprecated as its
functionality is superseded.

Updated the logic for setting the FMT_USE_CMAKE_MODULE flag to check the
versions for Ninja and MSVC according the CMAKE Documents and setting
the FMT_MODULE flag based on this
@MathewBensonCode MathewBensonCode marked this pull request as draft March 2, 2026 00:36
In the same vein as there is the `fmt::fmt-header-only`, `fmt::fmt` and
`fmt::fmt_c` targets, I propose the addition of a new target
`fmt::fmt-module` which will be for the compilation of the FMT_MODULE
library option.

The new target will have the properties requried for Compiling,
Installing and using the C++20 functionality in CMake

Updated the logic for setting the FMT_USE_CMAKE_MODULE flag to check the
versions for Ninja and MSVC according the CMAKE Documents and setting
the FMT_MODULE flag based on this

Fixed the test/CMakeLists.txt file which used the FMT_MODULE flag to
separate the module and non-module library testing, in particular
disableing the module version.

The module testing still needs to be fixed, but the expected behavior of
testing the non-modular version is working.
@MathewBensonCode MathewBensonCode marked this pull request as ready for review March 2, 2026 01:37
@ClausKlein
Copy link
Contributor

ClausKlein commented Mar 2, 2026

The CMakeFiles are still unreadable, the control flow is not clear, and the supported version range historic!

@MathewBensonCode I Ask to respect the cmake format style if possible. It is not clean yet, but mostly indented by 2 spaces and space after controls: if, else, endif, ...

This Files should be formatted with gersemi; then the formatting battle may end!

@vitaut Check your CI, most often on GitHub the most resent cmake version is available, Today the range is CMake version: 3.31.6...4.2.3.

Since 3.28 there are lot of fixes and improvements for CXX_MODULE handling in CMake

And starting with CMake v4.0, a lot of policies changed, you should notice this.
On CI no newer compiler and no newer OS runner is used.

The world changes, don't stay back!

@MathewBensonCode
Copy link
Contributor Author

The CMakeFiles are still unreadable, the control flow is not clear, and the supported version range historic!

@MathewBensonCode I Ask to respect the cmake format style if possible. It is not clean yet, but mostly indented by 2 spaces and space after controls: if, else, endif, ...

This Files should be formatted with gersemi; then the formatting battle may end!

@vitaut Check your CI, most often on GitHub the most resent cmake version is available, Today the range is CMake version: 3.31.6...4.2.3.

9> ### Since 3.28 there are lot of fixes and improvements for CXX_MODULE handling in CMake

And starting with CMake v4.0, a lot of policies changed, you should notice this. On CI no newer compiler and no newer OS runner is used.

The world changes, don't stay back!

@ClausKlein, thank you for the time you have taken to provide your reviews and comments. I will try and ensure that the code I write has better formatting. It is not a battle, it is usually a lower priority as I want to focus on correctness more.

On this matter of the version range, I believe that you are wrong. kindly refer to the documentation for the cmake_minimum_required for the use of the maximum policy.

We don't just write the latest version in the command and call it a day. It is meant to be a guarantee of the code we have written. This means we would have to write all the CMake code in this repo to that standard, test it everywhere and only after all that make that statement about the policies that our code supports.

That is a big undertaking that will require many other issues and pull requests.

The scope and goal of this pull request is simply to add a "modern" target but also to ensure that it doesn't break any expected behavior.

This repo is used in many varied environments, modern and legacy, so a lot of care is needed when making changes.

I have had to revert several good and modern ideas in this pull request to keep to the scope. I intend to bring those in separate issues and pull requests later.

So I appreciate and understand your desire to see modernization, I am pursuing the same goals and I'm certain @vitaut is too.

@vitaut vitaut merged commit 4e1b170 into fmtlib:master Mar 2, 2026
40 checks passed
@vitaut
Copy link
Contributor

vitaut commented Mar 2, 2026

Merged, thank you!

@vitaut
Copy link
Contributor

vitaut commented Mar 2, 2026

@ClausKlein please stop spamming the repo or you will be blocked. Improvements to CMake configs are fine but they should be split into independent PRs that tackle one thing at a time.

@MathewBensonCode MathewBensonCode deleted the installmodules branch March 2, 2026 20:52
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.

No support for Installing C++20 module Target separate from Regular compiled Target

4 participants