Skip to content

Commit

Permalink
bitwuzla: added build guide, native libs for windows, linux reassembled
Browse files Browse the repository at this point in the history
  • Loading branch information
dee-tree committed Oct 24, 2023
1 parent 9312ca2 commit a3fc264
Show file tree
Hide file tree
Showing 11 changed files with 356 additions and 47 deletions.
23 changes: 10 additions & 13 deletions ksmt-bitwuzla/bindings-native/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,24 @@ PROJECT(bitwuzla_jni)
FIND_PACKAGE(JNI REQUIRED)

SET(CXX_STANDARD 17)
SET(BITWUZLA_ROOT "${PROJECT_SOURCE_DIR}/bitwuzla")

# Path to bitwuzla.h
SET(BITWUZLA_INCLUDE "${PROJECT_SOURCE_DIR}/bitwuzla/include/bitwuzla/c")
# Path to enums.h, option.h
SET(BITWUZLA_INCLUDE2 "${PROJECT_SOURCE_DIR}/include")
# Path to bitwuzla.h, enums.h, option.h
SET(BITWUZLA_INCLUDE "${BITWUZLA_ROOT}/include")

# Path to Bitwuzla sources root (required for extensions)
SET(BITWUZLA_SRC_ROOT "${PROJECT_SOURCE_DIR}/bitwuzla/src")
# Path to bitwuzla library (libbitwuzla.so)
SET(BITWUZLA_LIB "${BITWUZLA_ROOT}/build/src/${CMAKE_SHARED_LIBRARY_PREFIX}bitwuzla${CMAKE_SHARED_LIBRARY_SUFFIX}")

# Path to bitwuzla library (libbitwuzla.so)
SET(BITWUZLA_LIB "${PROJECT_SOURCE_DIR}/bitwuzla/lib/${CMAKE_SHARED_LIBRARY_PREFIX}bitwuzla${CMAKE_SHARED_LIBRARY_SUFFIX}")
ADD_LIBRARY (bitwuzla_jni SHARED bitwuzla_jni.cpp bitwuzla_extension.cpp)

ADD_LIBRARY (bitwuzla_jni SHARED bitwuzla_jni.cpp)
message(${BITWUZLA_INCLUDE})

TARGET_INCLUDE_DIRECTORIES(bitwuzla_jni PRIVATE ${JNI_INCLUDE_DIRS})
TARGET_INCLUDE_DIRECTORIES(bitwuzla_jni PRIVATE "./include"})
TARGET_INCLUDE_DIRECTORIES(bitwuzla_jni PRIVATE ${BITWUZLA_INCLUDE})
TARGET_INCLUDE_DIRECTORIES(bitwuzla_jni PRIVATE ${BITWUZLA_INCLUDE2})

SET(BITWUZLA_SRC_INCLUDE "${BITWUZLA_SRC_ROOT}/bitwuzla/src")
TARGET_INCLUDE_DIRECTORIES(bitwuzla_jni PRIVATE ${BITWUZLA_SRC_INCLUDE})

SET(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)

# For Windows build, comment the first line, and uncomment the second one (link statically with libstdc++, ligbcc, lpthreads)
TARGET_LINK_LIBRARIES(bitwuzla_jni PRIVATE ${BITWUZLA_LIB})
#TARGET_LINK_LIBRARIES(bitwuzla_jni PRIVATE ${BITWUZLA_LIB} -static-libstdc++ -static-libgcc -Wl,-Bstatic -lstdc++ -lpthread -Wl,-Bdynamic)
30 changes: 30 additions & 0 deletions ksmt-bitwuzla/bindings-native/CMakeLists_win64.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
cmake_minimum_required (VERSION 3.8)

SET(CMAKE_SYSTEM_NAME "Windows")
SET(CMAKE_C_COMPILER "gcc")
SET(CMAKE_CXX_COMPILER "g++")
SET(CMAKE_MAKE_PROGRAM "mingw32-make")

PROJECT(bitwuzla_jni)


FIND_PACKAGE(JNI REQUIRED)

SET(CXX_STANDARD 17)
#SET(PROJECT_SOURCE_DIR "/path/to/bitwuzla/root")

# Path to bitwuzla.h, enums.h, option.h
SET(BITWUZLA_INCLUDE "${PROJECT_SOURCE_DIR}/include")

# Path to bitwuzla library (libbitwuzla.so)
SET(BITWUZLA_LIB "${PROJECT_SOURCE_DIR}/build/src/${CMAKE_SHARED_LIBRARY_PREFIX}bitwuzla${CMAKE_SHARED_LIBRARY_SUFFIX}")

ADD_LIBRARY (bitwuzla_jni SHARED bitwuzla_jni.cpp bitwuzla_extension.cpp)

TARGET_INCLUDE_DIRECTORIES(bitwuzla_jni PRIVATE ${JNI_INCLUDE_DIRS})
TARGET_INCLUDE_DIRECTORIES(bitwuzla_jni PRIVATE "./include"})
TARGET_INCLUDE_DIRECTORIES(bitwuzla_jni PRIVATE ${BITWUZLA_INCLUDE})

SET(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)

TARGET_LINK_LIBRARIES(bitwuzla_jni PRIVATE ${BITWUZLA_LIB})
27 changes: 27 additions & 0 deletions ksmt-bitwuzla/bindings-native/bitwuzla_extension.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "bitwuzla/c/bitwuzla.h"
#include <bitwuzla_extension.hpp>


#if __cplusplus
extern "C" {
#endif

void bitwuzla_extension_sort_dec_ref(BitwuzlaSort sort_id) {
bitwuzla_sort_dec_ref(sort_id);
}

void bitwuzla_extension_term_dec_ref(BitwuzlaTerm term_id) {
bitwuzla_term_dec_ref(term_id);
}

uint64_t bitwuzla_extension_bv_value_uint64(BitwuzlaTerm term) {
return bitwuzla_bv_value_uint64(term);
}

const char *bitwuzla_extension_bv_value_str(BitwuzlaTerm term, uint32_t base) {
return bitwuzla_bv_value_str(term, base);
}

#if __cplusplus
}
#endif
36 changes: 23 additions & 13 deletions ksmt-bitwuzla/bindings-native/bitwuzla_jni.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#include <iostream>
#include <atomic>
#include <bitwuzla.h>
#include <memory>
#include <unistd.h>
#include <vector>
#include <chrono>
#include "bitwuzla_jni.hpp"
#include <bitwuzla/c/bitwuzla.h>
#include <bitwuzla_jni.hpp>
#include <bitwuzla_extension.hpp>

#define BITWUZLA_JNI_EXCEPTION_CLS "org/ksmt/solver/bitwuzla/bindings/BitwuzlaNativeException"
#define BITWUZLA_CATCH_STATEMENT catch (const std::exception& e)
Expand Down Expand Up @@ -192,13 +190,13 @@ void Java_org_ksmt_solver_bitwuzla_bindings_Native_bitwuzlaInit(JNIEnv *env, jcl

void Java_org_ksmt_solver_bitwuzla_bindings_Native_bitwuzlaTermDecRef(JNIEnv *env, jclass native_class, jlong term) {
BZLA_TRY_VOID({
bitwuzla_term_dec_ref(TERM(term));
bitwuzla_extension_term_dec_ref(TERM(term));
})
}

void Java_org_ksmt_solver_bitwuzla_bindings_Native_bitwuzlaSortDecRef(JNIEnv *env, jclass native_class, jlong sort) {
BZLA_TRY_VOID({
bitwuzla_sort_dec_ref(SORT(sort));
bitwuzla_extension_sort_dec_ref(SORT(sort));
})
}

Expand Down Expand Up @@ -828,6 +826,18 @@ jint Java_org_ksmt_solver_bitwuzla_bindings_Native_bitwuzlaCheckSatAssumingNativ
})
}

jint Java_org_ksmt_solver_bitwuzla_bindings_Native_bitwuzlaCheckSatTimeout(JNIEnv* env, jobject native_class, jlong bitwuzla, jlongArray args, jlong timeout) {
BZLA_TRY_OR_ZERO({
auto termination_state = get_termination_state(BZLA);
ScopedTimeout _timeout(termination_state, timeout);

GET_PTR_ARRAY(BitwuzlaTerm, args_ptr, args);
jsize len = env->GetArrayLength(args);

return (jint) bitwuzla_check_sat_assuming(BZLA, (uint32_t) len, args_ptr);
})
}

jlong Java_org_ksmt_solver_bitwuzla_bindings_Native_bitwuzlaGetValue(JNIEnv *env, jclass native_class, jlong bitwuzla, jlong term) {
BZLA_TRY_OR_ZERO({
return (jlong) bitwuzla_get_value(BZLA, TERM(term));
Expand All @@ -836,13 +846,13 @@ jlong Java_org_ksmt_solver_bitwuzla_bindings_Native_bitwuzlaGetValue(JNIEnv *env

jlong Java_org_ksmt_solver_bitwuzla_bindings_Native_bitwuzlaGetBvValueUInt64(JNIEnv *env, jclass native_class, jlong term) {
BZLA_TRY_OR_ZERO({
return (jlong) bitwuzla_term_value_get_bv_uint64(TERM(term));
return (jlong) bitwuzla_extension_bv_value_uint64(TERM(term));
})
}

jstring Java_org_ksmt_solver_bitwuzla_bindings_Native_bitwuzlaGetBvValueString(JNIEnv *env, jclass native_class, jlong term) {
BZLA_TRY_OR_ZERO({
const char * str = bitwuzla_term_value_get_bv_str(TERM(term), 2);
const char * str = bitwuzla_extension_bv_value_str(TERM(term), 2);
return env->NewStringUTF(str);
})
}
Expand Down Expand Up @@ -1119,16 +1129,16 @@ jlong Java_org_ksmt_solver_bitwuzla_bindings_Native_bitwuzlaMkVar(JNIEnv *env, j
})
}

jlong Java_org_ksmt_solver_bitwuzla_bindings_Native_bitwuzlaSubstituteTerm(JNIEnv *env, jclass native_class, jlong bitwuzla, jlong term, jlongArray mapKeys, jlongArray mapValues) {
jlong Java_org_ksmt_solver_bitwuzla_bindings_Native_bitwuzlaSubstituteTerm(JNIEnv *env, jclass native_class, jlong term, jlongArray mapKeys, jlongArray mapValues) {
BZLA_TRY_OR_ZERO({
GET_PTR_ARRAY(BitwuzlaTerm, mapKeys_ptr, mapKeys);
GET_PTR_ARRAY(BitwuzlaTerm, mapValues_ptr, mapValues);
jsize map_size = env->GetArrayLength(mapKeys);
return (jlong) bitwuzla_substitute_term(BZLA, TERM(term), map_size, mapKeys_ptr, mapValues_ptr);
return (jlong) bitwuzla_substitute_term(TERM(term), map_size, mapKeys_ptr, mapValues_ptr);
})
}

void Java_org_ksmt_solver_bitwuzla_bindings_Native_bitwuzlaSubstituteTerms(JNIEnv *env, jclass native_class, jlong bitwuzla, jlongArray terms, jlongArray mapKeys, jlongArray mapValues) {
void Java_org_ksmt_solver_bitwuzla_bindings_Native_bitwuzlaSubstituteTerms(JNIEnv *env, jclass native_class, jlongArray terms, jlongArray mapKeys, jlongArray mapValues) {
BZLA_TRY_VOID({
GET_PTR_ARRAY(BitwuzlaTerm, terms_ptr, terms);
jsize terms_size = env->GetArrayLength(terms);
Expand All @@ -1137,6 +1147,6 @@ void Java_org_ksmt_solver_bitwuzla_bindings_Native_bitwuzlaSubstituteTerms(JNIEn
GET_PTR_ARRAY(BitwuzlaTerm, mapValues_ptr, mapValues);
jsize map_size = env->GetArrayLength(mapKeys);

bitwuzla_substitute_terms(BZLA, (size_t) terms_size, terms_ptr, map_size, mapKeys_ptr, mapValues_ptr);
bitwuzla_substitute_terms((size_t) terms_size, terms_ptr, map_size, mapKeys_ptr, mapValues_ptr);
})
}
159 changes: 159 additions & 0 deletions ksmt-bitwuzla/bindings-native/bitwuzla_patch.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
diff --git a/include/bitwuzla/c/bitwuzla.h b/include/bitwuzla/c/bitwuzla.h
index 2870e868..007f9d58 100644
--- a/include/bitwuzla/c/bitwuzla.h
+++ b/include/bitwuzla/c/bitwuzla.h
@@ -2007,6 +2007,14 @@ void bitwuzla_substitute_terms(size_t terms_size,

/** @} */

+void bitwuzla_sort_dec_ref(BitwuzlaSort sort_id);
+
+void bitwuzla_term_dec_ref(BitwuzlaTerm term_id);
+
+uint64_t bitwuzla_bv_value_uint64(BitwuzlaTerm term);
+
+const char *bitwuzla_bv_value_str(BitwuzlaTerm term, uint32_t base);
+
#if __cplusplus
}
#endif
diff --git a/src/api/c/bitwuzla.cpp b/src/api/c/bitwuzla.cpp
index ece639cc..5c3175f7 100644
--- a/src/api/c/bitwuzla.cpp
+++ b/src/api/c/bitwuzla.cpp
@@ -2069,3 +2069,56 @@ bitwuzla_term_print_fmt(BitwuzlaTerm term, FILE *file, uint8_t base)
}

/* -------------------------------------------------------------------------- */
+
+#include "node/node.h"
+#include "bv/bitvector.h"
+
+namespace bitwuzla {
+ template<>
+ bzla::BitVector
+ Term::value(uint8_t base) const {
+ (void) base;
+ BITWUZLA_CHECK_NOT_NULL(d_node);
+ BITWUZLA_CHECK_TERM_IS_BV_VALUE(*this);
+ return d_node->value<bzla::BitVector>();
+ }
+}
+
+void bitwuzla_sort_dec_ref(BitwuzlaSort sort_id) {
+ Bitwuzla::SortMap &sort_map = Bitwuzla::sort_map();
+ const auto it = sort_map.find(sort_id);
+ it->second.second -= 1;
+ if (it->second.second == 0) {
+ it->second.first.reset();
+ sort_map.erase(it);
+ }
+}
+
+void bitwuzla_term_dec_ref(BitwuzlaTerm term_id) {
+ Bitwuzla::TermMap &term_map = Bitwuzla::term_map();
+ const auto it = term_map.find(term_id);
+ it->second.second -= 1;
+ if (it->second.second == 0) {
+ it->second.first.reset();
+ term_map.erase(it);
+ }
+}
+
+uint64_t bitwuzla_bv_value_uint64(BitwuzlaTerm term) {
+ uint64_t res = false;
+ BITWUZLA_TRY_CATCH_BEGIN;
+ BITWUZLA_CHECK_TERM_ID(term);
+ bzla::BitVector bv = import_term(term).value<bzla::BitVector>();
+ res = bv.to_uint64();
+ BITWUZLA_TRY_CATCH_END;
+ return res;
+}
+
+const char *bitwuzla_bv_value_str(BitwuzlaTerm term, uint32_t base) {
+ static thread_local std::string res;
+ BITWUZLA_TRY_CATCH_BEGIN;
+ BITWUZLA_CHECK_TERM_ID(term);
+ res = import_term(term).value<bzla::BitVector>().str(base);
+ BITWUZLA_TRY_CATCH_END;
+ return res.c_str();
+}
diff --git a/src/api/c/checks.h b/src/api/c/checks.h
index 63a26fff..60be3ed8 100644
--- a/src/api/c/checks.h
+++ b/src/api/c/checks.h
@@ -47,7 +47,7 @@ class BitwuzlaAbortStream
{
stream() << msg_prefix << " ";
}
- ~BitwuzlaAbortStream()
+ ~BitwuzlaAbortStream() noexcept(false)
{
flush();
bitwuzla_abort(d_stream.str().c_str());
diff --git a/src/lib/meson.build b/src/lib/meson.build
index 7380e24c..38fcb022 100644
--- a/src/lib/meson.build
+++ b/src/lib/meson.build
@@ -32,19 +32,19 @@ rng_lib = static_library('bzlarng',
dependencies: gmp_dep)

# Bitwuzla bit-vector, bit-blast and local search libraries
-bitvector_lib = library('bitwuzlabv',
+bitvector_lib = static_library('bitwuzlabv',
sources: bv_sources,
link_whole: rng_lib,
dependencies: gmp_dep,
install_rpath: install_rpath,
install: true)
-bitblast_lib = library('bitwuzlabb',
+bitblast_lib = static_library('bitwuzlabb',
sources: bb_sources,
link_with: bitvector_lib,
dependencies: gmp_dep,
install_rpath: install_rpath,
install: true)
-local_search_lib = library('bitwuzlals',
+local_search_lib = static_library('bitwuzlals',
sources: ls_sources,
link_with: [bitvector_lib],
link_whole: rng_lib,
diff --git a/src/meson.build b/src/meson.build
index ddabc8e8..2260419b 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -9,7 +9,7 @@ cpp_compiler = meson.get_compiler('cpp')
gmp_dep = dependency('gmp',
version: '>=6.1',
required: true,
- static: build_static)
+ static: true)


# Subproject dependencies
@@ -195,15 +195,23 @@ endif
# Public header include directory
bitwuzla_inc = include_directories('../include', 'lib')

+
+link_args = []
+if host_machine.system() == 'windows'
+ link_args += ['-static-libstdc++', '-static-libgcc', '-Wl,-Bstatic', '-lstdc++', '-lpthread', '-Wl,-Bdynamic']
+endif
+
bitwuzla_lib = library('bitwuzla',
sources,
include_directories: bitwuzla_inc,
link_with: support_libs,
dependencies: dependencies,
- soversion: bitwuzla_lib_soversion,
+ #soversion: bitwuzla_lib_soversion,
install_rpath: install_rpath,
install: true,
- cpp_args: cpp_args)
+ cpp_args: cpp_args,
+ link_args: link_args
+ )

# Create library dependency
bitwuzla_dep = declare_dependency(include_directories: bitwuzla_inc,
Loading

0 comments on commit a3fc264

Please sign in to comment.