-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support using FFM in native-image (#269)
- Loading branch information
Showing
12 changed files
with
350 additions
and
124 deletions.
There are no files selected for viewing
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
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
84 changes: 84 additions & 0 deletions
84
src/main/java/org/fusesource/jansi/internal/NativeImageFeature.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,84 @@ | ||
/* | ||
* Copyright (C) 2009-2023 the original author(s). | ||
* | ||
* 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 org.fusesource.jansi.internal; | ||
|
||
import java.util.Objects; | ||
|
||
import org.fusesource.jansi.AnsiConsole; | ||
import org.graalvm.nativeimage.hosted.Feature; | ||
import org.graalvm.nativeimage.hosted.RuntimeClassInitialization; | ||
import org.graalvm.nativeimage.hosted.RuntimeSystemProperties; | ||
|
||
public class NativeImageFeature implements Feature { | ||
@Override | ||
public String getURL() { | ||
return "https://github.com/fusesource/jansi"; | ||
} | ||
|
||
@Override | ||
public void duringSetup(DuringSetupAccess access) { | ||
RuntimeClassInitialization.initializeAtBuildTime(AnsiConsoleSupportHolder.class); | ||
|
||
String providers = System.getProperty(AnsiConsole.JANSI_PROVIDERS); | ||
if (providers != null) { | ||
try { | ||
RuntimeSystemProperties.register(AnsiConsole.JANSI_PROVIDERS, providers); | ||
} catch (Throwable ignored) { | ||
// GraalVM version < 23.0 | ||
// No need to worry as we select the provider at build time | ||
} | ||
} | ||
|
||
String provider = Objects.requireNonNull(AnsiConsoleSupportHolder.getProviderName(), "No provider available"); | ||
if (provider.equals(AnsiConsole.JANSI_PROVIDER_JNI)) { | ||
String jansiNativeLibraryName = System.mapLibraryName("jansi"); | ||
if (jansiNativeLibraryName.endsWith(".dylib")) { | ||
jansiNativeLibraryName = jansiNativeLibraryName.replace(".dylib", ".jnilib"); | ||
} | ||
|
||
String packagePath = JansiLoader.class.getPackage().getName().replace('.', '/'); | ||
|
||
try { | ||
Class<?> moduleClass = Class.forName("java.lang.Module"); | ||
Class<?> rraClass = Class.forName("org.graalvm.nativeimage.hosted.RuntimeResourceAccess"); | ||
|
||
Object module = Class.class.getMethod("getModule").invoke(JansiLoader.class); | ||
rraClass.getMethod("addResource", moduleClass, String.class) | ||
.invoke( | ||
null, | ||
module, | ||
String.format( | ||
"%s/native/%s/%s", | ||
packagePath, | ||
OSInfo.getNativeLibFolderPathForCurrentOS(), | ||
jansiNativeLibraryName)); | ||
|
||
} catch (Throwable ignored) { | ||
// GraalVM version < 22.3 | ||
// Users need to manually add the JNI library as resources | ||
} | ||
} else if (provider.equals(AnsiConsole.JANSI_PROVIDER_FFM)) { | ||
try { | ||
// FFM is only available in JDK 21+, so we need to compile it separately | ||
Class.forName("org.fusesource.jansi.internal.ffm.NativeImageDowncallRegister") | ||
.getMethod("registerForDowncall") | ||
.invoke(null); | ||
} catch (Throwable e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.