* roottypes.cs: Rename from tree.cs.
[mono.git] / mono / metadata / make-bundle.pl
1 #!/usr/bin/perl -w
2 # Copyright (C) 2003 Ximian, Inc.
3 # Paolo Molaro (lupus@ximian.com)
4 #
5 # Create an header file to be included in the mono libraries to
6 # bundle assemblies inside the runtime.
7 # The template file contains a list of assemblies, one per line,
8 # with the name followed by a ':' and the filename.
9 # Lines starting with '#' and empty lines are ignored.
10 # See sample-bundle for an example.
11 # We need to use an assembly file because gcc can't handle large arrays:-(
12
13 if ($#ARGV != 2) {
14         die "Usage: make-bundle.pl template headerfile.h asm-file\n";
15 }
16
17 my $template = $ARGV [0];
18 my $header = $ARGV [1];
19 my $output = $ARGV [2];
20 my %assemblies = ();
21
22 my $line = 0;
23 open (T, $template) || die "Cannot open bundle template: $!\n";
24 while (<T>) {
25         ++$line;
26         next if (/^\s*#/);
27         next if (/^\s*$/);
28         if (/^([a-zA-Z0-9-.]+):\s*(.+?)\s*$/) {
29                 my ($name, $filename) = ($1, $2);
30                 if (exists $assemblies {$name}) {
31                         die "Assembly $name defined multiple times.\n";
32                 } else {
33                         $assemblies {$name} = $filename;
34                 }
35         } else {
36                 die "Unknown format at line $line: $_";
37         }
38 }
39 close (T);
40
41 open (O, ">$output.tmp") || die "Cannot open $output: $!\n";
42 open (H, ">$header.tmp") || die "Cannot open $output: $!\n";
43 print H <<"EOF";
44 /* File generated by make-bundle: do not edit! */
45
46 #ifndef __MONO_BUNDLE_H__
47 #define __MONO_BUNDLE_H__
48
49 typedef struct {
50         const char *name;
51         const unsigned char *data;
52         const unsigned int size;
53 } MonoBundledAssembly;
54
55 EOF
56
57 my $bundle_entries = "";
58
59 foreach my $name (sort keys %assemblies) {
60         my $file = $assemblies {$name};
61         my ($nread, $buf, $i, $cname, $need_eol, $size);
62         $cname = $name;
63         $cname =~ s/[-.]/_/g;
64         open (F, $file) || die "Cannot open $file: $!\n";
65         $size = -s F;
66 #       print O "/* assembly $name from $file */\n";
67 #       print O "static const unsigned char assembly_data_$cname [] = {\n";
68         print O ".globl assembly_data_$cname\n";
69         print O "\t.section .rodata\n";
70         print O "\t.align 32\n";
71         print O "\t.type assembly_data_$cname, \@object\n";
72         print O "\t.size assembly_data_$cname, $size\n";
73         print O "assembly_data_$cname:\n";
74         print H "extern const unsigned char assembly_data_$cname [];\n";
75         print H "static const MonoBundledAssembly assembly_bundle_$cname = {\"$name\", assembly_data_$cname, $size};\n";
76         $bundle_entries .= "\t&assembly_bundle_$cname,\n";
77         $need_eol = 0;
78         print "Adding assembly '$name' from $file...\n";
79         while (($n = sysread (F, $buf, 32))) {
80                 for ($i = 0; $i < $n; ++$i) {
81                         print O "\t.byte ", ord (substr ($buf, $i, 1)), "\n";
82                 }
83 #               print O ",\n" if ($need_eol);
84 #               $need_eol = 1;
85 #               print O "\t";
86 #               for ($i = 0; $i < $n; ++$i) {
87 #                       print O ", " if $i > 0;
88 #                       print O ord (substr ($buf, $i, 1));
89 #               }
90         }
91 #       print O "\n};\n\n";
92         close (F);
93 }
94
95 print H "\nstatic const MonoBundledAssembly* bundled_assemblies [] = {\n";
96 print H $bundle_entries;
97 print H "\tNULL\n";
98 print H "};\n\n";
99 print H "#endif /* __MONO_BUNDLE_H__ */\n";
100 close (O);
101 close (H);
102 rename ("$header.tmp", $header);
103 rename ("$output.tmp", $output);
104