diff options
author | Rodrigo Kumpera <kumpera@gmail.com> | 2013-02-01 02:58:06 +0400 |
---|---|---|
committer | Rodrigo Kumpera <kumpera@gmail.com> | 2013-02-01 02:58:06 +0400 |
commit | 17b3cd53bf21bbb6d200df8534508623145998a9 (patch) | |
tree | b4450f35b4a324cb0e3d0e8c4581cd8abd386778 /mcs | |
parent | 1c47612b58f7830131d8acc85089e71a73291e5a (diff) |
Add new mdbrebase tool that lets you change the prefix of its source files.
Diffstat (limited to 'mcs')
-rw-r--r-- | mcs/tools/Makefile | 3 | ||||
-rw-r--r-- | mcs/tools/mdbrebase/Makefile | 12 | ||||
-rw-r--r-- | mcs/tools/mdbrebase/mdbrebase.cs | 132 | ||||
-rw-r--r-- | mcs/tools/mdbrebase/mdbrebase.exe.sources | 2 |
4 files changed, 148 insertions, 1 deletions
diff --git a/mcs/tools/Makefile b/mcs/tools/Makefile index 672cc492826..fc01a9618a7 100644 --- a/mcs/tools/Makefile +++ b/mcs/tools/Makefile @@ -45,7 +45,8 @@ net_4_0_dirs := \ mono-configuration-crypto \ ccrewrite \ cccheck \ - security + security \ + mdbrebase net_2_0_dirs := \ $(per_profile_dirs) diff --git a/mcs/tools/mdbrebase/Makefile b/mcs/tools/mdbrebase/Makefile new file mode 100644 index 00000000000..866e81a024e --- /dev/null +++ b/mcs/tools/mdbrebase/Makefile @@ -0,0 +1,12 @@ +thisdir = tools/mdb-rebase +SUBDIRS = +include ../../build/rules.make + +PROGRAM = mdbrebase.exe + +LOCAL_MCS_FLAGS = \ + /r:Mono.Cecil.dll \ + /r:Mono.Cecil.Mdb.dll + + +include ../../build/executable.make diff --git a/mcs/tools/mdbrebase/mdbrebase.cs b/mcs/tools/mdbrebase/mdbrebase.cs new file mode 100644 index 00000000000..caed4813d8c --- /dev/null +++ b/mcs/tools/mdbrebase/mdbrebase.cs @@ -0,0 +1,132 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.IO; + +using Mono.Cecil; +using Mono.Cecil.Mdb; +using Mono.Options; + +namespace Mono.MdbRebase +{ + +class Settings +{ + public string OutputDirectory { get; set; } + public string InputPattern { get; set; } + public string OutputPattern { get; set; } + + public bool Validate () + { + return InputPattern != null && OutputPattern != null && !OutputPattern.StartsWith (InputPattern); + } + + public string Replace (string input) + { + if (input.StartsWith (InputPattern)) + return input.Replace (InputPattern, OutputPattern); + return input; + } +} + +class MdbRebase +{ + Settings settings; + + public MdbRebase (Settings settings) + { + this.settings = settings; + } + + public void RewriteMdbFile (string inputAssembly) + { + var assemblyName = new FileInfo (inputAssembly).Name; + + var readParams = new ReaderParameters () { + SymbolReaderProvider = new MdbReaderProvider (), + ReadSymbols = true, + }; + + var assembly = AssemblyDefinition.ReadAssembly (inputAssembly, readParams); + + foreach (var m in assembly.Modules) { + foreach (var t in m.Types) { + ProcessType (t); + foreach (var inner in t.NestedTypes) + ProcessType (inner); + } + } + + var writeParms = new WriterParameters () { + SymbolWriterProvider = new MdbWriterProvider (), + WriteSymbols = true, + }; + + var tmpdir = Path.GetTempPath (); + var tmpAsm = tmpdir + assemblyName; + string finalMdb = inputAssembly + ".mdb"; + if (settings.OutputDirectory != null) + finalMdb = Path.Combine (settings.OutputDirectory, assemblyName) + ".mdb"; + + assembly.Write (tmpAsm, writeParms); + Console.WriteLine (tmpAsm); + File.Delete (tmpAsm); + File.Delete (finalMdb); + Console.WriteLine ("Moving {0} to {1}", tmpAsm + ".mdb", finalMdb); + new FileInfo (tmpAsm + ".mdb").MoveTo (finalMdb); + } + + void ProcessMethod (MethodDefinition m) + { + foreach (var i in m.Body.Instructions) { + if (i.SequencePoint != null) + i.SequencePoint.Document.Url = settings.Replace (i.SequencePoint.Document.Url); + } + } + + void ProcessType (TypeDefinition t) + { + foreach (var m in t.Methods) + ProcessMethod (m); + } + +} + +class Driver { + static void Usage (OptionSet options) + { + Console.WriteLine (@"Usage: mdbrebase [options] <ASSEMBLY_TO_FIX>"); + if (options != null) { + Console.WriteLine (); + Console.WriteLine ("Available options:"); + options.WriteOptionDescriptions (Console.Out); + } + Console.WriteLine (); + + Environment.Exit (-1); + } + + static int Main (string[] args) { + var s = new Settings (); + bool showHelp = false; + + var p = new OptionSet () { + { "d=|output=", "Output directory to the mdb file, replace existing one if ommited", v => s.OutputDirectory = v }, + { "i=|input-pattern=", "Input pattern to replace (must not be a prefix to output-pattern)(required)", v => s.InputPattern = v }, + { "o=|output-pattern=", "Output pattern to replace (required)", v => s.OutputPattern = v }, + { "h|?|help", v => showHelp = true }, + }; + + List <string> extra = p.Parse (args); + + if (showHelp || extra.Count < 1 || !s.Validate ()) + Usage (p); + + var m = new MdbRebase (s); + foreach (var a in extra) + m.RewriteMdbFile (a); + return 0; + + } +} +}
\ No newline at end of file diff --git a/mcs/tools/mdbrebase/mdbrebase.exe.sources b/mcs/tools/mdbrebase/mdbrebase.exe.sources new file mode 100644 index 00000000000..424003ed5ab --- /dev/null +++ b/mcs/tools/mdbrebase/mdbrebase.exe.sources @@ -0,0 +1,2 @@ +../../class/Mono.Options/Mono.Options/Options.cs +./mdbrebase.cs
\ No newline at end of file |