-
Notifications
You must be signed in to change notification settings - Fork 53
/
Build-FFmpeg.ps1
613 lines (494 loc) · 20 KB
/
Build-FFmpeg.ps1
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
param(
[ValidateSet('x86', 'x64', 'ARM', 'ARM64')]
[string[]] $Platforms = ('x86', 'x64', '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.3',
[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.22000.0',
[ValidateSet('Debug', 'Release')]
[string] $Configuration = 'Release',
# shared: create shared dll - static: create lib for static linking
[ValidateSet('shared', 'static')]
[string] $SharedOrStatic = 'shared',
[ValidateSet('enable', 'disable')]
[string] $Gpl = 'disable',
[ValidateSet('enable', 'disable')]
[string] $Encoders = 'disable',
[ValidateSet('enable', 'disable')]
[string] $Devices = 'disable',
[ValidateSet('enable', 'disable')]
[string] $Programs = 'disable',
[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 $PSScriptRoot/Libs/ffmpeg/.git rev-parse HEAD),
[switch] $AllowParallelBuilds,
[switch] $SkipBuildPkgConfigFake,
[switch] $SkipBuildLibs,
[switch] $SkipConfigureFFmpeg
)
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',
[string] $LogFileName,
[bool] $SkipBuildPkgConfigFake,
[bool] $SkipBuildLibs,
[bool] $SkipConfigureFFmpeg
)
New-Item -ItemType Directory -Force $SolutionDir\Intermediate\FFmpeg$WindowsTarget | Out-Null
Start-Transcript -Path $LogFileName
$PSBoundParameters | Out-String
$hostArch = ( 'x86', 'x64' )[ [System.Environment]::Is64BitOperatingSystem ]
$targetArch = $Platform.ToLower()
# Build x86 and x64 with x86 toolchain
if (($targetArch -eq 'x86') -or ($targetArch -eq 'x64'))
{
$hostArch = 'x86'
}
Write-Host
Write-Host "Building FFmpeg for Windows 10 ($WindowsTarget) ${Platform} ${Gpl}-gpl ${Encoders}-encoders ${Devices}-devices ${Programs}-programs ..."
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"
New-Item -ItemType Directory -Force $SolutionDir\Intermediate\FFmpeg$WindowsTarget\$Platform -OutVariable build | Out-Null
New-Item -ItemType Directory -Force $SolutionDir\Output\FFmpeg$WindowsTarget\$Platform -OutVariable target | Out-Null
$env:LIB += ";$build\lib"
$env:INCLUDE += ";$build\include"
$env:Path += ";$SolutionDir\Libs\gas-preprocessor"
if (! $SkipBuildLibs)
{
# Build pkg-config fake
if (! $SkipBuildPkgConfigFake) {
invoke 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." }
}
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\$_ | Out-Null
New-Item -ItemType Directory -Force $target\$_ | Out-Null
}
# 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\"
invoke 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\build-scripts\LibOverrides.props `
/p:useenv=true
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
# Build openssl if not already exists
if (!(Test-Path("$build\lib\ssl.lib")) -or !(Test-Path("$build\lib\crypto.lib"))) {
Write-Host
Write-Host "Building Library openssl..."
Write-Host
$opensslPlatforms = @{
'x86' = 'VC-WIN32'
'x64' = 'VC-WIN64A'
'ARM' = 'VC-WIN32-ARM'
'ARM64' = 'VC-WIN64-ARM'
}
$opensslPlatform = $opensslPlatforms[$Platform]
if ($WindowsTarget -eq "UWP") {
$opensslPlatform = $opensslPlatform + "-UWP"
}
New-Item -ItemType Directory -Force $build\int\openssl -OutVariable ssldir | Out-Null
$oldPath = $env:Path
$env:Path += ";$SolutionDir\Tools\perl\perl\bin;C$SolutionDir\Tools\perl\c\bin;$SolutionDir\Tools\nasm"
$oldDir = get-location
set-location "$ssldir"
try {
invoke perl $SolutionDir\Libs\openssl\Configure $opensslPlatform --prefix=$build --openssldir=$build --with-zlib-include=$build\include --with-zlib-lib=$build\lib\zlib.lib no-tests no-secure-memory
invoke nmake clean
invoke nmake
invoke nmake install_sw
} finally {
set-location $oldDir
$env:Path = $oldPath
}
Copy-Item -Force $SolutionDir\Libs\openssl\license.txt $build\licenses\openssl.txt
Copy-Item -Force $ssldir\libssl_static.lib $build\lib\ssl.lib
Copy-Item -Force $ssldir\libcrypto_static.lib $build\lib\crypto.lib
} else {
Write-Host
Write-Host "Openssl already exists in target build configuration. Skipping build."
Write-Host
}
#Build dav1d
Write-Host ""
Write-Host "Building Library dav1d..."
Write-Host ""
invoke $BashExe --login -c "cd \$SolutionDir && Libs/build-scripts/build-dav1d.sh $WindowsTarget $Platform".Replace("\", "/").Replace(":", "")
if ($Encoders -eq "enable") {
$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
invoke cmd.exe /C $SolutionDir\Libs\build-scripts\build-x265.bat $SolutionDir\Libs\x265\source $build\int\x265 $cmakePlatform $PlatformToolset
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
invoke $BashExe --login -c "cd \$build\x264 && CC=cl ..\..\..\..\Libs\x264\configure --host=${x264Arch}-mingw64 --prefix=\$build --disable-cli --enable-static && make -j8 -e CPPFLAGS=-Oy && make install".Replace("\", "/").Replace(":", "")
#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
invoke $BashExe --login -c "unset tmp && unset temp && cd \$build\libvpx && ..\..\..\..\Libs\libvpx\configure --target=${vpxArch}-${vpxPlatform}-vs17 --prefix=\$build --enable-static --disable-thumb --disable-debug --disable-examples --disable-tools --disable-docs --disable-unit_tests && make -j8 -e CPPFLAGS=-Oy && make install".Replace("\", "/").Replace(":", "")
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
if ($SkipConfigureFFmpeg)
{
$ffmpegparam = "-SkipConfigure"
}
else
{
$ffmpegparam = ""
}
$folderName = "FFmpeg$WindowsTarget"
invoke $BashExe --login -x $SolutionDir\Build\FFmpegConfig.sh $WindowsTarget $Platform $SharedOrStatic $Gpl $Encoders $Devices $Programs $folderName $ffmpegparam
# Copy PDBs to built binaries dir
Get-ChildItem -Recurse -Include '*.pdb' $build\int\ffmpeg\ | Copy-Item -Destination $target\bin\ -Force
# Copy license files
if ($Gpl -eq "enable") {
# Openssl3 requires GPLv3 when not using LGPL
Copy-Item $SolutionDir\Libs\FFmpeg\COPYING.GPLv3 $target\licenses\ffmpeg.txt -Force
}
else {
Copy-Item $SolutionDir\Libs\FFmpeg\COPYING.LGPLv2.1 $target\licenses\ffmpeg.txt -Force
}
Copy-Item $build\licenses\* $target\licenses\ -Force
}
function Invoke() {
# A handy way to run a command, and automatically throw an error if the
# exit code is non-zero.
if ($args.Count -eq 0) {
throw "Must supply some arguments."
}
$command = $args[0]
$commandArgs = @()
if ($args.Count -gt 1) {
$commandArgs = $args[1..($args.Count - 1)]
}
$old = $ErrorActionPreference
$ErrorActionPreference = "Continue"
& $command $commandArgs | Out-Default
$result = $LASTEXITCODE
$ErrorActionPreference = $old
if ($result -ne 0) {
throw "$command $commandArgs exited with code $result.`r`nOriginal command line:`r`n$command $commandArgs"
}
}
$timestamp = Get-Date -Format "yyyyMMdd_HHmmss"
Write-Host
Write-Host "Building FFmpeg$WindowsTarget"
Write-Host
Write-Host "Timestamp: $timestamp"
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
}
# Check build tools
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-Warning "MSYS2 not found."
choice /c YN /m "Do you want to install MSYS2 now to C:\msys64?"
if ($LASTEXITCODE -eq 1)
{
.\Build\InstallTools.ps1 MSYS2
$BashExe = "C:\msys64\usr\bin\bash.exe"
if (!(Test-Path $BashExe)) {
Exit 1
}
}
else
{
Exit 1
}
}
}
if (! (Test-Path "$PSScriptRoot\Tools\nasm")) {
Write-Warning "NASM not found. Installing..."
.\Build\InstallTools.ps1 nasm
}
if (! (Test-Path "$PSScriptRoot\Tools\perl")) {
Write-Warning "Perl not found. Installing..."
.\Build\InstallTools.ps1 perl
}
[System.IO.DirectoryInfo] $vsLatestPath = `
& "$VSInstallerFolder\vswhere.exe" `
$VsWhereCriteria `
-property installationPath `
-products * `
-requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64
if (!$vsLatestPath){
Write-Error "Visual Studio not found!"
}
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'
# Check for nuget.exe if package shall be created
if ($NugetPackageVersion)
{
try
{
nuget > $null
}
catch
{
Write-Error "nuget.exe not found."
}
}
$start = Get-Date
$success = 1
if ($AllowParallelBuilds -and $Platforms.Count -gt 1)
{
$processes = @{}
$clear = ""
if ($ClearBuildFolders)
{
$clear = "-ClearBuildFolders"
}
$addparams = ""
if ($SkipBuildLibs)
{
$addparams += " -SkipBuildLibs"
}
if ($SkipConfigureFFmpeg)
{
$addparams += " -SkipConfigureFFmpeg"
}
$skipPkgConfig = ""
foreach ($platform in $Platforms) {
if ($SkipBuildPkgConfigFake)
{
$skip
}
$proc = Start-Process -PassThru powershell "-File .\Build-FFmpeg.ps1 -Platforms $platform -VcVersion $VcVersion -WindowsTarget $WindowsTarget -WindowsTargetPlatformVersion $WindowsTargetPlatformVersion -Configuration $Configuration -SharedOrStatic $SharedOrStatic -Gpl $Gpl -Encoders $Encoders -Devices $Devices -Programs $Programs -VSInstallerFolder ""$VSInstallerFolder"" -VsWhereCriteria ""$VsWhereCriteria"" -BashExe ""$BashExe"" $clear -FFmpegUrl $FFmpegUrl -FFmpegCommit $FFmpegCommit $skipPkgConfig $addparams"
$processes[$platform] = $proc
# only build PkgConfigFake once
$skipPkgConfig = "-SkipBuildPkgConfigFake"
}
foreach ($platform in $Platforms) {
$processes[$platform].WaitForExit();
$result = $processes[$platform].ExitCode;
if ($result -eq 0)
{
Write-Host "Build for $platform succeeded!"
}
else
{
Write-Host "Build for $platform failed with ErrorCode: $result"
$success = 0
}
}
}
else
{
# Save orignal environment variables
$oldEnv = @{};
foreach ($item in Get-ChildItem env:)
{
$oldEnv.Add($item.Name, $item.Value);
}
foreach ($platform in $Platforms) {
try { Stop-Transcript } catch { }
$logFile = "${PSScriptRoot}\Intermediate\FFmpeg$WindowsTarget\Build_" + $timestamp + "_$platform.log"
try
{
Build-Platform `
-SolutionDir "${PSScriptRoot}\" `
-Platform $platform `
-Configuration 'Release' `
-WindowsTargetPlatformVersion $WindowsTargetPlatformVersion `
-VcVersion $VcVersion `
-PlatformToolset $platformToolSet `
-VsLatestPath $vsLatestPath `
-BashExe $BashExe `
-LogFileName $logFile `
-SkipBuildPkgConfigFake $SkipBuildPkgConfigFake `
-SkipBuildLibs $SkipBuildLibs `
-SkipConfigureFFmpeg $SkipConfigureFFmpeg
}
catch
{
Write-Warning "Error occured: $PSItem"
$success = 0
Break
}
finally
{
try { Stop-Transcript } catch { }
# 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)"
}
}
}
# only build PkgConfigFake once
$BuildPkgConfigFake = $false;
}
}
if ($success -and $NugetPackageVersion)
{
nuget pack .\Build\FFmpegInteropX.$WindowsTarget.FFmpeg.nuspec `
-Properties "id=FFmpegInteropX.$WindowsTarget.FFmpeg;repositoryUrl=$FFmpegUrl;repositoryCommit=$FFmpegCommit;winsdk=$WindowsTargetPlatformVersion;NoWarn=NU5128" `
-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-Warning 'Build failed!'
Exit 1
}