-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.gradle.kts
More file actions
203 lines (169 loc) · 6.97 KB
/
Copy pathbuild.gradle.kts
File metadata and controls
203 lines (169 loc) · 6.97 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
/*
* Kotlin
*
* Copyright 2024-2026 MicroEJ Corp. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be found with this software.
* Build: 7E4D1F7C
*/
plugins {
id("com.microej.gradle.application") version "1.5.0"
}
group = "com.microej.example"
version = "3.0.0"
microej {
applicationEntryPoint = "com.microej.example.protobufexample.Main"
skippedCheckers = "nullanalysis"
}
// --- Protobuf library version selection ---
// --- Either "protobuf4" or "protobuf3". Favor Protobuf4 over Protobuf3 for security and maintainability reasons. ---
val protobufLibraryVersion =
"protobuf4" // /!\ Run gradle clean after changing the library version, or the build will fail because of stale files
if (protobufLibraryVersion != "protobuf3" && protobufLibraryVersion != "protobuf4") {
error("Unsupported protobufLibraryVersion '$protobufLibraryVersion': use \"protobuf3\" or \"protobuf4\"")
}
val isV4 = protobufLibraryVersion == "protobuf4"
val protocTools by configurations.creating { isTransitive = false }
// Detect platform for protoc binary download from Maven Central
val protocPlatform: String = run {
val os = System.getProperty("os.name").lowercase()
val arch = System.getProperty("os.arch").lowercase()
val osTag = when {
os.contains("windows") -> "windows"
os.contains("linux") -> "linux"
os.contains("mac") || os.contains("darwin") -> "osx"
else -> error("Unsupported OS: $os")
}
val archTag = when {
arch.contains("aarch64") || arch.contains("arm64") -> "aarch_64"
arch.contains("amd64") || arch.contains("x86_64") -> "x86_64"
else -> error("Unsupported architecture: $arch")
}
"$osTag-$archTag"
}
dependencies {
implementation("ej.api:edc:1.3.7")
implementation("ej.library.eclasspath:logging:1.2.1")
implementation("ej.library.eclasspath:bytebuffer:1.2.0")
if (isV4) {
val protobuf4Version = "1.0.1"
implementation("com.google:protobuf4:$protobuf4Version")
protocTools("com.google:protobuf4:$protobuf4Version:tools@zip")
} else {
implementation("com.google:protobuf3:2.0.3")
}
microejVee("com.nxp.vee.mimxrt1170:vee-port:3.0.0")
}
val generatedProtoDir = layout.buildDirectory.dir("generated/proto")
// --- Extract protoc-gen-microej from Protobuf4 tools zip (v4 only) ---
val extractProtocTools by tasks.registering(Copy::class) {
description = "Extract protoc-gen-microej plugin from Protobuf4 dependency"
enabled = isV4
from(protocTools.elements.map { files -> files.map { zipTree(it) } })
into(layout.buildDirectory.dir("protoc-tools"))
}
// --- Download protoc from Maven Central ---
val resolveProtoc by tasks.registering {
description = "Download protoc binary from Maven Central"
if (isV4) {
dependsOn(extractProtocTools)
}
val toolsDir = layout.buildDirectory.dir("protoc-tools").get().asFile
val protocFile = layout.buildDirectory.file("protoc-tools/protoc.exe")
outputs.file(protocFile)
doLast {
val protocVersion = if (isV4) {
val versionFile = File(toolsDir, "protoc.version")
if (!versionFile.exists()) {
throw GradleException("protoc.version file not found in tools package")
}
versionFile.readText().trim()
} else {
"3.7.1"
}
logger.lifecycle("Protoc version required: $protocVersion ($protocPlatform)")
// Download protoc
val protocUrl = "https://repo.maven.apache.org/maven2/com/google/protobuf/protoc" +
"/$protocVersion/protoc-$protocVersion-$protocPlatform.exe"
val dest = protocFile.get().asFile
dest.parentFile.mkdirs()
ant.invokeMethod("get", mapOf("src" to protocUrl, "dest" to dest, "skipexisting" to true))
dest.setExecutable(true)
logger.lifecycle("Downloaded protoc $protocVersion to ${dest.name}")
}
}
// --- generateProto task ---
val generateProto by tasks.registering {
description = "Generate Java sources from .proto files"
group = "protobuf"
dependsOn(resolveProtoc)
val outDir = generatedProtoDir.get().asFile
// Auto-discover all directories under src/ that contain .proto files
val protoDirs = listOf(file("src/main"), file("src/test")).flatMap { srcDir ->
srcDir.listFiles { f ->
f.isDirectory && f.name != "java" && f.name != "resources"
}?.filter { dir ->
fileTree(dir).matching { include("**/*.proto") }.files.isNotEmpty()
} ?: emptyList()
}
protoDirs.forEach { inputs.dir(it) }
outputs.dir(outDir)
doLast {
if (isV4) {
project.exec {
commandLine("pip3", "install", "protobuf")
}
}
var count = 0
protoDirs.forEach { protoDir ->
fileTree(protoDir).matching { include("**/*.proto") }.forEach { protoFile ->
logger.lifecycle("Generating ${protoFile.relativeTo(protoDir)} ...")
val toolsDir = layout.buildDirectory.dir("protoc-tools").get().asFile
val protoc = File(toolsDir, "protoc.exe")
if (isV4) {
val isWindows = System.getProperty("os.name").lowercase().contains("windows")
val pluginPath = if (isWindows) {
fileTree(toolsDir).matching { include("**/protoc-gen-microej.bat") }.singleFile.absolutePath
} else {
val pyScript = fileTree(toolsDir).matching { include("**/protoc-gen-microej.py") }.singleFile
pyScript.setExecutable(true)
pyScript.absolutePath
}
project.exec {
commandLine(
protoc.absolutePath,
"--plugin=protoc-gen-microej=$pluginPath",
"--microej_out=${outDir.absolutePath}",
"--proto_path=${protoDir.absolutePath}",
protoFile.absolutePath
)
}
} else {
project.exec {
commandLine(
protoc.absolutePath,
"--java_out=${outDir.absolutePath}",
"--proto_path=${protoDir.absolutePath}",
protoFile.absolutePath
)
}
}
count++
}
}
if (count == 0) {
logger.lifecycle("No .proto files found, skipping.")
} else {
logger.lifecycle("Done: $count proto files generated.")
}
}
}
sourceSets {
main {
java {
srcDir(generatedProtoDir)
}
}
}
tasks.named("compileJava") { dependsOn(generateProto) }
tasks.named("sourcesJar") { dependsOn(generateProto) }
tasks.named("shrinkRuntimeEnvironment") { dependsOn(generateProto) }