forked from python-project-templates/hatch-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmake.py
More file actions
97 lines (75 loc) · 3.66 KB
/
cmake.py
File metadata and controls
97 lines (75 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
from __future__ import annotations
from os import environ
from pathlib import Path
from sys import version_info
from typing import Any, Dict, Optional, Union
from pydantic import BaseModel, Field
from .common import Platform
__all__ = ("HatchCppCmakeConfiguration",)
DefaultMSVCGenerator = {
"12": "Visual Studio 12 2013",
"14": "Visual Studio 14 2015",
"14.0": "Visual Studio 14 2015",
"14.1": "Visual Studio 15 2017",
"14.2": "Visual Studio 16 2019",
"14.3": "Visual Studio 17 2022",
"14.4": "Visual Studio 17 2022",
}
class HatchCppCmakeConfiguration(BaseModel):
root: Optional[Path] = None
build: Path = Field(default_factory=lambda: Path("build"))
install: Optional[Path] = Field(default=None)
cmake_arg_prefix: Optional[str] = Field(default=None)
cmake_args: Dict[str, str] = Field(default_factory=dict)
cmake_env_args: Dict[Platform, Dict[str, str]] = Field(default_factory=dict)
include_flags: Optional[Dict[str, Union[str, int, float, bool]]] = Field(default=None)
def generate(self, config) -> Dict[str, Any]:
commands = []
# Derive prefix
if self.cmake_arg_prefix is None:
self.cmake_arg_prefix = f"{config.name.replace('.', '_').replace('-', '_').upper()}_"
# Append base command
commands.append(f"cmake {Path(self.root).parent} -DCMAKE_BUILD_TYPE={config.build_type} -B {self.build}")
# Hook in to vcpkg if active
if "vcpkg" in config._active_toolchains:
commands[-1] += f" -DCMAKE_TOOLCHAIN_FILE={Path(config.vcpkg.vcpkg_root) / 'scripts' / 'buildsystems' / 'vcpkg.cmake'}"
# Setup install path
if self.install:
commands[-1] += f" -DCMAKE_INSTALL_PREFIX={self.install}"
else:
commands[-1] += f" -DCMAKE_INSTALL_PREFIX={Path(self.root).parent}"
# TODO: CMAKE_CXX_COMPILER
# Respect CMAKE_GENERATOR environment variable
cmake_generator = environ.get("CMAKE_GENERATOR", "")
if config.platform.platform == "win32":
if not cmake_generator:
cmake_generator = "Visual Studio 17 2022"
commands[-1] += f' -G "{cmake_generator}"'
elif cmake_generator:
commands[-1] += f' -G "{cmake_generator}"'
# Put in CMake flags
args = self.cmake_args.copy()
for platform, env_args in self.cmake_env_args.items():
if platform == config.platform.platform:
for key, value in env_args.items():
args[key] = value
for key, value in args.items():
commands[-1] += f" -D{self.cmake_arg_prefix}{key.upper()}={value}"
# Include customs
if self.include_flags:
if self.include_flags.get("python_version", False):
commands[-1] += f" -D{self.cmake_arg_prefix}PYTHON_VERSION={version_info.major}.{version_info.minor}"
if self.include_flags.get("manylinux", False) and config.platform.platform == "linux":
commands[-1] += f" -D{self.cmake_arg_prefix}MANYLINUX=ON"
# Include mac deployment target
if config.platform.platform == "darwin":
commands[-1] += f" -DCMAKE_OSX_DEPLOYMENT_TARGET={environ.get('OSX_DEPLOYMENT_TARGET', '11')}"
# Respect CMAKE_ARGS environment variable
cmake_args_env = environ.get("CMAKE_ARGS", "").strip()
if cmake_args_env:
commands[-1] += " " + cmake_args_env
# Append build command
commands.append(f"cmake --build {self.build} --config {config.build_type}")
# Append install command
commands.append(f"cmake --install {self.build} --config {config.build_type}")
return commands