-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Written task for compiling Napi (NodeJS wrapper) and made it compile …
…for Linux properly. Skip supporting NodeJS on x86 Linux for now, because it is not shipped as 32 bit anyways. Successfully built Windows packages for NodeJS.
- Loading branch information
Showing
18 changed files
with
471 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
.../src/main/kotlin/com/github/animeshz/keyboard_mouse/native_compile/NapiCompilationTask.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.github.animeshz.keyboard_mouse.native_compile | ||
|
||
import org.apache.tools.ant.taskdefs.condition.Os | ||
import org.gradle.api.DefaultTask | ||
import org.gradle.api.GradleException | ||
import org.gradle.api.tasks.Input | ||
import org.gradle.api.tasks.TaskAction | ||
import org.gradle.api.tasks.options.Option | ||
import java.io.ByteArrayOutputStream | ||
import javax.inject.Inject | ||
|
||
/** | ||
* For building shared libraries out of C/C++ sources for NodeJS | ||
*/ | ||
open class NapiCompilationTask @Inject constructor( | ||
private val target: Target | ||
) : DefaultTask() { | ||
init { | ||
group = "nativeCompilation" | ||
} | ||
|
||
@get:Input | ||
@set:Option(option = "verbose", description = "Sets verbosity of output.") | ||
var isVerbose: Boolean = false | ||
|
||
@TaskAction | ||
fun run() { | ||
project.checkDockerInstallation() | ||
|
||
val tmpVar = project.file(".").absolutePath | ||
val path = if (Os.isFamily(Os.FAMILY_WINDOWS)) { | ||
"/run/desktop/mnt/host/${tmpVar[0].toLowerCase()}${tmpVar.substring(2 until tmpVar.length).replace('\\', '/')}" | ||
} else { | ||
tmpVar | ||
} | ||
|
||
val work: () -> Pair<Int, String> = { | ||
ByteArrayOutputStream().use { | ||
project.exec { | ||
commandLine( | ||
"docker", | ||
"run", | ||
"--rm", | ||
"-v", | ||
"$path:/work/project", | ||
target.dockerImage, | ||
"bash", | ||
"-c", | ||
"mkdir -p \$WORK_DIR/project/build/napi && " + | ||
"cmake-js compile --CDARCH=${target.arch} --arch=${target.arch} ${if (isVerbose) "-l=verbose " else ""} -d=\$WORK_DIR/project/src/jsMain/cpp/${target.os} && " + | ||
"cp -rf \$WORK_DIR/project/src/jsMain/cpp/${target.os}/build/{,Release/}KeyboardKt${target.os.capitalize()}${target.arch}.node \$WORK_DIR/project/build/napi 2>/dev/null || : && " + | ||
"rm -rf \$WORK_DIR/project/src/jsMain/cpp/${target.os}/build" | ||
) | ||
|
||
isIgnoreExitValue = true | ||
standardOutput = System.out | ||
errorOutput = it | ||
}.exitValue to it.toString() | ||
} | ||
} | ||
var (exit, error) = work() | ||
|
||
// Fix non-daemon docker on Docker for Windows | ||
val nonDaemonError = "docker: error during connect: This error may indicate that the docker daemon is not running." | ||
if (Os.isFamily(Os.FAMILY_WINDOWS) && error.startsWith(nonDaemonError)) { | ||
project.exec { commandLine("C:\\Program Files\\Docker\\Docker\\DockerCli.exe", "-SwitchDaemon") }.assertNormalExitValue() | ||
|
||
do { | ||
Thread.sleep(500) | ||
val result = work() | ||
exit = result.first | ||
error = result.second | ||
} while (error.startsWith(nonDaemonError)) | ||
} | ||
|
||
System.err.println(error) | ||
if (exit != 0) throw GradleException("An error occured while running the command, see the stderr for more details.") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
...site-build-src/src/main/kotlin/com/github/animeshz/keyboard_mouse/native_compile/Utils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.github.animeshz.keyboard_mouse.native_compile | ||
|
||
import org.apache.tools.ant.taskdefs.condition.Os | ||
import org.gradle.api.Project | ||
import kotlin.system.exitProcess | ||
|
||
internal fun Project.checkDockerInstallation() { | ||
println("Checking docker installation") | ||
|
||
val exit = exec { | ||
commandLine(if (Os.isFamily(Os.FAMILY_WINDOWS)) listOf("cmd", "/c", "where", "docker") else listOf("which", "docker")) | ||
isIgnoreExitValue = true | ||
}.exitValue | ||
if (exit != 0) { | ||
println("Please install docker before running this task") | ||
exitProcess(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.