Skip to content

Commit

Permalink
New NativeTarget class and refactor (#6)
Browse files Browse the repository at this point in the history
* Initial work on using llvm targets

* Major refactor of native target detection and resolving under-the-hood

* Major overhaul of how native targets are calculated and used with new NativeTarget class

* Tweak build system
  • Loading branch information
jjlauer authored Oct 24, 2023
1 parent f20d7f5 commit 0b177bb
Show file tree
Hide file tree
Showing 18 changed files with 1,417 additions and 278 deletions.
7 changes: 7 additions & 0 deletions .blaze/blaze.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
blaze.dependencies = [
"com.fizzed:blaze-ssh:1.1.0"
"com.fizzed:buildx:1.0.0"
"com.fizzed:jne:3.3.1-SNAPSHOT"
]

container-prefix = jne
254 changes: 254 additions & 0 deletions .blaze/blaze.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
import com.fizzed.blaze.Task;
import com.fizzed.jne.NativeTarget;

import java.util.List;
import static java.util.Arrays.asList;
import java.nio.file.Path;

import static com.fizzed.blaze.Contexts.withBaseDir;
import static com.fizzed.blaze.Systems.exec;
import com.fizzed.buildx.*;

public class blaze {

private final Path projectDir = withBaseDir("../").toAbsolutePath();
private final NativeTarget localNativeTarget = NativeTarget.detect();

@Task(order = 2)
public void test() throws Exception {
/*exec("env")
.workingDir(projectDir)
.run();*/
exec("mvn", "test")
.workingDir(projectDir)
.run();
}

private final List<Target> targets = asList(
//
// Linux
//

new Target("linux", "x64", "ubuntu16.04, jdk11")
.setTags("build")
.setContainerImage("fizzed/buildx:x64-ubuntu16-jdk11-buildx-linux-x64"),

new Target("linux", "arm64", "ubuntu16.04, jdk11")
.setTags("build")
.setContainerImage("fizzed/buildx:x64-ubuntu16-jdk11-buildx-linux-arm64"),

/*new Target("linux", "armhf")
.setTags("build")
.setContainerImage("fizzed/buildx:amd64-ubuntu16-jdk11-cross-build"),
new Target("linux", "armel")
.setTags("build")
.setContainerImage("fizzed/buildx:amd64-ubuntu16-jdk11-cross-build"),
// NOTE: ubuntu18 added support for riscv64
new Target("linux", "riscv64")
.setTags("build")
.setContainerImage("fizzed/buildx:amd64-ubuntu18-jdk11-cross-build"),*/

//
// Linux (w/ MUSL)
//

new Target("linux_musl", "x64", "ubuntu16.04, jdk11")
.setTags("build")
.setContainerImage("fizzed/buildx:x64-ubuntu16-jdk11-buildx-linux_musl-x64"),

new Target("linux_musl", "arm64", "ubuntu16.04, jdk11")
.setTags("build")
.setContainerImage("fizzed/buildx:x64-ubuntu16-jdk11-buildx-linux_musl-arm64"),

//
// FreeBSD
//

/*new Target("freebsd", "x64")
.setTags("build", "test")
.setHost("bmh-build-x64-freebsd12-1"),
new Target("freebsd", "arm64")
.setTags("build", "test")
.setHost("bmh-build-arm64-freebsd13-1"),*/

//
// OpenBSD
//

/*new Target("openbsd", "x64")
.setTags("build", "test")
.setHost("bmh-build-x64-openbsd67-1"),
new Target("openbsd", "arm64")
.setTags("build", "test")
.setHost("bmh-build-arm64-openbsd72-1"),*/

//
// MacOS
//

/*new Target("macos", "x64")
.setTags("build", "test")
.setHost("bmh-build-x64-macos1013-1"),
new Target("macos", "arm64")
.setTags("build", "test")
.setHost("bmh-build-arm64-macos12-1"),*/

//
// Windows
//

/*new Target("windows", "x64")
.setTags("build", "test")
.setHost("bmh-build-x64-win11-1"),
new Target("windows", "arm64")
.setTags("build")
.setHost("bmh-build-x64-win11-1"),*/

//
// test-only containers
//

new Target(localNativeTarget.toJneOsAbi(), localNativeTarget.toJneArch(), "local machine")
.setTags("test"),

new Target("linux", "x64", "ubuntu16.04, jdk11")
.setTags("test")
.setContainerImage("fizzed/buildx:x64-ubuntu16-jdk11"),

new Target("linux", "x64", "ubuntu22.04, jdk8")
.setTags("test")
.setContainerImage("fizzed/buildx:x64-ubuntu22-jdk8"),

new Target("linux", "x64", "ubuntu22.04, jdk11")
.setTags("test")
.setContainerImage("fizzed/buildx:x64-ubuntu22-jdk11"),

new Target("linux", "x64", "ubuntu22.04, jdk17")
.setTags("test")
.setContainerImage("fizzed/buildx:x64-ubuntu22-jdk17"),

new Target("linux", "x64", "ubuntu22.04, jdk21")
.setTags("test")
.setContainerImage("fizzed/buildx:x64-ubuntu22-jdk21"),

/*new Target("linux", "arm64")
.setTags("test")
.setHost("bmh-build-arm64-ubuntu22-1"),*/

new Target("linux", "arm64", "ubuntu16.04, jdk11")
.setTags("test")
.setContainerImage("fizzed/buildx:arm64-ubuntu16-jdk11"),


new Target("windows", "x64", "win10")
.setTags("test")
.setHost("bmh-build-x64-win10-1"),


new Target("linux_musl", "x64", "alpine3.11, jdk11")
.setTags("test")
.setContainerImage("fizzed/buildx:x64-alpine3.11-jdk11"),

new Target("linux_musl", "arm64", "alpine3.11, jdk11")
.setTags("test")
// faster to run on an arm64 box?
//.setHost("bmh-build-arm64-ubuntu22-1")
.setContainerImage("fizzed/buildx:arm64v8-alpine3.11-jdk11")

/*
new Target("windows", "arm64-test", "win11")
.setTags("test")
.setHost("bmh-build-arm64-win11-1")*/

/*
new Target("linux", "armhf-test")
.setTags("test")
.setContainerImage("fizzed/buildx:arm32v7-ubuntu18-jdk11"),
new Target("linux", "armel-test")
.setTags("test")
.setContainerImage("fizzed/buildx:arm32v5-debian11-jdk11"),
new Target("linux", "armel-test")
.setTags("test")
.setContainerImage("fizzed/buildx:arm32v5-debian11-jdk11"),
new Target("linux", "riscv64-test")
.setTags("test")
.setContainerImage("fizzed/buildx:riscv64-ubuntu20-jdk19"),
new Target("windows", "x64-test", "win10")
.setTags("test")
.setHost("bmh-build-x64-win10-1"),
new Target("windows", "x64-test", "win7")
.setTags("test")
.setHost("bmh-build-x64-win7-1"),
*/
);

@Task(order = 50)
public void cross_build_containers() throws Exception {
final String user = System.getenv("USER");
final String userId = exec("id", "-u", user).runCaptureOutput().toString();
new Buildx(targets)
.onlyWithContainers()
.execute((target, project) -> {
String dockerFile = "setup/Dockerfile.linux";
if (target.getContainerImage().contains("alpine")) {
dockerFile = "setup/Dockerfile.linux_musl";
}

project.exec("docker", "build",
"-f", dockerFile,
"--build-arg", "FROM_IMAGE="+target.getContainerImage(),
"--build-arg", "USERID="+userId,
"--build-arg", "USERNAME="+user,
"-t", project.getContainerName(),
"setup")
.run();
});
}

@Task(order = 51)
public void cross_build_natives() throws Exception {
new Buildx(targets)
.setTags("build")
.execute((target, project) -> {
String buildScript = "setup/build-native-lib-linux-action.sh";
if (target.getOs().equals("macos")) {
buildScript = "setup/build-native-lib-macos-action.sh";
} else if (target.getOs().equals("windows")) {
buildScript = "setup/build-native-lib-windows-action.bat";
}

project.action(buildScript, target.getOs(), target.getArch()).run();

// we know that the only modified file will be in the artifact dir
final String artifactRelPath = "src/test/resources/jne/" + target.getOs() + "/" + target.getArch() + "/";
project.rsync(artifactRelPath, artifactRelPath).run();
});
}

@Task(order = 53)
public void cross_tests() throws Exception {
new Buildx(targets)
.setTags("test")
.execute((target, project) -> {
project.action("java", "-jar", "blaze.jar", "test")
.run();
});
}

}
44 changes: 44 additions & 0 deletions .blaze/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<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>blaze</groupId>
<artifactId>jne-blaze</artifactId>
<name>jne-blaze</name>
<version>0.0.1</version>

<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>

<build>
<sourceDirectory>${project.basedir}</sourceDirectory>
</build>

<dependencies>
<dependency>
<groupId>com.fizzed</groupId>
<artifactId>blaze-lite</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>com.fizzed</groupId>
<artifactId>blaze-ssh</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>com.fizzed</groupId>
<artifactId>buildx</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.fizzed</groupId>
<artifactId>jne</artifactId>
<version>3.3.1-SNAPSHOT</version>
</dependency>
</dependencies>

</project>
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
*.iml
native/libhelloj/helloj/HelloLib.class
.temp-m2
.buildx
Binary file added blaze.jar
Binary file not shown.
13 changes: 8 additions & 5 deletions setup/build-native-lib-linux-action.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/sh -l
set -e

BASEDIR=$(dirname "$0")
Expand All @@ -9,11 +9,13 @@ BUILDOS=$1
BUILDARCH=$2

# Setup cross compile environment
if [ -f /opt/setup-cross-build-environment.sh ]; then
. /opt/setup-cross-build-environment.sh $BUILDOS $BUILDARCH
fi
#if [ -f /opt/setup-cross-build-environment.sh ]; then
# . /opt/setup-cross-build-environment.sh $BUILDOS $BUILDARCH
#fi

. setup/setup-environment.sh
#. setup/setup-environment.sh

#env

mkdir -p target
rsync -avrt --delete ./native/ ./target/
Expand All @@ -28,5 +30,6 @@ make

cd ..
OUTPUT_DIR="../src/test/resources/jne/${BUILDOS}/${BUILDARCH}"
mkdir -p "$OUTPUT_DIR"
cp jcat/jcat "$OUTPUT_DIR"
cp libhelloj/libhelloj.so "$OUTPUT_DIR"
42 changes: 42 additions & 0 deletions src/main/java/com/fizzed/jne/ABI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.fizzed.jne;

/*-
* #%L
* jne
* %%
* Copyright (C) 2016 - 2017 Fizzed, Inc
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/

/**
* For Linux this is the LibC implementation in use such as GLIBC or MUSL.
*/
public enum ABI {

DEFAULT,
GNU,
MUSL,
MSVC;

static public ABI resolve(String value) {
for (ABI abi : ABI.values()) {
if (abi.name().equalsIgnoreCase(value)) {
return abi;
}
}
return null;
}

}
Loading

0 comments on commit 0b177bb

Please sign in to comment.