public static void ValidateVendorSampleDependencies(ConstructedVendorSampleDirectory dir, string toolchainDir) { using (var sw = File.CreateText(@"e:\temp\0.txt")) { foreach (var vs in dir.Samples) { if (vs.AllDependencies == null) { continue; } var extraDeps = vs.AllDependencies.Except(vs.HeaderFiles ?? new string[0]).Except(vs.SourceFiles ?? new string[0]).Where(d => !d.StartsWith(toolchainDir, StringComparison.InvariantCultureIgnoreCase)).ToArray(); var knownDirs = vs.IncludeDirectories.Concat(vs.SourceFiles.Select(f => Path.GetDirectoryName(f))); foreach (var dep in extraDeps) { if (knownDirs.FirstOrDefault(d => dep.Replace('\\', '/').StartsWith(d.Replace('\\', '/'), StringComparison.InvariantCultureIgnoreCase)) == null) { bool found = false; foreach (var includeDir in vs.IncludeDirectories.Concat(vs.SourceFiles.Select(f => Path.GetDirectoryName(f)))) { string baseDir = Path.GetDirectoryName(includeDir); if (dep.Replace('\\', '/').StartsWith(baseDir.Replace('\\', '/'), StringComparison.InvariantCultureIgnoreCase)) { found = true; break; } } if (!found) { sw.WriteLine("Unexpected dependency: " + dep); } } } } } }
public PathMapper(ConstructedVendorSampleDirectory dir) { _SampleDir = dir; }
public virtual Dictionary <string, string> InsertVendorSamplesIntoBSP(ConstructedVendorSampleDirectory dir, VendorSample[] sampleList, string bspDirectory) { List <VendorSample> finalSamples = new List <VendorSample>(); string outputDir = Path.Combine(bspDirectory, "VendorSamples"); if (Directory.Exists(outputDir)) { Console.WriteLine($"Deleting {outputDir}..."); Directory.Delete(outputDir, true); } var mapper = CreatePathMapper(dir) ?? new PathMapper(dir); Dictionary <string, string> copiedFiles = new Dictionary <string, string>(); Console.WriteLine("Processing sample list..."); List <string> tooLongPaths = new List <string>(); foreach (var s in sampleList) { if (s.AllDependencies == null) { continue; } var deps = s.AllDependencies .Concat(new[] { s.LinkerScript }) .Concat(s.SourceFiles) .Distinct() .Select(d => new ParsedDependency { OriginalFile = d, MappedFile = mapper.MapPath(d) }) .Where(d => d.MappedFile != null) .ToArray(); mapper.MapPathList(ref s.HeaderFiles); mapper.MapPathList(ref s.IncludeDirectories); mapper.MapPathList(ref s.SourceFiles); s.LinkerScript = mapper.MapPath(s.LinkerScript); s.Configuration = DetectKnownFrameworksAndFilterPaths(ref s.SourceFiles, ref s.HeaderFiles, ref s.IncludeDirectories, ref s.PreprocessorMacros, ref deps, s.Configuration); FilterPreprocessorMacros(ref s.PreprocessorMacros); const int ReasonableVendorSampleDirPathLengthForUsers = 120; foreach (var dep in deps) { if (dep.MappedFile.StartsWith("$$SYS:BSP_ROOT$$/")) { continue; //The file was already copied } copiedFiles[dep.OriginalFile] = dep.MappedFile.Replace(SampleRootDirMarker, outputDir); int estimatedTargetPathLength = ReasonableVendorSampleDirPathLengthForUsers + dep.MappedFile.Length - SampleRootDirMarker.Length; if (estimatedTargetPathLength > 254) { tooLongPaths.Add(dep.MappedFile); } } s.AllDependencies = deps.Select(d => d.MappedFile).ToArray(); var rawPath = s.Path; s.Path = mapper.MapPath(rawPath); if (s.Path == null) { throw new Exception("Invalid sample path for " + s.UserFriendlyName); } s.VirtualPath = BuildVirtualSamplePath(s.Path); if (s.LinkerScript != null) { string prefix = s.Path.TrimEnd('/', '\\') + "/"; if (s.LinkerScript.StartsWith(prefix)) { s.LinkerScript = s.LinkerScript.Substring(prefix.Length).TrimStart('/'); } else if (s.LinkerScript.StartsWith("$$SYS:BSP_ROOT$$") || s.LinkerScript.StartsWith("$$SYS:VSAMPLE_DIR$$")) { //Nothing to do. VisualGDB will automatically expand this. } else { throw new Exception($"Unexpected linker script path {s.LinkerScript}. VisualGDB may not be able to expand it."); } } finalSamples.Add(s); } if (tooLongPaths.Count > 0) { throw new Exception($"Found {tooLongPaths.Count} files with excessively long paths. Please update MapPath() in the BSP-specific path mapper to shorten the target paths."); } Console.WriteLine($"Copying {copiedFiles.Count} files..."); foreach (var kv in copiedFiles) { Directory.CreateDirectory(Path.GetDirectoryName(kv.Value)); if (!File.Exists(kv.Value)) { File.Copy(kv.Key, kv.Value); } File.SetAttributes(kv.Value, File.GetAttributes(kv.Value) & ~(FileAttributes.ReadOnly | FileAttributes.Hidden | FileAttributes.System)); } Console.WriteLine("Updating BSP..."); VendorSampleDirectory finalDir = new VendorSampleDirectory { Samples = finalSamples.ToArray() }; Directory.CreateDirectory(outputDir); using (var fs = File.Create(Path.Combine(outputDir, "VendorSamples.xml.gz"))) using (var gs = new GZipStream(fs, CompressionMode.Compress)) { XmlTools.SaveObjectToStream(finalDir, gs); } return(copiedFiles); }
protected virtual PathMapper CreatePathMapper(ConstructedVendorSampleDirectory dir) => null;
public void InsertVendorSamplesIntoBSP(ConstructedVendorSampleDirectory dir, string bspDirectory, PathMapper mapper = null) { List <VendorSample> finalSamples = new List <VendorSample>(); string outputDir = Path.Combine(bspDirectory, "VendorSamples"); if (Directory.Exists(outputDir)) { Console.WriteLine($"Deleting {outputDir}..."); Directory.Delete(outputDir, true); } if (mapper == null) { mapper = new PathMapper(dir); } Dictionary <string, string> copiedFiles = new Dictionary <string, string>(); Console.WriteLine("Processing sample list..."); foreach (var s in dir.Samples) { if (s.AllDependencies == null) { continue; } var deps = s.AllDependencies.Concat(s.SourceFiles).Distinct().Select(d => new ParsedDependency { OriginalFile = d, MappedFile = mapper.MapPath(d) }).Where(d => d.MappedFile != null).ToArray(); mapper.MapPathList(ref s.HeaderFiles); mapper.MapPathList(ref s.IncludeDirectories); mapper.MapPathList(ref s.SourceFiles); s.Configuration = DetectKnownFrameworksAndFilterPaths(ref s.SourceFiles, ref s.HeaderFiles, ref s.IncludeDirectories, ref deps, s.Configuration.MCUConfiguration); FilterPreprocessorMacros(ref s.PreprocessorMacros); foreach (var dep in deps) { copiedFiles[dep.OriginalFile] = dep.MappedFile.Replace(SampleRootDirMarker, outputDir); } s.AllDependencies = deps.Select(d => d.MappedFile).ToArray(); s.Path = mapper.MapPath(s.Path); s.VirtualPath = BuildVirtualSamplePath(s.Path); finalSamples.Add(s); } Console.WriteLine($"Copying {copiedFiles.Count} files..."); foreach (var kv in copiedFiles) { Directory.CreateDirectory(Path.GetDirectoryName(kv.Value)); if (!File.Exists(kv.Value)) { File.Copy(kv.Key, kv.Value); } File.SetAttributes(kv.Value, File.GetAttributes(kv.Value) & ~(FileAttributes.ReadOnly | FileAttributes.Hidden | FileAttributes.System)); } Console.WriteLine("Updating BSP..."); VendorSampleDirectory finalDir = new VendorSampleDirectory { Samples = finalSamples.ToArray() }; Directory.CreateDirectory(outputDir); using (var fs = File.Create(Path.Combine(outputDir, "VendorSamples.xml.gz"))) using (var gs = new GZipStream(fs, CompressionMode.Compress)) { XmlTools.SaveObjectToStream(finalDir, gs); } }
private static ConstructedVendorSampleDirectory BuildOrLoadSampleDirectory(string SDKdir, string outputDir, string sampleListFile) { ConstructedVendorSampleDirectory sampleDir; if (!File.Exists(sampleListFile) && !File.Exists(sampleListFile + ".gz")) { if (Directory.Exists(outputDir)) Directory.Delete(outputDir, true); Directory.CreateDirectory(outputDir); var samples = ParseVendorSamples(SDKdir); sampleDir = new ConstructedVendorSampleDirectory { SourceDirectory = SDKdir, Samples = samples.ToArray(), }; XmlTools.SaveObject(sampleDir, sampleListFile); } else { sampleDir = XmlTools.LoadObject<ConstructedVendorSampleDirectory>(sampleListFile); } return sampleDir; }