forked from ffmpeginteropx/FFmpegInteropX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuild-FFmpeg.ps1
More file actions
389 lines (310 loc) · 12.4 KB
/
Build-FFmpeg.ps1
File metadata and controls
389 lines (310 loc) · 12.4 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
param(
[ValidateSet('x86', 'x64', 'ARM', 'ARM64')]
[string[]] $Platforms = ('x86', 'x64', 'ARM', 'ARM64'),
<#
Example values:
14.1
14.2
14.16
14.16.27023
14.23.27820
Note. The PlatformToolset will be inferred from this value ('v141', 'v142'...)
#>
[version] $VcVersion = '14.2',
[ValidateSet('UWP', 'Desktop')]
[string] $WindowsTarget = 'UWP',
<#
Example values:
8.1
10.0.15063.0
10.0.17763.0
10.0.18362.0
#>
[version] $WindowsTargetPlatformVersion = '10.0.18362.0',
[ValidateSet('Debug', 'Release')]
[string] $Configuration = 'Release',
# shared: create shared dll - static: create lib for static linking
[ValidateSet('shared', 'static')]
[string] $SharedOrStatic = 'shared',
[System.IO.DirectoryInfo] $VSInstallerFolder = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer",
# Set the search criteria for VSWHERE.EXE.
[string[]] $VsWhereCriteria = '-latest',
[System.IO.FileInfo] $BashExe = 'C:\msys64\usr\bin\bash.exe',
[switch] $ClearBuildFolders,
# If a version string is specified, a NuGet package will be created.
[string] $NugetPackageVersion = $null,
# FFmpeg NuGet settings
[string] $FFmpegUrl = 'https://git.ffmpeg.org/ffmpeg.git',
[string] $FFmpegCommit = $(git --git-dir Libs/ffmpeg/.git rev-parse HEAD)
)
function Build-Platform {
param (
[System.IO.DirectoryInfo] $SolutionDir,
[string] $Platform,
[string] $Configuration,
[version] $WindowsTargetPlatformVersion,
[version] $VcVersion,
[string] $PlatformToolset,
[string] $VsLatestPath,
[string] $BashExe = 'C:\msys64\usr\bin\bash.exe'
)
$PSBoundParameters | Out-String
$hostArch = ( 'x86', 'x64' )[ [System.Environment]::Is64BitOperatingSystem ]
$targetArch = $Platform.ToLower()
Write-Host
Write-Host "Building FFmpeg for Windows 10 ($WindowsTarget) ${Platform}..."
Write-Host
# Load environment from VCVARS.
Import-Module "$VsLatestPath\Common7\Tools\Microsoft.VisualStudio.DevShell.dll"
Enter-VsDevShell `
-VsInstallPath $VsLatestPath `
-StartInPath "$PWD" `
-DevCmdArguments "-arch=$targetArch -host_arch=$hostArch -winsdk=$WindowsTargetPlatformVersion -vcvars_ver=$VcVersion -app_platform=$WindowsTarget"
# Build pkg-config fake
MSBuild.exe $SolutionDir\Libs\PkgConfigFake\PkgConfigFake.csproj `
/p:OutputPath="$SolutionDir\Intermediate\" `
/p:Configuration=$Configuration `
/p:Platform=${Env:\PreferredToolArchitecture}
if ($lastexitcode -ne 0) { throw "Failed to build PkgConfigFake." }
New-Item -ItemType Directory -Force $SolutionDir\Intermediate\FFmpeg$WindowsTarget\$Platform -OutVariable build
New-Item -ItemType Directory -Force $SolutionDir\Output\FFmpeg$WindowsTarget\$Platform -OutVariable target
if ($ClearBuildFolders) {
# Clean platform-specific build and output dirs.
Remove-Item -Force -Recurse $build\*
Remove-Item -Force -Recurse $target\*
}
('lib', 'licenses', 'include') | ForEach-Object {
New-Item -ItemType Directory -Force $build\$_
New-Item -ItemType Directory -Force $target\$_
}
$env:LIB += ";$build\lib"
$env:INCLUDE += ";$build\include"
$env:Path += ";$SolutionDir\Libs\gas-preprocessor"
# library definitions: <FolderName>, <ProjectName>, <FFmpegTargetName>
$libdefs = @(
@('zlib', 'libzlib', 'zlib'),
@('bzip2', 'libbz2', 'bz2'),
@('libiconv', 'libiconv', 'iconv'),
@('liblzma', 'liblzma', 'lzma'),
@('libxml2', 'libxml2', 'libxml2')
)
# Build all libraries
$libdefs | ForEach-Object {
$folder = $_[0]
$project = $_[1]
Write-Host
Write-Host "Building Library ${folder}..."
Write-Host
# Decide vcvars target string based on target platform
if ($WindowsTarget -eq "UWP") {
$configurationName = "${Configuration}WinRT"
$targetName = "${project}_winrt"
}
else {
$configurationName = ${Configuration}
$targetName = ${project}
}
$intDir = "$build\int\$project\"
$outDir = "$build\$project\"
MSBuild.exe $SolutionDir\Libs\$folder\SMP\$project.vcxproj `
/p:OutDir=$outDir `
/p:IntDir=$intDir `
/p:Configuration=$configurationName `
/p:Platform=$Platform `
/p:WindowsTargetPlatformVersion=$WindowsTargetPlatformVersion `
/p:PlatformToolset=$PlatformToolset `
/p:ForceImportBeforeCppTargets=$SolutionDir\Libs\LibOverrides.props `
/p:useenv=true
if ($lastexitcode -ne 0) { throw "Failed to build library $project." }
Copy-Item $build\$project\include\* $build\include\ -Recurse -Force
Copy-Item $build\$project\licenses\* $build\licenses\ -Recurse -Force
Copy-Item $build\$project\lib\$Platform\$targetName.lib $build\lib\ -Force
Copy-Item $build\$project\lib\$Platform\$targetName.pdb $build\lib\ -Force
}
# Rename all libraries to ffmpeg target names
$libdefs | ForEach-Object {
$project = $_[1];
$ffmpegTarget = $_[2];
$targetName = if ($WindowsTarget -eq "UWP") { "${project}_winrt" } else { $project }
Move-Item $build\lib\$targetName.lib $build\lib\$ffmpegTarget.lib -Force
Move-Item $build\lib\$targetName.pdb $build\lib\$ffmpegTarget.pdb -Force
}
# Fixup libxml2 includes for ffmpeg build
Copy-Item $build\include\libxml2\libxml $build\include\ -Force -Recurse
if ($WindowsTarget -eq "Desktop") {
$env:Path += ";$(Split-Path $BashExe)"
#Build x265
Write-Host ""
Write-Host "Building Library x265..."
Write-Host ""
$cmakePlatforms = @{
'x86' = 'Win32'
'x64' = 'x64'
'ARM' = 'ARM'
'ARM64' = 'ARM64'
}
$cmakePlatform = $cmakePlatforms[$Platform]
New-Item -ItemType Directory -Force $build\int\x265
$ErrorActionPreference = "Continue"
cmd.exe /C $SolutionDir\Libs\build-x265.bat $SolutionDir\Libs\x265\source $build\int\x265 $cmakePlatform $PlatformToolset
$ErrorActionPreference = "Stop"
if ($lastexitcode -ne 0) { throw "Failed to build library x264." }
Copy-Item $build\int\x265\x265-static.lib $build\lib\x265.lib -Force
Copy-Item $build\int\x265\include\* $build\include\ -Force
Copy-Item $SolutionDir\Libs\x265\COPYING $build\licenses\x265.txt -Force
#Build x264
Write-Host ""
Write-Host "Building Library x264..."
Write-Host ""
$x264Archs = @{
'x86' = 'x86'
'x64' = 'x86_64'
'ARM' = 'arm'
'ARM64' = 'aarch64'
}
$x264Arch = $x264Archs[$Platform]
New-Item -ItemType Directory -Force $build\x264
$ErrorActionPreference = "Continue"
& $BashExe --login -c "cd \$build\x264 && CC=cl ..\..\..\..\Libs\x264\configure --host=${x264Arch}-mingw64 --prefix=\$build --disable-cli --enable-static && make -j8 && make install".Replace("\", "/").Replace(":", "")
$ErrorActionPreference = "Stop"
if ($lastexitcode -ne 0) { throw "Failed to build library x264." }
#Build libvpx
Write-Host
Write-Host "Building Library libvpx..."
Write-Host
$vpxArchs = @{
'x86' = 'x86'
'x64' = 'x86_64'
'ARM' = 'armv7'
'ARM64' = 'arm64'
}
$vpxPlatforms = @{
'x86' = 'win32'
'x64' = 'win64'
'ARM' = 'win32'
'ARM64' = 'win64'
}
$vpxArch = $vpxArchs[$Platform]
$vpxPlatform = $vpxPlatforms[$Platform]
New-Item -ItemType Directory -Force $build\libvpx
$ErrorActionPreference = "Continue"
& $BashExe --login -c "cd \$build\libvpx && ..\..\..\..\Libs\libvpx\configure --target=${vpxArch}-${vpxPlatform}-vs15 --prefix=\$build --enable-static --disable-thumb --disable-debug --disable-examples --disable-tools --disable-docs --disable-unit_tests && make -j8 && make install".Replace("\", "/").Replace(":", "")
$ErrorActionPreference = "Stop"
if ($lastexitcode -ne 0) { throw "Failed to build library libvpx." }
Move-Item $build\lib\$cmakePlatform\vpxmd.lib $build\lib\vpx.lib -Force
Remove-Item $build\lib\$cmakePlatform -Force -Recurse
}
# Build ffmpeg
Write-Host
Write-Host "Building FFmpeg..."
Write-Host
$ErrorActionPreference = "Continue"
& $BashExe --login -x $SolutionDir\FFmpegConfig.sh $WindowsTarget $Platform $SharedOrStatic
$ErrorActionPreference = "Stop"
if ($lastexitcode -ne 0) { throw "Failed to build FFmpeg." }
# Copy PDBs to built binaries dir
Get-ChildItem -Recurse -Include '*.pdb' $build\int\ffmpeg\ | Copy-Item -Destination $target\bin\ -Force
# Copy license files
Copy-Item $SolutionDir\Libs\FFmpeg\COPYING.LGPLv2.1 $target\licenses\ffmpeg.txt -Force
Copy-Item $build\licenses\* $target\licenses\ -Force
}
Write-Host
Write-Host "Building FFmpeg$WindowsTarget"
Write-Host
# Stop on all PowerShell command errors
$ErrorActionPreference = "Stop"
if (! (Test-Path $PSScriptRoot\Libs\ffmpeg\configure)) {
Write-Error 'configure is not found in ffmpeg folder. Ensure this folder is populated with ffmpeg snapshot'
Exit 1
}
# Search for MSYS locations
if (!(Test-Path $BashExe)) {
$msysFound = $false
@( 'C:\msys64', 'C:\msys' ) | ForEach-Object {
if (Test-Path $_) {
$BashExe = "${_}\usr\bin\bash.exe"
$msysFound = $true
break
}
}
if (! $msysFound) {
Write-Error "MSYS2 not found."
Exit 1;
}
}
[System.IO.DirectoryInfo] $vsLatestPath = `
& "$VSInstallerFolder\vswhere.exe" `
$VsWhereCriteria `
-property installationPath `
-products * `
-requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64
Write-Host "Visual Studio Installation folder: [$vsLatestPath]"
# 14.16.27023 => v141
$platformToolSet = "v$($VcVersion.Major)$("$($VcVersion.Minor)"[0])"
Write-Host "Platform Toolset: [$platformToolSet]"
# Export full current PATH from environment into MSYS2
$env:MSYS2_PATH_TYPE = 'inherit'
# Save orignal environment variables
$oldEnv = @{};
foreach ($item in Get-ChildItem env:)
{
$oldEnv.Add($item.Name, $item.Value);
}
$start = Get-Date
$success = 1
foreach ($platform in $Platforms) {
try
{
Build-Platform `
-SolutionDir "${PSScriptRoot}\" `
-Platform $platform `
-Configuration 'Release' `
-WindowsTargetPlatformVersion $WindowsTargetPlatformVersion `
-VcVersion $VcVersion `
-PlatformToolset $platformToolSet `
-VsLatestPath $vsLatestPath `
-BashExe $BashExe
}
catch
{
Write-Warning "Error occured: $PSItem"
$success = 0
Break
}
finally
{
# Restore orignal environment variables
foreach ($item in $oldEnv.GetEnumerator())
{
Set-Item -Path env:"$($item.Name)" -Value $item.Value
}
foreach ($item in Get-ChildItem env:)
{
if (!$oldEnv.ContainsKey($item.Name))
{
Remove-Item -Path env:"$($item.Name)"
}
}
}
}
if ($success -and $NugetPackageVersion)
{
nuget pack .\FFmpegInteropX.FFmpegUWP.nuspec `
-Properties "id=FFmpegInteropX.FFmpegUWP;repositoryUrl=$FFmpegUrl;repositoryCommit=$FFmpegCommit" `
-Version $NugetPackageVersion `
-Symbols -SymbolPackageFormat symbols.nupkg `
-OutputDirectory "${PSScriptRoot}\Output\NuGet"
}
Write-Host
Write-Host 'Time elapsed'
Write-Host ('{0}' -f ((Get-Date) - $start))
Write-Host
if ($success)
{
Write-Host 'Build succeeded!'
}
else
{
Write-Error 'Build failed!'
Exit 1
}