Skip to content

`cxx_modules_converter.py` is a Python script to convert C++ sources and headers to C++20 modules.

License

Notifications You must be signed in to change notification settings

zowers/cxx_modules_converter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

4df3282 · Mar 21, 2025

History

48 Commits
Dec 28, 2024
Mar 21, 2025
Jan 12, 2025
Dec 16, 2024
Mar 16, 2025
Dec 16, 2024
Mar 16, 2025
Mar 21, 2025
Mar 21, 2025
Jan 12, 2025
Mar 16, 2025
Dec 16, 2024
Dec 18, 2024

Repository files navigation

cxx_modules_converter

cxx_modules_converter.py is a Python script to convert C++ sources and headers to C++20 modules.

License

cxx_modules_converter is licensed under the MIT license.

Usage

Script can be used as following:

cxx_modules_converter.py [-h] [-s DIRECTORY] [-d DESTINATION] [-a {modules,headers}] [-r ROOT] [-p] [-I INCLUDE] [-n NAME] [-k SKIP] [-c COMPAT] [-m COMPAT_MACRO] [-e HEADER] [--export EXPORT] [--exportsuffix EXPORTSUFFIX] [-v]

Options:

  • -h, --help show this help message and exit
  • -s DIRECTORY, --directory DIRECTORY the directory with files
  • -i, --inplace convert files in the same directory or put conversion result to destination (unsupported)
  • -d DESTINATION, --destination DESTINATION destination directory where to put conversion result, ignored when --inplace is provided
  • -a {modules,headers}, --action {modules,headers} action to perform - convert to modules or headers
  • -r ROOT, --root ROOT resolve module names starting from this root directory, ignored when --parent
  • -p, --parent resolve module names starting from parent of source directory
  • -I INCLUDE, --include INCLUDE include search path, starting from root or parent directory
  • -n NAME, --name NAME module name for modules in [root] directory which prefixes all modules
  • -k SKIP, --skip SKIP skip patterns - files and directories matching any pattern will not be converted or copied
  • -c COMPAT, --compat COMPAT compat patterns - files and directories matching any pattern will be converted in compatibility mode allowing to use as either module or header
  • -m COMPAT_MACRO, --compat-macro COMPAT_MACRO compatibility macro name used in compat modules and headers
  • -e HEADER, --header HEADER always include headers with matching names and copy them as is
  • --export EXPORT A=B means module A exports module B, i.e. --export A=B means module A will have export import B;. use --export "A=*" to export all imports. use --export "*=B" to export B from all modules. use --export "*=*" to export all from all modules.
  • --exportsuffix EXPORTSUFFIX export module suffix for which export import is used instead of simple import
  • --inextheader INEXTHEADER input header file extensions, .h by default. first use replaces the default, subsequent uses append.
  • --inextcxx INEXTCXX input C++ source file extensions, .cpp by default. first use replaces the default, subsequent uses append.
  • --outextmod OUTEXTMOD output module interface unit file extensions. default: .cppm
  • --outextmodimpl OUTEXTMODIMPL output module implementation unit file extensions. default: .cpp
  • -v, --version show version

Assumptions

The converter has several assumptions which are not configurable (at the moment):

  • following source files extensions are used:
    • .h - header file
    • .cpp - c++ source file
    • .cpp - module implementation unit
    • .cppm - module interface unit
  • header file path is used to determine module name by joining path parts with dots (.); same for c++ source files
  • system header includes using #include <> are moved to global module fragment

Tests

pytest is used to run tests. The venv can be used to create python3 virtual environment, assuming Linux and bash is used:

python3 -m venv .venv
source .venv/bin/activate

Install pytest requirements using

pip install -r requirements-test.txt

About

`cxx_modules_converter.py` is a Python script to convert C++ sources and headers to C++20 modules.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published