diff --git a/CMakeLists.txt b/CMakeLists.txt index 299576d..8ae1a57 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/gen_version_header.sh b/gen_version_header.sh new file mode 100755 index 0000000..12e592d --- /dev/null +++ b/gen_version_header.sh @@ -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 diff --git a/include/patchelfcrc/version.h b/include/patchelfcrc/version.h new file mode 100644 index 0000000..a5b9305 --- /dev/null +++ b/include/patchelfcrc/version.h @@ -0,0 +1,7 @@ +#ifndef _VERSION_H_ +#define _VERSION_H_ + +extern const char *version_string; +extern const char *argp_program_version; + +#endif /* _VERSION_H_ */ diff --git a/main.c b/main.c index 6a83808..3b9b89c 100644 --- a/main.c +++ b/main.c @@ -2,6 +2,7 @@ #include #include #include +#include #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 = ""; + 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) diff --git a/version.c b/version.c new file mode 100644 index 0000000..24f07ae --- /dev/null +++ b/version.c @@ -0,0 +1,5 @@ +#include +#include + +const char *version_string = GIT_VERSION_STRING; +const char *argp_program_version = GIT_VERSION_STRING;