Skip to content

Commit

Permalink
Including enso_parser library in the engine distribution (#3842)
Browse files Browse the repository at this point in the history
Make sure `libenso_parser.so`, `.dll` or `.dylib` are packaged and included when `sbt buildEngineDistribution`.

# Important Notes
There was [a discussion](https://discord.com/channels/401396655599124480/1036562819644141598) about proper location of the library. It was concluded that _"there's no functional difference between a dylib and a jar."_ and as such the library is placed in `component` folder.

Currently the old parser is still used for parsing. This PR just integrates the build system changes and makes us ready for smooth flipping of the parser in the future as part of #3611.
  • Loading branch information
JaroslavTulach authored Nov 2, 2022
1 parent 418120f commit 85f71cb
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 16 deletions.
7 changes: 6 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,12 @@ val generateRustParserLib =
allLibs.isEmpty ||
(`syntax-rust-definition` / generateRustParserLib).inputFileChanges.hasChanges
) {
Seq("cargo", "build", "-p", "enso-parser-jni") !
val os = System.getProperty("os.name")
if (os.startsWith("Mac")) {
Seq("cargo", "build", "-p", "enso-parser-jni", "--target", "x86_64-apple-darwin") !
} else {
Seq("cargo", "build", "-p", "enso-parser-jni") !
}
}
FileTreeView.default.list(Seq(libGlob)).map(_._1.toFile)
}
Expand Down
54 changes: 39 additions & 15 deletions lib/rust/parser/generate-java/java/org/enso/syntax2/Parser.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,58 @@
package org.enso.syntax2;

import org.enso.syntax2.Message;
import java.io.File;
import java.net.URISyntaxException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;

public final class Parser implements AutoCloseable {
static {
String os = System.getProperty("os.name");
File dir = new File(".").getAbsoluteFile();
for (; ; ) {
File parser;
if (os.startsWith("Mac")) {
parser = new File(dir, "target/rust/debug/libenso_parser.dylib");
} else if (os.startsWith("Windows")) {
parser = new File(dir, "target/rust/debug/enso_parser.dll");
} else {
parser = new File(dir, "target/rust/debug/libenso_parser.so");
String name;
if (os.startsWith("Mac")) {
name = "libenso_parser.dylib";
} else if (os.startsWith("Windows")) {
name = "enso_parser.dll";
} else {
name = "libenso_parser.so";
}

File parser = null;
try {
var whereAmI = Parser.class.getProtectionDomain().getCodeSource().getLocation();
File dir = new File(whereAmI.toURI()).getParentFile();
parser = new File(dir, name);
System.load(parser.getAbsolutePath());
} catch (URISyntaxException | LinkageError e) {
System.err.println("Cannot load " + parser);
File root = new File(".").getAbsoluteFile();
if (!searchFromDirToTop(e, root, "target", "rust", "x86_64-apple-darwin", "debug", name)
&& !searchFromDirToTop(e, root, "target", "rust", "debug", name)) {
throw new IllegalStateException("Cannot load parser from " + parser, e);
}
}
}

private static boolean searchFromDirToTop(Throwable chain, File root, String... names) {
while (root != null) {
var parser = root;
for (var e : names) {
parser = new File(parser, e);
}
try {
System.load(parser.getAbsolutePath());
break;
} catch (LinkageError e) {
dir = dir.getParentFile();
if (dir == null) {
throw e;
System.err.println("Succeeded loading " + parser.getAbsolutePath());
return true;
} catch (LinkageError err) {
while (chain.getCause() != null) {
chain = chain.getCause();
}
chain.initCause(err);
root = root.getParentFile();
}
}
return false;
}

private long state;
Expand Down
13 changes: 13 additions & 0 deletions project/DistributionPackage.scala
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,19 @@ object DistributionPackage {
distributionRoot / "component",
cacheFactory.make("engine-jars")
)
val os = System.getProperty("os.name")
val parser = "target/rust/debug/" + (if (os.startsWith("Mac")) {
"x86_64-apple-darwin/libenso_parser.dylib"
} else if (os.startsWith("Windows")) {
"enso_parser.dll"
} else {
"libenso_parser.so"
})
copyFilesIncremental(
Seq(file(parser)),
distributionRoot / "component",
cacheFactory.make("engine-parser-library")
)

(distributionRoot / "editions").mkdirs()
Editions.writeEditionConfig(
Expand Down

0 comments on commit 85f71cb

Please sign in to comment.