Skip to content

Commit 18adfad

Browse files
authored
Add --version option (#506)
1 parent bfa9cc3 commit 18adfad

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

build/build.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,27 @@
2424
args, _ = parser.parse_known_args()
2525

2626

27+
def get_version():
28+
try:
29+
cwd = os.path.dirname(__file__)
30+
31+
changed_files = subprocess.check_output(['git', 'status', '--porcelain', '--untracked=no'],
32+
cwd=cwd).decode('ascii').strip()
33+
34+
if changed_files:
35+
return None
36+
37+
version = subprocess.check_output(['git', 'tag', '--contains'], cwd=cwd).decode('ascii').strip()
38+
39+
if not version:
40+
version = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD'], cwd=cwd).decode(
41+
'ascii').strip()
42+
43+
return version
44+
except:
45+
return None
46+
47+
2748
def build_fmus(fmi_version, fmi_type=None):
2849

2950
if fmi_type is not None:
@@ -38,6 +59,13 @@ def build_fmus(fmi_version, fmi_type=None):
3859

3960
cmake_args = []
4061

62+
version = get_version()
63+
64+
if not version:
65+
version = 'development build'
66+
67+
cmake_args += ['-D', f'FMUSIM_VERSION="{version}"']
68+
4169
fmi_platform = args.platform
4270
fmi_architecture, fmi_system = fmi_platform.split('-')
4371

fmusim/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ else ()
1010
set(FMI_PLATFORM "${FMI_ARCHITECTURE}-linux")
1111
endif ()
1212

13+
set(FMUSIM_VERSION "" CACHE STRING "")
14+
1315
set(CVODE_DIR ${CMAKE_SOURCE_DIR}/build/cvode-${FMI_PLATFORM}/install/)
1416
set(LIBXML2_DIR ${CMAKE_SOURCE_DIR}/build/libxml2-${FMI_PLATFORM}/install/)
1517
set(ZLIB_DIR ${CMAKE_SOURCE_DIR}/build/zlib-${FMI_PLATFORM}/install/)
@@ -94,9 +96,9 @@ target_include_directories(fmusim PRIVATE
9496
)
9597

9698
if (WIN32)
97-
target_compile_definitions(fmusim PRIVATE YY_NO_UNISTD_H LIBXML_STATIC)
99+
target_compile_definitions(fmusim PRIVATE FMUSIM_VERSION=${FMUSIM_VERSION} YY_NO_UNISTD_H LIBXML_STATIC)
98100
else ()
99-
target_compile_definitions(fmusim PRIVATE LIBXML_STATIC)
101+
target_compile_definitions(fmusim PRIVATE FMUSIM_VERSION=${FMUSIM_VERSION} LIBXML_STATIC)
100102
endif ()
101103

102104
if (WIN32)

fmusim/fmusim.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
#define PROGNAME "fmusim"
3939

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

4243

@@ -101,10 +102,11 @@ static void logFunctionCall(FMIInstance* instance, FMIStatus status, const char*
101102

102103
void printUsage() {
103104
printf(
104-
"Usage: " PROGNAME " [OPTION]... [FMU]\n"
105+
"Usage: " PROGNAME " [OPTION]... [FMU]\n\n"
105106
"Simulate a Functional Mock-up Unit and write the output to result.csv.\n"
106107
"\n"
107108
" --help display this help and exit\n"
109+
" --version display the program version\n"
108110
" --interface-type [me|cs] the interface type to use\n"
109111
" --tolerance [TOLERANCE] relative tolerance\n"
110112
" --start-time [VALUE] start time\n"
@@ -253,7 +255,13 @@ int main(int argc, const char* argv[]) {
253255
goto TERMINATE;
254256
} else if (argc == 2 && !strcmp(argv[1], "--help")) {
255257
printUsage();
256-
status = FMIError;
258+
goto TERMINATE;
259+
} else if (argc == 2 && !strcmp(argv[1], "--version")) {
260+
printf(PROGNAME " "
261+
#ifdef FMUSIM_VERSION
262+
FMUSIM_VERSION
263+
#endif
264+
" (" FMI_PLATFORM_TUPLE ")\n");
257265
goto TERMINATE;
258266
}
259267

0 commit comments

Comments
 (0)