-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_opencv_windows.ps1
More file actions
176 lines (151 loc) · 6.81 KB
/
build_opencv_windows.ps1
File metadata and controls
176 lines (151 loc) · 6.81 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
# OpenCV Windows Build Script
# Builds both Release and Debug versions
# With clang-cl (
# .\build_opencv_windows.ps1 -UseClang
# With MSVC
# .\build_opencv_windows.ps1
# Custom version/paths
# .\build_opencv_windows.ps1 -OpenCVVersion "4.11.0" -BuildDir "C:\opencv_build"
# Need
# choco install cmake --installargs 'ADD_CMAKE_TO_PATH=System
param(
[string]$OpenCVVersion = "4.11.0",
[string]$BuildDir = "$env:USERPROFILE\opencv_build",
[string]$InstallDir = "$env:USERPROFILE\opencv_build\install",
[switch]$UseClang = $false # Use MSVC by default
)
# Ensure we stop on errors
$ErrorActionPreference = "Stop"
Write-Host "Building OpenCV $OpenCVVersion for Windows..." -ForegroundColor Green
# Create and enter build directory
New-Item -ItemType Directory -Force -Path $BuildDir | Out-Null
Set-Location $BuildDir
# Download sources if they don't exist
$OpenCVDir = "opencv-$OpenCVVersion"
$ContribDir = "opencv_contrib-$OpenCVVersion"
if (-not (Test-Path $OpenCVDir)) {
Write-Host "Downloading OpenCV $OpenCVVersion..." -ForegroundColor Yellow
$OpenCVUrl = "https://github.com/opencv/opencv/archive/refs/tags/$OpenCVVersion.tar.gz"
Invoke-WebRequest -Uri $OpenCVUrl -OutFile "opencv-$OpenCVVersion.tar.gz"
tar -xzf "opencv-$OpenCVVersion.tar.gz"
}
if (-not (Test-Path $ContribDir)) {
Write-Host "Downloading OpenCV Contrib $OpenCVVersion..." -ForegroundColor Yellow
$ContribUrl = "https://github.com/opencv/opencv_contrib/archive/refs/tags/$OpenCVVersion.tar.gz"
Invoke-WebRequest -Uri $ContribUrl -OutFile "opencv_contrib-$OpenCVVersion.tar.gz"
tar -xzf "opencv_contrib-$OpenCVVersion.tar.gz"
}
# Function to build for specific configuration
function Build-OpenCV {
param([string]$BuildType, [string]$BuildSuffix)
$BuildPath = "build_$BuildSuffix"
$InstallPath = "$InstallDir\$BuildSuffix"
Write-Host "Configuring CMake for $BuildType build..." -ForegroundColor Green
New-Item -ItemType Directory -Force -Path $BuildPath | Out-Null
Set-Location $BuildPath
# Base CMake arguments
$CMakeArgs = @(
"-DCMAKE_BUILD_TYPE=$BuildType"
"-DBUILD_LIST=calib3d,core,features2d,highgui,imgcodecs,imgproc,video,videoio,aruco,ml,stitching"
"-DOPENCV_EXTRA_MODULES_PATH=`"$BuildDir\$ContribDir\modules`""
"-DWITH_FFMPEG=ON"
"-DWITH_QT=ON"
"-DWITH_GTK=OFF"
"-DBUILD_SHARED_LIBS=ON"
"-DCMAKE_INSTALL_PREFIX=`"$InstallPath`""
"-DOPENCV_GENERATE_PKGCONFIG=OFF" # Disable pkg-config on Windows
"-DBUILD_TESTS=OFF"
"-DBUILD_PERF_TESTS=OFF"
"-DBUILD_EXAMPLES=OFF"
"-DWITH_OPENMP=ON"
"-DWITH_TBB=ON"
# Windows-specific optimizations
"-DCMAKE_CXX_FLAGS_RELEASE=`"/O2 /DNDEBUG`""
"-DCMAKE_C_FLAGS_RELEASE=`"/O2 /DNDEBUG`""
)
# Add compiler-specific flags
if ($UseClang) {
Write-Host "Using clang-cl compiler..." -ForegroundColor Cyan
$CMakeArgs += @(
"-DCMAKE_C_COMPILER=clang-cl"
"-DCMAKE_CXX_COMPILER=clang-cl"
"-DCMAKE_C_FLAGS=`"/std:c17`""
"-DCMAKE_CXX_FLAGS=`"/std:c++20`""
)
} else {
Write-Host "Using MSVC compiler..." -ForegroundColor Cyan
$CMakeArgs += @(
"-DCMAKE_C_FLAGS=`"/std:c17`""
"-DCMAKE_CXX_FLAGS=`"/std:c++20`""
)
}
# Add source directory
$CMakeArgs += "`"$BuildDir\$OpenCVDir`""
# Run CMake
& cmake @CMakeArgs
if ($LASTEXITCODE -ne 0) { throw "CMake configuration failed" }
Write-Host "Building OpenCV $BuildType..." -ForegroundColor Green
& cmake --build . --config $BuildType --parallel
if ($LASTEXITCODE -ne 0) { throw "Build failed" }
Write-Host "Installing OpenCV $BuildType..." -ForegroundColor Green
& cmake --install . --config $BuildType
if ($LASTEXITCODE -ne 0) { throw "Install failed" }
Set-Location $BuildDir
}
try {
# Build Release version
Build-OpenCV -BuildType "Release" -BuildSuffix "release"
# Build Debug version
Build-OpenCV -BuildType "Debug" -BuildSuffix "debug"
Write-Host ""
Write-Host "Build completed successfully!" -ForegroundColor Green
Write-Host "Release libraries: $InstallDir\release\lib" -ForegroundColor Cyan
Write-Host "Debug libraries: $InstallDir\debug\lib" -ForegroundColor Cyan
Write-Host "Headers: $InstallDir\release\include (same for both)" -ForegroundColor Cyan
# List the built libraries (check if directories exist first)
Write-Host ""
if (Test-Path "$InstallDir\release\lib") {
Write-Host "Release libraries:" -ForegroundColor Yellow
$releaseLibs = Get-ChildItem "$InstallDir\release\lib\opencv_*.lib" -ErrorAction SilentlyContinue
if ($releaseLibs) {
$releaseLibs | ForEach-Object { Write-Host " $($_.Name)" }
} else {
Write-Host " No .lib files found, checking for DLLs..." -ForegroundColor Gray
Get-ChildItem "$InstallDir\release\lib\*.dll" -ErrorAction SilentlyContinue | ForEach-Object { Write-Host " $($_.Name)" }
}
} else {
Write-Host "Release lib directory not found. Checking alternate locations..." -ForegroundColor Yellow
# Check x64 subfolder (common on Windows)
if (Test-Path "$InstallDir\release\x64") {
Write-Host "Found x64 directory structure" -ForegroundColor Gray
Get-ChildItem "$InstallDir\release\x64" -Recurse -Include "*.lib","*.dll" | ForEach-Object { Write-Host " $($_.FullName)" }
}
}
Write-Host ""
if (Test-Path "$InstallDir\debug\lib") {
Write-Host "Debug libraries:" -ForegroundColor Yellow
$debugLibs = Get-ChildItem "$InstallDir\debug\lib\opencv_*d.lib" -ErrorAction SilentlyContinue
if ($debugLibs) {
$debugLibs | ForEach-Object { Write-Host " $($_.Name)" }
} else {
Write-Host " No debug .lib files found, checking for DLLs..." -ForegroundColor Gray
Get-ChildItem "$InstallDir\debug\lib\*d.dll" -ErrorAction SilentlyContinue | ForEach-Object { Write-Host " $($_.Name)" }
}
} else {
Write-Host "Debug lib directory not found. Checking alternate locations..." -ForegroundColor Yellow
if (Test-Path "$InstallDir\debug\x64") {
Write-Host "Found debug x64 directory structure" -ForegroundColor Gray
Get-ChildItem "$InstallDir\debug\x64" -Recurse -Include "*d.lib","*d.dll" | ForEach-Object { Write-Host " $($_.FullName)" }
}
}
# Show what was actually created
Write-Host ""
Write-Host "Actual directory structure:" -ForegroundColor Cyan
if (Test-Path "$InstallDir\release") {
Write-Host "Release install contents:" -ForegroundColor Gray
Get-ChildItem "$InstallDir\release" | ForEach-Object { Write-Host " $($_.Name)" }
}
} catch {
Write-Error "Build failed: $_"
exit 1
}