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 support for generating native libraries for Linux and macOS aarch64 platforms (fixes #46). #50

Merged
merged 16 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
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
17 changes: 0 additions & 17 deletions .github/actions/native-lib-build/action.yml

This file was deleted.

75 changes: 63 additions & 12 deletions .github/workflows/build-and-release-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,63 @@ env:
on:
pull_request:
paths:
- ".github/actions/native-lib-build/action.yml"
- ".github/workflows/build-and-release-package.yml"
- ".gitmodules"
- "assembly-lib.xml"
- "assembly-package.xml"
- "CMakeLists.txt"
- "pom.xml"
- "src/**/*"
- "tools/scripts/ubuntu-focal/install-deps-build-test-package-native-lib.sh"
push:
paths:
- ".github/actions/native-lib-build/action.yml"
- ".github/workflows/build-and-release-package.yml"
- ".gitmodules"
- "assembly-lib.xml"
- "assembly-package.xml"
- "CMakeLists.txt"
- "pom.xml"
- "src/**/*"
- "tools/scripts/ubuntu-focal/install-deps-build-test-package-native-lib.sh"
workflow_dispatch:

# Concurrency group to prevent multiple workflow instances from trying to publish releases
concurrency: "${{github.workflow}}-${{github.ref}}"

jobs:
build-lib-for-linux-aarch64:
runs-on: "ubuntu-latest"
steps:
- uses: "actions/checkout@v4"
with:
submodules: "recursive"

- uses: "docker/setup-qemu-action@v3"
with:
platforms: "arm64"

- name: "Build native library for Linux aarch64"
run: >-
docker run
--rm
--platform linux/arm64
--mount type=bind,source=${{ github.workspace }},target=/mnt/clp
--workdir /mnt/clp
eclipse-temurin:11-jdk-focal
./tools/scripts/ubuntu-focal/install-deps-build-test-package-native-lib.sh

- uses: "actions/upload-artifact@v4"
with:
name: "libclp-ffi-java-linux-aarch64"
path: "${{github.workspace}}/target/clp-ffi-*-native-lib/"
if-no-files-found: "error"
retention-days: 1

build-lib-for-macos:
runs-on: "macos-latest"
strategy:
matrix:
runner: ["macos-13", "macos-14"]
runs-on: "${{ matrix.runner }}"
steps:
- uses: "actions/checkout@v4"
with:
Expand All @@ -40,19 +71,23 @@ jobs:
- name: "Install requirements"
run: "brew install cmake gcc java${{env.JAVA_VERSION}} maven"

- name: "Build native library for MacOS"
id: "build"
uses: "./.github/actions/native-lib-build"
- name: "Build, test, and package native library for MacOS"
run: "mvn --batch-mode validate generate-resources test assembly:single@assemble-lib-dir"

- uses: "actions/upload-artifact@v4"
with:
name: "libclp-ffi-java-macos"
path: "${{github.workspace}}/clp-ffi-*-native-lib/"
name: >-
${{ matrix.runner == 'macos-13'
&& 'libclp-ffi-java-macos-amd64'
|| 'libclp-ffi-java-macos-aarch64' }}
path: "${{github.workspace}}/target/clp-ffi-*-native-lib/"
if-no-files-found: "error"
retention-days: 1

build-and-release:
needs: "build-lib-for-macos"
needs:
- "build-lib-for-linux-aarch64"
- "build-lib-for-macos"
runs-on: "ubuntu-20.04"
permissions:
contents: "read"
Expand Down Expand Up @@ -87,13 +122,22 @@ jobs:
sudo apt update
sudo apt install -y build-essential cmake

- name: "Build, run tests, and package"
run: "mvn --batch-mode test package"
- name: "Build, and run tests"
run: "mvn --batch-mode test"

- uses: "actions/download-artifact@v4"
with:
name: "libclp-ffi-java-linux-aarch64"
path: "./target/."

- uses: "actions/download-artifact@v4"
with:
name: "libclp-ffi-java-macos"
pattern: "libclp-ffi-java-macos-*"
path: "./target/."
merge-multiple: true

- name: "Build package"
run: "mvn --batch-mode package -DskipTests"

- if: "github.event_name != 'pull_request' && github.ref == 'refs/heads/main'"
name: "Deploy to GitHub Packages"
Expand All @@ -108,3 +152,10 @@ jobs:
MAVEN_USERNAME: "${{secrets.OSSRH_USERNAME}}"
MAVEN_PASSWORD: "${{secrets.OSSRH_TOKEN}}"
MAVEN_GPG_PASSPHRASE: "${{secrets.GPG_PASSPHRASE}}"

- uses: "actions/upload-artifact@v4"
with:
name: "clp-ffi-java"
path: "${{github.workspace}}/target/clp-ffi-*.jar"
if-no-files-found: "error"
retention-days: 1
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,21 @@ task native-lib

# Testing

Run tests:
Run all tests:
```shell
task test
```

Run unit tests:
```shell
task unit-tests
```

Run integration tests:
```shell
task integration-tests
```

# Linting

Before submitting a PR, ensure you've run our linting tools and either fixed any violations or
Expand Down
26 changes: 24 additions & 2 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ includes:
vars:
G_BUILD_DIR: "{{.ROOT_DIR}}/target"
G_LINT_VENV_DIR: "{{.G_BUILD_DIR}}/lint-venv"
G_PROJECT_VERSION:
sh: "mvn help:evaluate -Dexpression=project.version -q -DforceStdout"

tasks:
default:
Expand All @@ -21,9 +23,29 @@ tasks:

native-lib:
# NOTE: `validate` is necessary to remove spaces from the OS name
cmd: "mvn generate-resources validate assembly:single@assemble-lib-dir"
cmd: "mvn validate generate-resources assembly:single@assemble-lib-dir"

test: "mvn test"
test:
cmds:
- task: "unit-tests"
- task: "integration-tests"

integration-tests:
env:
# Use a local directory for the Maven repo so that we can install the jar (for the integration
# tests to use) without affecting the user's environment.
# NOTE: Maven doesn't strip quotes around the value, but treats everything on the right hand
# side of the equals sign as the value; so we don't need to quote the path.
MAVEN_OPTS: "-Dmaven.repo.local={{.G_BUILD_DIR}}/repo"
cmds:
# NOTE: Since we're using a custom repo-directory, this will end up rebuilding the package.
- "mvn install -DskipTests"
- |-
cd "integration-tests/jar-load-native-lib"
mvn compile
mvn exec:java -Dexec.mainClass="com.yscope.clp.Main"

unit-tests: "mvn test"

lint:
deps: ["lint-venv"]
Expand Down
25 changes: 25 additions & 0 deletions integration-tests/jar-load-native-lib/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.yscope.clp</groupId>
<artifactId>jar-load-native-lib</artifactId>
<version>0.1.0</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>com.yscope.clp</groupId>
<artifactId>clp-ffi</artifactId>
<version>0.4-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.yscope.clp;

import com.yscope.clp.compressorfrontend.BuiltInVariableHandlingRuleVersions;
import com.yscope.clp.compressorfrontend.EncodedMessage;
import com.yscope.clp.compressorfrontend.MessageDecoder;
import com.yscope.clp.compressorfrontend.MessageEncoder;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class Main {
public static void main(String[] args) {
// To trigger loading the native library, we do a basic encode-decode test.
MessageEncoder messageEncoder = new MessageEncoder(
BuiltInVariableHandlingRuleVersions.VariablesSchemaV2,
BuiltInVariableHandlingRuleVersions.VariableEncodingMethodsV1
);
EncodedMessage encodedMessage = new EncodedMessage();
MessageDecoder messageDecoder =
new MessageDecoder(BuiltInVariableHandlingRuleVersions.VariablesSchemaV2,
BuiltInVariableHandlingRuleVersions.VariableEncodingMethodsV1);
try {
String message = "Static text, dictVar1, 123, 456.7, dictVar2, 987, 654.3";
messageEncoder.encodeMessage(message, encodedMessage);
String decodedMessage = messageDecoder.decodeMessage(
encodedMessage.getLogTypeAsString(),
encodedMessage.getDictionaryVarsAsStrings(),
encodedMessage.encodedVars
);
if (false == message.equals(decodedMessage)) {
throw new RuntimeException("Failed to encode message.");
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash

# Exit on error
set -e

script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
project_dir="${script_dir}/../../../"

apt-get update
DEBIAN_FRONTEND=noninteractive apt-get install -y build-essential cmake maven

cd "$project_dir"
mvn --batch-mode validate generate-resources test assembly:single@assemble-lib-dir
Copy link
Member Author

Choose a reason for hiding this comment

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

I moved validate before generate-resources to match the order that Maven executes them in (see mvn buildplan:list).

Loading