Skip to content

Commit 87f051f

Browse files
HanSur94claude
andcommitted
fix: recreate MATLAB dir structure with junctions for Engine build
The Engine setup.py validates MATLAB by walking up from extern/engines/python/ and checking for bin/, toolbox/, etc. Simply copying to temp broke this validation ("corrupted"). Now recreates the expected directory tree in %TEMP% and uses mklink /J (directory junctions, no admin needed) to point bin/, toolbox/, extern/include/, extern/lib/ back to the real MATLAB installation. Junctions are removed before cleanup to prevent rd /s /q from following them into C:\Program Files. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
1 parent b5279e1 commit 87f051f

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

install.bat

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -245,23 +245,36 @@ python -c "import matlab.engine" >nul 2>&1
245245
if %errorlevel% equ 0 (
246246
echo MATLAB Engine API already installed. Skipping.
247247
) else (
248-
:: Use robocopy to copy MATLAB Engine source to a writable temp dir.
249-
:: robocopy exit codes: 0=no change, 1=files copied, 2+=extras/mismatch, 8+=error
250-
set "ENGINE_TEMP=%TEMP%\matlab_engine_build"
248+
:: The MATLAB Engine setup.py finds MATLAB by walking up the directory
249+
:: tree from extern\engines\python. We can't build in C:\Program Files
250+
:: (read-only), so we recreate the directory structure in %TEMP% and use
251+
:: directory junctions (mklink /J, no admin needed) to point back to the
252+
:: real MATLAB dirs that setup.py validates.
253+
set "ENGINE_TEMP=%TEMP%\matlab_engine_root"
251254
if exist "!ENGINE_TEMP!" rd /s /q "!ENGINE_TEMP!" >nul 2>nul
252-
echo Copying MATLAB Engine source to writable temp directory...
253-
robocopy "!ENGINE_API_DIR!" "!ENGINE_TEMP!" /E /NJH /NJS /NFL /NDL /NC /NS /NP >nul 2>nul
255+
256+
echo Preparing MATLAB Engine build directory...
257+
mkdir "!ENGINE_TEMP!\extern\engines" 2>nul
258+
259+
:: Copy only the Python engine source (the part we need to build)
260+
robocopy "!ENGINE_API_DIR!" "!ENGINE_TEMP!\extern\engines\python" /E /NJH /NJS /NFL /NDL /NC /NS /NP >nul 2>nul
254261
set "_RC=!errorlevel!"
255262
if !_RC! geq 8 (
256-
echo [WARNING] Could not copy MATLAB Engine source files (robocopy exit !_RC!^).
263+
echo [WARNING] Could not copy MATLAB Engine source files.
257264
echo Continuing with MCP server installation...
258265
goto :install_mcp
259266
)
267+
268+
:: Create directory junctions so setup.py can validate the MATLAB install.
269+
:: mklink /J does NOT require admin rights (unlike mklink /D).
270+
mklink /J "!ENGINE_TEMP!\bin" "!MATLAB_ROOT!\bin" >nul 2>nul
271+
mklink /J "!ENGINE_TEMP!\extern\include" "!MATLAB_ROOT!\extern\include" >nul 2>nul
272+
mklink /J "!ENGINE_TEMP!\extern\lib" "!MATLAB_ROOT!\extern\lib" >nul 2>nul
273+
mklink /J "!ENGINE_TEMP!\toolbox" "!MATLAB_ROOT!\toolbox" >nul 2>nul
274+
260275
echo Building and installing MATLAB Engine API...
261-
:: Set MATLABROOT so setup.py can find MATLAB (it normally walks up
262-
:: from its own directory, which breaks when copied to temp)
263-
set "MATLABROOT=!MATLAB_ROOT!"
264-
pip install "!ENGINE_TEMP!" --no-build-isolation --quiet 2>&1
276+
set "MATLABROOT=!ENGINE_TEMP!"
277+
pip install "!ENGINE_TEMP!\extern\engines\python" --no-build-isolation --quiet 2>&1
265278
if !errorlevel! neq 0 (
266279
echo.
267280
echo [WARNING] MATLAB Engine API installation failed.
@@ -276,8 +289,12 @@ if %errorlevel% equ 0 (
276289
) else (
277290
echo [OK] MATLAB Engine API installed.
278291
)
279-
:: Clean up temp copy
280-
rd /s /q "!ENGINE_TEMP!" 2>nul
292+
:: Clean up — remove junctions first (rd /s /q follows junctions!)
293+
if exist "!ENGINE_TEMP!\bin" rmdir "!ENGINE_TEMP!\bin" >nul 2>nul
294+
if exist "!ENGINE_TEMP!\extern\include" rmdir "!ENGINE_TEMP!\extern\include" >nul 2>nul
295+
if exist "!ENGINE_TEMP!\extern\lib" rmdir "!ENGINE_TEMP!\extern\lib" >nul 2>nul
296+
if exist "!ENGINE_TEMP!\toolbox" rmdir "!ENGINE_TEMP!\toolbox" >nul 2>nul
297+
rd /s /q "!ENGINE_TEMP!" >nul 2>nul
281298
)
282299
echo.
283300

0 commit comments

Comments
 (0)