Add file(MD5) command to compute cryptographic hash

Provide a CMake-language binding to the md5sum function previously
available only by "cmake -E md5sum".
This commit is contained in:
Brad King 2011-11-15 20:39:36 -05:00
parent ed7cef5634
commit 042f7965c3
9 changed files with 62 additions and 1 deletions

View File

@ -13,6 +13,7 @@
#include "cmake.h" #include "cmake.h"
#include "cmHexFileConverter.h" #include "cmHexFileConverter.h"
#include "cmFileTimeComparison.h" #include "cmFileTimeComparison.h"
#include "cmCryptoHash.h"
#if defined(CMAKE_BUILD_WITH_CMAKE) #if defined(CMAKE_BUILD_WITH_CMAKE)
#include "cm_curl.h" #include "cm_curl.h"
@ -22,6 +23,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <cmsys/auto_ptr.hxx>
#include <cmsys/Directory.hxx> #include <cmsys/Directory.hxx>
#include <cmsys/Glob.hxx> #include <cmsys/Glob.hxx>
#include <cmsys/RegularExpression.hxx> #include <cmsys/RegularExpression.hxx>
@ -83,6 +85,10 @@ bool cmFileCommand
{ {
return this->HandleReadCommand(args); return this->HandleReadCommand(args);
} }
else if ( subCommand == "MD5" )
{
return this->HandleHashCommand(args);
}
else if ( subCommand == "STRINGS" ) else if ( subCommand == "STRINGS" )
{ {
return this->HandleStringsCommand(args); return this->HandleStringsCommand(args);
@ -338,6 +344,36 @@ bool cmFileCommand::HandleReadCommand(std::vector<std::string> const& args)
return true; return true;
} }
//----------------------------------------------------------------------------
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) bool cmFileCommand::HandleStringsCommand(std::vector<std::string> const& args)
{ {

View File

@ -65,6 +65,7 @@ public:
" file(WRITE filename \"message to write\"... )\n" " file(WRITE filename \"message to write\"... )\n"
" file(APPEND filename \"message to write\"... )\n" " file(APPEND filename \"message to write\"... )\n"
" file(READ filename variable [LIMIT numBytes] [OFFSET offset] [HEX])\n" " file(READ filename variable [LIMIT numBytes] [OFFSET offset] [HEX])\n"
" file(MD5 filename variable)\n"
" file(STRINGS filename variable [LIMIT_COUNT num]\n" " file(STRINGS filename variable [LIMIT_COUNT num]\n"
" [LIMIT_INPUT numBytes] [LIMIT_OUTPUT numBytes]\n" " [LIMIT_INPUT numBytes] [LIMIT_OUTPUT numBytes]\n"
" [LENGTH_MINIMUM numBytes] [LENGTH_MAXIMUM 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. " "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 " "If the argument HEX is given, the binary data will be converted to "
"hexadecimal representation and this will be stored in the variable.\n" "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 " "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 " "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 " "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 HandleRemove(std::vector<std::string> const& args, bool recurse);
bool HandleWriteCommand(std::vector<std::string> const& args, bool append); bool HandleWriteCommand(std::vector<std::string> const& args, bool append);
bool HandleReadCommand(std::vector<std::string> const& args); 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 HandleStringsCommand(std::vector<std::string> const& args);
bool HandleGlobCommand(std::vector<std::string> const& args, bool recurse); bool HandleGlobCommand(std::vector<std::string> const& args, bool recurse);
bool HandleMakeDirectoryCommand(std::vector<std::string> const& args); bool HandleMakeDirectoryCommand(std::vector<std::string> const& args);

View File

@ -12,7 +12,7 @@ function(check_cmake_test prefix)
) )
string(REGEX REPLACE "\n" "\n out> " out " out> ${stdout}") string(REGEX REPLACE "\n" "\n out> " out " out> ${stdout}")
string(REGEX REPLACE "\n" "\n err> " err " err> ${stderr}") string(REGEX REPLACE "\n" "\n err> " err " err> ${stderr}")
if(NOT "${result}" STREQUAL ${${test}-RESULT}) if(NOT "${result}" STREQUAL "${${test}-RESULT}")
message(FATAL_ERROR message(FATAL_ERROR
"Test ${test} result is [${result}], not [${${test}-RESULT}].\n" "Test ${test} result is [${result}], not [${${test}-RESULT}].\n"
"Test ${test} output:\n" "Test ${test} output:\n"

View File

@ -0,0 +1 @@
file(MD5)

View File

@ -0,0 +1 @@
file(MD5 ${CMAKE_CURRENT_LIST_DIR}/File-Copy-NoDest.cmake)

View File

@ -0,0 +1 @@
file(MD5 ${CMAKE_CURRENT_LIST_DIR}/File-Copy-NoDest.cmake md5 extra_arg)

View File

@ -0,0 +1 @@
file(MD5 ${CMAKE_CURRENT_LIST_DIR}/DoesNotExist.cmake md5)

View File

@ -0,0 +1,2 @@
file(MD5 ${CMAKE_CURRENT_LIST_DIR}/File-Copy-NoDest.cmake md5)
message("${md5}")

View File

@ -12,6 +12,16 @@ set(Copy-NoDest-RESULT 1)
set(Copy-NoDest-STDERR "given no DESTINATION") set(Copy-NoDest-STDERR "given no DESTINATION")
set(Copy-NoFile-RESULT 1) set(Copy-NoFile-RESULT 1)
set(Copy-NoFile-STDERR "COPY cannot find.*/does_not_exist\\.txt") set(Copy-NoFile-STDERR "COPY cannot find.*/does_not_exist\\.txt")
set(MD5-NoFile-RESULT 1)
set(MD5-NoFile-STDERR "file MD5 failed to read file")
set(MD5-BadArg1-RESULT 1)
set(MD5-BadArg1-STDERR "file must be called with at least two arguments")
set(MD5-BadArg2-RESULT 1)
set(MD5-BadArg2-STDERR "file MD5 requires a file name and output variable")
set(MD5-BadArg4-RESULT 1)
set(MD5-BadArg4-STDERR "file MD5 requires a file name and output variable")
set(MD5-Works-RESULT 0)
set(MD5-Works-STDERR "a28a44fb5b58a6acf0cbec46557bc775")
include("@CMAKE_CURRENT_SOURCE_DIR@/CheckCMakeTest.cmake") include("@CMAKE_CURRENT_SOURCE_DIR@/CheckCMakeTest.cmake")
check_cmake_test(File check_cmake_test(File
@ -22,6 +32,11 @@ check_cmake_test(File
Copy-LateArg Copy-LateArg
Copy-NoDest Copy-NoDest
Copy-NoFile Copy-NoFile
MD5-NoFile
MD5-BadArg1
MD5-BadArg2
MD5-BadArg4
MD5-Works
) )
# Also execute each test listed in FileTestScript.cmake: # Also execute each test listed in FileTestScript.cmake: