forked from ballerina-platform/nballerina
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a "release" build target to jni.llvm (ballerina-platform#1241)
* Fix incremental builds Cleanup build script Cleaned resource config Update the README Add Ballerina config for Aarch64 Refactor Makefile Add dist targets for jar and native compilers * Fix jar only build not including runtime * Implement cross compilation * Allow running the script from any directory * Fix incremental builds * Change gzip to xz * Fix target * Change the cross compiler to gcc * Fix file name * Fix aarch64 builds Some of the cases where we were using GEPs don't work in aarch64 (most likely due to clang packing values differently). Workaround this by adding inlinable functions to access values * Fix build scripts to use only sh * Fix build script search depth * Fix runner for OSX * Fix naming * Revert "Fix aarch64 builds" This reverts commit f594e86. * Change compilation scripts to use bash * Fix bash scripts
- Loading branch information
1 parent
18d42be
commit 9c70a7c
Showing
13 changed files
with
287 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
[package] | ||
org = "wso2" | ||
name = "nballerina" | ||
version = "0.1.0" | ||
[build-options] | ||
observabilityIncluded = false | ||
|
||
[[platform.java11.dependency]] | ||
path = "target/platform-libs/javacpp-1.5.9.jar" | ||
|
||
[[platform.java11.dependency]] | ||
path = "target/platform-libs/javacpp-platform-1.5.9.jar" | ||
|
||
[[platform.java11.dependency]] | ||
path = "target/platform-libs/javacpp-1.5.9-macosx-arm64.jar" | ||
|
||
[[platform.java11.dependency]] | ||
path = "target/platform-libs/llvm-16.0.4-1.5.9.jar" | ||
|
||
[[platform.java11.dependency]] | ||
path = "target/platform-libs/llvm-platform-16.0.4-1.5.9.jar" | ||
|
||
[[platform.java11.dependency]] | ||
path = "target/platform-libs/llvm-16.0.4-1.5.9-macosx-arm64.jar" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
[package] | ||
org = "wso2" | ||
name = "nballerina" | ||
version = "0.1.0" | ||
[build-options] | ||
observabilityIncluded = false | ||
|
||
[[platform.java11.dependency]] | ||
path = "target/platform-libs/javacpp-1.5.9.jar" | ||
|
||
[[platform.java11.dependency]] | ||
path = "target/platform-libs/javacpp-platform-1.5.9.jar" | ||
|
||
[[platform.java11.dependency]] | ||
path = "target/platform-libs/javacpp-1.5.9-linux-arm64.jar" | ||
|
||
[[platform.java11.dependency]] | ||
path = "target/platform-libs/llvm-16.0.4-1.5.9.jar" | ||
|
||
[[platform.java11.dependency]] | ||
path = "target/platform-libs/llvm-platform-16.0.4-1.5.9.jar" | ||
|
||
[[platform.java11.dependency]] | ||
path = "target/platform-libs/llvm-16.0.4-1.5.9-linux-arm64.jar" |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,25 @@ | ||
# Usage | ||
## Build the jni.llvm | ||
1. Run `./gradlew copyDependencies` to download and copy dependencies to the local `target/platform-libs` directory. (This needs to be done only once for the first build) | ||
2. Run `bal build` to build the project | ||
|
||
## Build nBallerina complier using JNI | ||
Run `./gradlew testBuild` | ||
## 1 Build nBallerina complier using JNI | ||
Run `make`. | ||
|
||
This will create new ballerina package in `testbuild` directory the root directory where | ||
1. Change all the imports of `print.llvm` to `jni.llvm` | ||
2. Remove the `print.llvm` module and insert `jni.llvm` module instead | ||
3. Run `bal build` on the new ballerina package | ||
This will create new ballerina package in `testbuild` directory by, | ||
1. Replace `print.llvm` with `jni.llvm`. | ||
2. Run `bal build` on the new package. | ||
3. Add the inline runtime (`balrt_inline.bc`) into the resulting `jar` file. | ||
4. Generate a native executable using [GraalVM](https://www.graalvm.org/). | ||
|
||
To clean this build either delete the `testbuild` directory or run `./gradlew cleanTestBuild`. | ||
Resulting compiler includes the necessary parts of the LLVM tool-chain (using [Bytedeco's LLVM presets](https://github.com/bytedeco/javacpp-presets/tree/master/llvm) as well as the inline runtime (`balrt_inline.bc`) and can compile Ballerina source files into object files directly. | ||
|
||
## Compare llvm ir generated against print.llvm | ||
Run `./gradlew compareTestCases` | ||
# Sample code | ||
``` | ||
import llvm_jni.llvm; | ||
import ballerina/io; | ||
public function main() { | ||
llvm:Context context = new; | ||
llvm:Builder builder = context.createBuilder(); | ||
llvm:Module m = context.createModule(); | ||
llvm:Function mainFunction = m.addFunction("main", {returnType: "void", paramTypes: ["i64", "i1"]}); | ||
llvm:BasicBlock initBlock = mainFunction.appendBasicBlock(); | ||
builder.positionAtEnd(initBlock); | ||
builder.ret(llvm:constInt("i64",0)); | ||
io:println(m.toString()); | ||
checkpanic m.toFile("test.ll"); | ||
} | ||
``` | ||
### 1.1 Pruning unwanted LLVM dependencies | ||
When building the native executable as an intermediate step we crate a Java version of the compiler that can run in multiple operating systems. For this we include LLVM tool-chain for each of those operating systems. However this is not needed when building a single native executable targeting a single operating system/architecture combination. To prune unnecessary dependencies you can pass in a `Ballerina.toml` file with only the dependencies needed for your operating system. For example in a Linux AMD64 machine we can use `make BAL_CONFIG=BallerinaLinuxAMD64.toml`. | ||
|
||
### 1.2 Creating distribution tar file | ||
Run `make dist` | ||
|
||
This will create the release artifacts in `build` directory. There are two possible versions | ||
1. If you use `ARCH=amd64` (this is the default) it will generate a native executable with support for cross compiling to AArch64, along with AMD64 and AArch64 versions of the runtime. | ||
2. Otherwise it will generate a `jar` file containing the compiler as well as runtime in the host systems architecture. | ||
|
||
## 2.Testing | ||
In order to test the compiler against the test suite run `make test LINK_FILE_EXTENSION=.o COMPILER=<Path to compiler>` in `test` directory. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# nBallerina:0.1 | ||
Compiler for the [Ballerina language](https://ballerina.io/) that can generate native executables. | ||
|
||
## Requirements | ||
+ Make sure you have Java 17 or later and `java` command is working. | ||
+ Compiler uses the operating system's C compiler (`cc`) for linking. | ||
|
||
## Usage | ||
Run `nballerina.sh <Path to bal source file>` to compile. Resulting executable (along with build artifacts) will be in `./build` directory | ||
|
||
## Language restrictions | ||
Currently supported subset of the language is defined in `subset.md` file. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#!/bin/sh | ||
set -e | ||
if [ $# -ne 1 ]; then | ||
echo "Error: expected $0 <source file>" | ||
exit 1 | ||
fi | ||
|
||
if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then | ||
echo "Usage: $0 <source file>" | ||
exit 0 | ||
fi | ||
|
||
scriptDir=$(realpath $(dirname "$0")) | ||
src="$1" | ||
buildDir="$(pwd)/build" | ||
runtime="$scriptDir/balrt.a" | ||
mkdir -p "$buildDir" | ||
|
||
java -jar "$scriptDir/./compiler.jar" --outDir "$buildDir" "$src" | ||
|
||
srcName=$(basename "$src" .bal) | ||
cd "$buildDir" | ||
objects=() | ||
for f in "$srcName"*.o; do | ||
objects+=("$buildDir/$f") | ||
done | ||
|
||
cc -O2 -o "$srcName" "$buildDir/$srcName"*.o -lm "$runtime" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# nBallerina:0.1 | ||
Compiler for the [Ballerina language](https://ballerina.io/) that can generate native executables. | ||
|
||
## Requirements | ||
+ Compiler uses the operating system's C compiler (`cc`) for linking. | ||
|
||
## Usage | ||
Run `nballerina.sh <Path to bal source file>` to compile. Resulting executable (along with build artifacts) will be in `./build` directory | ||
|
||
### Cross compiling | ||
Only supported target is `aarch64-linux-gnu`. In order to cross compile first install GCC cross compiler for aarch64. | ||
``` | ||
sudo apt-get install gcc-aarch64-linux-gnu | ||
``` | ||
Then run `nballerina.sh <Path to bal source file> --target aarch64` | ||
|
||
## Language restrictions | ||
Currently supported subset of the language is defined in `subset.md` file. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#!/bin/sh | ||
set -e | ||
|
||
print_usage_and_exit() { | ||
echo "Usage: $0 <source file> [--target aarch64]" | ||
exit "$1" | ||
} | ||
|
||
if [ $# -lt 1 ]; then | ||
print_usage_and_exit 1 | ||
fi | ||
|
||
if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then | ||
print_usage_and_exit 0 | ||
fi | ||
|
||
scriptDir=$(realpath $(dirname "$0")) | ||
src="$1" | ||
buildDir="$(pwd)/build" | ||
rm -rf "$buildDir" | ||
mkdir -p "$buildDir" | ||
|
||
c_compiler=cc | ||
runtime="$scriptDir/balrt.a" | ||
if [ $# -gt 1 ]; then | ||
if [ "$2" = "--target" ]; then | ||
if [ $# -lt 3 ]; then | ||
print_usage_and_exit 1 | ||
fi | ||
if [ "$3" = "aarch64" ]; then | ||
"$scriptDir/./compiler" --outDir "$buildDir" --target aarch64-unknown-linux-gnu "$src" | ||
c_compiler=aarch64-linux-gnu-gcc | ||
runtime="$scriptDir/balrt_aarch64.a" | ||
else | ||
echo "Error: unsupported target $3" | ||
exit 1 | ||
fi | ||
else | ||
print_usage_and_exit 1 | ||
fi | ||
else | ||
"$scriptDir"/./compiler --outDir "$buildDir" "$src" | ||
fi | ||
|
||
srcName=$(basename "$src" .bal) | ||
|
||
"$c_compiler" -O2 -static -o "$srcName" "$buildDir/$srcName"*.o -lm "$runtime" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.