forked from DataDog/java-profiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.gradle
More file actions
337 lines (287 loc) · 9.65 KB
/
Copy pathbuild.gradle
File metadata and controls
337 lines (287 loc) · 9.65 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
plugins {
id 'cpp-library'
id 'java'
id 'maven-publish'
id 'signing'
id 'com.github.ben-manes.versions' version '0.27.0'
id "com.diffplug.spotless" version "6.11.0"
}
def libraryName = "ddprof"
description = "Datadog Java Profiler Library"
def component_version = project.hasProperty("ddprof_version") ? project.ddprof_version : project.version
// this feels weird but it is the only way invoking `./gradlew :ddprof-lib:*` tasks will work
if (rootDir.toString().endsWith("ddprof-lib")) {
apply from: rootProject.file('../common.gradle')
}
def libraryTargetPath() {
return "${projectDir}/build/classes/java/main/META-INF/native-libs/${osIdentifier()}-${archIdentifier()}${isMusl() ? '-musl' : ''}"
}
def librarySourcePath(boolean debug = false) {
def osarchext = ""
if (osIdentifier() == 'linux' && archIdentifier() != 'x64') {
// when built on aarch64 the location the library is built in is 'x86-64' ¯\_(ツ)_/¯
osarchext = "x86-64"
} else if (osIdentifier() == 'macos') {
osarchext = archIdentifier() == 'x64' ? 'x86-64' : 'aarch64'
}
def libsDir = debug ? "debug" : "release"
def qualifier = debug ? "" : "stripped"
return "${projectDir}/build/lib/main/${libsDir}/${osIdentifier()}/${osarchext}/${qualifier}/libjavaProfiler.${osIdentifier() == 'macos' ? 'dylib' : 'so'}"
}
ext {
libraryTargetPath = this.&libraryTargetPath
librarySourcePath = this.&librarySourcePath
}
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
def isGitlabCI = System.getenv("GITLAB_CI") != null
// configure the compiler here
tasks.withType(CppCompile).configureEach {
onlyIf {
!project.hasProperty('skip-native')
}
def taskIncludes = ["${javaHome()}/include"]
def taskArgs = ["-fno-omit-frame-pointer", "-momit-leaf-frame-pointer", "-fvisibility=hidden",
"-fdata-sections", "-ffunction-sections", "-std=c++11", "-DPROFILER_VERSION=\"${version}\"", "-DCOUNTERS"]
if (os().isMacOsX()) {
taskIncludes.add "${javaHome()}/include/darwin"
taskArgs.addAll(["-D_XOPEN_SOURCE", "-D_DARWIN_C_SOURCE"])
} else if (os().isLinux()) {
taskIncludes.add "${javaHome()}/include/linux"
if (isMusl()) {
taskArgs.add '-D__musl__'
}
}
if (it.name.contains('Debug')) {
taskArgs.add("-DDEBUG")
} else {
taskArgs.add("-DNDEBUG")
}
// TODO for Clang we would use scan-build utility instead but it needs to be installed first
taskIncludes.add "${projectDir}/../malloc-shim/src/main/public"
includes {
taskIncludes
}
compilerArgs.addAll(taskArgs)
}
// configure linker
tasks.withType(LinkSharedLibrary) {
onlyIf {
!project.hasProperty('skip-native')
}
linkerArgs.addAll(["-ldl", "-lpthread", "-lm"])
if (os().isLinux()) {
linkerArgs.addAll(["-Wl,-z,defs", "-Wl,-z,nodelete", "-lrt", "-static-libstdc++", "-static-libgcc",
"-Wl,--exclude-libs,ALL", "-Wl,--gc-sections"])
}
linkerArgs.add '-v'
}
tasks.withType(StripSymbols) {
onlyIf {
!project.hasProperty('skip-native')
}
}
library {
baseName = "javaProfiler"
source.from file('src/main/cpp')
privateHeaders.from file('src/main/cpp')
// aarch64 support is still incubating
// for the time being an aarch64 linux machine will match 'machines.linux.x86_64'
targetMachines = [machines.macOS, machines.linux.x86_64]
linkage = [Linkage.SHARED]
}
dependencies {
if (os().isLinux()) {
// the malloc shim works only on linux
project(':malloc-shim')
}
}
// the bridge between the gradle build and gtests
// the gradle gtest plugin(s) are quite unusable at this moment - using a shell script seems to be the only sane option
task cppTest(type: Exec) {
if (project.hasProperty('skip-tests') || project.hasProperty('skip-cpp-tests')) {
environment 'SKIP_TESTS', 'true'
}
workingDir "$projectDir"
commandLine './test.sh'
dependsOn assemble
}
task copyLibs(type: Copy) {
dependsOn cppTest
if (!project.hasProperty("with-libs")) {
from file(librarySourcePath())
into file(libraryTargetPath())
}
}
task copyDebugLibs(type: Copy) {
// This must depend on copyLibs as Gradle detects that this task is using the same target path
// and keeps on complaining about undeclared inputs
dependsOn copyLibs
from file(librarySourcePath(true))
into file(libraryTargetPath())
}
// Allow specifying the external location for the native libraries
// The libraries should be properly sorted into subfolders corresponding to the `libraryTargetPath` value for each
// os/arch/libc combination
task copyExternalLibs(type: Copy) {
if (project.hasProperty("with-libs")) {
from(project.getProperty("with-libs")) {
include "**/*.so"
include "**/*.dylib"
}
into "${projectDir}/build/classes/java/main/META-INF/native-libs"
}
}
jar.dependsOn copyExternalLibs
task sourcesJar(type: Jar) {
from sourceSets.main.allJava
archiveBaseName = libraryName
archiveClassifier = "sources"
archiveVersion = component_version
}
task javadocJar(type: Jar) {
dependsOn javadoc
archiveBaseName = libraryName
archiveClassifier = 'javadoc'
archiveVersion = component_version
from javadoc.destinationDir
}
javadoc.dependsOn copyLibs
task assembleJar(type: Jar) {
dependsOn copyExternalLibs
if (!project.hasProperty('skip-native')) {
dependsOn copyLibs
}
archiveBaseName = libraryName
archiveClassifier = ""
archiveVersion = component_version
from sourceSets.main.output.classesDirs
}
task assembleDebugJar(type: Jar) {
if (!project.hasProperty('skip-native')) {
dependsOn copyDebugLibs
}
archiveBaseName = libraryName
archiveClassifier = "debug"
archiveVersion = component_version
from sourceSets.main.output.classesDirs
}
task scanBuild(type: Exec) {
workingDir "${projectDir}/src/test/make"
commandLine 'scan-build'
args "-o", "${projectDir}/build/reports/scan-build",
"--force-analyze-debug-code",
"--use-analyzer", "/usr/bin/clang++",
"make", "-j4", "clean", "all"
}
test {
onlyIf {
!project.hasProperty('skip-tests')
}
useJUnitPlatform()
}
tasks.withType(Test) {
onlyIf {
!project.hasProperty('skip-tests')
}
def javaHome = System.getenv("JAVA_TEST_HOME")
if (javaHome == null) {
javaHome = System.getenv("JAVA_HOME")
}
executable = new File("${javaHome}", 'bin/java')
}
configurations {
assembled {
canBeConsumed = true
canBeResolved = false
extendsFrom implementation
}
debug {
canBeConsumed = true
canBeResolved = false
extendsFrom implementation
}
}
artifacts {
assembled(assembleJar)
assembled(assembleDebugJar)
assembled(sourcesJar)
assembled(javadocJar)
assembled(assembleDebugJar)
debug(assembleDebugJar)
}
publishing {
publications {
assembled(MavenPublication) { publication ->
publication.artifact project.tasks.assembleJar
publication.artifact project.tasks.assembleDebugJar
publication.artifact sourcesJar
publication.artifact javadocJar
publication.groupId = 'com.datadoghq'
publication.artifactId = 'ddprof'
}
}
}
tasks.withType(GenerateMavenPom).configureEach {
doFirst {
MavenPom pom = it.pom
pom.name = project.name
pom.description = "${project.description} (${component_version})"
pom.packaging = "jar"
pom.url = "https://github.com/datadog/java-profiler"
pom.licenses {
license {
name = "The Apache Software License, Version 2.0"
url = "http://www.apache.org/licenses/LICENSE-2.0.txt"
distribution = "repo"
}
}
pom.scm {
connection = "scm:https://[email protected]/datadog/java-profiler"
developerConnection = "scm:[email protected]:datadog/java-profiler"
url = "https://github.com/datadog/java-profiler"
}
pom.developers {
developer {
id = "datadog"
name = "Datadog"
}
}
}
}
signing {
useInMemoryPgpKeys(System.getenv("GPG_PRIVATE_KEY"), System.getenv("GPG_PASSWORD"))
sign publishing.publications.assembled
}
tasks.withType(Sign).configureEach {
// Only sign in Gitlab CI
onlyIf { isGitlabCI || (System.getenv("GPG_PRIVATE_KEY") != null && System.getenv("GPG_PASSWORD") != null) }
}
/**
* State assertions below...
*/
gradle.taskGraph.whenReady { TaskExecutionGraph taskGraph ->
if (taskGraph.hasTask(publish) || taskGraph.hasTask("publishToSonatype")) {
assert project.findProperty("removeJarVersionNumbers") != true
if (taskGraph.hasTask("publishToSonatype")) {
assert System.getenv("SONATYPE_USERNAME") != null
assert System.getenv("SONATYPE_PASSWORD") != null
if (isCI) {
assert System.getenv("GPG_PRIVATE_KEY") != null
assert System.getenv("GPG_PASSWORD") != null
}
}
}
}
afterEvaluate {
assert description: "Project $project.path is published, must have a description"
}
// we are publishing very customized artifacts - we are attaching the native library to the resulting JAR artifact
tasks.withType(AbstractPublishToMaven).configureEach {
if (it.name.contains('AssembledPublication')) {
it.dependsOn assembleJar
}
rootProject.subprojects {
mustRunAfter tasks.matching { it instanceof VerificationTask }
}
}