-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathScanBuildPlugin.kt
More file actions
103 lines (91 loc) · 3.46 KB
/
ScanBuildPlugin.kt
File metadata and controls
103 lines (91 loc) · 3.46 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
package com.datadoghq.native.scanbuild
import com.datadoghq.native.model.Platform
import com.datadoghq.native.util.PlatformUtils
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.tasks.Exec
/**
* Gradle plugin that provides clang static analysis via scan-build.
*
* This plugin creates a `scanBuild` task that runs the clang static analyzer
* on the C++ codebase using a Makefile-based build.
*
* Usage:
* ```kotlin
* plugins {
* id("com.datadoghq.scanbuild")
* }
*
* scanBuild {
* makefileDir.set(layout.projectDirectory.dir("src/test/make"))
* outputDir.set(layout.buildDirectory.dir("reports/scan-build"))
* analyzer.set("/usr/bin/clang++")
* parallelJobs.set(4)
* }
* ```
*/
class ScanBuildPlugin : Plugin<Project> {
override fun apply(project: Project) {
// Create the extension
val extension = project.extensions.create(
"scanBuild",
ScanBuildExtension::class.java,
project
)
// Create the task after project evaluation
project.afterEvaluate {
createScanBuildTask(project, extension)
}
}
private fun createScanBuildTask(project: Project, extension: ScanBuildExtension) {
// Only create the task on Linux (scan-build is typically Linux-only in CI)
if (PlatformUtils.currentPlatform != Platform.LINUX) {
project.logger.info("Skipping scanBuild task - only available on Linux")
return
}
// Check if scan-build is available
if (!isScanBuildAvailable()) {
project.logger.warn("scan-build not found in PATH - scanBuild task will fail if executed")
}
val makefileDir = extension.makefileDir.get().asFile
val outputDir = extension.outputDir.get().asFile
val analyzer = extension.analyzer.get()
val parallelJobs = extension.parallelJobs.get()
val makeTargets = extension.makeTargets.get()
val scanBuildTask = project.tasks.register("scanBuild", Exec::class.java)
scanBuildTask.configure {
group = "verification"
description = "Run clang static analyzer via scan-build"
workingDir(makefileDir)
// Build command line as a single list to avoid vararg ambiguity
val command = mutableListOf(
"scan-build",
"-o", outputDir.absolutePath,
"--force-analyze-debug-code",
// core.StackAddressEscape fires on the intentional setjmp/longjmp pattern in
// StackWalker::walkVM: the jmp_buf address is stored in vm_thread->exception()
// for the duration of the stack walk and is always restored before the function
// returns. The analyzer cannot prove the lifetime is safe, but we can.
"-disable-checker", "core.StackAddressEscape",
"--use-analyzer", analyzer,
"make", "-j$parallelJobs"
)
command.addAll(makeTargets)
commandLine(command)
// Ensure output directory exists
doFirst {
outputDir.mkdirs()
}
}
}
private fun isScanBuildAvailable(): Boolean {
return try {
val process = ProcessBuilder("which", "scan-build")
.redirectErrorStream(true)
.start()
process.waitFor() == 0
} catch (e: Exception) {
false
}
}
}