Beispiel #1
0
        public CompilerDefines CreateCompilerDefines()
        {
            CompilerDefines result = CompilerDefines.CreateStandard();

            for (char option = 'A'; option <= 'Z'; ++option)
            {
                result.DefineDirective("IFOPT " + option + "-", _compilerOptions.IsOptionOff(option));
                result.DefineDirective("IFOPT " + option + "+", _compilerOptions.IsOptionOn(option));
            }
            foreach (string define in CustomDefines.Split(';'))
            {
                result.DefineSymbol(define);
            }
            result.DefineSymbol(DelphiVersionDefine);
            foreach (string condition in FalseIfConditions.Split(';'))
            {
                result.DefineDirectiveAsFalse(condition);
            }
            foreach (string condition in TrueIfConditions.Split(';'))
            {
                result.DefineDirectiveAsTrue(condition);
            }
            return(result);
        }
        private bool DoExecute()
        {
            if (string.IsNullOrEmpty(LauncherFileName))
            {
                LauncherFileName = "launcher.c";
            }
            AotAssemblies     = AotAssemblies ?? new ITaskItem[0];
            BundledAssemblies = BundledAssemblies ?? new ITaskItem[0];

            Log.LogDebugMessage("GenerateLauncher Task");
            Log.LogDebugMessage("  OutputDirectory: {0}", OutputDirectory);
            Log.LogDebugMessage("  MainAssemblyName: {0}", MainAssemblyName);
            Log.LogDebugMessage("  LauncherTemplatePath: {0}", LauncherTemplatePath);
            Log.LogDebugMessage("  LauncherFileName: {0}", LauncherFileName);
            Log.LogDebugMessage("  UseCustomPlatformImpl: {0}", UseCustomPlatformImpl);
            Log.LogDebugTaskItems("  AotAssemblies:", AotAssemblies);
            Log.LogDebugTaskItems("  BundledAssemblies:", BundledAssemblies);

            var mainAssemblyName = MainAssemblyName;

            if (!SkipMain && string.IsNullOrEmpty(mainAssemblyName))
            {
                var found = AotAssemblies.FirstOrDefault(a => Path.GetExtension(a.ItemSpec).ToLower() == ".exe");
                if (found == null)
                {
                    throw new InvalidOperationException("Could not determine main assembly. No .exe assembly found.");
                }
                mainAssemblyName = Path.GetFileName(found.ItemSpec);
                Log.LogDebugMessage("  Found main assembly: {0}", mainAssemblyName);
            }

            var aoted   = AotAssemblies.Select(a => Symbols.GetAotModuleSymbolName(a.ItemSpec, UserSymbolPrefix)).ToList();
            var bundled = BundledAssemblies.Select(a => {
                var assemblyName = Symbols.GetBundledAssemblyName(a.ItemSpec, Log);
                return(new {
                    getterSymbol = Symbols.GetBundledAssemblyGetter(assemblyName),
                    configGetterSymbol = Symbols.GetBundledAssemblyConfigGetter(assemblyName),
                    cleanupSymbol = Symbols.GetBundledAssemblyCleanup(assemblyName)
                });
            }).ToList();

            var nl       = "\n";
            var launcher = string.IsNullOrEmpty(LauncherTemplatePath) ? Resources.LauncherTemplate : File.ReadAllText(LauncherTemplatePath);

            var aotModules = new StringBuilder()
                             .AppendLine("BEGIN_DECLARE_AOT_MODULES")
                             .AppendLine(string.Join(nl, aoted.Select(a => $"\tDECLARE_AOT_MODULE ({a})")))
                             .AppendLine("END_DECLARE_AOT_MODULES")
                             .AppendLine("BEGIN_DEFINE_AOT_MODULES")
                             .AppendLine(string.Join(nl, aoted.Select(a => $"\tDEFINE_AOT_MODULE ({a})")))
                             .AppendLine("END_DEFINE_AOT_MODULES");

            launcher = Regex.Replace(launcher, @"(//\s*)\$\{AOTModules\}", aotModules.ToString());

            var bundledAssemblies = new StringBuilder()
                                    .AppendLine("BEGIN_DECLARE_BUNDLED_ASSEMBLIES")
                                    .AppendLine(string.Join(nl, bundled.Select(a => $"\tDECLARE_BUNDLED_ASSEMBLY ({a.getterSymbol})")))
                                    .AppendLine("END_DECLARE_BUNDLED_ASSEMBLIES")
                                    .AppendLine("BEGIN_DEFINE_BUNDLED_ASSEMBLIES")
                                    .AppendLine(string.Join(nl, bundled.Select(a => $"\tDEFINE_BUNDLED_ASSEMBLY ({a.getterSymbol})")))
                                    .AppendLine("END_DEFINE_BUNDLED_ASSEMBLIES");

            launcher = Regex.Replace(launcher, @"(//\s*)\$\{BundledAssemblies\}", bundledAssemblies.ToString());

            var bundledAssemblyConfigs = new StringBuilder()
                                         .AppendLine("BEGIN_DECLARE_BUNDLED_ASSEMBLY_CONFIGS")
                                         .AppendLine(string.Join(nl, bundled.Select(a => $"\tDECLARE_BUNDLED_ASSEMBLY_CONFIG ({a.configGetterSymbol})")))
                                         .AppendLine("END_DECLARE_BUNDLED_ASSEMBLY_CONFIGS")
                                         .AppendLine("BEGIN_DEFINE_BUNDLED_ASSEMBLY_CONFIGS")
                                         .AppendLine(string.Join(nl, bundled.Select(a => $"\tDEFINE_BUNDLED_ASSEMBLY_CONFIG ({a.configGetterSymbol})")))
                                         .AppendLine("END_DEFINE_BUNDLED_ASSEMBLY_CONFIGS");

            launcher = Regex.Replace(launcher, @"(//\s*)\$\{BundledAssemblyConfigs\}", bundledAssemblyConfigs.ToString());

            var bundledAssemblyCleanups = new StringBuilder()
                                          .AppendLine("BEGIN_DECLARE_BUNDLED_ASSEMBLY_CLEANUPS")
                                          .AppendLine(string.Join(nl, bundled.Select(a => $"\tDECLARE_BUNDLED_ASSEMBLY_CLEANUP ({a.cleanupSymbol})")))
                                          .AppendLine("END_DECLARE_BUNDLED_ASSEMBLY_CLEANUPS")
                                          .AppendLine("BEGIN_DEFINE_BUNDLED_ASSEMBLY_CLEANUPS")
                                          .AppendLine(string.Join(nl, bundled.Select(a => $"\tDEFINE_BUNDLED_ASSEMBLY_CLEANUP ({a.cleanupSymbol})")))
                                          .AppendLine("END_DEFINE_BUNDLED_ASSEMBLY_CLEANUPS");

            launcher = Regex.Replace(launcher, @"(//\s*)\$\{BundledAssemblyCleanups\}", bundledAssemblyCleanups.ToString());

            var defines = new List <string>();

            if (SkipMain)
            {
                defines.Add("SKIP_MAIN=1");
            }
            if (!string.IsNullOrEmpty(CustomDefines))
            {
                defines.AddRange(CustomDefines.Split(';'));
            }

            defines.RemoveAll(d => String.IsNullOrWhiteSpace(d));

            defines = defines
                      .Select(d => d.Split("=".ToCharArray(), 2))
                      .Select(a => "#define " + string.Join(" ", a.ToArray()).Trim()).ToList();
            launcher = Regex.Replace(launcher, @"(//\s*)\$\{Defines\}", string.Join(nl, defines));

            launcher = Regex.Replace(launcher, @"\$\{MainAssemblyName\}", mainAssemblyName ?? "");

            if (!Directory.Exists(OutputDirectory))
            {
                Directory.CreateDirectory(OutputDirectory);
            }
            var outputFiles     = new List <string>();
            var outputFile      = Path.Combine(OutputDirectory, LauncherFileName);
            var writeOutputFile = true;

            if (File.Exists(outputFile))
            {
                var oldContents = File.ReadAllText(outputFile);
                writeOutputFile = oldContents != launcher;
            }
            if (writeOutputFile)
            {
                File.WriteAllText(outputFile, launcher);
            }
            else
            {
                Log.LogDebugMessage("  Output file {0} hasn't changed. Won't overwrite.", outputFile);
            }
            outputFiles.Add(outputFile);

            if (string.IsNullOrEmpty(LauncherTemplatePath))
            {
                // Copy the bundled platform.h
                var platformHeaderFile = Path.Combine(OutputDirectory, "platform.h");
                File.WriteAllText(platformHeaderFile, Resources.PlatformHeader);
                outputFiles.Add(platformHeaderFile);

                if (!UseCustomPlatformImpl)
                {
                    // Use the bundled platform.c
                    var platformImplFile = Path.Combine(OutputDirectory, "platform.c");
                    File.WriteAllText(platformImplFile, Resources.PlatformImpl);
                    outputFiles.Add(platformImplFile);
                }
            }

            GeneratedFiles = outputFiles.Select(f => new TaskItem(f)).ToArray();
            Log.LogDebugTaskItems("  [Output] GeneratedFiles:", GeneratedFiles);

            return(true);
        }