- Notifications
You must be signed in to change notification settings - Fork0
Refactor toolchains, start on vcpkg#69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
7 changes: 4 additions & 3 deletionshatch_cpp/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| __version__ = "0.1.8" | ||
| from .config import * | ||
| from .hooks import * | ||
| from .plugin import * | ||
| from .toolchains import * |
80 changes: 80 additions & 0 deletionshatch_cpp/config.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| from __future__ import annotations | ||
| 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 | ||
| __all__ = ( | ||
| "HatchCppBuildConfig", | ||
| "HatchCppBuildPlan", | ||
| ) | ||
| class HatchCppBuildConfig(BaseModel): | ||
| """Build config values for Hatch C++ Builder.""" | ||
| verbose: 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=None) | ||
| @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") | ||
| 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) | ||
| def generate(self): | ||
| self.commands = [] | ||
| if self.vcpkg and Path(self.vcpkg.vcpkg).exists(): | ||
| self.commands.extend(self.vcpkg.generate(self.platform)) | ||
| if self.libraries: | ||
| 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: | ||
| self.commands.extend(self.cmake.generate(self)) | ||
| return self.commands | ||
| def execute(self): | ||
| for command in self.commands: | ||
| system_call(command) | ||
| return self.commands | ||
| def cleanup(self): | ||
| if self.platform.platform == "win32": | ||
| for temp_obj in Path(".").glob("*.obj"): | ||
| temp_obj.unlink() |
2 changes: 1 addition & 1 deletionhatch_cpp/plugin.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletionhatch_cpp/tests/test_structs.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletionshatch_cpp/toolchains/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| from .cmake import * | ||
| from .common import * | ||
| from .vcpkg import * |
73 changes: 73 additions & 0 deletionshatch_cpp/toolchains/cmake.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| from __future__ import annotations | ||
| from os import environ | ||
| from pathlib import Path | ||
| from sys import version_info | ||
| from typing import Any, Dict, Optional | ||
| from pydantic import BaseModel, Field | ||
| from .common import Platform | ||
| __all__ = ("HatchCppCmakeConfiguration",) | ||
| class HatchCppCmakeConfiguration(BaseModel): | ||
| root: Path | ||
| 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, Any]] = 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}") | ||
| # 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 | ||
| if config.platform.platform == "win32": | ||
| # TODO: prefix? | ||
| commands[-1] += f' -G "{environ.get("GENERATOR", "Visual Studio 17 2022")}"' | ||
| # 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')}" | ||
| # 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 |
130 changes: 10 additions & 120 deletionshatch_cpp/structs.py → hatch_cpp/toolchains/common.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletionshatch_cpp/toolchains/vcpkg.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| from __future__ import annotations | ||
| from pathlib import Path | ||
| from sys import platform as sys_platform | ||
| from typing import Optional | ||
| from pydantic import BaseModel, Field | ||
| __all__ = ("HatchCppVcpkgConfiguration",) | ||
| 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") | ||
| 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}") | ||
| commands.append( | ||
| f"./{self.vcpkg.vcpkg_root / 'bootstrap-vcpkg.sh' if sys_platform != 'win32' else self.vcpkg.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}" | ||
| ) | ||
| return commands |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.