forked from microsoft/mssql-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.bat
More file actions
182 lines (155 loc) · 6.61 KB
/
build.bat
File metadata and controls
182 lines (155 loc) · 6.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
@echo off
setlocal enabledelayedexpansion
REM Usage: build.bat [ARCH], If ARCH is not specified, it defaults to x64.
set ARCH=%1
if "%ARCH%"=="" set ARCH=x64
echo [DIAGNOSTIC] Target Architecture set to: %ARCH%
REM Clean up main build directory if it exists
echo Checking for main build directory...
if exist "build" (
echo Removing existing build directory...
rd /s /q build
echo Build directory removed.
)
REM Get Python version from active interpreter
for /f %%v in ('python -c "import sys; print(f'{sys.version_info.major}{sys.version_info.minor}')"') do set PYTAG=%%v
echo ===================================
echo Building for: %ARCH% / Python %PYTAG%
echo ===================================
REM Save absolute source directory
set SOURCE_DIR=%~dp0
REM Go to build output directory
set BUILD_DIR=%SOURCE_DIR%build\%ARCH%\py%PYTAG%
if exist "%BUILD_DIR%" rd /s /q "%BUILD_DIR%"
mkdir "%BUILD_DIR%"
cd /d "%BUILD_DIR%"
echo [DIAGNOSTIC] Changed to build directory: "%BUILD_DIR%"
REM Set CMake platform name
set PLATFORM_NAME=%ARCH%
echo [DIAGNOSTIC] Setting up for architecture: %ARCH%
REM Set CMake platform name
if "%ARCH%"=="x64" (
set PLATFORM_NAME=x64
echo [DIAGNOSTIC] Using platform name: x64
) else if "%ARCH%"=="x86" (
set PLATFORM_NAME=Win32
echo [DIAGNOSTIC] Using platform name: Win32
) else if "%ARCH%"=="arm64" (
set PLATFORM_NAME=ARM64
echo [DIAGNOSTIC] Using platform name: ARM64
) else (
echo [ERROR] Invalid architecture: %ARCH%
exit /b 1
)
echo [DIAGNOSTIC] Source directory: "%SOURCE_DIR%"
REM Check for Visual Studio - look in standard paths or check if vswhere is available
echo [DIAGNOSTIC] Searching for Visual Studio installation...
set VS_PATH=
REM Try direct paths first (most common locations)
for %%p in (
"%ProgramFiles(x86)%\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvarsall.bat"
"%ProgramFiles(x86)%\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\Build\vcvarsall.bat"
"%ProgramFiles(x86)%\Microsoft Visual Studio\2022\Professional\VC\Auxiliary\Build\vcvarsall.bat"
"%ProgramFiles(x86)%\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvarsall.bat"
"%ProgramFiles%\Microsoft Visual Studio\2022\Enterprise\VC\Auxiliary\Build\vcvarsall.bat"
"%ProgramFiles%\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\Build\vcvarsall.bat"
) do (
echo [DIAGNOSTIC] Checking path: %%p
if exist %%p (
set VS_PATH=%%p
echo [SUCCESS] Found Visual Studio at: %%p
goto vs_found
)
)
REM If we reach here, we didn't find Visual Studio in the standard paths
echo [DIAGNOSTIC] Visual Studio not found in standard paths
echo [DIAGNOSTIC] Looking for vswhere.exe...
REM Try using vswhere if available (common on Azure DevOps agents)
set VSWHERE_PATH="%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe"
if exist %VSWHERE_PATH% (
echo [DIAGNOSTIC] Found vswhere at: %VSWHERE_PATH%
for /f "usebackq tokens=*" %%i in (`%VSWHERE_PATH% -latest -products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationPath`) do (
set VS_DIR=%%i
if exist "%%i\VC\Auxiliary\Build\vcvarsall.bat" (
set VS_PATH="%%i\VC\Auxiliary\Build\vcvarsall.bat"
echo [SUCCESS] Found Visual Studio using vswhere at: %%i\VC\Auxiliary\Build\vcvarsall.bat
goto vs_found
)
)
)
echo [WARNING] Visual Studio not found in standard paths or using vswhere
echo [DIAGNOSTIC] Current directory structure:
dir "%ProgramFiles(x86)%\Microsoft Visual Studio" /s /b | findstr "vcvarsall.bat"
dir "%ProgramFiles%\Microsoft Visual Studio" /s /b | findstr "vcvarsall.bat"
echo [ERROR] Could not find Visual Studio installation
exit /b 1
:vs_found
REM Initialize MSVC toolchain
echo [DIAGNOSTIC] Initializing MSVC toolchain with: call %VS_PATH% %ARCH%
call %VS_PATH% %ARCH%
echo [DIAGNOSTIC] MSVC initialization exit code: %errorlevel%
if errorlevel 1 (
echo [ERROR] Failed to initialize MSVC toolchain
exit /b 1
)
REM Now invoke CMake with correct source path (options first, path last!)
echo [DIAGNOSTIC] Running CMake configure with: cmake -A %PLATFORM_NAME% -DARCHITECTURE=%ARCH% "%SOURCE_DIR:~0,-1%"
cmake -A %PLATFORM_NAME% -DARCHITECTURE=%ARCH% "%SOURCE_DIR:~0,-1%"
echo [DIAGNOSTIC] CMake configure exit code: %errorlevel%
if errorlevel 1 (
echo [ERROR] CMake configuration failed
exit /b 1
)
echo [DIAGNOSTIC] Running CMake build with: cmake --build . --config Release
cmake --build . --config Release
echo [DIAGNOSTIC] CMake build exit code: %errorlevel%
if errorlevel 1 (
echo [ERROR] CMake build failed
exit /b 1
)
echo [SUCCESS] Build completed successfully.
echo ===== Build completed for %ARCH% Python %PYTAG% ======
@REM REM Call the external script to preserve only the target architecture odbc libs
@REM REM This is commented out to avoid running it automatically. Uncomment if needed.
@REM call "%SOURCE_DIR%keep_single_arch.bat" "%ARCH%"
REM Copy the built .pyd file to source directory
set WHEEL_ARCH=%ARCH%
if "%WHEEL_ARCH%"=="x64" set WHEEL_ARCH=amd64
if "%WHEEL_ARCH%"=="arm64" set WHEEL_ARCH=arm64
if "%WHEEL_ARCH%"=="x86" set WHEEL_ARCH=win32
set PYD_NAME=ddbc_bindings.cp%PYTAG%-%WHEEL_ARCH%.pyd
set OUTPUT_DIR=%BUILD_DIR%\Release
if exist "%OUTPUT_DIR%\%PYD_NAME%" (
copy /Y "%OUTPUT_DIR%\%PYD_NAME%" "%SOURCE_DIR%\.."
echo [SUCCESS] Copied %PYD_NAME% to %SOURCE_DIR%\..
echo [DIAGNOSTIC] Copying PDB file if it exists...
set PDB_NAME=ddbc_bindings.cp%PYTAG%-%WHEEL_ARCH%.pdb
echo [DEBUG] Computed PDB_NAME: !PDB_NAME!
if exist "%OUTPUT_DIR%\!PDB_NAME!" (
echo [DIAGNOSTIC] Found PDB file: "!PDB_NAME!"
echo [DIAGNOSTIC] Copying PDB file to source directory...
copy /Y "%OUTPUT_DIR%\!PDB_NAME!" "%SOURCE_DIR%\.."
echo [SUCCESS] Copied !PDB_NAME! to %SOURCE_DIR%..
) else (
echo [WARNING] PDB file !PDB_NAME! not found in output directory.
)
setlocal enabledelayedexpansion
for %%I in ("%SOURCE_DIR%..") do (
set PARENT_DIR=%%~fI
)
echo [DIAGNOSTIC] Parent is: !PARENT_DIR!
set VCREDIST_DLL_PATH=!PARENT_DIR!\libs\windows\!ARCH!\vcredist\msvcp140.dll
echo [DIAGNOSTIC] Looking for msvcp140.dll at "!VCREDIST_DLL_PATH!"
if exist "!VCREDIST_DLL_PATH!" (
copy /Y "!VCREDIST_DLL_PATH!" "%SOURCE_DIR%\.."
echo [SUCCESS] Copied msvcp140.dll from !VCREDIST_DLL_PATH! to "%SOURCE_DIR%\.."
) else (
echo [ERROR] Could not find msvcp140.dll at "!VCREDIST_DLL_PATH!"
exit /b 1
)
) else (
echo [ERROR] Could not find built .pyd file: %PYD_NAME%
REM Exit with an error code here if the .pyd file is not found
exit /b 1
)
endlocal