Skip to content

Commit

Permalink
Add --version option (#506)
Browse files Browse the repository at this point in the history
  • Loading branch information
t-sommer authored May 21, 2024
1 parent bfa9cc3 commit 18adfad
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
28 changes: 28 additions & 0 deletions build/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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('-')

Expand Down
6 changes: 4 additions & 2 deletions fmusim/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 10 additions & 2 deletions fmusim/fmusim.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

#define PROGNAME "fmusim"


#define CALL(f) do { status = f; if (status > FMIOK) goto TERMINATE; } while (0)


Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 18adfad

Please sign in to comment.