[VirtualInstruction] Do a lookup instead of a linear search. NFC.
[polly-mirror.git] / CMakeLists.txt
blob353b09749d4ba8c4142c5507c171b2602eaa2d2a
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.4.3)
6   # Where is LLVM installed?
7   set(LLVM_INSTALL_ROOT "" CACHE PATH "Root of LLVM install.")
8   # Check if the LLVM_INSTALL_ROOT valid.
9   if( NOT EXISTS ${LLVM_INSTALL_ROOT}/include/llvm )
10     message(FATAL_ERROR "LLVM_INSTALL_ROOT (${LLVM_INSTALL_ROOT}) is not a valid LLVM installation.")
11   endif(NOT EXISTS ${LLVM_INSTALL_ROOT}/include/llvm)
12   # FileCheck, not and llvm-lit are not install by default, warn the user to copy them.
13   if( NOT EXISTS ${LLVM_INSTALL_ROOT}/bin/FileCheck
14       OR NOT EXISTS ${LLVM_INSTALL_ROOT}/bin/not
15       OR NOT EXISTS ${LLVM_INSTALL_ROOT}/bin/llvm-lit )
16     message(WARNING "'FileCheck', 'not' and 'llvm-lit' are required by running regress tests, "
17                     "but they are not installed! Please copy them to "
18                     "${LLVM_INSTALL_ROOT}/bin.")
19   endif()
20   # Add the llvm header path.
21   include_directories(${LLVM_INSTALL_ROOT}/include/)
23   # Get LLVM's own libraries.
24   execute_process(COMMAND "${LLVM_INSTALL_ROOT}/bin/llvm-config" --libs
25                   OUTPUT_VARIABLE LLVM_LIBS
26                   OUTPUT_STRIP_TRAILING_WHITESPACE)
28   # Get the system librarys that will link into LLVM.
29   execute_process(COMMAND "${LLVM_INSTALL_ROOT}/bin/llvm-config" --system-libs
30                   OUTPUT_VARIABLE LLVM_SYSTEM_LIBS
31                   OUTPUT_STRIP_TRAILING_WHITESPACE)
32   message(STATUS "System libs required by LLVM: ${LLVM_SYSTEM_LIBS}")
34   # Determine where LLVM stores its libraries.
35   execute_process(COMMAND "${LLVM_INSTALL_ROOT}/bin/llvm-config" --libdir
36                   OUTPUT_VARIABLE LLVM_LIBRARY_DIR
37                   OUTPUT_STRIP_TRAILING_WHITESPACE)
38   link_directories("${LLVM_LIBRARY_DIR}")
40   # Now set the header paths.
41   execute_process(COMMAND "${LLVM_INSTALL_ROOT}/bin/llvm-config" --includedir
42                   OUTPUT_VARIABLE LLVM_INCLUDE_DIR
43                   OUTPUT_STRIP_TRAILING_WHITESPACE)
44   include_directories( ${LLVM_INCLUDE_DIR} )
46   # Get the TARGET_TRIPLE
47   execute_process(COMMAND "${LLVM_INSTALL_ROOT}/bin/llvm-config" --host-target
48                   OUTPUT_VARIABLE TARGET_TRIPLE
49                   OUTPUT_STRIP_TRAILING_WHITESPACE)
51   # And then set the cxx flags.
52   execute_process(COMMAND "${LLVM_INSTALL_ROOT}/bin/llvm-config" --cxxflags
53                   OUTPUT_VARIABLE LLVM_CXX_FLAGS
54                   OUTPUT_STRIP_TRAILING_WHITESPACE)
55   set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} ${LLVM_CXX_FLAGS})
57   # Check LLVM_ENABLE_ASSERTIONS
58   execute_process(COMMAND "${LLVM_INSTALL_ROOT}/bin/llvm-config" --assertion-mode
59                   OUTPUT_VARIABLE LLVM_ENABLE_ASSERTIONS
60                   OUTPUT_STRIP_TRAILING_WHITESPACE)
61   # Copied from LLVM's HandleLLVMOptions.cmake
62   if( LLVM_ENABLE_ASSERTIONS )
63     # MSVC doesn't like _DEBUG on release builds. See PR 4379.
64     if( NOT MSVC )
65       add_definitions( -D_DEBUG )
66     endif()
67     # On non-Debug builds cmake automatically defines NDEBUG, so we
68     # explicitly undefine it:
69     if( NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" )
70       add_definitions( -UNDEBUG )
71       # Also remove /D NDEBUG to avoid MSVC warnings about conflicting defines.
72       foreach (flags_var_to_scrub
73           CMAKE_CXX_FLAGS_RELEASE
74           CMAKE_CXX_FLAGS_RELWITHDEBINFO
75           CMAKE_CXX_FLAGS_MINSIZEREL
76           CMAKE_C_FLAGS_RELEASE
77           CMAKE_C_FLAGS_RELWITHDEBINFO
78           CMAKE_C_FLAGS_MINSIZEREL)
79         string (REGEX REPLACE "(^| )[/-]D *NDEBUG($| )" " "
80           "${flags_var_to_scrub}" "${${flags_var_to_scrub}}")
81       endforeach()
82     endif()
83   endif()
85   # Sources available, too?
86   execute_process(COMMAND "${LLVM_INSTALL_ROOT}/bin/llvm-config" --src-root
87                   OUTPUT_VARIABLE MAIN_SRC_DIR
88                   OUTPUT_STRIP_TRAILING_WHITESPACE)
89   set(LLVM_SOURCE_ROOT ${MAIN_SRC_DIR} CACHE PATH "Path to LLVM source tree")
91   # Enable unit tests if available.
92   set(UNITTEST_DIR ${LLVM_SOURCE_ROOT}/utils/unittest)
93   if(EXISTS ${UNITTEST_DIR}/googletest/include/gtest/gtest.h)
94     add_library(gtest
95       ${UNITTEST_DIR}/googletest/src/gtest-all.cc
96       ${UNITTEST_DIR}/googlemock/src/gmock-all.cc
97     )
98     target_include_directories(gtest
99       PUBLIC
100       "${UNITTEST_DIR}/googletest/include"
101       "${UNITTEST_DIR}/googlemock/include"
103       PRIVATE
104       "${UNITTEST_DIR}/googletest"
105       "${UNITTEST_DIR}/googlemock"
106     )
107     target_link_libraries(gtest -lpthread)
109     add_library(gtest_main ${UNITTEST_DIR}/UnitTestMain/TestMain.cpp)
110     target_link_libraries(gtest_main gtest)
112     set(POLLY_GTEST_AVAIL 1)
113   endif()
115   # Make sure the isl c files are built as fPIC
116   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
118   # Set directory for polly-isl-test.
119   set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin")
120 else ()
121   set(LLVM_SOURCE_ROOT "${LLVM_MAIN_SRC_DIR}")
122   set(POLLY_GTEST_AVAIL 1)
123 endif ()
125 set(POLLY_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
126 set(POLLY_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
128 # Add path for custom modules
129 set(CMAKE_MODULE_PATH
130   ${CMAKE_MODULE_PATH}
131   "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
132   )
134 include("polly_macros")
136 # Add appropriate flags for GCC
137 if (CMAKE_COMPILER_IS_GNUCXX)
138   # FIXME: Turn off exceptions, RTTI:
139   # -fno-exceptions -fno-rtti
140   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")
141 elseif (MSVC)
142   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHs-c-")
143   add_definitions("-D_HAS_EXCEPTIONS=0")
144 else ()
145   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions -fno-rtti")
146 endif ()
148 # Add path for custom modules
149 set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${POLLY_SOURCE_DIR}/cmake")
151 SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
153 option(POLLY_ENABLE_GPGPU_CODEGEN "Enable GPGPU code generation feature" OFF)
154 if (POLLY_ENABLE_GPGPU_CODEGEN)
155   # Do not require CUDA/OpenCL, as GPU code generation test cases can be run
156   # without a CUDA/OpenCL library.
157   FIND_PACKAGE(CUDA)
158   FIND_PACKAGE(OpenCL)
159   set(GPU_CODEGEN TRUE)
160 else(POLLY_ENABLE_GPGPU_CODEGEN)
161   set(GPU_CODEGEN FALSE)
162 endif(POLLY_ENABLE_GPGPU_CODEGEN)
165 # Support GPGPU code generation if the library is available.
166 if (CUDALIB_FOUND)
167   add_definitions(-DHAS_LIBCUDART)
168   INCLUDE_DIRECTORIES( ${CUDALIB_INCLUDE_DIR} )
169 endif(CUDALIB_FOUND)
170 if (OpenCL_FOUND)
171   add_definitions(-DHAS_LIBOPENCL)
172   INCLUDE_DIRECTORIES( ${OpenCL_INCLUDE_DIR} )
173 endif(OpenCL_FOUND)
175 option(POLLY_BUNDLED_ISL "Use the bundled version of libisl included in Polly" ON)
176 if (NOT POLLY_BUNDLED_ISL)
177   find_package(ISL MODULE REQUIRED)
178   message(STATUS "Using external libisl ${ISL_VERSION} in: ${ISL_PREFIX}")
179   set(ISL_TARGET ISL)
180 else()
181   set(ISL_INCLUDE_DIRS
182     ${CMAKE_CURRENT_BINARY_DIR}/lib/External/isl/include
183     ${CMAKE_CURRENT_SOURCE_DIR}/lib/External/isl/include
184   )
185   set(ISL_TARGET PollyISL)
186 endif()
188 option(POLLY_BUNDLED_JSONCPP "Use the bundled version of jsoncpp included in Polly" ON)
189 if (POLLY_BUNDLED_JSONCPP)
190   set(JSONCPP_INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/lib/External/JSON/include")
191   set(JSONCPP_LIBRARIES)
192   set(POLLY_JSON_FILES
193     External/JSON/json_reader.cpp
194     External/JSON/json_value.cpp
195     External/JSON/json_writer.cpp
196   )
197 else ()
198   find_package(Jsoncpp REQUIRED)
199   set(POLLY_JSON_FILES)
200 endif ()
202 include_directories(
203   BEFORE
204   ${CMAKE_CURRENT_SOURCE_DIR}/include
205   ${ISL_INCLUDE_DIRS}
206   ${JSONCPP_INCLUDE_DIRS}
207   ${CMAKE_CURRENT_SOURCE_DIR}/lib/External/pet/include
208   ${CMAKE_CURRENT_SOURCE_DIR}/lib/External
209   ${CMAKE_CURRENT_BINARY_DIR}/include
210   )
212 if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
213   install(DIRECTORY include/
214     DESTINATION include
215     FILES_MATCHING
216     PATTERN "*.h"
217     PATTERN ".svn" EXCLUDE
218     )
220   install(DIRECTORY ${POLLY_BINARY_DIR}/include/
221     DESTINATION include
222     FILES_MATCHING
223     PATTERN "*.h"
224     PATTERN "CMakeFiles" EXCLUDE
225     PATTERN ".svn" EXCLUDE
226     )
227 endif()
229 add_definitions( -D_GNU_SOURCE )
231 add_subdirectory(docs)
232 add_subdirectory(lib)
233 add_subdirectory(test)
234 if (POLLY_GTEST_AVAIL)
235   add_subdirectory(unittests)
236 endif ()
237 add_subdirectory(tools)
238 add_subdirectory(cmake)
239 # TODO: docs.
242 configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/include/polly/Config/config.h.cmake
243                 ${POLLY_BINARY_DIR}/include/polly/Config/config.h )
245 # Add target to check formatting of polly files
246 file( GLOB_RECURSE files *.h lib/*.cpp lib/*.c tools/*.cpp tools/*.c tools/*.h unittests/*.cpp)
247 file( GLOB_RECURSE external lib/External/*.h lib/External/*.c lib/External/*.cpp)
248 list( REMOVE_ITEM files ${external})
250 set(check_format_depends)
251 set(update_format_depends)
252 set(i 0)
253 foreach (file IN LISTS files)
254   add_custom_command(OUTPUT polly-check-format${i}
255     COMMAND clang-format -sort-includes -style=llvm ${file} | diff -u ${file} -
256     VERBATIM
257     COMMENT "Checking format of ${file}..."
258   )
259   list(APPEND check_format_depends "polly-check-format${i}")
261   add_custom_command(OUTPUT polly-update-format${i}
262     COMMAND clang-format -sort-includes -i -style=llvm ${file}
263     VERBATIM
264     COMMENT "Updating format of ${file}..."
265   )
266   list(APPEND update_format_depends "polly-update-format${i}")
268   math(EXPR i ${i}+1)
269 endforeach ()
271 add_custom_target(polly-check-format DEPENDS ${check_format_depends})
272 set_target_properties(polly-check-format PROPERTIES FOLDER "Polly")
274 add_custom_target(polly-update-format DEPENDS ${update_format_depends})
275 set_target_properties(polly-update-format PROPERTIES FOLDER "Polly")