Beispiel #1
0
        /// <summary>初始化</summary>
        /// <param name="addlib"></param>
        /// <returns></returns>
        public virtual Boolean Init(Boolean addlib = true)
        {
            var tp = ToolPath;

            if (tp.IsNullOrEmpty() || !Directory.Exists(tp))
            {
                XTrace.WriteLine("未找到编译器!");
                return(false);
            }

            XTrace.WriteLine("发现 {0} {1} {2}", Name, Version, tp);

            Libs.Clear();
            Objs.Clear();

            // 扫描当前所有目录,作为头文件引用目录
            //var ss = new String[] { ".", "..\\SmartOS" };
            var ss = new String[] { ".", "inc", "include", "lib" };

            foreach (var src in ss)
            {
                var p = src.GetFullPath();
                if (!Directory.Exists(p))
                {
                    p = ("..\\" + src).GetFullPath();
                }
                if (!Directory.Exists(p))
                {
                    continue;
                }

                AddIncludes(p, false);
                if (addlib)
                {
                    AddLibs(p);
                }
            }

            return(true);
        }
Beispiel #2
0
        /// <summary>编译所有文件</summary>
        /// <returns></returns>
        public Int32 CompileAll()
        {
            Objs.Clear();
            var count = 0;

            if (Files.Count == 0)
            {
                return(0);
            }

            // 计算根路径,输出的对象文件以根路径下子路径的方式存放
            GetRoot();

            // 提前创建临时目录
            var obj  = GetObjPath(null);
            var list = new List <String>();

            var cmd  = GetCompileCommand(true);
            var cmd2 = GetCompileCommand(false);

            Console.Write("编译参数:");
            Console.ForegroundColor = ConsoleColor.Magenta;
            if (Files.Any(e => e.EndsWithIgnoreCase(".cpp", ".cxx")))
            {
                Console.WriteLine(cmd);
            }
            if (Files.Any(e => e.EndsWithIgnoreCase(".c")))
            {
                Console.WriteLine(cmd2);
            }
            Console.ResetColor();

            var asm = 0;

            foreach (var item in Files)
            {
                var rs = 0;
                var sw = new Stopwatch();
                sw.Start();
                switch (Path.GetExtension(item).ToLower())
                {
                case ".c":
                    rs = Compile(cmd2, item);
                    break;

                case ".cpp":
                case ".cxx":
                    rs = Compile(cmd, item);
                    break;

                case ".s":
                    rs = Assemble(item, asm == 0);
                    if (rs != -2)
                    {
                        asm++;
                    }
                    break;

                default:
                    break;
                }

                sw.Stop();

                if (rs == 0 || rs == -1)
                {
                    var fi   = item;
                    var root = _Root;
                    if (fi.StartsWith(root))
                    {
                        fi = fi.Substring(root.Length);
                    }
                    Console.Write("编译:{0}\t", fi);
                    var old = Console.ForegroundColor;
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("\t {0:n0}毫秒", sw.ElapsedMilliseconds);
                    Console.ForegroundColor = old;
                }

                if (rs <= 0)
                {
                    if (!Preprocess)
                    {
                        //var fi = obj.CombinePath(Path.GetFileNameWithoutExtension(item) + ".o");
                        var fi = GetObjPath(item) + ".o";
                        list.Add(fi);
                    }
                }
            }

            Console.WriteLine("等待编译完成:");
            var left = 0;

            try
            {
                // 如果外部把编译结果输出到文本文件,那么这里会抛出异常
                left = Console.CursorLeft;
            }
            catch { }
            var list2 = new List <String>(list);
            var end   = DateTime.Now.AddSeconds(10);
            var fs    = 0;

            while (fs < Files.Count)
            {
                for (var i = list2.Count - 1; i >= 0; i--)
                {
                    if (File.Exists(list[i]))
                    {
                        fs++;
                        list2.RemoveAt(i);
                    }
                }
                try
                {
                    // 如果外部把编译结果输出到文本文件,那么这里会抛出异常
                    Console.CursorLeft = left;
                }
                catch
                {
                    Console.WriteLine();
                }
                Console.Write("\t {0}/{1} = {2:p}", fs, Files.Count, (Double)fs / Files.Count);

                if (DateTime.Now > end)
                {
                    Console.Write(" 等待超时!");
                    break;
                }
                Thread.Sleep(500);
            }
            Console.WriteLine();

            for (var i = 0; i < list.Count; i++)
            {
                if (File.Exists(list[i]))
                {
                    count++;
                    Objs.Add(list[i]);
                }
            }

            return(count);
        }