Merge branch 'addmainunit3-altlink' into addmainunit3-altlink-sharedstmt
[lldb.git] / polly / CMakeLists.txt
1 # Check if this is a in tree build.
2 if (NOT DEFINED LLVM_MAIN_SRC_DIR)
3   project(Polly)
4   cmake_minimum_required(VERSION 3.13.4)
5
6   # Where is LLVM installed?
7   find_package(LLVM CONFIG REQUIRED)
8   set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${LLVM_CMAKE_DIR})
9   include(HandleLLVMOptions)
10   include(AddLLVM)
11
12   # Add the llvm header path.
13   include_directories(${LLVM_INCLUDE_DIRS})
14
15   # Sources available, too?
16   if (LLVM_BUILD_MAIN_SRC_DIR)
17     set(LLVM_SOURCE_ROOT ${LLVM_BUILD_MAIN_SRC_DIR} CACHE PATH
18       "Path to LLVM source tree")
19   else()
20     execute_process(COMMAND "${LLVM_TOOLS_BINARY_DIR}/llvm-config" --src-root
21       OUTPUT_VARIABLE MAIN_SRC_DIR
22       OUTPUT_STRIP_TRAILING_WHITESPACE)
23     set(LLVM_SOURCE_ROOT ${MAIN_SRC_DIR} CACHE PATH "Path to LLVM source tree")
24   endif()
25
26   # Enable unit tests if available.
27   set(POLLY_GTEST_AVAIL 0)
28   set(UNITTEST_DIR ${LLVM_SOURCE_ROOT}/utils/unittest)
29   if(EXISTS ${UNITTEST_DIR}/googletest/include/gtest/gtest.h)
30     if (NOT TARGET gtest)
31       add_subdirectory(${UNITTEST_DIR} utils/unittest)
32     endif()
33     set(POLLY_GTEST_AVAIL 1)
34   endif()
35
36   if (LLVM_ENABLE_PIC)
37     # Make sure the isl c files are built as fPIC if possible
38     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
39   endif ()
40
41   # Set directory for polly-isl-test.
42   set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin")
43 else ()
44   set(LLVM_SOURCE_ROOT "${LLVM_MAIN_SRC_DIR}")
45   set(POLLY_GTEST_AVAIL 1)
46 endif ()
47
48 set(POLLY_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
49 set(POLLY_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
50
51 # Add path for custom modules
52 set(CMAKE_MODULE_PATH
53   ${CMAKE_MODULE_PATH}
54   "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
55   )
56
57 include("polly_macros")
58
59 # Add appropriate flags for GCC
60 if (CMAKE_COMPILER_IS_GNUCXX)
61   # FIXME: Turn off exceptions, RTTI:
62   # -fno-exceptions -fno-rtti
63   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-common -Woverloaded-virtual -Wno-long-long -Wall -W -Wno-unused-parameter -Wwrite-strings -fno-exceptions -fno-rtti")
64 elseif (MSVC)
65   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHs-c-")
66   add_definitions("-D_HAS_EXCEPTIONS=0")
67 else ()
68   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions -fno-rtti")
69 endif ()
70
71 # Add path for custom modules
72 set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${POLLY_SOURCE_DIR}/cmake")
73
74 SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
75
76 option(POLLY_ENABLE_GPGPU_CODEGEN "Enable GPGPU code generation feature" OFF)
77 set(GPU_CODEGEN FALSE)
78 if (POLLY_ENABLE_GPGPU_CODEGEN)
79   # Do not require CUDA/OpenCL, as GPU code generation test cases can be run
80   # without a CUDA/OpenCL library.
81   if ("NVPTX" IN_LIST LLVM_TARGETS_TO_BUILD)
82     FIND_PACKAGE(CUDA)
83     FIND_PACKAGE(OpenCL)
84     set(GPU_CODEGEN TRUE)
85   else()
86     message(WARNING "The LLVM NVPTX target is required for GPU code generation")
87   endif()
88 endif(POLLY_ENABLE_GPGPU_CODEGEN)
89
90
91 # Support GPGPU code generation if the library is available.
92 if (CUDA_FOUND)
93   add_definitions(-DHAS_LIBCUDART)
94   INCLUDE_DIRECTORIES( ${CUDA_INCLUDE_DIRS} )
95 endif(CUDA_FOUND)
96 if (OpenCL_FOUND)
97   add_definitions(-DHAS_LIBOPENCL)
98   INCLUDE_DIRECTORIES( ${OpenCL_INCLUDE_DIR} )
99 endif(OpenCL_FOUND)
100
101 option(POLLY_BUNDLED_ISL "Use the bundled version of libisl included in Polly" ON)
102 if (NOT POLLY_BUNDLED_ISL)
103   find_package(ISL MODULE REQUIRED)
104   message(STATUS "Using external libisl ${ISL_VERSION} in: ${ISL_PREFIX}")
105   set(ISL_TARGET ISL)
106 else()
107   set(ISL_INCLUDE_DIRS
108     ${CMAKE_CURRENT_BINARY_DIR}/lib/External/isl/include
109     ${CMAKE_CURRENT_SOURCE_DIR}/lib/External/isl/include
110   )
111   set(ISL_TARGET PollyISL)
112 endif()
113
114 include_directories(
115   BEFORE
116   ${CMAKE_CURRENT_SOURCE_DIR}/include
117   ${ISL_INCLUDE_DIRS}
118   ${CMAKE_CURRENT_SOURCE_DIR}/lib/External/pet/include
119   ${CMAKE_CURRENT_SOURCE_DIR}/lib/External
120   ${CMAKE_CURRENT_BINARY_DIR}/include
121   )
122
123 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
124   install(DIRECTORY include/
125     DESTINATION include
126     FILES_MATCHING
127     PATTERN "*.h"
128     )
129
130   install(DIRECTORY ${POLLY_BINARY_DIR}/include/
131     DESTINATION include
132     FILES_MATCHING
133     PATTERN "*.h"
134     PATTERN "CMakeFiles" EXCLUDE
135     )
136 endif()
137
138 add_definitions( -D_GNU_SOURCE )
139
140 add_subdirectory(docs)
141 add_subdirectory(lib)
142 add_subdirectory(test)
143 if (POLLY_GTEST_AVAIL)
144   add_subdirectory(unittests)
145 endif ()
146 add_subdirectory(tools)
147 add_subdirectory(cmake)
148 # TODO: docs.
149
150
151 configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/include/polly/Config/config.h.cmake
152                 ${POLLY_BINARY_DIR}/include/polly/Config/config.h )
153
154 # Add target to check formatting of polly files
155 file( GLOB_RECURSE files *.h lib/*.cpp lib/*.c tools/*.cpp tools/*.c tools/*.h unittests/*.cpp)
156 file( GLOB_RECURSE external lib/External/*.h lib/External/*.c lib/External/*.cpp isl_config.h)
157 list( REMOVE_ITEM files ${external})
158
159 set(check_format_depends)
160 set(update_format_depends)
161 set(i 0)
162 foreach (file IN LISTS files)
163   add_custom_command(OUTPUT polly-check-format${i}
164     COMMAND clang-format -sort-includes -style=llvm ${file} | diff -u ${file} -
165     VERBATIM
166     COMMENT "Checking format of ${file}..."
167   )
168   list(APPEND check_format_depends "polly-check-format${i}")
169
170   add_custom_command(OUTPUT polly-update-format${i}
171     COMMAND clang-format -sort-includes -i -style=llvm ${file}
172     VERBATIM
173     COMMENT "Updating format of ${file}..."
174   )
175   list(APPEND update_format_depends "polly-update-format${i}")
176
177   math(EXPR i ${i}+1)
178 endforeach ()
179
180 add_custom_target(polly-check-format DEPENDS ${check_format_depends})
181 set_target_properties(polly-check-format PROPERTIES FOLDER "Polly")
182
183 add_custom_target(polly-update-format DEPENDS ${update_format_depends})
184 set_target_properties(polly-update-format PROPERTIES FOLDER "Polly")
185