[mono] Improve mono.proj, auto-detect build configurations (#31739)
authorEgor Bogatov <egorbo@gmail.com>
Thu, 6 Feb 2020 15:34:16 +0000 (18:34 +0300)
committerGitHub <noreply@github.com>
Thu, 6 Feb 2020 15:34:16 +0000 (18:34 +0300)
* Auto-detect build configuration in Makefile

* Improve RunCoreClrTests target

* Clone .dotnet to .dotnet-mono

* Implement Console

.gitignore
src/mono/mono.proj
src/mono/netcore/Makefile
src/mono/netcore/System.Private.CoreLib/System.Private.CoreLib.csproj
src/mono/netcore/System.Private.CoreLib/src/Mono/Console.Mono.cs [new file with mode: 0644]
src/mono/netcore/init-tools.sh [new file with mode: 0644]
src/mono/netcore/sample/HelloWorld/HelloWorld.csproj

index fa697b5..e111924 100644 (file)
@@ -4,6 +4,7 @@ syntax: glob
 
 # Tool Runtime Dir
 .dotnet/
+.dotnet-mono/
 .packages/
 .tools/
 
index 5d54552..32b1c10 100644 (file)
@@ -9,6 +9,8 @@
     <DotNetExec Condition="'$(DotNetExec)' == ''">dotnet</DotNetExec>
     <LocalDotnetDir>..\..\.dotnet</LocalDotnetDir>
     <LocalDotnet>$(LocalDotnetDir)\$(DotNetExec)</LocalDotnet>
+    <LocalMonoDotnetDir>..\..\.dotnet-mono</LocalMonoDotnetDir>
+    <LocalMonoDotnet>$(LocalMonoDotnetDir)\$(DotNetExec)</LocalMonoDotnet>
     <ScriptExt Condition="'$(OS)' == 'Windows_NT'">.cmd</ScriptExt>
     <ScriptExt Condition="'$(OS)' != 'Windows_NT'">.sh</ScriptExt>
     <_CoreClrFileName Condition="'$(TargetsWindows)' == 'true'">coreclr.dll</_CoreClrFileName>
     <Exec Command="msvc\run-msbuild.bat build $(_MonoBuildPlatform) $(Configuration) sgen &quot;$(_MonoBuildParams)&quot; msvc\mono-netcore.sln" IgnoreStandardErrorWarningFormat="true" />
   </Target>
 
+  <Target Name="BuildCoreLib">
+    <MSBuild Projects="$(MonoProjectRoot)netcore\System.Private.CoreLib\System.Private.CoreLib.csproj"
+             Properties="Configuration=$(Configuration)"
+             Targets="Build" />
+  </Target>
+
   <!-- General targets -->
   <Target Name="Build" DependsOnTargets="BuildMonoRuntimeUnix;BuildMonoRuntimeWindows">
     <PropertyGroup>
 
   <Target Name="Test" />
 
-  <!-- Copy Mono runtime bits to the local .dotnet dir for local experiments (temp solution) -->
-  <Target Name="PatchLocalDotnet" DependsOnTargets="ValidateLocalDotnet">
-    <PropertyGroup>
-      <LocalDotnetRuntimeDir>$([System.IO.Directory]::GetDirectories("$(LocalDotnetDir)\shared\Microsoft.NETCore.App")[0])</LocalDotnetRuntimeDir>
-    </PropertyGroup>
+  <!-- Copy Mono runtime bits to the local .dotnet-mono (clone of .dotnet) dir for local experiments (temp solution) -->
+  <Target Name="PatchLocalMonoDotnet" DependsOnTargets="ValidateLocalDotnet">
     <ItemGroup>
+      <_LocalDotnetFiles Include="$(LocalDotnetDir)\**\*.*" />
       <_MonoRuntimeArtifacts Include="$(BinDir)\*.*" />
     </ItemGroup>
-    <Copy SourceFiles="$(_MonoRuntimeArtifacts)"
-          DestinationFolder="$(LocalDotnetRuntimeDir)"
+    <Error Condition="@(_MonoRuntimeArtifacts->Count()) &lt; 2" Text="Mono artifacts were not found at $(BinDir)" />
+    <!-- copy .dotnet to .dotnet-mono if it doesn't exist -->
+    <Copy SourceFiles="@(_LocalDotnetFiles)"
+          DestinationFolder="$(LocalMonoDotnetDir)\%(RecursiveDir)"
           SkipUnchangedFiles="true" />
-  </Target>
-
-  <!-- Copy Coreclr runtime bits to the local .dotnet dir -->
-  <Target Name="RestoreLocalDotnet" DependsOnTargets="ValidateLocalDotnet">
-    <Copy SourceFiles="$(CoreCLRArtifactsPath)\System.Private.CoreLib.dll"
-          DestinationFiles="$(LocalDotnetRuntimeDir)\System.Private.CoreLib.dll" />
-    <Copy SourceFiles="$(CoreCLRArtifactsPath)\$(_CoreClrFileName)"
-          DestinationFiles="$(LocalDotnetRuntimeDir)\$(_CoreClrFileName)" />
+    <Copy SourceFiles="@(_MonoRuntimeArtifacts)"
+          DestinationFolder="$([System.IO.Directory]::GetDirectories('$(LocalMonoDotnetDir)\shared\Microsoft.NETCore.App')[0])" />
   </Target>
 
   <!-- Copy Mono runtime bits to the coreclr's Core_Root in order to run runtime tests -->
     <ItemGroup>
       <_MonoRuntimeArtifacts Include="$(BinDir)\*.*" />
     </ItemGroup>
+    <Error Condition="@(_MonoRuntimeArtifacts->Count()) &lt; 2" Text="Mono artifacts were not found at $(BinDir)" />
     <Copy SourceFiles="@(_MonoRuntimeArtifacts)"
-          DestinationFolder="$(CoreClrTestCoreRoot)" 
-          SkipUnchangedFiles="true"/>
+          DestinationFolder="$(CoreClrTestCoreRoot)" />
   </Target>
 
   <!-- Copy Coreclr runtime bits back to Core_Root -->
   </Target>
 
   <!-- Run netcore\sample\HelloWorld sample using Mono Runtime -->
-  <Target Name="RunSample" DependsOnTargets="PatchLocalDotnet">
-    <Exec Command="$(LocalDotnet) run -c Release -f $(NetCoreAppCurrent) -p $(MonoProjectRoot)netcore\sample\HelloWorld" />
+  <Target Name="RunSample" DependsOnTargets="PatchLocalMonoDotnet">
+    <PropertyGroup>
+      <EnvVars Condition="'$(OS)' != 'Windows_NT'"><![CDATA[
+      COMPlus_DebugWriteToStdErr=1 \
+      MONO_ENV_OPTIONS="" \
+      ]]>
+      </EnvVars>
+    </PropertyGroup>
+    <Exec Condition="'$(OS)' == 'Windows_NT'" Command="$(EnvVars) $(LocalMonoDotnet) run -c $(Configuration) -p $(MonoProjectRoot)netcore/sample/HelloWorld" />
+    <Exec Condition="'$(OS)' != 'Windows_NT'" Command="$(EnvVars) $(LocalMonoDotnet) run -c $(Configuration) -p $(MonoProjectRoot)netcore\sample\HelloWorld" />
   </Target>
 
   <!-- Run CoreCLR runtime test using testhost -->
     <Exec Command="$(CoreClrTest) -coreroot=&quot;$(CoreClrTestCoreRoot)&quot;"/>
   </Target>
 
-  <!-- Make sure coreclr tests are built (in $(CoreClrTestConfig) configuration), e.g. 
-    *nix: `cd ../coreclr && ./build.sh -release && ./build-testh.sh -release` 
-    Windows: `cd ../coreclr && build.cmd -release` -->
+  <!-- Run coreclr tests using runtest.py -->
   <Target Name="RunCoreClrTests" DependsOnTargets="ValidateLocalDotnet;PatchCoreClrCoreRoot">
-    <ItemGroup>        
-        <CoreClrTests Include="$(ArtifactsDir)tests\coreclr\$(OSGroup).$(Platform).$(CoreClrTestConfig)\**\*$(ScriptExt)" />   
-    </ItemGroup>       
-    <Exec Condition="'$(OS)' == 'Windows_NT'" Command="&quot;%(CoreClrTests.Identity)&quot; -coreroot &quot;$(CoreClrTestCoreRoot)&quot;" ContinueOnError="WarnAndContinue" />
-    <Exec Condition="'$(OS)' != 'Windows_NT'" Command="bash &quot;%(CoreClrTests.Identity)&quot; -coreroot=&quot;$(CoreClrTestCoreRoot)&quot;" ContinueOnError="WarnAndContinue" />
-  </Target>
-
-  <!-- Show summary for coreclr tests -->
-  <Target Name="CoreClrTestsSummary">
-    <Exec Command="python $(MonoProjectRoot)..\coreclr\tests\runtest.py --analyze_results_only -test_location $(ArtifactsDir)tests\coreclr\$(OSGroup).$(Platform).$(CoreClrTestConfig) -build_type $(CoreClrTestConfig)" />
+    <Exec Condition="'$(OS)' == 'Windows_NT'" Command="$(MonoProjectRoot)..\coreclr\tests\runtest.cmd $(CoreClrTestConfig)" ContinueOnError="ErrorAndContinue" />
+    <Exec Condition="'$(OS)' != 'Windows_NT'" Command="$(MonoProjectRoot)../coreclr/tests/./runtest.sh $(CoreClrTestConfig)" ContinueOnError="ErrorAndContinue" />
   </Target>
 
-  <Target Name="RunBenchmarks">
+  <Target Name="RunBenchmarks" DependsOnTargets="PatchLocalMonoDotnet">
     <Error Condition="$(BenchmarksRepo) == ''" Text="BenchmarksRepo variable is not set" />
-    <Exec WorkingDirectory="$(BenchmarksRepo)\src\benchmarks\micro" Command="$(LocalDotnet) run -c Release -f $(NetCoreAppCurrent) --cli $(LocalDotnet)" />
+    <Exec WorkingDirectory="$(BenchmarksRepo)\src\benchmarks\micro" Command="$(LocalDotnet) run -c Release -f $(NetCoreAppCurrent) --cli $(LocalMonoDotnet)" />
   </Target>
 
   <Target Name="ValidateLocalDotnet">
index 57aa399..de287eb 100644 (file)
@@ -1,38 +1,49 @@
-DOTNET=../../../.dotnet/dotnet
+DOTNET := $(shell bash init-tools.sh | tail -1)
+# DOTNET_MONO is a copy of DOTNET (local .dotnet) with Mono Runtime bits (see patch-mono-dotnet rule)
+DOTNET_MONO = ../../../.dotnet-mono/dotnet
 
-# run sample using local .dotnet (will be patched with Mono Runtime)
-run-sample:
-       $(DOTNET) msbuild /t:RunSample ../mono.proj
+CORECLR_TESTS_CONFIG=Release
+MONO_RUNTIME_CONFIG=Release
+
+# auto detect configurations for mono runtime and coreclr tests
+ifeq ($(words $(wildcard ../../../artifacts/bin/mono/*.*.*)), 1)
+       MONO_RUNTIME_CONFIG := $(word 3,$(subst ., ,$(notdir $(wildcard ../../../artifacts/bin/mono/*.*.*))))
+endif
+
+ifeq ($(words $(wildcard ../../../artifacts/tests/coreclr/*.*.*)), 1)
+       CORECLR_TESTS_CONFIG := $(word 3,$(subst ., ,$(notdir $(wildcard ../../../artifacts/tests/coreclr/*.*.*))))
+endif
+
+MONO_PROJ=/p:CoreClrTestConfig=$(CORECLR_TESTS_CONFIG) /p:Configuration=$(MONO_RUNTIME_CONFIG) ../mono.proj
+
+# run sample using local .dotnet-mono
+run-sample: patch-mono-dotnet
+       COMPlus_DebugWriteToStdErr=1 $(DOTNET_MONO) run -c Debug -p sample/HelloWorld
 
-# run sample using dotnet from PATH
 run-sample-coreclr:
-       dotnet run -c Release -p sample/HelloWorld -f netcoreapp3.1
+       $(DOTNET) run -c Debug -p sample/HelloWorld
 
-runtime:
-       $(DOTNET) msbuild /t:Build ../mono.proj
+bcl corelib:
+       $(DOTNET) msbuild /t:BuildCoreLib $(MONO_PROJ)
 
-# temp: makes $(DOTNET) to use mono runtime (to run real-world apps using '$(DOTNET) run')
-patch-local-dotnet:
-       $(DOTNET) msbuild /t:PatchLocalDotnet ../mono.proj
+runtime:
+       $(DOTNET) msbuild /t:Build $(MONO_PROJ)
 
-restore-local-dotnet:
-       $(DOTNET) msbuild /t:RestoreLocalDotnet ../mono.proj
+# call it if you want to use $(DOTNET_MONO) in this Makefile
+patch-mono-dotnet:
+       $(DOTNET) msbuild /t:PatchLocalMonoDotnet $(MONO_PROJ)
 
 # run specific coreclr test, e.g.:
-# make run-tests-coreclr CoreClrTest="bash ../../artifacts/tests/coreclr/OSX.x64.Checked/JIT/opt/InstructionCombining/DivToMul/DivToMul.sh"
+# make run-tests-coreclr CoreClrTest="bash ../../artifacts/tests/coreclr/OSX.x64.Release/JIT/opt/InstructionCombining/DivToMul/DivToMul.sh"
 run-tests-coreclr:
-       $(DOTNET) msbuild /t:RunCoreClrTest /p:CoreClrTest="$(CoreClrTest)" ../mono.proj
+       $(DOTNET) msbuild /t:RunCoreClrTest /p:CoreClrTest="$(CoreClrTest)" $(MONO_PROJ)
 
 # run all coreclr tests
 run-tests-coreclr-all:
-       $(DOTNET) msbuild /t:RunCoreClrTests ../mono.proj
-
-# show summary for coreclr tests
-tests-coreclr-summary:
-       $(DOTNET) msbuild /t:CoreClrTestsSummary ../mono.proj
+       $(DOTNET) msbuild /t:RunCoreClrTests $(MONO_PROJ)
 
 # run 'dotnet/performance' benchmarks
 # e.g. 'make run-benchmarks BenchmarksRepo=/prj/performance'
 # you can append BDN parameters at the end, e.g. ` -- --filter Burgers --keepFiles`
 run-benchmarks: patch-local-dotnet
-       $(DOTNET) msbuild /t:RunBenchmarks /p:BenchmarksRepo=$(BenchmarksRepo)
\ No newline at end of file
+       $(DOTNET) msbuild /t:RunBenchmarks /p:BenchmarksRepo=$(BenchmarksRepo)
index 388eea8..45e60e8 100644 (file)
 
   <!-- Sources -->
   <ItemGroup>
+      <Compile Include="$(BclSourcesRoot)\Mono\Console.Mono.cs" />
       <Compile Include="$(BclSourcesRoot)\Mono\MonoListItem.cs" />
       <Compile Include="$(BclSourcesRoot)\Mono\MonoDomain.cs" />
       <Compile Include="$(BclSourcesRoot)\Mono\MonoDomainSetup.cs" />
diff --git a/src/mono/netcore/System.Private.CoreLib/src/Mono/Console.Mono.cs b/src/mono/netcore/System.Private.CoreLib/src/Mono/Console.Mono.cs
new file mode 100644 (file)
index 0000000..396dfec
--- /dev/null
@@ -0,0 +1,19 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System;
+using System.Diagnostics;
+
+namespace Internal
+{
+    // Some CoreCLR tests use it for internal printf-style debugging in System.Private.CoreLib
+    public static class Console
+    {
+        public static void Write(string? s) => DebugProvider.WriteCore(s);
+
+        public static void WriteLine(string? s) => Write(s + Environment.NewLineConst);
+
+        public static void WriteLine() => Write(Environment.NewLineConst);
+    }
+}
\ No newline at end of file
diff --git a/src/mono/netcore/init-tools.sh b/src/mono/netcore/init-tools.sh
new file mode 100644 (file)
index 0000000..ae2ed15
--- /dev/null
@@ -0,0 +1,7 @@
+#!/usr/bin/env bash
+
+# always ignore system dotnet
+export use_installed_dotnet_cli=false
+. "../../../eng/common/tools.sh"
+InitializeDotNetCli true
+which dotnet
\ No newline at end of file
index af90cfd..521875c 100644 (file)
@@ -3,7 +3,7 @@
   <PropertyGroup>
     <OutputType>Exe</OutputType>
     <OutputPath>bin</OutputPath>
-    <TargetFrameworks>$(NetCoreAppCurrent);netcoreapp3.1</TargetFrameworks>
+    <TargetFramework>$(NetCoreAppCurrent)</TargetFramework>
     <DebugType>full</DebugType>
   </PropertyGroup>