Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use native api to enable windows ansi support #4103

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.quarkus.runtime.logging;

import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;
import org.graalvm.nativeimage.c.CContext;
import org.graalvm.nativeimage.c.function.CFunction;
import org.graalvm.nativeimage.c.type.CIntPointer;
import org.graalvm.word.Pointer;

@CContext(WindowsConsoleDirectives.class)
@Platforms(Platform.WINDOWS.class)
public class ConsoleAPI {

static final int STD_INPUT_HANDLE = -10;
static final int STD_OUTPUT_HANDLE = -11;
static final int STD_ERROR_HANDLE = -12;

static final int ENABLE_PROCESSED_INPUT = 0x0001;
static final int ENABLE_LINE_INPUT = 0x0002;
static final int ENABLE_ECHO_INPUT = 0x0004;
static final int ENABLE_WINDOW_INPUT = 0x0008;
static final int ENABLE_MOUSE_INPUT = 0x0010;
static final int ENABLE_INSERT_MODE = 0x0020;
static final int ENABLE_QUICK_EDIT_MODE = 0x0040;
static final int ENABLE_EXTENDED_FLAGS = 0x0080;

// HANDLE WINAPI GetStdHandle(
// __in DWORD nStdHandle
// );
@CFunction
public static native Pointer GetStdHandle(int nStdHandle);

// BOOL WINAPI SetConsoleMode(
// _In_ HANDLE hConsoleHandle,
// _In_ DWORD dwMode);
@CFunction
public static native int SetConsoleMode(Pointer hConsoleHandle, int dwMode);

// BOOL WINAPI GetConsoleMode(
// _In_ HANDLE hConsoleHandle,
// _Out_ LPDWORD lpMode);
@CFunction
public static native void GetConsoleMode(Pointer hConsoleHandle, CIntPointer dwMode);

/**
* kernel32 = ctypes.windll.kernel32
* kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), 7)
**/
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.logging.Level;

import org.graalvm.nativeimage.ImageInfo;
import org.graalvm.word.Pointer;
import org.jboss.logmanager.EmbeddedConfigurator;
import org.jboss.logmanager.LogContext;
import org.jboss.logmanager.Logger;
Expand Down Expand Up @@ -107,7 +108,13 @@ private boolean hasColorSupport() {
// setConsoleMode.
// For now we turn it off to not generate noisy output for most
// users.
return false;
if (ImageInfo.inImageRuntimeCode()) {
Pointer getStdHandle = ConsoleAPI.GetStdHandle(ConsoleAPI.STD_OUTPUT_HANDLE);
ConsoleAPI.SetConsoleMode(getStdHandle, 7);
return true;
} else {
return false;
}
} else {
// Must be on some Unix variant or ANSI-enabled windows terminal...
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.quarkus.runtime.logging;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;
import org.graalvm.nativeimage.c.CContext;

import com.oracle.svm.core.util.VMError;

@Platforms(Platform.WINDOWS.class)
public class WindowsConsoleDirectives implements CContext.Directives {

private static final String[] windowsLibs = new String[] { "<windows.h>" };
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currently I'm getting an error I highly assumes is missing header files but not sure.
Error is:

Error: Error compiling query code (in C:\Users\max\AppData\Local\Temp\SVM-3772467201033451472\NativeInfoDirectives.cpp). Compiler command  CL C:\Users\max\AppData\Local\Temp\SVM-3772467201033451472\NativeInfoDirectives.cpp /FeC:\Users\max\AppData\Local\Temp\SVM-3772467201033451472\NativeInfoDirectives.exe output included error: [Microsoft (R) C/C++ Optimizing Compiler Version 16.00.30319.01 for x64, Copyright (C) Microsoft Corporation.  All rights reserved., ]
com.oracle.svm.core.util.UserError$UserException: Error compiling query code (in C:\Users\max\AppData\Local\Temp\SVM-3772467201033451472\NativeInfoDirectives.cpp). Compiler command  CL C:\Users\max\AppData\Local\Temp\SVM-3772467201033451472\NativeInfoDirectives.cpp /FeC:\Users\max\AppData\Local\Temp\SVM-3772467201033451472\NativeInfoDirectives.exe output included error: [Microsoft (R) C/C++ Optimizing Compiler Version 16.00.30319.01 for x64, Copyright (C) Microsoft Corporation.  All rights reserved., ]
        at com.oracle.svm.core.util.UserError.abort(UserError.java:114)
        at com.oracle.svm.hosted.c.NativeLibraries.reportErrors(NativeLibraries.java:196)
        at com.oracle.svm.hosted.NativeImageGenerator.processNativeLibraryImports(NativeImageGenerator.java:1512)
        at com.oracle.svm.hosted.NativeImageGenerator.setupNativeLibraries(NativeImageGenerator.java:993)
        at com.oracle.svm.hosted.NativeImageGenerator.setupNativeImage(NativeImageGenerator.java:827)
        at com.oracle.svm.hosted.NativeImageGenerator.doRun(NativeImageGenerator.java:520)
        at com.oracle.svm.hosted.NativeImageGenerator.lambda$run$0(NativeImageGenerator.java:440)
        at java.util.concurrent.ForkJoinTask$AdaptedRunnableAction.exec(ForkJoinTask.java:1386)
        at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
        at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
        at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
        at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)
Error: Image build request failed with exit status 1

waiting for oracle/graal#1675 to be fixed or someone magically figuring out what is wrong ;)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you show the content of C:\Users\max\AppData\Local\Temp\SVM-3772467201033451472\NativeInfoDirectives.cpp?


@Override
public boolean isInConfiguration() {
return Platform.includedIn(Platform.WINDOWS.class);
}

@Override
public List<String> getHeaderFiles() {
if (Platform.includedIn(Platform.WINDOWS.class)) {
List<String> result = new ArrayList<>(Arrays.asList(windowsLibs));
return result;
} else {
throw VMError.shouldNotReachHere("Unsupported OS");
}
}

@Override
public List<String> getMacroDefinitions() {
return Arrays.asList("_WIN64");
}

}