Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for building C/C++ libraries via bob #326

Merged
merged 1 commit into from
Nov 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly same as before, but can override the output library file

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"));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making it easy to control the library name from bob

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 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a shortened copy of buildEngine() without the linker step.

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