fb62dbd3a09bea1173db48f5cbca184ff230a617
[platform/upstream/llvm.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 if(NOT DEFINED LLVM_COMMON_CMAKE_UTILS)
52   set(LLVM_COMMON_CMAKE_UTILS ${POLLY_SOURCE_DIR}/../cmake)
53 endif()
54
55 # Make sure that our source directory is on the current cmake module path so that
56 # we can include cmake files from this directory.
57 list(INSERT CMAKE_MODULE_PATH 0
58   "${POLLY_SOURCE_DIR}/cmake"
59   "${LLVM_COMMON_CMAKE_UTILS}/Modules"
60   )
61
62 include("polly_macros")
63
64 # Add appropriate flags for GCC
65 if (CMAKE_COMPILER_IS_GNUCXX)
66   # FIXME: Turn off exceptions, RTTI:
67   # -fno-exceptions -fno-rtti
68   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")
69 elseif (MSVC)
70   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHs-c-")
71   add_definitions("-D_HAS_EXCEPTIONS=0")
72 else ()
73   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions -fno-rtti")
74 endif ()
75
76 SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
77
78 option(POLLY_ENABLE_GPGPU_CODEGEN "Enable GPGPU code generation feature" OFF)
79 set(GPU_CODEGEN FALSE)
80 if (POLLY_ENABLE_GPGPU_CODEGEN)
81   # Do not require CUDA/OpenCL, as GPU code generation test cases can be run
82   # without a CUDA/OpenCL library.
83   if ("NVPTX" IN_LIST LLVM_TARGETS_TO_BUILD)
84     FIND_PACKAGE(CUDA)
85     FIND_PACKAGE(OpenCL)
86     set(GPU_CODEGEN TRUE)
87   else()
88     message(WARNING "The LLVM NVPTX target is required for GPU code generation")
89   endif()
90 endif(POLLY_ENABLE_GPGPU_CODEGEN)
91
92
93 # Support GPGPU code generation if the library is available.
94 if (CUDA_FOUND)
95   add_definitions(-DHAS_LIBCUDART)
96   INCLUDE_DIRECTORIES( ${CUDA_INCLUDE_DIRS} )
97 endif(CUDA_FOUND)
98 if (OpenCL_FOUND)
99   add_definitions(-DHAS_LIBOPENCL)
100   INCLUDE_DIRECTORIES( ${OpenCL_INCLUDE_DIR} )
101 endif(OpenCL_FOUND)
102
103 option(POLLY_BUNDLED_ISL "Use the bundled version of libisl included in Polly" ON)
104 if (NOT POLLY_BUNDLED_ISL)
105   find_package(ISL MODULE REQUIRED)
106   message(STATUS "Using external libisl ${ISL_VERSION} in: ${ISL_PREFIX}")
107   set(ISL_TARGET ISL)
108 else()
109   set(ISL_INCLUDE_DIRS
110     ${CMAKE_CURRENT_BINARY_DIR}/lib/External/isl/include
111     ${CMAKE_CURRENT_SOURCE_DIR}/lib/External/isl/include
112   )
113   set(ISL_TARGET PollyISL)
114 endif()
115
116 include_directories(
117   BEFORE
118   ${CMAKE_CURRENT_SOURCE_DIR}/include
119   ${ISL_INCLUDE_DIRS}
120   ${CMAKE_CURRENT_SOURCE_DIR}/lib/External/pet/include
121   ${CMAKE_CURRENT_SOURCE_DIR}/lib/External
122   ${CMAKE_CURRENT_BINARY_DIR}/include
123   )
124
125 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
126   install(DIRECTORY include/
127     DESTINATION include
128     FILES_MATCHING
129     PATTERN "*.h"
130     )
131
132   install(DIRECTORY ${POLLY_BINARY_DIR}/include/
133     DESTINATION include
134     FILES_MATCHING
135     PATTERN "*.h"
136     PATTERN "CMakeFiles" EXCLUDE
137     )
138 endif()
139
140 add_definitions( -D_GNU_SOURCE )
141
142 add_subdirectory(docs)
143 add_subdirectory(lib)
144 add_subdirectory(test)
145 if (POLLY_GTEST_AVAIL)
146   add_subdirectory(unittests)
147 endif ()
148 add_subdirectory(tools)
149 add_subdirectory(cmake)
150 # TODO: docs.
151
152
153 configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/include/polly/Config/config.h.cmake
154                 ${POLLY_BINARY_DIR}/include/polly/Config/config.h )
155
156 # Add target to check formatting of polly files
157 file( GLOB_RECURSE files *.h lib/*.cpp lib/*.c tools/*.cpp tools/*.c tools/*.h unittests/*.cpp)
158 file( GLOB_RECURSE external lib/External/*.h lib/External/*.c lib/External/*.cpp isl_config.h)
159 list( REMOVE_ITEM files ${external})
160
161 set(check_format_depends)
162 set(update_format_depends)
163 set(i 0)
164 foreach (file IN LISTS files)
165   add_custom_command(OUTPUT polly-check-format${i}
166     COMMAND clang-format -sort-includes -style=llvm ${file} | diff -u ${file} -
167     VERBATIM
168     COMMENT "Checking format of ${file}..."
169   )
170   list(APPEND check_format_depends "polly-check-format${i}")
171
172   add_custom_command(OUTPUT polly-update-format${i}
173     COMMAND clang-format -sort-includes -i -style=llvm ${file}
174     VERBATIM
175     COMMENT "Updating format of ${file}..."
176   )
177   list(APPEND update_format_depends "polly-update-format${i}")
178
179   math(EXPR i ${i}+1)
180 endforeach ()
181
182 add_custom_target(polly-check-format DEPENDS ${check_format_depends})
183 set_target_properties(polly-check-format PROPERTIES FOLDER "Polly")
184
185 add_custom_target(polly-update-format DEPENDS ${update_format_depends})
186 set_target_properties(polly-update-format PROPERTIES FOLDER "Polly")
187