diff --git a/build.bat b/build.bat index ddc59f97a363fc529744516aaee5659324068bc9..dafa3d7b96f15a8a944295e794d6b0e916708740 100644 --- a/build.bat +++ b/build.bat @@ -325,6 +325,51 @@ rem Locate python or python3 call %SCRIPTDIR%\locate_python3.bat if %ERRORLEVEL% NEQ 0 exit /B %ERRORLEVEL% +if !_IS_MINDSPORE_CI! == 1 ( + rem ============================================================================ + rem CI WORKAROUND for linker path-with-space issue + rem + rem The linker on some Windows CI nodes (ld.lld.exe) fails to process + rem library paths that contain spaces (e.g., Python installed in "Program Files"). + rem To work around this, we copy the required Python .lib file to a local, + rem space-free directory and then force CMake to use this local copy. + rem This logic is only active when _IS_MINDSPORE_CI is true. + rem ============================================================================ + set NEW_PYTHON_LIB_DIR=%ROOTDIR%\local_python_libs + echo Creating local directory for Python library at !NEW_PYTHON_LIB_DIR! + if not exist "!NEW_PYTHON_LIB_DIR!" md "!NEW_PYTHON_LIB_DIR!" + + echo Locating original Python library... + for /f "delims=" %%i in ('!PYTHON! -c "import sys; print(sys.prefix)"') do set PYTHON_PREFIX=%%i + for /f "delims=" %%j in ('!PYTHON! -c "import sys; print('python{0}{1}.lib'.format(sys.version_info.major, sys.version_info.minor))"') do set PYTHON_LIB_FILENAME=%%j + if not defined PYTHON_PREFIX ( + echo ERROR: Could not determine Python prefix. + exit /B 1 + ) + if not defined PYTHON_LIB_FILENAME ( + echo ERROR: Could not determine Python library filename. + exit /B 1 + ) + set PYTHON_LIB_DIR=!PYTHON_PREFIX!\libs + echo Original Python library directory is: !PYTHON_LIB_DIR! + echo Python library filename is: !PYTHON_LIB_FILENAME! + + set PYTHON_LIB_FILE=!PYTHON_LIB_DIR!\!PYTHON_LIB_FILENAME! + echo Checking for Python library file at: !PYTHON_LIB_FILE! + if not exist "!PYTHON_LIB_FILE!" ( + echo ERROR: !PYTHON_LIB_FILENAME! not found at the expected location. + exit /B 1 + ) + + echo Copying Python library to local directory... + copy /Y "!PYTHON_LIB_FILE!" "!NEW_PYTHON_LIB_DIR!\!PYTHON_LIB_FILENAME!" + if %ERRORLEVEL% NEQ 0 ( + echo ERROR: Failed to copy !PYTHON_LIB_FILENAME!. + exit /B 1 + ) + echo Successfully copied !PYTHON_LIB_FILENAME!. +) + rem ============================================================================ @@ -411,6 +456,15 @@ if NOT "!CC!" == "" set args=!args! -C--global-option=--var -C--global-option=CM if NOT "!CXX!" == "" set args=!args! -C--global-option=--var -C--global-option=CMAKE_CXX_COMPILER -C--global-option=!CXX! if NOT "!CUDACXX!" == "" set args=!args! -C--global-option=--var -C--global-option=CMAKE_CUDA_COMPILER -C--global-option=!CUDACXX! +rem Add CMake override for the local Python library, part of the CI WORKAROUND. +if !_IS_MINDSPORE_CI! == 1 ( + set OVERRIDDEN_PYTHON_LIB_PATH=!NEW_PYTHON_LIB_DIR!\!PYTHON_LIB_FILENAME! + set OVERRIDDEN_PYTHON_LIB_PATH_CMAKE=!OVERRIDDEN_PYTHON_LIB_PATH:\=/! + echo Overriding Python library to use: !OVERRIDDEN_PYTHON_LIB_PATH_CMAKE! + set args=!args! -C--global-option=--var -C--global-option=Python_LIBRARIES -C--global-option="!OVERRIDDEN_PYTHON_LIB_PATH_CMAKE!" + set args=!args! -C--global-option=--var -C--global-option=Python_LIBRARY -C--global-option="!OVERRIDDEN_PYTHON_LIB_PATH_CMAKE!" +) + rem ============================================================================ if !enable_gpu! == 1 ( diff --git a/build.ps1 b/build.ps1 index cc43265226a34be7875b7ddecc0619bb7227b04e..dbee6be172a56d2f9bfbc6121c17f433a38e3dea 100644 --- a/build.ps1 +++ b/build.ps1 @@ -198,6 +198,49 @@ $ErrorActionPreference = 'Stop' Write-Output "Called with: $($MyInvocation.Line)" +if ($global:_IS_MINDSPORE_CI) { + # ============================================================================== + # CI WORKAROUND for linker path-with-space issue + # + # The linker on some Windows CI nodes (ld.lld.exe) fails to process + # library paths that contain spaces (e.g., Python installed in "Program Files"). + # To work around this, we copy the required Python .lib file to a local, + # space-free directory and then force CMake to use this local copy. + # This logic is only active when $global:_IS_MINDSPORE_CI is true. + # ============================================================================== + $global:NEW_PYTHON_LIB_DIR = (Join-Path $ROOTDIR 'local_python_libs') + Write-Output "Creating local directory for Python library at $($global:NEW_PYTHON_LIB_DIR)" + if (-not (Test-Path $global:NEW_PYTHON_LIB_DIR)) { + New-Item -ItemType Directory -Path $global:NEW_PYTHON_LIB_DIR | Out-Null + } + + Write-Output "Locating original Python library..." + $pythonPrefix = (& "$PYTHON" -c "import sys; print(sys.prefix)").Trim() + $pythonLibFilename = (& "$PYTHON" -c "import sys; print('python{0}{1}.lib'.format(sys.version_info.major, sys.version_info.minor))").Trim() + if (-not $pythonPrefix) { + Write-Error "ERROR: Could not determine Python prefix."; exit 1 + } + if (-not $pythonLibFilename) { + Write-Error "ERROR: Could not determine Python library filename."; exit 1 + } + $pythonLibDir = (Join-Path $pythonPrefix 'libs') + Write-Output "Original Python library directory is: $pythonLibDir" + Write-Output "Python library filename is: $pythonLibFilename" + + $sourceLibFile = (Join-Path $pythonLibDir $pythonLibFilename) + $destLibFile = (Join-Path $global:NEW_PYTHON_LIB_DIR $pythonLibFilename) + Write-Output "Checking for Python library file at: $sourceLibFile" + if (-not (Test-Path $sourceLibFile)) { + Write-Error "ERROR: $pythonLibFilename not found at the expected location."; exit 1 + } + + Write-Output "Copying Python library to local directory..." + Copy-Item -Path $sourceLibFile -Destination $destLibFile -Force + Write-Output "Successfully copied $pythonLibFilename." + + $global:OVERRIDDEN_PYTHON_LIB_PATH_CMAKE = ($destLibFile).Replace('\', '/') +} + cd "$ROOTDIR" # ------------------------------------------------------------------------------ @@ -360,6 +403,13 @@ if([bool]$Env:CUDACXX) { $build_args += '--var', 'CMAKE_CUDA_COMPILER', "$Env:CUDACXX" } +# Add CMake override for the local Python library, part of the CI WORKAROUND. +if ($global:_IS_MINDSPORE_CI) { + Write-Output "Overriding Python library to use: $($global:OVERRIDDEN_PYTHON_LIB_PATH_CMAKE)" + $build_args += '--var', 'Python_LIBRARIES', "$($global:OVERRIDDEN_PYTHON_LIB_PATH_CMAKE)" + $build_args += '--var', 'Python_LIBRARY', "$($global:OVERRIDDEN_PYTHON_LIB_PATH_CMAKE)" +} + Write-Debug 'Will be passing these arguments to setup.py:' Write-Debug " $build_args"