Merge pull request #1936 from esdrubal/DotNetRelativeOrAbsolute
[mono.git] / mono / tests / find_aot_method_in_amodule_stress / Template.py
1 import subprocess as sub
2 import sys
3
4 commands = {}
5
6 def typeof (typ):
7         return "typeof (" + typ + ")\n"
8
9 def inst (one, two):
10         return "TypeBucket.Foo <TypeBucket." + one + ", TypeBucket." + two + ">.CallMe ();\n"
11
12 def bucketCall (filePrefix):
13         return "TypeBucket." + filePrefix + ".CallMe ();\n"
14
15 def classDef (name):
16         return "public struct " + name + " {\t\n public int inner; \t\n}\n"
17
18 def run (cmd):
19         print(cmd)
20         child = sub.Popen (cmd, shell=True)
21         child.wait()
22         error = child.returncode
23         if error != 0:
24                 raise Exception ("Compilation error " + str(error))
25
26 def typeBucketInst (types, filePrefix):
27         accum = "\npublic class " + filePrefix + "{\n"
28         accum += "\tpublic static void CallMe () {\n"
29         for t1 in types:
30                 for t2 in types:
31                         accum += inst (t1, t2)
32         accum += "\t\n}\n}\n"
33         return accum
34
35 def makeGenericDef (fileName):
36         fileTemplate = """
37 using System.Runtime.CompilerServices;
38
39 namespace TypeBucket {
40         public class Foo <S, P> {
41                 public static void CallMe () {
42                         A ();
43                         B ();
44                         C ();
45                         D ();
46                         E ();
47                 }
48
49                 [MethodImplAttribute (MethodImplOptions.NoInlining)]
50                 public static void A () {
51                 }
52                 [MethodImplAttribute (MethodImplOptions.NoInlining)]
53                 public static void B () {
54                 }
55                 [MethodImplAttribute (MethodImplOptions.NoInlining)]
56                 public static void C () {
57                 }
58                 [MethodImplAttribute (MethodImplOptions.NoInlining)]
59                 public static void D () {
60                 }
61                 [MethodImplAttribute (MethodImplOptions.NoInlining)]
62                 public static void E () {
63                 }
64         }
65 }
66
67 """
68         csName = fileName + ".cs"
69         with open(csName, 'w') as f:
70                 f.write (fileTemplate);
71         cmd = commands ["mcs"] + " -t:library " + csName
72         run (cmd)
73         
74 def makeFile (prefix, insts, files, genericDefDll):
75         types = []
76         fileName = prefix + "TypeBucket"
77         csName = fileName + ".cs"
78         templatePrefix = "using TypeBucket;\n\tnamespace TypeBucket {\n"
79         with open(csName, 'w') as f:
80                 f.write (templatePrefix)
81                 for i in range(100):
82                         name = "classy" + prefix + str(i)
83                         f.write (classDef (name))
84                         types.append (name)
85                 f.write(typeBucketInst (types, fileName))
86                 f.write ("\n}\n")
87
88         insts.append(bucketCall (fileName))
89
90         cmd = commands ["mcs"] + " -t:library " + csName + " -r:" + genericDefDll
91         run (cmd)
92
93         files.append (fileName)
94
95
96 template_head = """
97 using TypeBucket;
98
99 class MainClass {
100         public static void Main () {
101                 """
102
103 template_tail = """
104         }
105 }
106 """
107
108 def main (bin_prefix):
109         commands ["mono"] = "MONO_PATH=. " + bin_prefix + "/bin/mono "
110         commands ["mcs"] = "MONO_PATH=. " + bin_prefix + "/bin/mcs "
111
112         generic_def_file = "Foo"
113         makeGenericDef (generic_def_file)
114
115         insts = []
116         files = [generic_def_file]
117         for prefix in ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"]:
118                 makeFile (prefix, insts, files, generic_def_file + ".dll")
119
120         template = template_head + "\n\t".join(insts) + template_tail
121
122         f = open("Hello.cs", 'w')
123         f.write (template)
124         f.close ();
125
126         cmd = commands ["mcs"] + " Hello.cs"
127         for f in files:
128                 cmd = cmd + " -r:" + f + ".dll"
129         run (cmd)
130
131
132         run (commands ["mono"] + "--aot=full " + "Hello.exe")
133
134         files.append ("mscorlib")
135         for f in files:
136                 run (commands ["mono"] + "--aot=full " +  f + ".dll")
137
138 if __name__ == "__main__":
139         main (sys.argv [1])