Skip to content

Commit

Permalink
native support library
Browse files Browse the repository at this point in the history
Signed-off-by: mxtartaglia <maxi@swirldslabs.com>
  • Loading branch information
mxtartaglia-sl committed Jul 23, 2024
1 parent ebd210e commit 42aa783
Show file tree
Hide file tree
Showing 21 changed files with 643 additions and 0 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

#include <jni.h>
#include <iostream>
#include "com_swirlds_platform_base_example_jni_HelloWorld.h" // This is the generated header file

// Implementation of the native method
JNIEXPORT void JNICALL Java_com_swirlds_platform_base_example_jni_HelloWorld_printHelloWorld(JNIEnv *, jobject) {
std::cout << "Hello, World from C++!" << std::endl;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
requires com.swirlds.metrics.api;
requires com.fasterxml.jackson.databind;
requires com.google.common;
requires com.hedera.nativesupport;
requires jdk.httpserver;
requires jdk.management;
requires org.apache.logging.log4j;
Expand Down
22 changes: 22 additions & 0 deletions platform-sdk/hedera-nativesupport/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (C) 2024 Hedera Hashgraph, LLC
*
* 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.
*/

plugins {
id("com.hedera.gradle.platform")
id("com.hedera.gradle.platform-publish")
}

testModuleInfo { requires("org.junit.jupiter.api") }
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (C) 2024 Hedera Hashgraph, LLC
*
* 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.
*/

package com.hedera.nativesupport;

/**
* Defines the supported architectures.
*/
public enum Architecture {
/**
* AMD64
*/
AMD64,
/**
* ARM64
*/
ARM64,
/**
* I386
*/
I386;

/**
* Attempts to determine the current architecture based on the system property "os.arch".
*
* @return the current architecture.
* @throws IllegalStateException if the current architecture is not supported.
*/
public static Architecture current() {
final String osArch = System.getProperty("os.arch").toLowerCase();
if (osArch.contains("amd64") || osArch.contains("x86_64")) {
return AMD64;
} else if (osArch.contains("arm64") || osArch.contains("aarch64")) {
return ARM64;
} else if (osArch.contains("i386")) {
return I386;
} else {
throw new IllegalStateException("Unsupported architecture: " + osArch);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Copyright (C) 2023-2024 Hedera Hashgraph, LLC
*
* 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.
*/

package com.hedera.nativesupport;

import edu.umd.cs.findbugs.annotations.NonNull;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;

/**
* Utility class that helps to extract a library contained in the JAR file into a temporary directory.
*/
public final class LibraryLoader {

/**
* The root resources folder where the software is located.
*/
private static final String SOFTWARE_FOLDER_NAME = "software";

/**
* The path delimiter used in the JAR file.
*/
private static final String RESOURCE_PATH_DELIMITER = File.separator;

/**
* The {@link ResourceLoader} used to load the Helm executable.
*/
private static final ResourceLoader RESOURCE_LOADER = new ResourceLoader(LibraryLoader.class);

private static final Map<OperatingSystem, String> DEFAULT_LIB_EXTENSIONS =
Map.of(OperatingSystem.WINDOWS, "dll", OperatingSystem.LINUX, "so", OperatingSystem.DARWIN, "dylib");

/**
* Private constructor to prevent instantiation of this utility class.
*/
private LibraryLoader() {
// Empty private constructor to prevent instantiation of this utility class.
}

/**
* Unpacks a library in the JAR file into a temporary directory.
*
* @param libraryName the library to load.
* @return the path to the library to load.
* @implNote This method expects the executable to be present at the following location in the JAR file:
* {@code /software/<os>/<arch>/libraryName}.
*/
public static Path install(@NonNull final String libraryName) {
return install(libraryName, DEFAULT_LIB_EXTENSIONS);
}

/**
* Unpacks a library in the JAR file into a temporary directory.
*
* @param libraryName the library to load.
* @param libExtensions defaults extensions for each os to use to load the library
* @return the path to the library to load.
* @implNote This method expects the executable to be present at the following location in the JAR file:
* {@code /software/<os>/<arch>/libraryName}.
*/
public static Path install(
@NonNull final String libraryName, @NonNull final Map<OperatingSystem, String> libExtensions) {
Objects.requireNonNull(libraryName, "libraryName must not be null");
Objects.requireNonNull(libExtensions, "libExtensions must not be null");
try {
final OperatingSystem os = OperatingSystem.current();
final Architecture arch = Architecture.current();

String libExtension = libExtensions.get(os);
if (!libExtensions.isEmpty()) {
libExtension = "." + libExtension;
}
return RESOURCE_LOADER.load(SOFTWARE_FOLDER_NAME
+ RESOURCE_PATH_DELIMITER
+ os.name().toLowerCase(Locale.US)
+ RESOURCE_PATH_DELIMITER
+ arch.name().toLowerCase(Locale.US)
+ RESOURCE_PATH_DELIMITER
+ libraryName
+ libExtension);
} catch (IOException | SecurityException | IllegalStateException e) {
throw new IllegalStateException("Could not install requested library " + libraryName, e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (C) 2024 Hedera Hashgraph, LLC
*
* 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.
*/

package com.hedera.nativesupport;

/**
* Defines the supported Operative systems
*/
public enum OperatingSystem {
/**
* WINDOWS
*/
WINDOWS,
/**
* LINUX
*/
LINUX,
/**
* DARWIN
*/
DARWIN;

/**
* Attempts to determine the current operating system based on the system property "os.name".
*
* @return the current operating system.
* @throws IllegalStateException if the current operating system is not supported.
*/
public static OperatingSystem current() {
final String osName = System.getProperty("os.name").toLowerCase();
if (osName.contains("win")) {
return WINDOWS;
} else if (osName.contains("nix") || osName.contains("nux")) {
return LINUX;
} else if (osName.contains("mac")) {
return DARWIN;
} else {
throw new IllegalStateException("Unsupported operating system: " + osName);
}
}
}
Loading

0 comments on commit 42aa783

Please sign in to comment.