From 55be0a6d127d7b8bbbf56e287ceb8a6144f70f58 Mon Sep 17 00:00:00 2001 From: Kidy Lee Date: Thu, 6 Apr 2023 20:16:10 +0800 Subject: [PATCH] 1. Change rustc to cargo 2. use rust-maven-plugin instead of manual build --- .gitignore | 7 + Cargo.lock | 7 + Cargo.toml | 12 + NOTICE | 2 +- pom.xml | 83 ++--- .../javarust/build/CompileRustCrates.java | 297 ------------------ src/build/scripts/rustup.ps1 | 23 -- 7 files changed, 54 insertions(+), 377 deletions(-) create mode 100644 Cargo.lock create mode 100644 Cargo.toml delete mode 100644 src/build/java/com/github/drrb/javarust/build/CompileRustCrates.java delete mode 100644 src/build/scripts/rustup.ps1 diff --git a/.gitignore b/.gitignore index 640abcd..e03aa70 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,10 @@ /target *.swp /hs_err_pid*.log + + +# Added by cargo +# +# already existing elements were commented out + +#/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..6b735dc --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "greetings" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..6edb40c --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "greetings" +version = "0.1.0" +edition = "2021" + +[lib] +path = "src/main/rust/com/github/drrb/javarust/lib/greetings.rs" +crate-type = ["cdylib"] +doc = false +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/NOTICE b/NOTICE index fc261ee..6def362 100644 --- a/NOTICE +++ b/NOTICE @@ -2,7 +2,7 @@ This project depends on the following libraries (including test and runtime depe Hamcrest All under New BSD License Hamcrest Core under New BSD License - Java Native Access under LGPL, version 2.1 or ASL, version 2 + Java Native Access under LGPL-2.1-or-later or Apache-2.0 Java Rust Example under GNU General Public License, Version 3 JUnit under Common Public License Version 1.0 diff --git a/pom.xml b/pom.xml index 23db604..5331777 100644 --- a/pom.xml +++ b/pom.xml @@ -29,6 +29,7 @@ along with this program. If not, see . UTF-8 + 1.0.1-SNAPSHOT @@ -45,9 +46,10 @@ along with this program. If not, see . net.java.dev.jna jna - 4.0.0 + 5.13.0 + junit @@ -110,73 +112,42 @@ along with this program. If not, see . - - exec-maven-plugin - org.codehaus.mojo - 1.2.1 + com.github.kidylee.rust-maven-plugin + rust-maven-plugin + 1.0.1-SNAPSHOT - compile-rust-crates - compile + opendal-java - java + build - com.github.drrb.javarust.build.CompileRustCrates + ../java-rust-example + true + ${project.build.directory}/classes + true + + --color=always + - - - - org.apache.maven.plugins - maven-assembly-plugin - 2.5.3 - - false - - jar-with-dependencies - - - - com.github.drrb.javarust.Main - - - - - - package - - single - - - - - - - org.jasig.maven - maven-notice-plugin - 1.0.4 - - - - generate - - process-resources - - - + + + jitpack.io + https://jitpack.io + + + + + jitpack.io + https://jitpack.io + + diff --git a/src/build/java/com/github/drrb/javarust/build/CompileRustCrates.java b/src/build/java/com/github/drrb/javarust/build/CompileRustCrates.java deleted file mode 100644 index 0942401..0000000 --- a/src/build/java/com/github/drrb/javarust/build/CompileRustCrates.java +++ /dev/null @@ -1,297 +0,0 @@ -/* - * Copyright (C) 2015 drrb - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.github.drrb.javarust.build; - -import java.io.File; -import java.io.IOException; -import static java.nio.charset.StandardCharsets.UTF_8; -import java.nio.file.FileVisitResult; -import java.nio.file.FileVisitor; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.nio.file.StandardCopyOption; -import java.nio.file.attribute.BasicFileAttributes; -import static java.util.Arrays.asList; -import java.util.Date; -import java.util.LinkedList; -import java.util.List; -import java.util.Locale; -import java.util.Map; - -/** - * - */ -public class CompileRustCrates { - - private static final Date EPOCH = new Date(0); - private static final Path RUST_OUTPUT_DIR = Paths.get("target", "rust-libs"); - - public static void main(String[] args) throws Exception { - Paths.get("target", "rust-libs").toFile().mkdirs(); - if (changesDetected()) { - System.out.println("Changes detected. Compiling all Rust crates!"); - for (Path crate : crates()) { - compile(crate); - } - } else { - System.out.println("No changes detected. Not recompiling Rust crates."); - } - } - - private static boolean changesDetected() throws IOException { - Date lastSourceChange = newestChange(rustSources()); - Date lastCompilation = newestChange(compiledRustLibraries()); - return lastSourceChange.getTime() > lastCompilation.getTime(); - } - - private static void compile(Path sourceFile) { - System.out.format("Compiling crate %s%n", sourceFile); - try { - Process process = rustcProcess(sourceFile).inheritIO().start(); - process.waitFor(); - if (process.exitValue() != 0) { - throw new RuntimeException(String.format("rustc exited nonzero (status code = %s)", process.exitValue())); - } - for (Path compiledRustLibrary : compiledRustLibraries()) { - moveLibIntoClasspath(compiledRustLibrary); - } - } catch (IOException | InterruptedException ex) { - throw new RuntimeException(ex); - } - } - - private static ProcessBuilder rustcProcess(Path crateFile) { - List commandParts; - if (inNetbeans() && new File("/bin/bash").isFile()) { - System.out.println("(running rustc via bash because we're in NetBeans)"); - commandParts = asList("/bin/bash", "-lc", String.format("rustc --out-dir %s %s", RUST_OUTPUT_DIR, crateFile)); - } else { - commandParts = asList("rustc", "--out-dir", RUST_OUTPUT_DIR.toString(), crateFile.toString()); - } - System.out.format("Running command: %s%n", commandParts); - return new ProcessBuilder(commandParts); - } - - private static void moveLibIntoClasspath(Path library) { - try { - Path outputDir = outputDir(); - outputDir.toFile().mkdirs(); - System.out.format("Installing %s into %s%n", library, outputDir); - Files.copy(library, outputDir.resolve(library.getFileName()), StandardCopyOption.REPLACE_EXISTING); - } catch (IOException ex) { - throw new RuntimeException(ex); - } - } - - private static Path outputDir() { - return Paths.get("target", "classes", osArchName()); - } - - private static String osArchName() { - return Os.getCurrent().jnaArchString(); - } - - private static List crates() throws IOException { - List crates = new LinkedList<>(); - for (Path rustSource : rustSources()) { - if (isCrate(rustSource)) { - crates.add(rustSource); - } - } - return crates; - } - - private static List rustSources() throws IOException { - return findFiles(Paths.get("src", "main", "rust"), new FileFinder() { - - @Override - protected boolean accept(Path file, BasicFileAttributes attrs) { - return isRustSource(file, attrs); - } - }); - } - - private static List compiledRustLibraries() throws IOException { - return findFiles(RUST_OUTPUT_DIR, new FileFinder() { - - @Override - protected boolean accept(Path file, BasicFileAttributes attrs) { - return isDylib(file, attrs); - } - }); - } - - private static List findFiles(Path startPath, FileFinder finder) throws IOException { - Files.walkFileTree(startPath, finder); - return finder.getFound(); - } - - private static boolean inNetbeans() { - for (Map.Entry envVars : System.getenv().entrySet()) { - String key = envVars.getKey(); - String value = envVars.getValue(); - if (key.matches("JAVA_MAIN_CLASS_\\d+") && value.equals("org.netbeans.Main")) { - return true; - } - } - return false; - } - - private static boolean isRustSource(Path path, BasicFileAttributes attributes) { - return attributes.isRegularFile() && path.toString().endsWith(".rs"); - } - - private static boolean isCrate(Path path) { - try { - return path.toFile().isFile() - && path.toString().endsWith(".rs") - && new String(Files.readAllBytes(path), UTF_8).contains("#![crate_type"); - } catch (IOException ex) { - throw new RuntimeException(ex); - } - } - - private static boolean isDylib(Path path, BasicFileAttributes attributes) { - String pathString = path.toString(); - String pathExtension = pathString.substring(pathString.lastIndexOf(".")); - List dylibExtensions = asList(".dylib", ".so", ".dll"); - return attributes.isRegularFile() && dylibExtensions.contains(pathExtension); - } - - private static Date newestChange(List paths) { - Date lastChange = EPOCH; - for (Path path : paths) { - Date change = mtime(path); - if (change.getTime() > lastChange.getTime()) { - lastChange = change; - } - } - return lastChange; - } - - @SuppressWarnings("CallToPrintStackTrace") - private static Date mtime(Path path) { - try { - return new Date(Files.getLastModifiedTime(path).toMillis()); - } catch (IOException ex) { - ex.printStackTrace(); - return EPOCH; - } - } - - private enum Os { - MAC_OS("mac", "darwin") { - @Override - public String jnaArchString() { - return "darwin"; - } - }, - WINDOWS("win") { - @Override - public String jnaArchString() { - if (currentIs64Bit()) { - return "win32-x86-64"; - } else { - return "win32-x86"; - } - } - }, - GNU_SLASH_LINUX("nux") { - @Override - public String jnaArchString() { - if (currentIs64Bit()) { - return "linux-x86-64"; - } else { - return "linux-x86"; - } - } - }, - UNKNOWN() { - @Override - public String jnaArchString() { - throw new RuntimeException("Unknown platform. Can't tell what platform we're running on!"); - } - }; - private final String[] substrings; - - private Os(String... substrings) { - this.substrings = substrings; - } - - public abstract String jnaArchString(); - - public static Os getCurrent() { - for (Os os : values()) { - if (os.isCurrent()) { - return os; - } - } - return UNKNOWN; - } - - public boolean isCurrent() { - for (String substring : substrings) { - if (currentOsString().contains(substring)) { - return true; - } - } - return false; - } - - private static boolean currentIs64Bit() { - return System.getProperty("os.arch").contains("64"); - } - - private static String currentOsString() { - return System.getProperty("os.name", "unknown").toLowerCase(Locale.ENGLISH); - } - } - - private static abstract class FileFinder implements FileVisitor { - private final List found = new LinkedList<>(); - - public List getFound() { - return found; - } - - @Override - public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { - return FileVisitResult.CONTINUE; - } - - @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { - if (accept(file, attrs)) { - found.add(file); - } - return FileVisitResult.CONTINUE; - } - - @Override - public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { - return FileVisitResult.CONTINUE; - } - - @Override - public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { - return FileVisitResult.CONTINUE; - } - - protected abstract boolean accept(Path file, BasicFileAttributes attrs); - } -} diff --git a/src/build/scripts/rustup.ps1 b/src/build/scripts/rustup.ps1 deleted file mode 100644 index fbb9138..0000000 --- a/src/build/scripts/rustup.ps1 +++ /dev/null @@ -1,23 +0,0 @@ -$ErrorActionPreference = "Stop" - -if ([environment]::Is64BitOperatingSystem) { - $arch = "x86_64" - $install_dir = "C:\Program Files\Rust" -} else { - $arch = "i686" - $install_dir = "C:\Program Files (x86)\Rust" -} - -$package = "rust-nightly-$($arch)-pc-windows-gnu.exe" -$url = "https://static.rust-lang.org/dist/$($package)" - -echo "Downloading Rust and Cargo from $($url)" -Start-FileDownload $url - -echo "Installing Rust" -Start-Process ".\$($package)" -ArgumentList "/VERYSILENT /NORESTART" -NoNewWindow -Wait - -echo "Refreshing Path" -$env:Path = [System.Environment]::GetEnvironmentVariable("Path", "User") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "Machine") - -echo "Rust and Cargo are ready to roll!"