summaryrefslogtreecommitdiffstats
path: root/Source
diff options
context:
space:
mode:
authorBrad King <brad.king@kitware.com>2011-11-16 01:39:36 (GMT)
committerBrad King <brad.king@kitware.com>2011-11-16 15:15:01 (GMT)
commit042f7965c3a5db7420363fdb76f9ebaa8e93efdc (patch)
treed6537eef3a37cbd65e3ea09da6df0b23cd192e9a /Source
parented7cef563445644684af47720c2f7c6fb0a2e440 (diff)
downloadCMake-042f7965c3a5db7420363fdb76f9ebaa8e93efdc.zip
CMake-042f7965c3a5db7420363fdb76f9ebaa8e93efdc.tar.gz
CMake-042f7965c3a5db7420363fdb76f9ebaa8e93efdc.tar.bz2
Add file(MD5) command to compute cryptographic hash
Provide a CMake-language binding to the md5sum function previously available only by "cmake -E md5sum".
Diffstat (limited to 'Source')
-rw-r--r--Source/cmFileCommand.cxx36
-rw-r--r--Source/cmFileCommand.h4
2 files changed, 40 insertions, 0 deletions
diff --git a/Source/cmFileCommand.cxx b/Source/cmFileCommand.cxx
index f933666..32454f5 100644
--- a/Source/cmFileCommand.cxx
+++ b/Source/cmFileCommand.cxx
@@ -13,6 +13,7 @@
#include "cmake.h"
#include "cmHexFileConverter.h"
#include "cmFileTimeComparison.h"
+#include "cmCryptoHash.h"
#if defined(CMAKE_BUILD_WITH_CMAKE)
#include "cm_curl.h"
@@ -22,6 +23,7 @@
#include <sys/types.h>
#include <sys/stat.h>
+#include <cmsys/auto_ptr.hxx>
#include <cmsys/Directory.hxx>
#include <cmsys/Glob.hxx>
#include <cmsys/RegularExpression.hxx>
@@ -83,6 +85,10 @@ bool cmFileCommand
{
return this->HandleReadCommand(args);
}
+ else if ( subCommand == "MD5" )
+ {
+ return this->HandleHashCommand(args);
+ }
else if ( subCommand == "STRINGS" )
{
return this->HandleStringsCommand(args);
@@ -339,6 +345,36 @@ bool cmFileCommand::HandleReadCommand(std::vector<std::string> const& args)
}
//----------------------------------------------------------------------------
+bool cmFileCommand::HandleHashCommand(std::vector<std::string> const& args)
+{
+ if(args.size() != 3)
+ {
+ cmOStringStream e;
+ e << args[0] << " requires a file name and output variable";
+ this->SetError(e.str().c_str());
+ return false;
+ }
+
+ cmsys::auto_ptr<cmCryptoHash> hash;
+ if(args[0] == "MD5")
+ { hash.reset(new cmCryptoHashMD5); }
+ if(hash.get())
+ {
+ std::string out = hash->HashFile(args[1].c_str());
+ if(!out.empty())
+ {
+ this->Makefile->AddDefinition(args[2].c_str(), out.c_str());
+ return true;
+ }
+ cmOStringStream e;
+ e << args[0] << " failed to read file \"" << args[1] << "\": "
+ << cmSystemTools::GetLastSystemError();
+ this->SetError(e.str().c_str());
+ }
+ return false;
+}
+
+//----------------------------------------------------------------------------
bool cmFileCommand::HandleStringsCommand(std::vector<std::string> const& args)
{
if(args.size() < 3)
diff --git a/Source/cmFileCommand.h b/Source/cmFileCommand.h
index 162890a..dce6478 100644
--- a/Source/cmFileCommand.h
+++ b/Source/cmFileCommand.h
@@ -65,6 +65,7 @@ public:
" file(WRITE filename \"message to write\"... )\n"
" file(APPEND filename \"message to write\"... )\n"
" file(READ filename variable [LIMIT numBytes] [OFFSET offset] [HEX])\n"
+ " file(MD5 filename variable)\n"
" file(STRINGS filename variable [LIMIT_COUNT num]\n"
" [LIMIT_INPUT numBytes] [LIMIT_OUTPUT numBytes]\n"
" [LENGTH_MINIMUM numBytes] [LENGTH_MAXIMUM numBytes]\n"
@@ -94,6 +95,8 @@ public:
"variable. It will start at the given offset and read up to numBytes. "
"If the argument HEX is given, the binary data will be converted to "
"hexadecimal representation and this will be stored in the variable.\n"
+ "MD5 "
+ "will compute a cryptographic hash of the content of a file.\n"
"STRINGS will parse a list of ASCII strings from a file and "
"store it in a variable. Binary data in the file are ignored. Carriage "
"return (CR) characters are ignored. It works also for Intel Hex and "
@@ -227,6 +230,7 @@ protected:
bool HandleRemove(std::vector<std::string> const& args, bool recurse);
bool HandleWriteCommand(std::vector<std::string> const& args, bool append);
bool HandleReadCommand(std::vector<std::string> const& args);
+ bool HandleHashCommand(std::vector<std::string> const& args);
bool HandleStringsCommand(std::vector<std::string> const& args);
bool HandleGlobCommand(std::vector<std::string> const& args, bool recurse);
bool HandleMakeDirectoryCommand(std::vector<std::string> const& args);