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

Add script to pull extism libraries and maven plugin to include them in jar #20

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Zwiterrion
Copy link

@Zwiterrion Zwiterrion commented Feb 9, 2024

Hi, (#3)

I have used this method in other projects. It's really simple but effective. Before bundling the java-sdk, I run a script to fetch and include libextism in the resources folder. In the resources folder, we have every processor architecture.

When the java-sdk jar is included, the right libextism library is loaded.

Screenshot 2024-02-09 at 16 42 51 Screenshot 2024-02-09 at 16 43 19 Screenshot 2024-02-09 at 16 43 28

@bhelx
Copy link
Contributor

bhelx commented Feb 9, 2024

Wow this is awesome!

@bhelx bhelx self-requested a review February 14, 2024 20:33
@bhelx
Copy link
Contributor

bhelx commented Feb 19, 2024

The CI is not liking something about the bash script: https://github.com/extism/java-sdk/actions/runs/7853990948/job/21743265959?pr=20

@@ -0,0 +1,33 @@
EXTISM_VERSION=$(curl https://api.github.com/repos/extism/extism/releases/latest | jq -r '.name')
Copy link
Contributor

Choose a reason for hiding this comment

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

May want to add a shebang and set -e on the top of this script so it fails if something goes wrong.

@thomasdarimont
Copy link
Contributor

thomasdarimont commented Apr 4, 2024

I took a look at the update-extism.sh script and fixed a few shellcheck warnings:

#!/bin/bash
set -e

EXTISM_VERSION=$(curl https://api.github.com/repos/extism/extism/releases/latest | jq -r '.name')

echo "latest extism version is: ${EXTISM_VERSION}"

rm -rf src/main/resources/*
mkdir -p ./src/main/resources/natives/

create_librairies_folders() {
  archs=("darwin-aarch64" "darwin-x86-64" "linux-aarch64" "linux-x86-64" "win32-x86-64")
  for i in "${archs[@]}"; do
     mkdir "./src/main/resources/$i"
  done
}

fetch_and_unzip_library() {
  ARCH="$1"
  LIBRARY_FOLDER="$2"
  FILENAME="$3"

  curl -L -o "./src/main/resources/natives/${ARCH}-${EXTISM_VERSION}.tar.gz" "https://github.com/extism/extism/releases/download/${EXTISM_VERSION}/${ARCH}-${EXTISM_VERSION}.tar.gz"
  tar -xvf "./src/main/resources/natives/${ARCH}-${EXTISM_VERSION}.tar.gz" --directory ./src/main/resources/natives/
  mv "./src/main/resources/natives/${FILENAME}" "./src/main/resources/${LIBRARY_FOLDER}/${FILENAME}"
}

create_librairies_folders

fetch_and_unzip_library "libextism-aarch64-apple-darwin" "darwin-aarch64" "libextism.dylib"
fetch_and_unzip_library "libextism-x86_64-apple-darwin" "darwin-x86-64" "libextism.dylib"
fetch_and_unzip_library "libextism-aarch64-unknown-linux-gnu" "linux-aarch64" "libextism.so"
fetch_and_unzip_library "libextism-x86_64-unknown-linux-gnu" "linux-x86-64" "libextism.so"
fetch_and_unzip_library "libextism-x86_64-pc-windows-gnu" "win32-x86-64" "extism.dll"

rm -rf src/main/resources/natives 2> /dev/null

Besides that, I think it is a good idea to bundle the native lib with the jar, however this will effectively create a jar library that is 115MB+ large where ~80MB will be consumed by the library versions for other platforms.

Perhaps it would be better to build indiviual jar for the respective platform? This approach is used by other libraries too, e.g. the eclipse swt project: https://central.sonatype.com/namespace/org.eclipse.swt

@kevinduffey
Copy link

Is this going to get merged at some point? This seems quite helpful to have as presently we have to manually download the various shared libs and put them in to target/classes (which get wiped out on a mvn clean) or other path and configure it.

@bhelx
Copy link
Contributor

bhelx commented Apr 5, 2024

I'll merge it when the tests are fixed. I haven't had a chance to look at it. If you'd like to contribute a fix that would be helpful.

@thomasdarimont
Copy link
Contributor

Would it be possible to reduce the size of the binaries somehow?
Perhaps strip the binaries? https://www.baeldung.com/linux/strip-executables

Regarding the idea of creating os/arch specific jars that can be added later as an optimization to reduce the size of a jar file.

@thomasdarimont
Copy link
Contributor

thomasdarimont commented Apr 5, 2024

Small experiment: strip libextism.so yields 33% size reduction... not bad.

-rwxr-xr-x 1 tom tom  24M  5. Apr 17:13 libextism.so.orig*
-rwxr-xr-x 1 tom tom  16M  5. Apr 17:13 libextism.so.stripped*

@@ -0,0 +1,36 @@
#!/bin/sh

Choose a reason for hiding this comment

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

/bin/sh is often a POSIX shell, which does not support arrays. Could you please try this #!/bin/bash as a first line

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants