aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorEgor Tensin <egor@tensin.name>2026-10-09 16:49:25 +0500
committerEgor Tensin <egor@tensin.name>2026-10-09 16:58:00 +0500
commit21a16d5f91429cf2a61dcdae45c5448c096123d7 (patch)
tree8052fd16287a06c4d0fd564cb62cf44de0b60d76
parentugprade to StrEnum (diff)
downloadcmake-common-21a16d5f91429cf2a61dcdae45c5448c096123d7.tar.gz
cmake-common-21a16d5f91429cf2a61dcdae45c5448c096123d7.zip
use the match statement
-rw-r--r--project/build.py2
-rw-r--r--project/configuration.py10
-rw-r--r--project/format.py15
-rw-r--r--project/os.py18
-rw-r--r--project/platform.py114
-rw-r--r--project/toolset.py32
6 files changed, 107 insertions, 84 deletions
diff --git a/project/build.py b/project/build.py
index 1604ccd..692fefe 100644
--- a/project/build.py
+++ b/project/build.py
@@ -123,7 +123,7 @@ class BuildPhase:
def _cmake_args(self, toolset):
result = ["--build", self.build_dir]
- result += ["--config", str(self.configuration)]
+ result += ["--config", self.configuration]
if self.install_dir is not None:
result += ["--target", "install"]
result += ["--"] + toolset.build_system_args()
diff --git a/project/configuration.py b/project/configuration.py
index 69dfe10..3e925d7 100644
--- a/project/configuration.py
+++ b/project/configuration.py
@@ -22,7 +22,7 @@ class Configuration(StrEnum):
@staticmethod
def parse(s):
try:
- return Configuration(s)
+ return Configuration(s.lower())
except ValueError as e:
raise argparse.ArgumentTypeError(f"invalid configuration: {s}") from e
@@ -34,9 +34,11 @@ class Configuration(StrEnum):
install_dir/PLATFORM/CONFIGURATION/lib, if CONFIGURATION is
MinSizeRel/RelWithDebInfo.
"""
- if self in (Configuration.MINSIZEREL, Configuration.RELWITHDEBINFO):
- return Configuration.RELEASE.b2_variant()
- return str(self).lower()
+ match self:
+ case Configuration.MINSIZEREL | Configuration.RELWITHDEBINFO:
+ return Configuration.RELEASE.b2_variant()
+ case _:
+ return self.lower()
def b2_args(self):
return [f"variant={self.b2_variant()}"]
diff --git a/project/format.py b/project/format.py
index f98f726..fc5ae0e 100644
--- a/project/format.py
+++ b/project/format.py
@@ -101,9 +101,18 @@ def filter_files(paths, exclude):
return (path for path in paths if not excluded(path, exclude))
-CPP_FILE_EXTENSIONS = set(
- (".c", ".h", ".cc", ".hh", ".cpp", ".hpp", ".cxx", ".hxx", ".cp", ".c++")
-)
+CPP_FILE_EXTENSIONS = {
+ ".c",
+ ".h",
+ ".cc",
+ ".hh",
+ ".cpp",
+ ".hpp",
+ ".cxx",
+ ".hxx",
+ ".cp",
+ ".c++",
+}
def list_cpp_files():
diff --git a/project/os.py b/project/os.py
index 199086f..38dc5d9 100644
--- a/project/os.py
+++ b/project/os.py
@@ -3,26 +3,22 @@
# For details, see https://github.com/egor-tensin/cmake-common
# Distributed under the MIT License.
-from enum import auto, StrEnum
+from enum import StrEnum
import platform
class OS(StrEnum):
- WINDOWS = auto()
- LINUX = auto()
- MACOS = auto()
+ WINDOWS = "Windows"
+ LINUX = "Linux"
+ MACOS = "Darwin"
@staticmethod
def current():
system = platform.system()
- mapping = {
- "Windows": OS.WINDOWS,
- "Linux": OS.LINUX,
- "Darwin": OS.MACOS,
- }
- if system not in mapping:
+ try:
+ return OS(system)
+ except ValueError:
raise NotImplementedError(f"unsupported OS: {system}")
- return mapping[system]
def on_windows():
diff --git a/project/platform.py b/project/platform.py
index 5091f7f..4f28ecf 100644
--- a/project/platform.py
+++ b/project/platform.py
@@ -46,53 +46,61 @@ class Platform(StrEnum):
raise argparse.ArgumentTypeError(f"invalid platform: {s}") from e
def mingw_prefix(self):
- if self is Platform.AUTO:
- if on_windows():
+ match self:
+ case Platform.AUTO if on_windows():
# On Windows, use the host architecture.
return Platform.windows_native().mingw_prefix()
- # On Linux, assume that the target is x64.
- return Platform.X64.mingw_prefix()
- if self is Platform.X86:
- return "i686"
- if self is Platform.X64:
- return "x86_64"
- raise NotImplementedError(f"unsupported platform: {self}")
+ case Platform.AUTO:
+ # On Linux, assume that the target is x64.
+ return Platform.X64.mingw_prefix()
+ case Platform.X86:
+ return "i686"
+ case Platform.X64:
+ return "x86_64"
+ case _:
+ raise NotImplementedError(f"unsupported platform: {self}")
def address_model(self):
"""Maps to Boost's address-model."""
- if self is Platform.AUTO:
- if on_windows():
+ match self:
+ case Platform.AUTO if on_windows():
# On Windows, use the host architecture.
return Platform.windows_native().address_model()
- # On Linux, assume that the target is x64.
- raise RuntimeError(
- "cannot determine address model unless the target platform is specified explicitly"
- )
- if self is Platform.X86:
- return 32
- if self is Platform.X64:
- return 64
- raise NotImplementedError(f"unsupported platform: {self}")
+ case Platform.AUTO:
+ # On Linux, assume that the target is x64.
+ # FIXME: the comment above doesn't seem to reflect the code?
+ raise RuntimeError(
+ "cannot determine address model unless the target platform is specified explicitly"
+ )
+ case Platform.X86:
+ return 32
+ case Platform.X64:
+ return 64
+ case _:
+ raise NotImplementedError(f"unsupported platform: {self}")
def installdir(self, configuration):
"""Path to the installation directory inside the Boost build directory."""
- if self is Platform.AUTO:
- if on_windows():
+ match self:
+ case Platform.AUTO if on_windows():
# On Windows, use the host architecture.
return Platform.windows_native().installdir(configuration)
# On Linux, the libraries are stored in install_dir/auto/CONFIGURATION/lib.
- return os.path.join("install_dir", str(self), str(configuration))
+ case _:
+ return os.path.join("install_dir", self, configuration)
def boost_installdir(self, configuration):
"""Same as above, but for CMake."""
return self.installdir(configuration)
def b2_address_model(self):
- if self is Platform.AUTO and not on_windows():
- # On Linux, don't specify the architecture explicitly (it is
- # assumed that the host architecture will be targeted).
- return []
- return [f"address-model={self.address_model()}"]
+ match self:
+ case Platform.AUTO if not on_windows():
+ # On Linux, don't specify the architecture explicitly (it is
+ # assumed that the host architecture will be targeted).
+ return []
+ case _:
+ return [f"address-model={self.address_model()}"]
def b2_installdir(self, configuration):
return [f"--prefix={self.installdir(configuration)}"]
@@ -106,33 +114,35 @@ class Platform(StrEnum):
def cmake_toolset_file(self):
# For Makefile generators, we make a special toolset file that
# specifies the -m32/-m64 flags, etc.
- if self is Platform.AUTO:
- # Let the compiler decide.
- return ""
- if self is Platform.X86:
- address_model = 32
- elif self is Platform.X64:
- address_model = 64
- else:
- raise NotImplementedError(f"unsupported platform: {self}")
- return f"""
-set(CMAKE_C_FLAGS -m{address_model})
-set(CMAKE_CXX_FLAGS -m{address_model})
+ template = """
+set(CMAKE_C_FLAGS -m{bitness})
+set(CMAKE_CXX_FLAGS -m{bitness})
"""
+ match self:
+ case Platform.AUTO:
+ return ""
+ case Platform.X86:
+ return template.format(bitness=32)
+ case Platform.X64:
+ return template.format(bitness=64)
+ case _:
+ raise NotImplementedError(f"unsupported platform: {self}")
def msvc_arch(self):
"""Maps to CMake's -A argument for MSVC."""
- if self is Platform.AUTO:
- if on_windows():
+ match self:
+ case Platform.AUTO if on_windows():
# On Windows, use the host architecture.
return Platform.windows_native().msvc_arch()
- # I don't think the -A argument is supported on any generators
- # except the Visual Studio ones.
- raise RuntimeError(
- "-A parameter is only supported for Visual Studio generators"
- )
- if self is Platform.X86:
- return "Win32"
- if self is Platform.X64:
- return "x64"
- raise NotImplementedError(f"unsupported platform: {self}")
+ case Platform.AUTO:
+ # I don't think the -A argument is supported on any generators
+ # except the Visual Studio ones.
+ raise RuntimeError(
+ "-A parameter is only supported for Visual Studio generators"
+ )
+ case Platform.X86:
+ return "Win32"
+ case Platform.X64:
+ return "x64"
+ case _:
+ raise NotImplementedError(f"unsupported platform: {self}")
diff --git a/project/toolset.py b/project/toolset.py
index f93881f..86137e8 100644
--- a/project/toolset.py
+++ b/project/toolset.py
@@ -174,23 +174,29 @@ class ToolsetType(StrEnum):
@property
def is_versioned(self):
- if self is ToolsetType.MSVC or self is ToolsetType.VISUAL_STUDIO:
- return True
- return False
+ match self:
+ case ToolsetType.MSVC | ToolsetType.VISUAL_STUDIO:
+ return True
+ case _:
+ return False
def parse_version(self, s):
- if self is ToolsetType.MSVC:
- return MSVCVersion.parse(s)
- if self is ToolsetType.VISUAL_STUDIO:
- return VisualStudioVersion.parse(s)
- raise RuntimeError(f"this toolset doesn't support versions: {self}")
+ match self:
+ case ToolsetType.MSVC:
+ return MSVCVersion.parse(s)
+ case ToolsetType.VISUAL_STUDIO:
+ return VisualStudioVersion.parse(s)
+ case _:
+ raise RuntimeError(f"this toolset doesn't support versions: {self}")
def all_versions(self):
- if self is ToolsetType.MSVC:
- return MSVCVersion.all()
- if self is ToolsetType.VISUAL_STUDIO:
- return VisualStudioVersion.all()
- raise RuntimeError(f"this toolset doesn't support versions: {self}")
+ match self:
+ case ToolsetType.MSVC:
+ return MSVCVersion.all()
+ case ToolsetType.VISUAL_STUDIO:
+ return VisualStudioVersion.all()
+ case _:
+ raise RuntimeError(f"this toolset doesn't support versions: {self}")
@staticmethod
def all():