Skip to content

Commit

Permalink
added constantine in build.sh
Browse files Browse the repository at this point in the history
Signed-off-by: Nischal Sharma <nischal@web3labs.com>
  • Loading branch information
NickSneo committed Jul 29, 2024
1 parent 235c91a commit 7c9f645
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 61 deletions.
27 changes: 21 additions & 6 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ jobs:
with:
name: gnark native build artifacts
path: gnark/build/
- uses: actions/upload-artifact@v3.1.0
with:
name: constantine native build artifacts
path: constantine/build/

native-build-linux-arm64:
runs-on: besu-arm64
Expand Down Expand Up @@ -107,6 +111,10 @@ jobs:
with:
name: gnark native build artifacts
path: gnark/build/
- uses: actions/upload-artifact@v3.1.0
with:
name: constantine native build artifacts
path: constantine/build/

native-build-macos:
runs-on: macos-13
Expand Down Expand Up @@ -164,6 +172,10 @@ jobs:
with:
name: gnark native build artifacts
path: gnark/build/
- uses: actions/upload-artifact@v3.1.0
with:
name: constantine native build artifacts
path: constantine/build/

native-build-m1:
runs-on: macos-13-xlarge
Expand Down Expand Up @@ -244,6 +256,10 @@ jobs:
with:
name: gnark native build artifacts
path: gnark/build/
- uses: actions/upload-artifact@v3.1.0
with:
name: constantine native build artifacts
path: constantine/build/

final-assembly:
runs-on: ubuntu-20.04
Expand Down Expand Up @@ -290,12 +306,11 @@ jobs:
with:
name: gnark native build artifacts
path: gnark/build/
- name: Install Nim
run: |
# nim dependencies
export CHOOSENIM_CHOOSE_VERSION=2.0.4
curl https://nim-lang.org/choosenim/init.sh -sSf | sh -s -- -y
export PATH=$HOME/.nimble/bin:$PATH
- name: Download constantine
uses: actions/download-artifact@v3
with:
name: constantine native build artifacts
path: constantine/build/
- name: Set up Java
uses: actions/setup-java@v4
with:
Expand Down
30 changes: 30 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -319,13 +319,43 @@ EOF
cp libgnark_eip_196.* "$SCRIPTDIR/gnark/build/${OSARCH}/lib"
}

build_constantine() {
echo "#############################"
echo "####### build constantine ####"
echo "#############################"

cd "$SCRIPTDIR/constantine/constantine"

# delete old build dir, if exists
rm -rf "$SCRIPTDIR/constantine/build" || true
mkdir -p "$SCRIPTDIR/constantine/build/"

# Build the constantine library
export CTT_LTO=false
nimble make_lib

cd "$SCRIPTDIR/constantine/"

# Compile the native library
if [[ "$OSTYPE" == "darwin"* ]]; then
clang -I"${JAVA_HOME}/include" -I"${JAVA_HOME}/include/darwin" -shared -o "$SCRIPTDIR/constantine/build/${OSARCH}/lib/libconstantine.jni" ethereum_evm_precompiles.c -Iconstantine/include -I. -Lconstantine/lib -lconstantine
elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
clang -I"${JAVA_HOME}/include" -I"${JAVA_HOME}/include/linux" -fPIC -shared -o "$SCRIPTDIR/constantine/build/${OSARCH}/lib/libconstantine.so" ethereum_evm_precompiles.c -Iconstantine/include -I. -Lconstantine/lib -lconstantine
else
echo "Unsupported OS/architecture: ${OSARCH}"
exit 1
fi
}


build_blake2bf
build_secp256k1
build_arithmetic
build_bls12_381
build_ipa_multipoint
build_secp256r1
build_gnark
build_constantine


build_jars
Expand Down
74 changes: 19 additions & 55 deletions constantine/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,77 +21,41 @@ java {
}
}

task makeConstantineLib(type: Exec) {
description = 'Compiles the Constantine library'
workingDir = file('./constantine')
environment 'PATH', "${System.env.PATH}:${System.getenv('HOME')}/.nimble/bin"
environment 'CTT_LTO', 'false'
commandLine "${System.getenv('HOME')}/.nimble/bin/nimble", 'make_lib'
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}

task compileJavaSource(type: Exec) {
description = 'Compiles the Java source files'
commandLine 'javac', '-d', 'out', 'src/main/java/org/hyperledger/besu/nativelib/constantine/LibConstantineEIP196.java'
task macArmLibCopy(type: Copy) {
from "constantine/build/macos-aarch64/lib/libconstantine.jnilib"
into 'build/resources/main/lib'
}

task compileNativeLibrary(type: Exec) {
description = 'Compiles the native library'
task macLibCopy(type: Copy) {
from "constantine/build/macos-x86_64/lib/libconstantine.jnilib"
into 'build/resources/main/lib'
}

def osName = System.getProperty('os.name').toLowerCase()
def isMac = osName.contains('mac')
def isLinux = osName.contains('linux')
task linuxLibCopy(type: Copy) {
from "constantine/build/linux-x86_64/lib/libconstantine.so"
into 'build/resources/main/lib'
}

if (isMac) {
commandLine 'clang', '-I', "${System.env.JAVA_HOME}/include", '-I', "${System.env.JAVA_HOME}/include/darwin", '-shared', '-o', 'out/org/hyperledger/besu/nativelib/constantine/libconstantine.jnilib', 'ethereum_evm_precompiles.c', '-Iconstantine/include', '-I.', '-Lconstantine/lib', '-lconstantine'
} else if (isLinux) {
commandLine 'clang', '-I', "${System.env.JAVA_HOME}/include", '-I', "${System.env.JAVA_HOME}/include/linux", '-fPIC', '-shared', '-o', 'out/org/hyperledger/besu/nativelib/constantine/libconstantine.so', 'ethereum_evm_precompiles.c', '-Iconstantine/include', '-I.', '-Lconstantine/lib', '-lconstantine'
} else {
throw new GradleException("Unsupported operating system: ${osName}")
}
task linuxArm64LibCopy(type: Copy) {
from "constantine/build/linux-aarch64/lib/libconstantine.so"
into 'build/resources/main/lib'
}

compileJavaSource.dependsOn makeConstantineLib
compileNativeLibrary.dependsOn compileJavaSource
processResources.dependsOn macArmLibCopy, macLibCopy, linuxLibCopy, linuxArm64LibCopy

tasks.named('test', Test) {
description = 'Runs the Java tests'
useJUnit {
include '**/*Test.class'
}
jvmArgs = ['-Djava.library.path=out/org/hyperledger/besu/nativelib/constantine']
dependsOn compileNativeLibrary
jvmArgs = ['-Djava.library.path=build/resources/main/lib']
dependsOn linuxLibCopy, linuxArm64LibCopy, macLibCopy, macArmLibCopy
}

tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}

//task macArmLibCopy(type: Copy) {
// dependsOn compileNativeLibrary
// from 'constantine/lib/libconstantine.dylib'
// into 'build/resources/main/darwin-aarch64'
//}
//
//task macLibCopy(type: Copy) {
// dependsOn compileNativeLibrary
// from 'constantine/lib/libconstantine.dylib'
// into 'build/resources/main/darwin-x86-64'
//}
//
//task linuxLibCopy(type: Copy) {
// dependsOn compileNativeLibrary
// from 'constantine/lib/libconstantine.so'
// into 'build/resources/main/linux-x86-64'
//}
//
//task linuxArm64LibCopy(type: Copy) {
// dependsOn compileNativeLibrary
// from 'constantine/lib/libconstantine.so'
// into 'build/resources/main/linux-aarch64'
//}

//processResources.dependsOn macArmLibCopy, macLibCopy, linuxLibCopy, linuxArm64LibCopy

jar {
archiveBaseName = 'besu-native-constantine'
includeEmptyDirs = false
Expand Down

0 comments on commit 7c9f645

Please sign in to comment.