hatch-cpp 0.3.3__py3-none-any.whl → 0.3.5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
hatch_cpp/__init__.py CHANGED
@@ -1,4 +1,4 @@
1
- __version__ = "0.3.3"
1
+ __version__ = "0.3.5"
2
2
 
3
3
  from .config import *
4
4
  from .hooks import *
hatch_cpp/config.py CHANGED
@@ -93,7 +93,9 @@ class HatchCppBuildPlan(HatchCppBuildConfig):
93
93
 
94
94
  def execute(self):
95
95
  for command in self.commands:
96
- system_call(command)
96
+ ret = system_call(command)
97
+ if ret != 0:
98
+ raise RuntimeError(f"hatch-cpp build command failed with exit code {ret}: {command}")
97
99
  return self.commands
98
100
 
99
101
  def cleanup(self):
@@ -0,0 +1,174 @@
1
+ """Tests for vcpkg ref/branch checkout support."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from pathlib import Path
6
+
7
+ from hatch_cpp.toolchains.vcpkg import (
8
+ HatchCppVcpkgConfiguration,
9
+ _read_vcpkg_ref_from_gitmodules,
10
+ )
11
+
12
+
13
+ class TestReadVcpkgRefFromGitmodules:
14
+ """Tests for the _read_vcpkg_ref_from_gitmodules helper."""
15
+
16
+ def test_no_gitmodules_file(self, tmp_path, monkeypatch):
17
+ monkeypatch.chdir(tmp_path)
18
+ assert _read_vcpkg_ref_from_gitmodules(Path("vcpkg")) is None
19
+
20
+ def test_gitmodules_without_vcpkg_submodule(self, tmp_path, monkeypatch):
21
+ monkeypatch.chdir(tmp_path)
22
+ (tmp_path / ".gitmodules").write_text('[submodule "other"]\n\tpath = other\n\turl = https://github.com/example/other.git\n')
23
+ assert _read_vcpkg_ref_from_gitmodules(Path("vcpkg")) is None
24
+
25
+ def test_gitmodules_vcpkg_without_branch(self, tmp_path, monkeypatch):
26
+ monkeypatch.chdir(tmp_path)
27
+ (tmp_path / ".gitmodules").write_text('[submodule "vcpkg"]\n\tpath = vcpkg\n\turl = https://github.com/microsoft/vcpkg.git\n')
28
+ assert _read_vcpkg_ref_from_gitmodules(Path("vcpkg")) is None
29
+
30
+ def test_gitmodules_vcpkg_with_branch(self, tmp_path, monkeypatch):
31
+ monkeypatch.chdir(tmp_path)
32
+ (tmp_path / ".gitmodules").write_text(
33
+ '[submodule "vcpkg"]\n\tpath = vcpkg\n\turl = https://github.com/microsoft/vcpkg.git\n\tbranch = 2024.01.12\n'
34
+ )
35
+ assert _read_vcpkg_ref_from_gitmodules(Path("vcpkg")) == "2024.01.12"
36
+
37
+ def test_gitmodules_vcpkg_with_commit_sha_branch(self, tmp_path, monkeypatch):
38
+ monkeypatch.chdir(tmp_path)
39
+ sha = "a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2"
40
+ (tmp_path / ".gitmodules").write_text(
41
+ f'[submodule "vcpkg"]\n\tpath = vcpkg\n\turl = https://github.com/microsoft/vcpkg.git\n\tbranch = {sha}\n'
42
+ )
43
+ assert _read_vcpkg_ref_from_gitmodules(Path("vcpkg")) == sha
44
+
45
+ def test_gitmodules_custom_vcpkg_root(self, tmp_path, monkeypatch):
46
+ monkeypatch.chdir(tmp_path)
47
+ (tmp_path / ".gitmodules").write_text(
48
+ '[submodule "deps/vcpkg"]\n\tpath = deps/vcpkg\n\turl = https://github.com/microsoft/vcpkg.git\n\tbranch = 2024.06.15\n'
49
+ )
50
+ # Default vcpkg root won't match
51
+ assert _read_vcpkg_ref_from_gitmodules(Path("vcpkg")) is None
52
+ # Custom root matches
53
+ assert _read_vcpkg_ref_from_gitmodules(Path("deps/vcpkg")) == "2024.06.15"
54
+
55
+ def test_gitmodules_multiple_submodules(self, tmp_path, monkeypatch):
56
+ monkeypatch.chdir(tmp_path)
57
+ (tmp_path / ".gitmodules").write_text(
58
+ '[submodule "other"]\n'
59
+ "\tpath = other\n"
60
+ "\turl = https://github.com/example/other.git\n"
61
+ "\tbranch = main\n"
62
+ '[submodule "vcpkg"]\n'
63
+ "\tpath = vcpkg\n"
64
+ "\turl = https://github.com/microsoft/vcpkg.git\n"
65
+ "\tbranch = 2024.01.12\n"
66
+ )
67
+ assert _read_vcpkg_ref_from_gitmodules(Path("vcpkg")) == "2024.01.12"
68
+
69
+
70
+ class TestVcpkgRefConfig:
71
+ """Tests for vcpkg_ref configuration field."""
72
+
73
+ def test_default_vcpkg_ref_is_none(self):
74
+ cfg = HatchCppVcpkgConfiguration()
75
+ assert cfg.vcpkg_ref is None
76
+
77
+ def test_explicit_vcpkg_ref(self):
78
+ cfg = HatchCppVcpkgConfiguration(vcpkg_ref="2024.01.12")
79
+ assert cfg.vcpkg_ref == "2024.01.12"
80
+
81
+ def test_explicit_vcpkg_ref_commit_sha(self):
82
+ sha = "a1b2c3d4e5f6"
83
+ cfg = HatchCppVcpkgConfiguration(vcpkg_ref=sha)
84
+ assert cfg.vcpkg_ref == sha
85
+
86
+
87
+ class TestResolveVcpkgRef:
88
+ """Tests for _resolve_vcpkg_ref priority logic."""
89
+
90
+ def test_explicit_ref_takes_priority_over_gitmodules(self, tmp_path, monkeypatch):
91
+ monkeypatch.chdir(tmp_path)
92
+ (tmp_path / ".gitmodules").write_text(
93
+ '[submodule "vcpkg"]\n\tpath = vcpkg\n\turl = https://github.com/microsoft/vcpkg.git\n\tbranch = 2024.01.12\n'
94
+ )
95
+ cfg = HatchCppVcpkgConfiguration(vcpkg_ref="my-custom-tag")
96
+ assert cfg._resolve_vcpkg_ref() == "my-custom-tag"
97
+
98
+ def test_falls_back_to_gitmodules(self, tmp_path, monkeypatch):
99
+ monkeypatch.chdir(tmp_path)
100
+ (tmp_path / ".gitmodules").write_text(
101
+ '[submodule "vcpkg"]\n\tpath = vcpkg\n\turl = https://github.com/microsoft/vcpkg.git\n\tbranch = 2024.01.12\n'
102
+ )
103
+ cfg = HatchCppVcpkgConfiguration()
104
+ assert cfg._resolve_vcpkg_ref() == "2024.01.12"
105
+
106
+ def test_returns_none_when_no_ref(self, tmp_path, monkeypatch):
107
+ monkeypatch.chdir(tmp_path)
108
+ cfg = HatchCppVcpkgConfiguration()
109
+ assert cfg._resolve_vcpkg_ref() is None
110
+
111
+
112
+ class TestVcpkgGenerate:
113
+ """Tests that generate() includes the checkout command when a ref is set."""
114
+
115
+ def _make_vcpkg_env(self, tmp_path):
116
+ """Create a minimal vcpkg.json so generate() produces commands."""
117
+ (tmp_path / "vcpkg.json").write_text("{}")
118
+
119
+ def test_generate_with_explicit_ref(self, tmp_path, monkeypatch):
120
+ monkeypatch.chdir(tmp_path)
121
+ self._make_vcpkg_env(tmp_path)
122
+
123
+ cfg = HatchCppVcpkgConfiguration(vcpkg_ref="2024.01.12")
124
+ commands = cfg.generate(None)
125
+
126
+ assert any("git clone" in cmd for cmd in commands)
127
+ assert any("git -C vcpkg checkout 2024.01.12" in cmd for cmd in commands)
128
+ # checkout must come after clone but before bootstrap
129
+ clone_idx = next(i for i, c in enumerate(commands) if "git clone" in c)
130
+ checkout_idx = next(i for i, c in enumerate(commands) if "checkout" in c)
131
+ bootstrap_idx = next(i for i, c in enumerate(commands) if "bootstrap" in c)
132
+ assert clone_idx < checkout_idx < bootstrap_idx
133
+
134
+ def test_generate_with_gitmodules_ref(self, tmp_path, monkeypatch):
135
+ monkeypatch.chdir(tmp_path)
136
+ self._make_vcpkg_env(tmp_path)
137
+ (tmp_path / ".gitmodules").write_text(
138
+ '[submodule "vcpkg"]\n\tpath = vcpkg\n\turl = https://github.com/microsoft/vcpkg.git\n\tbranch = 2024.06.15\n'
139
+ )
140
+
141
+ cfg = HatchCppVcpkgConfiguration()
142
+ commands = cfg.generate(None)
143
+
144
+ assert any("git -C vcpkg checkout 2024.06.15" in cmd for cmd in commands)
145
+
146
+ def test_generate_without_ref(self, tmp_path, monkeypatch):
147
+ monkeypatch.chdir(tmp_path)
148
+ self._make_vcpkg_env(tmp_path)
149
+
150
+ cfg = HatchCppVcpkgConfiguration()
151
+ commands = cfg.generate(None)
152
+
153
+ assert not any("checkout" in cmd for cmd in commands)
154
+ assert any("git clone" in cmd for cmd in commands)
155
+
156
+ def test_generate_skips_clone_when_vcpkg_root_exists(self, tmp_path, monkeypatch):
157
+ monkeypatch.chdir(tmp_path)
158
+ self._make_vcpkg_env(tmp_path)
159
+ (tmp_path / "vcpkg").mkdir()
160
+
161
+ cfg = HatchCppVcpkgConfiguration(vcpkg_ref="2024.01.12")
162
+ commands = cfg.generate(None)
163
+
164
+ # When vcpkg_root already exists, no clone or checkout happens
165
+ assert not any("git clone" in cmd for cmd in commands)
166
+ assert not any("checkout" in cmd for cmd in commands)
167
+ assert any("vcpkg" in cmd and "install" in cmd for cmd in commands)
168
+
169
+ def test_generate_no_vcpkg_json(self, tmp_path, monkeypatch):
170
+ monkeypatch.chdir(tmp_path)
171
+ # No vcpkg.json => no commands at all
172
+ cfg = HatchCppVcpkgConfiguration(vcpkg_ref="2024.01.12")
173
+ commands = cfg.generate(None)
174
+ assert commands == []
@@ -323,7 +323,9 @@ class HatchCppPlatform(BaseModel):
323
323
  flags += " " + " ".join(f"/U{macro}" for macro in effective_undef_macros)
324
324
  flags += " /EHsc /DWIN32"
325
325
  if library.std:
326
- flags += f" /std:{library.std}"
326
+ # MSVC minimum is c++14; clamp older standards
327
+ std = library.std if library.std not in ("c++11", "c++0x") else "c++14"
328
+ flags += f" /std:{std}"
327
329
  # clean
328
330
  while flags.count(" "):
329
331
  flags = flags.replace(" ", " ")
@@ -1,5 +1,6 @@
1
1
  from __future__ import annotations
2
2
 
3
+ import configparser
3
4
  from pathlib import Path
4
5
  from platform import machine as platform_machine
5
6
  from sys import platform as sys_platform
@@ -38,14 +39,45 @@ VcpkgPlatformDefaults = {
38
39
  }
39
40
 
40
41
 
42
+ def _read_vcpkg_ref_from_gitmodules(vcpkg_root: Path) -> Optional[str]:
43
+ """Read the branch/ref for vcpkg from .gitmodules if it exists.
44
+
45
+ Looks for a submodule whose path matches ``vcpkg_root`` and returns
46
+ its ``branch`` value when present.
47
+ """
48
+ gitmodules_path = Path(".gitmodules")
49
+ if not gitmodules_path.exists():
50
+ return None
51
+
52
+ parser = configparser.ConfigParser()
53
+ parser.read(str(gitmodules_path))
54
+
55
+ for section in parser.sections():
56
+ if parser.get(section, "path", fallback=None) == str(vcpkg_root):
57
+ return parser.get(section, "branch", fallback=None)
58
+
59
+ return None
60
+
61
+
41
62
  class HatchCppVcpkgConfiguration(BaseModel):
42
63
  vcpkg: Optional[str] = Field(default="vcpkg.json")
43
64
  vcpkg_root: Optional[Path] = Field(default=Path("vcpkg"))
44
65
  vcpkg_repo: Optional[str] = Field(default="https://github.com/microsoft/vcpkg.git")
45
66
  vcpkg_triplet: Optional[VcpkgTriplet] = Field(default=None)
67
+ vcpkg_ref: Optional[str] = Field(
68
+ default=None,
69
+ description="Branch, tag, or commit SHA to checkout after cloning vcpkg. "
70
+ "If not set, falls back to the branch specified in .gitmodules for the vcpkg submodule.",
71
+ )
46
72
 
47
73
  # TODO: overlay
48
74
 
75
+ def _resolve_vcpkg_ref(self) -> Optional[str]:
76
+ """Return the ref to checkout: explicit config takes priority, then .gitmodules."""
77
+ if self.vcpkg_ref is not None:
78
+ return self.vcpkg_ref
79
+ return _read_vcpkg_ref_from_gitmodules(self.vcpkg_root)
80
+
49
81
  def generate(self, config):
50
82
  commands = []
51
83
 
@@ -57,9 +89,12 @@ class HatchCppVcpkgConfiguration(BaseModel):
57
89
  if self.vcpkg and Path(self.vcpkg).exists():
58
90
  if not Path(self.vcpkg_root).exists():
59
91
  commands.append(f"git clone {self.vcpkg_repo} {self.vcpkg_root}")
60
- commands.append(
61
- f"./{self.vcpkg_root / 'bootstrap-vcpkg.sh' if sys_platform != 'win32' else self.vcpkg_root / 'sbootstrap-vcpkg.bat'}"
62
- )
92
+
93
+ ref = self._resolve_vcpkg_ref()
94
+ if ref is not None:
95
+ commands.append(f"git -C {self.vcpkg_root} checkout {ref}")
96
+
97
+ commands.append(f"./{self.vcpkg_root / 'bootstrap-vcpkg.sh' if sys_platform != 'win32' else self.vcpkg_root / 'bootstrap-vcpkg.bat'}")
63
98
  commands.append(f"./{self.vcpkg_root / 'vcpkg'} install --triplet {self.vcpkg_triplet}")
64
99
 
65
100
  return commands
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: hatch-cpp
3
- Version: 0.3.3
3
+ Version: 0.3.5
4
4
  Summary: Hatch plugin for C++ builds
5
5
  Project-URL: Repository, https://github.com/python-project-templates/hatch-cpp
6
6
  Project-URL: Homepage, https://github.com/python-project-templates/hatch-cpp
@@ -1,5 +1,5 @@
1
- hatch_cpp/__init__.py,sha256=F_Va8GKZ1FkEZH6EYG9DQRgQpZ3DVRwQDTsMo6Qf9FI,114
2
- hatch_cpp/config.py,sha256=LlwDBmD1nir08Y5GOl4wa29h2iJBtNRSzYMGHRVFJUo,3698
1
+ hatch_cpp/__init__.py,sha256=oGC85OzqpNweL1rFPBDebJVlVz3i-HmX54zLu5_A8gU,114
2
+ hatch_cpp/config.py,sha256=6oO8J2k91r88DwZwDJpxtxt2F5xZZ-N65IqSyabwOg4,3831
3
3
  hatch_cpp/hooks.py,sha256=SQkF5WJIgzw-8rvlTzuQvBqdP6K3fHgnh6CZOZFag50,203
4
4
  hatch_cpp/plugin.py,sha256=NcIDi7c9KH-_RkPeeCgWKoH5InIjw2eECswazwvGl_c,4304
5
5
  hatch_cpp/utils.py,sha256=lxd3U3aMYsTS6DYMc1y17MR16LzgRzv92f_xh87LSg8,297
@@ -7,6 +7,7 @@ hatch_cpp/tests/test_hatch_build.py,sha256=q2IKTFi3Pq99UsOyL84V-C9VtFUs3ZcA9UlA8
7
7
  hatch_cpp/tests/test_platform_specific.py,sha256=8EFHEv86e0CD1bIruHgLb28yf2pSRF1VgvcX2tK5bRo,20124
8
8
  hatch_cpp/tests/test_projects.py,sha256=vs-kHdiHM7pCqM3oGQsSryq5blRi0HyK5QFfHxlJDi8,1773
9
9
  hatch_cpp/tests/test_structs.py,sha256=cpzbPLneEbEONzXmPS1S9PL9mbhya4udc4eqTpU9w6w,2576
10
+ hatch_cpp/tests/test_vcpkg_ref.py,sha256=OEb4u3Ny-OYNYLanjGaG7-dL4WW48EYdbsxeDN0sqwM,7371
10
11
  hatch_cpp/tests/test_project_basic/pyproject.toml,sha256=eqM1UVpNmJWDfsuO18ZG_VOV9I4tAWgsM5Dhf49X8Nc,694
11
12
  hatch_cpp/tests/test_project_basic/cpp/project/basic.cpp,sha256=gQ2nmdLqIdgaqxSKvkN_vbp6Iv_pAoVIHETXPRnALb0,117
12
13
  hatch_cpp/tests/test_project_basic/cpp/project/basic.hpp,sha256=SO5GhPj8k3RzWrfH37lFSDc8w1Vf3yqTUhxmr1hnoko,422
@@ -57,10 +58,10 @@ hatch_cpp/tests/test_project_pybind_vcpkg/cpp/project/basic.hpp,sha256=LZSfCfhLY
57
58
  hatch_cpp/tests/test_project_pybind_vcpkg/project/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
58
59
  hatch_cpp/toolchains/__init__.py,sha256=anza4tXKxauobWMOUrxX3sEO0qs7sp6VdLXhLae3vkY,64
59
60
  hatch_cpp/toolchains/cmake.py,sha256=VQYYqiOu0ZEF0IBuep35Xad7jryWy04-dHYYf7c0XgY,3331
60
- hatch_cpp/toolchains/common.py,sha256=U8EYVEAzp8sQ0S8rslEKD4v-x8al4s5V7TLX80esrXA,18932
61
- hatch_cpp/toolchains/vcpkg.py,sha256=Gwv3YNXBngpdebvRuYuUcWCzPyDbmDvy8c0K7uV4k38,2146
62
- hatch_cpp-0.3.3.dist-info/METADATA,sha256=Sg9bdyfspii3R8hnhW-7HKoYBriu2i0ntsh90IqvCoo,9896
63
- hatch_cpp-0.3.3.dist-info/WHEEL,sha256=QccIxa26bgl1E6uMy58deGWi-0aeIkkangHcxk2kWfw,87
64
- hatch_cpp-0.3.3.dist-info/entry_points.txt,sha256=RgXfjpD4iwomJQK5n7FnPkXzb5fOnF5v3DI5hbkgVcw,30
65
- hatch_cpp-0.3.3.dist-info/licenses/LICENSE,sha256=FWyFTpd5xXEz50QpGDtsyIv6dgR2z_dHdoab_Nq_KMw,11452
66
- hatch_cpp-0.3.3.dist-info/RECORD,,
61
+ hatch_cpp/toolchains/common.py,sha256=o6whV_7W9GGtBsxgxmg-dHQqdFnuMLffkLHzdbqKqME,19075
62
+ hatch_cpp/toolchains/vcpkg.py,sha256=ku5NE4ihdzbxdReNUXsGx03fPG5FYdQC48mJhdpW-oo,3456
63
+ hatch_cpp-0.3.5.dist-info/METADATA,sha256=BNI_Ezt3hDfW5xhETfORdexE3tb4Ul4dY4M__K0hj10,9896
64
+ hatch_cpp-0.3.5.dist-info/WHEEL,sha256=QccIxa26bgl1E6uMy58deGWi-0aeIkkangHcxk2kWfw,87
65
+ hatch_cpp-0.3.5.dist-info/entry_points.txt,sha256=RgXfjpD4iwomJQK5n7FnPkXzb5fOnF5v3DI5hbkgVcw,30
66
+ hatch_cpp-0.3.5.dist-info/licenses/LICENSE,sha256=FWyFTpd5xXEz50QpGDtsyIv6dgR2z_dHdoab_Nq_KMw,11452
67
+ hatch_cpp-0.3.5.dist-info/RECORD,,