Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,6 @@ hatch_cpp/labextension

# Rust
target

vcpkg
vcpkg_installed
31 changes: 26 additions & 5 deletions hatch_cpp/config.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
from __future__ import annotations

from logging import getLogger
from os import system as system_call
from pathlib import Path
from typing import List, Optional

from pydantic import BaseModel, Field, model_validator

from .toolchains import BuildType, HatchCppCmakeConfiguration, HatchCppLibrary, HatchCppPlatform, HatchCppVcpkgConfiguration
from .toolchains import BuildType, HatchCppCmakeConfiguration, HatchCppLibrary, HatchCppPlatform, HatchCppVcpkgConfiguration, Toolchain

__all__ = (
"HatchCppBuildConfig",
"HatchCppBuildPlan",
)


_log = getLogger(__name__)


class HatchCppBuildConfig(BaseModel):
"""Build config values for Hatch C++ Builder."""

Expand All @@ -22,7 +26,7 @@ class HatchCppBuildConfig(BaseModel):
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=None)
vcpkg: Optional[HatchCppVcpkgConfiguration] = Field(default_factory=HatchCppVcpkgConfiguration)

@model_validator(mode="wrap")
@classmethod
Expand All @@ -41,6 +45,8 @@ def validate_model(cls, data, handler):
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.")
Expand All @@ -51,20 +57,35 @@ 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.commands.extend(self.vcpkg.generate(self.platform))

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}"
)
elif self.cmake:

if "cmake" in self._active_toolchains:
self.commands.extend(self.cmake.generate(self))

return self.commands
Expand Down
2 changes: 1 addition & 1 deletion hatch_cpp/tests/test_project_cmake_vcpkg/vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "main",
"version-string": "latest",
"dependencies": [
"arrow"
"nlohmann-json"
],
"builtin-baseline": "b94ade01f19e4436d8c8a16a5c52e8c802ef67dd"
}
2 changes: 1 addition & 1 deletion hatch_cpp/tests/test_project_pybind_vcpkg/vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "main",
"version-string": "latest",
"dependencies": [
"arrow"
"nlohmann-json"
],
"builtin-baseline": "b94ade01f19e4436d8c8a16a5c52e8c802ef67dd"
}
16 changes: 15 additions & 1 deletion hatch_cpp/toolchains/cmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@

__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: Path
Expand All @@ -33,6 +43,10 @@ def generate(self, config) -> Dict[str, Any]:
# 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}"
Expand All @@ -42,7 +56,7 @@ def generate(self, config) -> Dict[str, Any]:
# TODO: CMAKE_CXX_COMPILER
if config.platform.platform == "win32":
# TODO: prefix?
commands[-1] += f' -G "{environ.get("GENERATOR", "Visual Studio 17 2022")}"'
commands[-1] += f' -G "{environ.get("CMAKE_GENERATOR", "Visual Studio 17 2022")}"'

# Put in CMake flags
args = self.cmake_args.copy()
Expand Down
2 changes: 2 additions & 0 deletions hatch_cpp/toolchains/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
__all__ = (
"BuildType",
"CompilerToolchain",
"Toolchain",
"Language",
"Binding",
"Platform",
Expand All @@ -24,6 +25,7 @@

BuildType = Literal["debug", "release"]
CompilerToolchain = Literal["gcc", "clang", "msvc"]
Toolchain = Literal["vcpkg", "cmake", "vanilla"]
Language = Literal["c", "c++"]
Binding = Literal["cpython", "pybind11", "nanobind", "generic"]
Platform = Literal["linux", "darwin", "win32"]
Expand Down
50 changes: 42 additions & 8 deletions hatch_cpp/toolchains/vcpkg.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,64 @@
from __future__ import annotations

from pathlib import Path
from platform import machine as platform_machine
from sys import platform as sys_platform
from typing import Optional
from typing import Literal, Optional

from pydantic import BaseModel, Field

__all__ = ("HatchCppVcpkgConfiguration",)


VcpkgTriplet = Literal[
"x64-android",
"x64-osx",
"x64-linux",
"x64-uwp",
"x64-windows",
"x64-windows-release",
"x64-windows-static",
"x64-windows-static-md",
"x86-windows",
"arm-neon-android",
"arm64-android",
"arm64-osx",
"arm64-uwp",
"arm64-windows",
"arm64-windows-static-md",
]
VcpkgPlatformDefaults = {
("linux", "x86_64"): "x64-linux",
# ("linux", "arm64"): "",
("darwin", "x86_64"): "x64-osx",
("darwin", "arm64"): "arm64-osx",
("win32", "x86_64"): "x64-windows-static-md",
("win32", "arm64"): "arm64-windows-static-md",
}


class HatchCppVcpkgConfiguration(BaseModel):
vcpkg: Optional[str] = Field(default="vcpkg.json")
vcpkg_root: Optional[Path] = Field(default=Path("vcpkg"))
vcpkg_repo: Optional[str] = Field(default="https://github.com/microsoft/vcpkg.git")
vcpkg_triplet: Optional[VcpkgTriplet] = Field(default=None)

# TODO: overlay

def generate(self, config):
commands = []

if self.vcpkg and Path(self.vcpkg.vcpkg).exists():
if not Path(self.vcpkg.vcpkg_root).exists():
commands.append(f"git clone {self.vcpkg.vcpkg_repo} {self.vcpkg.vcpkg_root}")
if self.vcpkg_triplet is None:
self.vcpkg_triplet = VcpkgPlatformDefaults.get((sys_platform, platform_machine()))
if self.vcpkg_triplet is None:
raise ValueError(f"Could not determine vcpkg triplet for platform {sys_platform} and architecture {platform_machine()}")

if self.vcpkg and Path(self.vcpkg).exists():
if not Path(self.vcpkg_root).exists():
commands.append(f"git clone {self.vcpkg_repo} {self.vcpkg_root}")
commands.append(
f"./{self.vcpkg.vcpkg_root / 'bootstrap-vcpkg.sh' if sys_platform != 'win32' else self.vcpkg.vcpkg_root / 'sbootstrap-vcpkg.bat'}"
f"./{self.vcpkg_root / 'bootstrap-vcpkg.sh' if sys_platform != 'win32' else self.vcpkg_root / 'sbootstrap-vcpkg.bat'}"
)
commands.append(
f"./{self.vcpkg.vcpkg_root / 'vcpkg'} install --triplet {config.platform.platform}-{config.platform.toolchain} --manifest-root {Path(self.vcpkg.vcpkg).parent}"
)
commands.append(f"./{self.vcpkg_root / 'vcpkg'} install --triplet {self.vcpkg_triplet}")

return commands