-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: mxtartaglia <maxi@swirldslabs.com>
- Loading branch information
1 parent
ebd210e
commit 42aa783
Showing
21 changed files
with
643 additions
and
0 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
...lds-platform-base-example/src/main/cpp/com_swirlds_platform_base_example_jni_HelloWorld.h
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
9 changes: 9 additions & 0 deletions
9
example-apps/swirlds-platform-base-example/src/main/cpp/hello.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") } |
54 changes: 54 additions & 0 deletions
54
platform-sdk/hedera-nativesupport/src/main/java/com/hedera/nativesupport/Architecture.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
platform-sdk/hedera-nativesupport/src/main/java/com/hedera/nativesupport/LibraryLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...form-sdk/hedera-nativesupport/src/main/java/com/hedera/nativesupport/OperatingSystem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
Oops, something went wrong.