Skip to content

Add modules support #1015

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

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open

Conversation

mikomikotaishi
Copy link

@mikomikotaishi mikomikotaishi commented Mar 25, 2025

I have tried making a C++ modules version for FTXUI, which is built if CMake version is above 3.28. I've tested these modules on my own projects and they work fine, and I'm sure most C++20+ users would like the ability to import FTXUI as a module.

FTXUI is now importable with:

import ftxui; // Imports all of the submodules

// Alternatively, each submodule can be imported individually
import ftxui.component;
import ftxui.dom;
import ftxui.screen;
import ftxui.util;

@ArthurSonzogni
Copy link
Owner

Thanks! I am enthousiastic.

Things to keep in mind. FTXUI supports C++11, so it must not break those users. A priori, it is possible to support both.

To provide a review, I would need to spend some time learning about C++ module, to ensure this patch is the best we can do, without regrets. It will take me some time.

@mikomikotaishi
Copy link
Author

Thanks! I am enthousiastic.

Things to keep in mind. FTXUI supports C++11, so it must not break those users. A priori, it is possible to support both.

To provide a review, I would need to spend some time learning about C++ module, to ensure this patch is the best we can do, without regrets. It will take me some time.

I think it would be best if modules were just opt-in, so that those using C++20 can benefit from them while the builds using pre-C++20 do not break either. I can make this change, and the library should be compatible for all versions.

@ArthurSonzogni
Copy link
Owner

I got an error:

CMake Error in modules/ftxui/CMakeLists.txt:
  The target named "ftxui_modules" has C++ sources that may use modules, but
  modules are not supported by this generator:
    Unix Makefiles
  Modules are supported only by Ninja, Ninja Multi-Config, and Visual Studio
  generators for VS 17.4 and newer.  See the cmake-cxxmodules(7) manual for
  details.  Use the CMAKE_CXX_SCAN_FOR_MODULES variable to enable or disable
  scanning.

This is problematic, because it means all FTXUI users will have this error. We should probably try to detect what cmake generator is in use and enable modules when supported.


Then I tried:

cmake .. -DCMAKE_GENERATOR:INTERNAL=Ninja

but it doesn't work:

CMake Error in modules/ftxui/CMakeLists.txt:
  The target named "ftxui_modules" 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.

I see I need to install clang-scan-deps, but it isn't available yet by default.

Maybe this is too early to support C++ modules by default?

Alternatively, maybe we should put this behind a build flag.
We should add some github action to test this is compiling.

@mikomikotaishi
Copy link
Author

I put it behind a flag that should now only build if the flag FTXUI_BUILD_MODULES is enabled, and added some notes to the README about modules.

@mikomikotaishi
Copy link
Author

Hi, are there any changes you're still looking for before you can accept the pull request and have it merged?

@mikomikotaishi
Copy link
Author

I'm actually now wondering if modules should be a top level directory or if it should just be moved into src. Originally I kept it in a distinct location, but I am wondering if it might make more sense to keep it in src? Of course simultaneously I am considering that no actual source code or implementation is contained in these .cppm files, as they are all just re-exports of headers.

@ArthurSonzogni
Copy link
Owner

Thanks!

I am going to:

  • Add CI test that all the example can be build by replacing the header by modules.
  • (maybe) Move the cppm to the source.

I don't have a lot of time, so please expect ~1 month before I can fully merge this patch. I don't want to rush support this.

@ArthurSonzogni
Copy link
Owner

I made an update:

  1. Moved the .cppm into the src/ftxui directory.
  2. Added a github workflow to test building the examples with modules. You can test yourself after running ./tools/generate_examples_modules.sh. Not all examples are working yet.

Tasks (Feel free to grab)

  • Add support for import ftxui. Should we add ftxui.cppm file?
  • Update the failing examples to compile with modules. Most are likely just missing includes that were grabbed from ftxui, but aren't anymore thanks to modules.
  • GCC does crash. Report the bug.
  • Check ftxui-modules can be "installed" in the system and a project can make use of it.
  • Add GCC / MSVC config the github workflow matrix.
  • Add additional OSes into the github workflow matrix.
  • Consider auto-detecting module support and update FTXUI_BUILD_MODULES default value based on this.

@mikomikotaishi
Copy link
Author

I think C++ modules require CMake 3.28, is there a reason you lowered the version requirement?

@ArthurSonzogni
Copy link
Owner

I think C++ modules require CMake 3.28, is there a reason you lowered the version requirement?

Increasing the cmake version would prevent a lot of users from using FTXUI.

It can be conditionnal on FTXUI_BUILD_MODULES.

@mikomikotaishi
Copy link
Author

mikomikotaishi commented Apr 11, 2025

I am supportive of the idea of having a single ftxui module (in addition to the granular ftxui.component, ftxui.dom, ftxui.screen, and ftxui.util modules) that imports these four. All it would be is just export importing all these four modules which I am sure some users would like the convenience of.

By the way, the two additional repositories adding additional components,

I don't know if you plan on eventually integrating these into the main repo, but for now I do have a template on my own project for a ftxui.external module that imports these two. Would you ever plan on adding support for these components?

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