-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconfig.py
More file actions
104 lines (82 loc) · 3.74 KB
/
config.py
File metadata and controls
104 lines (82 loc) · 3.74 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
98
99
100
101
102
103
104
from __future__ import annotations
from os import system as system_call
from pathlib import Path
from typing import List, Optional
from pkn import getSimpleLogger
from pydantic import BaseModel, Field, model_validator
from .toolchains import BuildType, HatchCppCmakeConfiguration, HatchCppLibrary, HatchCppPlatform, HatchCppVcpkgConfiguration, Toolchain
__all__ = (
"HatchCppBuildConfig",
"HatchCppBuildPlan",
)
log = getSimpleLogger("hatch_cpp")
class HatchCppBuildConfig(BaseModel):
"""Build config values for Hatch C++ Builder."""
verbose: Optional[bool] = Field(default=False)
skip: Optional[bool] = Field(default=False)
name: Optional[str] = Field(default=None)
libraries: List[HatchCppLibrary] = Field(default_factory=list)
cmake: Optional[HatchCppCmakeConfiguration] = Field(default=None)
platform: Optional[HatchCppPlatform] = Field(default_factory=HatchCppPlatform.default)
vcpkg: Optional[HatchCppVcpkgConfiguration] = Field(default_factory=HatchCppVcpkgConfiguration)
@model_validator(mode="wrap")
@classmethod
def validate_model(cls, data, handler):
if "toolchain" in data:
data["platform"] = HatchCppPlatform.platform_for_toolchain(data["toolchain"])
data.pop("toolchain")
elif "platform" not in data:
data["platform"] = HatchCppPlatform.default()
if "cc" in data:
data["platform"].cc = data["cc"]
data.pop("cc")
if "cxx" in data:
data["platform"].cxx = data["cxx"]
data.pop("cxx")
if "ld" in data:
data["platform"].ld = data["ld"]
data.pop("ld")
if "vcpkg" in data and data["vcpkg"] == "false":
data["vcpkg"] = None
model = handler(data)
if model.cmake and model.libraries:
raise ValueError("Must not provide libraries when using cmake toolchain.")
return model
class HatchCppBuildPlan(HatchCppBuildConfig):
build_type: BuildType = "release"
commands: List[str] = Field(default_factory=list)
_active_toolchains: List[Toolchain] = []
def generate(self):
self.commands = []
# Evaluate toolchains
if self.vcpkg and Path(self.vcpkg.vcpkg).exists():
self._active_toolchains.append("vcpkg")
if self.libraries:
self._active_toolchains.append("vanilla")
elif self.cmake:
self._active_toolchains.append("cmake")
# Collect toolchain commands
if "vcpkg" in self._active_toolchains:
self.commands.extend(self.vcpkg.generate(self))
if "vanilla" in self._active_toolchains:
if "vcpkg" in self._active_toolchains:
log.warning("vcpkg toolchain is active; ensure that your compiler is configured to use vcpkg includes and libs.")
for library in self.libraries:
compile_flags = self.platform.get_compile_flags(library, self.build_type)
link_flags = self.platform.get_link_flags(library, self.build_type)
self.commands.append(
f"{self.platform.cc if library.language == 'c' else self.platform.cxx} {' '.join(library.sources)} {compile_flags} {link_flags}"
)
if "cmake" in self._active_toolchains:
self.commands.extend(self.cmake.generate(self))
return self.commands
def execute(self):
for command in self.commands:
ret = system_call(command)
if ret != 0:
raise RuntimeError(f"hatch-cpp build command failed with exit code {ret}: {command}")
return self.commands
def cleanup(self):
if self.platform.platform == "win32":
for temp_obj in Path(".").glob("*.obj"):
temp_obj.unlink()