-
Notifications
You must be signed in to change notification settings - Fork 173
/
Copy pathpackrLauncher.gradle.kts
459 lines (401 loc) · 17.5 KB
/
packrLauncher.gradle.kts
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
/*
* Copyright 2020 See AUTHORS file
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@file:Suppress("UnstableApiUsage")
import com.libgdx.gradle.isSnapshot
import com.libgdx.gradle.packrPublishRepositories
import org.gradle.internal.jvm.Jvm
import java.nio.file.Path
group = rootProject.group
version = rootProject.version
plugins {
`cpp-application`
`cpp-unit-test`
xcode
`microsoft-visual-cpp-compiler`
`visual-studio`
`maven-publish`
signing
}
repositories {
jcenter()
maven {
url = uri("https://repo.gradle.org/gradle/libs-snapshots-local/")
}
}
/**
* Path to the JVM that Gradle is running in
*/
val javaHomePathString: String = Jvm.current().javaHome.absolutePath
/**
* Where to output executable files
*/
val distributionDirectoryPath: Path = buildDir.toPath().resolve("distribute")
/**
* The combined platform MacOS executable file path
*/
val macOsLipoOutputFilePath: Path = distributionDirectoryPath.resolve("packrLauncher-macos")
/**
* Combines the executables into a combined platform executable. Currently only 64 bit architecture is built, so it doesn't do anything.
*/
val macOsLipo = tasks.register<Exec>("macOsLipo") {
workingDir = distributionDirectoryPath.toFile()
outputs.file(macOsLipoOutputFilePath.toFile())
executable = "lipo"
args("-create")
args("-output")
args(macOsLipoOutputFilePath.fileName.toString())
}
/**
* Configuration for holding the release executables that are built
*/
val currentOsExecutableZip = tasks.register<Zip>("currentOsExecutableZip") {}
tasks.withType(CppCompile::class).configureEach {
source.from(fileTree(file("src/main/cpp")) {
include("**/*.c")
})
}
dependencies {
implementation(project(":DrOpt"))
//testImplementation("org.gradle.cpp-samples:googletest:1.9.0-gr4-SNAPSHOT")
}
/**
* Stores a mapping from the registered publication names to the operating system family for generating the name in the published POM file.
*/
val operatingSystemFamilyByPublicationName: MutableMap<String, OperatingSystemFamily> = mutableMapOf()
/**
* Linux x86 is no longer built because it's impossible to find a survey that shows anyone running x86 Linux.
* MacOS x86 is no longer built because it requires and older version of Xcode and Apple makes it too difficult to install on newer versions of Mac
* Windows x86 is no longer built because the Adopt OpenJDK has crash failures.
*/
val targetPlatformsToBuild = listOf(machines.windows.x86_64, machines.linux.x86_64, machines.macOS.x86_64, machines.macOS.architecture("aarch64"))
application {
targetMachines.set(targetPlatformsToBuild)
toolChains.configureEach {
if (this is Clang) {
target("host:x86-64") {
cppCompiler.withArguments { add("--target=x86_64-apple-darwin") }
getcCompiler().withArguments { add("--target=x86_64-apple-darwin") }
objcCompiler.withArguments { add("--target=x86_64-apple-darwin") }
objcppCompiler.withArguments { add("--target=x86_64-apple-darwin") }
linker.withArguments { add("--target=x86_64-apple-darwin") }
assembler.withArguments { add("--target=x86_64-apple-darwin") }
}
target("host:aarch64") {
cppCompiler.withArguments { add("--target=arm64-apple-darwin") }
getcCompiler().withArguments { add("--target=arm64-apple-darwin") }
objcCompiler.withArguments { add("--target=arm64-apple-darwin") }
objcppCompiler.withArguments { add("--target=arm64-apple-darwin") }
linker.withArguments { add("--target=arm64-apple-darwin") }
assembler.withArguments { add("--target=arm64-apple-darwin") }
}
}
}
binaries.configureEach(CppExecutable::class.java) {
logger.debug("Configuring executable ${this.name}")
val binaryToolChain = toolChain
val binaryCompileTask = compileTask.get()
addJvmHeaders(binaryCompileTask, this)
val binaryLinkTask: LinkExecutable = linkTask.get()
//
binaryCompileTask.macros["PACKR_VERSION_STRING"] = "\"" + (project.version as String) + "\""
// Create a single special publication from lipo on MacOS since that allows combining multiple architectures into a single binary
val publicationName =
"packrLauncher-${targetMachine.operatingSystemFamily.name}${if (!targetMachine.operatingSystemFamily.isMacOs) "-${targetMachine.architecture.name}" else ""}"
if (binaryCompileTask.isOptimized && publishing.publications.findByName(publicationName) == null) {
logger.info("executableFile = ${executableFile.get()}")
operatingSystemFamilyByPublicationName[publicationName] = targetMachine.operatingSystemFamily
publishing.publications.register<MavenPublication>(publicationName) {
this.groupId = project.group as String
this.artifactId = publicationName
this.version = project.version as String
val artifactFile: File =
if (targetMachine.operatingSystemFamily.isMacOs) macOsLipoOutputFilePath.toFile() else executableFile.get().asFile
artifact(artifactFile) {
if (targetMachine.operatingSystemFamily.isMacOs) {
builtBy(macOsLipo)
} else {
builtBy(executableFileProducer)
}
}
}
// Add the executable to the current OS produced configuration
currentOsExecutableZip.configure {
dependsOn(executableFileProducer)
if (targetMachine.operatingSystemFamily.isMacOs) {
dependsOn(macOsLipo)
from(macOsLipoOutputFilePath) {
rename(".*", publicationName)
}
} else {
from(executableFile) {
rename(".*", publicationName)
}
}
}
}
// Add another target to lipo
if (binaryCompileTask.isOptimized && targetMachine.operatingSystemFamily.isMacOs) {
macOsLipo.configure {
dependsOn(executableFileProducer)
inputs.file(executableFile)
args("-arch")
when (targetMachine.architecture.name) {
MachineArchitecture.X86 -> args("i386")
MachineArchitecture.X86_64 -> args("x86_64")
"aarch64" -> args("arm64")
else -> throw GradleException("Don't know the lipo -arch flag for architecture ${targetMachine.architecture.name}")
}
args(executableFile.get().asFile.absolutePath)
}
}
if (binaryToolChain is VisualCpp) {
binaryCompileTask.macros["DLL_EXPORT"] = null
binaryCompileTask.macros["_WIN32"] = null
binaryCompileTask.macros["WIN32"] = null
binaryCompileTask.macros["UNICODE"] = null
binaryCompileTask.macros["_UNICODE"] = null
binaryCompileTask.macros["_WIN32_WINNT_WINXP=0x0501"] = null // target windows xp
binaryCompileTask.compilerArgs.add("/EHs")
binaryCompileTask.compilerArgs.add("/MT")
binaryCompileTask.compilerArgs.add("/nologo")
binaryCompileTask.compilerArgs.add("/std:c++14")
binaryCompileTask.compilerArgs.add("/utf-8")
binaryLinkTask.linkerArgs.add("/nologo")
binaryLinkTask.linkerArgs.add("/SUBSYSTEM:WINDOWS")
binaryLinkTask.linkerArgs.add("User32.lib")
binaryLinkTask.linkerArgs.add("Shell32.lib")
if (binaryCompileTask.isOptimized) {
binaryCompileTask.compilerArgs.add("/Os")
binaryCompileTask.compilerArgs.add("/Gw")
binaryCompileTask.compilerArgs.add("/Gy")
binaryLinkTask.linkerArgs.add("/opt:icf")
binaryLinkTask.linkerArgs.add("/opt:ref")
}
} else if (binaryToolChain is Gcc) {
binaryCompileTask.compilerArgs.add("-fPIC")
binaryCompileTask.compilerArgs.add("-c")
binaryCompileTask.compilerArgs.add("-fmessage-length=0")
binaryCompileTask.compilerArgs.add("-Wwrite-strings")
binaryCompileTask.compilerArgs.add("-std=c++14")
binaryCompileTask.compilerArgs.add("-no-pie")
binaryCompileTask.compilerArgs.add("-fno-pie")
// compiler linux
if (targetPlatform.targetMachine.architecture.name == MachineArchitecture.X86) {
binaryCompileTask.compilerArgs.add("-m32")
}
if (targetPlatform.targetMachine.architecture.name == MachineArchitecture.X86_64) {
binaryCompileTask.compilerArgs.add("-m64")
}
// compiler osx
if (targetPlatform.targetMachine.operatingSystemFamily.isMacOs) {
binaryCompileTask.includes(file("/System/Library/Frameworks/JavaVM.framework/Versions/A/Headers"))
}
if (targetMachine.operatingSystemFamily.isLinux) {
binaryLinkTask.linkerArgs.add("-lpthread")
}
binaryLinkTask.linkerArgs.add("-ldl")
binaryLinkTask.linkerArgs.add("-no-pie")
binaryLinkTask.linkerArgs.add("-fno-pie")
} else if (binaryToolChain is Clang) {
binaryCompileTask.compilerArgs.add("-fPIC")
binaryCompileTask.compilerArgs.add("-c")
binaryCompileTask.compilerArgs.add("-fmessage-length=0")
binaryCompileTask.compilerArgs.add("-Wwrite-strings")
if (targetMachine.operatingSystemFamily.isMacOs) {
binaryCompileTask.compilerArgs.add("-mmacosx-version-min=${rootProject.ext["macOsMinimumVersion"]}")
binaryLinkTask.linkerArgs.add("-mmacosx-version-min=${rootProject.ext["macOsMinimumVersion"]}")
}
binaryCompileTask.compilerArgs.add("-std=c++14")
binaryLinkTask.linkerArgs.add("-ldl")
if (targetPlatform.targetMachine.operatingSystemFamily.isMacOs) {
binaryLinkTask.linkerArgs.add("-framework")
binaryLinkTask.linkerArgs.add("CoreFoundation")
}
}
}
}
unitTest {
targetMachines.set(targetPlatformsToBuild)
toolChains.configureEach {
if (this is Clang) {
target("host:x86-64") {
cppCompiler.withArguments { add("--target=x86_64-apple-darwin") }
getcCompiler().withArguments { add("--target=x86_64-apple-darwin") }
objcCompiler.withArguments { add("--target=x86_64-apple-darwin") }
objcppCompiler.withArguments { add("--target=x86_64-apple-darwin") }
linker.withArguments { add("--target=x86_64-apple-darwin") }
assembler.withArguments { add("--target=x86_64-apple-darwin") }
}
target("host:aarch64") {
cppCompiler.withArguments { add("--target=arm64-apple-darwin") }
getcCompiler().withArguments { add("--target=arm64-apple-darwin") }
objcCompiler.withArguments { add("--target=arm64-apple-darwin") }
objcppCompiler.withArguments { add("--target=arm64-apple-darwin") }
linker.withArguments { add("--target=arm64-apple-darwin") }
assembler.withArguments { add("--target=arm64-apple-darwin") }
}
}
}
binaries.configureEach(CppTestExecutable::class.java) {
val binaryCompileTask = compileTask.get()
val binaryLinkTask = linkTask.get()
when (toolChain) {
is Gcc -> {
binaryCompileTask.compilerArgs.add("-std=c++14")
if (targetMachine.operatingSystemFamily.isLinux) {
binaryLinkTask.linkerArgs.add("-lpthread")
}
binaryLinkTask.linkerArgs.add("-ldl")
binaryLinkTask.linkerArgs.add("-no-pie")
binaryLinkTask.linkerArgs.add("-fno-pie")
}
is Clang -> {
binaryCompileTask.compilerArgs.add("-std=c++14")
binaryLinkTask.linkerArgs.add("-ldl")
}
is VisualCpp -> {
binaryCompileTask.macros["UNICODE"] = null
binaryCompileTask.macros["_UNICODE"] = null
binaryCompileTask.compilerArgs.add("/std:c++14")
binaryLinkTask.linkerArgs.add("/SUBSYSTEM:CONSOLE")
binaryLinkTask.linkerArgs.add("Shell32.lib")
}
}
if (targetMachine.operatingSystemFamily.isMacOs) {
binaryLinkTask.linkerArgs.add("-framework")
binaryLinkTask.linkerArgs.add("CoreFoundation")
}
addJvmHeaders(compileTask.get(), this)
addGoogleTest(compileTask.get())
}
}
tasks.withType(RunTestExecutable::class).configureEach {
workingDir = buildDir.toPath().resolve("cppTestDirectory").toFile()
}
artifacts {
add(configurations.register("currentOsExecutables").name, currentOsExecutableZip)
}
publishing {
repositories {
packrPublishRepositories(project)
/*
* Publishing to GitHub for the executables is causing issues:
* Could not GET 'https://maven.pkg.github.com/libgdx/packr/com/badlogicgames/packr/packrLauncher-linux-x86-64/3.0.0-SNAPSHOT/maven-metadata.xml'.
* Received status code 400 from server: Bad Request
*/
// gitHubRepositoryForPackr(project)
// Inorder to build the packr.jar, executables must be available from all supported platforms.
val ngToken: String? =
findProperty("NG_ARTIFACT_REPOSITORY_TOKEN") as String? ?: System.getenv("NG_ARTIFACT_REPOSITORY_TOKEN")
if (ngToken != null) {
val ngUsername = findProperty("NG_ARTIFACT_REPOSITORY_USER") as String? ?: System.getenv("NG_ARTIFACT_REPOSITORY_USER")
if (isSnapshot) {
maven("https://artifactory.nimblygames.com/artifactory/ng-public-snapshot/") {
credentials {
username = ngUsername
password = ngToken
}
}
} else {
maven("https://artifactory.nimblygames.com/artifactory/ng-public-release/") {
credentials {
username = ngUsername
password = ngToken
}
}
}
}
}
publications {
configureEach {
if (this is MavenPublication) {
this.groupId = project.group as String
this.version = project.version as String
val publicationOperatingSystemFamily = operatingSystemFamilyByPublicationName[name]
if (publicationOperatingSystemFamily != null) {
pom {
val osName = when {
publicationOperatingSystemFamily.isLinux -> "Linux"
publicationOperatingSystemFamily.isMacOs -> "macOS"
publicationOperatingSystemFamily.isWindows -> "Windows"
else -> throw IllegalArgumentException("Unknown OS $publicationOperatingSystemFamily")
}
name.set("Packr native launcher for $osName")
description.set("A native executable for launching a JVM app, making it appear like a native app.")
url.set("https://github.com/libgdx/packr")
licenses {
license {
name.set("The Apache License, Version 2.0")
url.set("http://www.apache.org/licenses/LICENSE-2.0.txt")
}
}
developers {
developer {
id.set("KarlSabo")
name.set("Karl Sabo")
email.set("karl@nimblygames.com")
}
}
scm {
connection.set("scm:git:https://github.com/libgdx/packr")
developerConnection.set("scm:git:https://github.com/libgdx/packr")
url.set("https://github.com/libgdx/packr")
}
}
}
}
}
}
}
signing.useGpgCmd()
if (isSnapshot) {
logger.info("Skipping signing ")
} else {
publishing.publications.configureEach {
logger.info("Should sign publication ${this.name}")
signing.sign(this)
}
}
afterEvaluate {
tasks.withType(PublishToMavenRepository::class).configureEach {
if (name.startsWith("publishMain")) {
logger.info("Disabling CPP plugin publishing task ${this.name}")
enabled = false
}
}
}
/**
* Adds JVM include header paths to [binaryCompileTask].
*/
fun addJvmHeaders(binaryCompileTask: CppCompile, cppBinary: CppBinary) {
binaryCompileTask.includes(file("${javaHomePathString}/include"))
when {
cppBinary.targetMachine.operatingSystemFamily.isLinux -> {
binaryCompileTask.includes(file("${javaHomePathString}/include/linux"))
}
cppBinary.targetMachine.operatingSystemFamily.isMacOs -> {
binaryCompileTask.includes(file("${javaHomePathString}/include/darwin"))
}
cppBinary.targetMachine.operatingSystemFamily.isWindows -> {
binaryCompileTask.includes(file("${javaHomePathString}/include/win32"))
}
}
}
fun addGoogleTest(binaryCompileTask: CppCompile) {
binaryCompileTask.includes(file("${rootProject.projectDir}/googletest"))
binaryCompileTask.includes(file("${rootProject.projectDir}/googletest/include"))
binaryCompileTask.source(file("${rootProject.projectDir}/googletest/src/gtest-all.cc"))
}