From fb19922ea5734efc63aad11a673708bc0cfda09c Mon Sep 17 00:00:00 2001 From: Fabian Schiebel Date: Mon, 4 Mar 2024 19:30:12 +0100 Subject: [PATCH 1/5] Do not install phasar's dependencies to the global namespace anymore --- CMakeLists.txt | 50 ++++++++++++++--------------------- Config.cmake.in | 7 +++-- cmake/add_nlohmann_json.cmake | 50 +++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 34 deletions(-) create mode 100644 cmake/add_nlohmann_json.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 2ddf030caf..98d53d667e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -207,8 +207,8 @@ else() message(STATUS "Dynamic log disabled") endif() -# RPATH if (NOT PHASAR_IN_TREE) + # RPATH set(CMAKE_INSTALL_RPATH ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}) if (NOT "${CMAKE_INSTALL_LIBDIR}" STREQUAL "lib") @@ -217,6 +217,13 @@ if (NOT PHASAR_IN_TREE) endif() set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) + + # Export set + set(PHASAR_DEPS_EXPORT_SET PhasarDepsExports) + set(PHASAR_DEPS_INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/phasar/deps) +else() + # Export set + set(PHASAR_DEPS_EXPORT_SET LLVMExports) endif() # Filesystem @@ -257,30 +264,11 @@ find_package(Boost 1.65.1 COMPONENTS graph REQUIRED) set(CMAKE_CXX_CLANG_TIDY "") # Nlohmann JSON -set(JSON_BuildTests OFF) -set(JSON_Install ON) -add_subdirectory(external/json) - -# We need to work around the behavior of nlohmann_json_schema_validator and nlohmann_json here -# The validator needs the json part, but if you include it, the library of nlohmann_json_schema_validator -# is not installed, leading to linker error. But just including nlohmann_json is not sufficient, as -# in the installed state the nlohmann_json_schema_validator needs the nlohmann_json package which needs -# to be installed. -# The following workaround may collapse or become unnecessary once the issue is -# changed or fixed in nlohmann_json_schema_validator. - -# Override option of nlohmann_json_schema_validator to not build its tests -set(BUILD_TESTS OFF CACHE BOOL "Build json-schema-validator-tests") -if (PHASAR_IN_TREE) - set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS nlohmann_json_schema_validator) +include(add_nlohmann_json) - set (PHASAR_USE_Z3 OFF) -endif() - -# Json Schema Validator -set(JSON_VALIDATOR_INSTALL ON) -add_subdirectory(external/json-schema-validator) +add_nlohmann_json() +add_json_schema_validator() # Googletest if (NOT PHASAR_IN_TREE) @@ -350,7 +338,10 @@ if(NOT LLVM_ENABLE_RTTI AND NOT PHASAR_IN_TREE) endif() # Z3 Solver -if(PHASAR_USE_Z3) +if(PHASAR_IN_TREE) + set (PHASAR_USE_Z3 OFF) +endif() +if(PHASAR_USE_Z3 AND NOT PHASAR_IN_TREE) # This z3-version is the same version LLVM requires; however, we cannot just use Z3 via the LLVM interface # as it lacks some functionality (such as z3::expr::simplify()) that we require find_package(Z3 4.7.1 REQUIRED) @@ -507,6 +498,11 @@ if(NOT PHASAR_IN_TREE) NAMESPACE phasar:: DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/phasar" ) + install(EXPORT ${PHASAR_DEPS_EXPORT_SET} + FILE ${PHASAR_DEPS_EXPORT_SET}.cmake + NAMESPACE phasar:: + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/phasar" + ) else() install(TARGETS phasar_interface EXPORT LLVMExports @@ -514,12 +510,6 @@ else() set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS phasar_interface) endif() -# Install the header only json container ### TODO Fix this! -install(DIRECTORY external/json/single_include/ - DESTINATION include - FILES_MATCHING PATTERN "*.hpp" -) - # Install Phasar utils helper scripts install(DIRECTORY utils/ DESTINATION bin diff --git a/Config.cmake.in b/Config.cmake.in index 305cf04e28..f160d869b4 100644 --- a/Config.cmake.in +++ b/Config.cmake.in @@ -5,11 +5,10 @@ set_and_check(PHASAR_INCLUDE_DIR "@PACKAGE_INCLUDE_INSTALL_DIR@") set_and_check(PHASAR_LIBRARY_DIR "@PACKAGE_LIBRARY_INSTALL_DIR@") include (CMakeFindDependencyMacro) -find_dependency(nlohmann_json) -find_dependency(nlohmann_json_schema_validator) -find_package(Boost 1.65.1 COMPONENTS graph REQUIRED) -find_package(LLVM 14 REQUIRED CONFIG) +include("${CMAKE_CURRENT_LIST_DIR}/PhasarDepsExports.cmake") +find_dependency(Boost 1.65.1 COMPONENTS graph REQUIRED) +find_dependency(LLVM 14 REQUIRED CONFIG) set(PHASAR_USE_LLVM_FAT_LIB @USE_LLVM_FAT_LIB@) set(PHASAR_BUILD_DYNLIB @PHASAR_BUILD_DYNLIB@) diff --git a/cmake/add_nlohmann_json.cmake b/cmake/add_nlohmann_json.cmake new file mode 100644 index 0000000000..4ee85bb6b5 --- /dev/null +++ b/cmake/add_nlohmann_json.cmake @@ -0,0 +1,50 @@ + +function(add_nlohmann_json) + set(JSON_BuildTests OFF) + set(JSON_Install OFF) + + add_subdirectory(external/json) + set_property(TARGET nlohmann_json APPEND PROPERTY + INTERFACE_INCLUDE_DIRECTORIES $ + ) + + install(TARGETS nlohmann_json + EXPORT ${PHASAR_DEPS_EXPORT_SET} + LIBRARY DESTINATION ${PHASAR_DEPS_INSTALL_DESTINATION}/lib + ARCHIVE DESTINATION ${PHASAR_DEPS_INSTALL_DESTINATION}/lib + RUNTIME DESTINATION ${PHASAR_DEPS_INSTALL_DESTINATION}/bin + ) + install(DIRECTORY external/json/include/ + DESTINATION ${PHASAR_DEPS_INSTALL_DESTINATION}/include + ) +endfunction() + +function(add_json_schema_validator) + # We need to work around the behavior of nlohmann_json_schema_validator and nlohmann_json here + # The validator needs the json part, but if you include it, the library of nlohmann_json_schema_validator + # is not installed, leading to linker error. But just including nlohmann_json is not sufficient, as + # in the installed state the nlohmann_json_schema_validator needs the nlohmann_json package which needs + # to be installed. + # The following workaround may collapse or become unnecessary once the issue is + # changed or fixed in nlohmann_json_schema_validator. + if (PHASAR_IN_TREE) + set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS nlohmann_json_schema_validator) + endif() + + set(JSON_VALIDATOR_INSTALL OFF) + + add_subdirectory(external/json-schema-validator) + set_property(TARGET nlohmann_json_schema_validator APPEND PROPERTY + INTERFACE_INCLUDE_DIRECTORIES $ + ) + + install(TARGETS nlohmann_json_schema_validator + EXPORT ${PHASAR_DEPS_EXPORT_SET} + LIBRARY DESTINATION ${PHASAR_DEPS_INSTALL_DESTINATION}/lib + ARCHIVE DESTINATION ${PHASAR_DEPS_INSTALL_DESTINATION}/lib + RUNTIME DESTINATION ${PHASAR_DEPS_INSTALL_DESTINATION}/bin + ) + install(FILES external/json-schema-validator/src/nlohmann/json-schema.hpp + DESTINATION ${PHASAR_DEPS_INSTALL_DESTINATION}/include/nlohmann + ) +endfunction() From 55e30464ad3cadeba29b7cbacc6ee74c5c4345b4 Mon Sep 17 00:00:00 2001 From: Fabian Schiebel Date: Mon, 4 Mar 2024 19:42:36 +0100 Subject: [PATCH 2/5] fix for in-tree build --- cmake/add_nlohmann_json.cmake | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cmake/add_nlohmann_json.cmake b/cmake/add_nlohmann_json.cmake index 4ee85bb6b5..2f5258a015 100644 --- a/cmake/add_nlohmann_json.cmake +++ b/cmake/add_nlohmann_json.cmake @@ -3,6 +3,10 @@ function(add_nlohmann_json) set(JSON_BuildTests OFF) set(JSON_Install OFF) + if (PHASAR_IN_TREE) + set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS nlohmann_json) + endif() + add_subdirectory(external/json) set_property(TARGET nlohmann_json APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES $ From 5787680276fa063b2db897997c7ce186c158b273 Mon Sep 17 00:00:00 2001 From: Fabian Schiebel Date: Fri, 8 Mar 2024 18:48:17 +0100 Subject: [PATCH 3/5] out-factor the inclusion of LLVM and clang to a separate cmake file --- CMakeLists.txt | 98 +++----------------------------------------- cmake/add_llvm.cmake | 96 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 93 deletions(-) create mode 100644 cmake/add_llvm.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 98d53d667e..23178219ec 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -266,7 +266,6 @@ set(CMAKE_CXX_CLANG_TIDY "") # Nlohmann JSON include(add_nlohmann_json) - add_nlohmann_json() add_json_schema_validator() @@ -288,54 +287,8 @@ find_library(SQLITE3_LIBRARY NAMES sqlite3) option(USE_LLVM_FAT_LIB "Link against libLLVM.so instead of the individual LLVM libraries if possible (default is OFF; always on if BUILD_SHARED_LIBS is ON)" OFF) # LLVM -if (NOT PHASAR_IN_TREE) - # Only search for LLVM if we build out of tree - find_package(LLVM 14 REQUIRED CONFIG) - find_library(LLVM_LIBRARY NAMES LLVM PATHS ${LLVM_LIBRARY_DIRS} NO_DEFAULT_PATH) - - if(USE_LLVM_FAT_LIB AND ${LLVM_LIBRARY} STREQUAL "LLVM_LIBRARY-NOTFOUND") - message(WARNING "Did not find requested libLLVM.so. Link against individual modules instead") - set(USE_LLVM_FAT_LIB OFF) - elseif(BUILD_SHARED_LIBS AND NOT ${LLVM_LIBRARY} STREQUAL "LLVM_LIBRARY-NOTFOUND") - message(STATUS "Found consolidated shared LLVM lib ${LLVM_LIBRARY} that will be linked against.") - set(USE_LLVM_FAT_LIB ON) - endif() - - if (NOT USE_LLVM_FAT_LIB) - message(STATUS "Link against individual LLVM modules") - set(LLVM_REQUIRED_LIBRARIES - Core - Support - BitWriter - Analysis - Passes - Demangle - Analysis - IRReader - Linker - ) - foreach(lib ${LLVM_REQUIRED_LIBRARIES}) - find_library(LLVM_SMALL_LIB${lib} NAMES LLVM${lib} PATHS ${LLVM_LIBRARY_DIRS} NO_DEFAULT_PATH) - if(LLVM_SMALL_LIB${lib} MATCHES "NOTFOUND$") - list(APPEND LLVM_SMALL_LIB_NOTFOUND "LLVM${lib}") - endif() - endforeach() - - if(DEFINED LLVM_SMALL_LIB_NOTFOUND) - if(${LLVM_LIBRARY} STREQUAL "LLVM_LIBRARY-NOTFOUND") - message(FATAL_ERROR "Did not find a complete version of LLVM: Did not find the fat lib libLLVM.so, but also did not find the individual modules ${LLVM_SMALL_LIB_NOTFOUND}.") - else() - set(USE_LLVM_FAT_LIB ON) - list(JOIN LLVM_SMALL_LIB_NOTFOUND ", " LLVM_SMALL_LIB_NOTFOUND_PRETTY) - message(WARNING "Did not find the LLVM modules ${LLVM_SMALL_LIB_NOTFOUND_PRETTY}. Fallback to link against ${LLVM_LIBRARY}. To silence this warning, set -DUSE_LLVM_FAT_LIB=ON in the cmake invocation.") - endif() - endif(DEFINED LLVM_SMALL_LIB_NOTFOUND) - endif(NOT USE_LLVM_FAT_LIB) -endif(NOT PHASAR_IN_TREE) - -if(NOT LLVM_ENABLE_RTTI AND NOT PHASAR_IN_TREE) - message(FATAL_ERROR "PhASAR requires a LLVM version that is built with RTTI") -endif() +include(add_llvm) +add_llvm() # Z3 Solver if(PHASAR_IN_TREE) @@ -353,54 +306,13 @@ if(PHASAR_USE_Z3 AND NOT PHASAR_IN_TREE) set_property(TARGET z3 PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${Z3_INCLUDE_DIR}) endif() -endif(PHASAR_USE_Z3) +endif() # Clang option(BUILD_PHASAR_CLANG "Build the phasar_clang library (default is ON)" ON) - if(BUILD_PHASAR_CLANG) - # The clang-cpp shared library is now the preferred way to link dynamically against libclang if we build out of tree. - if(NOT PHASAR_IN_TREE) - find_library(CLANG_LIBRARY NAMES clang-cpp libclang-cpp HINTS ${LLVM_LIBRARY_DIRS}) - if(${CLANG_LIBRARY} STREQUAL "CLANG_LIBRARY-NOTFOUND") - set(NEED_LIBCLANG_COMPONENT_LIBS on) - endif() - endif() - # As fallback, look for the small clang libraries - if(PHASAR_IN_TREE OR NEED_LIBCLANG_COMPONENT_LIBS) - set(CLANG_LIBRARY - clangTooling - clangFrontendTool - clangFrontend - clangDriver - clangSerialization - clangCodeGen - clangParse - clangSema - clangStaticAnalyzerFrontend - clangStaticAnalyzerCheckers - clangStaticAnalyzerCore - clangAnalysis - clangARCMigrate - clangRewrite - clangRewriteFrontend - clangEdit - clangAST - clangASTMatchers - clangLex - clangBasic - LLVMFrontendOpenMP) - endif() - - if (PHASAR_IN_TREE) - # Phasar needs clang headers, specificaly some that are generated by clangs table-gen - include_directories( - ${CLANG_INCLUDE_DIR} - ${PHASAR_SRC_DIR}/../clang/include - ${PROJECT_BINARY_DIR}/tools/clang/include - ) - endif() -endif(BUILD_PHASAR_CLANG) + add_clang() +endif() # Set up clang-tidy to run during PhASAR's compilation to indicate code smells if (PHASAR_ENABLE_CLANG_TIDY_DURING_BUILD) diff --git a/cmake/add_llvm.cmake b/cmake/add_llvm.cmake new file mode 100644 index 0000000000..c6d98c3661 --- /dev/null +++ b/cmake/add_llvm.cmake @@ -0,0 +1,96 @@ +macro(add_llvm) + + if (NOT PHASAR_IN_TREE) + # Only search for LLVM if we build out of tree + find_package(LLVM 14 REQUIRED CONFIG) + find_library(LLVM_LIBRARY NAMES LLVM PATHS ${LLVM_LIBRARY_DIRS} NO_DEFAULT_PATH) + + if(USE_LLVM_FAT_LIB AND ${LLVM_LIBRARY} STREQUAL "LLVM_LIBRARY-NOTFOUND") + message(WARNING "Did not find requested libLLVM.so. Link against individual modules instead") + set(USE_LLVM_FAT_LIB OFF) + elseif(BUILD_SHARED_LIBS AND NOT ${LLVM_LIBRARY} STREQUAL "LLVM_LIBRARY-NOTFOUND") + message(STATUS "Found consolidated shared LLVM lib ${LLVM_LIBRARY} that will be linked against.") + set(USE_LLVM_FAT_LIB ON) + endif() + + if (NOT USE_LLVM_FAT_LIB) + message(STATUS "Link against individual LLVM modules") + set(LLVM_REQUIRED_LIBRARIES + Core + Support + BitWriter + Analysis + Passes + Demangle + Analysis + IRReader + Linker + ) + foreach(lib ${LLVM_REQUIRED_LIBRARIES}) + find_library(LLVM_SMALL_LIB${lib} NAMES LLVM${lib} PATHS ${LLVM_LIBRARY_DIRS} NO_DEFAULT_PATH) + if(LLVM_SMALL_LIB${lib} MATCHES "NOTFOUND$") + list(APPEND LLVM_SMALL_LIB_NOTFOUND "LLVM${lib}") + endif() + endforeach() + + if(DEFINED LLVM_SMALL_LIB_NOTFOUND) + if(${LLVM_LIBRARY} STREQUAL "LLVM_LIBRARY-NOTFOUND") + message(FATAL_ERROR "Did not find a complete version of LLVM: Did not find the fat lib libLLVM.so, but also did not find the individual modules ${LLVM_SMALL_LIB_NOTFOUND}.") + else() + set(USE_LLVM_FAT_LIB ON) + list(JOIN LLVM_SMALL_LIB_NOTFOUND ", " LLVM_SMALL_LIB_NOTFOUND_PRETTY) + message(WARNING "Did not find the LLVM modules ${LLVM_SMALL_LIB_NOTFOUND_PRETTY}. Fallback to link against ${LLVM_LIBRARY}. To silence this warning, set -DUSE_LLVM_FAT_LIB=ON in the cmake invocation.") + endif() + endif(DEFINED LLVM_SMALL_LIB_NOTFOUND) + endif(NOT USE_LLVM_FAT_LIB) + endif(NOT PHASAR_IN_TREE) + + if(NOT LLVM_ENABLE_RTTI AND NOT PHASAR_IN_TREE) + message(FATAL_ERROR "PhASAR requires a LLVM version that is built with RTTI") + endif() + +endmacro() + +function(add_clang) + # The clang-cpp shared library is now the preferred way to link dynamically against libclang if we build out of tree. + if(NOT PHASAR_IN_TREE) + find_library(CLANG_LIBRARY NAMES clang-cpp libclang-cpp HINTS ${LLVM_LIBRARY_DIRS}) + if(${CLANG_LIBRARY} STREQUAL "CLANG_LIBRARY-NOTFOUND") + set(NEED_LIBCLANG_COMPONENT_LIBS ON) + endif() + endif() + # As fallback, look for the small clang libraries + if(PHASAR_IN_TREE OR NEED_LIBCLANG_COMPONENT_LIBS) + set(CLANG_LIBRARY + clangTooling + clangFrontendTool + clangFrontend + clangDriver + clangSerialization + clangCodeGen + clangParse + clangSema + clangStaticAnalyzerFrontend + clangStaticAnalyzerCheckers + clangStaticAnalyzerCore + clangAnalysis + clangARCMigrate + clangRewrite + clangRewriteFrontend + clangEdit + clangAST + clangASTMatchers + clangLex + clangBasic + LLVMFrontendOpenMP) + endif() + + if (PHASAR_IN_TREE) + # Phasar needs clang headers, specificaly some that are generated by clangs table-gen + include_directories( + ${CLANG_INCLUDE_DIR} + ${PHASAR_SRC_DIR}/../clang/include + ${PROJECT_BINARY_DIR}/tools/clang/include + ) + endif() +endfunction(add_clang) From aed3c7ad001aab48160611e0627650c4a0f9158f Mon Sep 17 00:00:00 2001 From: Fabian Schiebel Date: Wed, 13 Mar 2024 20:31:19 +0100 Subject: [PATCH 4/5] Make PHASAR_CONFIG_INSTALL_DIR respect the CMAKE_INSTALL_PREFIX, even if that is not set at configure time --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 23178219ec..23d7d0e864 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -234,9 +234,9 @@ else() endif() # Config -set(PHASAR_CUSTOM_CONFIG_INSTALL_DIR "" CACHE STRING "If set, customizes the directory, where configuration files for PhASAR are installed (default is /usr/local/.phasar-config)") +set(PHASAR_CUSTOM_CONFIG_INSTALL_DIR "" CACHE STRING "If set, customizes the directory, where configuration files for PhASAR are installed (default is ${CMAKE_INSTALL_PREFIX}/.phasar-config)") if ("${PHASAR_CUSTOM_CONFIG_INSTALL_DIR}" STREQUAL "") - set(PHASAR_CONFIG_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/.phasar-config/") + set(PHASAR_CONFIG_INSTALL_DIR ".phasar-config/") else() set(PHASAR_CONFIG_INSTALL_DIR "${PHASAR_CUSTOM_CONFIG_INSTALL_DIR}") endif() From c95056959d7f7dc34cc612693b2227927e9e61f1 Mon Sep 17 00:00:00 2001 From: Fabian Schiebel Date: Wed, 13 Mar 2024 20:51:45 +0100 Subject: [PATCH 5/5] Fix in-tree build --- cmake/add_llvm.cmake | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmake/add_llvm.cmake b/cmake/add_llvm.cmake index c6d98c3661..7cf3ceb94c 100644 --- a/cmake/add_llvm.cmake +++ b/cmake/add_llvm.cmake @@ -51,7 +51,7 @@ macro(add_llvm) endmacro() -function(add_clang) +macro(add_clang) # The clang-cpp shared library is now the preferred way to link dynamically against libclang if we build out of tree. if(NOT PHASAR_IN_TREE) find_library(CLANG_LIBRARY NAMES clang-cpp libclang-cpp HINTS ${LLVM_LIBRARY_DIRS}) @@ -93,4 +93,4 @@ function(add_clang) ${PROJECT_BINARY_DIR}/tools/clang/include ) endif() -endfunction(add_clang) +endmacro(add_clang)