Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame^] | 1 | ext.createGenerateResourcesTask = { name, prefix, namespace, project -> |
| 2 | def generatedOutputDir = file("$buildDir/generated/$name/cpp") |
| 3 | |
| 4 | def inputDir = file("$projectDir/src/$name/native/resources") |
| 5 | |
| 6 | if (!prefix.isEmpty()) prefix += '_' |
| 7 | |
| 8 | def task = project.tasks.create("generateResources-$name") { |
| 9 | outputs.dir generatedOutputDir |
| 10 | inputs.dir inputDir |
| 11 | |
| 12 | doLast { |
| 13 | generatedOutputDir.mkdirs() |
| 14 | inputDir.eachFile { inputFile -> |
| 15 | if (inputFile.name.startsWith('.')) return |
| 16 | def fileBytes = inputFile.bytes |
| 17 | def outputFile = file("$generatedOutputDir/${inputFile.name}.cpp") |
| 18 | def funcName = "GetResource_" + inputFile.name.replaceAll('[^a-zA-Z0-9]', '_') |
| 19 | outputFile.withWriter { out -> |
| 20 | def inputBytes = inputFile.bytes |
| 21 | out.print '''#include <stddef.h> |
| 22 | #include <wpi/StringRef.h> |
| 23 | extern "C" { |
| 24 | static const unsigned char contents[] = { ''' |
| 25 | |
| 26 | for (int i = 0; i < fileBytes.size(); i++) { |
| 27 | out.print String.format('0x%02x', (int) fileBytes[i] & 0xff) |
| 28 | out.print ', ' |
| 29 | } |
| 30 | out.println """}; |
| 31 | const unsigned char* ${prefix}${funcName}(size_t* len) { |
| 32 | *len = ${fileBytes.size()}; |
| 33 | return contents; |
| 34 | } |
| 35 | }""" |
| 36 | if (!namespace.isEmpty()) { |
| 37 | out.println "namespace ${namespace} {" |
| 38 | } |
| 39 | out.println """wpi::StringRef ${funcName}() { |
| 40 | return wpi::StringRef(reinterpret_cast<const char*>(contents), ${fileBytes.size()}); |
| 41 | }""" |
| 42 | if (!namespace.isEmpty()) { |
| 43 | out.println '}' |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | return task |
| 50 | } |