Skip to content

Commit 5194751

Browse files
committed
build: use jni-utils for platform detections
1 parent 9319e17 commit 5194751

File tree

7 files changed

+110
-45
lines changed

7 files changed

+110
-45
lines changed

.github/actions/deploy-ubuntu/action.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,9 +317,9 @@ runs:
317317
- name: Publish release
318318
shell: bash
319319
if: "github.event_name == 'release'"
320-
run: ./gradlew build publish -Pnative.extension=${{ matrix.ext }} -Pdeploy.version=${{ github.event.release.tag_name }} --console=plain --info --configure-on-demand --parallel --build-cache
320+
run: ./gradlew build publish -Pffmpeg.gpl=${{ matrix.gpl }} -Pdeploy.version=${{ github.event.release.tag_name }} --console=plain --info --configure-on-demand --parallel --build-cache
321321

322322
- name: Publish snapshot
323323
shell: bash
324324
if: "github.event_name != 'release'"
325-
run: ./gradlew build publish -Pnative.extension=${{ matrix.ext }} --console=plain --info --configure-on-demand --parallel --build-cache
325+
run: ./gradlew build publish -Pffmpeg.gpl=${{ matrix.gpl }} --console=plain --info --configure-on-demand --parallel --build-cache

.github/workflows/ffmpeg.yml

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,63 +26,63 @@ jobs:
2626
# runs-on: ubuntu-22.04
2727
# strategy:
2828
# matrix:
29-
# ext: ["", -gpl]
29+
# gpl: ["false", "true"]
3030
# steps:
3131
# - uses: actions/checkout@v4
3232
# - uses: ./.github/actions/deploy-ubuntu
3333
# android-arm64:
3434
# runs-on: ubuntu-22.04
3535
# strategy:
3636
# matrix:
37-
# ext: ["", -gpl]
37+
# gpl: ["false", "true"]
3838
# steps:
3939
# - uses: actions/checkout@v4
4040
# - uses: ./.github/actions/deploy-ubuntu
4141
# android-x86:
4242
# runs-on: ubuntu-22.04
4343
# strategy:
4444
# matrix:
45-
# ext: ["", -gpl]
45+
# gpl: ["false", "true"]
4646
# steps:
4747
# - uses: actions/checkout@v4
4848
# - uses: ./.github/actions/deploy-ubuntu
4949
# android-x86_64:
5050
# runs-on: ubuntu-22.04
5151
# strategy:
5252
# matrix:
53-
# ext: ["", -gpl]
53+
# gpl: ["false", "true"]
5454
# steps:
5555
# - uses: actions/checkout@v4
5656
# - uses: ./.github/actions/deploy-ubuntu
5757
# linux-armhf:
5858
# runs-on: ubuntu-22.04
5959
# strategy:
6060
# matrix:
61-
# ext: ["", -gpl]
61+
# gpl: ["false", "true"]
6262
# steps:
6363
# - uses: actions/checkout@v4
6464
# - uses: ./.github/actions/deploy-ubuntu
6565
# linux-arm64:
6666
# runs-on: ubuntu-22.04
6767
# strategy:
6868
# matrix:
69-
# ext: ["", -gpl]
69+
# gpl: ["false", "true"]
7070
# steps:
7171
# - uses: actions/checkout@v4
7272
# - uses: ./.github/actions/deploy-ubuntu
7373
# linux-ppc64le:
7474
# runs-on: ubuntu-22.04
7575
# strategy:
7676
# matrix:
77-
# ext: ["", -gpl]
77+
# gpl: ["false", "true"]
7878
# steps:
7979
# - uses: actions/checkout@v4
8080
# - uses: ./.github/actions/deploy-ubuntu
8181
# linux-x86:
8282
# runs-on: ubuntu-22.04
8383
# strategy:
8484
# matrix:
85-
# ext: ["", -gpl]
85+
# gpl: ["false", "true"]
8686
# steps:
8787
# - uses: actions/checkout@v4
8888
# - uses: ./.github/actions/deploy-ubuntu
@@ -100,31 +100,31 @@ jobs:
100100
# runs-on: macos-12
101101
# strategy:
102102
# matrix:
103-
# ext: ["", -gpl]
103+
# gpl: ["false", "true"]
104104
# steps:
105105
# - uses: actions/checkout@v4
106106
# - uses: ./.github/actions/deploy-macosx
107107
# macosx-x86_64:
108108
# runs-on: macos-12
109109
# strategy:
110110
# matrix:
111-
# ext: ["", -gpl]
111+
# gpl: ["false", "true"]
112112
# steps:
113113
# - uses: actions/checkout@v4
114114
# - uses: ./.github/actions/deploy-macosx
115115
# windows-x86:
116116
# runs-on: windows-2019
117117
# strategy:
118118
# matrix:
119-
# ext: ["", -gpl]
119+
# gpl: ["false", "true"]
120120
# steps:
121121
# - uses: actions/checkout@v4
122122
# - uses: ./.github/actions/deploy-windows
123123
# windows-x86_64:
124124
# runs-on: windows-2019
125125
# strategy:
126126
# matrix:
127-
# ext: ["", -gpl]
127+
# gpl: ["false", "true"]
128128
# steps:
129129
# - uses: actions/checkout@v4
130130
# - uses: ./.github/actions/deploy-windows

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.idea/
22
.gradle/
33
.kotlin/
4-
/build/
4+
build/
55
local.properties
66
/downloads/
77
*.iml

build.gradle.kts

Lines changed: 53 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,70 @@
1-
import org.gradle.internal.extensions.stdlib.capitalized
1+
import dev.silenium.libs.jni.NativePlatform
2+
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
3+
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion
4+
5+
buildscript {
6+
repositories {
7+
maven("https://reposilite.silenium.dev/releases") {
8+
name = "silenium-releases"
9+
}
10+
}
11+
dependencies {
12+
classpath(libs.jni.utils)
13+
}
14+
}
215

316
plugins {
4-
base
17+
alias(libs.plugins.kotlin)
518
`maven-publish`
619
}
720

8-
group = "dev.silenium.libs"
21+
group = "dev.silenium.libs.ffmpeg"
922
version = findProperty("deploy.version") as String? ?: "0.0.0-SNAPSHOT"
1023

11-
val platformTxt = layout.buildDirectory.file("platform.txt").apply {
12-
get().asFile.parentFile.mkdirs()
24+
repositories {
25+
mavenCentral()
26+
maven("https://reposilite.silenium.dev/releases") {
27+
name = "silenium-releases"
28+
}
1329
}
14-
exec {
15-
commandLine(
16-
"bash",
17-
"-c",
18-
"bash " + layout.projectDirectory.file("detect-platform.sh").asFile.absolutePath + " > " + platformTxt.get().asFile.absolutePath,
19-
)
20-
workingDir(layout.projectDirectory.asFile)
21-
}.assertNormalExitValue()
22-
val platform: String = platformTxt.get().asFile.readText().trim()
23-
val platformExtension = findProperty("native.extension") as String? ?: ""
24-
logger.lifecycle("Building for platform: $platform${platformExtension}")
2530

26-
val compileDir = layout.projectDirectory.dir("ffmpeg/cppbuild/${platform}${platformExtension}")
31+
dependencies {
32+
implementation(libs.jni.utils)
33+
}
34+
35+
val withGPL: Boolean = findProperty("ffmpeg.gpl")?.toString()?.toBoolean() ?: false
36+
val platformExtension = "-gpl".takeIf { withGPL }.orEmpty()
37+
val platform = NativePlatform.platform(platformExtension)
2738

2839
val compileNative by tasks.registering(Exec::class) {
2940
commandLine(
3041
"bash",
3142
layout.projectDirectory.file("cppbuild.sh").asFile.absolutePath,
32-
"-extension", platformExtension,
33-
"install",
43+
"-extension", platform.extension,
44+
"-platform", platform.osArch,
45+
"install", "ffmpeg",
3446
)
3547
workingDir(layout.projectDirectory.asFile)
36-
environment("PROJECTS" to "ffmpeg")
3748

3849
inputs.property("platform", platform)
50+
inputs.files(layout.projectDirectory.files("cppbuild.sh"))
51+
inputs.files(layout.projectDirectory.files("detect-platform.sh"))
52+
inputs.files(layout.projectDirectory.files("ffmpeg/cppbuild.sh"))
3953
inputs.files(layout.projectDirectory.files("ffmpeg/*.patch"))
4054
inputs.files(layout.projectDirectory.files("ffmpeg/*.diff"))
41-
inputs.files(layout.projectDirectory.files("ffmpeg/cppbuild.sh"))
42-
outputs.dir(compileDir)
55+
outputs.dir(layout.projectDirectory.dir("ffmpeg/cppbuild/${platform}"))
4356
outputs.cacheIf { true }
4457
}
4558

46-
tasks.build {
47-
dependsOn(compileNative)
48-
}
59+
tasks.processResources {
60+
val platform = NativePlatform.platform(platformExtension)
4961

50-
val bundleJar by tasks.registering(Jar::class) {
5162
from(compileNative.get().outputs.files) {
5263
include("lib/*.so")
5364
include("lib/*.dll")
5465
include("lib/*.dylib")
5566
eachFile {
56-
path = "natives/$platform/${this.name}"
67+
path = "natives/$platform/$name"
5768
}
5869
into("natives/$platform/")
5970
}
@@ -68,12 +79,24 @@ val zipBuild by tasks.registering(Zip::class) {
6879
}
6980
}
7081

82+
kotlin {
83+
compilerOptions {
84+
jvmTarget = JvmTarget.JVM_1_8
85+
languageVersion = KotlinVersion.KOTLIN_1_7
86+
}
87+
jvmToolchain(8)
88+
}
89+
90+
java {
91+
withSourcesJar()
92+
}
93+
7194
publishing {
7295
publications {
73-
create<MavenPublication>("native${platform.split("-").joinToString("") { it.capitalized() }}") {
74-
artifact(bundleJar)
96+
create<MavenPublication>("native${platform.capitalized}") {
97+
from(components["java"])
7598
artifact(zipBuild)
76-
artifactId = "ffmpeg-natives-${platform}${platformExtension}"
99+
artifactId = "ffmpeg-natives-${platform}"
77100
}
78101
}
79102

gradle/libs.versions.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[versions]
2+
kotlin = "2.0.0"
3+
jni-utils = "0.1.2"
4+
5+
[libraries]
6+
jni-utils = { group = "dev.silenium.libs.jni", name = "jni-utils", version.ref = "jni-utils" }
7+
8+
[plugins]
9+
kotlin = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }

settings.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rootProject.name = "ffmpeg-static"
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package dev.silenium.libs.ffmpeg
2+
3+
import dev.silenium.libs.jni.NativeLoader
4+
import dev.silenium.libs.jni.NativePlatform
5+
6+
object FFmpeg {
7+
private val libs = listOf(
8+
"avcodec",
9+
"avdevice",
10+
"avfilter",
11+
"avformat",
12+
"avutil",
13+
"postproc",
14+
"swresample",
15+
"swscale",
16+
)
17+
private var loaded = false
18+
19+
@Synchronized
20+
fun ensureLoaded(withGPL: Boolean = false) {
21+
if (loaded) return
22+
23+
libs.forEach {
24+
NativeLoader.loadLibraryFromClasspath(
25+
baseName = it,
26+
platform = NativePlatform.platform("-gpl".takeIf { withGPL }.orEmpty()),
27+
)
28+
}
29+
30+
loaded = true
31+
}
32+
}

0 commit comments

Comments
 (0)