Skip to content

Commit 8555bbf

Browse files
committed
didnt
1 parent 2d20eab commit 8555bbf

File tree

4 files changed

+65
-22
lines changed

4 files changed

+65
-22
lines changed

bootstrap/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
venv
2+
build_environment.bat
3+

bootstrap/main.py

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
compiler_override = package_manager.install_compiler()
4242
else:
4343
compiler_override = ""
44+
package_manager.ensureEnvironment()
45+
4446
cmake_options_for_parsing = " ".join(filter(None, [args.minifi_options, compiler_override]))
4547
cmake_options_for_cmake = " ".join(filter(None, [args.cmake_options, compiler_override]))
4648

bootstrap/package_manager.py

+60-21
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020
from typing import Dict, Set
2121

2222
from distro import distro
23+
from enum import Enum
2324

25+
class VsWhereLocation(Enum):
26+
CHOCO = 1
27+
DEFAULT = 2
2428

2529
def _query_yes_no(question: str, no_confirm: bool) -> bool:
2630
valid = {"yes": True, "y": True, "ye": True, "no": False, "n": False}
@@ -52,6 +56,9 @@ def install(self, dependencies: Dict[str, Set[str]]) -> bool:
5256

5357
def install_compiler(self) -> str:
5458
raise Exception("NotImplementedException")
59+
60+
def ensureEnvironment(self):
61+
pass
5562

5663
def _install(self, dependencies: Dict[str, Set[str]], replace_dict: Dict[str, Set[str]], install_cmd: str) -> bool:
5764
dependencies.update({k: v for k, v in replace_dict.items() if k in dependencies})
@@ -76,6 +83,7 @@ def run_cmd(self, cmd: str) -> bool:
7683
return result.returncode == 0
7784

7885

86+
7987
class BrewPackageManager(PackageManager):
8088
def __init__(self, no_confirm):
8189
PackageManager.__init__(self, no_confirm)
@@ -174,40 +182,56 @@ def install_compiler(self) -> str:
174182
return ""
175183

176184

177-
def _get_vs_dev_cmd_path() -> str:
185+
def _get_vs_dev_cmd_path(vs_where_location: VsWhereLocation) -> str:
186+
if vs_where_location == VsWhereLocation.CHOCO:
187+
vs_where_path = "vswhere"
188+
else:
189+
vs_where_path = "%ProgramFiles(x86)%\\Microsoft Visual Studio\\Installer\\vswhere.exe"
190+
178191
vswhere_results = subprocess.run(
179-
"vswhere -products * -property installationPath -requires Microsoft.VisualStudio.Component.VC.ATL",
192+
f"{vs_where_path} -products * -property installationPath -requires Microsoft.VisualStudio.Component.VC.ATL -version 17",
180193
capture_output=True)
181194

182195
for vswhere_result in vswhere_results.stdout.splitlines():
183196
possible_path = f"{vswhere_result.decode()}\\Common7\\Tools\\VsDevCmd.bat"
184197
if os.path.exists(possible_path):
185198
return f'"{possible_path}"'
186-
raise Exception("Could not find valid Visual Studio installation")
199+
return None
187200

188201

189-
def _get_vs_dev_cmd() -> str:
190-
vs_dev_path = _get_vs_dev_cmd_path()
191-
return f"{vs_dev_path} -arch=x64 -host_arch=x64"
202+
def _get_vs_dev_cmd(vs_where_location: VsWhereLocation) -> str:
203+
vs_dev_path = _get_vs_dev_cmd_path(vs_where_location)
204+
return f"{vs_dev_path}"
192205

193206

194-
def _get_venv_path():
195-
return pathlib.Path(__file__).parent.parent.resolve() / "build"
207+
def _get_activate_venv_path():
208+
return pathlib.Path(__file__).parent.resolve() / "venv" / "Scripts" / "activate.bat"
196209

197210

198-
def _minifi_setup_env_str() -> str:
199-
return f"""@echo off
211+
def _minifi_setup_env_str(vs_where_location: VsWhereLocation) -> str:
212+
return f"""
200213
call refreshenv
201-
call "{_get_vs_dev_cmd()}"
214+
call {_get_vs_dev_cmd(vs_where_location)}
202215
setlocal EnableDelayedExpansion
203-
set PATH=!PATH:C:\\Strawberry\\c\\bin;=!;C:\Program Files\\NASM;
216+
set PATH=!PATH:C:\\Strawberry\\c\\bin;=!;C:\\Program Files\\NASM;
204217
endlocal & set PATH=%PATH%
205218
set build_platform=x64
206-
{_get_venv_path()}
219+
IF "%VIRTUALENV%"=="" (
220+
echo already in venv
221+
) ELSE (
222+
{_get_activate_venv_path()}
223+
)
224+
207225
"""
208226

227+
def _create_minifi_setup_env_batch(vs_where_location: VsWhereLocation) -> str:
228+
with open("build_environment.bat","w") as f:
229+
f.write(_minifi_setup_env_str(vs_where_location))
230+
209231

210232
class ChocolateyPackageManager(PackageManager):
233+
234+
211235
def __init__(self, no_confirm):
212236
PackageManager.__init__(self, no_confirm)
213237

@@ -240,20 +264,35 @@ def _get_installed_packages(self) -> Set[str]:
240264
lines.append("NASM") # choco doesnt remember NASM
241265
return set(lines)
242266

267+
def _acquire_vswhere(self):
268+
installed_packages = self._get_installed_packages()
269+
if "vswhere" in installed_packages:
270+
return VsWhereLocation.CHOCO
271+
vswhere_default_path = "%ProgramFiles(x86)%\\Microsoft Visual Studio\\Installer\\vswhere.exe"
272+
if os.path.exists(vswhere_default_path):
273+
return VsWhereLocation.DEFAULT
274+
self.install({"vswhere":{"vswhere"}})
275+
return VsWhereLocation.CHOCO
276+
277+
243278
def install_compiler(self) -> str:
244-
self.install({"visualstudio2022buildtools": {'visualstudio2022buildtools --package-parameters "--wait --quiet '
245-
'--add Microsoft.VisualStudio.Workload.VCTools '
246-
'--add Microsoft.VisualStudio.Component.VC.ATL '
247-
'--includeRecommended"'},
248-
"vswhere": {"vswhere"}})
279+
vs_where_loc = self._acquire_vswhere()
280+
vs_dev_path = _get_vs_dev_cmd_path(vs_where_loc)
281+
if not vs_dev_path:
282+
self.install({"visualstudio2022buildtools": {'visualstudio2022buildtools --package-parameters "--wait --quiet '
283+
'--add Microsoft.VisualStudio.Workload.VCTools '
284+
'--add Microsoft.VisualStudio.Component.VC.ATL '
285+
'--includeRecommended"'}})
249286
return ""
250287

251288
def run_cmd(self, cmd: str) -> bool:
252-
cmd_command = f"refreshenv & {_get_vs_dev_cmd()} & set PATH=!PATH:C:\\Strawberry\\c\\bin;=!;C:\\Program Files\\NASM; & {cmd}"
253-
cmd_command_list = f'cmd /V:ON /C {cmd_command}'
254-
res = subprocess.run(cmd_command_list, check=True, text=True)
289+
env_bat_path = pathlib.Path(__file__).parent.resolve() / "build_environment.bat"
290+
res = subprocess.run(f"{env_bat_path} & {cmd}", check=True, text=True)
255291

256292
return res.returncode == 0
293+
294+
def ensureEnvironment(self):
295+
_create_minifi_setup_env_batch(self._acquire_vswhere())
257296

258297

259298
def get_package_manager(no_confirm: bool) -> PackageManager:

bootstrap/py_bootstrap.bat

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ if exist "%VENV_DIR%" (
2525
call "%VENV_DIR%\Scripts\activate.bat"
2626
pip install -r "%SCRIPT_DIR%requirements.txt"
2727
)
28-
2928
python "%SCRIPT_DIR%main.py"
29+
deactivate

0 commit comments

Comments
 (0)