Skip to content

Commit 49f1112

Browse files
authored
Add version API (#60)
1 parent 1edfb89 commit 49f1112

7 files changed

Lines changed: 174 additions & 4 deletions

File tree

CMakeLists.txt

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
1-
# Specify the minimum version.
2-
cmake_minimum_required ( VERSION 3.0 )
1+
# Specify the minimum version (3.9 required for regex submatches).
2+
cmake_minimum_required ( VERSION 3.9 )
3+
4+
## Get project version
5+
# Reconfigure if VERSION file has changed
6+
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${CMAKE_SOURCE_DIR}/VERSION")
7+
# Read file
8+
file(READ "${CMAKE_SOURCE_DIR}/VERSION" version_string)
9+
# Parse string
10+
if (${version_string} MATCHES "^([0-9]+)\\.([0-9]+)\\.([0-9]+)([+-][A-Za-z0-9_-]+)?")
11+
set(LIBTRIXI_VERSION ${CMAKE_MATCH_0})
12+
set(LIBTRIXI_VERSION_MAJOR ${CMAKE_MATCH_1})
13+
set(LIBTRIXI_VERSION_MINOR ${CMAKE_MATCH_2})
14+
set(LIBTRIXI_VERSION_PATCH ${CMAKE_MATCH_3})
15+
else()
16+
message(FATAL_ERROR "Unable to parse version from string")
17+
endif()
318

419
# Specify the project info.
5-
project ( trixi VERSION 0.1.0 DESCRIPTION "Interface library for using Trixi.jl from C/C++/Fortran" )
20+
project ( trixi VERSION ${LIBTRIXI_VERSION_MAJOR}.${LIBTRIXI_VERSION_MINOR}.${LIBTRIXI_VERSION_PATCH}
21+
DESCRIPTION "Interface library for using Trixi.jl from C/C++/Fortran" )
622

723
# Enable C and Fortran
824
enable_language(C Fortran)
@@ -28,6 +44,7 @@ find_package( MPI REQUIRED )
2844
# Library target
2945
add_library ( ${PROJECT_NAME} SHARED
3046
src/trixi.c
47+
src/trixi_version.c
3148
src/trixi.h
3249
src/trixi.f90
3350
)
@@ -58,6 +75,13 @@ target_link_libraries( ${PROJECT_NAME} PRIVATE ${JULIA_LIBRARY} ${MPI_C_LIB_NAME
5875
# Set appropriate compile flags
5976
target_compile_options( ${PROJECT_NAME} PUBLIC "-fPIC" )
6077

78+
# Pass version information
79+
target_compile_definitions( ${PROJECT_NAME} PRIVATE
80+
LIBTRIXI_VERSION=\"${LIBTRIXI_VERSION}\"
81+
LIBTRIXI_VERSION_MAJOR=${LIBTRIXI_VERSION_MAJOR}
82+
LIBTRIXI_VERSION_MINOR=${LIBTRIXI_VERSION_MINOR}
83+
LIBTRIXI_VERSION_PATCH=${LIBTRIXI_VERSION_PATCH})
84+
6185
6286
# Add auxiliary *object* library to support fast thread-local storage (TLS)
6387
add_library ( ${PROJECT_NAME}_tls OBJECT

docs/doxygen/Doxyfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,7 @@ WARN_LOGFILE =
943943
# spaces. See also FILE_PATTERNS and EXTENSION_MAPPING
944944
# Note: If this tag is empty the current directory is searched.
945945

946-
INPUT = ../../src/trixi.h ../../src/trixi.c ../../src/trixi.f90 libtrixi.dox
946+
INPUT = ../../src/trixi.h ../../src/trixi.c ../../src/trixi_version.c ../../src/trixi.f90 libtrixi.dox
947947

948948
# This tag can be used to specify the character encoding of the source files
949949
# that doxygen parses. Internally doxygen uses the UTF-8 encoding. Doxygen uses

examples/simple_trixi_controller.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ int main ( int argc, char *argv[] ) {
5050
return 2;
5151
}
5252

53+
// Print version information
54+
printf("libtrixi version: %s\n", trixi_version());
55+
5356
// Initialize MPI
5457
printf("\n*** Trixi controller *** Initialize MPI\n");
5558
init_mpi_external(argc, argv);

examples/simple_trixi_controller.f90

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ program simple_trixi_controller_f
2222
stop 1
2323
end if
2424

25+
! Print version information
26+
write(*, '(a, a)') "libtrixi version: ", trixi_version()
2527

2628
! Initialize Trixi
2729
call get_command_argument(1, argument)

src/trixi.f90

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,55 @@ subroutine julia_eval_string_c(code) bind(c, name='julia_eval_string')
127127
use, intrinsic :: iso_c_binding, only: c_char
128128
character(kind=c_char), dimension(*), intent(in) :: code
129129
end subroutine
130+
131+
!>
132+
!! @fn LibTrixi::trixi_version_major::trixi_version_major()
133+
!!
134+
!! @brief Return major version number of libtrixi.
135+
!!
136+
!! @return Major version number as integer.
137+
!!
138+
!! @see @ref trixi_version_major_api_c "trixi_version_major (C API)"
139+
integer(c_int) function trixi_version_major() bind(c)
140+
use, intrinsic :: iso_c_binding, only: c_int
141+
end function
142+
143+
!>
144+
!! @fn LibTrixi::trixi_version_minor::trixi_version_minor()
145+
!!
146+
!! @brief Return minor version number of libtrixi.
147+
!!
148+
!! @return Minor version number as integer.
149+
!!
150+
!! @see @ref trixi_version_minor_api_c "trixi_version_minor (C API)"
151+
integer(c_int) function trixi_version_minor() bind(c)
152+
use, intrinsic :: iso_c_binding, only: c_int
153+
end function
154+
155+
!>
156+
!! @fn LibTrixi::trixi_version_patch::trixi_version_patch()
157+
!!
158+
!! @brief Return patch version number of libtrixi.
159+
!!
160+
!! @return Patch version number as integer.
161+
!!
162+
!! @see @ref trixi_version_patch_api_c "trixi_version_patch (C API)"
163+
integer(c_int) function trixi_version_patch() bind(c)
164+
use, intrinsic :: iso_c_binding, only: c_int
165+
end function
166+
167+
!>
168+
!! @fn LibTrixi::trixi_version_c::trixi_version_c()
169+
!!
170+
!! @brief Return full version string of libtrixi (C char pointer version).
171+
!!
172+
!! @return Full version string as C char pointer.
173+
!!
174+
!! @see @ref trixi_version "trixi_version (Fortran convenience version)"
175+
!! @see @ref trixi_version_api_c "trixi_version (C API)"
176+
type(c_ptr) function trixi_version_c() bind(c, name='trixi_version')
177+
use, intrinsic :: iso_c_binding, only: c_ptr
178+
end function
130179
end interface
131180

132181
contains
@@ -205,6 +254,35 @@ subroutine julia_eval_string(code)
205254

206255
call julia_eval_string_c(trim(adjustl(code)) // c_null_char)
207256
end subroutine
257+
258+
!>
259+
!! @brief Return full version string of libtrixi (Fortran convenience version).
260+
!!
261+
!! @return Full version string as Fortran allocatable string.
262+
!!
263+
!! @see @ref trixi_version_c::trixi_version_c
264+
!! "trixi_version_c (C char pointer version)"
265+
!! @see @ref trixi_version_api_c
266+
!! "trixi_version (C API)"
267+
function trixi_version()
268+
use, intrinsic :: iso_c_binding, only: c_char, c_null_char, c_f_pointer
269+
character(len=:), allocatable :: trixi_version
270+
character(len=128, kind=c_char), pointer :: buffer
271+
integer :: length, i
272+
273+
! Associate buffer with C pointer
274+
call c_f_pointer(trixi_version_c(), buffer)
275+
276+
! Determine the actual length of the version string
277+
length = 0
278+
do i = 1,128
279+
if ( buffer(i:i) == c_null_char ) exit
280+
length = length + 1
281+
end do
282+
283+
! Store relevant part in return value
284+
trixi_version = buffer(1:(length + 1))
285+
end function
208286
end module
209287

210288
!>

src/trixi.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ void trixi_step(int handle);
1414
void trixi_finalize_simulation(int handle);
1515
void trixi_finalize();
1616

17+
int trixi_version_major();
18+
int trixi_version_minor();
19+
int trixi_version_patch();
20+
const char* trixi_version();
21+
1722
void julia_eval_string(const char * code);
1823

1924
/**

src/trixi_version.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* @anchor trixi_version_major_api_c
3+
*
4+
* @brief Return major version number of libtrixi.
5+
*
6+
* This function may be run before `trixi_initialize` has been called.
7+
*
8+
* @return Major version of libtrixi.
9+
*/
10+
int trixi_version_major() {
11+
return LIBTRIXI_VERSION_MAJOR;
12+
}
13+
14+
/**
15+
* @anchor trixi_version_minor_api_c
16+
*
17+
* @brief Return minor version number of libtrixi.
18+
*
19+
* This function may be run before `trixi_initialize` has been called.
20+
*
21+
* @return Minor version of libtrixi.
22+
*/
23+
int trixi_version_minor() {
24+
return LIBTRIXI_VERSION_MINOR;
25+
}
26+
27+
/**
28+
* @anchor trixi_version_patch_api_c
29+
*
30+
* @brief Return patch version number of libtrixi.
31+
*
32+
* This function may be run before `trixi_initialize` has been called.
33+
*
34+
* @return Patch version of libtrixi.
35+
*/
36+
int trixi_version_patch() {
37+
return LIBTRIXI_VERSION_PATCH;
38+
}
39+
40+
/**
41+
* @anchor trixi_version_api_c
42+
*
43+
* @brief Return full version string of libtrixi.
44+
*
45+
* The return value is a read-only pointer to a NULL-terminated string with the version
46+
* information. This may include not just MAJOR.MINOR.PATCH but possibly also additional
47+
* build or development version information.
48+
*
49+
* The returned pointer is to static memory and must not be used to change the contents of
50+
* the version string. Multiple calls to the function will return the same address.
51+
*
52+
* This function is thread-safe and may be run before `trixi_initialize` has been called.
53+
*
54+
* @return Pointer to a read-only, NULL-terminated character array with the full version of libtrixi.
55+
*/
56+
const char* trixi_version() {
57+
return LIBTRIXI_VERSION;
58+
}

0 commit comments

Comments
 (0)