package com.oracle.svm.hosted;
import java.lang.reflect.Field;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Collectors;
import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.hosted.Feature;
import org.graalvm.word.PointerBase;
import org.graalvm.word.WordFactory;
import com.oracle.svm.core.SubstrateOptions;
import com.oracle.svm.core.VM;
import com.oracle.svm.core.annotate.AutomaticFeature;
import com.oracle.svm.core.c.CGlobalData;
import com.oracle.svm.core.c.CGlobalDataFactory;
import com.oracle.svm.core.c.libc.LibCBase;
import com.oracle.svm.core.util.VMError;
import com.oracle.svm.hosted.c.CGlobalDataFeature;
import com.oracle.svm.hosted.c.NativeLibraries;
import com.oracle.svm.hosted.c.codegen.CCompilerInvoker;
@AutomaticFeature
public class VMFeature implements Feature {
private NativeLibraries nativeLibraries;
private static final String STATIC_BINARY_MARKER_SYMBOL_NAME = "__svm_vm_is_static_binary";
@Override
public void beforeAnalysis(BeforeAnalysisAccess a) {
if (SubstrateOptions.DumpTargetInfo.getValue()) {
System.out.println("# Building image for target platform: " + ImageSingletons.lookup(Platform.class).getClass().getName());
if (ImageSingletons.contains(CCompilerInvoker.class)) {
System.out.println("# Using native toolchain:");
ImageSingletons.lookup(CCompilerInvoker.class).compilerInfo.dump(x -> System.out.println("# " + x));
}
System.out.println("# Using CLibrary: " + ImageSingletons.lookup(LibCBase.class).getClass().getName());
}
FeatureImpl.BeforeAnalysisAccessImpl access = (FeatureImpl.BeforeAnalysisAccessImpl) a;
String fieldName = "VERSION_INFO";
try {
Field declaredField = VM.class.getDeclaredField(fieldName);
access.registerAsRead(declaredField);
} catch (NoSuchFieldException e) {
VMError.shouldNotReachHere(VM.class.getName() + " should have field " + fieldName);
}
nativeLibraries = access.getNativeLibraries();
}
@Override
public void afterAnalysis(AfterAnalysisAccess access) {
addCGlobalDataString("Target.Platform", ImageSingletons.lookup(Platform.class).getClass().getName());
addCGlobalDataString("Target.LibC", ImageSingletons.lookup(LibCBase.class).getClass().getName());
addCGlobalDataString("Target.Libraries", String.join("|", nativeLibraries.getLibraries()));
addCGlobalDataString("Target.StaticLibraries", nativeLibraries.getStaticLibraries().stream().map(Path::getFileName).map(Path::toString).collect(Collectors.joining("|")));
if (ImageSingletons.contains(CCompilerInvoker.class)) {
addCGlobalDataString("Target.CCompiler", ImageSingletons.lookup(CCompilerInvoker.class).compilerInfo.toString());
}
if (SubstrateOptions.DumpTargetInfo.getValue()) {
System.out.println("# Static libraries:");
Path current = Paths.get(".").toAbsolutePath();
nativeLibraries.getStaticLibraries().stream().map(current::relativize).map(Path::toString).forEach(x -> System.out.println("# " + x));
System.out.println("# Other libraries: " + String.join(",", nativeLibraries.getLibraries()));
}
CGlobalData<PointerBase> isStaticBinaryMarker = CGlobalDataFactory.createWord(WordFactory.unsigned(SubstrateOptions.StaticExecutable.getValue() ? 1 : 0), STATIC_BINARY_MARKER_SYMBOL_NAME);
CGlobalDataFeature.singleton().registerAsAccessedOrGet(isStaticBinaryMarker);
}
private static void addCGlobalDataString(String infoType, String content) {
String data = VM.class.getName() + "." + infoType + VM.valueSeparator + content;
String symbolName = "__svm_vm_" + infoType.toLowerCase().replace(".", "_");
CGlobalDataFeature.singleton().registerAsAccessedOrGet(CGlobalDataFactory.createCString(data, symbolName));
}
}