Skip to content

Commit

Permalink
Installing SVF now places includes in svf/... subdirectories, as is t…
Browse files Browse the repository at this point in the history
…he standard
  • Loading branch information
Johanmyst committed May 7, 2024
1 parent 4fbb19c commit 639da85
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 120 deletions.
136 changes: 49 additions & 87 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@ project(
VERSION 3.0
DESCRIPTION "SVF is a static value-flow analysis tool for LLVM-based languages"
HOMEPAGE_URL "https://github.com/SVF-tools/SVF"
LANGUAGES C CXX
)
LANGUAGES C CXX)

#################################################################################
# #
# Base configuration #
# #
#################################################################################
# =================================================================================
# Base configuration
# =================================================================================

# Ensure installation directories like ${CMAKE_INSTALL_LIBDIR} are available
include(GNUInstallDirs)
Expand All @@ -27,17 +24,14 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION OFF)

#################################################################################
# #
# SVF options & settings #
# #
#################################################################################
# =================================================================================
# SVF options & settings
# =================================================================================

# Configurable (string) options for building SVF
set(SVF_SANITIZE
""
CACHE STRING "Create sanitizer build (address)"
)
CACHE STRING "Create sanitizer build (address)")

# Configurable (boolean) options for building SVF
option(SVF_COVERAGE "Create coverage build")
Expand All @@ -59,49 +53,39 @@ set(SVF_LLVM_INCLUDES ${SVF_LLVM_ROOT}/include)
# Set the installation tree structure
set(SVF_INSTALL_BINDIR ${CMAKE_INSTALL_BINDIR})
set(SVF_INSTALL_LIBDIR ${CMAKE_INSTALL_LIBDIR})
set(SVF_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_INCLUDEDIR})
set(SVF_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_INCLUDEDIR}/svf)

# Set where extapi.bc is compiled and installed to
set(SVF_EXTAPI_OUT_NAME extapi.bc)
set(SVF_EXTAPI_INSTALL_DIR ${CMAKE_INSTALL_LIBDIR})
set(SVF_EXTAPI_INSTALL_FILE ${SVF_EXTAPI_INSTALL_DIR}/${SVF_EXTAPI_OUT_NAME})

message(
STATUS
"Building ${PROJECT_NAME} with configuration:
message(STATUS "Building ${PROJECT_NAME} with configuration:
SVF version: ${SVF_VERSION}
SVF root directory: ${SVF_SOURCE_DIR}
SVF option - create sanitiser build (str): ${SVF_SANITIZE}
SVF option - coverage build: ${SVF_COVERAGE}
SVF option - warnings as errors: ${SVF_WARN_AS_ERROR}
SVF option - unused dynamic symbols: ${SVF_EXPORT_DYNAMIC}
SVF option - enable build assertions: ${SVF_ENABLE_ASSERTIONS}
SVF option - build opaque pointers: ${SVF_ENABLE_OPAQUE_POINTERS}"
)
SVF option - build opaque pointers: ${SVF_ENABLE_OPAQUE_POINTERS}")

message(
STATUS
"Using CMake build configuration:
message(STATUS "Using CMake build configuration:
CMake generator: ${CMAKE_GENERATOR}
CMake C compiler: ${CMAKE_C_COMPILER_ID}
CMake C++ compiler: ${CMAKE_CXX_COMPILER_ID}
CMake current list directory: ${CMAKE_CURRENT_LIST_DIR}
CMake current source directory: ${CMAKE_CURRENT_SOURCE_DIR}
CMake current binary directory: ${CMAKE_CURRENT_BINARY_DIR}
CMake install directory prefix: ${CMAKE_INSTALL_PREFIX}"
)
CMake install directory prefix: ${CMAKE_INSTALL_PREFIX}")

#################################################################################
# #
# Build configuration/settings #
# #
#################################################################################
# =================================================================================
# Build configuration/settings
# =================================================================================

# By default, build SVF and its targets treating all compiler warnings as errors (except deprecations)
add_compile_options(
"$<$<BOOL:${SVF_WARN_AS_ERROR}>:-Wall>" "$<$<BOOL:${SVF_WARN_AS_ERROR}>:-Werror>"
"$<$<BOOL:${SVF_WARN_AS_ERROR}>:-Wno-deprecated-declarations>"
)
add_compile_options("$<$<BOOL:${SVF_WARN_AS_ERROR}>:-Wall>" "$<$<BOOL:${SVF_WARN_AS_ERROR}>:-Werror>"
"$<$<BOOL:${SVF_WARN_AS_ERROR}>:-Wno-deprecated-declarations>")

# Keep assertions enabled if requested
add_compile_options("$<$<BOOL:${SVF_ENABLE_ASSERTIONS}>:-UNDEBUG>")
Expand All @@ -110,14 +94,10 @@ add_compile_options("$<$<BOOL:${SVF_ENABLE_ASSERTIONS}>:-UNDEBUG>")
add_compile_options("$<$<BOOL:${SVF_EXPORT_DYNAMIC}>:-rdynamic>")

# Configure whether a coverage build should be created for SVF (i.e. add runtime instrumentation)
add_compile_options(
"$<$<OR:$<BOOL:${SVF_COVERAGE}>,$<BOOL:$ENV{SVF_COVERAGE}>>:-fprofile-arcs>"
"$<$<OR:$<BOOL:${SVF_COVERAGE}>,$<BOOL:$ENV{SVF_COVERAGE}>>:-ftest-coverage>"
)
add_link_options(
"$<$<OR:$<BOOL:${SVF_COVERAGE}>,$<BOOL:$ENV{SVF_COVERAGE}>>:-fprofile-arcs>"
"$<$<OR:$<BOOL:${SVF_COVERAGE}>,$<BOOL:$ENV{SVF_COVERAGE}>>:-ftest-coverage>"
)
add_compile_options("$<$<OR:$<BOOL:${SVF_COVERAGE}>,$<BOOL:$ENV{SVF_COVERAGE}>>:-fprofile-arcs>"
"$<$<OR:$<BOOL:${SVF_COVERAGE}>,$<BOOL:$ENV{SVF_COVERAGE}>>:-ftest-coverage>")
add_link_options("$<$<OR:$<BOOL:${SVF_COVERAGE}>,$<BOOL:$ENV{SVF_COVERAGE}>>:-fprofile-arcs>"
"$<$<OR:$<BOOL:${SVF_COVERAGE}>,$<BOOL:$ENV{SVF_COVERAGE}>>:-ftest-coverage>")

# If building with sanitiser, load the given sanitiser mode
if(SVF_SANITIZE STREQUAL "address")
Expand All @@ -132,40 +112,33 @@ elseif(NOT SVF_SANITIZE STREQUAL "")
message(FATAL_ERROR "Unknown sanitizer type: ${SVF_SANITIZE}")
endif()

#################################################################################
# #
# Find & load external Z3 dependencies #
# #
#################################################################################
# =================================================================================
# Find & load external Z3 dependencies
# =================================================================================

# Find Z3 and its include directory from the top-level include file
find_library(
Z3_LIBRARIES REQUIRED
NAMES z3
HINTS ${Z3_DIR} ENV Z3_DIR
PATH_SUFFIXES bin lib
)
PATH_SUFFIXES bin lib)
find_path(
Z3_INCLUDES REQUIRED
NAMES z3++.h
HINTS ${Z3_DIR} ENV Z3_DIR
PATH_SUFFIXES include z3
)
PATH_SUFFIXES include z3)
message(STATUS "Z3 STATUS:
Z3 library file: ${Z3_LIBRARIES}
Z3 include directory: ${Z3_INCLUDES}"
)
Z3 include directory: ${Z3_INCLUDES}")

# Add the Z3 include directory and link the Z3 library to all targets of SVF
set(CMAKE_INSTALL_RPATH ${Z3_INCLUDES})
link_libraries(${Z3_LIBRARIES})
include_directories(SYSTEM ${Z3_INCLUDES})

#################################################################################
# #
# Export C/C++ config.h header file #
# #
#################################################################################
# =================================================================================
# Export C/C++ config.h header file
# =================================================================================

# Create config.h based on config.in; export to <build_dir>/include/Util/config.h
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include)
Expand All @@ -174,11 +147,9 @@ configure_file(.config.in ${CMAKE_CURRENT_BINARY_DIR}/include/Util/config.h)
# Ensure the include directory in the build tree is exposed/available during building
include_directories(${CMAKE_CURRENT_BINARY_DIR}/include)

#################################################################################
# #
# Test suite configuration & build #
# #
#################################################################################
# =================================================================================
# Test suite configuration & build
# =================================================================================

# Check if the test-suite is present, if it is then build bc files and add testing to cmake build
if(EXISTS "${PROJECT_SOURCE_DIR}/Test-Suite")
Expand All @@ -188,28 +159,23 @@ if(EXISTS "${PROJECT_SOURCE_DIR}/Test-Suite")
include(CTest)
endif()

#################################################################################
# #
# Include core SVF source trees #
# #
#################################################################################
# =================================================================================
# Include core SVF source trees
# =================================================================================

# Add the actual SVF and SVF-LLVM targets
add_subdirectory(svf)
add_subdirectory(svf-llvm)

# Whether RTTI/Exceptions are enabled currently depends on whether the LLVM instance used to build
# SVF had them enabled; since the LLVM instance is found in the "svf-llvm" subdirectory, it sets the
# below variables in its parent directory (i.e. for this CMakeLists.txt) so check them here
add_compile_definitions(
$<IF:$<BOOL:${SVF_ENABLE_RTTI}>,,-fno-rtti> $<IF:$<BOOL:${SVF_ENABLE_EXCEPTIONS}>,,-fno-exceptions>
)
# Whether RTTI/Exceptions are enabled currently depends on whether the LLVM instance used to build SVF had them enabled;
# since the LLVM instance is found in the "svf-llvm" subdirectory, it sets the below variables in its parent directory
# (i.e. for this CMakeLists.txt) so check them here
add_compile_definitions($<IF:$<BOOL:${SVF_ENABLE_RTTI}>,,-fno-rtti>
$<IF:$<BOOL:${SVF_ENABLE_EXCEPTIONS}>,,-fno-exceptions>)

#################################################################################
# #
# Installation & package configuration #
# #
#################################################################################
# =================================================================================
# Installation & package configuration
# =================================================================================

# Generate SVFConfig.cmake
configure_package_config_file(
Expand All @@ -227,24 +193,21 @@ configure_package_config_file(
SVF_INSTALL_LIBDIR
SVF_INSTALL_INCLUDEDIR
SVF_EXTAPI_INSTALL_DIR
SVF_EXTAPI_INSTALL_FILE
)
SVF_EXTAPI_INSTALL_FILE)

# Generate SVFConfigVersion.cmake
write_basic_package_version_file(
${CMAKE_CURRENT_BINARY_DIR}/lib/cmake/SVF/SVFConfigVersion.cmake
VERSION "${SVF_VERSION_MAJOR}.${SVF_VERSION_MINOR}"
COMPATIBILITY AnyNewerVersion
)
COMPATIBILITY AnyNewerVersion)

# Install generated configuration header (see `configure_file()`) to top-level include dir of SVF
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/include/Util/config.h DESTINATION ${SVF_INSTALL_INCLUDEDIR}/Util)

# Install above CMake files as part of installation
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/lib/cmake/SVF/SVFConfig.cmake
${CMAKE_CURRENT_BINARY_DIR}/lib/cmake/SVF/SVFConfigVersion.cmake
DESTINATION ${SVF_INSTALL_LIBDIR}/cmake/SVF
)
DESTINATION ${SVF_INSTALL_LIBDIR}/cmake/SVF)

# Export the targets so that SvfCore & SvfLLVM are available after find_package(SVF)
export(EXPORT SVFTargets FILE ${CMAKE_CURRENT_BINARY_DIR}/lib/cmake/SVF/SVFTargets.cmake)
Expand All @@ -253,5 +216,4 @@ export(EXPORT SVFTargets FILE ${CMAKE_CURRENT_BINARY_DIR}/lib/cmake/SVF/SVFTarge
install(
EXPORT SVFTargets
FILE SVFTargets.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/SVF
)
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/SVF)
32 changes: 12 additions & 20 deletions svf-llvm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ find_package(
CONFIG
HINTS
${LLVM_DIR}
$ENV{LLVM_DIR}
)
message(
STATUS
"LLVM STATUS:
$ENV{LLVM_DIR})
message(STATUS "LLVM STATUS:
Version ${LLVM_VERSION}
Definitions ${LLVM_DEFINITIONS}
Includes ${LLVM_INCLUDE_DIRS}
Expand All @@ -18,14 +15,12 @@ message(
Build type ${LLVM_BUILD_TYPE}
Exceptions ${LLVM_ENABLE_EH}
RTTI ${LLVM_ENABLE_RTTI}
Dynamic lib ${LLVM_LINK_LLVM_DYLIB}"
)
Dynamic lib ${LLVM_LINK_LLVM_DYLIB}")

# Though not necessary, check if SVF is being built in debug mode & if that matches LLVM
if(CMAKE_BUILD_TYPE STREQUAL "Debug" AND NOT ${LLVM_BUILD_TYPE} STREQUAL "Debug")
message(NOTICE "Building SVF in debug-mode but LLVM was not built in debug-mode; "
"debug information could be incomplete when using SVF from LLVM"
)
"debug information could be incomplete when using SVF from LLVM")
endif()

# Add LLVM's include directories and link directory for all targets defined hereafter
Expand All @@ -36,12 +31,10 @@ add_definitions(${LLVM_DEFINITIONS})
# Ensure SVF is built with RTTI/exception handling if the used LLVM instance has them enabled
set(SVF_ENABLE_RTTI
${LLVM_ENABLE_RTTI}
PARENT_SCOPE
)
PARENT_SCOPE)
set(SVF_ENABLE_EXCEPTIONS
${LLVM_ENABLE_EH}
PARENT_SCOPE
)
PARENT_SCOPE)

# Check if LLVM was built generating the single libLLVM.so shared library file or as separate static libraries
if(LLVM_LINK_LLVM_DYLIB)
Expand Down Expand Up @@ -71,8 +64,7 @@ else()
scalaropts
support
target
transformutils
)
transformutils)
endif()

# Make the "add_llvm_library()" command available and configure LLVM/CMake
Expand All @@ -85,9 +77,10 @@ add_library(SVF::LLVM ALIAS SvfLLVM)
set_target_properties(SvfLLVM PROPERTIES VERSION ${SVF_VERSION} SOVERSION ${SVF_VERSION_MAJOR})

# Add the public headers as an include directory
target_include_directories(
SvfLLVM PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> $<INSTALL_INTERFACE:${SVF_INCLUDE_DIR}>
)
target_include_directories(SvfLLVM
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${SVF_INCLUDE_DIR}>)

# Link LLVM's libraries to SvfLLVM, as well as the SVF core library
target_link_libraries(SvfLLVM PUBLIC ${llvm_libs} SvfCore)
Expand All @@ -112,5 +105,4 @@ install(
LIBRARY DESTINATION ${SVF_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${SVF_INSTALL_LIBDIR}
FILE_SET HEADERS
DESTINATION ${SVF_INSTALL_INCLUDEDIR}
)
DESTINATION ${SVF_INSTALL_INCLUDEDIR})
3 changes: 1 addition & 2 deletions svf-llvm/include/SVF-LLVM/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ target_sources(
BASE_DIRS
${SVF_LLVM_INCLUDES}
FILES
${SVF_LLVM_HEADERS}
)
${SVF_LLVM_HEADERS})
9 changes: 3 additions & 6 deletions svf-llvm/lib/GenExtAPI.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ set(SVF_EXTAPI_FLAGS
-emit-llvm
-fno-discard-value-names
-Xclang
-disable-O0-optnone
)
-disable-O0-optnone)

if(NOT ${SVF_ENABLE_OPAQUE_POINTERS})
list(APPEND SVF_EXTAPI_FLAGS -Xclang -no-opaque-pointers)
Expand All @@ -27,15 +26,13 @@ find_program(
LLVM_CLANG
NAMES clang
HINTS ${LLVM_BINARY_DIR}
PATH_SUFFIXES bin REQUIRED
)
PATH_SUFFIXES bin REQUIRED)

# Define a custom command to compile extapi.c to LLVM IR bitcode in extapi.bc (in the build directory)
add_custom_command(
OUTPUT ${SVF_EXTAPI_OUT_FILE}
COMMAND ${LLVM_CLANG} ${SVF_EXTAPI_FLAGS} -o ${SVF_EXTAPI_OUT_FILE} ${SVF_EXTAPI_IN_FILE}
DEPENDS ${SVF_EXTAPI_IN_FILE}
)
DEPENDS ${SVF_EXTAPI_IN_FILE})

# Add a custom target for generating the LLVM bytecode file (and add it to the default build targets)
add_custom_target(gen_extapi_ir ALL DEPENDS ${SVF_EXTAPI_OUT_FILE})
Expand Down
10 changes: 5 additions & 5 deletions svf/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ add_library(SVF::Core ALIAS SvfCore)
set_target_properties(SvfCore PROPERTIES VERSION ${SVF_VERSION} SOVERSION ${SVF_VERSION_MAJOR})

# Add the public headers as an include directory
target_include_directories(
SvfCore PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> $<INSTALL_INTERFACE:${SVF_INCLUDE_DIR}>
)
target_include_directories(SvfCore
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${SVF_INCLUDE_DIR}>)

# Sources and headers are defined in the subdirectories
add_subdirectory(lib)
Expand All @@ -20,5 +21,4 @@ install(
LIBRARY DESTINATION ${SVF_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${SVF_INSTALL_LIBDIR}
FILE_SET HEADERS
DESTINATION ${SVF_INSTALL_INCLUDEDIR}
)
DESTINATION ${SVF_INSTALL_INCLUDEDIR})

0 comments on commit 639da85

Please sign in to comment.