public void AppendedTest()
        {
            AbsoluteCrosspath xLocalIncludeDirectory = AbsoluteCrosspath.GetCurrentDirectory();
            RelativeCrosspath xRelPath = RelativeCrosspath.FromString("lzcintrin.h");
            AbsoluteCrosspath xPath    = xLocalIncludeDirectory.Appended(xRelPath);

            Assert.AreEqual(xPath.ToString(), xLocalIncludeDirectory + @"\" + xRelPath);
        }
Exemple #2
0
        public void DownloadStandardIncludeDirectories(RemoteHost remote)
        {
            if (Skip)
            {
                return;
            }
            // use sftp, or, when not possible, ssh cat
            AbsoluteCrosspath xpwd         = AbsoluteCrosspath.GetCurrentDirectory();
            AbsoluteCrosspath xCompilerDir = xpwd.Appended(RelativeCrosspath.FromString($@"compilers\{ShortName}"));

            Directory.CreateDirectory(xCompilerDir.ToString());
            foreach (IncludeDirectory includeDirectory in IncludeDirectories)
            {
                includeDirectory.RebaseToLocal(remote, xCompilerDir);
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="remote">Remote host to download from</param>
        /// <param name="localXpath">Local directory for remote include directories, e.g. D:\Project1\remote\192.168.0.1.</param>
        public void RebaseToLocal(RemoteHost remote, AbsoluteCrosspath localXpath)
        {
            if (this.Flavor == CrosspathFlavor.Windows)
            {
                return;
            }

            int xtractBufSize = 65536;

            Byte[] zipExtractBuf = new Byte[xtractBufSize];

            String remoteFilename        = $"/tmp/{ShortName}.zip";
            String localDirectoryPattern = $@"include\{ShortName}";
            String localFilenamePattern  = $"{ShortName}.zip";

            AbsoluteCrosspath xLocalIncludeDirectory = localXpath.Appended(RelativeCrosspath.FromString(localDirectoryPattern));
            String            localIncludeDirectory  = xLocalIncludeDirectory.ToString();
            String            localFilename          = localXpath.Appended(RelativeCrosspath.FromString(localFilenamePattern)).ToString();

            Logger.WriteLine(LogLevel.Info, $"Rolling {this} into {localFilenamePattern}...");
            Directory.CreateDirectory(localIncludeDirectory);
            if (remote.Execute($"pushd {this} && zip -1 -r -q {remoteFilename} . && popd", out String result) != RemoteHost.Success)
            {
                Logger.WriteLine(LogLevel.Error, result);
                // FIXME, hack
                this.Rebase(this, xLocalIncludeDirectory);
                autoDownloaded = true;
                return;
            }

            remote.DownloadFile(remoteFilename, localFilename);
            File.WriteAllText(xLocalIncludeDirectory.Appended(RelativeCrosspath.FromString($@"..\{ShortName}_origin.txt")).ToString(), this.ToString());

            // not working bcz of NTFS case & special names restrictions. extract manually.
            //ZipFile.ExtractToDirectory(localFilename, localDirectory, Encoding.UTF8);

            using (FileStream fs = new FileStream(localFilename, FileMode.Open)) {
                ZipArchive za = new ZipArchive(fs, ZipArchiveMode.Read);
                foreach (ZipArchiveEntry zaEntry in za.Entries)
                {
                    RelativeCrosspath xRelPath = RelativeCrosspath.FromString(zaEntry.FullName);
                    AbsoluteCrosspath xPath    = xLocalIncludeDirectory.Appended(xRelPath);
                    if (zaEntry.FullName.EndsWith("/"))
                    {
                        // packed directory
                        continue;
                    }

                    String path = xPath.ToString();
                    if (File.Exists(path))
                    {
                        Logger.WriteLine(LogLevel.Warning, $"file '{zaEntry.FullName}' already exists as '{path}' - case problems?");
                        continue;
                    }

                    // hax, remove leading / from path
                    if (xRelPath.ToString() == Compiler.RemoteTempFile.Substring(1))
                    {
                        continue;
                    }

                    String dirname = new AbsoluteCrosspath(xPath).ToContainingDirectory().ToString();
                    try {
                        Directory.CreateDirectory(dirname);
                        // packed file
                        using (FileStream xfs = new FileStream(path, FileMode.CreateNew)) {
                            using (Stream zas = zaEntry.Open()) {
                                while (true)
                                {
                                    int len = zas.Read(zipExtractBuf, 0, xtractBufSize);
                                    if (len == 0)
                                    {
                                        // EOF
                                        break;
                                    }

                                    xfs.Write(zipExtractBuf, 0, len);
                                }
                                zas.Close();
                            }
                        }
                    }
                    catch (Exception e) {
                        Logger.WriteLine(LogLevel.Error, $"Could not extract '${zaEntry.FullName}': ${e.Message}");
                    }
                }
            }

            // if extraction went ok, we can remove files
            remote.Execute($"rm {remoteFilename}", out String _);
            File.Delete(localFilename);

            // since we definitely have a local copy of include directory, rebase on it
            this.Rebase(this, xLocalIncludeDirectory);
            autoDownloaded = true;
        }
        /// <summary>
        /// Writes compiler_${ShortName}.props and compiler_$(ShortName}_compat.h
        /// </summary>
        public void WriteToFile(AbsoluteCrosspath solutionDir)
        {
            if (BaseCompiler.Skip)
            {
                return;
            }

            AbsoluteCrosspath xpath = solutionDir.Appended(RelativeCrosspath.FromString(CompilerInstanceCompatHeaderPath)).ToContainingDirectory();

            Directory.CreateDirectory(xpath.ToString());
            using (StreamWriter sw = new StreamWriter(CompilerInstanceCompatHeaderPath, false, Encoding.UTF8)) {
                sw.WriteLine(@"#pragma once");
                sw.WriteLine($"/* This is generated from compiler instance {BaseCompiler} {this} */");
                foreach (Define compilerInternalDefine in Defines)
                {
                    sw.WriteLine($"#define {compilerInternalDefine.Name} {compilerInternalDefine.Value}");
                }
            }

            XmlDocument doc = new XmlDocument();

            doc.AppendChild(doc.CreateXmlDeclaration("1.0", "utf-8", null));

            XmlElement projectNode = doc.CreateElement("Project");

            projectNode.SetAttribute("DefaultTargets", "Build");
            projectNode.SetAttribute("ToolsVersion", "Current");
            projectNode.SetAttribute("xmlns", "http://schemas.microsoft.com/developer/msbuild/2003");

            XmlElement projectImportProps = doc.CreateElement("Import");

            projectImportProps.SetAttribute("Project", $@"$(SolutionDir)\{BaseCompiler.PropsFileName}");
            projectNode.AppendChild(projectImportProps);

            // Platform settings

            /*
             * <ItemGroup Label="ProjectConfigurations">
             * <ProjectConfiguration Include="Debug|x64">
             * <Configuration>Debug</Configuration>
             * <Platform>x64</Platform>
             * </ProjectConfiguration>
             * <ProjectConfiguration Include="Release|x64">
             * <Configuration>Release</Configuration>
             * <Platform>x64</Platform>
             * </ProjectConfiguration>
             * </ItemGroup>
             */
            XmlElement projectItemGroupPlatform = doc.CreateElement("ItemGroup");

            projectItemGroupPlatform.SetAttribute("Label", "ProjectConfigurations");

            foreach (String buildConfiguration in SolutionStructure.SolutionConfigurations)
            {
                XmlElement projectPlatformConfiguration = doc.CreateElement("ProjectConfiguration");
                projectPlatformConfiguration.SetAttribute("Include", $"{buildConfiguration}|{VSPlatform}");
                XmlElement projectPlatformConfiguration_Configuration = doc.CreateElement("Configuration");
                projectPlatformConfiguration_Configuration.InnerText = buildConfiguration;
                projectPlatformConfiguration.AppendChild(projectPlatformConfiguration_Configuration);
                XmlElement projectPlatformConfiguration_Platform = doc.CreateElement("Platform");
                projectPlatformConfiguration_Platform.InnerText = VSPlatform.ToString();
                projectPlatformConfiguration.AppendChild(projectPlatformConfiguration_Platform);
                projectItemGroupPlatform.AppendChild(projectPlatformConfiguration);
            }

            projectNode.AppendChild(projectItemGroupPlatform);

            // IDU settings
            XmlElement projectPropertyGroupIDU = doc.CreateElement("PropertyGroup");

            XmlElement projectIncludePaths = doc.CreateElement("NMakeIncludeSearchPath");

            // DONE: intermix project include directories with compiler include directories
            foreach (IncludeDirectory includePath in IncludeDirectories)
            {
                projectIncludePaths.InnerText += includePath.GetLocalProjectPath(solutionDir) + ";";
            }
            projectIncludePaths.InnerText += "$(CompilerIncludeDirAfter);$(NMakeIncludeSearchPath)";
            projectPropertyGroupIDU.AppendChild(projectIncludePaths);

            // maybe someday this will be helpful, but now it can be inherited from Solution.props
            XmlElement projectForcedIncludes = doc.CreateElement("NMakeForcedIncludes");

            // DONE: add compiler compat header to forced includes
            projectForcedIncludes.InnerText = $@"$(SolutionDir)\solution_compat.h;$(SolutionDir)\{CompilerInstanceCompatHeaderPath};$(SolutionDir)\solution_post_compiler_compat.h";
            projectPropertyGroupIDU.AppendChild(projectForcedIncludes);

            XmlElement compilerForcedIncludes = doc.CreateElement("CompilerCompat");

            // DONE: add compiler compat header to forced includes
            compilerForcedIncludes.InnerText = $@"$(SolutionDir)\{CompilerInstanceCompatHeaderPath}";
            projectPropertyGroupIDU.AppendChild(compilerForcedIncludes);

            projectNode.AppendChild(projectPropertyGroupIDU);

            doc.AppendChild(projectNode);
            doc.Save(PropsFileName);
        }