Skip to content

Commit ebc6a38

Browse files
committed
fix(compiletime) reorganise code logic of compileCpp task
1 parent 3dbf104 commit ebc6a38

File tree

1 file changed

+84
-2
lines changed

1 file changed

+84
-2
lines changed

build.gradle.kts

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import org.gradle.kotlin.dsl.cpp
2+
import java.nio.file.Files
3+
import java.nio.file.Paths
4+
import java.util.Optional
25

36
plugins {
47
kotlin("jvm") version "1.8.0"
@@ -79,7 +82,55 @@ tasks.register<JavaCompile>("generateJni") {
7982
tasks.register<Exec>("compileCpp") {
8083
group = "build"
8184
workingDir(cppSourceDir)
82-
commandLine("g++", "-I", "${System.getProperty("java.home")}/include", "-I", "${System.getProperty("java.home")}/include/win32", "-I", "$buildDir/generated/jni", "-shared", "-o", "$buildDir/dll/native.dll", "*.cpp")
85+
86+
val osOptional = OperatingSystem.current()
87+
if (osOptional.isEmpty)
88+
throw RuntimeException("Failed to compile JNI source due to unsupported operating system.")
89+
val os = osOptional.get()
90+
91+
fun dirName(): String {
92+
return when (os) {
93+
OperatingSystem.WINDOWS -> "win32"
94+
OperatingSystem.MACOS -> "darwin"
95+
OperatingSystem.LINUX -> "linux"
96+
}
97+
}
98+
fun extName(): String {
99+
return when (os) {
100+
OperatingSystem.WINDOWS -> ".dll"
101+
OperatingSystem.MACOS -> ".dylib"
102+
OperatingSystem.LINUX -> ".so"
103+
}
104+
}
105+
106+
val outputDirStr = "$buildDir/dll"
107+
val outputDir = File(outputDirStr)
108+
if (!outputDir.exists()) {
109+
outputDir.mkdir()
110+
}
111+
112+
val cmdArgs = ArrayList<String>()
113+
cmdArgs.addAll(listOf(
114+
"g++",
115+
"-fPIC",
116+
"-I",
117+
"${System.getProperty("java.home")}/include",
118+
"-I",
119+
"${System.getProperty("java.home")}/include/${dirName()}",
120+
"-I",
121+
"$buildDir/generated/jni",
122+
"-shared",
123+
"-o",
124+
"$outputDirStr/native${extName()}"
125+
))
126+
127+
val cppPath = cppSourceDir.toPath()
128+
Files.walk(cppPath)
129+
.map { it.toAbsolutePath().toString() }
130+
.filter { it.endsWith(".cpp") }
131+
.forEach { cmdArgs.add(it) }
132+
133+
commandLine(cmdArgs)
83134
}
84135

85136
kotlin {
@@ -93,4 +144,35 @@ tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
93144

94145
application {
95146
mainClass.set("top.mcfpp.MCFPPKt")
96-
}
147+
}
148+
149+
enum class OperatingSystem {
150+
WINDOWS,
151+
LINUX,
152+
MACOS;
153+
companion object {
154+
private fun isWindows(): Boolean {
155+
val osName = System.getProperty("os.name")
156+
return osName != null && osName.startsWith("Windows")
157+
}
158+
159+
private fun isMacOs(): Boolean {
160+
val osName = System.getProperty("os.name")
161+
return osName != null && osName.startsWith("Mac")
162+
}
163+
164+
private fun isLinux(): Boolean {
165+
val osName = System.getProperty("os.name")
166+
return osName != null && osName.startsWith("Linux")
167+
}
168+
169+
fun current(): Optional<OperatingSystem> {
170+
return when {
171+
isWindows() -> Optional.of(WINDOWS)
172+
isMacOs() -> Optional.of(MACOS)
173+
isLinux() -> Optional.of(LINUX)
174+
else -> Optional.empty()
175+
}
176+
}
177+
}
178+
}

0 commit comments

Comments
 (0)