Skip to content

Commit 003e5f9

Browse files
authored
Merge branch 'main' into bewithgaurav/bcp-cross-repo-ci
2 parents 0f3042a + 63376fd commit 003e5f9

File tree

4 files changed

+2089
-0
lines changed

4 files changed

+2089
-0
lines changed
Lines changed: 312 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,312 @@
1+
---
2+
description: "Build C++ pybind11 extension (ddbc_bindings)"
3+
name: "mssql-python-build"
4+
agent: 'agent'
5+
model: Claude Sonnet 4.5 (copilot)
6+
---
7+
# Build DDBC Extensions Prompt for microsoft/mssql-python
8+
9+
You are a development assistant helping rebuild the DDBC C++ pybind11 extensions for the mssql-python driver.
10+
11+
## PREREQUISITES
12+
13+
> ⚠️ **This prompt assumes your development environment is already set up.**
14+
> If you haven't set up your environment yet, use `#setup-dev-env` first.
15+
16+
**Quick sanity check:**
17+
```bash
18+
# Verify venv is active
19+
if [ -z "$VIRTUAL_ENV" ]; then
20+
echo "❌ No virtual environment active. Run: source myvenv/bin/activate"
21+
exit 1
22+
fi
23+
24+
# Verify pybind11 is installed
25+
python -c "import pybind11; print('✅ Ready to build with Python', __import__('sys').version.split()[0])"
26+
```
27+
28+
**Important:** The C++ extension will be built for the active Python version. Make sure you're using the same venv and Python version you'll use to run the code.
29+
30+
---
31+
32+
## TASK
33+
34+
Help the developer rebuild the DDBC bindings after making C++ code changes. Follow this process sequentially.
35+
36+
---
37+
38+
## STEP 0: Understand What You're Building
39+
40+
### What Are DDBC Bindings?
41+
42+
The `ddbc_bindings` module is a **C++ pybind11 extension** that provides:
43+
- Low-level ODBC connectivity to SQL Server
44+
- High-performance database operations
45+
- Platform-specific optimizations
46+
47+
### When to Rebuild
48+
49+
- ✅ After modifying any `.cpp` or `.h` files in `mssql_python/pybind/`
50+
- ✅ After changing `CMakeLists.txt`
51+
- ✅ After upgrading Python version
52+
- ✅ After pulling changes that include C++ modifications
53+
- ❌ After Python-only changes (no rebuild needed)
54+
55+
### Key Files
56+
57+
```
58+
mssql_python/pybind/
59+
├── ddbc_bindings.cpp # Main bindings implementation
60+
├── ddbc_bindings.h # Header file
61+
├── logger_bridge.cpp # Python logging bridge
62+
├── logger_bridge.hpp # Logger header
63+
├── connection/ # Connection implementation
64+
│ ├── connection.cpp
65+
│ ├── connection.h
66+
│ ├── connection_pool.cpp
67+
│ └── connection_pool.h
68+
├── CMakeLists.txt # CMake build configuration
69+
├── build.sh # macOS/Linux build script
70+
└── build.bat # Windows build script
71+
```
72+
73+
---
74+
75+
## STEP 1: Build the Extension
76+
77+
### 1.1 Run Build Script
78+
79+
**Important:** The commands below will automatically return to the repository root after building.
80+
81+
#### macOS / Linux
82+
83+
```bash
84+
# Standard build
85+
cd mssql_python/pybind && ./build.sh && cd ../..
86+
87+
# Build with code coverage instrumentation (Linux only)
88+
cd mssql_python/pybind && ./build.sh codecov && cd ../..
89+
```
90+
91+
#### Windows (in Developer Command Prompt)
92+
93+
```cmd
94+
cd mssql_python\pybind && build.bat && cd ..\..
95+
```
96+
97+
### 1.2 What the Build Does
98+
99+
1. **Cleans** existing `build/` directory
100+
2. **Detects** Python version and architecture
101+
3. **Configures** CMake with correct paths
102+
4. **Compiles** C++ code to platform-specific extension
103+
5. **Copies** the built extension to `mssql_python/` directory
104+
6. **Signs** the extension (macOS only - for SIP compliance)
105+
7. **Returns** to repository root directory
106+
107+
**Output files by platform:**
108+
| Platform | Output File |
109+
|----------|-------------|
110+
| macOS | `ddbc_bindings.cp{version}-universal2.so` |
111+
| Linux | `ddbc_bindings.cp{version}-{arch}.so` |
112+
| Windows | `ddbc_bindings.cp{version}-{arch}.pyd` |
113+
114+
---
115+
116+
## STEP 2: Verify the Build
117+
118+
**These commands assume you're at the repository root** (which you should be after Step 1).
119+
120+
### 2.1 Check Output File Exists
121+
122+
```bash
123+
# macOS/Linux
124+
ls -la mssql_python/ddbc_bindings.*.so
125+
126+
# Windows
127+
dir mssql_python\ddbc_bindings.*.pyd
128+
```
129+
130+
### 2.2 Verify Import Works
131+
132+
```bash
133+
python -c "from mssql_python import connect; print('✅ Import successful')"
134+
```
135+
136+
---
137+
138+
## STEP 3: Clean Build (If Needed)
139+
140+
If you need a completely fresh build:
141+
142+
```bash
143+
# From repository root
144+
rm -rf mssql_python/pybind/build/
145+
rm -f mssql_python/ddbc_bindings.*.so
146+
rm -f mssql_python/ddbc_bindings.*.pyd
147+
148+
# Rebuild
149+
cd mssql_python/pybind
150+
./build.sh # or build.bat on Windows
151+
```
152+
153+
---
154+
155+
## Troubleshooting
156+
157+
### ❌ "CMake configuration failed"
158+
159+
**Cause:** CMake can't find Python or pybind11 paths
160+
161+
**Fix:**
162+
```bash
163+
# Verify Python include directory exists
164+
python -c "import sysconfig; print(sysconfig.get_path('include'))"
165+
ls $(python -c "import sysconfig; print(sysconfig.get_path('include'))")
166+
167+
# Verify pybind11 include directory exists
168+
python -c "import pybind11; print(pybind11.get_include())"
169+
ls $(python -c "import pybind11; print(pybind11.get_include())")
170+
```
171+
172+
If pybind11 path doesn't exist, run: `pip install pybind11`
173+
174+
### ❌ "pybind11 not found" during build
175+
176+
**Cause:** pybind11 not installed in active venv
177+
178+
**Fix:**
179+
```bash
180+
# Ensure venv is active
181+
source myvenv/bin/activate # adjust path if needed
182+
183+
# Install pybind11
184+
pip install pybind11
185+
186+
# Verify
187+
python -c "import pybind11; print(pybind11.get_include())"
188+
```
189+
190+
### ❌ "sql.h not found" (macOS)
191+
192+
**Cause:** ODBC development headers not installed
193+
194+
**Fix:**
195+
```bash
196+
# Install Microsoft ODBC Driver (provides headers)
197+
brew tap microsoft/mssql-release https://github.com/Microsoft/homebrew-mssql-release
198+
ACCEPT_EULA=Y brew install msodbcsql18
199+
200+
# Or specify custom path
201+
export ODBC_INCLUDE_DIR=/path/to/odbc/headers
202+
./build.sh
203+
```
204+
205+
### ❌ "undefined symbol" errors at runtime
206+
207+
**Cause:** Built with different Python than you're running
208+
209+
**Fix:**
210+
```bash
211+
# Check which Python was used to build (look at output filename)
212+
ls mssql_python/ddbc_bindings.*.so
213+
# e.g., ddbc_bindings.cp313-universal2.so means Python 3.13
214+
215+
# Check current Python
216+
python --version
217+
218+
# If mismatch, rebuild with correct Python
219+
rm -rf mssql_python/pybind/build/
220+
cd mssql_python/pybind
221+
./build.sh
222+
```
223+
224+
### ❌ "cmake is not recognized" (Windows)
225+
226+
**Cause:** Not using Developer Command Prompt
227+
228+
**Fix:**
229+
1. Close current terminal
230+
2. Open **Start Menu** → search "Developer Command Prompt for VS 2022"
231+
3. Navigate to project: `cd C:\path\to\mssql-python\mssql_python\pybind`
232+
4. Run: `build.bat`
233+
234+
### ❌ "codesign failed" (macOS)
235+
236+
**Cause:** macOS SIP (System Integrity Protection) issues
237+
238+
**Fix:** The build script handles this automatically. If issues persist:
239+
```bash
240+
codesign -s - -f mssql_python/ddbc_bindings.*.so
241+
```
242+
243+
### ❌ Build succeeds but import fails
244+
245+
**Cause:** Usually path issues or old cached files
246+
247+
**Fix:**
248+
```bash
249+
# Clear Python cache
250+
find . -type d -name __pycache__ -exec rm -rf {} + 2>/dev/null
251+
252+
# Clear any .pyc files
253+
find . -name "*.pyc" -delete
254+
255+
# Reinstall in dev mode
256+
pip install -e .
257+
258+
# Try import again
259+
python -c "from mssql_python import connect; print('✅ OK')"
260+
```
261+
262+
### ❌ "Permission denied" running build.sh
263+
264+
**Fix:**
265+
```bash
266+
chmod +x mssql_python/pybind/build.sh
267+
./build.sh
268+
```
269+
270+
### ❌ Build takes too long / seems stuck
271+
272+
**Cause:** Universal binary build on macOS compiles for both architectures
273+
274+
**Info:** This is normal. macOS builds for both arm64 and x86_64. First build takes longer, subsequent builds use cache.
275+
276+
**If truly stuck (>10 minutes):**
277+
```bash
278+
# Cancel with Ctrl+C, then clean and retry
279+
rm -rf build/
280+
./build.sh
281+
```
282+
283+
---
284+
285+
## Quick Reference
286+
287+
### One-Liner Build Commands
288+
289+
```bash
290+
# macOS/Linux - Full rebuild from repo root
291+
cd mssql_python/pybind && rm -rf build && ./build.sh && cd ../.. && python -c "from mssql_python import connect; print('✅ Build successful')"
292+
```
293+
294+
### Build Output Naming Convention
295+
296+
| Platform | Python | Architecture | Output File |
297+
|----------|--------|--------------|-------------|
298+
| macOS | 3.13 | Universal | `ddbc_bindings.cp313-universal2.so` |
299+
| Linux | 3.12 | x86_64 | `ddbc_bindings.cp312-x86_64.so` |
300+
| Linux | 3.11 | ARM64 | `ddbc_bindings.cp311-arm64.so` |
301+
| Windows | 3.13 | x64 | `ddbc_bindings.cp313-amd64.pyd` |
302+
| Windows | 3.12 | ARM64 | `ddbc_bindings.cp312-arm64.pyd` |
303+
304+
---
305+
306+
## After Building
307+
308+
Once the build succeeds:
309+
310+
1. **Run tests** → Use `#run-tests`
311+
2. **Test manually** with a connection to SQL Server
312+
3. **Create a PR** with your C++ changes → Use `#create-pr`

0 commit comments

Comments
 (0)