Skip to content

Commit

Permalink
feat(capi): Add API to get runtime library version
Browse files Browse the repository at this point in the history
  • Loading branch information
kanru committed Jul 13, 2024
1 parent 87d46eb commit bf4bcc2
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 2 deletions.
2 changes: 2 additions & 0 deletions capi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ mod io;
mod logger;
mod public;

pub mod version;

/// Initializes chewing context and environment settings.
///
/// Most of the Chewing IM APIs require a
Expand Down
5 changes: 5 additions & 0 deletions capi/src/symbols-elf.map
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,9 @@ CHEWING_0.9 {
chewing_config_get_int;
chewing_config_set_str;
chewing_config_get_str;
chewing_version;
chewing_version_major;
chewing_version_minor;
chewing_version_patch;
chewing_version_extra;
} CHEWING_0.5;
7 changes: 6 additions & 1 deletion capi/src/symbols-mach_o.map
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,9 @@ _chewing_config_has_option
_chewing_config_set_int
_chewing_config_get_int
_chewing_config_set_str
_chewing_config_get_str
_chewing_config_get_str
_chewing_version
_chewing_version_major
_chewing_version_minor
_chewing_version_patch
_chewing_version_extra
7 changes: 6 additions & 1 deletion capi/src/symbols-msvc.def
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,9 @@ EXPORTS
chewing_config_set_int;
chewing_config_get_int;
chewing_config_set_str;
chewing_config_get_str;
chewing_config_get_str;
chewing_version;
chewing_version_major;
chewing_version_minor;
chewing_version_patch;
chewing_version_extra;
26 changes: 26 additions & 0 deletions capi/src/version.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use std::ffi::{c_char, c_int};

#[no_mangle]
pub extern "C" fn chewing_version() -> *const c_char {
c"0.9.0-alpha.1".as_ptr()
}

#[no_mangle]
pub extern "C" fn chewing_version_major() -> c_int {
0
}

#[no_mangle]
pub extern "C" fn chewing_version_minor() -> c_int {
9
}

#[no_mangle]
pub extern "C" fn chewing_version_patch() -> c_int {
0
}

#[no_mangle]
pub extern "C" fn chewing_version_extra() -> *const c_char {
c"-alpha.1".as_ptr()
}
10 changes: 10 additions & 0 deletions include/chewing.h
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,16 @@ void chewing_set_hsuSelKeyType(struct ChewingContext *_ctx, int mode);
*/
int chewing_get_hsuSelKeyType(struct ChewingContext *_ctx);

const char *chewing_version(void);

int chewing_version_major(void);

int chewing_version_minor(void);

int chewing_version_patch(void);

const char *chewing_version_extra(void);

#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
Expand Down
18 changes: 18 additions & 0 deletions tests/test-config.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* of this file.
*/

#include <stdio.h>
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
Expand Down Expand Up @@ -726,6 +727,21 @@ void test_new2()
test_new2_userpath();
}

void test_runtime_version()
{
char buf[256];
int major = chewing_version_major();
int minor = chewing_version_minor();
int patch = chewing_version_patch();
const char *extra = chewing_version_extra();
const char *version = chewing_version();

ok(version != NULL, "chewing_version returns a version string");

sprintf(buf, "%d.%d.%d%s", major, minor, patch, extra);
ok(strcmp(buf, version) == 0, "chewing_version can be created from components");
}

int main(int argc, char *argv[])
{
char *logname;
Expand Down Expand Up @@ -764,6 +780,8 @@ int main(int argc, char *argv[])

test_new2();

test_runtime_version();

fclose(fd);

return exit_status();
Expand Down

0 comments on commit bf4bcc2

Please sign in to comment.