From 18adfad01bd939b388313c83c0465c84c11febac Mon Sep 17 00:00:00 2001 From: Torsten Sommer Date: Tue, 21 May 2024 10:35:06 +0200 Subject: [PATCH] Add --version option (#506) --- build/build.py | 28 ++++++++++++++++++++++++++++ fmusim/CMakeLists.txt | 6 ++++-- fmusim/fmusim.c | 12 ++++++++++-- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/build/build.py b/build/build.py index 9b2de884..02760f9c 100644 --- a/build/build.py +++ b/build/build.py @@ -24,6 +24,27 @@ args, _ = parser.parse_known_args() +def get_version(): + try: + cwd = os.path.dirname(__file__) + + changed_files = subprocess.check_output(['git', 'status', '--porcelain', '--untracked=no'], + cwd=cwd).decode('ascii').strip() + + if changed_files: + return None + + version = subprocess.check_output(['git', 'tag', '--contains'], cwd=cwd).decode('ascii').strip() + + if not version: + version = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD'], cwd=cwd).decode( + 'ascii').strip() + + return version + except: + return None + + def build_fmus(fmi_version, fmi_type=None): if fmi_type is not None: @@ -38,6 +59,13 @@ def build_fmus(fmi_version, fmi_type=None): cmake_args = [] + version = get_version() + + if not version: + version = 'development build' + + cmake_args += ['-D', f'FMUSIM_VERSION="{version}"'] + fmi_platform = args.platform fmi_architecture, fmi_system = fmi_platform.split('-') diff --git a/fmusim/CMakeLists.txt b/fmusim/CMakeLists.txt index 615c3f20..78d78dc2 100644 --- a/fmusim/CMakeLists.txt +++ b/fmusim/CMakeLists.txt @@ -10,6 +10,8 @@ else () set(FMI_PLATFORM "${FMI_ARCHITECTURE}-linux") endif () +set(FMUSIM_VERSION "" CACHE STRING "") + set(CVODE_DIR ${CMAKE_SOURCE_DIR}/build/cvode-${FMI_PLATFORM}/install/) set(LIBXML2_DIR ${CMAKE_SOURCE_DIR}/build/libxml2-${FMI_PLATFORM}/install/) set(ZLIB_DIR ${CMAKE_SOURCE_DIR}/build/zlib-${FMI_PLATFORM}/install/) @@ -94,9 +96,9 @@ target_include_directories(fmusim PRIVATE ) if (WIN32) - target_compile_definitions(fmusim PRIVATE YY_NO_UNISTD_H LIBXML_STATIC) + target_compile_definitions(fmusim PRIVATE FMUSIM_VERSION=${FMUSIM_VERSION} YY_NO_UNISTD_H LIBXML_STATIC) else () - target_compile_definitions(fmusim PRIVATE LIBXML_STATIC) + target_compile_definitions(fmusim PRIVATE FMUSIM_VERSION=${FMUSIM_VERSION} LIBXML_STATIC) endif () if (WIN32) diff --git a/fmusim/fmusim.c b/fmusim/fmusim.c index e7fd8f58..7ee4b548 100644 --- a/fmusim/fmusim.c +++ b/fmusim/fmusim.c @@ -37,6 +37,7 @@ #define PROGNAME "fmusim" + #define CALL(f) do { status = f; if (status > FMIOK) goto TERMINATE; } while (0) @@ -101,10 +102,11 @@ static void logFunctionCall(FMIInstance* instance, FMIStatus status, const char* void printUsage() { printf( - "Usage: " PROGNAME " [OPTION]... [FMU]\n" + "Usage: " PROGNAME " [OPTION]... [FMU]\n\n" "Simulate a Functional Mock-up Unit and write the output to result.csv.\n" "\n" " --help display this help and exit\n" + " --version display the program version\n" " --interface-type [me|cs] the interface type to use\n" " --tolerance [TOLERANCE] relative tolerance\n" " --start-time [VALUE] start time\n" @@ -253,7 +255,13 @@ int main(int argc, const char* argv[]) { goto TERMINATE; } else if (argc == 2 && !strcmp(argv[1], "--help")) { printUsage(); - status = FMIError; + goto TERMINATE; + } else if (argc == 2 && !strcmp(argv[1], "--version")) { + printf(PROGNAME " " +#ifdef FMUSIM_VERSION + FMUSIM_VERSION +#endif + " (" FMI_PLATFORM_TUPLE ")\n"); goto TERMINATE; }