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
13 changes: 13 additions & 0 deletions compile_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,9 @@ def __init__(
# MSBuild falls back to the newest installed compiler regardless of what the
# environment or -vcvars_ver requested.
self.msvc_tools_version = None
# CMake's default Visual Studio generator is the newest installed version, regardless
# of the environment that vcvars sets up, so the choice must be passed explicitly.
self.cmake_generator = None
self.mode = mode
self.strict_mode = True

Expand Down Expand Up @@ -1361,10 +1364,20 @@ def _arm64_platform_flag(self, generator_args: List[str]) -> List[str]:
return ["-A ARM64"] if "Visual Studio" in generator else []
return ["-A ARM64"]

def _default_generator_args(self, generator_args: List[str]) -> List[str]:
"""Return the arguments selecting the configured CMake generator, or an empty list
when the caller already chose one or none is configured."""
if not self.cmake_generator:
return []
if any(arg.startswith("-G") for arg in generator_args or []):
return []
return ["-G", self.cmake_generator]

def _cmake_configure(self, extra_args: List[str] = None):
options = self.get_cmake_options()
if extra_args:
options.extend(extra_args)
options.extend(self._default_generator_args(extra_args))
options.extend(self._arm64_platform_flag(extra_args))
options.append(
".."
Expand Down
29 changes: 27 additions & 2 deletions create_libpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,11 @@ def write_manifest(outer_config: dict, mode_used: compile_all.BuildMode):
"2026": "[18.0,19.0)",
}

CMAKE_GENERATORS_BY_VS_MAJOR_VERSION = {
"17": "Visual Studio 17 2022",
"18": "Visual Studio 18 2026",
}


def list_msvc_tools_versions(vs_install_path: str) -> list:
"""Return the names of MSVC tools directories that contain a working compiler. Used
Expand Down Expand Up @@ -469,7 +474,14 @@ def resolve_msvc_tools_version(vs_install_path: str, requested: str) -> str:
return matches[0]


def build_vswhere_args(vs_version: str) -> list:
def cmake_generator_for_vs_version(installation_version: str) -> str:
"""Return the CMake generator matching a Visual Studio installation version such as
'17.14.36408.4', or None if this script does not know that major version."""
major_version = installation_version.strip().split(".")[0]
return CMAKE_GENERATORS_BY_VS_MAJOR_VERSION.get(major_version)


def build_vswhere_args(vs_version: str, vs_property: str = "installationPath") -> list:
"""Build the vswhere command line for the requested Visual Studio selection. The
'latest' value selects whatever vswhere considers newest. Nicer aliases like
'2022' and '2026' translate to vswhere's -version range syntax. Any other value is
Expand All @@ -482,7 +494,7 @@ def build_vswhere_args(vs_version: str) -> list:
"-requires",
"Microsoft.VisualStudio.Component.VC.Tools.x86.x64", # Dirty lie, works on ARM too
"-property",
"installationPath",
vs_property,
]
if vs_version == "latest":
return [vswhere, "-latest"] + base[1:]
Expand Down Expand Up @@ -696,6 +708,19 @@ def prevent_sleep_mode():
)
exit(1)

vs_installation_version = subprocess.check_output(
build_vswhere_args(args["vs_version"], "installationVersion"),
text=True,
)
compiler.cmake_generator = cmake_generator_for_vs_version(vs_installation_version)
if not compiler.cmake_generator:
print(
f"ERROR: No CMake generator is known for Visual Studio version "
f"{vs_installation_version.strip()!r}. Add it to "
"CMAKE_GENERATORS_BY_VS_MAJOR_VERSION."
)
exit(1)

base_path = Path(vs_install_path) / "VC" / "Auxiliary" / "Build"
if platform.machine() == "ARM64":
init_bat = str(base_path / "vcvarsarm64.bat")
Expand Down
31 changes: 31 additions & 0 deletions test_compile_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,37 @@ def tearDown(self) -> None:
os.chdir(self.original_dir)
super().tearDown()

def test_default_generator_args_uses_configured_generator(self):
"""Without an explicit generator, the configured one is passed to CMake."""
self.compiler.cmake_generator = "Visual Studio 17 2022"
self.assertEqual(
self.compiler._default_generator_args(["-D FOO=BAR"]),
["-G", "Visual Studio 17 2022"],
)
self.assertEqual(
self.compiler._default_generator_args(None),
["-G", "Visual Studio 17 2022"],
)

def test_default_generator_args_respects_explicit_generator(self):
"""A package that selects its own generator, such as Ninja, keeps that choice."""
self.compiler.cmake_generator = "Visual Studio 17 2022"
self.assertEqual(self.compiler._default_generator_args(["-G", "Ninja"]), [])
self.assertEqual(self.compiler._default_generator_args(["-GNinja"]), [])

def test_default_generator_args_without_configured_generator(self):
"""With no configured generator the choice is left to CMake."""
self.assertEqual(self.compiler._default_generator_args(["-D FOO=BAR"]), [])

@patch("compile_all.Compiler._run_cmake")
@patch("compile_all.Compiler.get_cmake_options", return_value=[])
def test_cmake_configure_passes_configured_generator(self, _, run_cmake_mock: MagicMock):
"""Configuring a package passes the configured generator through to CMake."""
self.compiler.cmake_generator = "Visual Studio 17 2022"
self.compiler._cmake_configure(["-D FOO=BAR"])
options = run_cmake_mock.call_args[0][0]
self.assertEqual(options[options.index("-G") + 1], "Visual Studio 17 2022")

@patch("os.chdir")
@patch("compile_all.Compiler.build_nonexistent")
def test_compile_all_calls_build_function(self, nonexistent_mock: MagicMock, _):
Expand Down
22 changes: 22 additions & 0 deletions test_create_libpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,28 @@ def test_build_vswhere_args_raw_range_passthrough(self):
idx = args.index("-version")
self.assertEqual(args[idx + 1], "[16.0,17.0)")

def test_build_vswhere_args_requested_property(self):
"""The queried vswhere property can be overridden, defaulting to the install path."""
default_args = create_libpack.build_vswhere_args("2022")
self.assertEqual(default_args[default_args.index("-property") + 1], "installationPath")
args = create_libpack.build_vswhere_args("2022", "installationVersion")
self.assertEqual(args[args.index("-property") + 1], "installationVersion")

def test_cmake_generator_for_vs_version(self):
"""The generator follows the major version of the selected Visual Studio install."""
self.assertEqual(
create_libpack.cmake_generator_for_vs_version("17.14.36408.4\n"),
"Visual Studio 17 2022",
)
self.assertEqual(
create_libpack.cmake_generator_for_vs_version("18.0.11111.16"),
"Visual Studio 18 2026",
)

def test_cmake_generator_for_unknown_vs_version(self):
"""An unrecognized major version yields None so that the caller can refuse to build."""
self.assertIsNone(create_libpack.cmake_generator_for_vs_version("16.11.0.0"))

@patch("os.chdir")
@patch("subprocess.run")
def test_clone_qt_skips_submodule_init(self, run_mock: MagicMock, _):
Expand Down
Loading