Beispiel #1
0
        /// <summary>
        /// IAR项目转VS工程
        /// </summary>
        /// <param name="projectName"></param>
        public bool IARProjectToVSProject(string projectName)
        {
            //---判断文件是否存在
            if (!File.Exists(projectName))
            {
                return(false);
            }

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

            //---读取设备的文件---创建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._name = subXmlRead.Value;

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

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

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

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

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

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

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

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

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

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

            //---读取配置文件---不断读取直至找到指定的元素
            //---获取组文件
            xmlRead.ReadToFollowing("group");
            do
            {
                ProjectGroup _return = GetIarSubNodeGroup(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)
                {
                    ProjectGroup _return = GetIarSubNodeFile(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._name) + "\" }");

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

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

            return(true);
        }
Beispiel #2
0
        public bool KeilProjectToVSProject(string projectName)
        {
            //---判断文件是否存在
            if (!File.Exists(projectName))
            {
                return(false);
            }

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

            //---读取设备的文件---创建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._name = subXmlRead.Value;

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

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

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

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

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

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

                //prjcfg._define.Add("__packed=");
                //prjcfg._define.Add("__weak=");
                prjcfg._define.Add("__packed=__attribute__((__packed__))");
                prjcfg._define.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._includePath = this.GetKeilSubNodeValue(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)
                {
                    ProjectGroup _return = GetKeilSubNodeGroup(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)
                {
                    ProjectGroup _return = GetKeilSubNodeFile(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 includedirs = string.Join("\", \"", prjcfg._includePath);

            if (keilPath != null)
            {
                keilPath     = keilPath.Replace("\\", "/");
                keilPath     = keilPath.Replace("UV4", "ARM/RV31");
                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._name) + "\" }");

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

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

            return(true);
        }