Skip to content

Commit

Permalink
[cmake] Add support to generate arbitrary runtime library configurations
Browse files Browse the repository at this point in the history
Every runtime library can be build with multiple configurations.

Replace the Makefile-based setup by cmake one.
Currently, we generate 32bit and 64bit libraries simultaneously and can link against them.
  • Loading branch information
MartinNowack authored and ccadar committed Nov 4, 2020
1 parent 5b8e54a commit 6156b4e
Show file tree
Hide file tree
Showing 16 changed files with 300 additions and 481 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ message(STATUS "KLEE_RUNTIME_BUILD_TYPE: ${KLEE_RUNTIME_BUILD_TYPE}")
set(KLEE_INSTALL_RUNTIME_DIR "${CMAKE_INSTALL_FULL_LIBDIR}/klee/runtime")

# Location where KLEE will look for the built runtimes by default.
set(KLEE_RUNTIME_DIRECTORY "${CMAKE_BINARY_DIR}/${KLEE_RUNTIME_BUILD_TYPE}/lib")
set(KLEE_RUNTIME_DIRECTORY "${CMAKE_BINARY_DIR}/runtime/lib")

file(MAKE_DIRECTORY ${KLEE_RUNTIME_DIRECTORY})

Expand Down
60 changes: 60 additions & 0 deletions cmake/compile_bitcode_library.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#===------------------------------------------------------------------------===#
#
# The KLEE Symbolic Virtual Machine
#
# This file is distributed under the University of Illinois Open Source
# License. See LICENSE.TXT for details.
#
#===------------------------------------------------------------------------===#
function(compile_bitcode_library library_name source_files compile_cc_flags compile_cxx_flags opt_suffix)
# Compile every source file
set(BC_FILES)
foreach(source_file ${source_files})
# Get filename without extension
get_filename_component(file_name_only "${source_file}" NAME_WE)
set(bc_file "${CMAKE_CURRENT_BINARY_DIR}/${file_name_only}${opt_suffix}.bc" )
get_filename_component(source_file_type "${source_file}" EXT)
if("${source_file_type}" STREQUAL ".cpp")
add_custom_command(
OUTPUT ${bc_file}
COMMAND ${LLVMCXX} -c "-emit-llvm" ${compile_cxx_flags} "${source_file}" -o ${bc_file}
DEPENDS ${source_file}
)
else()
add_custom_command(
OUTPUT ${bc_file}
COMMAND ${LLVMCC} -c "-emit-llvm" ${compile_cc_flags} "${source_file}" -o ${bc_file}
DEPENDS ${source_file}
)
endif()

list(APPEND BC_FILES ${bc_file})
endforeach()

# Add command to link them to an archive
add_custom_command(
OUTPUT ${library_name}
COMMAND ${LLVM_AR} rcs ${library_name} ${BC_FILES}
DEPENDS ${BC_FILES}
)
endfunction(compile_bitcode_library)

function(prefix_with_path files prefix output_var)
set(_result)
foreach(file ${files})
list(APPEND _result "${prefix}${file}")
endforeach()
set(${output_var} "${_result}" PARENT_SCOPE)
endfunction(prefix_with_path)

function(add_bitcode_library_targets lib_prefix prefixed_files cc_extra_args cxx_extra_args)
set(_lib_dependencies)
foreach(_suffix ${LIB_BC_SUFFIX})
set(final_cc_flags ${LIB_BC_FLAGS_${_suffix}} ${cc_extra_args})
set(final_cxx_flags ${LIB_BC_FLAGS_${_suffix}} ${cxx_extra_args})
compile_bitcode_library("${KLEE_RUNTIME_DIRECTORY}/libklee${lib_prefix}${_suffix}.bca" "${prefixed_files}" "${final_cc_flags}" "${final_cxx_flags}" "${_suffix}")
list(APPEND _lib_dependencies "${KLEE_RUNTIME_DIRECTORY}/libklee${lib_prefix}${_suffix}.bca")
endforeach()

add_custom_target(${lib_prefix} DEPENDS "${_lib_dependencies}")
endfunction(add_bitcode_library_targets)
231 changes: 95 additions & 136 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,150 +6,109 @@
# License. See LICENSE.TXT for details.
#
#===------------------------------------------------------------------------===#
# Handle binaries
add_subdirectory(Runtest)

if ("${KLEE_RUNTIME_BUILD_TYPE}" MATCHES "Release")
set(RUNTIME_IS_RELEASE 1)
else()
set(RUNTIME_IS_RELEASE 0)
endif()
# Handle bitcode libraries
# Define the different configurations to be compiled and made available using a specific suffix

set(bc_architectures 32 64)

set(LIB_BC_SUFFIX "")

foreach (bc_architecture ${bc_architectures})
foreach (bc_optimization ${available_klee_runtime_build_types})
# Add configuration to the set of available configurations
list(APPEND LIB_BC_SUFFIX "${bc_architecture}_${bc_optimization}")

# Set specific flags for that configuration
set(local_flags "")
if (bc_architecture EQUAL "32")
list(APPEND local_flags -m32)
endif ()


# Set specific compiler flags depending on the optimization
if (bc_optimization STREQUAL "Release")
list(APPEND local_flags -O2 -DNDebug)
elseif (bc_optimization STREQUAL "Release+Debug")
list(APPEND local_flags -O2 -g -DNDebug)
elseif (bc_optimization STREQUAL "Release+Asserts")
list(APPEND local_flags -O2 -g)
elseif (bc_optimization STREQUAL "Release+Debug+Asserts")
list(APPEND local_flags -O2 -g)
elseif (bc_optimization STREQUAL "Debug")
list(APPEND local_flags -g -DNDebug)
elseif (bc_optimization STREQUAL "Debug+Asserts")
list(APPEND local_flags -g)
else()
message(FATAL_ERROR
"Optimization (\"${bc_optimization}\") for runtime library unknown.")
endif ()

# Define suffix-specific optimizations
set("LIB_BC_FLAGS_${bc_architecture}_${bc_optimization}" ${local_flags})
endforeach ()
endforeach ()

message(STATUS "LIB_BC_SUFFIX: ${LIB_BC_SUFFIX}")
message(STATUS "KLEE_RUNTIME_DIRECTORY: ${KLEE_RUNTIME_DIRECTORY}")

# Add additional setups if needed, e.g.
# `list(APPEND LIB_BC_SUFFIX MY_SPECIAL_CONFIG)`

# Following define the specific flags: LIB_BC_FLAGS_*SUFFIX_FROM_ABOVE*
set(LIB_BC_FLAGS_64)
set(LIB_BC_FLAGS_32
-m32
)

# Common for all library configurations
# Setup all compilation flags
set(COMMON_CC_FLAGS
"-I${CMAKE_SOURCE_DIR}/include"
"-I${CMAKE_BINARY_DIR}/include"
-g
-D_DEBUG
-D_GNU_SOURCE
-D__STDC_LIMIT_MACROS
-D__STDC_CONSTANT_MACROS
-Wall
-Wwrite-strings
)

if ("${KLEE_RUNTIME_BUILD_TYPE}" MATCHES "Asserts")
set(RUNTIME_HAS_ASSERTIONS 1)
else()
set(RUNTIME_HAS_ASSERTIONS 0)
endif()
if (${LLVM_VERSION_MAJOR} GREATER 4)
list(APPEND COMMON_CC_FLAGS "-Xclang" "-disable-O0-optnone")
endif ()

if ("${KLEE_RUNTIME_BUILD_TYPE}" MATCHES "Debug")
set(RUNTIME_HAS_DEBUG_SYMBOLS 1)
else()
set(RUNTIME_HAS_DEBUG_SYMBOLS 0)
endif()
foreach (_suffix ${LIB_BC_SUFFIX})
list(APPEND "LIB_BC_FLAGS_${_suffix}" ${COMMON_CC_FLAGS})
endforeach ()

if (ENABLE_POSIX_RUNTIME)
set(BUILD_POSIX_RUNTIME 1)
else()
set(BUILD_POSIX_RUNTIME 0)
endif()
add_subdirectory(FreeStanding)
add_subdirectory(Intrinsic)
add_subdirectory(klee-libc)

set(RUNTIME_LIBRARIES
RuntimeFreeStanding
RuntimeIntrinsic
RuntimeKLEELibc
)

if (ENABLE_KLEE_EH_CXX)
set(BUILD_KLEE_EH_CXX 1)
else()
set(BUILD_KLEE_EH_CXX 0)
endif()

# Configure the bitcode build system
configure_file("Makefile.cmake.bitcode.config.in"
"Makefile.cmake.bitcode.config"
@ONLY
)

# Copy over the makefiles to the build directory
configure_file("Makefile.cmake.bitcode" "Makefile.cmake.bitcode" COPYONLY)
configure_file("Makefile.cmake.bitcode.rules" "Makefile.cmake.bitcode.rules" COPYONLY)

# Makefile for root runtime directory
# Copy over makefiles for libraries
set(BITCODE_LIBRARIES "Intrinsic" "klee-libc" "FreeStanding")
if (ENABLE_POSIX_RUNTIME)
list(APPEND BITCODE_LIBRARIES "POSIX")
endif()
if (ENABLE_KLEE_EH_CXX)
list(APPEND BITCODE_LIBRARIES "klee-eh-cxx")
endif()
foreach (bl ${BITCODE_LIBRARIES})
configure_file("${bl}/Makefile.cmake.bitcode"
"${CMAKE_CURRENT_BINARY_DIR}/${bl}/Makefile.cmake.bitcode"
COPYONLY)
endforeach()

# Find GNU make
find_program(MAKE_BINARY
NAMES make gmake
)

if (NOT MAKE_BINARY)
message(STATUS "Failed to find make binary")
endif()

# Find env
find_program(ENV_BINARY
NAMES env
)
if (NOT ENV_BINARY)
message(FATAL_ERROR "Failed to find env binary")
endif()

option(KLEE_RUNTIME_ALWAYS_REBUILD "Always try to rebuild KLEE runtime" ON)
if (KLEE_RUNTIME_ALWAYS_REBUILD)
set(EXTERNAL_PROJECT_BUILD_ALWAYS_ARG 1)
else()
set(EXTERNAL_PROJECT_BUILD_ALWAYS_ARG 0)
endif()

# Build the runtime as an external project.
# We do this because CMake isn't really suitable
# for building the runtime because it can't handle
# the source file dependencies properly.
include(ExternalProject)
ExternalProject_Add(BuildKLEERuntimes
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}"
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}"
CONFIGURE_COMMAND "${CMAKE_COMMAND}" -E echo "" # Dummy command
BUILD_COMMAND "${CMAKE_COMMAND}" -E echo "" # Dummy command
INSTALL_COMMAND "${CMAKE_COMMAND}" -E echo "" # Dummy command
)

set(O0OPT "-O0")
if (${LLVM_VERSION_MAJOR} GREATER 4)
set(O0OPT "${O0OPT} -Xclang -disable-O0-optnone")
endif()


# Use `ExternalProject_Add_Step` with `ALWAYS` argument instead of directly
# building in `ExternalProject_Add` with `BUILD_ALWAYS` argument due to lack of
# support for the `BUILD_ALWAYS` argument in CMake < 3.1.
ExternalProject_Add_Step(BuildKLEERuntimes RuntimeBuild
# `env` is used here to make sure `MAKEFLAGS` of KLEE's build
# is not propagated into the bitcode build system.
COMMAND ${ENV_BINARY} MAKEFLAGS="" O0OPT=${O0OPT} ${MAKE_BINARY} -f Makefile.cmake.bitcode all
ALWAYS ${EXTERNAL_PROJECT_BUILD_ALWAYS_ARG}
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
${EXTERNAL_PROJECT_ADD_STEP_USES_TERMINAL_ARG}
)

# Target for cleaning the bitcode build system
# NOTE: Invoking `make clean` does not invoke this target.
# Instead the user needs to invoke the `clean_all` target.
# It's also weird that `ExternalProject` provides no way to do a clean.
add_custom_target(clean_runtime
COMMAND ${ENV_BINARY} MAKEFLAGS="" ${MAKE_BINARY} -f Makefile.cmake.bitcode clean
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
${ADD_CUSTOM_COMMAND_USES_TERMINAL_ARG}
)
add_dependencies(clean_all clean_runtime)

###############################################################################
# Runtime install
###############################################################################
set(RUNTIME_FILES_TO_INSTALL)

list(APPEND RUNTIME_FILES_TO_INSTALL
"${KLEE_RUNTIME_DIRECTORY}/libkleeRuntimeIntrinsic.bca"
"${KLEE_RUNTIME_DIRECTORY}/libklee-libc.bca"
"${KLEE_RUNTIME_DIRECTORY}/libkleeRuntimeFreeStanding.bca"
)
list(APPEND RUNTIME_LIBRARIES eh-cxx)
add_subdirectory(klee-eh-cxx)
endif ()

if (ENABLE_POSIX_RUNTIME)
list(APPEND RUNTIME_FILES_TO_INSTALL
"${KLEE_RUNTIME_DIRECTORY}/libkleeRuntimePOSIX.bca")
endif()
list(APPEND RUNTIME_LIBRARIES RuntimePOSIX)
add_subdirectory(POSIX)
endif ()

if (ENABLE_KLEE_EH_CXX)
list(APPEND RUNTIME_FILES_TO_INSTALL
"${KLEE_RUNTIME_DIRECTORY}/libklee-eh-cxx.bca")
endif()
add_custom_target(BuildKLEERuntimes
DEPENDS "${RUNTIME_LIBRARIES}"
)

install(FILES
${RUNTIME_FILES_TO_INSTALL}
DESTINATION "${KLEE_INSTALL_RUNTIME_DIR}")
install(DIRECTORY "${KLEE_RUNTIME_DIRECTORY}/"
DESTINATION "${KLEE_INSTALL_RUNTIME_DIR}"
)
21 changes: 21 additions & 0 deletions runtime/FreeStanding/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#===------------------------------------------------------------------------===#
#
# The KLEE Symbolic Virtual Machine
#
# This file is distributed under the University of Illinois Open Source
# License. See LICENSE.TXT for details.
#
#===------------------------------------------------------------------------===#

set(LIB_PREFIX "RuntimeFreeStanding")
set(SRC_FILES
memcmp.c
memcpy.c
memmove.c
memset.c
)

# Build it
include("${CMAKE_SOURCE_DIR}/cmake/compile_bitcode_library.cmake")
prefix_with_path("${SRC_FILES}" "${CMAKE_CURRENT_SOURCE_DIR}/" prefixed_files)
add_bitcode_library_targets("${LIB_PREFIX}" "${prefixed_files}" "" "")
15 changes: 0 additions & 15 deletions runtime/FreeStanding/Makefile.cmake.bitcode

This file was deleted.

24 changes: 24 additions & 0 deletions runtime/Intrinsic/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#===------------------------------------------------------------------------===#
#
# The KLEE Symbolic Virtual Machine
#
# This file is distributed under the University of Illinois Open Source
# License. See LICENSE.TXT for details.
#
#===------------------------------------------------------------------------===#
# Set up
set(LIB_PREFIX "RuntimeIntrinsic")
set(SRC_FILES
dso_handle.c
klee_choose.c
klee_div_zero_check.c
klee_int.c
klee_is_replay.c
klee_overshift_check.c
klee_range.c
)

# Build it
include("${CMAKE_SOURCE_DIR}/cmake/compile_bitcode_library.cmake")
prefix_with_path("${SRC_FILES}" "${CMAKE_CURRENT_SOURCE_DIR}/" prefixed_files)
add_bitcode_library_targets("${LIB_PREFIX}" "${prefixed_files}" "-std=gnu89" "")
15 changes: 0 additions & 15 deletions runtime/Intrinsic/Makefile.cmake.bitcode

This file was deleted.

Loading

0 comments on commit 6156b4e

Please sign in to comment.