Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add version info to all executable apps #926

Merged
merged 2 commits into from
Feb 18, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
retdec: add version info to all executable apps
  • Loading branch information
PeterMatula committed Feb 13, 2021
commit 5d5b6babe9afbcb6de77509af2afe42c041d6500
16 changes: 12 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ project(retdec
VERSION 4.0
)

include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/GetGitRevisionDescription.cmake)
get_git_head_revision(
RETDEC_GIT_REFSPEC
RETDEC_GIT_COMMIT_HASH
)
git_describe(
RETDEC_GIT_VERSION_TAG
"--tags"
)
string(TIMESTAMP RETDEC_BUILD_DATE UTC)

set(CMAKE_POSITION_INDEPENDENT_CODE ON)

set(CMAKE_CXX_STANDARD 17)
Expand Down Expand Up @@ -188,12 +199,9 @@ install(
)

# Install commit id file.
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/GetGitRevisionDescription.cmake)
get_git_head_revision(GIT_REFSPEC GIT_COMMIT)
string(TIMESTAMP DATE UTC)
file(WRITE
"${CMAKE_CURRENT_BINARY_DIR}/BUILD-ID"
"This build was generated from commit ${GIT_COMMIT} on ${DATE} (UTC)."
"RetDec ${RETDEC_GIT_VERSION_TAG} built from commit ${RETDEC_GIT_COMMIT_HASH} on ${RETDEC_BUILD_DATE} (UTC).\n"
)
install(
FILES
Expand Down
55 changes: 55 additions & 0 deletions include/retdec/utils/version.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* @file include/retdec/utils/version.h
* @brief RetDec version header.
* @copyright (c) 2021 Avast Software, licensed under the MIT license
*/

#ifndef RETDEC_UTILS_VERSION_H
#define RETDEC_UTILS_VERSION_H

#include <string>

namespace retdec {
namespace utils {
namespace version {

/**
* \return The full current Git commit hash.
*/
std::string getCommitHash();

/**
* \return First \a length character of the current Git commit hash.
*/
std::string getShortCommitHash(unsigned length = 8);

/**
* \return Build date.
*/
std::string getBuildDate();

/**
* \return The Git version tag.
* E.g. "v4.0" if the current commit is associated with an exact tag,
* or e.g. "v4.0-294-g21baf36d" if the commit is on top of an tag.
*/
std::string getVersionTag();

/**
* \return Full version string containing all the necessary parts.
* Can be used in implementation of \c --version application option etc.
* \note This can return multiline string, use \c getVersionStringShort() if you
* want only one line.
*/
std::string getVersionStringLong();

/**
* \return Shorter one-line version of \c getVersionStringLong().
*/
std::string getVersionStringShort();

} // namespace version
} // namespace utils
} // namespace retdec

#endif // RETDEC_UTILS_VERSION_H
9 changes: 9 additions & 0 deletions src/ar-extractortool/ar_extractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "retdec/utils/filesystem.h"
#include "retdec/utils/io/log.h"
#include "retdec/utils/version.h"
#include "retdec/ar-extractor/archive_wrapper.h"
#include "retdec/ar-extractor/detection.h"

Expand Down Expand Up @@ -51,6 +52,10 @@ void printUsage(Logger& log)

log << "Usage: ar_extractor [OPTIONS] FILE\n\n"
"Options:\n\n"
"-h --help\n"
" Show this help.\n\n"
"--version\n"
" Show RetDec version.\n\n"
"--arch-magic\n"
" Check if file starts with archive magic constants.\n"
" Exit code = 0 if archive magic, 1 otherwise.\n\n"
Expand Down Expand Up @@ -216,6 +221,10 @@ int processArguments(
printUsage(Log::get(Log::Type::Info));
return 0;
}
else if (arg == "--version") {
Log::info() << version::getVersionStringLong() << "\n";
return 0;
}
else if (arg == "-o" || arg == "--output") {
if (!getArgFromArgs(++i, args, outPath)) {
return printErrorAndReturn("No -o/--output value");
Expand Down
14 changes: 12 additions & 2 deletions src/bin2pat/bin2pat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "retdec/utils/filesystem.h"
#include "retdec/utils/io/log.h"
#include "retdec/utils/version.h"
#include "retdec/patterngen/pattern_extractor/pattern_extractor.h"
#include "yaramod/yaramod.h"

Expand All @@ -21,13 +22,18 @@
* Output is set of yara rules (http://yara.readthedocs.io/en/v3.5.0/).
*/

using namespace retdec::utils;
using namespace retdec::utils::io;
using namespace retdec::patterngen;

void printUsage(Logger &log)
{
log << "Usage: bin2pat [-o OUTPUT_FILE] [-n NOTE]"
<< " <INPUT_FILE [INPUT_FILE...] | -l LIST_FILE>\n\n"
<< "-h --help\n"
<< " Show this help.\n\n"
<< "--version\n"
<< " Show RetDec version.\n\n "
<< "-o --output OUTPUT_FILE\n"
<< " Output file path (if not given, stdout is used).\n"
<< " If multiple paths are given, only last one is used.\n\n"
Expand All @@ -53,8 +59,7 @@ void needValue(
printErrorAndDie("argument " + arg + " requires value");
}

void processArgs(
const std::vector<std::string> &args)
void processArgs(const std::vector<std::string> &args)
{
std::string note;
std::string outPath;
Expand All @@ -65,6 +70,11 @@ void processArgs(
printUsage(Log::get(Log::Type::Info));
return;
}
else if (args[i] == "--version")
{
Log::info() << version::getVersionStringLong() << "\n";
return;
}
else if (args[i] == "-o" || args[i] == "--output") {
if (i + 1 < e) {
outPath = args[++i];
Expand Down
11 changes: 10 additions & 1 deletion src/capstone2llvmirtool/capstone2llvmir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
#include "retdec/utils/conversion.h"
#include "retdec/utils/string.h"
#include "retdec/utils/io/log.h"
#include "retdec/utils/version.h"

#include "retdec/capstone2llvmir/capstone2llvmir.h"

using namespace retdec::utils;
using namespace retdec::utils::io;

// byte ptr [0x12345678], 0x11
Expand All @@ -41,7 +43,12 @@ class ProgramOptions
{
std::string c = argv[i];

if (c == "-a")
if (c == "--version")
{
Log::info() << version::getVersionStringLong() << "\n";
exit(0);
}
else if (c == "-a")
{
_arch = getParamOrDie(argc, argv, i);
if (_arch == "arm") arch = CS_ARCH_ARM;
Expand Down Expand Up @@ -171,6 +178,8 @@ class ProgramOptions
std::string tmp;
retdec::utils::bytesToHexString(CODE, tmp, 0, 0, false, true);
Log::info() << _programName << ":\n"
"\t-h|--help Show this help.\n"
"\t--version Show RetDec version.\n"
"\t-a name Set architecture name.\n"
"\t Possible values: arm, arm64, mips, x86, ppc, sparc, sysz, xcore\n"
"\t Default value: x86.\n"
Expand Down
16 changes: 13 additions & 3 deletions src/demanglertool/demangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
#include "retdec/demangler/demangler.h"

#include "retdec/utils/io/log.h"
#include "retdec/utils/version.h"

using namespace std::string_literals;
using namespace retdec::utils;
using namespace retdec::utils::io;

using ItaniumDemangler = retdec::demangler::ItaniumDemangler;
Expand All @@ -22,8 +25,9 @@ using BorlandDemangler = retdec::demangler::BorlandDemangler;
*/
const std::string helpmsg =
"Usage:\n"
"\t'retdec-demangler [-h, --help] | Show this help.\n"
"\t'retdec-demangler <mangledname> | Attempt to demangle <mangledname> using all available demanglers and print result if succeded.\n";
"\tretdec-demangler [-h, --help] | Show this help.\n"
"\tretdec-demangler --version | Show RetDec version.\n"
"\tretdec-demangler <mangledname> | Attempt to demangle <mangledname> using all available demanglers and print result if succeded.\n";

/**
* @brief Main function of the Demangler tool.
Expand All @@ -38,11 +42,17 @@ int main(int argc, char *argv[])
std::string demangledMs;
std::string demangledBorland;

if (argc <= 1 || strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) {
if (argc <= 1 || "-h"s == argv[1] || "--help"s == argv[1]) {
Log::info() << helpmsg;
return 0;
}

if ("--version"s == argv[1])
{
Log::info() << version::getVersionStringLong() << std::endl;
return 0;
}

//process all mangled arguments
for (unsigned int i = 1; i < static_cast<unsigned int>(argc); i++) {
//demangle using all available demanglers
Expand Down
7 changes: 7 additions & 0 deletions src/fileinfo/fileinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "retdec/utils/memory.h"
#include "retdec/utils/io/log.h"
#include "retdec/utils/string.h"
#include "retdec/utils/version.h"
#include "retdec/ar-extractor/detection.h"
#include "retdec/cpdetect/errors.h"
#include "retdec/cpdetect/settings.h"
Expand Down Expand Up @@ -155,6 +156,7 @@ void printHelp()
<< "Usage: fileinfo [options] file\n\n"
<< "Options list:\n"
<< " --help, -h Display this help.\n"
<< " --version Display program's version.\n"
<< "\n"
<< "Options specifying type of YARA patterns matching for detection of used compiler\n"
<< "or packer:\n"
Expand Down Expand Up @@ -561,6 +563,11 @@ bool doParams(int argc, char **_argv, ProgParams &params)
printHelp();
exit(EXIT_SUCCESS);
}
else if (c == "--version")
{
Log::info() << version::getVersionStringLong() << "\n";
exit(EXIT_SUCCESS);
}
else if (c == "-x" || c == "--exact")
{
params.searchMode = SearchType::EXACT_MATCH;
Expand Down
15 changes: 11 additions & 4 deletions src/getsig/getsig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "retdec/utils/conversion.h"
#include "retdec/utils/string.h"
#include "retdec/utils/io/log.h"
#include "retdec/utils/version.h"

using namespace retdec::fileformat;
using namespace retdec::utils;
Expand All @@ -27,13 +28,14 @@ void printUsage()
"offset and prints signature representing contents of all files.\n\n"
"Usage: getsig [OPTIONS] FILE1 [FILE2 ...]\n\n"
"General:\n"
" -h --help Print this message.\n\n"
" -h --help Show this help.\n\n"
" --version Show RetDec version.\n\n"
"Rule options:\n"
" -r --rule-name NAME\n"
" Set name of rule. Default value is 'unknown'.\n\n"
" -n --name NAME\n"
" Set name of tool. Default value is 'unknown'.\n\n"
" -v --version VERSION\n"
" -v --tool-version VERSION\n"
" Set version of tool. Attribute is omitted if not specified.\n\n"
" -e --extra INFO\n"
" Set extra information. Attribute is omitted if not specified.\n\n"
Expand Down Expand Up @@ -154,7 +156,7 @@ bool doParams(
{
"r", "rule-name",
"n", "name",
"v", "version",
"v", "tool-version",
"e", "extra",
"l", "language",
"s", "size",
Expand Down Expand Up @@ -197,6 +199,11 @@ bool doParams(
printUsage();
exit(EXIT_SUCCESS);
}
else if (c == "--version")
{
Log::info() << version::getVersionStringLong() << "\n";
exit(EXIT_SUCCESS);
}
else if (c == "-b" || c == "--bytecode")
{
options.bytecode = true;
Expand Down Expand Up @@ -236,7 +243,7 @@ bool doParams(
{
options.name = getParamOrDie(argv, i);
}
else if (c == "-v" || c == "--version")
else if (c == "-v" || c == "--tool-version")
{
options.version = getParamOrDie(argv, i);
}
Expand Down
7 changes: 7 additions & 0 deletions src/idr2pat/idr2pat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@

#include "retdec/utils/conversion.h"
#include "retdec/utils/io/log.h"
#include "retdec/utils/version.h"
#include "yaramod/builder/yara_expression_builder.h"
#include "yaramod/builder/yara_hex_string_builder.h"
#include "yaramod/builder/yara_rule_builder.h"

using namespace std::string_literals;
using namespace retdec::utils;
using namespace retdec::utils::io;
using namespace yaramod;
Expand Down Expand Up @@ -309,6 +311,11 @@ int main(int argc, char** argv)
return printError("need one argument - KB path");
}

if ("--version"s == argv[1]) {
Log::info() << version::getVersionStringLong() << "\n";
return 0;
}

std::ifstream inputFile(argv[1], std::ios::binary);
if (!inputFile) {
return printError("could not open input file");
Expand Down
14 changes: 13 additions & 1 deletion src/macho-extractortool/macho_extractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "retdec/utils/conversion.h"
#include "retdec/utils/string.h"
#include "retdec/utils/io/log.h"
#include "retdec/utils/version.h"
#include "retdec/macho-extractor/break_fat.h"

using namespace retdec::utils::io;
Expand Down Expand Up @@ -55,7 +56,12 @@ void printUsage()
" ignores --json option.\n\n"
"Output options:\n\n"
" -o --out PATH\n"
" Output will be written to the PATH.\n\n";
" Output will be written to the PATH.\n\n"
"Other options:\n\n"
" -h --help\n"
" Show this help.\n\n"
" --version\n"
" Show RetDec version.\n\n";
}

/**
Expand Down Expand Up @@ -175,6 +181,12 @@ int handleArguments(
printUsage();
return 0;
}
else if (c == "--version")
{
Log::info() << retdec::utils::version::getVersionStringLong()
<< "\n";
return 0;
}
else if(c == "--check-archive")
{
isArchiveVerif = true;
Expand Down
Loading