Skip to content

Commit 2a1a93d

Browse files
refackMylesBorins
authored andcommitted
build,windows: implement PEP514 python detection
Backport-PR-URL: #14842 PR-URL: #13900 Fixes: #13882 Reviewed-By: Tobias Nießen <[email protected]>
1 parent 1633f8b commit 2a1a93d

File tree

2 files changed

+66
-11
lines changed

2 files changed

+66
-11
lines changed

tools/msvs/find_python.cmd

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
@IF NOT DEFINED DEBUG_HELPER @ECHO OFF
2+
SETLOCAL
3+
:: If python.exe is in %Path%, just validate
4+
FOR /F "delims=" %%a IN ('where python 2^> NUL') DO (
5+
SET need_path=0
6+
SET p=%%~dpa
7+
IF NOT ERRORLEVEL 1 GOTO :validate
8+
)
9+
10+
:: Query the 3 locations mentioned in PEP 514 for a python2 InstallPath
11+
FOR %%K IN ( "HKCU\Software", "HKLM\SOFTWARE", "HKLM\Software\Wow6432Node") DO (
12+
SET need_path=1
13+
CALL :find-main-branch %%K
14+
:: If validate returns 0 just jump to the end
15+
IF NOT ERRORLEVEL 1 GOTO :validate
16+
)
17+
EXIT /B 1
18+
19+
:: Helper subroutine to handle quotes in %1
20+
:find-main-branch
21+
SET main_key="%~1\Python\PythonCore"
22+
REG QUERY %main_key% /s | findstr "2." | findstr InstallPath > NUL 2> NUL
23+
IF NOT ERRORLEVEL 1 CALL :find-key %main_key%
24+
EXIT /B
25+
26+
:: Query registry sub-tree for InstallPath
27+
:find-key
28+
FOR /F "delims=" %%a IN ('REG QUERY %1 /s ^| findstr "2." ^| findstr InstallPath') DO IF NOT ERRORLEVEL 1 CALL :find-path %%a
29+
EXIT /B
30+
31+
:: Parse the value of %1 as the path for python.exe
32+
:find-path
33+
FOR /F "tokens=3*" %%a IN ('REG QUERY %1 /ve') DO (
34+
SET pt=%%a
35+
IF NOT ERRORLEVEL 1 SET p=%pt%
36+
EXIT /B 0
37+
)
38+
EXIT /B 1
39+
40+
:: Check if %p% holds a path to a real python2 executable
41+
:validate
42+
IF NOT EXIST "%p%python.exe" EXIT /B 1
43+
:: Check if %p% is python2
44+
%p%python.exe -V 2>&1 | findstr /R "^Python.2.*" > NUL
45+
IF ERRORLEVEL 1 EXIT /B %ERRORLEVEL%
46+
:: We can wrap it up
47+
ENDLOCAL & SET pt=%p%& SET need_path_ext=%need_path%
48+
SET VCBUILD_PYTHON_LOCATION=%pt%python.exe
49+
IF %need_path_ext%==1 SET Path=%Path%;%pt%
50+
SET need_path_ext=
51+
EXIT /B %ERRORLEVEL%

vcbuild.bat

+15-11
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,7 @@ goto run
174174
if defined noprojgen goto msbuild
175175

176176
@rem Generate the VS project.
177-
echo configure %configure_flags% --dest-cpu=%target_arch% --tag=%TAG%
178-
python configure %configure_flags% --dest-cpu=%target_arch% --tag=%TAG%
177+
call :run-python configure %configure_flags% --dest-cpu=%target_arch% --tag=%TAG%
179178
if errorlevel 1 goto create-msvs-files-failed
180179
if not exist node.sln goto create-msvs-files-failed
181180
echo Project files generated.
@@ -340,8 +339,7 @@ if "%config%"=="Debug" set test_args=--mode=debug %test_args%
340339
if "%config%"=="Release" set test_args=--mode=release %test_args%
341340
echo running 'cctest %cctest_args%'
342341
"%config%\cctest" %cctest_args%
343-
echo running 'python tools\test.py %test_args%'
344-
python tools\test.py %test_args%
342+
call :run-python tools\test.py %test_args%
345343
goto jslint
346344

347345
:jslint
@@ -377,6 +375,14 @@ echo vcbuild.bat build-release : builds the release distribution as used by n
377375
echo vcbuild.bat enable-vtune : builds nodejs with Intel VTune profiling support to profile JavaScript
378376
goto exit
379377

378+
:run-python
379+
call tools\msvs\find_python.cmd
380+
if errorlevel 1 echo Could not find python2 & goto :exit
381+
set cmd1=%VCBUILD_PYTHON_LOCATION% %*
382+
echo %cmd1%
383+
%cmd1%
384+
exit /b %ERRORLEVEL%
385+
380386
:exit
381387
goto :EOF
382388

@@ -388,8 +394,9 @@ rem ***************
388394
set NODE_VERSION=
389395
set TAG=
390396
set FULLVERSION=
391-
392-
for /F "usebackq tokens=*" %%i in (`python "%~dp0tools\getnodeversion.py"`) do set NODE_VERSION=%%i
397+
:: Call as subroutine for validation of python
398+
call :run-python tools\getnodeversion.py > nul
399+
for /F "tokens=*" %%i in ('%VCBUILD_PYTHON_LOCATION% tools\getnodeversion.py') do set NODE_VERSION=%%i
393400
if not defined NODE_VERSION (
394401
echo Cannot determine current version of Node.js
395402
exit /b 1
@@ -398,7 +405,7 @@ if not defined NODE_VERSION (
398405
if not defined DISTTYPE set DISTTYPE=release
399406
if "%DISTTYPE%"=="release" (
400407
set FULLVERSION=%NODE_VERSION%
401-
goto exit
408+
exit /b 0
402409
)
403410
if "%DISTTYPE%"=="custom" (
404411
if not defined CUSTOMTAG (
@@ -425,7 +432,4 @@ if not "%DISTTYPE%"=="custom" (
425432
set TAG=%DISTTYPE%%DATESTRING%%COMMIT%
426433
)
427434
set FULLVERSION=%NODE_VERSION%-%TAG%
428-
429-
:exit
430-
if not defined DISTTYPEDIR set DISTTYPEDIR=%DISTTYPE%
431-
goto :EOF
435+
exit /b 0

0 commit comments

Comments
 (0)