Skip to content

Commit 9aace27

Browse files
committed
bitwuzla: added build guide, native libs for windows, linux reassembled
1 parent 9785cfa commit 9aace27

11 files changed

+356
-47
lines changed

Diff for: ksmt-bitwuzla/bindings-native/CMakeLists.txt

+10-13
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,24 @@ PROJECT(bitwuzla_jni)
55
FIND_PACKAGE(JNI REQUIRED)
66

77
SET(CXX_STANDARD 17)
8+
SET(BITWUZLA_ROOT "${PROJECT_SOURCE_DIR}/bitwuzla")
89

9-
# Path to bitwuzla.h
10-
SET(BITWUZLA_INCLUDE "${PROJECT_SOURCE_DIR}/bitwuzla/include/bitwuzla/c")
11-
# Path to enums.h, option.h
12-
SET(BITWUZLA_INCLUDE2 "${PROJECT_SOURCE_DIR}/include")
10+
# Path to bitwuzla.h, enums.h, option.h
11+
SET(BITWUZLA_INCLUDE "${BITWUZLA_ROOT}/include")
1312

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

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

20-
ADD_LIBRARY (bitwuzla_jni SHARED bitwuzla_jni.cpp)
18+
message(${BITWUZLA_INCLUDE})
2119

2220
TARGET_INCLUDE_DIRECTORIES(bitwuzla_jni PRIVATE ${JNI_INCLUDE_DIRS})
21+
TARGET_INCLUDE_DIRECTORIES(bitwuzla_jni PRIVATE "./include"})
2322
TARGET_INCLUDE_DIRECTORIES(bitwuzla_jni PRIVATE ${BITWUZLA_INCLUDE})
24-
TARGET_INCLUDE_DIRECTORIES(bitwuzla_jni PRIVATE ${BITWUZLA_INCLUDE2})
25-
26-
SET(BITWUZLA_SRC_INCLUDE "${BITWUZLA_SRC_ROOT}/bitwuzla/src")
27-
TARGET_INCLUDE_DIRECTORIES(bitwuzla_jni PRIVATE ${BITWUZLA_SRC_INCLUDE})
2823

2924
SET(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
3025

26+
# For Windows build, comment the first line, and uncomment the second one (link statically with libstdc++, ligbcc, lpthreads)
3127
TARGET_LINK_LIBRARIES(bitwuzla_jni PRIVATE ${BITWUZLA_LIB})
28+
#TARGET_LINK_LIBRARIES(bitwuzla_jni PRIVATE ${BITWUZLA_LIB} -static-libstdc++ -static-libgcc -Wl,-Bstatic -lstdc++ -lpthread -Wl,-Bdynamic)

Diff for: ksmt-bitwuzla/bindings-native/CMakeLists_win64.txt

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
cmake_minimum_required (VERSION 3.8)
2+
3+
SET(CMAKE_SYSTEM_NAME "Windows")
4+
SET(CMAKE_C_COMPILER "gcc")
5+
SET(CMAKE_CXX_COMPILER "g++")
6+
SET(CMAKE_MAKE_PROGRAM "mingw32-make")
7+
8+
PROJECT(bitwuzla_jni)
9+
10+
11+
FIND_PACKAGE(JNI REQUIRED)
12+
13+
SET(CXX_STANDARD 17)
14+
#SET(PROJECT_SOURCE_DIR "/path/to/bitwuzla/root")
15+
16+
# Path to bitwuzla.h, enums.h, option.h
17+
SET(BITWUZLA_INCLUDE "${PROJECT_SOURCE_DIR}/include")
18+
19+
# Path to bitwuzla library (libbitwuzla.so)
20+
SET(BITWUZLA_LIB "${PROJECT_SOURCE_DIR}/build/src/${CMAKE_SHARED_LIBRARY_PREFIX}bitwuzla${CMAKE_SHARED_LIBRARY_SUFFIX}")
21+
22+
ADD_LIBRARY (bitwuzla_jni SHARED bitwuzla_jni.cpp bitwuzla_extension.cpp)
23+
24+
TARGET_INCLUDE_DIRECTORIES(bitwuzla_jni PRIVATE ${JNI_INCLUDE_DIRS})
25+
TARGET_INCLUDE_DIRECTORIES(bitwuzla_jni PRIVATE "./include"})
26+
TARGET_INCLUDE_DIRECTORIES(bitwuzla_jni PRIVATE ${BITWUZLA_INCLUDE})
27+
28+
SET(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
29+
30+
TARGET_LINK_LIBRARIES(bitwuzla_jni PRIVATE ${BITWUZLA_LIB})

Diff for: ksmt-bitwuzla/bindings-native/bitwuzla_extension.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include "bitwuzla/c/bitwuzla.h"
2+
#include <bitwuzla_extension.hpp>
3+
4+
5+
#if __cplusplus
6+
extern "C" {
7+
#endif
8+
9+
void bitwuzla_extension_sort_dec_ref(BitwuzlaSort sort_id) {
10+
bitwuzla_sort_dec_ref(sort_id);
11+
}
12+
13+
void bitwuzla_extension_term_dec_ref(BitwuzlaTerm term_id) {
14+
bitwuzla_term_dec_ref(term_id);
15+
}
16+
17+
uint64_t bitwuzla_extension_bv_value_uint64(BitwuzlaTerm term) {
18+
return bitwuzla_bv_value_uint64(term);
19+
}
20+
21+
const char *bitwuzla_extension_bv_value_str(BitwuzlaTerm term, uint32_t base) {
22+
return bitwuzla_bv_value_str(term, base);
23+
}
24+
25+
#if __cplusplus
26+
}
27+
#endif

Diff for: ksmt-bitwuzla/bindings-native/bitwuzla_jni.cpp

+23-13
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
#include <iostream>
22
#include <atomic>
3-
#include <bitwuzla.h>
4-
#include <memory>
5-
#include <unistd.h>
6-
#include <vector>
73
#include <chrono>
8-
#include "bitwuzla_jni.hpp"
4+
#include <bitwuzla/c/bitwuzla.h>
5+
#include <bitwuzla_jni.hpp>
6+
#include <bitwuzla_extension.hpp>
97

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

193191
void Java_org_ksmt_solver_bitwuzla_bindings_Native_bitwuzlaTermDecRef(JNIEnv *env, jclass native_class, jlong term) {
194192
BZLA_TRY_VOID({
195-
bitwuzla_term_dec_ref(TERM(term));
193+
bitwuzla_extension_term_dec_ref(TERM(term));
196194
})
197195
}
198196

199197
void Java_org_ksmt_solver_bitwuzla_bindings_Native_bitwuzlaSortDecRef(JNIEnv *env, jclass native_class, jlong sort) {
200198
BZLA_TRY_VOID({
201-
bitwuzla_sort_dec_ref(SORT(sort));
199+
bitwuzla_extension_sort_dec_ref(SORT(sort));
202200
})
203201
}
204202

@@ -828,6 +826,18 @@ jint Java_org_ksmt_solver_bitwuzla_bindings_Native_bitwuzlaCheckSatAssumingNativ
828826
})
829827
}
830828

829+
jint Java_org_ksmt_solver_bitwuzla_bindings_Native_bitwuzlaCheckSatTimeout(JNIEnv* env, jobject native_class, jlong bitwuzla, jlongArray args, jlong timeout) {
830+
BZLA_TRY_OR_ZERO({
831+
auto termination_state = get_termination_state(BZLA);
832+
ScopedTimeout _timeout(termination_state, timeout);
833+
834+
GET_PTR_ARRAY(BitwuzlaTerm, args_ptr, args);
835+
jsize len = env->GetArrayLength(args);
836+
837+
return (jint) bitwuzla_check_sat_assuming(BZLA, (uint32_t) len, args_ptr);
838+
})
839+
}
840+
831841
jlong Java_org_ksmt_solver_bitwuzla_bindings_Native_bitwuzlaGetValue(JNIEnv *env, jclass native_class, jlong bitwuzla, jlong term) {
832842
BZLA_TRY_OR_ZERO({
833843
return (jlong) bitwuzla_get_value(BZLA, TERM(term));
@@ -836,13 +846,13 @@ jlong Java_org_ksmt_solver_bitwuzla_bindings_Native_bitwuzlaGetValue(JNIEnv *env
836846

837847
jlong Java_org_ksmt_solver_bitwuzla_bindings_Native_bitwuzlaGetBvValueUInt64(JNIEnv *env, jclass native_class, jlong term) {
838848
BZLA_TRY_OR_ZERO({
839-
return (jlong) bitwuzla_term_value_get_bv_uint64(TERM(term));
849+
return (jlong) bitwuzla_extension_bv_value_uint64(TERM(term));
840850
})
841851
}
842852

843853
jstring Java_org_ksmt_solver_bitwuzla_bindings_Native_bitwuzlaGetBvValueString(JNIEnv *env, jclass native_class, jlong term) {
844854
BZLA_TRY_OR_ZERO({
845-
const char * str = bitwuzla_term_value_get_bv_str(TERM(term), 2);
855+
const char * str = bitwuzla_extension_bv_value_str(TERM(term), 2);
846856
return env->NewStringUTF(str);
847857
})
848858
}
@@ -1119,16 +1129,16 @@ jlong Java_org_ksmt_solver_bitwuzla_bindings_Native_bitwuzlaMkVar(JNIEnv *env, j
11191129
})
11201130
}
11211131

1122-
jlong Java_org_ksmt_solver_bitwuzla_bindings_Native_bitwuzlaSubstituteTerm(JNIEnv *env, jclass native_class, jlong bitwuzla, jlong term, jlongArray mapKeys, jlongArray mapValues) {
1132+
jlong Java_org_ksmt_solver_bitwuzla_bindings_Native_bitwuzlaSubstituteTerm(JNIEnv *env, jclass native_class, jlong term, jlongArray mapKeys, jlongArray mapValues) {
11231133
BZLA_TRY_OR_ZERO({
11241134
GET_PTR_ARRAY(BitwuzlaTerm, mapKeys_ptr, mapKeys);
11251135
GET_PTR_ARRAY(BitwuzlaTerm, mapValues_ptr, mapValues);
11261136
jsize map_size = env->GetArrayLength(mapKeys);
1127-
return (jlong) bitwuzla_substitute_term(BZLA, TERM(term), map_size, mapKeys_ptr, mapValues_ptr);
1137+
return (jlong) bitwuzla_substitute_term(TERM(term), map_size, mapKeys_ptr, mapValues_ptr);
11281138
})
11291139
}
11301140

1131-
void Java_org_ksmt_solver_bitwuzla_bindings_Native_bitwuzlaSubstituteTerms(JNIEnv *env, jclass native_class, jlong bitwuzla, jlongArray terms, jlongArray mapKeys, jlongArray mapValues) {
1141+
void Java_org_ksmt_solver_bitwuzla_bindings_Native_bitwuzlaSubstituteTerms(JNIEnv *env, jclass native_class, jlongArray terms, jlongArray mapKeys, jlongArray mapValues) {
11321142
BZLA_TRY_VOID({
11331143
GET_PTR_ARRAY(BitwuzlaTerm, terms_ptr, terms);
11341144
jsize terms_size = env->GetArrayLength(terms);
@@ -1137,6 +1147,6 @@ void Java_org_ksmt_solver_bitwuzla_bindings_Native_bitwuzlaSubstituteTerms(JNIEn
11371147
GET_PTR_ARRAY(BitwuzlaTerm, mapValues_ptr, mapValues);
11381148
jsize map_size = env->GetArrayLength(mapKeys);
11391149

1140-
bitwuzla_substitute_terms(BZLA, (size_t) terms_size, terms_ptr, map_size, mapKeys_ptr, mapValues_ptr);
1150+
bitwuzla_substitute_terms((size_t) terms_size, terms_ptr, map_size, mapKeys_ptr, mapValues_ptr);
11411151
})
11421152
}

Diff for: ksmt-bitwuzla/bindings-native/bitwuzla_patch.patch

+159
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
diff --git a/include/bitwuzla/c/bitwuzla.h b/include/bitwuzla/c/bitwuzla.h
2+
index 2870e868..007f9d58 100644
3+
--- a/include/bitwuzla/c/bitwuzla.h
4+
+++ b/include/bitwuzla/c/bitwuzla.h
5+
@@ -2007,6 +2007,14 @@ void bitwuzla_substitute_terms(size_t terms_size,
6+
7+
/** @} */
8+
9+
+void bitwuzla_sort_dec_ref(BitwuzlaSort sort_id);
10+
+
11+
+void bitwuzla_term_dec_ref(BitwuzlaTerm term_id);
12+
+
13+
+uint64_t bitwuzla_bv_value_uint64(BitwuzlaTerm term);
14+
+
15+
+const char *bitwuzla_bv_value_str(BitwuzlaTerm term, uint32_t base);
16+
+
17+
#if __cplusplus
18+
}
19+
#endif
20+
diff --git a/src/api/c/bitwuzla.cpp b/src/api/c/bitwuzla.cpp
21+
index ece639cc..5c3175f7 100644
22+
--- a/src/api/c/bitwuzla.cpp
23+
+++ b/src/api/c/bitwuzla.cpp
24+
@@ -2069,3 +2069,56 @@ bitwuzla_term_print_fmt(BitwuzlaTerm term, FILE *file, uint8_t base)
25+
}
26+
27+
/* -------------------------------------------------------------------------- */
28+
+
29+
+#include "node/node.h"
30+
+#include "bv/bitvector.h"
31+
+
32+
+namespace bitwuzla {
33+
+ template<>
34+
+ bzla::BitVector
35+
+ Term::value(uint8_t base) const {
36+
+ (void) base;
37+
+ BITWUZLA_CHECK_NOT_NULL(d_node);
38+
+ BITWUZLA_CHECK_TERM_IS_BV_VALUE(*this);
39+
+ return d_node->value<bzla::BitVector>();
40+
+ }
41+
+}
42+
+
43+
+void bitwuzla_sort_dec_ref(BitwuzlaSort sort_id) {
44+
+ Bitwuzla::SortMap &sort_map = Bitwuzla::sort_map();
45+
+ const auto it = sort_map.find(sort_id);
46+
+ it->second.second -= 1;
47+
+ if (it->second.second == 0) {
48+
+ it->second.first.reset();
49+
+ sort_map.erase(it);
50+
+ }
51+
+}
52+
+
53+
+void bitwuzla_term_dec_ref(BitwuzlaTerm term_id) {
54+
+ Bitwuzla::TermMap &term_map = Bitwuzla::term_map();
55+
+ const auto it = term_map.find(term_id);
56+
+ it->second.second -= 1;
57+
+ if (it->second.second == 0) {
58+
+ it->second.first.reset();
59+
+ term_map.erase(it);
60+
+ }
61+
+}
62+
+
63+
+uint64_t bitwuzla_bv_value_uint64(BitwuzlaTerm term) {
64+
+ uint64_t res = false;
65+
+ BITWUZLA_TRY_CATCH_BEGIN;
66+
+ BITWUZLA_CHECK_TERM_ID(term);
67+
+ bzla::BitVector bv = import_term(term).value<bzla::BitVector>();
68+
+ res = bv.to_uint64();
69+
+ BITWUZLA_TRY_CATCH_END;
70+
+ return res;
71+
+}
72+
+
73+
+const char *bitwuzla_bv_value_str(BitwuzlaTerm term, uint32_t base) {
74+
+ static thread_local std::string res;
75+
+ BITWUZLA_TRY_CATCH_BEGIN;
76+
+ BITWUZLA_CHECK_TERM_ID(term);
77+
+ res = import_term(term).value<bzla::BitVector>().str(base);
78+
+ BITWUZLA_TRY_CATCH_END;
79+
+ return res.c_str();
80+
+}
81+
diff --git a/src/api/c/checks.h b/src/api/c/checks.h
82+
index 63a26fff..60be3ed8 100644
83+
--- a/src/api/c/checks.h
84+
+++ b/src/api/c/checks.h
85+
@@ -47,7 +47,7 @@ class BitwuzlaAbortStream
86+
{
87+
stream() << msg_prefix << " ";
88+
}
89+
- ~BitwuzlaAbortStream()
90+
+ ~BitwuzlaAbortStream() noexcept(false)
91+
{
92+
flush();
93+
bitwuzla_abort(d_stream.str().c_str());
94+
diff --git a/src/lib/meson.build b/src/lib/meson.build
95+
index 7380e24c..38fcb022 100644
96+
--- a/src/lib/meson.build
97+
+++ b/src/lib/meson.build
98+
@@ -32,19 +32,19 @@ rng_lib = static_library('bzlarng',
99+
dependencies: gmp_dep)
100+
101+
# Bitwuzla bit-vector, bit-blast and local search libraries
102+
-bitvector_lib = library('bitwuzlabv',
103+
+bitvector_lib = static_library('bitwuzlabv',
104+
sources: bv_sources,
105+
link_whole: rng_lib,
106+
dependencies: gmp_dep,
107+
install_rpath: install_rpath,
108+
install: true)
109+
-bitblast_lib = library('bitwuzlabb',
110+
+bitblast_lib = static_library('bitwuzlabb',
111+
sources: bb_sources,
112+
link_with: bitvector_lib,
113+
dependencies: gmp_dep,
114+
install_rpath: install_rpath,
115+
install: true)
116+
-local_search_lib = library('bitwuzlals',
117+
+local_search_lib = static_library('bitwuzlals',
118+
sources: ls_sources,
119+
link_with: [bitvector_lib],
120+
link_whole: rng_lib,
121+
diff --git a/src/meson.build b/src/meson.build
122+
index ddabc8e8..2260419b 100644
123+
--- a/src/meson.build
124+
+++ b/src/meson.build
125+
@@ -9,7 +9,7 @@ cpp_compiler = meson.get_compiler('cpp')
126+
gmp_dep = dependency('gmp',
127+
version: '>=6.1',
128+
required: true,
129+
- static: build_static)
130+
+ static: true)
131+
132+
133+
# Subproject dependencies
134+
@@ -195,15 +195,23 @@ endif
135+
# Public header include directory
136+
bitwuzla_inc = include_directories('../include', 'lib')
137+
138+
+
139+
+link_args = []
140+
+if host_machine.system() == 'windows'
141+
+ link_args += ['-static-libstdc++', '-static-libgcc', '-Wl,-Bstatic', '-lstdc++', '-lpthread', '-Wl,-Bdynamic']
142+
+endif
143+
+
144+
bitwuzla_lib = library('bitwuzla',
145+
sources,
146+
include_directories: bitwuzla_inc,
147+
link_with: support_libs,
148+
dependencies: dependencies,
149+
- soversion: bitwuzla_lib_soversion,
150+
+ #soversion: bitwuzla_lib_soversion,
151+
install_rpath: install_rpath,
152+
install: true,
153+
- cpp_args: cpp_args)
154+
+ cpp_args: cpp_args,
155+
+ link_args: link_args
156+
+ )
157+
158+
# Create library dependency
159+
bitwuzla_dep = declare_dependency(include_directories: bitwuzla_inc,

0 commit comments

Comments
 (0)