Browse Source

cmake: Teach "-E tar" command a "--format=" option

Allows specifying a libarchive defined archive format currently restricted to
7zip, gnutar, pax, paxr and zip.

The default is "paxr" (pax restricted).
Nils Gladitz 10 years ago
parent
commit
d2cc580704

+ 4 - 0
Help/manual/cmake.1.rst

@@ -215,6 +215,10 @@ Available commands are:
     names start in ``-``.
   ``--mtime=<date>``
     Specify modification time recorded in tarball entries.
+  ``--format=<format>``
+    Specify the format of the archive to be created.
+    Supported formats are: ``7zip``, ``gnutar``, ``pax``,
+    ``paxr`` (restricted pax, default), and ``zip``.
 
 ``time <command> [<args>...]``
   Run command and return elapsed time.

+ 6 - 0
Help/release/dev/tar-write-format.rst

@@ -0,0 +1,6 @@
+tar-write-format
+----------------
+
+* The :manual:`cmake(1)` ``-E tar`` command learned a new
+  ``--format<format>`` option to specify the archive format to
+  be written.

+ 1 - 1
Source/CPack/cmCPack7zGenerator.cxx

@@ -15,7 +15,7 @@
 //----------------------------------------------------------------------
 cmCPack7zGenerator::cmCPack7zGenerator()
   :cmCPackArchiveGenerator(cmArchiveWrite::CompressNone,
-                           cmArchiveWrite::Type7Zip)
+                           "7zip")
 {
 }
 

+ 3 - 3
Source/CPack/cmCPackArchiveGenerator.cxx

@@ -27,10 +27,10 @@
 
 //----------------------------------------------------------------------
 cmCPackArchiveGenerator::cmCPackArchiveGenerator(cmArchiveWrite::Compress t,
-  cmArchiveWrite::Type at)
+  std::string const& format)
 {
   this->Compress = t;
-  this->Archive = at;
+  this->ArchiveFormat = format;
 }
 
 //----------------------------------------------------------------------
@@ -108,7 +108,7 @@ if (!GenerateHeader(&gf)) \
             << ">." << std::endl); \
     return 0; \
   } \
-cmArchiveWrite archive(gf,this->Compress, this->Archive); \
+cmArchiveWrite archive(gf,this->Compress, this->ArchiveFormat); \
 if (!archive) \
   { \
   cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem to create archive < " \

+ 2 - 2
Source/CPack/cmCPackArchiveGenerator.h

@@ -31,7 +31,7 @@ public:
   /**
    * Construct generator
    */
-  cmCPackArchiveGenerator(cmArchiveWrite::Compress, cmArchiveWrite::Type);
+  cmCPackArchiveGenerator(cmArchiveWrite::Compress, std::string const& format);
   virtual ~cmCPackArchiveGenerator();
   // Used to add a header to the archive
   virtual int GenerateHeader(std::ostream* os);
@@ -68,7 +68,7 @@ protected:
   int PackageComponentsAllInOne();
   virtual const char* GetOutputExtension() = 0;
   cmArchiveWrite::Compress Compress;
-  cmArchiveWrite::Type Archive;
+  std::string ArchiveFormat;
   };
 
 #endif

+ 1 - 1
Source/CPack/cmCPackTGZGenerator.cxx

@@ -15,7 +15,7 @@
 //----------------------------------------------------------------------
 cmCPackTGZGenerator::cmCPackTGZGenerator()
   :cmCPackArchiveGenerator(cmArchiveWrite::CompressGZip,
-                           cmArchiveWrite::TypeTAR)
+                           "paxr")
 {
 }
 

+ 1 - 1
Source/CPack/cmCPackTXZGenerator.cxx

@@ -15,7 +15,7 @@
 //----------------------------------------------------------------------
 cmCPackTXZGenerator::cmCPackTXZGenerator()
   :cmCPackArchiveGenerator(cmArchiveWrite::CompressXZ,
-                           cmArchiveWrite::TypeTAR)
+                           "paxr")
 {
 }
 

+ 1 - 1
Source/CPack/cmCPackTarBZip2Generator.cxx

@@ -14,7 +14,7 @@
 //----------------------------------------------------------------------
 cmCPackTarBZip2Generator::cmCPackTarBZip2Generator()
  :cmCPackArchiveGenerator(cmArchiveWrite::CompressBZip2,
-                          cmArchiveWrite::TypeTAR)
+                          "paxr")
 {
 }
 

+ 1 - 1
Source/CPack/cmCPackTarCompressGenerator.cxx

@@ -15,7 +15,7 @@
 //----------------------------------------------------------------------
 cmCPackTarCompressGenerator::cmCPackTarCompressGenerator()
   :cmCPackArchiveGenerator(cmArchiveWrite::CompressCompress,
-                           cmArchiveWrite::TypeTAR)
+                           "paxr")
 {
 }
 

+ 1 - 1
Source/CPack/cmCPackZIPGenerator.cxx

@@ -15,7 +15,7 @@
 //----------------------------------------------------------------------
 cmCPackZIPGenerator::cmCPackZIPGenerator()
   :cmCPackArchiveGenerator(cmArchiveWrite::CompressNone,
-                           cmArchiveWrite::TypeZIP)
+                           "zip")
 {
 }
 

+ 13 - 31
Source/cmArchiveWrite.cxx

@@ -79,11 +79,12 @@ struct cmArchiveWrite::Callback
 };
 
 //----------------------------------------------------------------------------
-cmArchiveWrite::cmArchiveWrite(std::ostream& os, Compress c, Type t):
-  Stream(os),
-  Archive(archive_write_new()),
-  Disk(archive_read_disk_new()),
-  Verbose(false)
+cmArchiveWrite::cmArchiveWrite(
+  std::ostream& os, Compress c, std::string const& format):
+    Stream(os),
+    Archive(archive_write_new()),
+    Disk(archive_read_disk_new()),
+    Verbose(false)
 {
   switch (c)
     {
@@ -141,35 +142,16 @@ cmArchiveWrite::cmArchiveWrite(std::ostream& os, Compress c, Type t):
     {
     this->Error = "archive_read_disk_set_standard_lookup: ";
     this->Error += cm_archive_error_string(this->Archive);
-    return;;
+    return;
     }
 #endif
-  switch (t)
+
+  if(archive_write_set_format_by_name(this->Archive, format.c_str())
+    != ARCHIVE_OK)
     {
-    case TypeZIP:
-      if(archive_write_set_format_zip(this->Archive) != ARCHIVE_OK)
-        {
-        this->Error = "archive_write_set_format_zip: ";
-        this->Error += cm_archive_error_string(this->Archive);
-        return;
-        }
-      break;
-    case TypeTAR:
-      if(archive_write_set_format_pax_restricted(this->Archive) != ARCHIVE_OK)
-        {
-        this->Error = "archive_write_set_format_pax_restricted: ";
-        this->Error += cm_archive_error_string(this->Archive);
-        return;
-        }
-      break;
-    case Type7Zip:
-      if(archive_write_set_format_7zip(this->Archive) != ARCHIVE_OK)
-        {
-        this->Error = "archive_write_set_format_7zip: ";
-        this->Error += cm_archive_error_string(this->Archive);
-        return;
-        }
-      break;
+    this->Error = "archive_write_set_format_by_name: ";
+    this->Error += cm_archive_error_string(this->Archive);
+    return;
     }
 
   // do not pad the last block!!

+ 3 - 9
Source/cmArchiveWrite.h

@@ -38,16 +38,10 @@ public:
     CompressXZ
   };
 
-  /** Archive Type */
-  enum Type
-  {
-    TypeTAR,
-    TypeZIP,
-    Type7Zip
-  };
-
   /** Construct with output stream to which to write archive.  */
-  cmArchiveWrite(std::ostream& os, Compress c = CompressNone, Type = TypeTAR);
+  cmArchiveWrite(std::ostream& os, Compress c = CompressNone,
+    std::string const& format = "paxr");
+
   ~cmArchiveWrite();
 
   /**

+ 5 - 2
Source/cmSystemTools.cxx

@@ -1475,7 +1475,8 @@ bool cmSystemTools::IsPathToFramework(const char* path)
 bool cmSystemTools::CreateTar(const char* outFileName,
                               const std::vector<std::string>& files,
                               cmTarCompression compressType,
-                              bool verbose, std::string const& mtime)
+                              bool verbose, std::string const& mtime,
+                              std::string const& format)
 {
 #if defined(CMAKE_BUILD_WITH_CMAKE)
   std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
@@ -1505,8 +1506,10 @@ bool cmSystemTools::CreateTar(const char* outFileName,
       compress = cmArchiveWrite::CompressNone;
       break;
     }
+
   cmArchiveWrite a(fout, compress,
-                   cmArchiveWrite::TypeTAR);
+    format.empty() ? "paxr" : format);
+
   a.SetMTime(mtime);
   a.SetVerbose(verbose);
   for(std::vector<std::string>::const_iterator i = files.begin();

+ 2 - 1
Source/cmSystemTools.h

@@ -395,7 +395,8 @@ public:
   static bool CreateTar(const char* outFileName,
                         const std::vector<std::string>& files,
                         cmTarCompression compressType, bool verbose,
-                        std::string const& mtime = std::string());
+                        std::string const& mtime = std::string(),
+                        std::string const& format = std::string());
   static bool ExtractTar(const char* inFileName, bool verbose);
   // This should be called first thing in main
   // it will keep child processes from inheriting the

+ 31 - 2
Source/cmcmd.cxx

@@ -703,10 +703,20 @@ int cmcmd::ExecuteCMakeCommand(std::vector<std::string>& args)
     // Tar files
     else if (args[1] == "tar" && args.size() > 3)
       {
+      const char* knownFormats[] =
+        {
+        "7zip",
+        "gnutar",
+        "pax",
+        "paxr",
+        "zip"
+        };
+
       std::string flags = args[2];
       std::string outFile = args[3];
       std::vector<std::string> files;
       std::string mtime;
+      std::string format;
       bool doing_options = true;
       for (std::string::size_type cc = 4; cc < args.size(); cc ++)
         {
@@ -729,6 +739,19 @@ int cmcmd::ExecuteCMakeCommand(std::vector<std::string>& args)
               return 1;
               }
             }
+          else if (cmHasLiteralPrefix(arg, "--format="))
+            {
+            format = arg.substr(9);
+            bool isKnown = std::find(cmArrayBegin(knownFormats),
+              cmArrayEnd(knownFormats), format) != cmArrayEnd(knownFormats);
+
+            if(!isKnown)
+              {
+              cmSystemTools::Error("Unknown -E tar --format= argument: ",
+                format.c_str());
+              return 1;
+              }
+            }
           else
             {
             cmSystemTools::Error("Unknown option to -E tar: ", arg.c_str());
@@ -759,7 +782,13 @@ int cmcmd::ExecuteCMakeCommand(std::vector<std::string>& args)
         compress = cmSystemTools::TarCompressGZip;
         ++nCompress;
         }
-      if ( nCompress > 1 )
+      if ( (format == "7zip" || format == "zip") && nCompress > 0 )
+        {
+        cmSystemTools::Error("Can not use compression flags with format: ",
+          format.c_str());
+        return 1;
+        }
+      else if ( nCompress > 1 )
         {
         cmSystemTools::Error("Can only compress a tar file one way; "
                              "at most one flag of z, j, or J may be used");
@@ -781,7 +810,7 @@ int cmcmd::ExecuteCMakeCommand(std::vector<std::string>& args)
       else if ( flags.find_first_of('c') != flags.npos )
         {
         if ( !cmSystemTools::CreateTar(
-               outFile.c_str(), files, compress, verbose, mtime) )
+               outFile.c_str(), files, compress, verbose, mtime, format) )
           {
           cmSystemTools::Error("Problem creating tar: ", outFile.c_str());
           return 1;

+ 1 - 0
Tests/RunCMake/CommandLine/E_tar-bad-format-result.txt

@@ -0,0 +1 @@
+1

+ 1 - 0
Tests/RunCMake/CommandLine/E_tar-bad-format-stderr.txt

@@ -0,0 +1 @@
+CMake Error: Unknown -E tar --format= argument: bad-format

+ 1 - 0
Tests/RunCMake/CommandLine/RunCMakeTest.cmake

@@ -10,6 +10,7 @@ run_cmake_command(E_tar-bad-from5  ${CMAKE_COMMAND} -E tar cvf bad.tar --files-f
 run_cmake_command(E_tar-end-opt1   ${CMAKE_COMMAND} -E tar cvf bad.tar -- --bad)
 run_cmake_command(E_tar-end-opt2   ${CMAKE_COMMAND} -E tar cvf bad.tar --)
 run_cmake_command(E_tar-mtime      ${CMAKE_COMMAND} -E tar cvf bad.tar "--mtime=1970-01-01 00:00:00 UTC")
+run_cmake_command(E_tar-bad-format ${CMAKE_COMMAND} -E tar cvf bad.tar "--format=bad-format")
 
 run_cmake_command(build-no-cache
   ${CMAKE_COMMAND} --build ${RunCMake_SOURCE_DIR})