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).
This commit is contained in:
Nils Gladitz 2015-04-07 12:36:52 +02:00 committed by Brad King
parent 1264c5b2c4
commit d2cc580704
18 changed files with 78 additions and 56 deletions

View File

@ -215,6 +215,10 @@ Available commands are:
names start in ``-``. names start in ``-``.
``--mtime=<date>`` ``--mtime=<date>``
Specify modification time recorded in tarball entries. 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>...]`` ``time <command> [<args>...]``
Run command and return elapsed time. Run command and return elapsed time.

View File

@ -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.

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -38,16 +38,10 @@ public:
CompressXZ CompressXZ
}; };
/** Archive Type */
enum Type
{
TypeTAR,
TypeZIP,
Type7Zip
};
/** Construct with output stream to which to write archive. */ /** 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(); ~cmArchiveWrite();
/** /**

View File

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

View File

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

View File

@ -703,10 +703,20 @@ int cmcmd::ExecuteCMakeCommand(std::vector<std::string>& args)
// Tar files // Tar files
else if (args[1] == "tar" && args.size() > 3) else if (args[1] == "tar" && args.size() > 3)
{ {
const char* knownFormats[] =
{
"7zip",
"gnutar",
"pax",
"paxr",
"zip"
};
std::string flags = args[2]; std::string flags = args[2];
std::string outFile = args[3]; std::string outFile = args[3];
std::vector<std::string> files; std::vector<std::string> files;
std::string mtime; std::string mtime;
std::string format;
bool doing_options = true; bool doing_options = true;
for (std::string::size_type cc = 4; cc < args.size(); cc ++) 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; 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 else
{ {
cmSystemTools::Error("Unknown option to -E tar: ", arg.c_str()); 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; compress = cmSystemTools::TarCompressGZip;
++nCompress; ++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; " cmSystemTools::Error("Can only compress a tar file one way; "
"at most one flag of z, j, or J may be used"); "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 ) else if ( flags.find_first_of('c') != flags.npos )
{ {
if ( !cmSystemTools::CreateTar( 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()); cmSystemTools::Error("Problem creating tar: ", outFile.c_str());
return 1; return 1;

View File

@ -0,0 +1 @@
1

View File

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

View File

@ -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-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-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-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 run_cmake_command(build-no-cache
${CMAKE_COMMAND} --build ${RunCMake_SOURCE_DIR}) ${CMAKE_COMMAND} --build ${RunCMake_SOURCE_DIR})