[Tizen] Add support for gbs build for i586, x86_64, armv7l, armv7hl, aarch64 (includi...
[platform/upstream/dotnet/runtime.git] / src / tests / readytorun / coreroot_determinism / Program.cs
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3
4 using System;
5 using System.Collections.Generic;
6 using System.Diagnostics;
7 using System.IO;
8 using System.Linq;
9 using System.Runtime.InteropServices;
10
11 internal class Program
12 {
13     public static int CompareDLLs(string folder1, string folder2)
14     {
15         int result = 100;
16
17         string superIlcFolder1 = Directory.GetDirectories(folder1, "CPAOT*").First();
18         string superIlcFolder2 = Directory.GetDirectories(folder2, "CPAOT*").First();
19
20         // Check for files that failed compilation with one of the seeds but not the other
21         HashSet<string> uniqueFilenames = new HashSet<string>(Directory.GetFiles(superIlcFolder1, "*.dll").Select(Path.GetFileName));
22         uniqueFilenames.SymmetricExceptWith(Directory.GetFiles(superIlcFolder2, "*.dll").Select(Path.GetFileName));
23         foreach (string uniqueFilename in uniqueFilenames)
24         {
25             Console.WriteLine($"{uniqueFilename} was found in only one of the output folders.");
26             result = 1;
27         }
28
29         foreach (string filename in Directory.GetFiles(superIlcFolder1, "*.dll").Select(Path.GetFileName))
30         {
31             if (uniqueFilenames.Contains(filename))
32                 continue;
33
34             byte[] file1 = File.ReadAllBytes(Path.Combine(superIlcFolder1, Path.GetFileName(filename)));
35             byte[] file2 = File.ReadAllBytes(Path.Combine(superIlcFolder2, Path.GetFileName(filename)));
36
37             if (file1.Length != file2.Length)
38             {
39                 Console.WriteLine(filename);
40                 Console.WriteLine($"Expected ReadyToRun'd files to be identical but they have different sizes ({file1.Length} and {file2.Length})");
41                 result = 1;
42                 continue;
43             }
44
45             int byteDifferentCount = 0;
46             for (int i = 0; i < file1.Length; ++i)
47             {
48                 if (file1[i] != file2[i])
49                 {
50                     ++byteDifferentCount;
51                 }
52             }
53
54             if (byteDifferentCount > 0)
55             {
56                 result = 1;
57                 Console.WriteLine($"Error: Found {byteDifferentCount} different bytes in {filename}");
58                 continue;
59             }
60
61             Console.WriteLine($"Files of length {file1.Length} were identical.");
62         }
63         return result;
64     }
65
66     public static string OSExeSuffix(string path) => (OperatingSystem.IsWindows() ? path + ".exe" : path);
67
68     private static void PrepareCompilationInputFolder(string coreRootFolder, string compilationInputFolder)
69     {
70         if (Directory.Exists(compilationInputFolder))
71         {
72             Directory.Delete(compilationInputFolder, true);
73         }
74         Directory.CreateDirectory(compilationInputFolder);
75
76         CopyDeterminismTestAssembly(coreRootFolder, compilationInputFolder, "System.Private.CoreLib.dll");
77     }
78
79     private static void CopyDeterminismTestAssembly(string coreRootFolder, string compilationInputFolder, string fileName)
80     {
81         File.Copy(Path.Combine(coreRootFolder, fileName), Path.Combine(compilationInputFolder, fileName));
82     }
83
84     public static bool CompileWithSeed(int seed, string coreRootPath, string compilationInputFolder, string outDir)
85     {
86         string superIlcPath = Path.Combine(coreRootPath, "R2RTest", "R2RTest.dll");
87         string coreRunPath = Path.Combine(coreRootPath, OSExeSuffix("corerun"));
88
89         Console.WriteLine($"================================== Compiling with seed {seed} ==================================");
90         Environment.SetEnvironmentVariable("CoreRT_DeterminismSeed", seed.ToString());
91         if (Directory.Exists(outDir))
92         {
93             Directory.Delete(outDir, true);
94         }
95         Directory.CreateDirectory(outDir);
96
97         string crossgen2OptsR2RTest = Environment.GetEnvironmentVariable("CrossGen2OptionsR2RTest");
98
99         ProcessStartInfo processStartInfo = new ProcessStartInfo(coreRunPath, $"{superIlcPath} compile-directory -cr {coreRootPath} -in {compilationInputFolder} {crossgen2OptsR2RTest} --nojit --noexe --large-bubble --release --nocleanup -out {outDir}");
100         var process = Process.Start(processStartInfo);
101         process.WaitForExit();
102         if (process.ExitCode != 0)
103         {
104             Console.WriteLine($"Compilation failed. {processStartInfo.FileName} {processStartInfo.Arguments} failed with exit code {process.ExitCode}");
105         }
106         return 0 == process.ExitCode;
107     }
108
109     public static int Main()
110     {
111         string coreRootPath = Environment.GetEnvironmentVariable("CORE_ROOT");
112         string compilationInputFolder = "TestAssemblies";
113         PrepareCompilationInputFolder(coreRootPath, compilationInputFolder);
114         if (!CompileWithSeed(1, coreRootPath, compilationInputFolder, "seed1"))
115             return 1;
116         if (!CompileWithSeed(2, coreRootPath, compilationInputFolder, "seed2"))
117             return 1;
118         return CompareDLLs("seed1", "seed2");
119     }
120 }