-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathbuild.gradle
More file actions
221 lines (201 loc) · 8.52 KB
/
build.gradle
File metadata and controls
221 lines (201 loc) · 8.52 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
plugins {
id 'cpp-application'
}
// this feels weird but it is the only way invoking `./gradlew :ddprof-lib:*` tasks will work
if (rootDir.toString().endsWith("ddprof-lib/gradle")) {
apply from: rootProject.file('../../common.gradle')
}
// disable the default compile and link tasks not to interfere with our custom ones
tasks.withType(CppCompile).configureEach { task ->
if (task.name.startsWith('compileRelease') || task.name.startsWith('compileDebug')) {
task.onlyIf {
false
}
}
}
tasks.withType(LinkExecutable).configureEach { task ->
if (task.name.startsWith('linkRelease') || task.name.startsWith('linkDebug')) {
task.onlyIf {
false
}
}
}
tasks.withType(ExtractSymbols).configureEach { task ->
task.onlyIf {
false
}
}
tasks.withType(StripSymbols).configureEach { task ->
task.onlyIf {
false
}
}
def buildNativeLibsTask = tasks.register("buildNativeLibs") {
group = 'build'
description = "Build the native libs for the Google Tests"
onlyIf {
hasGtest && !project.hasProperty('skip-native') && !project.hasProperty('skip-gtest') && os().isLinux()
}
def srcDir = project(':ddprof-lib').file('src/test/resources/native-libs')
def targetDir = project(':ddprof-lib').file('build/test/resources/native-libs/')
// Move the exec calls to the execution phase
doLast {
srcDir.eachDir { dir ->
def libName = dir.name
def libDir = file("${targetDir}/${libName}")
def libSrcDir = file("${srcDir}/${libName}")
exec {
commandLine "sh", "-c", """
echo "Processing library: ${libName} @ ${libSrcDir}"
mkdir -p ${libDir}
cd ${libSrcDir}
make TARGET_DIR=${libDir}
"""
}
}
}
inputs.files project(':ddprof-lib').files('src/test/resources/native-libs/**/*')
outputs.dir "${targetDir}"
}
def gtestAll = tasks.register("gtest") {
onlyIf {
hasGtest && !project.hasProperty('skip-tests') && !project.hasProperty('skip-native') && !project.hasProperty('skip-gtest')
}
group = 'verification'
description = "Run all Google Tests for all build configurations of the library"
if (!hasGtest) {
logger.warn("WARNING: Google Test not found - skipping native tests")
}
}
// we need this trickery to reuse the toolchain and system config from tasks created by the cpp-application plugin
tasks.whenTaskAdded { task ->
if (task instanceof CppCompile) {
if (!task.name.startsWith('compileGtest') && task.name.contains('Release')) {
buildConfigurations.each { config ->
if (config.os == osIdentifier() && config.arch == archIdentifier()) {
project(':ddprof-lib').file("src/test/cpp/").eachFile {
def testFile = it
def testName = it.name.substring(0, it.name.lastIndexOf('.'))
def gtestCompileTask = tasks.register("compileGtest${config.name.capitalize()}_${testName}", CppCompile) {
onlyIf {
config.active && hasGtest && !project.hasProperty('skip-tests') && !project.hasProperty('skip-native')
&& !project.hasProperty('skip-gtest')
}
group = 'build'
description = "Compile the Google Test ${testName} for the ${config.name} build of the library"
objectFileDir = file("$buildDir/obj/gtest/${config.name}/${testName}")
compilerArgs.addAll(config.compilerArgs.findAll {
// need to drop the -std and -DNDEBUG flags because we need a different standard and assertions enabled
it != '-std=c++17' && it != '-DNDEBUG'
})
if (os().isLinux() && isMusl()) {
compilerArgs.add('-D__musl__')
}
compilerArgs.add('-std=c++17')
toolChain = task.toolChain
targetPlatform = task.targetPlatform
includes task.includes
includes project(':ddprof-lib').file('src/main/cpp-external').toString()
includes project(':ddprof-lib').file('src/main/cpp').toString()
includes "${javaHome()}/include"
includes project(':malloc-shim').file('src/main/public').toString()
if (os().isMacOsX()) {
includes "${javaHome()}/include/darwin"
includes "/opt/homebrew/opt/googletest/include/"
} else if (os().isLinux()) {
includes "${javaHome()}/include/linux"
}
includes task.systemIncludes
source project(':ddprof-lib').fileTree('src/main/cpp') {
include '**/*'
}
source project(':ddprof-lib').fileTree('src/main/cpp-external') {
include '**/*'
}
source testFile
inputs.files source
outputs.dir objectFileDir
}
def linkTask = tasks.named("linkGtest${config.name.capitalize()}_${testName}")
if (linkTask != null) {
linkTask.get().dependsOn gtestCompileTask
}
def subrepoInitTask = project(':ddprof-lib').tasks.named("initSubrepo")
if (subrepoInitTask != null) {
gtestCompileTask.configure {
dependsOn subrepoInitTask
}
}
}
}
}
}
} else if (task instanceof LinkExecutable) {
if (!task.name.startsWith('linkGtest') && task.name.contains('Release')) {
buildConfigurations.each { config ->
if (config.os == osIdentifier() && config.arch == archIdentifier()) {
def gtestTask = tasks.register("gtest${config.name.capitalize()}") {
group = 'verification'
description = "Run all Google Tests for the ${config.name} build of the library"
}
project(':ddprof-lib').file("src/test/cpp/").eachFile {
def testFile = it
def testName = it.name.substring(0, it.name.lastIndexOf('.'))
def binary = file("$buildDir/bin/gtest/${config.name}_${testName}/${testName}")
def gtestLinkTask = tasks.register("linkGtest${config.name.capitalize()}_${testName}", LinkExecutable) {
onlyIf {
config.active && hasGtest && !project.hasProperty('skip-tests') && !project.hasProperty('skip-native')
&& !project.hasProperty('skip-gtest')
}
group = 'build'
description = "Link the Google Test for the ${config.name} build of the library"
source = fileTree("$buildDir/obj/gtest/${config.name}/${testName}")
linkedFile = binary
if (config.name != 'release') {
// linking the gtests using the minimizing release flags is making gtest unhappy
linkerArgs.addAll(config.linkerArgs)
}
linkerArgs.addAll("-lgtest", "-lgtest_main", "-lgmock", "-lgmock_main", "-ldl", "-lpthread", "-lm")
if (os().isMacOsX()) {
linkerArgs.addAll("-L/opt/homebrew/opt/googletest/lib")
} else {
linkerArgs.add("-lrt")
}
toolChain = task.toolChain
targetPlatform = task.targetPlatform
libs = task.libs
inputs.files source
outputs.file linkedFile
}
def gtestExecuteTask = tasks.register("gtest${config.name.capitalize()}_${testName}", Exec) {
onlyIf {
config.active && hasGtest && !project.hasProperty('skip-tests') && !project.hasProperty('skip-native')
&& !project.hasProperty('skip-gtest')
}
group = 'verification'
description = "Run the Google Test ${testName} for the ${config.name} build of the library"
dependsOn gtestLinkTask
executable binary
config.testEnv.each { key, value ->
environment key, value
}
inputs.files binary
// Test tasks should run every time the test command is run
outputs.upToDateWhen { false }
}
def compileTask = tasks.findByName("compileGtest${config.name.capitalize()}_${testName}")
if (compileTask != null) {
gtestLinkTask.dependsOn compileTask
}
gtestTask.get().dependsOn gtestExecuteTask.get()
if (os().isLinux()) {
// custom binaries for tests are built only on linux
gtestExecuteTask.get().dependsOn(buildNativeLibs)
}
gtestAll.get().dependsOn gtestExecuteTask.get()
}
}
}
}
}
}