|
| 1 | +// ----------------------------------------------------------------------- |
| 2 | +// <copyright file="Program.cs" company="Ubiquity.NET Contributors"> |
| 3 | +// Copyright (c) Ubiquity.NET Contributors. All rights reserved. |
| 4 | +// </copyright> |
| 5 | +// ----------------------------------------------------------------------- |
| 6 | + |
| 7 | +using System; |
| 8 | +using System.Collections.Generic; |
| 9 | +using System.IO; |
| 10 | +using System.Linq; |
| 11 | +using System.Text.RegularExpressions; |
| 12 | + |
| 13 | +using CommandLine; |
| 14 | + |
| 15 | +using DiffMatchPatch; |
| 16 | + |
| 17 | +[assembly: CLSCompliant(true)] |
| 18 | + |
| 19 | +namespace PatchVsForLibClang |
| 20 | +{ |
| 21 | + internal class Program |
| 22 | + { |
| 23 | + // example paths |
| 24 | + // With Fix From MS => @"D:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.28.29617\include\intrin0.h"; |
| 25 | + // MSVC 16.8.3 => @"D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333\include\intrin0.h"; |
| 26 | + |
| 27 | + // VS 2019 16.9 Preview 2 is the first publicly available version with the fix included |
| 28 | + private static readonly Version MinVersionWithFix = new Version( 14, 28, 2933 ); |
| 29 | + private const string MsVCRelativePath = @"VC\Tools\MSVC"; |
| 30 | + private const string VersionRelativeRoot = "Include"; |
| 31 | + |
| 32 | + public static int Main( string[ ] args ) |
| 33 | + { |
| 34 | + using var x = new Parser( ); |
| 35 | + return x.ParseArguments( args, typeof(PatchOptions), typeof(GenerateOptions) ) |
| 36 | + .MapResult( (PatchOptions o) => ApplyPatch( o ), |
| 37 | + (GenerateOptions o) => GenerateDiffFile(o), |
| 38 | + _ => -1 ); |
| 39 | + } |
| 40 | + |
| 41 | + private static int ApplyPatch(PatchOptions options) |
| 42 | + { |
| 43 | + if( !Directory.Exists( options.VsInstallPath ) ) |
| 44 | + { |
| 45 | + Console.Error.WriteLine( "Specified VS install path does not exist. '{0}'", options.VsInstallPath ); |
| 46 | + return -1; |
| 47 | + } |
| 48 | + |
| 49 | + string msvcPath = Path.Combine( options.VsInstallPath, MsVCRelativePath ); |
| 50 | + if( !Directory.Exists( msvcPath ) ) |
| 51 | + { |
| 52 | + Console.Error.WriteLine( "Specified VS install path does not contain the MSVC libraries path. '{0}'", msvcPath ); |
| 53 | + return -1; |
| 54 | + } |
| 55 | + |
| 56 | + var versions = GetInstalledMsVcVersions( msvcPath ).ToList( ); |
| 57 | + if( versions.Count == 0 ) |
| 58 | + { |
| 59 | + Console.Error.WriteLine( "Could not find any installed versions at '{0}'", msvcPath ); |
| 60 | + return -1; |
| 61 | + } |
| 62 | + |
| 63 | + var maxInstalledVersion = versions.Max( ); |
| 64 | + if( maxInstalledVersion <= MinVersionWithFix ) |
| 65 | + { |
| 66 | + Console.WriteLine( "Installation already contains the fix from MS no files modified" ); |
| 67 | + return 0; |
| 68 | + } |
| 69 | + |
| 70 | + string versionRelativeIncludePath = Path.Combine( msvcPath, maxInstalledVersion.ToString( ), VersionRelativeRoot ); |
| 71 | + if( !Directory.Exists( versionRelativeIncludePath ) ) |
| 72 | + { |
| 73 | + Console.Error.WriteLine( "Could not find include path '{0}'", versionRelativeIncludePath ); |
| 74 | + return -1; |
| 75 | + } |
| 76 | + |
| 77 | + string intrin0_h_Path = Path.Combine( versionRelativeIncludePath, "intrin0.h" ); |
| 78 | + if( !File.Exists( intrin0_h_Path ) ) |
| 79 | + { |
| 80 | + Console.Error.WriteLine( "intrin0.h not found. ({0})", intrin0_h_Path ); |
| 81 | + return -1; |
| 82 | + } |
| 83 | + |
| 84 | + // compute patched content before trying backup as it might not apply to this install |
| 85 | + string patchedContent = PatchFile( intrin0_h_Path, Resources.Intrin0_h_diff ); |
| 86 | + if( string.IsNullOrWhiteSpace( patchedContent ) ) |
| 87 | + { |
| 88 | + Console.WriteLine( "File contents did not match - patches not applied." ); |
| 89 | + return 0; |
| 90 | + } |
| 91 | + |
| 92 | + string backupPath = Path.Combine( versionRelativeIncludePath, "intrin0.h.bak" ); |
| 93 | + if( File.Exists( backupPath ) && !options.Force ) |
| 94 | + { |
| 95 | + Console.WriteLine( "Backup already exists, no backup will be made" ); |
| 96 | + } |
| 97 | + else |
| 98 | + { |
| 99 | + try |
| 100 | + { |
| 101 | + File.Copy( intrin0_h_Path, backupPath, false ); |
| 102 | + } |
| 103 | + catch( IOException ex ) |
| 104 | + { |
| 105 | + Console.Error.WriteLine( "ERROR creating backup: {0}", ex.Message ); |
| 106 | + return -1; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + File.WriteAllText( intrin0_h_Path, patchedContent ); |
| 111 | + return 0; |
| 112 | + } |
| 113 | + |
| 114 | + private static string PatchFile( string source, string diffText ) |
| 115 | + { |
| 116 | + string fileText = File.ReadAllText( source ); |
| 117 | + var patches = PatchList.Parse( diffText ); |
| 118 | + var (patchedText, results) = patches.Apply( fileText ); |
| 119 | + |
| 120 | + // all patches must apply or the source wasn't a proper match |
| 121 | + return results.Aggregate( true, ( l, r ) => l & r ) ? patchedText : string.Empty; |
| 122 | + } |
| 123 | + |
| 124 | + private static IEnumerable<Version> GetInstalledMsVcVersions( string msvcPath ) |
| 125 | + { |
| 126 | + var verPattern = new Regex( @"\d+\.\d+\.\d+" ); |
| 127 | + return from dir in Directory.EnumerateDirectories( msvcPath ) |
| 128 | + where verPattern.IsMatch( dir ) |
| 129 | + select Version.Parse( Path.GetFileName( dir ) ); |
| 130 | + } |
| 131 | + |
| 132 | + // While VS team did publish the GIT patch DIFF, that's not usable by the DiffMatchPatch library |
| 133 | + // (or any other I could find on short notice). So this is used to generate the DIFF applied. |
| 134 | + // |
| 135 | + // The resulting intrin0_h.diff is already part of this project so not likely needed, but |
| 136 | + // this is retained as a reference for how it was created) |
| 137 | + private static int GenerateDiffFile( GenerateOptions o ) |
| 138 | + { |
| 139 | + string unpatched = File.ReadAllText( o.Unpatched ); |
| 140 | + string patched = File.ReadAllText( o.Patched ); |
| 141 | + |
| 142 | + // compute and write the diff file for inclusion |
| 143 | + File.WriteAllText( "intrin0_h.diff", Patch.Compute( unpatched, patched ).ToText( ) ); |
| 144 | + return 0; |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments