Skip to content

Commit d8f084d

Browse files
committed
[build] Test building CUDA-enabled OpenCV (#225)
1 parent f1ae389 commit d8f084d

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# GitHub Actions Workflow: Build opencv-python with CUDA support on Windows
2+
#
3+
# This workflow compiles opencv-python from source with CUDA enabled on a
4+
# GitHub-hosted Windows runner. The resulting Python wheel is then uploaded
5+
# as a build artifact.
6+
#
7+
# This is a complex and long-running process. It is configured to run only
8+
# on manual trigger (workflow_dispatch).
9+
10+
name: Build OpenCV (Windows, CUDA)
11+
12+
on:
13+
workflow_dispatch:
14+
15+
jobs:
16+
build:
17+
runs-on: windows-latest
18+
19+
defaults:
20+
run:
21+
shell: pwsh
22+
23+
steps:
24+
- name: 📥 Checkout code
25+
uses: actions/checkout@v4
26+
27+
- name: 🏗️ Install Build Dependencies
28+
run: |
29+
echo "Installing CMake and Ninja for building..."
30+
choco install -y cmake ninja
31+
32+
- name: 🐍 Set up Python
33+
uses: actions/setup-python@v5
34+
with:
35+
python-version: '3.11'
36+
37+
- name: 📦 Install Python Build Requirements
38+
run: |
39+
python -m pip install --upgrade pip
40+
pip install numpy wheel
41+
42+
- name: 🔧 Install NVIDIA CUDA Toolkit
43+
run: |
44+
echo "Downloading CUDA Toolkit..."
45+
$cuda_installer_url = "https://developer.download.nvidia.com/compute/cuda/12.1.1/network_installers/cuda_12.1.1_windows_network.exe"
46+
# Use the runner's temp directory, which is guaranteed to exist.
47+
$installer_path = Join-Path $env:RUNNER_TEMP "cuda_installer.exe"
48+
curl -L -o $installer_path $cuda_installer_url
49+
50+
echo "Installing CUDA Toolkit silently..."
51+
# Use Start-Process for robust execution in PowerShell.
52+
# The arguments must be passed as a single string.
53+
$arguments = "-s nvcc_12.1 cudart_12.1"
54+
Start-Process -FilePath $installer_path -ArgumentList $arguments -Wait -NoNewWindow
55+
56+
echo "Adding CUDA to PATH..."
57+
$cuda_path = "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.1"
58+
echo "CUDA_PATH=$cuda_path" | Out-File -FilePath $env:GITHUB_ENV -Append
59+
echo "$cuda_path\bin" | Out-File -FilePath $env:GITHUB_PATH -Append
60+
echo "$cuda_path\lib\x64" | Out-File -FilePath $env:GITHUB_PATH -Append
61+
shell: pwsh
62+
63+
- name: Clone opencv-python repository
64+
run: |
65+
git clone --depth 1 --recursive https://github.com/opencv/opencv-python.git
66+
67+
- name: 🛠️ Build opencv-python with CUDA
68+
run: |
69+
cd opencv-python
70+
71+
# Set environment variables to configure the build.
72+
# This is the core of the customization.
73+
# - WITH_CUDA=ON: Enables CUDA support.
74+
# - CUDA_ARCH_BIN: Specifies the CUDA architectures to build for.
75+
# - CUDA_ARCH_PTX: Specifies the virtual architecture for forward compatibility.
76+
# - BUILD_TESTS=OFF: Speeds up the build by not compiling tests.
77+
# Use single quotes to avoid PowerShell parsing issues with nested quotes.
78+
$env:CMAKE_ARGS = '-D CMAKE_BUILD_TYPE=RELEASE -D WITH_CUDA=ON -D CUDA_ARCH_BIN="6.0;6.1;7.0;7.5" -D CUDA_ARCH_PTX=7.5 -D OPENCV_ENABLE_NONFREE=ON -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D BUILD_EXAMPLES=OFF'
79+
80+
# Set this to force a build from source instead of downloading a pre-built wheel.
81+
$env:PIP_NO_BINARY = ":all:"
82+
83+
# Run the build process. This will compile OpenCV and create a wheel.
84+
# The output wheel will be placed in the 'wheelhouse' directory.
85+
pip wheel . --wheel-dir ../wheelhouse
86+
87+
- name: 🧪 Verify Build
88+
run: |
89+
# Install the wheel we just built
90+
pip install ../wheelhouse/opencv_python*.whl
91+
92+
# Print OpenCV build information. We expect to see the "CUDA" section.
93+
echo "Verifying OpenCV build..."
94+
python -c "import cv2; print(cv2.getBuildInformation())"
95+
96+
- name: 📤 Upload Artifact
97+
uses: actions/upload-artifact@v4
98+
with:
99+
name: opencv-python-cuda-wheel
100+
path: wheelhouse/

0 commit comments

Comments
 (0)