Skip to content

Commit

Permalink
[Polly][CMake] Use the CMake Package instead of llvm-config in out-of…
Browse files Browse the repository at this point in the history
…-tree builds

Summary:
As of now, Polly uses llvm-config to set up LLVM dependencies in an out-of-tree build.

This is problematic for two reasons:
1) Right now, in-tree and out-of-tree builds in fact do different things. E.g., in an in-tree build, libPolly depends on a handful of LLVM libraries, while in an out-of-tree build it depends on all of them. This means that we often need to treat both paths seperately.
2) I'm specifically unhappy with the way libPolly is linked right now, because it just blindly links against all the LLVM libs. That doesn't make a lot of sense. For instance, one of these libs is LLVMTableGen, which contains a command line definition of a -o option. This means that I can not link an out-of-tree libPolly into a tool which might want to offer a -o option as well.

This patch (mostly) drop the use of llvm-config  in favor of LLVMs exported cmake package. However, building Polly with unittests requires access to the gtest sources (in the LLVM source tree). If we're building against an LLVM installation, this source tree is unavailable and must specified. I'm using llvm-config to provide a default in this case.

Reviewers: Meinersbur, grosser

Reviewed By: grosser

Subscribers: tstellar, bollu, chapuni, mgorny, pollydev, llvm-commits

Differential Revision: https://reviews.llvm.org/D33299

llvm-svn: 307650
  • Loading branch information
pfaffe committed Jul 11, 2017
1 parent b4d500f commit d99c406
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 313 deletions.
143 changes: 44 additions & 99 deletions polly/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,112 +4,57 @@ if (NOT DEFINED LLVM_MAIN_SRC_DIR)
cmake_minimum_required(VERSION 3.4.3)

# Where is LLVM installed?
set(LLVM_INSTALL_ROOT "" CACHE PATH "Root of LLVM install.")
# Check if the LLVM_INSTALL_ROOT valid.
if( NOT EXISTS ${LLVM_INSTALL_ROOT}/include/llvm )
message(FATAL_ERROR "LLVM_INSTALL_ROOT (${LLVM_INSTALL_ROOT}) is not a valid LLVM installation.")
endif(NOT EXISTS ${LLVM_INSTALL_ROOT}/include/llvm)
# FileCheck, not and llvm-lit are not install by default, warn the user to copy them.
if( NOT EXISTS ${LLVM_INSTALL_ROOT}/bin/FileCheck
OR NOT EXISTS ${LLVM_INSTALL_ROOT}/bin/not
OR NOT EXISTS ${LLVM_INSTALL_ROOT}/bin/llvm-lit )
message(WARNING "'FileCheck', 'not' and 'llvm-lit' are required by running regress tests, "
"but they are not installed! Please copy them to "
"${LLVM_INSTALL_ROOT}/bin.")
endif()
find_package(LLVM CONFIG REQUIRED)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${LLVM_CMAKE_DIR})
include(HandleLLVMOptions)
include(AddLLVM)

# Add the llvm header path.
include_directories(${LLVM_INSTALL_ROOT}/include/)

# Get LLVM's own libraries.
execute_process(COMMAND "${LLVM_INSTALL_ROOT}/bin/llvm-config" --libs
OUTPUT_VARIABLE LLVM_LIBS
OUTPUT_STRIP_TRAILING_WHITESPACE)

# Get the system librarys that will link into LLVM.
execute_process(COMMAND "${LLVM_INSTALL_ROOT}/bin/llvm-config" --system-libs
OUTPUT_VARIABLE LLVM_SYSTEM_LIBS
OUTPUT_STRIP_TRAILING_WHITESPACE)
message(STATUS "System libs required by LLVM: ${LLVM_SYSTEM_LIBS}")

# Determine where LLVM stores its libraries.
execute_process(COMMAND "${LLVM_INSTALL_ROOT}/bin/llvm-config" --libdir
OUTPUT_VARIABLE LLVM_LIBRARY_DIR
OUTPUT_STRIP_TRAILING_WHITESPACE)
link_directories("${LLVM_LIBRARY_DIR}")

# Now set the header paths.
execute_process(COMMAND "${LLVM_INSTALL_ROOT}/bin/llvm-config" --includedir
OUTPUT_VARIABLE LLVM_INCLUDE_DIR
OUTPUT_STRIP_TRAILING_WHITESPACE)
include_directories( ${LLVM_INCLUDE_DIR} )

# Get the TARGET_TRIPLE
execute_process(COMMAND "${LLVM_INSTALL_ROOT}/bin/llvm-config" --host-target
OUTPUT_VARIABLE TARGET_TRIPLE
OUTPUT_STRIP_TRAILING_WHITESPACE)

# And then set the cxx flags.
execute_process(COMMAND "${LLVM_INSTALL_ROOT}/bin/llvm-config" --cxxflags
OUTPUT_VARIABLE LLVM_CXX_FLAGS
OUTPUT_STRIP_TRAILING_WHITESPACE)
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} ${LLVM_CXX_FLAGS})

# Check LLVM_ENABLE_ASSERTIONS
execute_process(COMMAND "${LLVM_INSTALL_ROOT}/bin/llvm-config" --assertion-mode
OUTPUT_VARIABLE LLVM_ENABLE_ASSERTIONS
OUTPUT_STRIP_TRAILING_WHITESPACE)
# Copied from LLVM's HandleLLVMOptions.cmake
if( LLVM_ENABLE_ASSERTIONS )
# MSVC doesn't like _DEBUG on release builds. See PR 4379.
if( NOT MSVC )
add_definitions( -D_DEBUG )
endif()
# On non-Debug builds cmake automatically defines NDEBUG, so we
# explicitly undefine it:
if( NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" )
add_definitions( -UNDEBUG )
# Also remove /D NDEBUG to avoid MSVC warnings about conflicting defines.
foreach (flags_var_to_scrub
CMAKE_CXX_FLAGS_RELEASE
CMAKE_CXX_FLAGS_RELWITHDEBINFO
CMAKE_CXX_FLAGS_MINSIZEREL
CMAKE_C_FLAGS_RELEASE
CMAKE_C_FLAGS_RELWITHDEBINFO
CMAKE_C_FLAGS_MINSIZEREL)
string (REGEX REPLACE "(^| )[/-]D *NDEBUG($| )" " "
"${flags_var_to_scrub}" "${${flags_var_to_scrub}}")
endforeach()
endif()
endif()
include_directories(${LLVM_INCLUDE_DIRS})

# Sources available, too?
execute_process(COMMAND "${LLVM_INSTALL_ROOT}/bin/llvm-config" --src-root
OUTPUT_VARIABLE MAIN_SRC_DIR
OUTPUT_STRIP_TRAILING_WHITESPACE)
set(LLVM_SOURCE_ROOT ${MAIN_SRC_DIR} CACHE PATH "Path to LLVM source tree")
if (LLVM_BUILD_MAIN_SRC_DIR)
set(LLVM_SOURCE_ROOT ${LLVM_BUILD_MAIN_SRC_DIR} CACHE PATH
"Path to LLVM source tree")
else()
execute_process(COMMAND "${LLVM_TOOLS_BINARY_DIR}/llvm-config" --src-root
OUTPUT_VARIABLE MAIN_SRC_DIR
OUTPUT_STRIP_TRAILING_WHITESPACE)
set(LLVM_SOURCE_ROOT ${MAIN_SRC_DIR} CACHE PATH "Path to LLVM source tree")
endif()

# Enable unit tests if available.
set(UNITTEST_DIR ${LLVM_SOURCE_ROOT}/utils/unittest)
if(EXISTS ${UNITTEST_DIR}/googletest/include/gtest/gtest.h)
add_library(gtest
${UNITTEST_DIR}/googletest/src/gtest-all.cc
${UNITTEST_DIR}/googlemock/src/gmock-all.cc
)
target_include_directories(gtest
PUBLIC
"${UNITTEST_DIR}/googletest/include"
"${UNITTEST_DIR}/googlemock/include"

PRIVATE
"${UNITTEST_DIR}/googletest"
"${UNITTEST_DIR}/googlemock"
)
target_link_libraries(gtest -lpthread)

add_library(gtest_main ${UNITTEST_DIR}/UnitTestMain/TestMain.cpp)
target_link_libraries(gtest_main gtest)

set(POLLY_GTEST_AVAIL 1)
# The build tree already exports the gtest target, which we can reuse
if (TARGET gtest)
# LLVM Doesn't export gtest's include directorys, so do that here
set_target_properties(gtest
PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
"${UNITTEST_DIR}/googletest/include;${UNITTEST_DIR}/googlemock/include"
)
set(POLLY_GTEST_AVAIL 1)
else()
add_library(gtest
${UNITTEST_DIR}/googletest/src/gtest-all.cc
${UNITTEST_DIR}/googlemock/src/gmock-all.cc
)
target_include_directories(gtest
PUBLIC
"${UNITTEST_DIR}/googletest/include"
"${UNITTEST_DIR}/googlemock/include"

PRIVATE
"${UNITTEST_DIR}/googletest"
"${UNITTEST_DIR}/googlemock"
)
target_link_libraries(gtest -lpthread)

add_library(gtest_main ${UNITTEST_DIR}/UnitTestMain/TestMain.cpp)
target_link_libraries(gtest_main gtest)

set(POLLY_GTEST_AVAIL 1)
endif()
endif()

# Make sure the isl c files are built as fPIC
Expand Down
5 changes: 0 additions & 5 deletions polly/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
# Keep this in sync with llvm/cmake/CMakeLists.txt!
if (LLVM_INSTALL_ROOT)
# this simplifies things down the road, by not requiring to distinguish
# between in-tree and out of tree builds
set(LLVM_BINARY_DIR ${LLVM_INSTALL_ROOT})
endif()

set(LLVM_INSTALL_PACKAGE_DIR "lib${LLVM_LIBDIR_SUFFIX}/cmake/llvm")
set(POLLY_INSTALL_PACKAGE_DIR "lib${LLVM_LIBDIR_SUFFIX}/cmake/polly")
Expand Down
81 changes: 32 additions & 49 deletions polly/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,59 +79,42 @@ if (GPU_CODEGEN)
target_link_libraries(Polly PollyPPCG)
endif ()

# Add Polly's LLVM dependencies.
# When built inside the LLVM source tree, these are CMake targets that will
# depend on their dependencies and CMake ensures they are added in the right
# order.
# If Polly is built independently, just add all LLVM libraries. LLVM_ROOT_DIR
# might have been configured to compile to individual libraries or a single
# libLLVM.so (called dylib), reported by llvm-config, so there is no fixed list
# of required libraries.
if (DEFINED LLVM_MAIN_SRC_DIR)

# Polly-ACC requires the NVPTX backend to work. Ask LLVM about its libraries.
set(nvptx_libs)
if (GPU_CODEGEN)
# This call emits an error if they NVPTX backend is not enable.
llvm_map_components_to_libnames(nvptx_libs NVPTX)
endif ()
# Polly-ACC requires the NVPTX backend to work. Ask LLVM about its libraries.
set(nvptx_libs)
if (GPU_CODEGEN)
# This call emits an error if they NVPTX backend is not enable.
llvm_map_components_to_libnames(nvptx_libs NVPTX)
endif ()

if (LLVM_LINK_LLVM_DYLIB)
# The shlib/dylib contains all the LLVM components
# (including NVPTX is enabled) already. Adding them to target_link_libraries
# would cause them being twice in the address space
# (their LLVM*.a/so and their copies in libLLVM.so)
# which results in errors when the two instances try to register the same
# command-line switches.
target_link_libraries(Polly LLVM)
else ()
target_link_libraries(Polly
LLVMSupport
LLVMCore
LLVMScalarOpts
LLVMInstCombine
LLVMTransformUtils
LLVMAnalysis
LLVMipo
LLVMMC
LLVMPasses
${nvptx_libs}
# The libraries below are required for darwin: http://PR26392
LLVMBitReader
LLVMMCParser
LLVMObject
LLVMProfileData
LLVMTarget
LLVMVectorize
)
endif ()
if (LLVM_LINK_LLVM_DYLIB)
# The shlib/dylib contains all the LLVM components
# (including NVPTX is enabled) already. Adding them to target_link_libraries
# would cause them being twice in the address space
# (their LLVM*.a/so and their copies in libLLVM.so)
# which results in errors when the two instances try to register the same
# command-line switches.
target_link_libraries(Polly LLVM)
else ()
# When Polly-ACC is enabled, we assume that the host LLVM was also built with
# the NVPTX target enabled.
target_link_libraries(Polly
${LLVM_LIBS}
${LLVM_SYSTEM_LIBS}
)
LLVMSupport
LLVMCore
LLVMScalarOpts
LLVMInstCombine
LLVMTransformUtils
LLVMAnalysis
LLVMipo
LLVMMC
LLVMPasses
${nvptx_libs}
# The libraries below are required for darwin: http://PR26392
LLVMBitReader
LLVMMCParser
LLVMObject
LLVMProfileData
LLVMTarget
LLVMVectorize
)
endif ()

# Create a loadable module Polly.so that can be loaded using
Expand Down
Loading

0 comments on commit d99c406

Please sign in to comment.