From 121d8f5bfa84cf676760eef51af92aa897ba1cd7 Mon Sep 17 00:00:00 2001 From: Martin Mory Date: Tue, 4 Feb 2020 18:22:59 +0100 Subject: [PATCH 1/3] This makes PhASAR a package that can be used via find_package() in an out-of-tree tool. Dependent libraries, include dirs and definitions are propagated to the out-of-tree tool. See https://github.com/MMory/OOTPhasarToolDemo for an example. --- CMakeLists.txt | 30 +++++++++++++++++++++++++++++- Config.cmake.in | 7 +++++++ cmake/phasar_macros.cmake | 2 ++ lib/PhasarClang/CMakeLists.txt | 1 + lib/Utils/CMakeLists.txt | 30 +++++++++++++++++++++++++++++- 5 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 Config.cmake.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 62643a1b8d..91057643a5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -101,7 +101,6 @@ endif() # Boost find_package(Boost 1.65.1 COMPONENTS filesystem graph system program_options log ${BOOST_THREAD} REQUIRED) include_directories(${Boost_INCLUDE_DIRS}) -add_definitions(-DBOOST_LOG_DYN_LINK) # JSON library option(JSON_BuildTests OFF) @@ -231,6 +230,8 @@ if (PHASAR_BUILD_IR) add_subdirectory(test) endif() +set(INCLUDE_INSTALL_DIR include/ CACHE PATH "Install dir of headers") + # Install targets of phasar-llvm, other executables, and libraries are to be # found in the individual subdirectories of tools/ @@ -271,6 +272,33 @@ install(DIRECTORY config/ WORLD_READ ) +install(EXPORT phasarTargets + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/phasar + NAMESPACE phasar:: + FILE phasarTargets.cmake +) + +include(CMakePackageConfigHelpers) +configure_package_config_file( + Config.cmake.in + phasarConfig.cmake + INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/phasar + PATH_VARS INCLUDE_INSTALL_DIR + ) + +write_basic_package_version_file( + ${CMAKE_CURRENT_BINARY_DIR}/phasarConfigVersion.cmake + VERSION 1.0.0 + COMPATIBILITY SameMajorVersion + ) + +### Install Config and ConfigVersion files +install( + FILES "${CMAKE_CURRENT_BINARY_DIR}/phasarConfig.cmake" + "${CMAKE_CURRENT_BINARY_DIR}/phasarConfigVersion.cmake" + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/phasar" + ) + # If the Phasar shared object libraries are not installed into a system folder # the so libs must be added manually to the linker search path and the linker # config must be updated as follows: diff --git a/Config.cmake.in b/Config.cmake.in new file mode 100644 index 0000000000..bfd2dcc95c --- /dev/null +++ b/Config.cmake.in @@ -0,0 +1,7 @@ +set(PHASAR_VERSION 1.0.0) + +@PACKAGE_INIT@ +set_and_check(PHASAR_INCLUDE_DIR "@PACKAGE_INCLUDE_INSTALL_DIR@") +include( "${CMAKE_CURRENT_LIST_DIR}/phasarTargets.cmake" ) + +check_required_components(phasar) diff --git a/cmake/phasar_macros.cmake b/cmake/phasar_macros.cmake index 6acadb08e1..9295781472 100644 --- a/cmake/phasar_macros.cmake +++ b/cmake/phasar_macros.cmake @@ -160,6 +160,7 @@ macro(add_phasar_library name) set(libkind) endif() add_library( ${name} ${libkind} ${srcs} ) + add_library( phasar::${name} ALIAS ${name} ) if( LLVM_COMMON_DEPENDS ) add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} ) endif( LLVM_COMMON_DEPENDS ) @@ -188,6 +189,7 @@ macro(add_phasar_library name) endif(MSVC) install(TARGETS ${name} EXPORT LLVMExports + EXPORT phasarTargets LIBRARY DESTINATION lib ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX}) set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${name}) diff --git a/lib/PhasarClang/CMakeLists.txt b/lib/PhasarClang/CMakeLists.txt index 1a3901de1d..cd3ed9ddc5 100644 --- a/lib/PhasarClang/CMakeLists.txt +++ b/lib/PhasarClang/CMakeLists.txt @@ -22,6 +22,7 @@ endif() find_package(Boost COMPONENTS log REQUIRED) target_link_libraries(phasar_clang LINK_PUBLIC + phasar_utils clangTooling clangFrontendTool clangFrontend diff --git a/lib/Utils/CMakeLists.txt b/lib/Utils/CMakeLists.txt index 89ee4e625e..40e1d5f78d 100644 --- a/lib/Utils/CMakeLists.txt +++ b/lib/Utils/CMakeLists.txt @@ -16,7 +16,6 @@ set(LLVM_LINK_COMPONENTS BitWriter ) - if(BUILD_SHARED_LIBS) add_phasar_library(phasar_utils SHARED @@ -29,11 +28,40 @@ else() ) endif() +set(LLVM_LINK_COMPONENTS + coverage + coroutines + libdriver + lto + support + analysis + bitwriter + core + ipo + irreader + instcombine + instrumentation + linker + objcarcopts + scalaropts + transformutils + codegen + vectorize +) + +llvm_map_components_to_libnames(llvm_libs + ${LLVM_LINK_COMPONENTS} +) + +target_include_directories(phasar_utils PUBLIC ${LLVM_INCLUDE_DIRS}) +target_compile_definitions(phasar_utils PUBLIC -DBOOST_LOG_DYN_LINK) + find_package(Boost COMPONENTS log REQUIRED) target_link_libraries(phasar_utils LINK_PUBLIC ${Boost_LIBRARIES} ${CMAKE_DL_LIBS} + ${llvm_libs} ) set_target_properties(phasar_utils From cbd60b7d24c6e6f027c3607e246ee80d2d362fb8 Mon Sep 17 00:00:00 2001 From: Martin Mory Date: Wed, 5 Feb 2020 10:22:29 +0100 Subject: [PATCH 2/3] added example program that uses phasar as library to examples/ --- examples/use-phasar-as-library/CMakeLists.txt | 41 ++++++++++ examples/use-phasar-as-library/README.md | 1 + .../use-phasar-as-library/myphasartool.cpp | 76 +++++++++++++++++++ 3 files changed, 118 insertions(+) create mode 100644 examples/use-phasar-as-library/CMakeLists.txt create mode 100644 examples/use-phasar-as-library/README.md create mode 100644 examples/use-phasar-as-library/myphasartool.cpp diff --git a/examples/use-phasar-as-library/CMakeLists.txt b/examples/use-phasar-as-library/CMakeLists.txt new file mode 100644 index 0000000000..86642f0e0e --- /dev/null +++ b/examples/use-phasar-as-library/CMakeLists.txt @@ -0,0 +1,41 @@ +cmake_minimum_required(VERSION 3.12) + +project(PhasarExttoolTest) + +set(CMAKE_EXPORT_COMPILE_COMMANDS YES) +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) + + +# Build a small test tool to show how phasar may be used +add_executable(myphasartool + myphasartool.cpp +) + +find_package(phasar) +include_directories(${PHASAR_INCLUDE_DIR}) +link_directories(${PHASAR_LIBRARY_DIR}) + +target_link_libraries(myphasartool + LINK_PUBLIC + phasar::phasar_config + phasar::phasar_controller + phasar::phasar_db + phasar::phasar_experimental + phasar::phasar_clang + phasar::phasar_controlflow + phasar::phasar_ifdside + phasar::phasar_mono + phasar::phasar_passes + phasar::phasar_pointer + phasar::phasar_typehierarchy + phasar::phasar_phasarllvm_utils +# phasar::phasar_utils #already introduced through phasar_clang +) + +install(TARGETS myphasartool + RUNTIME DESTINATION bin + LIBRARY DESTINATION lib + ARCHIVE DESTINATION lib +) diff --git a/examples/use-phasar-as-library/README.md b/examples/use-phasar-as-library/README.md new file mode 100644 index 0000000000..2a905cb90b --- /dev/null +++ b/examples/use-phasar-as-library/README.md @@ -0,0 +1 @@ +This is a demo tool that uses PhASAR as a library. Currently this is only supported in the f-CMakePackage feature branch of PhASAR. The number of phasar libraries explicitly stated in CMakeLists.txt can be further reduced by stating the non-transitive dependencies of phasar libraries. This is pending work on the PhASAR side. diff --git a/examples/use-phasar-as-library/myphasartool.cpp b/examples/use-phasar-as-library/myphasartool.cpp new file mode 100644 index 0000000000..e5b52b04a1 --- /dev/null +++ b/examples/use-phasar-as-library/myphasartool.cpp @@ -0,0 +1,76 @@ +/****************************************************************************** + * Copyright (c) 2017 Philipp Schubert. + * All rights reserved. This program and the accompanying materials are made + * available under the terms of LICENSE.txt. + * + * Contributors: + * Philipp Schubert and others + *****************************************************************************/ + +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace llvm { +class Value; +} // namespace llvm + +using namespace psr; + +int main(int argc, const char **argv) { + initializeLogger(false); + auto &lg = lg::get(); + if (argc < 2 || !boost::filesystem::exists(argv[1]) || + boost::filesystem::is_directory(argv[1])) { + std::cerr << "myphasartool\n" + "A small PhASAR-based example program\n\n" + "Usage: myphasartool \n"; + return 1; + } + initializeLogger(false); + ProjectIRDB DB({argv[1]}); + if (auto F = DB.getFunctionDefinition("main")) { + LLVMTypeHierarchy H(DB); + // print type hierarchy + H.print(); + LLVMPointsToInfo P(DB); + // print points-to information + P.print(); + LLVMBasedICFG I(DB, CallGraphAnalysisType::OTF, {"main"}, &H, &P); + // print inter-procedural control-flow graph + I.print(); + // IFDS template parametrization test + std::cout << "Testing IFDS:\n"; + IFDSLinearConstantAnalysis L(&DB, &H, &I, &P, {"main"}); + IFDSSolver + S(L); + S.solve(); + S.dumpResults(); + // IDE template parametrization test + std::cout << "Testing IDE:\n"; + IDELinearConstantAnalysis M(&DB, &H, &I, &P, {"main"}); + IDESolver + T(M); + T.solve(); + T.dumpResults(); + } else { + std::cerr << "error: file does not contain a 'main' function!\n"; + } + return 0; +} From 06f1b8ad2e83cd2cb94e77b8f0959765b978702a Mon Sep 17 00:00:00 2001 From: Martin Mory Date: Wed, 5 Feb 2020 14:45:40 +0100 Subject: [PATCH 3/3] introduced option PHASAR_DEBUG_LIBDEPS to introduce private linking of phasar libraries to be able to detect and control dependency changes --- CMakeLists.txt | 2 ++ cmake/phasar_macros.cmake | 6 +++++- lib/PhasarClang/CMakeLists.txt | 5 ++++- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 91057643a5..e5cd644dd1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -49,6 +49,8 @@ option(PHASAR_BUILD_IR "Build IR test code (default is OFF)" OFF) option(PHASAR_BUILD_DOC "Build documentation" OFF) +option(PHASAR_DEBUG_LIBDEPS "Debug internal library dependencies (private linkage)" OFF) + option(BUILD_SHARED_LIBS "Build shared libraries (default is ON)" ON) option(PHASAR_ENABLE_WARNINGS "Enable warnings" ON) diff --git a/cmake/phasar_macros.cmake b/cmake/phasar_macros.cmake index 9295781472..94af4d6bc8 100644 --- a/cmake/phasar_macros.cmake +++ b/cmake/phasar_macros.cmake @@ -172,7 +172,11 @@ macro(add_phasar_library name) if(PHASAR_LINK_LIBS) foreach(lib ${PHASAR_LINK_LIBS}) - target_link_libraries(${name} LINK_PRIVATE ${lib}) + if(PHASAR_DEBUG_LIBDEPS) + target_link_libraries(${name} LINK_PRIVATE ${lib}) + else() + target_link_libraries(${name} LINK_PUBLIC ${lib}) + endif(PHASAR_DEBUG_LIBDEPS) endforeach(lib) endif(PHASAR_LINK_LIBS) diff --git a/lib/PhasarClang/CMakeLists.txt b/lib/PhasarClang/CMakeLists.txt index cd3ed9ddc5..4cef18da2f 100644 --- a/lib/PhasarClang/CMakeLists.txt +++ b/lib/PhasarClang/CMakeLists.txt @@ -2,6 +2,10 @@ file(GLOB_RECURSE PHASARCLANG_SRC *.h *.cpp) include_directories(${CLANG_INCLUDE_DIRS}) +set(PHASAR_LINK_LIBS + phasar_utils +) + set(LLVM_LINK_COMPONENTS Support Core @@ -22,7 +26,6 @@ endif() find_package(Boost COMPONENTS log REQUIRED) target_link_libraries(phasar_clang LINK_PUBLIC - phasar_utils clangTooling clangFrontendTool clangFrontend