Fix compiling EXE modules as native libraries (#78949)
authorMichal Strehovský <MichalStrehovsky@users.noreply.github.com>
Wed, 30 Nov 2022 02:05:04 +0000 (11:05 +0900)
committerGitHub <noreply@github.com>
Wed, 30 Nov 2022 02:05:04 +0000 (11:05 +0900)
When `--nativelib` is used together with a module that defines an entrypoint, we would incorrectly run some of the logic meant for EXEs. This is failure is only reachable when one constructs inputs to `ilc.exe` manually. The SDK doesn't allow constructing such inputs.

src/coreclr/tools/aot/ILCompiler/Program.cs

index 49cf0a7..a1c7c86 100644 (file)
@@ -182,14 +182,6 @@ namespace ILCompiler
                     compilationRoots.Add(new ExportedMethodsRootProvider(module));
                 }
 
-                string[] runtimeOptions = Get(_command.RuntimeOptions);
-                if (entrypointModule != null)
-                {
-                    compilationRoots.Add(new MainMethodRootProvider(entrypointModule, CreateInitializerList(typeSystemContext)));
-                    compilationRoots.Add(new RuntimeConfigurationRootProvider(runtimeOptions));
-                    compilationRoots.Add(new ExpectedIsaFeaturesRootProvider(instructionSetSupport));
-                }
-
                 bool nativeLib = Get(_command.NativeLib);
                 if (multiFile)
                 {
@@ -219,6 +211,7 @@ namespace ILCompiler
                     compilationGroup = new SingleFileCompilationModuleGroup();
                 }
 
+                string[] runtimeOptions = Get(_command.RuntimeOptions);
                 if (nativeLib)
                 {
                     // Set owning module of generated native library startup method to compiler generated module,
@@ -227,6 +220,12 @@ namespace ILCompiler
                     compilationRoots.Add(new RuntimeConfigurationRootProvider(runtimeOptions));
                     compilationRoots.Add(new ExpectedIsaFeaturesRootProvider(instructionSetSupport));
                 }
+                else if (entrypointModule != null)
+                {
+                    compilationRoots.Add(new MainMethodRootProvider(entrypointModule, CreateInitializerList(typeSystemContext)));
+                    compilationRoots.Add(new RuntimeConfigurationRootProvider(runtimeOptions));
+                    compilationRoots.Add(new ExpectedIsaFeaturesRootProvider(instructionSetSupport));
+                }
 
                 foreach (var rdXmlFilePath in Get(_command.RdXmlFilePaths))
                 {