-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.gradle
163 lines (143 loc) · 4.6 KB
/
build.gradle
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
import org.gradle.nativeplatform.platform.internal.DefaultNativePlatform
plugins {
id 'java-library'
id 'maven-publish'
}
group = 'no.nr'
version = '1.0'
repositories {
mavenCentral()
}
dependencies {
implementation("io.github.bonede:tree-sitter:0.22.6")
testImplementation(platform(libs.junit.bom))
testImplementation("org.junit.jupiter:junit-jupiter")
// https://stackoverflow.com/a/77605392
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}
test {
useJUnitPlatform()
}
def libName = "tree-sitter-ng-dbspec"
java {
withJavadocJar()
withSourcesJar()
}
processResources {
dependsOn("buildNative")
}
sourcesJar {
// Native binaries are not source files
exclude("lib")
}
publishing {
repositories {
maven {
name = "GitHubPackages"
url = "https://maven.pkg.github.com/immortalvm/tree-sitter-ng-dbspec"
credentials {
username = System.getenv("GITHUB_ACTOR")
password = System.getenv("GITHUB_TOKEN")
}
}
}
publications {
// IntelliJ warnings can be ignored:
// https://youtrack.jetbrains.com/issue/IDEA-338343/Publishing-DSL-has-various-IDE-warnings-and-errors-with-Groovy-DSL
maven(MavenPublication) {
from components.java
pom {
name = libName
url = "https://github.com/immortalvm/tree-sitter-ng-dbspec"
description = "DbSpec parser bindings"
licenses {
license {
name = "MIT"
}
}
scm {
connection = "scm:git:https://github.com/immortalvm/tree-sitter-ng-dbspec.git"
developerConnection = "scm:git:https://github.com/immortalvm/tree-sitter-ng-dbspec.git"
url = "https://github.com/immortalvm/tree-sitter-ng-dbspec"
}
}
}
}
}
// The rest of this file is taken from https://github.com/bonede/tree-sitter-ng with some adjustments.
static String libExt(String target){
if(target.contains("windows")){
return "dll"
}else if(target.contains("linux")){
return "so"
}else if(target.contains("macos")){
return "dylib"
}else{
throw new GradleException("Does not support $target")
}
}
static String jniMdInclude(String target) {
if (target.contains("windows")) {
return "win32"
} else if (target.contains("linux")) {
return "linux"
} else if (target.contains("macos")) {
return "darwin"
} else {
throw new GradleException("Does not support $target")
}
}
tasks.register("buildNative") {
group = "build"
description = "Build parser native modules"
def pd = layout.projectDirectory
def jniSrcDir = pd.dir("src/main/c")
def jniOutDir = pd.dir("src/main/resources/lib")
def jniCFile = jniSrcDir.file("no_nr_TreeSitterDbspec.c")
def libSrcDir = pd.dir("tree-sitter-dbspec/src")
def jniInclude = pd.dir("include/jni")
def srcFiles = fileTree(libSrcDir) {
include("parser.c")
include("scanner.c")
}.toList()
inputs.files(srcFiles)
inputs.file(jniCFile)
inputs.file(pd.file("gradle.properties"))
def targets = ["x86_64-windows", "x86_64-macos", "x86_64-linux-gnu", "aarch64-linux-gnu", "aarch64-macos"]
def outputFiles = targets.collect() { t ->
String ext = libExt(t)
jniOutDir.file("$t-$libName.$ext")
}
outputs.files(outputFiles)
doLast {
mkdir(jniOutDir)
// This is a standard way to "zip" two lists in using Groovy.
[targets, outputFiles].transpose().each { String target, RegularFile jniOutFile ->
def cmd = [
DefaultNativePlatform.currentOperatingSystem.windows ? "zig.exe" : "zig",
"c++",
"-g0",
// No effect:
// "-Wno-dll-attribute-on-redeclaration",
"-shared",
"-target", target,
"-I", libSrcDir,
"-I", jniInclude,
"-I", jniInclude.dir(jniMdInclude(target)),
"-o", jniOutFile,
jniCFile,
]
cmd.addAll(srcFiles)
exec {
workingDir(jniSrcDir)
commandLine(cmd)
}
}
// Remove Windows debug files
def files = fileTree(jniOutDir) {
include("**/*.pdb")
include("**/*.lib")
}
delete(files)
}
}