Tuesday, October 4
Shadow

earlier hwhmerge.csx

using System.IO.Compression;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Globalization;

if (Args.Count != 2)
{
    Console.WriteLine("usage: xsamerge.csx target.xsa source.xsa");
    return 1;
}

var xsa_in_path = Args[1];
var xsa_out_path = Args[0];

var temp_xsa_out_path = Path.GetTempFileName();

using (var xsa_archive = ZipFile.Open(xsa_in_path, ZipArchiveMode.Read))
{
    // files to not copy into the new .xsa
    var xsa_exclude_files = new HashSet<string>{
        "hwdef.xml",
        "xsa.xml",
        "xsa.json",
    };

    var xsa_json_lines = new List<String>();
    //var xsa_json = JsonDocument.Parse("good luck");

    using (var sr = new StreamReader(xsa_archive.GetEntry("xsa.json").Open()))
    {
        string line;
        while ((line = sr.ReadLine()) != null)
            if (!line.Contains("Timestamp")) // haha StreamReader go brrrrr
                xsa_json_lines.Add(line);
    }

    var hwdef_document = XElement.Load(xsa_archive.GetEntry("hwdef.xml").Open());

    // begone block diagram tcl scripts, this isn't Vivado
    var bd_tcl_files = hwdef_document.Descendants("File").Where(f => (string)f.Attribute("Type") == "BD_TCL");
    foreach (var bd_tcl_entry in bd_tcl_files)
    {
        xsa_exclude_files.Add((string)bd_tcl_entry.Attribute("Name"));
    }
    bd_tcl_files.Remove();

    // we meld the first PS hwh we find with later hwh
    XElement target_hwh = null;

    var outgoing_hwh_files = new SortedDictionary<string, XElement>(); // sorted by filename to try to keep zip file deterministic
    var hwh_files_to_merge = new List<XElement>();

    // find all the block diagram hwh files
    var root_hwh_file_query = hwdef_document.Descendants("File").Where(f => (string)f.Attribute("BD_TYPE") == "DEFAULT_BD").ToArray();
    foreach (var hwh_file_entry in root_hwh_file_query)
    {
        var hwh_filename = hwh_file_entry.Attribute("Name").Value;
        var incoming_hwh = XElement.Load(xsa_archive.GetEntry(hwh_filename).Open());

        xsa_exclude_files.Add(hwh_filename);

        var incoming_modules = incoming_hwh.Elements("MODULES");
        var ps_module = incoming_modules.Descendants("MODULE").Where(m => (string)m.Attribute("IS_PL") == "FALSE");

        if (ps_module.Any() && target_hwh == null)

Leave a Reply