2023-08-21 09:19:16 +08:00
|
|
|
#include "common/printing.h"
|
|
|
|
|
#include "common/jsonconfig.h"
|
2023-10-05 19:06:15 +08:00
|
|
|
#include "detection/libc/libc.h"
|
2023-08-21 09:19:16 +08:00
|
|
|
#include "detection/version/version.h"
|
|
|
|
|
#include "modules/version/version.h"
|
|
|
|
|
#include "util/stringUtils.h"
|
|
|
|
|
|
2023-10-05 19:06:15 +08:00
|
|
|
#define FF_VERSION_NUM_FORMAT_ARGS 9
|
2023-08-21 09:19:16 +08:00
|
|
|
|
|
|
|
|
void ffPrintVersion(FFVersionOptions* options)
|
|
|
|
|
{
|
2023-09-12 20:36:00 +08:00
|
|
|
FFVersionResult result;
|
2023-08-21 09:19:16 +08:00
|
|
|
ffDetectVersion(&result);
|
|
|
|
|
|
|
|
|
|
if(options->moduleArgs.outputFormat.length == 0)
|
|
|
|
|
{
|
|
|
|
|
ffPrintLogoAndKey(FF_VERSION_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
|
|
|
|
|
printf("%s %s%s%s (%s)\n", result.projectName, result.version, result.versionTweak, result.debugMode ? "-debug" : "", result.architecture);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-10-05 19:06:15 +08:00
|
|
|
FFLibcResult libcResult;
|
|
|
|
|
FF_STRBUF_AUTO_DESTROY buf = ffStrbufCreate();
|
|
|
|
|
if (!ffDetectLibc(&libcResult))
|
|
|
|
|
{
|
|
|
|
|
ffStrbufSetS(&buf, libcResult.name);
|
|
|
|
|
if (libcResult.version)
|
|
|
|
|
{
|
|
|
|
|
ffStrbufAppendC(&buf, ' ');
|
|
|
|
|
ffStrbufAppendS(&buf, libcResult.version);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-21 09:19:16 +08:00
|
|
|
ffPrintFormat(FF_VERSION_MODULE_NAME, 0, &options->moduleArgs, FF_VERSION_NUM_FORMAT_ARGS, (FFformatarg[]){
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRING, result.projectName},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRING, result.version},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRING, result.versionTweak},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRING, result.debugMode ? "debug" : "release"},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRING, result.architecture},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRING, result.cmakeBuiltType},
|
2023-09-24 23:07:03 +08:00
|
|
|
{FF_FORMAT_ARG_TYPE_STRING, result.compileTime},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRING, result.compiler},
|
2023-10-05 19:06:15 +08:00
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &buf},
|
2023-08-21 09:19:16 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ffInitVersionOptions(FFVersionOptions* options)
|
|
|
|
|
{
|
2023-10-07 13:40:40 +08:00
|
|
|
ffOptionInitModuleBaseInfo(&options->moduleInfo, FF_VERSION_MODULE_NAME, ffParseVersionCommandOptions, ffParseVersionJsonObject, ffPrintVersion, ffGenerateVersionJson, ffPrintVersionHelpFormat);
|
2023-08-21 09:19:16 +08:00
|
|
|
ffOptionInitModuleArg(&options->moduleArgs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ffParseVersionCommandOptions(FFVersionOptions* options, const char* key, const char* value)
|
|
|
|
|
{
|
|
|
|
|
const char* subKey = ffOptionTestPrefix(key, FF_VERSION_MODULE_NAME);
|
|
|
|
|
if (!subKey) return false;
|
|
|
|
|
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ffDestroyVersionOptions(FFVersionOptions* options)
|
|
|
|
|
{
|
|
|
|
|
ffOptionDestroyModuleArg(&options->moduleArgs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ffParseVersionJsonObject(FFVersionOptions* options, yyjson_val* module)
|
|
|
|
|
{
|
|
|
|
|
yyjson_val *key_, *val;
|
|
|
|
|
size_t idx, max;
|
|
|
|
|
yyjson_obj_foreach(module, idx, max, key_, val)
|
|
|
|
|
{
|
|
|
|
|
const char* key = yyjson_get_str(key_);
|
|
|
|
|
if(ffStrEqualsIgnCase(key, "type"))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
ffPrintError(FF_VERSION_MODULE_NAME, 0, &options->moduleArgs, "Unknown JSON key %s", key);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-09-12 20:36:00 +08:00
|
|
|
|
|
|
|
|
void ffGenerateVersionJson(FF_MAYBE_UNUSED FFVersionOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
|
|
|
|
|
{
|
|
|
|
|
FFVersionResult result;
|
|
|
|
|
ffDetectVersion(&result);
|
|
|
|
|
|
|
|
|
|
yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
|
|
|
|
|
yyjson_mut_obj_add_str(doc, obj, "projectName", result.projectName);
|
|
|
|
|
yyjson_mut_obj_add_str(doc, obj, "architecture", result.architecture);
|
|
|
|
|
yyjson_mut_obj_add_str(doc, obj, "version", result.version);
|
|
|
|
|
yyjson_mut_obj_add_str(doc, obj, "versionTweak", result.versionTweak);
|
|
|
|
|
yyjson_mut_obj_add_str(doc, obj, "cmakeBuiltType", result.cmakeBuiltType);
|
2023-09-24 23:07:03 +08:00
|
|
|
yyjson_mut_obj_add_str(doc, obj, "compileTime", result.compileTime);
|
|
|
|
|
yyjson_mut_obj_add_str(doc, obj, "compiler", result.compiler);
|
2023-09-12 20:36:00 +08:00
|
|
|
yyjson_mut_obj_add_bool(doc, obj, "debugMode", result.debugMode);
|
2023-10-05 19:06:15 +08:00
|
|
|
|
|
|
|
|
FFLibcResult libcResult;
|
|
|
|
|
if (ffDetectLibc(&libcResult))
|
|
|
|
|
{
|
|
|
|
|
yyjson_mut_obj_add_null(doc, obj, "libc");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
FF_STRBUF_AUTO_DESTROY buf = ffStrbufCreateS(libcResult.name);
|
|
|
|
|
if (libcResult.version)
|
|
|
|
|
{
|
|
|
|
|
ffStrbufAppendC(&buf, ' ');
|
|
|
|
|
ffStrbufAppendS(&buf, libcResult.version);
|
|
|
|
|
}
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "libc", &buf);
|
|
|
|
|
}
|
2023-09-12 20:36:00 +08:00
|
|
|
}
|
2023-10-07 13:40:40 +08:00
|
|
|
|
|
|
|
|
void ffPrintVersionHelpFormat(void)
|
|
|
|
|
{
|
|
|
|
|
ffPrintModuleFormatHelp(FF_VERSION_MODULE_NAME, "{1} {2}{3} ({5})", FF_VERSION_NUM_FORMAT_ARGS, (const char* []) {
|
|
|
|
|
"Project name",
|
|
|
|
|
"Version",
|
|
|
|
|
"Version tweak",
|
|
|
|
|
"Build type (debug or release)",
|
|
|
|
|
"Architecture",
|
|
|
|
|
"CMake build type (Debug, Release, RelWithDebInfo, MinSizeRel)",
|
|
|
|
|
"Date time when compiling",
|
|
|
|
|
"Compiler used",
|
|
|
|
|
"Libc used"
|
|
|
|
|
});
|
|
|
|
|
}
|