1 INCLUDE(CheckCXXSourceRuns)
3 # Function to check Z3's version
4 function(check_z3_version z3_include z3_lib)
6 set(z3_link_libs "${z3_lib}")
8 # Try to find a threading module in case Z3 was built with threading support.
9 # Threads are required elsewhere in LLVM, but not marked as required here because
10 # Z3 could have been compiled without threading support.
12 # CMAKE_THREAD_LIBS_INIT may be empty if the thread functions are provided by the
13 # system libraries and no special flags are needed.
14 if(CMAKE_THREAD_LIBS_INIT)
15 list(APPEND z3_link_libs "${CMAKE_THREAD_LIBS_INIT}")
18 # The program that will be executed to print Z3's version.
19 file(WRITE ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/testz3.cpp
23 unsigned int major, minor, build, rev;
24 Z3_get_version(&major, &minor, &build, &rev);
25 printf(\"%u.%u.%u\", major, minor, build);
33 ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/testz3.cpp
34 COMPILE_DEFINITIONS -I"${z3_include}"
35 LINK_LIBRARIES ${z3_link_libs}
36 COMPILE_OUTPUT_VARIABLE COMPILE_OUTPUT
37 RUN_OUTPUT_VARIABLE SRC_OUTPUT
41 string(REGEX REPLACE "([0-9]*\\.[0-9]*\\.[0-9]*)" "\\1"
42 z3_version "${SRC_OUTPUT}")
43 set(Z3_VERSION_STRING ${z3_version} PARENT_SCOPE)
45 message(NOTICE "${COMPILE_OUTPUT}")
46 message(WARNING "Failed to compile Z3 program that is used to determine library version.")
48 endfunction(check_z3_version)
50 # Looking for Z3 in LLVM_Z3_INSTALL_DIR
51 find_path(Z3_INCLUDE_DIR NAMES z3.h
53 PATHS ${LLVM_Z3_INSTALL_DIR}/include
54 PATH_SUFFIXES libz3 z3
57 find_library(Z3_LIBRARIES NAMES z3 libz3
59 PATHS ${LLVM_Z3_INSTALL_DIR}
63 # If Z3 has not been found in LLVM_Z3_INSTALL_DIR look in the default directories
64 find_path(Z3_INCLUDE_DIR NAMES z3.h
65 PATH_SUFFIXES libz3 z3
68 find_library(Z3_LIBRARIES NAMES z3 libz3
72 # Searching for the version of the Z3 library is a best-effort task
73 unset(Z3_VERSION_STRING)
75 # First, try to check it dynamically, by compiling a small program that
77 if(Z3_INCLUDE_DIR AND Z3_LIBRARIES)
78 # We do not have the Z3 binary to query for a version. Try to use
79 # a small C++ program to detect it via the Z3_get_version() API call.
80 check_z3_version(${Z3_INCLUDE_DIR} ${Z3_LIBRARIES})
83 # If the dynamic check fails, we might be cross compiling: if that's the case,
84 # check the version in the headers, otherwise, fail with a message
85 if(NOT Z3_VERSION_STRING AND (CMAKE_CROSSCOMPILING AND
87 EXISTS "${Z3_INCLUDE_DIR}/z3_version.h"))
88 # TODO: print message warning that we couldn't find a compatible lib?
90 # Z3 4.8.1+ has the version is in a public header.
91 file(STRINGS "${Z3_INCLUDE_DIR}/z3_version.h"
92 z3_version_str REGEX "^#define[\t ]+Z3_MAJOR_VERSION[\t ]+.*")
93 string(REGEX REPLACE "^.*Z3_MAJOR_VERSION[\t ]+([0-9]).*$" "\\1"
94 Z3_MAJOR "${z3_version_str}")
96 file(STRINGS "${Z3_INCLUDE_DIR}/z3_version.h"
97 z3_version_str REGEX "^#define[\t ]+Z3_MINOR_VERSION[\t ]+.*")
98 string(REGEX REPLACE "^.*Z3_MINOR_VERSION[\t ]+([0-9]).*$" "\\1"
99 Z3_MINOR "${z3_version_str}")
101 file(STRINGS "${Z3_INCLUDE_DIR}/z3_version.h"
102 z3_version_str REGEX "^#define[\t ]+Z3_BUILD_NUMBER[\t ]+.*")
103 string(REGEX REPLACE "^.*Z3_BUILD_NUMBER[\t ]+([0-9]).*$" "\\1"
104 Z3_BUILD "${z3_version_str}")
106 set(Z3_VERSION_STRING ${Z3_MAJOR}.${Z3_MINOR}.${Z3_BUILD})
107 unset(z3_version_str)
110 if(NOT Z3_VERSION_STRING)
111 # Give up: we are unable to obtain a version of the Z3 library. Be
112 # conservative and force the found version to 0.0.0 to make version
113 # checks always fail.
114 set(Z3_VERSION_STRING "0.0.0")
115 message(WARNING "Failed to determine Z3 library version, defaulting to 0.0.0.")
118 # handle the QUIETLY and REQUIRED arguments and set Z3_FOUND to TRUE if
119 # all listed variables are TRUE
120 include(FindPackageHandleStandardArgs)
121 FIND_PACKAGE_HANDLE_STANDARD_ARGS(Z3
122 REQUIRED_VARS Z3_LIBRARIES Z3_INCLUDE_DIR
123 VERSION_VAR Z3_VERSION_STRING)
125 mark_as_advanced(Z3_INCLUDE_DIR Z3_LIBRARIES)