Skip to content

Commit

Permalink
report the commit sha that the binary was built with
Browse files Browse the repository at this point in the history
  • Loading branch information
tantaman committed Dec 21, 2023
1 parent 3bb5b46 commit 53c0857
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 5 deletions.
13 changes: 9 additions & 4 deletions core/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -170,19 +170,19 @@ $(shell.c):
$(sqlite3.c):
cd $(sqlite_src) && make sqlite3.c

$(rs_lib_dbg_static_cpy): FORCE $(dbg_prefix)
$(rs_lib_dbg_static_cpy): FORCE write_sha $(dbg_prefix)
cd ./rs/$(bundle) && cargo rustc $(RS_TARGET) --features static,omit_load_extension$(libsql_feature) $(rs_build_flags)
cp $(rs_lib_dbg_static) $(rs_lib_dbg_static_cpy)

$(rs_lib_static_cpy): FORCE $(prefix)
$(rs_lib_static_cpy): FORCE write_sha $(prefix)
cd ./rs/$(bundle) && cargo rustc $(RS_TARGET) --release --features static,omit_load_extension$(libsql_feature) $(rs_build_flags)
cp $(rs_lib_static) $(rs_lib_static_cpy)

$(rs_lib_loadable_cpy): FORCE $(prefix)
$(rs_lib_loadable_cpy): FORCE write_sha $(prefix)
cd ./rs/$(bundle) && cargo $(rs_ndk) build $(RS_TARGET) --release --features loadable_extension$(libsql_feature) $(rs_build_flags)
cp $(rs_lib_loadable) $(rs_lib_loadable_cpy)

$(rs_lib_dbg_loadable_cpy): FORCE $(dbg_prefix)
$(rs_lib_dbg_loadable_cpy): FORCE write_sha $(dbg_prefix)
cd ./rs/$(bundle) && cargo rustc $(RS_TARGET) --features loadable_extension$(libsql_feature) $(rs_build_flags)
cp $(rs_lib_dbg_loadable) $(rs_lib_dbg_loadable_cpy)

Expand Down Expand Up @@ -281,3 +281,8 @@ $(TARGET_FUZZ): $(prefix) $(TARGET_SQLITE3_EXTRA_C) src/fuzzer.cc $(ext_files)
ubsan analyzer fuzz asan static

FORCE: ;

write_sha:
@COMMIT_SHA=`git rev-parse HEAD` && \
sed -i.bak "s/\"[^\"]*\"/\"$$COMMIT_SHA\"/" ./rs/core/src/sha.rs && \
rm -f ./rs/core/src/sha.rs.bak
2 changes: 1 addition & 1 deletion core/rs/core/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ pub const TBL_SCHEMA: &'static str = "crsql_master";
// and, if we ever need it, we can track individual builds of a patch release
// 00_05_01_01
pub const CRSQLITE_VERSION: i32 = 16_01_00;
pub const CRSQLITE_VERSION_STR: &'static str = "0.16.1";
pub const SITE_ID_LEN: i32 = 16;
pub const SHA: &'static str = "03885434ab73d473c3ce02ac89c42cb870dba0a5";
pub const ROWID_SLAB_SIZE: i64 = 10000000000000;
// db version is a signed 64bit int since sqlite doesn't support saving and
// retrieving unsigned 64bit ints. (2^64 / 2) is a big enough number to write 1
Expand Down
54 changes: 54 additions & 0 deletions core/rs/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ mod local_writes;
pub mod pack_columns;
#[cfg(not(feature = "test"))]
mod pack_columns;
mod sha;
mod stmt_cache;
#[cfg(feature = "test")]
pub mod tableinfo;
Expand Down Expand Up @@ -266,6 +267,40 @@ pub extern "C" fn sqlite3_crsqlcore_init(
return null_mut();
}

let rc = db
.create_function_v2(
"crsql_sha",
0,
sqlite::UTF8 | sqlite::INNOCUOUS | sqlite::DETERMINISTIC,
None,
Some(x_crsql_sha),
None,
None,
None,
)
.unwrap_or(ResultCode::ERROR);
if rc != ResultCode::OK {
unsafe { crsql_freeExtData(ext_data) };
return null_mut();
}

let rc = db
.create_function_v2(
"crsql_version",
0,
sqlite::UTF8 | sqlite::INNOCUOUS | sqlite::DETERMINISTIC,
None,
Some(x_crsql_version),
None,
None,
None,
)
.unwrap_or(ResultCode::ERROR);
if rc != ResultCode::OK {
unsafe { crsql_freeExtData(ext_data) };
return null_mut();
}

let rc = db
.create_function_v2(
"crsql_increment_and_get_seq",
Expand Down Expand Up @@ -697,6 +732,25 @@ unsafe extern "C" fn x_crsql_next_db_version(
ctx.result_int64(ret);
}

/**
* The sha of the commit that this version of crsqlite was built from.
*/
unsafe extern "C" fn x_crsql_sha(
ctx: *mut sqlite::context,
argc: i32,
argv: *mut *mut sqlite::value,
) {
ctx.result_text_static(sha::SHA);
}

unsafe extern "C" fn x_crsql_version(
ctx: *mut sqlite::context,
argc: i32,
argv: *mut *mut sqlite::value,
) {
ctx.result_int64(consts::CRSQLITE_VERSION as i64);
}

unsafe extern "C" fn x_free_connection_ext_data(data: *mut c_void) {
let ext_data = data as *mut c::crsql_ExtData;
crsql_freeExtData(ext_data);
Expand Down
2 changes: 2 additions & 0 deletions core/rs/core/src/sha.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// The sha of the commit that this version of crsqlite was built from.
pub const SHA: &'static str = "";

0 comments on commit 53c0857

Please sign in to comment.