Skip to content

Commit

Permalink
Added support for building C/C++ libraries via bob
Browse files Browse the repository at this point in the history
  • Loading branch information
JCash committed Nov 22, 2023
1 parent 384e378 commit f66e7a0
Showing 1 changed file with 63 additions and 17 deletions.
80 changes: 63 additions & 17 deletions server/src/main/java/com/defold/extender/Extender.java
Original file line number Diff line number Diff line change
Expand Up @@ -976,14 +976,12 @@ public boolean accept(File pathname) {
return outputFiles;
}

private void buildExtension(File manifest, Map<String, Object> manifestContext) throws IOException, InterruptedException, ExtenderException {
private List<File> buildExtensionInternal(File manifest, Map<String, Object> manifestContext, List<File> srcDirs, File libraryOut) throws IOException, InterruptedException, ExtenderException {
LOGGER.info("buildExtension");

File extDir = manifest.getParentFile();

// Gather all the C++ files
List<File> srcDirs = new ArrayList<>();
srcDirs.add(new File(extDir, FOLDER_COMMON_SRC));
srcDirs.add(new File(extDir, FOLDER_ENGINE_SRC));
// Gather all the source files
List<File> srcFiles = ExtenderUtil.listFiles(srcDirs, platformConfig.sourceRe);

// Added in 1.4.9
Expand All @@ -1007,17 +1005,39 @@ private void buildExtension(File manifest, Map<String, Object> manifestContext)
objs.addAll(compileExtensionSourceFiles(extDir, manifestContext, srcFiles));

// Create c++ library
File lib = null;
if (platformConfig.writeLibPattern != null) {
File lib;
if (libraryOut != null)
lib = libraryOut;
else
lib = createBuildFile(String.format(platformConfig.writeLibPattern, manifestContext.get("extension_name") + "_" + getNameUUID()));
} else {
lib = uniqueTmpFile("lib", ".a"); // Deprecated, remove in a few versions
}

List<File> outputFiles = new ArrayList<>();
outputFiles.add(lib);

Map<String, Object> context = createContext(manifestContext);
context.put("tgt", lib);
context.put("objs", objs);
String command = templateExecutor.execute(platformConfig.libCmd, context);
processExecutor.execute(command);

return outputFiles;
}

private List<File> buildExtension(File manifest, Map<String, Object> manifestContext) throws IOException, InterruptedException, ExtenderException {
LOGGER.info("buildExtension");
File extDir = manifest.getParentFile();
List<File> srcDirs = new ArrayList<>();
srcDirs.add(new File(extDir, FOLDER_COMMON_SRC));
srcDirs.add(new File(extDir, FOLDER_ENGINE_SRC));
return buildExtensionInternal(manifest, manifestContext, srcDirs, null);
}

private List<File> buildLibrary(File manifest, Map<String, Object> manifestContext) throws IOException, InterruptedException, ExtenderException {
LOGGER.info("buildLibrary");
List<File> srcDirs = new ArrayList<>();
srcDirs.add(uploadDirectory);
String libName = String.format(platformConfig.writeLibPattern, manifestContext.get("extension_name"));
return buildExtensionInternal(manifest, manifestContext, srcDirs, new File(buildDirectory, libName));
}

private List<File> buildPipelineExtension(File manifest, Map<String, Object> manifestContext) throws IOException, InterruptedException, ExtenderException {
Expand Down Expand Up @@ -2073,6 +2093,34 @@ private boolean shouldBuildEngine() {
private boolean shouldBuildPlugins() {
return shouldBuildArtifact("plugins");
}
private boolean shouldBuildLibrary() {
return shouldBuildArtifact("library");
}

private List<File> buildLibraries() throws ExtenderException {
System.out.printf("buildLibrary\n");

if (!shouldBuildLibrary()) {
return new ArrayList<>();
}
LOGGER.info("Building library for platform {} with extension source {}", platform, uploadDirectory);

List<File> outputFiles = new ArrayList<>();
try {
List<String> symbols = getSortedKeys(manifestConfigs.keySet());
for (String extensionSymbol : symbols) {
Map<String, Object> extensionContext = manifestConfigs.get(extensionSymbol);
File manifest = manifestFiles.get(extensionSymbol);

// TODO: Thread this step
outputFiles.addAll(buildLibrary(manifest, extensionContext));
}

return outputFiles;
} catch (IOException | InterruptedException e) {
throw new ExtenderException(e, processExecutor.getOutput());
}
}

private List<File> buildEngine() throws ExtenderException {
if (!shouldBuildEngine()) {
Expand All @@ -2090,7 +2138,7 @@ private List<File> buildEngine() throws ExtenderException {
File manifest = manifestFiles.get(extensionSymbol);

// TODO: Thread this step
buildExtension(manifest, extensionContext);
outputFiles.addAll(buildExtension(manifest, extensionContext));
}

File resourceFile = null;
Expand Down Expand Up @@ -2152,11 +2200,6 @@ private List<File> buildPipelinePlugin() throws ExtenderException {
}
}

private void putLog(String msg) {
System.out.printf(msg);
processExecutor.putLog(msg);
}

private List<File> copyAndroidJniFolders(String platform) throws ExtenderException {
List<File> jniFolders = getAndroidJniFolders(platform);
if (jniFolders.isEmpty()) {
Expand Down Expand Up @@ -2441,7 +2484,10 @@ List<File> build() throws ExtenderException {
if (platform.endsWith("android")) {
outputFiles.addAll(buildAndroid(platform));
}
outputFiles.addAll(buildEngine());
if (shouldBuildLibrary())
outputFiles.addAll(buildLibraries());
else
outputFiles.addAll(buildEngine());
outputFiles.addAll(buildPipelinePlugin());
File log = writeLog();
if (log.exists()) {
Expand Down

0 comments on commit f66e7a0

Please sign in to comment.