private static TestTarget Add(List<TestTarget>list, string name, string version )
 {
     foreach(var item in list) {
         if(name == item.Name && version== item.Version)
             return item;
     }
     var t = new TestTarget { Name=name, Version=version };
     list.Add(t);
     return t;
 }
 public void CreateEXE(TestTarget exe)
 {
     CreateBinary(exe, ApplicationTemplate, ".exe", ExecutableManifest, @" ""{0}"" ""/Fe{1}"" /DISOLATION_AWARE_ENABLED=1 /TC /Y- ", AppAutopackage);
 }
        public void CreateBinary(TestTarget binary,string binaryTemplate, string extension , string manifestTemplate ,  string commandLine, string packageTemplate)
        {
            if(binary.Built)
                return;

            foreach(var dep in binary.Dependencies) {
                CreateDLL(dep);
            }

            if(binary.Ignore)
                return;

            Console.WriteLine("Building Binary [{0}-{1}]", binary.Name, binary.Version);

            var importLibrary = new StringBuilder();
            var callLibrary = new StringBuilder();

            foreach(var dll in binary.Dependencies) {
                importLibrary.Append(LibraryReferenceTemplate.Replace("[$LIBNAME]", dll.Name).Replace("[$LIBVERSION]", dll.Version).Replace("[$PUBLICKEYTOKEN]", publicKeyToken).Replace("[$ARCH]", arch));
                callLibrary.Append(LibraryFunctionCallTemplate.Replace("[$LIBNAME]", dll.Name).Replace("[$LIBVERSION]", dll.Version).Replace("[$PUBLICKEYTOKEN]", publicKeyToken).Replace("[$ARCH]", arch));
            }

            var tempFolder = Path.Combine(Path.GetTempPath(), binary.Name + "-" + binary.Version);
            Directory.CreateDirectory(tempFolder);
            var outputBinaryFolder = Path.Combine(outputPath, binary.Name+"-"+binary.Version);
            Directory.CreateDirectory(outputBinaryFolder);
            Environment.CurrentDirectory = outputBinaryFolder;

            // create DEF files to create import .lib files
            foreach(var dll in binary.Dependencies) {
                var defFile = ModuleDefinitionFileTemplate + "print_" + dll.Name;
                File.WriteAllText(Path.Combine(tempFolder, dll.Name+".def"), defFile);
                cleanupFiles.Add(Path.Combine(tempFolder, dll.Name+".def")); // add to list of files to clean up later.
                cleanupFiles.Add(Path.Combine(tempFolder, dll.Name+".exp")); // add to list of files to clean up later.
                if(!noCleanupLibs)
                    cleanupFiles.Add(Path.Combine(tempFolder, dll.Name+".lib")); // add to list of files to clean up later.

                if( lib.Exec("/out:{0} /def:{1} /machine:{2}", Path.Combine(tempFolder, dll.Name+".lib"), Path.Combine(tempFolder, dll.Name+".def"),arch == "x86"?arch:"x64") != 0)
                    throw new CoAppException(lib.StandardOut);

                commandLine = @"{0} /link ""{1}""".format(commandLine, Path.Combine(tempFolder, dll.Name + ".lib"));
            }

            // make C file
            var binaryText = binaryTemplate.Replace("[$LIBNAME]", binary.Name).Replace("[$LIBVERSION]", binary.Version).Replace("[$IMPORT_LIBRARY]", importLibrary.ToString()).Replace("[$CALL_LIBRARY]", callLibrary.ToString()).Replace("[$PUBLICKEYTOKEN]", publicKeyToken).Replace("[$ARCH]", arch);
            var tempCFile = Path.Combine(tempFolder, binary.Name+".c");
            File.WriteAllText(tempCFile, binaryText);
            cleanupFiles.Add(tempCFile); // add to list of files to clean up later.
            cleanupFiles.Add(Path.Combine(tempFolder, binary.Name+".obj")); // add to list of files to clean up later.
            cleanupFiles.Add(Path.Combine(tempFolder, binary.Name+".exp")); // add to list of files to clean up later.
            if(!noCleanupLibs)
                cleanupFiles.Add(Path.Combine(tempFolder, binary.Name+".lib")); // add to list of files to clean up later.

            // compile it
            var outputBinary = Path.Combine(outputBinaryFolder, binary.Name+extension);
            if(cl.Exec(commandLine, tempCFile, outputBinary)  != 0)
                throw new CoAppException(cl.StandardOut);

            // make manifest
            var manifestDependencies = new StringBuilder();
            var manifestFileName = Path.Combine(outputBinaryFolder, binary.Name + extension + ".manifest");

            foreach(var dll in binary.Dependencies) {
                manifestDependencies.Append(DependencyEntry.Replace("[$LIBNAME]", dll.Name).Replace("[$LIBVERSION]", dll.Version).Replace("[$PUBLICKEYTOKEN]",publicKeyToken).Replace("[$ARCH]",arch) );
            }
            var manifest = manifestTemplate.Replace("[$LIBNAME]", binary.Name).Replace("[$LIBVERSION]", binary.Version).Replace("[$DEPENDENCY]", manifestDependencies.ToString()).Replace("[$PUBLICKEYTOKEN]", publicKeyToken).Replace("[$ARCH]", arch);
            File.WriteAllText(manifestFileName,manifest);

            if( mt.Exec(@"-manifest ""{0}"" -outputresource:{1}", manifestFileName, outputBinary) !=0 )
                throw new CoAppException(mt.StandardOut);

            /*
            if(signTool.Exec(@"sign /v /t  http://timestamp.verisign.com/scripts/timestamp.dll /f ""{0}"" /p password ""{1}""", signingCertPath, outputBinary) != 0)
                throw new CoAppException(signTool.StandardOut);

            if( mt.Exec(@"-manifest ""{0}"" -hashupdate -makecdfs", manifestFileName) !=0 )
                throw new CoAppException(mt.StandardOut);

            cleanupFiles.Add(manifestFileName+".cdf"); // add to list of files to clean up later.

            if(0!= makecat.Exec(manifestFileName + ".cdf"))
                throw new CoAppException(makecat.StandardOut);

            if(signTool.Exec(@"sign /v /t  http://timestamp.verisign.com/scripts/timestamp.dll /f ""{0}"" /p password ""{1}.cat""", signingCertPath, outputBinary)!= 0)
                throw new CoAppException(signTool.StandardOut);

            foreach(Tuple<string,string> each in binary.Policies) {
                string PolicyLowerVersion = each.Item1;
                string PolicyHigherVersion = each.Item2;

                if (!string.IsNullOrEmpty(PolicyLowerVersion)) {
                    // make the publisher configuration file
                    var ver = PolicyLowerVersion.Substring(0, PolicyLowerVersion.LastIndexOf('.'));
                    ver = ver.Substring(0, ver.LastIndexOf('.'));
                    manifestFileName = Path.Combine(outputBinaryFolder,
                                                    "policy.{0}.{1}.manifest".format(ver, binary.Name));

                    manifest =
                        PublisherConfiguration.Replace("[$LIBNAME]", binary.Name).Replace("[$LIBVERSION]",
                                                                                          binary.Version).Replace(
                                                                                              "[$DEPENDENCY]",
                                                                                              manifestDependencies.
                                                                                                  ToString()).Replace(
                                                                                                      "[$PUBLICKEYTOKEN]",
                                                                                                      publicKeyToken).
                            Replace("[$ARCH]", arch).Replace("[$OLDVERSION]",
                                                             string.IsNullOrEmpty(PolicyHigherVersion)
                                                                 ? PolicyLowerVersion
                                                                 : PolicyLowerVersion + "-" +
                                                                   PolicyHigherVersion).Replace(
                                                                       "[$LIBMAJORMINOR]", ver);
                    File.WriteAllText(manifestFileName, manifest);
                    if( mt.Exec(@"-manifest ""{0}"" -hashupdate -makecdfs", manifestFileName) !=0 )
                        throw new CoAppException(mt.StandardOut);

                    cleanupFiles.Add(manifestFileName + ".cdf"); // add to list of files to clean up later.

                    if (0 !=
                        makecat.Exec(Path.Combine(outputBinaryFolder,
                                                  "policy.{0}.{1}.manifest.cdf".format(ver, binary.Name))))
                        throw new CoAppException(makecat.StandardOut);

                    if (
                        signTool.Exec(
                            @"sign /v /t  http://timestamp.verisign.com/scripts/timestamp.dll /f ""{0}"" /p password ""{1}""",
                            signingCertPath,
                            Path.Combine(outputBinaryFolder, "policy.{0}.{1}.cat".format(ver, binary.Name))) != 0)
                        throw new CoAppException(signTool.StandardOut);
                }
            }
            */

            // make autopackage
            var packageDependencies = new StringBuilder();
            var packageFileName = Path.Combine(outputBinaryFolder, binary.Name + extension + ".autopkg");
            foreach (var dll in binary.Dependencies) {
                packageDependencies.Append(PackageDependencyEntry.Replace("[$LIBNAME]", dll.Name).Replace("[$LIBVERSION]", dll.Version).Replace("[$PUBLICKEYTOKEN]", publicKeyToken).Replace("[$ARCH]", arch));
            }

            var policies = new StringBuilder();
            var policyFiles = new StringBuilder();
            ulong minPolicy = 0L;
            ulong maxPolicy = 0L;

            foreach (Tuple<string, string> each in binary.Policies) {
                string PolicyLowerVersion = each.Item1;
                string PolicyHigherVersion = each.Item2;
                if (string.IsNullOrEmpty(PolicyHigherVersion))
                    PolicyHigherVersion = PolicyLowerVersion;

                if (!string.IsNullOrEmpty(PolicyLowerVersion)) { // if there is a policy at all.
                    var mmVer = GetMajorMinorVersion(PolicyLowerVersion);
                    var majorVerStart = GetMajorVersion(PolicyLowerVersion);
                    var minorVerStart = GetMinorVersion(PolicyLowerVersion);

                    var majorVerEnd = GetMajorVersion(PolicyHigherVersion);
                    // var minorVerEnd= GetMinorVersion(PolicyHigherVersion);

                    if (PolicyLowerVersion.VersionStringToUInt64() < minPolicy || minPolicy == 0) {
                        minPolicy = PolicyLowerVersion.VersionStringToUInt64();
                    }

                    if (PolicyHigherVersion.VersionStringToUInt64() > maxPolicy) {
                        maxPolicy = PolicyHigherVersion.VersionStringToUInt64();
                    }

                    var minorVerEnd = 10;
                    // we're gonna create policy files for every major version between policy low and policy high, with minor versions 0 to 10
                    for (int major = majorVerStart; major <= majorVerEnd; major++) {
                        if (major == majorVerEnd) {
                            minorVerEnd = GetMinorVersion(PolicyHigherVersion);
                        }
                        for (int minor = minorVerStart; minor <= minorVerEnd; minor++) {
                            policies.Append(PackagePolicies.Replace("[$VER]", "{0}.{1}".format(major, minor)));
                            policyFiles.Append(PackagePolicyFiles.Replace("[$VER]", "{0}.{1}".format(major, minor)));
                        }
                        minorVerStart = 0;
                    }
                }
            }

            var package = packageTemplate.Replace("[$LIBNAME]", binary.Name).Replace("[$LIBVERSION]", binary.Version).Replace("[$DEPENDENCY]", packageDependencies.ToString()).Replace("[$PUBLICKEYTOKEN]", publicKeyToken).Replace("[$ARCH]", arch).Replace("[$POLICIES]", policies.ToString()).Replace("[$POLICY-FILES]", policyFiles.ToString()).Replace("[$CERTFILE]", signingCertPath);
            if (minPolicy != 0) {
                package = package.Replace("[$BINDING]", @"binding-min: ""{0}"";
            binding-max: ""{1}"";".format(minPolicy.UInt64VersiontoString(), maxPolicy.UInt64VersiontoString()));
            }
            else {
                package = package.Replace("[$BINDING]", "");
            }
            File.WriteAllText(packageFileName, package);

            // autoPackage.Exec("--load={0} --password=password", packageFileName);
            cmd.Exec( @"/c start ""autopackage"" /wait autopackage.exe --load={0} --password=password", packageFileName);
            Console.WriteLine(autoPackage.StandardOut);
            Console.WriteLine(autoPackage.StandardError);
            cmd.Exec("/c move *.msi ..");

            binary.Built = true;
        }
 public void CreateDLL(TestTarget dll)
 {
     CreateBinary(dll, SharedLibraryTemplate, ".dll", SharedLibraryManifest, @" ""{0}"" ""/Fe{1}"" /DISOLATION_AWARE_ENABLED=1 /LD /TC /Y- ", LibraryAutopackage);
 }
Ejemplo n.º 5
0
        public void CreateBinary(TestTarget binary,string binaryTemplate, string extension , string manifestTemplate ,  string commandLine )
        {
            if(binary.Built)
                return;

            foreach(var dep in binary.Dependencies) {
                CreateDLL(dep);
            }

            if(binary.Ignore)
                return;

            Console.WriteLine("Building Binary [{0}-{1}]", binary.Name, binary.Version);

            var importLibrary = new StringBuilder();
            var callLibrary = new StringBuilder();

            foreach(var dll in binary.Dependencies) {
                importLibrary.Append(LibraryReferenceTemplate.Replace("[$LIBNAME]", dll.Name).Replace("[$LIBVERSION]", dll.Version).Replace("[$PUBLICKEYTOKEN]", publicKeyToken).Replace("[$ARCH]", arch));
                callLibrary.Append(LibraryFunctionCallTemplate.Replace("[$LIBNAME]", dll.Name).Replace("[$LIBVERSION]", dll.Version).Replace("[$PUBLICKEYTOKEN]", publicKeyToken).Replace("[$ARCH]", arch));
            }

            var tempFolder = Path.Combine(Path.GetTempPath(), binary.Name + "-" + binary.Version);
            Directory.CreateDirectory(tempFolder);
            var outputBinaryFolder = Path.Combine(outputPath, binary.Name+"-"+binary.Version);
            Directory.CreateDirectory(outputBinaryFolder);
            Environment.CurrentDirectory = outputBinaryFolder;

            // create DEF files to create import .lib files
            foreach(var dll in binary.Dependencies) {
                var defFile = ModuleDefinitionFileTemplate + "print_" + dll.Name;
                File.WriteAllText(Path.Combine(tempFolder, dll.Name+".def"), defFile);
                cleanupFiles.Add(Path.Combine(tempFolder, dll.Name+".def")); // add to list of files to clean up later.
                cleanupFiles.Add(Path.Combine(tempFolder, dll.Name+".exp")); // add to list of files to clean up later.
                if(!noCleanupLibs)
                    cleanupFiles.Add(Path.Combine(tempFolder, dll.Name+".lib")); // add to list of files to clean up later.

                if( lib.Exec("/out:{0} /def:{1} /machine:{2}", Path.Combine(tempFolder, dll.Name+".lib"), Path.Combine(tempFolder, dll.Name+".def"),arch == "x86"?arch:"x64") != 0)
                    throw new Exception(lib.StandardOut);

                commandLine = @"{0} /link ""{1}""".format(commandLine, Path.Combine(tempFolder, dll.Name + ".lib"));
            }

            // make C file
            var binaryText = binaryTemplate.Replace("[$LIBNAME]", binary.Name).Replace("[$LIBVERSION]", binary.Version).Replace("[$IMPORT_LIBRARY]", importLibrary.ToString()).Replace("[$CALL_LIBRARY]", callLibrary.ToString()).Replace("[$PUBLICKEYTOKEN]", publicKeyToken).Replace("[$ARCH]", arch);
            var tempCFile = Path.Combine(tempFolder, binary.Name+".c");
            File.WriteAllText(tempCFile, binaryText);
            cleanupFiles.Add(tempCFile); // add to list of files to clean up later.
            cleanupFiles.Add(Path.Combine(tempFolder, binary.Name+".obj")); // add to list of files to clean up later.
            cleanupFiles.Add(Path.Combine(tempFolder, binary.Name+".exp")); // add to list of files to clean up later.
            if(!noCleanupLibs)
                cleanupFiles.Add(Path.Combine(tempFolder, binary.Name+".lib")); // add to list of files to clean up later.

            // compile it
            var outputBinary = Path.Combine(outputBinaryFolder, binary.Name+extension);
            if(cl.Exec(commandLine, tempCFile, outputBinary)  != 0)
                throw new Exception(cl.StandardOut);

            // make manifest

            var manifestDependencies = new StringBuilder();
            var manifestFileName = Path.Combine(outputBinaryFolder, binary.Name + extension + ".manifest");

            foreach(var dll in binary.Dependencies) {
                manifestDependencies.Append(DependencyEntry.Replace("[$LIBNAME]", dll.Name).Replace("[$LIBVERSION]", dll.Version).Replace("[$PUBLICKEYTOKEN]",publicKeyToken).Replace("[$ARCH]",arch) );
            }
            var manifest = manifestTemplate.Replace("[$LIBNAME]", binary.Name).Replace("[$LIBVERSION]", binary.Version).Replace("[$DEPENDENCY]", manifestDependencies.ToString()).Replace("[$PUBLICKEYTOKEN]", publicKeyToken).Replace("[$ARCH]", arch);
            File.WriteAllText(manifestFileName,manifest);

            if( mt.Exec(@"-manifest ""{0}"" -outputresource:{1}", manifestFileName, outputBinary) !=0 )
                throw new Exception(mt.StandardOut);

            if(signTool.Exec(@"sign /v /t  http://timestamp.verisign.com/scripts/timestamp.dll /f ""{0}"" /p password ""{1}""", signingCertPath, outputBinary) != 0)
                throw new Exception(signTool.StandardOut);

            if( mt.Exec(@"-manifest ""{0}"" -hashupdate -makecdfs", manifestFileName) !=0 )
                throw new Exception(mt.StandardOut);

            cleanupFiles.Add(manifestFileName+".cdf"); // add to list of files to clean up later.

            if(0!= makecat.Exec(manifestFileName + ".cdf"))
                throw new Exception(makecat.StandardOut);

            if(signTool.Exec(@"sign /v /t  http://timestamp.verisign.com/scripts/timestamp.dll /f ""{0}"" /p password ""{1}.cat""", signingCertPath, outputBinary)!= 0)
                throw new Exception(signTool.StandardOut);

            foreach(Tuple<string,string> each in binary.Policies) {
                string PolicyLowerVersion = each.Item1;
                string PolicyHigherVersion = each.Item2;

                if (!string.IsNullOrEmpty(PolicyLowerVersion)) {
                    // make the publisher configuration file
                    var ver = PolicyLowerVersion.Substring(0, PolicyLowerVersion.LastIndexOf('.'));
                    ver = ver.Substring(0, ver.LastIndexOf('.'));
                    manifestFileName = Path.Combine(outputBinaryFolder,
                                                    "policy.{0}.{1}.manifest".format(ver, binary.Name));

                    manifest =
                        PublisherConfiguration.Replace("[$LIBNAME]", binary.Name).Replace("[$LIBVERSION]",
                                                                                          binary.Version).Replace(
                                                                                              "[$DEPENDENCY]",
                                                                                              manifestDependencies.
                                                                                                  ToString()).Replace(
                                                                                                      "[$PUBLICKEYTOKEN]",
                                                                                                      publicKeyToken).
                            Replace("[$ARCH]", arch).Replace("[$OLDVERSION]",
                                                             string.IsNullOrEmpty(PolicyHigherVersion)
                                                                 ? PolicyLowerVersion
                                                                 : PolicyLowerVersion + "-" +
                                                                   PolicyHigherVersion).Replace(
                                                                       "[$LIBMAJORMINOR]", ver);
                    File.WriteAllText(manifestFileName, manifest);
                    if( mt.Exec(@"-manifest ""{0}"" -hashupdate -makecdfs", manifestFileName) !=0 )
                        throw new Exception(mt.StandardOut);

                    cleanupFiles.Add(manifestFileName + ".cdf"); // add to list of files to clean up later.

                    if (0 !=
                        makecat.Exec(Path.Combine(outputBinaryFolder,
                                                  "policy.{0}.{1}.manifest.cdf".format(ver, binary.Name))))
                        throw new Exception(makecat.StandardOut);

                    if (
                        signTool.Exec(
                            @"sign /v /t  http://timestamp.verisign.com/scripts/timestamp.dll /f ""{0}"" /p password ""{1}""",
                            signingCertPath,
                            Path.Combine(outputBinaryFolder, "policy.{0}.{1}.cat".format(ver, binary.Name))) != 0)
                        throw new Exception(signTool.StandardOut);
                }
            }

            binary.Built = true;
        }