diff --git a/src/coreclr/crossgen-corelib.proj b/src/coreclr/crossgen-corelib.proj
index 363c82d242359c..ebe09a78aa9dad 100644
--- a/src/coreclr/crossgen-corelib.proj
+++ b/src/coreclr/crossgen-corelib.proj
@@ -161,6 +161,7 @@
$(CrossGenDllCmd) "--opt-cross-module:*"
$(CrossGenDllCmd) --composite
$(CrossGenDllCmd) --targetos:$(TargetOS)
+ $(CrossGenDllCmd) --target-allows-runtime-code-generation:$(FeatureDynamicCodeCompiled)
$(CrossGenDllCmd) -m:$(MergedMibcPath) --embed-pgo-data
$(CrossGenDllCmd) -O
diff --git a/src/coreclr/tools/aot/crossgen2/Crossgen2RootCommand.cs b/src/coreclr/tools/aot/crossgen2/Crossgen2RootCommand.cs
index 644fe3d92b02ff..396e5b42f6e896 100644
--- a/src/coreclr/tools/aot/crossgen2/Crossgen2RootCommand.cs
+++ b/src/coreclr/tools/aot/crossgen2/Crossgen2RootCommand.cs
@@ -94,6 +94,8 @@ internal class Crossgen2RootCommand : RootCommand
new("--maxgenericcyclebreadth") { DefaultValueFactory = _ => ReadyToRunCompilerContext.DefaultGenericCycleBreadthCutoff, Description = SR.GenericCycleBreadthCutoff };
public Option TargetOS { get; } =
new("--targetos") { Description = SR.TargetOSOption };
+ public Option TargetAllowsRuntimeCodeGeneration { get; } =
+ new("--target-allows-runtime-code-generation") { Description = SR.TargetAllowsRuntimeCodeGenerationOption };
public Option JitPath { get; } =
new("--jitpath") { Description = SR.JitPathOption };
public Option PrintReproInstructions { get; } =
@@ -199,6 +201,7 @@ public Crossgen2RootCommand(string[] args) : base(SR.Crossgen2BannerText)
Options.Add(GenericCycleBreadthCutoff);
Options.Add(TargetArchitecture);
Options.Add(TargetOS);
+ Options.Add(TargetAllowsRuntimeCodeGeneration);
Options.Add(JitPath);
Options.Add(PrintReproInstructions);
Options.Add(SingleMethodTypeName);
diff --git a/src/coreclr/tools/aot/crossgen2/Program.cs b/src/coreclr/tools/aot/crossgen2/Program.cs
index 691add6dd78b2a..75892ef120f613 100644
--- a/src/coreclr/tools/aot/crossgen2/Program.cs
+++ b/src/coreclr/tools/aot/crossgen2/Program.cs
@@ -78,7 +78,8 @@ public int Run()
(TargetArchitecture targetArchitecture, TargetOS targetOS, TargetAbi targetAbi) =
Helpers.GetTargetSpec(Get(_command.TargetArchitecture), Get(_command.TargetOS));
- bool targetAllowsRuntimeCodeGeneration = GetTargetAllowsRuntimeCodeGeneration(targetOS, targetArchitecture);
+ bool targetAllowsRuntimeCodeGeneration = Get(_command.TargetAllowsRuntimeCodeGeneration)
+ ?? GetTargetAllowsRuntimeCodeGeneration(targetOS, targetArchitecture);
// Crossgen2 is partial AOT and its pre-compiled methods can be thrown away at runtime if
// they mismatch in required ISAs or computed layouts of structs. On targets that allow
diff --git a/src/coreclr/tools/aot/crossgen2/Properties/Resources.resx b/src/coreclr/tools/aot/crossgen2/Properties/Resources.resx
index 8a34770d35e215..f07167b26a351f 100644
--- a/src/coreclr/tools/aot/crossgen2/Properties/Resources.resx
+++ b/src/coreclr/tools/aot/crossgen2/Properties/Resources.resx
@@ -282,6 +282,9 @@
Target architecture for cross compilation
+
+ Override whether the target supports runtime code generation
+
Target OS for cross compilation