Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CMake fixes #296

Merged
merged 6 commits into from
Aug 31, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
[submodule "external/doctest/src"]
path = external/doctest/src
url = https://github.com/onqtam/doctest.git
[submodule "external/lifting-tools-ci"]
path = external/lifting-tools-ci
url = https://github.com/lifting-bits/lifting-tools-ci.git
13 changes: 4 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -16,7 +16,6 @@ set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_C_VISIBILITY_PRESET hidden)
set (VISIBILITY_INLINES_HIDDEN YES)
include("cmake/settings.cmake")
include("cmake/llvm.cmake")
include(GNUInstallDirs)

if(RELLIC_ENABLE_INSTALL)
@@ -51,7 +50,9 @@ endif(NOT DEFINED WIN32)
find_package(gflags CONFIG REQUIRED)
find_package(glog CONFIG REQUIRED)
find_package(Z3 4.8 CONFIG REQUIRED)
find_llvm(llvm)
find_package(doctest CONFIG REQUIRED)
find_package(LLVM CONFIG REQUIRED)
llvm_map_components_to_libnames(llvm_libs support core irreader bitreader bitwriter)
find_package(Clang CONFIG REQUIRED)


@@ -81,18 +82,12 @@ if(RELLIC_ENABLE_INSTALL)
)
install(
TARGETS
"llvm" "${PROJECT_NAME}_cxx_settings"
"${PROJECT_NAME}_cxx_settings"
EXPORT
"${PROJECT_NAME}Targets"
)
endif(RELLIC_ENABLE_INSTALL)

#
# external libraries
#

add_subdirectory(external)

#
# rellic libraries
#
71 changes: 62 additions & 9 deletions cmake/git_watcher.cmake
Original file line number Diff line number Diff line change
@@ -27,6 +27,15 @@
# -- The path to the git executable. It'll automatically be set if the
# user doesn't supply a path.
#
# GIT_FAIL_IF_NONZERO_EXIT (OPTIONAL)
# -- Raise a FATAL_ERROR if any of the git commands return a non-zero
# exit code. This is set to TRUE by default. You can set this to FALSE
# if you'd like the build to continue even if a git command fails.
#
# GIT_IGNORE_UNTRACKED (OPTIONAL)
# -- Ignore the presence of untracked files when detecting if the
# working tree is dirty. This is set to FALSE by default.
#
# DESIGN
# - This script was designed similar to a Python application
# with a Main() function. I wanted to keep it compact to
@@ -57,17 +66,25 @@ macro(CHECK_REQUIRED_VARIABLE var_name)
endmacro()

# Check that an optional variable is set, or, set it to a default value.
macro(CHECK_OPTIONAL_VARIABLE var_name default_value)
macro(CHECK_OPTIONAL_VARIABLE_NOPATH var_name default_value)
if(NOT DEFINED ${var_name})
set(${var_name} ${default_value})
endif()
endmacro()

# Check that an optional variable is set, or, set it to a default value.
# Also converts that path to an abspath.
macro(CHECK_OPTIONAL_VARIABLE var_name default_value)
CHECK_OPTIONAL_VARIABLE_NOPATH(${var_name} ${default_value})
PATH_TO_ABSOLUTE(${var_name})
endmacro()

CHECK_REQUIRED_VARIABLE(PRE_CONFIGURE_FILE)
CHECK_REQUIRED_VARIABLE(POST_CONFIGURE_FILE)
CHECK_OPTIONAL_VARIABLE(GIT_STATE_FILE "${CMAKE_BINARY_DIR}/git-state-hash")
CHECK_OPTIONAL_VARIABLE(GIT_STATE_FILE "${CMAKE_CURRENT_BINARY_DIR}/git-state-hash")
CHECK_OPTIONAL_VARIABLE(GIT_WORKING_DIR "${CMAKE_SOURCE_DIR}")
CHECK_OPTIONAL_VARIABLE_NOPATH(GIT_FAIL_IF_NONZERO_EXIT TRUE)
CHECK_OPTIONAL_VARIABLE_NOPATH(GIT_IGNORE_UNTRACKED FALSE)

# Check the optional git variable.
# If it's not set, we'll try to find it using the CMake packaging system.
@@ -87,6 +104,7 @@ set(_state_variable_names
GIT_COMMIT_SUBJECT
GIT_COMMIT_BODY
GIT_DESCRIBE
GIT_BRANCH
# >>>
# 1. Add the name of the additional git variable you're interested in monitoring
# to this list.
@@ -96,17 +114,30 @@ set(_state_variable_names

# Macro: RunGitCommand
# Description: short-hand macro for calling a git function. Outputs are the
# "exit_code" and "output" variables.
# "exit_code" and "output" variables. The "_permit_git_failure"
# variable can locally override the exit code checking- use it
# with caution.
macro(RunGitCommand)
execute_process(COMMAND
"${GIT_EXECUTABLE}" ${ARGV}
WORKING_DIRECTORY "${_working_dir}"
RESULT_VARIABLE exit_code
OUTPUT_VARIABLE output
ERROR_QUIET
ERROR_VARIABLE stderr
OUTPUT_STRIP_TRAILING_WHITESPACE)
if(NOT exit_code EQUAL 0)
if(NOT exit_code EQUAL 0 AND NOT _permit_git_failure)
set(ENV{GIT_RETRIEVED_STATE} "false")

# Issue 26: git info not properly set
#
# Check if we should fail if any of the exit codes are non-zero.
# Most methods have a fall-back default value that's used in case of non-zero
# exit codes. If you're feeling risky, disable this safety check and use
# those default values.
if(GIT_FAIL_IF_NONZERO_EXIT )
string(REPLACE ";" " " args_with_spaces "${ARGV}")
message(FATAL_ERROR "${stderr} (${GIT_EXECUTABLE} ${args_with_spaces})")
endif()
endif()
endmacro()

@@ -123,7 +154,12 @@ function(GetGitState _working_dir)
set(ENV{GIT_RETRIEVED_STATE} "true")

# Get whether or not the working tree is dirty.
RunGitCommand(status --porcelain)
if (GIT_IGNORE_UNTRACKED)
set(untracked_flag "-uno")
else()
set(untracked_flag "-unormal")
endif()
RunGitCommand(status --porcelain ${untracked_flag})
if(NOT exit_code EQUAL 0)
set(ENV{GIT_IS_DIRTY} "false")
else()
@@ -158,6 +194,8 @@ function(GetGitState _working_dir)

RunGitCommand(show -s "--format=%s" ${object})
if(exit_code EQUAL 0)
# Escape \
string(REPLACE "\\" "\\\\" output "${output}")
# Escape quotes
string(REPLACE "\"" "\\\"" output "${output}")
set(ENV{GIT_COMMIT_SUBJECT} "${output}")
@@ -166,6 +204,8 @@ function(GetGitState _working_dir)
RunGitCommand(show -s "--format=%b" ${object})
if(exit_code EQUAL 0)
if(output)
# Escape \
string(REPLACE "\\" "\\\\" output "${output}")
# Escape quotes
string(REPLACE "\"" "\\\"" output "${output}")
# Escape line breaks in the commit message.
@@ -178,9 +218,9 @@ function(GetGitState _working_dir)
# There was no commit body - set the safe string to empty.
set(safe "")
endif()
set(ENV{GIT_COMMIT_BODY} "\"${safe}\"")
set(ENV{GIT_COMMIT_BODY} "${safe}")
else()
set(ENV{GIT_COMMIT_BODY} "\"\"") # empty string.
set(ENV{GIT_COMMIT_BODY} "") # empty string.
endif()

# Get output of git describe
@@ -191,6 +231,17 @@ function(GetGitState _working_dir)
set(ENV{GIT_DESCRIBE} "${output}")
endif()

# Convert HEAD to a symbolic ref. This can fail, in which case we just
# set that variable to HEAD.
set(_permit_git_failure ON)
RunGitCommand(symbolic-ref --short -q ${object})
unset(_permit_git_failure)
if(NOT exit_code EQUAL 0)
set(ENV{GIT_BRANCH} "${object}")
else()
set(ENV{GIT_BRANCH} "${output}")
endif()

# >>>
# 2. Additional git properties can be added here via the
# "execute_process()" command. Be sure to set them in
@@ -288,6 +339,8 @@ function(SetupGitMonitoring)
-DGIT_STATE_FILE=${GIT_STATE_FILE}
-DPRE_CONFIGURE_FILE=${PRE_CONFIGURE_FILE}
-DPOST_CONFIGURE_FILE=${POST_CONFIGURE_FILE}
-DGIT_FAIL_IF_NONZERO_EXIT=${GIT_FAIL_IF_NONZERO_EXIT}
-DGIT_IGNORE_UNTRACKED=${GIT_IGNORE_UNTRACKED}
-P "${CMAKE_CURRENT_LIST_FILE}")
endfunction()

@@ -311,4 +364,4 @@ function(Main)
endfunction()

# And off we go...
Main()
Main()
37 changes: 0 additions & 37 deletions cmake/llvm.cmake

This file was deleted.

52 changes: 31 additions & 21 deletions cmake/modules/FindZ3.cmake
Original file line number Diff line number Diff line change
@@ -3,28 +3,47 @@ INCLUDE(CheckCXXSourceRuns)
# Function to check Z3's version
function(check_z3_version z3_include z3_lib)
# Get lib path
get_filename_component(z3_lib_path ${z3_lib} PATH)
set(z3_link_libs "${z3_lib}")

# Try to find a threading module in case Z3 was built with threading support.
# Threads are required elsewhere in LLVM, but not marked as required here because
# Z3 could have been compiled without threading support.
find_package(Threads)
set(z3_link_libs "-lz3" "${CMAKE_THREAD_LIBS_INIT}")
# CMAKE_THREAD_LIBS_INIT may be empty if the thread functions are provided by the
# system libraries and no special flags are needed.
if(CMAKE_THREAD_LIBS_INIT)
list(APPEND z3_link_libs "${CMAKE_THREAD_LIBS_INIT}")
endif()

# The program that will be executed to print Z3's version.
file(WRITE ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/testz3.cpp
"#include <assert.h>
#include <z3.h>
int main() {
unsigned int major, minor, build, rev;
Z3_get_version(&major, &minor, &build, &rev);
printf(\"%u.%u.%u\", major, minor, build);
return 0;
}")

try_run(
Z3_RETURNCODE
Z3_COMPILED
${CMAKE_BINARY_DIR}
${CMAKE_SOURCE_DIR}/cmake/modules/testz3.cpp
${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/testz3.cpp
COMPILE_DEFINITIONS -I"${z3_include}"
LINK_LIBRARIES -L${z3_lib_path} ${z3_link_libs}
LINK_LIBRARIES ${z3_link_libs}
COMPILE_OUTPUT_VARIABLE COMPILE_OUTPUT
RUN_OUTPUT_VARIABLE SRC_OUTPUT
)

if(Z3_COMPILED)
string(REGEX REPLACE "([0-9]*\\.[0-9]*\\.[0-9]*)" "\\1"
z3_version "${SRC_OUTPUT}")
set(Z3_VERSION_STRING ${z3_version} PARENT_SCOPE)
else()
message(NOTICE "${COMPILE_OUTPUT}")
message(WARNING "Failed to compile Z3 program that is used to determine library version.")
endif()
endfunction(check_z3_version)

@@ -35,7 +54,7 @@ find_path(Z3_INCLUDE_DIR NAMES z3.h
PATH_SUFFIXES libz3 z3
)

find_library(Z3_LIBS NAMES z3 libz3
find_library(Z3_LIBRARIES NAMES z3 libz3
NO_DEFAULT_PATH
PATHS ${LLVM_Z3_INSTALL_DIR}
PATH_SUFFIXES lib bin
@@ -46,7 +65,7 @@ find_path(Z3_INCLUDE_DIR NAMES z3.h
PATH_SUFFIXES libz3 z3
)

find_library(Z3_LIBS NAMES z3 libz3
find_library(Z3_LIBRARIES NAMES z3 libz3
PATH_SUFFIXES lib bin
)

@@ -55,10 +74,10 @@ unset(Z3_VERSION_STRING)

# First, try to check it dynamically, by compiling a small program that
# prints Z3's version
if(Z3_INCLUDE_DIR AND Z3_LIBS)
if(Z3_INCLUDE_DIR AND Z3_LIBRARIES)
# We do not have the Z3 binary to query for a version. Try to use
# a small C++ program to detect it via the Z3_get_version() API call.
check_z3_version(${Z3_INCLUDE_DIR} ${Z3_LIBS})
check_z3_version(${Z3_INCLUDE_DIR} ${Z3_LIBRARIES})
endif()

# If the dynamic check fails, we might be cross compiling: if that's the case,
@@ -93,23 +112,14 @@ if(NOT Z3_VERSION_STRING)
# conservative and force the found version to 0.0.0 to make version
# checks always fail.
set(Z3_VERSION_STRING "0.0.0")
message(WARNING "Failed to determine Z3 library version, defaulting to 0.0.0.")
endif()

# handle the QUIETLY and REQUIRED arguments and set Z3_FOUND to TRUE if
# all listed variables are TRUE
include(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(Z3
REQUIRED_VARS Z3_LIBS Z3_INCLUDE_DIR
REQUIRED_VARS Z3_LIBRARIES Z3_INCLUDE_DIR
VERSION_VAR Z3_VERSION_STRING)
if(Z3_FOUND AND NOT TARGET Z3)
add_library(Z3 UNKNOWN IMPORTED)
set_target_properties(Z3 PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${Z3_INCLUDE_DIR}"
IMPORTED_LOCATION "${Z3_LIBS}"
VISIBILITY_INLINES_HIDDEN YES
CXX_VISIBILITY_PRESET hidden
)
)
set(Z3_LIBRARIES Z3)
endif()
mark_as_advanced(Z3_INCLUDE_DIR Z3_LIBS Z3_LIBRARIES)

mark_as_advanced(Z3_INCLUDE_DIR Z3_LIBRARIES)
8 changes: 0 additions & 8 deletions cmake/modules/testz3.cpp

This file was deleted.

9 changes: 0 additions & 9 deletions external/CMakeLists.txt

This file was deleted.

12 changes: 0 additions & 12 deletions external/doctest/CMakeLists.txt

This file was deleted.

1 change: 0 additions & 1 deletion external/doctest/src
Submodule src deleted from 4d8716
2 changes: 1 addition & 1 deletion lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -113,7 +113,7 @@ target_link_libraries("${PROJECT_NAME}"
"${PROJECT_NAME}_cxx_settings"
glog::glog
z3::libz3
llvm
"${llvm_libs}"
clangIndex
clangCodeGen
clangASTMatchers
2 changes: 1 addition & 1 deletion lib/Version.cpp.in
Original file line number Diff line number Diff line change
@@ -50,7 +50,7 @@ namespace Version {
return "@GIT_COMMIT_SUBJECT@";
}
std::string GetCommitBody() {
return @GIT_COMMIT_BODY@;
return "@GIT_COMMIT_BODY@";
}
std::string GetVersionString() {
return "@VERSION_STRING@";
2 changes: 1 addition & 1 deletion unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ target_link_libraries(${RELLIC_UNITTEST} PRIVATE
"${PROJECT_NAME}_cxx_settings"
"${PROJECT_NAME}"
z3::libz3
thirdparty_doctest
doctest::doctest
)

target_compile_options(${RELLIC_UNITTEST} PRIVATE -fexceptions)