Beispiel #1
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="masterGroup"></param>
 public CProjectGroup(CProjectGroup masterGroup)
 {
     this.defaultParentGroup = masterGroup;
     this.defaultExclude     = new List <string>();
     this.defaultFile        = new List <CProjectFile>();
     this.defaultChildGroup  = new List <CProjectGroup>();
 }
Beispiel #2
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="masterGroup"></param>
 public CProjectGroup()
 {
     this.defaultParentGroup = null;
     this.defaultExclude     = new List <string>();
     this.defaultFile        = new List <CProjectFile>();
     this.defaultChildGroup  = new List <CProjectGroup>();
 }
        /// <summary>
        /// 查找MDK节点文件
        /// </summary>
        /// <param name="xmlRead"></param>
        /// <param name="NodeName"></param>
        /// <returns></returns>
        public CProjectGroup FindMDKSubNodeFile(XmlReader xmlRead, string NodeName)
        {
            CProjectGroup _return = new CProjectGroup();

            //---是否位于结尾
            if (xmlRead.EOF)
            {
                return(null);
            }
            _return.mName = string.Empty;

            //---读取包含的文件信息
            do
            {
                //---Files后的节点深度是5
                if (xmlRead.Depth != 3)
                {
                    continue;
                }
                XmlReader subXmlRead = xmlRead.ReadSubtree();
                if (subXmlRead.Read() && (subXmlRead.NodeType == XmlNodeType.Element))
                {
                    //---读取当前节点以及子节点
                    XmlReader subsubXmlRead = subXmlRead.ReadSubtree();
                    do
                    {
                        subsubXmlRead.Read();
                        switch (subsubXmlRead.Name)
                        {
                        case "FileName":
                            subsubXmlRead.Read();
                            if (/*subsubXmlRead.Read() && */ (subsubXmlRead.NodeType == XmlNodeType.Text) && (subsubXmlRead.HasValue) && (subsubXmlRead.Depth == 3))
                            {
                                _return.mFile.Add(new CProjectFile());
                                string temp = subsubXmlRead.Value;
                                temp = temp.Replace("$PROJ_DIR$\\", "");
                                temp = temp.Replace("$PROJ_DIR$/", "");
                                temp = temp.Replace("\\", "/");
                                _return.mFile.Last().mName = temp;
                            }
                            break;

                        case "excluded":
                            if (subsubXmlRead.NodeType == XmlNodeType.Element)
                            {
                                XmlReader subSubSubXmlRead = subsubXmlRead.ReadSubtree();
                                do
                                {
                                    if (subSubSubXmlRead.Read() && (subSubSubXmlRead.NodeType == XmlNodeType.Text) && (subSubSubXmlRead.HasValue))
                                    {
                                        _return.mFile.Last().mExclude.Add(subSubSubXmlRead.Value);
                                    }
                                }while (!subSubSubXmlRead.EOF);
                                subSubSubXmlRead.Close();
                            }
                            break;
                        }
                    }while (!subsubXmlRead.EOF);
                    subsubXmlRead.Close();
                }
            }while (xmlRead.ReadToNextSibling(NodeName));
            return(_return);
        }
        /// <summary>
        /// 超找MDK节点分组
        /// </summary>
        /// <param name="xmlRead"></param>
        /// <param name="str"></param>
        /// <param name="parent"></param>
        /// <returns></returns>
        private CProjectGroup FindMDKSubNodeGroup(XmlReader xmlRead, string str, CProjectGroup parent = null)
        {
            CProjectGroup _return = new CProjectGroup(parent);

            if (xmlRead.NodeType != XmlNodeType.Element || xmlRead.Name != str)
            {
                xmlRead.ReadToFollowing(str);
            }
            if (xmlRead.EOF)
            {
                return(_return);
            }
            XmlReader subXmlRead = xmlRead.ReadSubtree();

            do
            {
                subXmlRead.Read();
            }while (subXmlRead.NodeType != XmlNodeType.Text);
            _return.mName = subXmlRead.Value;
            do
            {
                if (subXmlRead.Read() && subXmlRead.NodeType == XmlNodeType.Element)
                {
                    switch (subXmlRead.Name)
                    {
                    case "Group":

                        //---子分组信息---递归调用
                        _return.mChildGroup.Add(this.FindMDKSubNodeGroup(subXmlRead, str, _return));
                        break;

                    case "Files":

                        //---获取节点
                        XmlReader subSubXmlRead = subXmlRead.ReadSubtree();
                        do
                        {
                            subSubXmlRead.Read();
                            switch (subSubXmlRead.Name)
                            {
                            case "FilePath":                                            //"FilePath":
                                if (subSubXmlRead.Read() && (subSubXmlRead.NodeType == XmlNodeType.Text) && subSubXmlRead.HasValue && (subSubXmlRead.Depth == 3))
                                {
                                    _return.mFile.Add(new CProjectFile());
                                    string temp = subSubXmlRead.Value;
                                    temp = temp.Replace("$PROJ_DIR$\\", "");
                                    temp = temp.Replace("$PROJ_DIR$/", "");
                                    temp = temp.Replace("\\", "/");
                                    _return.mFile.Last().mName = temp;                                                    //subsubReader.Value;
                                }
                                break;

                            case "excluded":
                                if (subSubXmlRead.NodeType == XmlNodeType.Element)
                                {
                                    XmlReader subSubSubXmlRead = subSubXmlRead.ReadSubtree();
                                    do
                                    {
                                        subSubSubXmlRead.Read();
                                        if (subSubSubXmlRead.NodeType == XmlNodeType.Text)
                                        {
                                            _return.mFile.Last().mExclude.Add(subSubSubXmlRead.Value);
                                        }
                                    }while (!subSubSubXmlRead.EOF);
                                    subSubSubXmlRead.Close();
                                }
                                break;

                            default:
                                break;
                            }
                        }while (!subSubXmlRead.EOF);
                        subSubXmlRead.Close();
                        break;

                    case "excluded":
                        do
                        {
                            if (subXmlRead.Read() && (subXmlRead.NodeType == XmlNodeType.Text) && (subXmlRead.HasValue))
                            {
                                _return.mExclude.Add(subXmlRead.Value);
                            }
                        }while (xmlRead.Name != "excluded");
                        break;

                    default:
                        break;
                    }
                }
            }while (!subXmlRead.EOF);
            subXmlRead.Close();
            return(_return);
        }
        /// <summary>
        /// IAR项目转VS工程
        /// </summary>
        /// <param name="projectName"></param>
        public bool IARToVisualStudio(string projectName)
        {
            //---判断文件是否存在
            if (!File.Exists(projectName))
            {
                return(false);
            }

            //---变量定义
            CProjectConfig       prjcfg   = new CProjectConfig();
            List <CProjectGroup> prjgroup = new List <CProjectGroup>();

            //---读取设备的文件---创建XmlReader对象的实例并将其返回给调用程序
            XmlReader xmlRead = XmlReader.Create(new StringReader(File.ReadAllText(projectName)));

            //---读取配置文件---不断读取直至找到指定的元素
            xmlRead.ReadToFollowing("configuration");
            do
            {
                //---读取子节点---而是用于创建 XML 元素周围的边界
                XmlReader subXmlRead = xmlRead.ReadSubtree();

                //---移动读取器至下一个匹配子孙元素的节点
                subXmlRead.ReadToDescendant("name");

                //---从流中读取下一个节点
                subXmlRead.Read();
                prjcfg.mName = subXmlRead.Value;

                //---获取设置
                subXmlRead.ReadToFollowing("settings");

                //---
                subXmlRead.ReadToDescendant("archiveVersion");

                //---从流中读取下一个节点
                subXmlRead.Read();

                //---判断数据值
                if (int.Parse(subXmlRead.Value) > 0)
                {
                    prjcfg.mCMSIS = true;
                }
                else
                {
                    prjcfg.mCMSIS = false;
                }

                //---读取下一个节点
                subXmlRead.ReadToFollowing("settings");
                prjcfg.mDefine = this.FindIARSubNodeValue(subXmlRead, "CCDefines");

                //---IAR中增加的信息
                prjcfg.mDefine.Add("_IAR_");
                prjcfg.mDefine.Add("__ICCARM__");
                prjcfg.mDefine.Add("_Pragma(x)=");
                prjcfg.mDefine.Add("__interrupt=");

                //prjcfg._define.Add("__packed=");
                //prjcfg._define.Add("__weak=");
                prjcfg.mDefine.Add("__packed=__attribute__((__packed__))");
                prjcfg.mDefine.Add("__weak=__attribute__((weak))");

                //prjcfg._define.Add("__attribute__((x))=");
                //prjcfg._define.Add("__STATIC_INLINE=");
                //---获取预包含的头文件
                prjcfg.mPreInclude = this.FindIARSubNodeValue(subXmlRead, "PreInclude");

                //---获取包含文件的路径
                prjcfg.mIncludePath = this.FindIARSubNodeValue(subXmlRead, "CCIncludePath2");
            }while (xmlRead.ReadToNextSibling("configuration"));
            xmlRead.Close();

            xmlRead = XmlReader.Create(new StringReader(File.ReadAllText(projectName)));

            //---读取配置文件---不断读取直至找到指定的元素
            //---获取组文件
            xmlRead.ReadToFollowing("group");
            do
            {
                CProjectGroup _return = FindIARSubNodeGroup(xmlRead, "group");
                if (_return != null)
                {
                    prjgroup.Add(_return);
                }
            }while (xmlRead.ReadToNextSibling("group"));
            xmlRead.Close();

            //---获取C文件
            xmlRead = XmlReader.Create(new StringReader(File.ReadAllText(projectName)));
            do
            {
                xmlRead.ReadToFollowing("file");
                if (xmlRead.Depth == 1 && xmlRead.NodeType == XmlNodeType.Element)
                {
                    CProjectGroup _return = FindIARSubNodeFile(xmlRead, "file");
                    if (_return != null)
                    {
                        prjgroup.Add(_return);
                    }
                }
            }while (!xmlRead.EOF);
            xmlRead.Close();

            //string configurations = "Debug" + "\", \"" + "Release";
            //StreamWriter file =new StreamWriter(Path.GetDirectoryName(projectName) + "\\IARToVS.lua");
            StreamWriter file = new StreamWriter(Path.GetDirectoryName(projectName) + "\\premake5.lua");

            //StreamWriter file = new StreamWriter(Directory.GetParent(Path.GetDirectoryName(projectName)).FullName + "\\premake5.lua");
            {
                //
                file.WriteLine("  solution \"" + Path.GetFileNameWithoutExtension(projectName) + "\"");
                file.WriteLine("  configurations { \"" + string.Join("\", \"", prjcfg.mName) + "\" }");

                //file.WriteLine("  configurations { \"" + configurations + "\" }");
                file.WriteLine("  project\"" + Path.GetFileNameWithoutExtension(projectName) + "\"");
                file.WriteLine("  kind \"ConsoleApp\"");
                file.WriteLine("  language \"C\"");
                file.WriteLine("  filter \"configurations:" + prjcfg.mName + "\"");
                file.WriteLine("  sysincludedirs  {\"$(VC_IncludePath)\"}");
                file.WriteLine("  defines { \"" + string.Join("\", \"", prjcfg.mDefine) + "\" }");
                file.WriteLine("  forceincludes { \"" + string.Join("\", \"", prjcfg.mPreInclude) + "\" }");
                file.WriteLine("  includedirs { \"" + string.Join("\", \"", prjcfg.mIncludePath) + "\" }");
                List <string> srcFiles = new CProjectGroup().GetFile(prjgroup, prjcfg.mName);
                file.WriteLine("  files { \"" + string.Join("\", \"", srcFiles) + "\" }");
                List <string> vGroups = new CProjectGroup().GetPath(prjgroup, prjcfg.mName);
                file.WriteLine("  vpaths {" + string.Join(" , ", vGroups) + " }");

                //file.Write(IncOverride);
                file.Close();
            }

            return(true);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="projectName"></param>
        /// <returns></returns>
        public bool MDKToVisualStudio(string projectName)
        {
            //---判断文件是否存在
            if (!File.Exists(projectName))
            {
                return(false);
            }

            //---变量定义
            CProjectConfig       prjcfg   = new CProjectConfig();
            List <CProjectGroup> prjgroup = new List <CProjectGroup>();

            //---读取设备的文件---创建XmlReader对象的实例并将其返回给调用程序
            XmlReader xmlRead = XmlReader.Create(new StringReader(File.ReadAllText(projectName)));

            //---读取配置文件---不断读取直至找到指定的元素
            xmlRead.ReadToFollowing("TargetOption");
            do
            {
                //---读取子节点---而是用于创建 XML 元素周围的边界
                XmlReader subXmlRead = xmlRead.ReadSubtree();

                //---移动读取器至下一个匹配子孙元素的节点
                subXmlRead.ReadToDescendant("OutputName");

                //---从流中读取下一个节点
                subXmlRead.Read();
                prjcfg.mName = subXmlRead.Value;

                //---获取设置
                subXmlRead.ReadToFollowing("Cads");

                //---
                subXmlRead.ReadToDescendant("Optim");

                //---从流中读取下一个节点
                subXmlRead.Read();

                //---判断数据值
                if (int.Parse(subXmlRead.Value) > 0)
                {
                    prjcfg.mCMSIS = true;
                }
                else
                {
                    prjcfg.mCMSIS = false;
                }

                //---读取下一个节点
                subXmlRead.ReadToFollowing("VariousControls");
                subXmlRead.ReadToFollowing("Define");
                prjcfg.mDefine = this.FindMDKSubNodeValue(subXmlRead.ReadSubtree());

                //----Keil工程的生成中借用了IAR的一些文件,用于避免一些数据类型报错的问题
                //---IAR中增加的信息
                prjcfg.mDefine.Add("_IAR_");
                prjcfg.mDefine.Add("__ICCARM__");
                prjcfg.mDefine.Add("__CC_ARM");
                prjcfg.mDefine.Add("_Pragma(x)=");
                prjcfg.mDefine.Add("__interrupt=");

                //prjcfg._define.Add("__packed=");
                //prjcfg._define.Add("__weak=");
                prjcfg.mDefine.Add("__packed=__attribute__((__packed__))");
                prjcfg.mDefine.Add("__weak=__attribute__((weak))");

                //prjcfg._define.Add("__attribute__((x))=");
                //prjcfg._define.Add("__STATIC_INLINE=");
                //---获取预包含的头文件
                //subXmlRead.ReadToFollowing("PreInclude");
                //prjcfg._preInclude = this.GetKeilSubNodeValue(subXmlRead,';');
                //---获取包含文件的路径
                subXmlRead.ReadToFollowing("IncludePath");
                prjcfg.mIncludePath = this.FindMDKSubNodeValue(subXmlRead.ReadSubtree(), ';');
            }while (xmlRead.ReadToNextSibling("TargetOption"));
            xmlRead.Close();

            xmlRead = XmlReader.Create(new StringReader(File.ReadAllText(projectName)));

            //---读取配置文件---不断读取直至找到指定的元素
            //---获取组文件

            do
            {
                xmlRead.ReadToFollowing("Group");
                if (xmlRead.Depth == 4 && xmlRead.NodeType == XmlNodeType.Element)
                {
                    CProjectGroup _return = FindMDKSubNodeGroup(xmlRead, "Group");
                    if (_return != null)
                    {
                        prjgroup.Add(_return);
                    }
                }
            }while (!xmlRead.EOF);
            xmlRead.Close();

            //---处理MDK文档
            //foreach (var temp in prjgroup)
            //{
            //	if (temp._name.Contains("MDK"))
            //	{
            //		temp._name = temp._name.Substring(temp._name.IndexOf("/MDK") + 1);
            //	}
            //	else
            //	{
            //		continue;
            //	}
            //}

            //---获取C文件
            xmlRead = XmlReader.Create(new StringReader(File.ReadAllText(projectName)));
            do
            {
                xmlRead.ReadToFollowing("Files");
                if (xmlRead.Depth == 3 && xmlRead.NodeType == XmlNodeType.Element)
                {
                    CProjectGroup _return = FindMDKSubNodeFile(xmlRead, "Files");
                    if (_return != null)
                    {
                        prjgroup.Add(_return);
                    }
                }
            }while (!xmlRead.EOF);
            xmlRead.Close();

            //---获取Keil的执行路径
            //---Keil5

            /*
             * string keilPath = System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetProcessesByName("UV4")[0].MainModule.FileName);
             * if (keilPath == null)
             * {
             *      //---keil4
             *      keilPath = System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetProcessesByName("UV3")[0].MainModule.FileName);
             * }
             * if (keilPath == null)
             * {
             *      //---keil2
             *      keilPath = System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetProcessesByName("UV2")[0].MainModule.FileName);
             * }
             */
            string keilPath    = this.GetMDKPatch();
            string includedirs = string.Join("\", \"", prjcfg.mIncludePath);

            if (keilPath != null)
            {
                keilPath    += @"\RV31";
                keilPath     = keilPath.Replace("\\", "/");
                includedirs += "\", \"" + keilPath;
            }

            //string configurations = "Debug" + "\", \"" + "Release";
            //StreamWriter file = new StreamWriter(Path.GetDirectoryName(projectName) + "\\KeilToVS.lua");
            StreamWriter file = new StreamWriter(Path.GetDirectoryName(projectName) + "\\premake5.lua");

            //StreamWriter file = new StreamWriter(Directory.GetParent(Path.GetDirectoryName(projectName)).FullName + "\\premake5.lua");
            {
                //
                file.WriteLine("  solution \"" + Path.GetFileNameWithoutExtension(projectName) + "\"");
                file.WriteLine("  configurations { \"" + string.Join("\", \"", prjcfg.mName) + "\" }");

                //file.WriteLine("  configurations { \"" + configurations + "\" }");
                file.WriteLine("  project\"" + Path.GetFileNameWithoutExtension(projectName) + "\"");
                file.WriteLine("  kind \"ConsoleApp\"");
                file.WriteLine("  language \"C\"");
                file.WriteLine("  filter \"configurations:" + prjcfg.mName + "\"");
                file.WriteLine("  sysincludedirs  {\"$(VC_IncludePath)\"}");
                file.WriteLine("  defines { \"" + string.Join("\", \"", prjcfg.mDefine) + "\" }");
                file.WriteLine("  forceincludes { \"" + string.Join("\", \"", prjcfg.mPreInclude) + "\" }");
                file.WriteLine("  includedirs { \"" + includedirs + "\" }");
                List <string> srcFiles = new CProjectGroup().GetFile(prjgroup, prjcfg.mName);
                file.WriteLine("  files { \"" + string.Join("\", \"", srcFiles) + "\"}");
                List <string> vGroups = new CProjectGroup().GetPath(prjgroup, prjcfg.mName);
                file.WriteLine("  vpaths {" + string.Join(" , ", vGroups) + " }");

                //file.Write(IncOverride);
                file.Close();
            }

            return(true);
        }