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

[native-image] HelloWorld fails to build on Windows #948

Closed
Nik-- opened this issue Jan 30, 2019 · 6 comments
Closed

[native-image] HelloWorld fails to build on Windows #948

Nik-- opened this issue Jan 30, 2019 · 6 comments
Assignees

Comments

@Nik--
Copy link

Nik-- commented Jan 30, 2019

When I try to build the HelloWorld class using native-image on windows, I get this error:

$ mx native-image HelloWorld -H:+ReportExceptionStackTraces
[helloworld:6536]    classlist:  11,957.75 ms
[helloworld:6536]        (cap):   2,106.63 ms
[helloworld:6536]        setup:   4,052.94 ms
Error: Error compiling query code (in C:\Users\Nik\AppData\Local\Temp\SVM-5554957858263508583\LibCHelperDirectives.cpp). Compiler command  CL C:\Users\Nik\AppData\Local\Temp\SVM-5554957858263508583\LibCHelperDirectives.cpp /FeC:\Users\Nik\AppData\Local\Temp\SVM-5554957858263508583\LibCHelperDirectives.exe output included error: [Microsoft (R) C/C++ Optimizing Compiler Version 19.16.27024.1 for x64, Copyright (C) Microsoft Corporation.  All rights reserved., ]
com.oracle.svm.core.util.UserError$UserException: Error compiling query code (in C:\Users\Nik\AppData\Local\Temp\SVM-5554957858263508583\LibCHelperDirectives.cpp). Compiler command  CL C:\Users\Nik\AppData\Local\Temp\SVM-5554957858263508583\LibCHelperDirectives.cpp /FeC:\Users\Nik\AppData\Local\Temp\SVM-5554957858263508583\LibCHelperDirectives.exe output includederror: [Microsoft (R) C/C++ Optimizing Compiler Version 19.16.27024.1 for x64, Copyright (C) Microsoft Corporation.  All rights reserved., ]
        at com.oracle.svm.core.util.UserError.abort(UserError.java:132)
        at com.oracle.svm.hosted.c.NativeLibraries.reportErrors(NativeLibraries.java:186)
        at com.oracle.svm.hosted.NativeImageGenerator.processNativeLibraryImports(NativeImageGenerator.java:1415)
        at com.oracle.svm.hosted.NativeImageGenerator.setupNativeLibraries(NativeImageGenerator.java:958)
        at com.oracle.svm.hosted.NativeImageGenerator.setupNativeImage(NativeImageGenerator.java:800)
        at com.oracle.svm.hosted.NativeImageGenerator.doRun(NativeImageGenerator.java:490)
        at com.oracle.svm.hosted.NativeImageGenerator.lambda$run$0(NativeImageGenerator.java:410)
        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 building with exit status 1

EDIT: After debugging the input stream and output stream of the "cl.exe", I've got the following result:

ed local variable 'tmp_jvalue_10' used
IS LINE: Microsoft (R) Incremental Linker Version 14.16.27024.1
IS LINE: Copyright (C) Microsoft Corporation.  All rights reserved.
IS LINE:
IS LINE: /out:C:\Users\Nik\AppData\Local\Temp\SVM-6216101280912814773\JNIHeaderDirectives.exe
IS LINE: JNIHeaderDirectives.obj
IS LINE: LINK : fatal error LNK1104: cannot open file 'LIBCMT.lib'
ES LINE: Microsoft (R) C/C++ Optimizing Compiler Version 19.16.27024.1 for x64
ES LINE: Copyright (C) Microsoft Corporation.  All rights reserved.
ES LINE:
[helloworld:4404]        (cap):   1,791.65 ms
[helloworld:4404]        setup:   2,639.81 ms
Error: Error compiling query code (in C:\Users\Nik\AppData\Local\Temp\SVM-6216101280912814773\JNIHeaderDirectives.cpp). Compiler command  CL -IC:\Users\Nik\Desktop\labsjdk1.8.0_192-jvmci-0.54\include\win32 C:\Users\Nik\AppData\Local\Temp\SVM-6216101280912814773\JNIHeaderDirectives.cpp /FeC:\Users\Nik\AppData\Local\Temp\SVM-6216101280912814773\JNIHeaderDirectives.exe output included error: [Microsoft (R) C/C++ Optimizing Compiler Version 19.16.27024.1 for x64, Copyright (C) MicrosoftCorporation.  All rights reserved., ]
Error: Use -H:+ReportExceptionStackTraces to print stacktrace of underlying exception
Error: Image building with exit status 1

Looks like native-image has failed to parse the line LINK : fatal error LNK1104: cannot open file 'LIBCMT.lib' in the input stream as error line.

By applying this patch, the actual error is properly being displayed:

diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/c/codegen/CCompilerInvoker.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/c/codegen/CCompilerInvoker.java
index e4095240531..2d9ee774b8d 100644
--- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/c/codegen/CCompilerInvoker.java
+++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/c/codegen/CCompilerInvoker.java
@@ -64,19 +64,29 @@ public class CCompilerInvoker {
             InputStream es = compilingProcess.getErrorStream();
             InputStream is = compilingProcess.getInputStream();
 
-            List<String> lines = FileUtils.readAllLines(es);
-            FileUtils.readAllLines(is);
+            List<String> errorLines = FileUtils.readAllLines(es);
+            List<String> inputLines = FileUtils.readAllLines(is);
             int status = compilingProcess.waitFor();
 
             boolean errorReported = false;
-            for (String line : lines) {
-                if (line.contains(": error:") || line.contains(": fatal error:")) {
+            for (String line : errorLines) {
+                if (line.contains(": error:") || line.contains(": fatal error")) {
                     reportCompilerError(source, line);
                     errorReported = true;
                 }
             }
+
+            if (!errorReported) {
+                for (String line : inputLines) {
+                    if (line.contains(": error:") || line.contains(": fatal error")) {
+                        reportCompilerError(source, line);
+                        errorReported = true;
+                    }
+                }
+            }
+            
             if (status != 0 && !errorReported) {
-                reportCompilerError(source, lines.toString());
+                reportCompilerError(source, errorLines.toString());
             }
             compilingProcess.destroy();
             return target;

Ok, after dealing with missing includes and libs, I've managed to get further into the native-image build, just to be greeted by this error:

 Creating library C:\Users\Nik\Desktop\graal\graal\substratevm\helloworld.liband object C:\Users\Nik\Desktop\graal\graal\substratevm\helloworld.exp
net.lib(net_util_md.obj) : warning LNK4217: locally defined symbol printf imported in function dumpAddr
net.lib(NetworkInterface.obj) : error LNK2019: unresolved external symbol __imp__snprintf_s referenced in function enumInterfaces
java.lib(Console_md.obj) : error LNK2001: unresolved external symbol __imp_sprintf
net.lib(TwoStacksPlainDatagramSocketImpl.obj) : error LNK2001: unresolved external symbol __imp_sprintf
net.lib(ResolverConfigurationImpl.obj) : error LNK2001: unresolved external symbol __imp_sprintf
net.lib(net_util_md.obj) : error LNK2001: unresolved external symbol __imp_sprintf
net.lib(NetworkInterface_winXP.obj) : error LNK2001: unresolved external symbol__imp_sprintf
java.lib(jni_util.obj) : error LNK2019: unresolved external symbol __imp_fprintf referenced in function JNU_PrintString
java.lib(jni_util.obj) : error LNK2019: unresolved external symbol __imp___iob_func referenced in function JNU_PrintString
C:\Users\Nik\Desktop\graal\graal\substratevm\helloworld.exe : fatal error LNK1120: 4 unresolved externals

        at com.oracle.svm.hosted.image.NativeBootImageViaCC.write(NativeBootImageViaCC.java:290)
        at com.oracle.svm.hosted.NativeImageGenerator.doRun(NativeImageGenerator.java:618)
        at com.oracle.svm.hosted.NativeImageGenerator.lambda$run$0(NativeImageGenerator.java:410)
        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 building with exit status 1
@olpaw
Copy link
Member

olpaw commented Jan 31, 2019

Please have a look at #946 (comment)

If after carefully following the instructions provided there you still fail to build a simple HelloWorld image on Windows feel free to reopen this issue.

@olpaw olpaw closed this as completed Jan 31, 2019
@pquiring
Copy link

I think legacy_stdio_definitions.lib needs to be added to the list of libraries in https://github.com/oracle/graal/blob/master/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/image/NativeBootImageViaCC.java
right after iphlpapi.lib

@olpaw
Copy link
Member

olpaw commented Jul 11, 2019

@pquiring interesting. If we add that would that allow us to work with later SDKs OOTB?

@pquiring
Copy link

Also found one more missing import (__imp___iob_func) which is easy to generate.

Create this file called stdio.cpp

#include <stdio.h>
extern "C" { __declspec(dllexport) FILE __iob_func[3] = { *stdin,*stdout,*stderr }; }

Steps to follow:
Copy iphlpapi.lib and legacy_stdio_definitions.lib to project folder from SDK lib folder.
compile stdio.cpp
cl /LD stdio.cpp
Generate new library
lib iphlpapi.lib legacy_stdio_definitions.lib stdio.lib /out:new.lib
Copy new.lib to iphlpapi.lib in your SDK folder (requires admin rights , make a backup of orginal)

Works for me!

@pquiring
Copy link

Only downside is that generated project now requires stdio.dll but it works with latest SDK.

@pquiring
Copy link

To use latest SDK OOTB would requires adding legacy_stdio_definitions.lib to the list of LIB files and also compiling the substrateVM against latest SDK to use the newer stdio names (that is what the __iob_func stuff is).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants