Add version handling

This commit is contained in:
Mario Hüttel 2022-08-16 22:27:37 +02:00
parent 9107a2797c
commit 199b48d963
5 changed files with 51 additions and 5 deletions

View File

@ -7,13 +7,27 @@ pkg_check_modules(ELF REQUIRED libelf)
set (CFILES
main.c
version.c
)
set(GEN_HEADER_PATH "${CMAKE_CURRENT_BINARY_DIR}/include/generated")
add_custom_target(
version-header
COMMAND
mkdir -p ${GEN_HEADER_PATH} && bash "${CMAKE_CURRENT_SOURCE_DIR}/gen_version_header.sh" "${GEN_HEADER_PATH}/version.h"
WORKING_DIRECTORY
${CMAKE_CURRENT_SOURCE_DIR}
)
add_compile_options(-Wall -Wextra -Wold-style-declaration -Wuninitialized -Wmaybe-uninitialized -Wunused-parameter)
add_executable(${PROJECT_NAME} ${CFILES})
target_link_libraries(${PROJECT_NAME} ${ELF_LIBRARIES})
target_link_directories(${PROJECT_NAME} PRIVATE ${ELF_LIBRARY_DIRS})
target_include_directories(${PROJECT_NAME} PRIVATE ${ELF_INCLUDE_DIRS})
target_include_directories(${PROJECT_NAME} PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/include")
target_include_directories(${PROJECT_NAME} PRIVATE "include")
add_dependencies(${PROJECT_NAME} version-header)

11
gen_version_header.sh Executable file
View File

@ -0,0 +1,11 @@
#!/bin/bash
if [[ -z $1 ]]; then
exit -1;
fi
ver=`git describe --tags --always --dirty`
echo "#ifndef _VERSION_GENERATED_H_" > $1
echo "#define _VERSION_GENERATED_H_" >> $1
echo "#define GIT_VERSION_STRING \"$ver\"" >> $1
echo "#endif /* _VERSION_GENERATED_H_ */" >> $1

View File

@ -0,0 +1,7 @@
#ifndef _VERSION_H_
#define _VERSION_H_
extern const char *version_string;
extern const char *argp_program_version;
#endif /* _VERSION_H_ */

15
main.c
View File

@ -2,6 +2,7 @@
#include <libelf.h>
#include <argp.h>
#include <stdbool.h>
#include <patchelfcrc/version.h>
#define print_err(fmt, ...) fprintf(stderr, (fmt), ## __VA_ARGS__);
#define print_debug(fmt, ...) do { \
@ -10,6 +11,8 @@
} \
} while (0)
const char *argp_program_bug_address = "<mario.huettel@linux.com>";
enum granularity {
GRANULARITY_BYTE = 1,
GRANULARITY_16BIT,
@ -31,17 +34,22 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state)
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static int parse_cmdline_options(int *argc, char ***argv, struct command_line_options *cmd_opts)
{
error_t err;
if (!argc || !argv)
return -1000;
static struct argp_option options[] = {
{"little-endian", 'l', 0, 0, "Memory image is little endian. Only relevant if granularity is greater than a single byte"},
{"little-endian", 'l', 0, 0, "Memory image is little endian. Only relevant if granularity is greater than a single byte", 0},
/* Sentinel */
{NULL, 0, 0, 0, NULL}
{NULL, 0, 0, 0, NULL, 0}
};
static struct argp arg_parser = {
@ -52,8 +60,9 @@ static int parse_cmdline_options(int *argc, char ***argv, struct command_line_op
0, 0, 0
};
argp_parse(&arg_parser, *argc, *argv, 0, 0, cmd_opts);
err = argp_parse(&arg_parser, *argc, *argv, 0, 0, cmd_opts);
return err ? -1 : 0;
}
int main(int argc, char **argv)

5
version.c Normal file
View File

@ -0,0 +1,5 @@
#include <patchelfcrc/version.h>
#include <generated/version.h>
const char *version_string = GIT_VERSION_STRING;
const char *argp_program_version = GIT_VERSION_STRING;