Program.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Text;
  6. using ProtoBuf.Meta;
  7. namespace ProtoBuf.Precompile
  8. {
  9. class Program
  10. {
  11. static int Main(string[] args)
  12. {
  13. try
  14. {
  15. Console.WriteLine("protobuf-net pre-compiler");
  16. PreCompileContext ctx;
  17. if (!CommandLineAttribute.TryParse(args, out ctx))
  18. {
  19. return -1;
  20. }
  21. if (ctx.Help)
  22. {
  23. Console.WriteLine();
  24. Console.WriteLine();
  25. Console.WriteLine(ctx.GetUsage());
  26. return -1;
  27. }
  28. if (!ctx.SanityCheck()) return -1;
  29. bool allGood = ctx.Execute();
  30. return allGood ? 0 : -1;
  31. }
  32. catch (Exception ex)
  33. {
  34. while (ex != null)
  35. {
  36. Console.Error.WriteLine(ex.Message);
  37. Console.Error.WriteLine(ex.StackTrace);
  38. Console.Error.WriteLine();
  39. ex = ex.InnerException;
  40. }
  41. return -1;
  42. }
  43. }
  44. }
  45. /// <summary>
  46. /// Defines the rules for a precompilation operation
  47. /// </summary>
  48. public class PreCompileContext
  49. {
  50. /// <summary>
  51. /// The target framework to use
  52. /// </summary>
  53. [CommandLine("f"), CommandLine("framework")]
  54. public string Framework { get; set; }
  55. private readonly List<string> probePaths = new List<string>();
  56. /// <summary>
  57. /// Locations to check for referenced assemblies
  58. /// </summary>
  59. [CommandLine("p"), CommandLine("probe")]
  60. public List<string> ProbePaths { get { return probePaths; } }
  61. private readonly List<string> inputs = new List<string>();
  62. /// <summary>
  63. /// The paths for assemblies to process
  64. /// </summary>
  65. [CommandLine("")]
  66. public List<string> Inputs { get { return inputs; } }
  67. /// <summary>
  68. /// The type name of the serializer to generate
  69. /// </summary>
  70. [CommandLine("t"), CommandLine("type")]
  71. public string TypeName { get; set; }
  72. /// <summary>
  73. /// The name of the assembly to generate
  74. /// </summary>
  75. [CommandLine("o"), CommandLine("out")]
  76. public string AssemblyName { get; set; }
  77. /// <summary>
  78. /// Show help
  79. /// </summary>
  80. [CommandLine("?"), CommandLine("help"), CommandLine("h")]
  81. public bool Help { get; set; }
  82. /// <summary>
  83. /// The accessibility of the generated type
  84. /// </summary>
  85. [CommandLine("access")]
  86. public ProtoBuf.Meta.RuntimeTypeModel.Accessibility Accessibility { get; set; }
  87. /// <summary>
  88. /// The path to the file to use to sign the assembly
  89. /// </summary>
  90. [CommandLine("keyfile")]
  91. public string KeyFile { get; set; }
  92. /// <summary>
  93. /// The container to use to sign the assembly
  94. /// </summary>
  95. [CommandLine("keycontainer")]
  96. public string KeyContainer { get; set; }
  97. /// <summary>
  98. /// The public key (in hexadecimal) to use to sign the assembly
  99. /// </summary>
  100. [CommandLine("publickey")]
  101. public string PublicKey { get; set; }
  102. /// <summary>
  103. /// Create a new instance of PreCompileContext
  104. /// </summary>
  105. public PreCompileContext()
  106. {
  107. Accessibility = ProtoBuf.Meta.RuntimeTypeModel.Accessibility.Public;
  108. }
  109. static string TryInferFramework(string path)
  110. {
  111. string imageRuntimeVersion = null;
  112. try
  113. {
  114. using (var uni = new IKVM.Reflection.Universe())
  115. {
  116. uni.AssemblyResolve += (s, a) => ((IKVM.Reflection.Universe)s).CreateMissingAssembly(a.Name);
  117. var asm = uni.LoadFile(path);
  118. imageRuntimeVersion = asm.ImageRuntimeVersion;
  119. var attr = uni.GetType("System.Attribute, mscorlib");
  120. foreach(var attrib in asm.__GetCustomAttributes(attr, false))
  121. {
  122. if (attrib.Constructor.DeclaringType.FullName == "System.Runtime.Versioning.TargetFrameworkAttribute"
  123. && attrib.ConstructorArguments.Count == 1)
  124. {
  125. var parts = ((string)attrib.ConstructorArguments[0].Value).Split(',');
  126. string runtime = null, version = null, profile = null;
  127. for (int i = 0; i < parts.Length; i++)
  128. {
  129. int idx = parts[i].IndexOf('=');
  130. if (idx < 0)
  131. {
  132. runtime = parts[i];
  133. } else
  134. {
  135. switch(parts[i].Substring(0,idx))
  136. {
  137. case "Version":
  138. version = parts[i].Substring(idx + 1);
  139. break;
  140. case "Profile":
  141. profile = parts[i].Substring(idx + 1);
  142. break;
  143. }
  144. }
  145. }
  146. if(runtime != null)
  147. {
  148. var sb = new StringBuilder(runtime);
  149. if(version != null)
  150. {
  151. sb.Append(Path.DirectorySeparatorChar).Append(version);
  152. }
  153. if (profile != null)
  154. {
  155. sb.Append(Path.DirectorySeparatorChar).Append("Profile").Append(Path.DirectorySeparatorChar).Append(profile);
  156. }
  157. string targetFramework = sb.ToString();
  158. return targetFramework;
  159. }
  160. }
  161. }
  162. }
  163. }
  164. catch (Exception ex) {
  165. // not really fussed; we could have multiple inputs to try, and the user
  166. // can always use -f:blah to specify it explicitly
  167. Debug.WriteLine(ex.Message);
  168. }
  169. if (!string.IsNullOrEmpty(imageRuntimeVersion))
  170. {
  171. string frameworkPath = Path.Combine(
  172. Environment.ExpandEnvironmentVariables(@"%windir%\Microsoft.NET\Framework"),
  173. imageRuntimeVersion);
  174. if (Directory.Exists(frameworkPath)) return frameworkPath;
  175. }
  176. return null;
  177. }
  178. /// <summary>
  179. /// Check the context for obvious errrs
  180. /// </summary>
  181. public bool SanityCheck()
  182. {
  183. bool allGood = true;
  184. if (inputs.Count == 0)
  185. {
  186. Console.Error.WriteLine("No input assemblies");
  187. allGood = false;
  188. }
  189. if (string.IsNullOrEmpty(TypeName))
  190. {
  191. Console.Error.WriteLine("No serializer type-name specified");
  192. allGood = false;
  193. }
  194. if (string.IsNullOrEmpty(AssemblyName))
  195. {
  196. Console.Error.WriteLine("No output assembly file specified");
  197. allGood = false;
  198. }
  199. if (string.IsNullOrEmpty(Framework))
  200. {
  201. foreach (var inp in inputs)
  202. {
  203. string tmp = TryInferFramework(inp);
  204. if (tmp != null)
  205. {
  206. Console.WriteLine("Detected framework: " + tmp);
  207. Framework = tmp;
  208. break;
  209. }
  210. }
  211. }
  212. if (string.IsNullOrEmpty(Framework))
  213. {
  214. Console.WriteLine("No framework specified; defaulting to " + Environment.Version);
  215. probePaths.Add(Path.GetDirectoryName(typeof(string).Assembly.Location));
  216. }
  217. else
  218. {
  219. if (Directory.Exists(Framework))
  220. { // very clear and explicit
  221. probePaths.Add(Framework);
  222. }
  223. else
  224. {
  225. string root = Environment.GetEnvironmentVariable("ProgramFiles(x86)");
  226. if (string.IsNullOrEmpty(root)) root = Environment.GetEnvironmentVariable("ProgramFiles");
  227. root = Path.Combine(root, @"Reference Assemblies\Microsoft\Framework\");
  228. if (!Directory.Exists(root))
  229. {
  230. Console.Error.WriteLine("Framework reference assemblies root folder could not be found");
  231. allGood = false;
  232. }
  233. else
  234. {
  235. string frameworkRoot = Path.Combine(root, Framework);
  236. if (Directory.Exists(frameworkRoot))
  237. {
  238. // fine
  239. probePaths.Add(frameworkRoot);
  240. }
  241. else
  242. {
  243. Console.Error.WriteLine("Framework not found: " + Framework);
  244. Console.Error.WriteLine("Available frameworks are:");
  245. string[] files = Directory.GetFiles(root, "mscorlib.dll", SearchOption.AllDirectories);
  246. foreach (var file in files)
  247. {
  248. string dir = Path.GetDirectoryName(file);
  249. if (dir.StartsWith(root)) dir = dir.Substring(root.Length);
  250. Console.Error.WriteLine(dir);
  251. }
  252. allGood = false;
  253. }
  254. }
  255. }
  256. }
  257. if (!string.IsNullOrEmpty(KeyFile) && !File.Exists(KeyFile))
  258. {
  259. Console.Error.WriteLine("Key file not found: " + KeyFile);
  260. allGood = false;
  261. }
  262. foreach (var inp in inputs)
  263. {
  264. if(File.Exists(inp)) {
  265. string dir = Path.GetDirectoryName(inp);
  266. if(!probePaths.Contains(dir)) probePaths.Add(dir);
  267. }
  268. else
  269. {
  270. Console.Error.WriteLine("Input not found: " + inp);
  271. allGood = false;
  272. }
  273. }
  274. return allGood;
  275. }
  276. IEnumerable<string> ProbeForFiles(string file)
  277. {
  278. foreach (var probePath in probePaths)
  279. {
  280. string combined = Path.Combine(probePath, file);
  281. if (File.Exists(combined))
  282. {
  283. yield return combined;
  284. }
  285. }
  286. }
  287. /// <summary>
  288. /// Perform the precompilation operation
  289. /// </summary>
  290. public bool Execute()
  291. {
  292. // model to work with
  293. var model = TypeModel.Create();
  294. model.Universe.AssemblyResolve += (sender, args) =>
  295. {
  296. string nameOnly = args.Name.Split(',')[0];
  297. if (nameOnly == "IKVM.Reflection" && args.RequestingAssembly != null && args.RequestingAssembly.FullName.StartsWith("protobuf-net"))
  298. {
  299. throw new InvalidOperationException("This operation needs access to the protobuf-net.dll used by your library, **in addition to** the protobuf-net.dll that is included with the precompiler; the easiest way to do this is to ensure the referenced protobuf-net.dll is in the same folder as your library.");
  300. }
  301. var uni = model.Universe;
  302. foreach (var tmp in uni.GetAssemblies())
  303. {
  304. if (tmp.GetName().Name == nameOnly) return tmp;
  305. }
  306. var asm = ResolveNewAssembly(uni, nameOnly + ".dll");
  307. if(asm != null) return asm;
  308. asm = ResolveNewAssembly(uni, nameOnly + ".exe");
  309. if(asm != null) return asm;
  310. throw new InvalidOperationException("All assemblies must be resolved explicity; did not resolve: " + args.Name);
  311. };
  312. bool allGood = true;
  313. var mscorlib = ResolveNewAssembly(model.Universe, "mscorlib.dll");
  314. if (mscorlib == null)
  315. {
  316. Console.Error.WriteLine("mscorlib.dll not found!");
  317. allGood = false;
  318. }
  319. ResolveNewAssembly(model.Universe, "System.dll"); // not so worried about whether that one exists...
  320. if (ResolveNewAssembly(model.Universe, "protobuf-net.dll") == null)
  321. {
  322. Console.Error.WriteLine("protobuf-net.dll not found!");
  323. allGood = false;
  324. }
  325. if (!allGood) return false;
  326. var assemblies = new List<IKVM.Reflection.Assembly>();
  327. MetaType metaType = null;
  328. foreach (var file in inputs)
  329. {
  330. assemblies.Add(model.Load(file));
  331. }
  332. // scan for obvious protobuf types
  333. var attributeType = model.Universe.GetType("System.Attribute, mscorlib");
  334. var toAdd = new List<IKVM.Reflection.Type>();
  335. foreach (var asm in assemblies)
  336. {
  337. foreach (var type in asm.GetTypes())
  338. {
  339. bool add = false;
  340. if (!(type.IsClass || type.IsValueType)) continue;
  341. foreach (var attrib in type.__GetCustomAttributes(attributeType, true))
  342. {
  343. string name = attrib.Constructor.DeclaringType.FullName;
  344. switch(name)
  345. {
  346. case "ProtoBuf.ProtoContractAttribute":
  347. add = true;
  348. break;
  349. }
  350. if (add) break;
  351. }
  352. if (add) toAdd.Add(type);
  353. }
  354. }
  355. if (toAdd.Count == 0)
  356. {
  357. Console.Error.WriteLine("No [ProtoContract] types found; nothing to do!");
  358. return false;
  359. }
  360. // add everything we explicitly know about
  361. toAdd.Sort((x, y) => string.Compare(x.FullName, y.FullName));
  362. foreach (var type in toAdd)
  363. {
  364. Console.WriteLine("Adding " + type.FullName + "...");
  365. var tmp = model.Add(type, true);
  366. if (metaType == null) metaType = tmp; // use this as the template for the framework version
  367. }
  368. // add everything else we can find
  369. model.Cascade();
  370. var inferred = new List<IKVM.Reflection.Type>();
  371. foreach (MetaType type in model.GetTypes())
  372. {
  373. if(!toAdd.Contains(type.Type)) inferred.Add(type.Type);
  374. }
  375. inferred.Sort((x, y) => string.Compare(x.FullName, y.FullName));
  376. foreach (var type in inferred)
  377. {
  378. Console.WriteLine("Adding " + type.FullName + "...");
  379. }
  380. // configure the output file/serializer name, and borrow the framework particulars from
  381. // the type we loaded
  382. var options = new RuntimeTypeModel.CompilerOptions
  383. {
  384. TypeName = TypeName,
  385. OutputPath = AssemblyName,
  386. ImageRuntimeVersion = mscorlib.ImageRuntimeVersion,
  387. MetaDataVersion = 0x20000, // use .NET 2 onwards
  388. KeyContainer = KeyContainer,
  389. KeyFile = KeyFile,
  390. PublicKey = PublicKey
  391. };
  392. if (mscorlib.ImageRuntimeVersion == "v1.1.4322")
  393. { // .NET 1.1-style
  394. options.MetaDataVersion = 0x10000;
  395. }
  396. if (metaType != null)
  397. {
  398. options.SetFrameworkOptions(metaType);
  399. }
  400. options.Accessibility = this.Accessibility;
  401. Console.WriteLine("Compiling " + options.TypeName + " to " + options.OutputPath + "...");
  402. // GO WORK YOUR MAGIC, CRAZY THING!!
  403. model.Compile(options);
  404. Console.WriteLine("All done");
  405. return true;
  406. }
  407. private IKVM.Reflection.Assembly ResolveNewAssembly(IKVM.Reflection.Universe uni, string fileName)
  408. {
  409. foreach (var match in ProbeForFiles(fileName))
  410. {
  411. var asm = uni.LoadFile(match);
  412. if (asm != null)
  413. {
  414. Console.WriteLine("Resolved " + match);
  415. return asm;
  416. }
  417. }
  418. return null;
  419. }
  420. /// <summary>
  421. /// Return the syntax guide for the utility
  422. /// </summary>
  423. public string GetUsage()
  424. {
  425. return
  426. @"Generates a serialization dll that can be used with just the
  427. (platform-specific) protobuf-net core, allowing fast and efficient
  428. serialization even on light frameworks (CF, SL, SP7, Metro, etc).
  429. The input assembly(ies) is(are) anaylsed for types decorated with
  430. [ProtoContract]. All such types are added to the model, as are any
  431. types that they require.
  432. Note: the compiler must be able to resolve a protobuf-net.dll
  433. that is suitable for the target framework; this is done most simply
  434. by ensuring that the appropriate protobuf-net.dll is next to the
  435. input assembly.
  436. Options:
  437. -f[ramework]:<framework>
  438. Can be an explicit path, or a path relative to:
  439. Reference Assemblies\Microsoft\Framework
  440. -o[ut]:<file>
  441. Output dll path
  442. -t[ype]:<typename>
  443. Type name of the serializer to generate
  444. -p[robe]:<path>
  445. Additional directory to probe for assemblies
  446. -access:<access>
  447. Specify accessibility of generated serializer
  448. to 'Public' or 'Internal'
  449. -keyfile:<file>
  450. Sign with the file (snk, etc) specified
  451. -keycontainer:<container>
  452. Sign with the container specified
  453. -publickey:<key>
  454. Sign with the public key specified (as hex)
  455. <file>
  456. Input file to analyse
  457. Example:
  458. precompile -f:.NETCore\v4.5 MyDtos\My.dll -o:MySerializer.dll
  459. -t:MySerializer";
  460. }
  461. }
  462. /// <summary>
  463. /// Defines a mapping from command-line attributes to properties
  464. /// </summary>
  465. [AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
  466. public class CommandLineAttribute : Attribute
  467. {
  468. /// <summary>
  469. /// Attempt to parse the incoming command-line switches, matching by prefix
  470. /// onto properties of the specified type
  471. /// </summary>
  472. public static bool TryParse<T>(string[] args, out T result) where T : class, new()
  473. {
  474. result = new T();
  475. bool allGood = true;
  476. var props = typeof(T).GetProperties();
  477. char[] leadChars = {'/', '+', '-'};
  478. for (int i = 0; i < args.Length; i++)
  479. {
  480. string arg = args[i].Trim(), prefix, value;
  481. if(arg.IndexOfAny(leadChars) == 0)
  482. {
  483. int idx = arg.IndexOf(':');
  484. if (idx < 0)
  485. {
  486. prefix = arg.Substring(1);
  487. value = "";
  488. }
  489. else
  490. {
  491. prefix = arg.Substring(1,idx - 1);
  492. value = arg.Substring(idx + 1);
  493. }
  494. }
  495. else
  496. {
  497. prefix = "";
  498. value = arg;
  499. }
  500. System.Reflection.PropertyInfo foundProp = null;
  501. foreach (var prop in props)
  502. {
  503. foreach (CommandLineAttribute atttib in prop.GetCustomAttributes(typeof(CommandLineAttribute), true))
  504. {
  505. if (atttib.Prefix == prefix)
  506. {
  507. foundProp = prop;
  508. break;
  509. }
  510. }
  511. if (foundProp != null) break;
  512. }
  513. if (foundProp == null)
  514. {
  515. allGood = false;
  516. Console.Error.WriteLine("Argument not understood: " + arg);
  517. }
  518. else
  519. {
  520. if (foundProp.PropertyType == typeof(string))
  521. {
  522. foundProp.SetValue(result, value, null);
  523. }
  524. else if (foundProp.PropertyType == typeof(List<string>))
  525. {
  526. ((List<string>)foundProp.GetValue(result, null)).Add(value);
  527. }
  528. else if (foundProp.PropertyType == typeof(bool))
  529. {
  530. foundProp.SetValue(result, true, null);
  531. }
  532. else if (foundProp.PropertyType.IsEnum)
  533. {
  534. object parsedValue;
  535. try {
  536. parsedValue = Enum.Parse(foundProp.PropertyType, value, true);
  537. } catch {
  538. Console.Error.WriteLine("Invalid option for: " + arg);
  539. Console.Error.WriteLine("Options: " + string.Join(", ", Enum.GetNames(foundProp.PropertyType)));
  540. allGood = false;
  541. parsedValue = null;
  542. }
  543. if (parsedValue != null) foundProp.SetValue(result, parsedValue, null);
  544. }
  545. }
  546. }
  547. return allGood;
  548. }
  549. private readonly string prefix;
  550. /// <summary>
  551. /// Create a new CommandLineAttribute object for the given prefix
  552. /// </summary>
  553. public CommandLineAttribute(string prefix) { this.prefix = prefix; }
  554. /// <summary>
  555. /// The prefix to recognise this command-line switch
  556. /// </summary>
  557. public string Prefix { get { return prefix; } }
  558. }
  559. }