11import org.apache.tools.ant.taskdefs.condition.Os
22
3+ // This is the directory the library headers will be generated in
34ext. generatedHeadersDir = new File (buildDir, " generatedHeaders" )
45if (! ext. generatedHeadersDir. exists() && ! ext. generatedHeadersDir. mkdirs()) {
56 throw new GradleException (" Failed to create directory ${ ext.generatedHeadersDir.absolutePath} " )
67}
78
9+ // This is the directory where the native binaries can be found after compilation
810ext. nativeBinariesDir = new File (buildDir, " nativeBinaries" )
911if (! ext. nativeBinariesDir. exists() && ! ext. nativeBinariesDir. mkdirs()) {
1012 throw new GradleException (" Failed to create directory ${ ext.nativeBinariesDir.absolutePath} " )
1113}
1214
15+ // Properties determining the build type
1316def isExternalBuild = false
1417def ciBuild = project. hasProperty(" CI" ) && Boolean . parseBoolean(project. property(" CI" ). toString())
1518
1619if (! project. hasProperty(" nativeBinaryExternalDir" )) {
17- if (Os . isFamily(Os . FAMILY_WINDOWS )) {
20+ // The binaries will possibly be need to be built locally
21+ if (Os . isFamily(Os . FAMILY_WINDOWS )) {
1822 // WARNING, this assumes CMake uses visual studio
1923 // For GCC or Clang the output should be $nativeBinariesDir/libultralight-java.dll
2024 ext. nativeBinaries = Arrays . asList(new File (nativeBinariesDir, " Debug/ultralight-java.dll" ))
21- } else if (Os . isFamily(Os . FAMILY_MAC )) {
25+ } else if (Os . isFamily(Os . FAMILY_MAC )) {
2226 ext. nativeBinaries = Arrays . asList(new File (nativeBinariesDir, " libultralight-java.dylib" ))
23- } else if (Os . isFamily(Os . FAMILY_UNIX )) {
27+ } else if (Os . isFamily(Os . FAMILY_UNIX )) {
2428 ext. nativeBinaries = Arrays . asList(new File (nativeBinariesDir, " libultralight-java.so" ))
2529 } else {
26- if (! project. hasProperty(" nativeBinaryLibrary" )) {
30+ if (! project. hasProperty(" nativeBinaryLibrary" )) {
31+ // Unknown OS and missing binary, bail out
2732 throw new GradleException (" Unable to determine native output library, " +
2833 " pass -PnativeBinaryLibrary=/path/to/native/binary to gradle" )
2934 } else {
35+ // Unknown OS but the user specified the output binary
3036 ext. nativeBinaries = [new File (project. property(" nativeBinaryLibrary" ). toString())]
3137 }
3238 }
3339} else {
40+ // The binaries have been built by an external process and can be used now
3441 def nativeBinaryExternalDir = file(project. property(" nativeBinaryExternalDir" ). toString())
3542 if (! nativeBinaryExternalDir. exists()) {
43+ // The directory where the binaries are supposed to be is missing, bail out
3644 throw new GradleException (" nativeBinaryExternalDir ${ nativeBinaryExternalDir.absolutePath} does not exist" )
3745 }
3846
3947 ext. nativeBinaries = new ArrayList<File > ()
4048
49+ // Collect all native binaries built externally
4150 for (def file in nativeBinaryExternalDir. listFiles()) {
4251 println " Found native binary ${ file.absolutePath} "
4352 ext. nativeBinaries. add(file)
4453 }
4554
55+ // Flag the build
4656 isExternalBuild = true
4757}
4858
4959project(" :ultralight-java-base" ). afterEvaluate { javaProject ->
5060 project(" :ultralight-java-native" ). afterEvaluate { nativeProject ->
5161 if (! isExternalBuild) {
62+ // If the binaries have not been built externally, building the java project requires
63+ // building OS dependent binary locally
5264 javaProject. tasks[" processResources" ]. dependsOn(nativeProject. tasks[" build" ])
5365 }
5466 }
5567}
5668
69+ /**
70+ * Helper function to retrieve the file extensions from a file.
71+ *
72+ * @param file The file to retrieve the extension from
73+ * @return The extension of the file, or an empty string, if the file does not have an extension
74+ */
5775static def getExtension (File file ) {
5876 String fileName = file. getName()
5977
@@ -64,16 +82,30 @@ static def getExtension(File file) {
6482 }
6583}
6684
85+ /**
86+ * Helper function to retrieve the absolute path of a file without its extension.
87+ *
88+ * @param file The file to retrieve the path from
89+ * @return The absolute path of the file without its extension, or the path itself,
90+ * if the file does not have an extension
91+ */
6792static def getPathWithoutExtension (File file ) {
68- String fileName = file. getAbsolutePath()
93+ String path = file. getAbsolutePath()
6994
70- if (fileName . lastIndexOf(" ." ) != -1 && fileName . lastIndexOf(" ." ) != 0 ) {
71- return fileName . substring(0 , fileName . lastIndexOf(" ." ))
95+ if (path . lastIndexOf(" ." ) != -1 && path . lastIndexOf(" ." ) != 0 ) {
96+ return path . substring(0 , path . lastIndexOf(" ." ))
7297 } else {
73- return " "
98+ return path
7499 }
75100}
76101
102+ /**
103+ * Processes a binary file by calculating its new path and moving it if desired.
104+ *
105+ * @param binary The binary to process
106+ * @param doMove If {@code true }, the file will be moved to the new path
107+ * @return The new file of the binary
108+ */
77109def processBinary (File binary , boolean doMove ) {
78110 String extension = getExtension(binary)
79111 String path = getPathWithoutExtension(binary)
@@ -89,12 +121,16 @@ def processBinary(File binary, boolean doMove) {
89121}
90122
91123if (ciBuild) {
124+ // We are running a CI build
92125 def ciDir = file(" ci" )
93126 if (! ciDir. exists() && ! ciDir. mkdirs()) {
127+ // Failed to create a necessary directory, bail out
94128 throw new GradleException (" Failed to create directory ${ ciDir.absolutePath} " )
95129 }
96130
97131 new File (ciDir, " binaries" ). withWriter {
132+ // Write a file with paths to the native binaries, required by our script
133+ // at .github/workflows/ci.yml to create an archive with natives
98134 for (File binary in ext. nativeBinaries) {
99135 it. write(processBinary(binary, false ). absolutePath)
100136 }
@@ -104,13 +140,15 @@ if(ciBuild) {
104140
105141 task moveNativeBinaries {
106142 doLast {
107- for (File binary in binaries) {
143+ for (File binary in binaries) {
144+ // Move the binaries to their new locations
108145 processBinary(binary, true )
109146 }
110147 }
111148 }
112149
113150 project(" :ultralight-java-native" ). afterEvaluate { nativeProject ->
151+ // Make sure the binaries are moved after the build
114152 nativeProject. tasks[" build" ]. finalizedBy(moveNativeBinaries)
115153 }
116154}
0 commit comments