Oscar Fuentes | 1bbdd46 | 2008-11-14 22:06:14 +0000 | [diff] [blame] | 1 | include(LLVMProcessSources) |
Oscar Fuentes | e7510c2 | 2011-04-05 17:02:48 +0000 | [diff] [blame] | 2 | include(LLVM-Config) |
Oscar Fuentes | 3d01fc7 | 2008-09-22 01:08:49 +0000 | [diff] [blame] | 3 | |
| 4 | macro(add_llvm_library name) |
Oscar Fuentes | 50925fb | 2008-11-15 02:08:08 +0000 | [diff] [blame] | 5 | llvm_process_sources( ALL_FILES ${ARGN} ) |
| 6 | add_library( ${name} ${ALL_FILES} ) |
Oscar Fuentes | 6d857ca | 2011-02-18 22:06:14 +0000 | [diff] [blame] | 7 | set_property( GLOBAL APPEND PROPERTY LLVM_LIBS ${name} ) |
Oscar Fuentes | 1d8e4cf | 2008-09-22 18:21:51 +0000 | [diff] [blame] | 8 | if( LLVM_COMMON_DEPENDS ) |
| 9 | add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} ) |
| 10 | endif( LLVM_COMMON_DEPENDS ) |
Oscar Fuentes | 00a9618 | 2010-10-14 15:54:41 +0000 | [diff] [blame] | 11 | |
| 12 | if( BUILD_SHARED_LIBS ) |
Oscar Fuentes | 879d3a9 | 2011-03-12 16:48:54 +0000 | [diff] [blame] | 13 | llvm_config( ${name} ${LLVM_LINK_COMPONENTS} ) |
Oscar Fuentes | 00a9618 | 2010-10-14 15:54:41 +0000 | [diff] [blame] | 14 | endif() |
| 15 | |
Oscar Fuentes | 23f0bfe | 2011-03-29 20:51:08 +0000 | [diff] [blame] | 16 | # Ensure that the system libraries always comes last on the |
| 17 | # list. Without this, linking the unit tests on MinGW fails. |
| 18 | link_system_libs( ${name} ) |
| 19 | |
Nick Lewycky | bcffb1f | 2011-04-29 02:12:06 +0000 | [diff] [blame] | 20 | if( EXCLUDE_FROM_ALL ) |
| 21 | set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON) |
| 22 | else() |
| 23 | install(TARGETS ${name} |
| 24 | LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX} |
| 25 | ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX}) |
| 26 | endif() |
Oscar Fuentes | 0b85d07 | 2011-02-20 22:06:10 +0000 | [diff] [blame] | 27 | set_target_properties(${name} PROPERTIES FOLDER "Libraries") |
Daniel Dunbar | 288bc5c | 2011-11-29 01:31:52 +0000 | [diff] [blame] | 28 | |
| 29 | # Add the explicit dependency information for this library. |
| 30 | # |
| 31 | # It would be nice to verify that we have the dependencies for this library |
| 32 | # name, but using get_property(... SET) doesn't suffice to determine if a |
| 33 | # property has been set to an empty value. |
| 34 | get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${name}) |
| 35 | target_link_libraries(${name} ${lib_deps}) |
Oscar Fuentes | 3d01fc7 | 2008-09-22 01:08:49 +0000 | [diff] [blame] | 36 | endmacro(add_llvm_library name) |
| 37 | |
Oscar Fuentes | c5e1ae1 | 2009-11-10 02:45:37 +0000 | [diff] [blame] | 38 | macro(add_llvm_loadable_module name) |
Oscar Fuentes | 01a575e | 2010-10-22 19:03:24 +0000 | [diff] [blame] | 39 | if( NOT LLVM_ON_UNIX OR CYGWIN ) |
Oscar Fuentes | c5e1ae1 | 2009-11-10 02:45:37 +0000 | [diff] [blame] | 40 | message(STATUS "Loadable modules not supported on this platform. |
| 41 | ${name} ignored.") |
NAKAMURA Takumi | 51c06bf | 2010-12-10 02:15:36 +0000 | [diff] [blame] | 42 | # Add empty "phony" target |
| 43 | add_custom_target(${name}) |
Oscar Fuentes | c5e1ae1 | 2009-11-10 02:45:37 +0000 | [diff] [blame] | 44 | else() |
Oscar Fuentes | c5e1ae1 | 2009-11-10 02:45:37 +0000 | [diff] [blame] | 45 | llvm_process_sources( ALL_FILES ${ARGN} ) |
Oscar Fuentes | 7110428 | 2010-12-22 08:30:17 +0000 | [diff] [blame] | 46 | if (MODULE) |
| 47 | set(libkind MODULE) |
| 48 | else() |
| 49 | set(libkind SHARED) |
| 50 | endif() |
| 51 | |
| 52 | add_library( ${name} ${libkind} ${ALL_FILES} ) |
Oscar Fuentes | c5e1ae1 | 2009-11-10 02:45:37 +0000 | [diff] [blame] | 53 | set_target_properties( ${name} PROPERTIES PREFIX "" ) |
Douglas Gregor | 8781416 | 2009-11-10 15:30:33 +0000 | [diff] [blame] | 54 | |
Oscar Fuentes | 879d3a9 | 2011-03-12 16:48:54 +0000 | [diff] [blame] | 55 | llvm_config( ${name} ${LLVM_LINK_COMPONENTS} ) |
Oscar Fuentes | 23f0bfe | 2011-03-29 20:51:08 +0000 | [diff] [blame] | 56 | link_system_libs( ${name} ) |
Oscar Fuentes | 879d3a9 | 2011-03-12 16:48:54 +0000 | [diff] [blame] | 57 | |
Douglas Gregor | 8781416 | 2009-11-10 15:30:33 +0000 | [diff] [blame] | 58 | if (APPLE) |
| 59 | # Darwin-specific linker flags for loadable modules. |
| 60 | set_target_properties(${name} PROPERTIES |
| 61 | LINK_FLAGS "-Wl,-flat_namespace -Wl,-undefined -Wl,suppress") |
| 62 | endif() |
| 63 | |
Oscar Fuentes | ab92d1e | 2011-04-26 14:55:27 +0000 | [diff] [blame] | 64 | if( EXCLUDE_FROM_ALL ) |
Nick Lewycky | bcffb1f | 2011-04-29 02:12:06 +0000 | [diff] [blame] | 65 | set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON) |
Oscar Fuentes | ab92d1e | 2011-04-26 14:55:27 +0000 | [diff] [blame] | 66 | else() |
| 67 | install(TARGETS ${name} |
| 68 | LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX} |
| 69 | ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX}) |
| 70 | endif() |
Oscar Fuentes | c5e1ae1 | 2009-11-10 02:45:37 +0000 | [diff] [blame] | 71 | endif() |
Oscar Fuentes | 0b85d07 | 2011-02-20 22:06:10 +0000 | [diff] [blame] | 72 | |
| 73 | set_target_properties(${name} PROPERTIES FOLDER "Loadable modules") |
Oscar Fuentes | c5e1ae1 | 2009-11-10 02:45:37 +0000 | [diff] [blame] | 74 | endmacro(add_llvm_loadable_module name) |
| 75 | |
| 76 | |
Oscar Fuentes | 3d01fc7 | 2008-09-22 01:08:49 +0000 | [diff] [blame] | 77 | macro(add_llvm_executable name) |
Oscar Fuentes | 50925fb | 2008-11-15 02:08:08 +0000 | [diff] [blame] | 78 | llvm_process_sources( ALL_FILES ${ARGN} ) |
Oscar Fuentes | b8352de | 2009-11-23 00:21:43 +0000 | [diff] [blame] | 79 | if( EXCLUDE_FROM_ALL ) |
| 80 | add_executable(${name} EXCLUDE_FROM_ALL ${ALL_FILES}) |
| 81 | else() |
| 82 | add_executable(${name} ${ALL_FILES}) |
| 83 | endif() |
| 84 | set(EXCLUDE_FROM_ALL OFF) |
Oscar Fuentes | 23f0bfe | 2011-03-29 20:51:08 +0000 | [diff] [blame] | 85 | llvm_config( ${name} ${LLVM_LINK_COMPONENTS} ) |
Oscar Fuentes | d8b1b9a | 2009-08-14 04:38:57 +0000 | [diff] [blame] | 86 | if( LLVM_COMMON_DEPENDS ) |
| 87 | add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} ) |
| 88 | endif( LLVM_COMMON_DEPENDS ) |
Oscar Fuentes | 23f0bfe | 2011-03-29 20:51:08 +0000 | [diff] [blame] | 89 | link_system_libs( ${name} ) |
Oscar Fuentes | 3d01fc7 | 2008-09-22 01:08:49 +0000 | [diff] [blame] | 90 | endmacro(add_llvm_executable name) |
| 91 | |
| 92 | |
| 93 | macro(add_llvm_tool name) |
| 94 | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LLVM_TOOLS_BINARY_DIR}) |
Oscar Fuentes | 7be498e | 2009-11-23 00:32:42 +0000 | [diff] [blame] | 95 | if( NOT LLVM_BUILD_TOOLS ) |
Oscar Fuentes | b8352de | 2009-11-23 00:21:43 +0000 | [diff] [blame] | 96 | set(EXCLUDE_FROM_ALL ON) |
| 97 | endif() |
Oscar Fuentes | 066de85 | 2010-09-25 20:25:25 +0000 | [diff] [blame] | 98 | add_llvm_executable(${name} ${ARGN}) |
Oscar Fuentes | 7be498e | 2009-11-23 00:32:42 +0000 | [diff] [blame] | 99 | if( LLVM_BUILD_TOOLS ) |
| 100 | install(TARGETS ${name} RUNTIME DESTINATION bin) |
| 101 | endif() |
Oscar Fuentes | 0b85d07 | 2011-02-20 22:06:10 +0000 | [diff] [blame] | 102 | set_target_properties(${name} PROPERTIES FOLDER "Tools") |
Oscar Fuentes | 3d01fc7 | 2008-09-22 01:08:49 +0000 | [diff] [blame] | 103 | endmacro(add_llvm_tool name) |
| 104 | |
| 105 | |
| 106 | macro(add_llvm_example name) |
| 107 | # set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LLVM_EXAMPLES_BINARY_DIR}) |
Oscar Fuentes | 7be498e | 2009-11-23 00:32:42 +0000 | [diff] [blame] | 108 | if( NOT LLVM_BUILD_EXAMPLES ) |
Oscar Fuentes | b8352de | 2009-11-23 00:21:43 +0000 | [diff] [blame] | 109 | set(EXCLUDE_FROM_ALL ON) |
| 110 | endif() |
Oscar Fuentes | 066de85 | 2010-09-25 20:25:25 +0000 | [diff] [blame] | 111 | add_llvm_executable(${name} ${ARGN}) |
Oscar Fuentes | 7be498e | 2009-11-23 00:32:42 +0000 | [diff] [blame] | 112 | if( LLVM_BUILD_EXAMPLES ) |
| 113 | install(TARGETS ${name} RUNTIME DESTINATION examples) |
| 114 | endif() |
Oscar Fuentes | 0b85d07 | 2011-02-20 22:06:10 +0000 | [diff] [blame] | 115 | set_target_properties(${name} PROPERTIES FOLDER "Examples") |
Oscar Fuentes | 3d01fc7 | 2008-09-22 01:08:49 +0000 | [diff] [blame] | 116 | endmacro(add_llvm_example name) |
Oscar Fuentes | e1ad087 | 2008-09-26 04:40:32 +0000 | [diff] [blame] | 117 | |
| 118 | |
Oscar Fuentes | 0b85d07 | 2011-02-20 22:06:10 +0000 | [diff] [blame] | 119 | macro(add_llvm_utility name) |
| 120 | add_llvm_executable(${name} ${ARGN}) |
| 121 | set_target_properties(${name} PROPERTIES FOLDER "Utils") |
| 122 | endmacro(add_llvm_utility name) |
| 123 | |
| 124 | |
Oscar Fuentes | e1ad087 | 2008-09-26 04:40:32 +0000 | [diff] [blame] | 125 | macro(add_llvm_target target_name) |
Oscar Fuentes | d7808d2 | 2011-07-25 20:17:01 +0000 | [diff] [blame] | 126 | include_directories(BEFORE |
| 127 | ${CMAKE_CURRENT_BINARY_DIR} |
| 128 | ${CMAKE_CURRENT_SOURCE_DIR}) |
Douglas Gregor | 7e9e36a | 2009-06-23 17:57:35 +0000 | [diff] [blame] | 129 | add_llvm_library(LLVM${target_name} ${ARGN} ${TABLEGEN_OUTPUT}) |
Oscar Fuentes | 00d78f1 | 2011-02-20 02:55:27 +0000 | [diff] [blame] | 130 | set( CURRENT_LLVM_TARGET LLVM${target_name} ) |
Oscar Fuentes | e1ad087 | 2008-09-26 04:40:32 +0000 | [diff] [blame] | 131 | endmacro(add_llvm_target) |
Michael J. Spencer | 3c00db7 | 2012-04-26 19:43:35 +0000 | [diff] [blame] | 132 | |
| 133 | # Add external project that may want to be built as part of llvm such as Clang, |
| 134 | # lld, and Polly. This adds two options. One for the source directory of the |
| 135 | # project, which defaults to ${CMAKE_CURRENT_SOURCE_DIR}/${name}. Another to |
| 136 | # enable or disable building it with everthing else. |
| 137 | macro(add_llvm_external_project name) |
| 138 | string(TOUPPER ${name} nameUPPER) |
| 139 | set(LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${name}" |
| 140 | CACHE PATH "Path to ${name} source directory") |
| 141 | if (NOT ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} STREQUAL "" |
| 142 | AND EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}/CMakeLists.txt) |
| 143 | option(LLVM_EXTERNAL_${nameUPPER}_BUILD |
| 144 | "Whether to build ${name} as part of LLVM" ON) |
| 145 | if (LLVM_EXTERNAL_${nameUPPER}_BUILD) |
| 146 | add_subdirectory(${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} ${name}) |
| 147 | endif() |
| 148 | endif() |
| 149 | endmacro(add_llvm_external_project) |
Chandler Carruth | b60182e | 2012-06-21 05:16:58 +0000 | [diff] [blame] | 150 | |
| 151 | # Generic support for adding a unittest. |
Chandler Carruth | 7c888ee | 2012-06-21 09:51:26 +0000 | [diff] [blame] | 152 | function(add_unittest test_suite test_name) |
Chandler Carruth | b60182e | 2012-06-21 05:16:58 +0000 | [diff] [blame] | 153 | if (CMAKE_BUILD_TYPE) |
| 154 | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY |
Chandler Carruth | 7c888ee | 2012-06-21 09:51:26 +0000 | [diff] [blame] | 155 | ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_BUILD_TYPE}) |
Chandler Carruth | b60182e | 2012-06-21 05:16:58 +0000 | [diff] [blame] | 156 | else() |
Chandler Carruth | 7c888ee | 2012-06-21 09:51:26 +0000 | [diff] [blame] | 157 | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) |
Chandler Carruth | b60182e | 2012-06-21 05:16:58 +0000 | [diff] [blame] | 158 | endif() |
| 159 | if( NOT LLVM_BUILD_TESTS ) |
| 160 | set(EXCLUDE_FROM_ALL ON) |
| 161 | endif() |
| 162 | |
| 163 | add_llvm_executable(${test_name} ${ARGN}) |
| 164 | target_link_libraries(${test_name} |
| 165 | gtest |
| 166 | gtest_main |
| 167 | LLVMSupport # gtest needs it for raw_ostream. |
| 168 | ) |
| 169 | |
| 170 | add_dependencies(${test_suite} ${test_name}) |
| 171 | get_target_property(test_suite_folder ${test_suite} FOLDER) |
| 172 | if (NOT ${test_suite_folder} STREQUAL "NOTFOUND") |
| 173 | set_property(TARGET ${test_name} PROPERTY FOLDER "${test_suite_folder}") |
| 174 | endif () |
| 175 | |
| 176 | # Visual Studio 2012 only supports up to 8 template parameters in |
| 177 | # std::tr1::tuple by default, but gtest requires 10 |
| 178 | if (MSVC AND MSVC_VERSION EQUAL 1700) |
| 179 | set_property(TARGET ${test_name} APPEND PROPERTY COMPILE_DEFINITIONS _VARIADIC_MAX=10) |
| 180 | endif () |
| 181 | |
| 182 | include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include) |
| 183 | set_property(TARGET ${test_name} APPEND PROPERTY COMPILE_DEFINITIONS GTEST_HAS_RTTI=0) |
Chandler Carruth | b60182e | 2012-06-21 05:16:58 +0000 | [diff] [blame] | 184 | if (NOT LLVM_ENABLE_THREADS) |
| 185 | set_property(TARGET ${test_name} APPEND PROPERTY COMPILE_DEFINITIONS GTEST_HAS_PTHREAD=0) |
| 186 | endif () |
| 187 | |
Chandler Carruth | 343c32a | 2012-06-21 18:44:24 +0000 | [diff] [blame] | 188 | get_property(target_compile_flags TARGET ${test_name} PROPERTY COMPILE_FLAGS) |
| 189 | if (LLVM_COMPILER_IS_GCC_COMPATIBLE) |
| 190 | set(target_compile_flags "${target_compile_flags} -fno-rtti") |
| 191 | elseif (MSVC) |
| 192 | set(target_compile_flags "${target_compile_flags} /GR-") |
Chandler Carruth | b60182e | 2012-06-21 05:16:58 +0000 | [diff] [blame] | 193 | endif () |
Chandler Carruth | 343c32a | 2012-06-21 18:44:24 +0000 | [diff] [blame] | 194 | |
| 195 | if (SUPPORTS_NO_VARIADIC_MACROS_FLAG) |
| 196 | set(target_compile_flags "${target_compile_flags} -Wno-variadic-macros") |
| 197 | endif () |
| 198 | set_property(TARGET ${test_name} PROPERTY COMPILE_FLAGS "${target_compile_flags}") |
Chandler Carruth | b60182e | 2012-06-21 05:16:58 +0000 | [diff] [blame] | 199 | endfunction() |
Chandler Carruth | c502ed6 | 2012-06-28 06:36:24 +0000 | [diff] [blame] | 200 | |
| 201 | # This function provides an automatic way to 'configure'-like generate a file |
| 202 | # based on a set of common and custom variables, specifically targetting the |
| 203 | # variables needed for the 'lit.site.cfg' files. This function bundles the |
| 204 | # common variables that any Lit instance is likely to need, and custom |
| 205 | # variables can be passed in. |
| 206 | function(configure_lit_site_cfg input output) |
| 207 | foreach(c ${LLVM_TARGETS_TO_BUILD}) |
| 208 | set(TARGETS_BUILT "${TARGETS_BUILT} ${c}") |
| 209 | endforeach(c) |
| 210 | set(TARGETS_TO_BUILD ${TARGETS_BUILT}) |
| 211 | |
| 212 | set(SHLIBEXT "${LTDL_SHLIB_EXT}") |
| 213 | set(SHLIBDIR "${LLVM_BINARY_DIR}/lib/${CMAKE_CFG_INTDIR}") |
| 214 | |
| 215 | if(BUILD_SHARED_LIBS) |
| 216 | set(LLVM_SHARED_LIBS_ENABLED "1") |
| 217 | else() |
| 218 | set(LLVM_SHARED_LIBS_ENABLED "0") |
| 219 | endif(BUILD_SHARED_LIBS) |
| 220 | |
| 221 | if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") |
| 222 | set(SHLIBPATH_VAR "DYLD_LIBRARY_PATH") |
| 223 | else() # Default for all other unix like systems. |
| 224 | # CMake hardcodes the library locaction using rpath. |
| 225 | # Therefore LD_LIBRARY_PATH is not required to run binaries in the |
| 226 | # build dir. We pass it anyways. |
| 227 | set(SHLIBPATH_VAR "LD_LIBRARY_PATH") |
| 228 | endif() |
| 229 | |
| 230 | # Configuration-time: See Unit/lit.site.cfg.in |
| 231 | set(LLVM_BUILD_MODE "%(build_mode)s") |
| 232 | |
| 233 | set(LLVM_SOURCE_DIR ${LLVM_MAIN_SRC_DIR}) |
| 234 | set(LLVM_BINARY_DIR ${LLVM_BINARY_DIR}) |
| 235 | set(LLVM_TOOLS_DIR "${LLVM_TOOLS_BINARY_DIR}/%(build_config)s") |
| 236 | set(PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE}) |
| 237 | set(ENABLE_SHARED ${LLVM_SHARED_LIBS_ENABLED}) |
| 238 | set(SHLIBPATH_VAR ${SHLIBPATH_VAR}) |
| 239 | |
| 240 | if(LLVM_ENABLE_ASSERTIONS AND NOT MSVC_IDE) |
| 241 | set(ENABLE_ASSERTIONS "1") |
| 242 | else() |
| 243 | set(ENABLE_ASSERTIONS "0") |
| 244 | endif() |
| 245 | |
| 246 | set(HOST_OS ${CMAKE_HOST_SYSTEM_NAME}) |
| 247 | set(HOST_ARCH ${CMAKE_HOST_SYSTEM_PROCESSOR}) |
| 248 | |
| 249 | configure_file(${input} ${output} @ONLY) |
| 250 | endfunction() |