1 include(LLVMParseArguments) 2 include(LLVMProcessSources) 3 include(LLVM-Config) 4 5 macro(add_llvm_library name) 6 llvm_process_sources( ALL_FILES ${ARGN} ) 7 add_library( ${name} ${ALL_FILES} ) 8 set_property( GLOBAL APPEND PROPERTY LLVM_LIBS ${name} ) 9 if( LLVM_COMMON_DEPENDS ) 10 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} ) 11 endif( LLVM_COMMON_DEPENDS ) 12 13 if( BUILD_SHARED_LIBS ) 14 llvm_config( ${name} ${LLVM_LINK_COMPONENTS} ) 15 endif() 16 17 # Ensure that the system libraries always comes last on the 18 # list. Without this, linking the unit tests on MinGW fails. 19 link_system_libs( ${name} ) 20 21 if( EXCLUDE_FROM_ALL ) 22 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON) 23 else() 24 install(TARGETS ${name} 25 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX} 26 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX}) 27 endif() 28 set_target_properties(${name} PROPERTIES FOLDER "Libraries") 29 30 # Add the explicit dependency information for this library. 31 # 32 # It would be nice to verify that we have the dependencies for this library 33 # name, but using get_property(... SET) doesn't suffice to determine if a 34 # property has been set to an empty value. 35 get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${name}) 36 target_link_libraries(${name} ${lib_deps}) 37 endmacro(add_llvm_library name) 38 39 macro(add_llvm_loadable_module name) 40 if( NOT LLVM_ON_UNIX OR CYGWIN ) 41 message(STATUS "Loadable modules not supported on this platform. 42 ${name} ignored.") 43 # Add empty "phony" target 44 add_custom_target(${name}) 45 else() 46 llvm_process_sources( ALL_FILES ${ARGN} ) 47 if (MODULE) 48 set(libkind MODULE) 49 else() 50 set(libkind SHARED) 51 endif() 52 53 add_library( ${name} ${libkind} ${ALL_FILES} ) 54 set_target_properties( ${name} PROPERTIES PREFIX "" ) 55 56 llvm_config( ${name} ${LLVM_LINK_COMPONENTS} ) 57 link_system_libs( ${name} ) 58 59 if (APPLE) 60 # Darwin-specific linker flags for loadable modules. 61 set_target_properties(${name} PROPERTIES 62 LINK_FLAGS "-Wl,-flat_namespace -Wl,-undefined -Wl,suppress") 63 endif() 64 65 if( EXCLUDE_FROM_ALL ) 66 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON) 67 else() 68 install(TARGETS ${name} 69 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX} 70 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX}) 71 endif() 72 endif() 73 74 set_target_properties(${name} PROPERTIES FOLDER "Loadable modules") 75 endmacro(add_llvm_loadable_module name) 76 77 78 macro(add_llvm_executable name) 79 llvm_process_sources( ALL_FILES ${ARGN} ) 80 if( EXCLUDE_FROM_ALL ) 81 add_executable(${name} EXCLUDE_FROM_ALL ${ALL_FILES}) 82 else() 83 add_executable(${name} ${ALL_FILES}) 84 endif() 85 set(EXCLUDE_FROM_ALL OFF) 86 llvm_config( ${name} ${LLVM_LINK_COMPONENTS} ) 87 if( LLVM_COMMON_DEPENDS ) 88 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} ) 89 endif( LLVM_COMMON_DEPENDS ) 90 link_system_libs( ${name} ) 91 endmacro(add_llvm_executable name) 92 93 94 macro(add_llvm_tool name) 95 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LLVM_TOOLS_BINARY_DIR}) 96 if( NOT LLVM_BUILD_TOOLS ) 97 set(EXCLUDE_FROM_ALL ON) 98 endif() 99 add_llvm_executable(${name} ${ARGN}) 100 if( LLVM_BUILD_TOOLS ) 101 install(TARGETS ${name} RUNTIME DESTINATION bin) 102 endif() 103 set_target_properties(${name} PROPERTIES FOLDER "Tools") 104 endmacro(add_llvm_tool name) 105 106 107 macro(add_llvm_example name) 108 # set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LLVM_EXAMPLES_BINARY_DIR}) 109 if( NOT LLVM_BUILD_EXAMPLES ) 110 set(EXCLUDE_FROM_ALL ON) 111 endif() 112 add_llvm_executable(${name} ${ARGN}) 113 if( LLVM_BUILD_EXAMPLES ) 114 install(TARGETS ${name} RUNTIME DESTINATION examples) 115 endif() 116 set_target_properties(${name} PROPERTIES FOLDER "Examples") 117 endmacro(add_llvm_example name) 118 119 120 macro(add_llvm_utility name) 121 add_llvm_executable(${name} ${ARGN}) 122 set_target_properties(${name} PROPERTIES FOLDER "Utils") 123 endmacro(add_llvm_utility name) 124 125 126 macro(add_llvm_target target_name) 127 include_directories(BEFORE 128 ${CMAKE_CURRENT_BINARY_DIR} 129 ${CMAKE_CURRENT_SOURCE_DIR}) 130 add_llvm_library(LLVM${target_name} ${ARGN} ${TABLEGEN_OUTPUT}) 131 set( CURRENT_LLVM_TARGET LLVM${target_name} ) 132 endmacro(add_llvm_target) 133 134 # Add external project that may want to be built as part of llvm such as Clang, 135 # lld, and Polly. This adds two options. One for the source directory of the 136 # project, which defaults to ${CMAKE_CURRENT_SOURCE_DIR}/${name}. Another to 137 # enable or disable building it with everthing else. 138 # Additional parameter can be specified as the name of directory. 139 macro(add_llvm_external_project name) 140 set(add_llvm_external_dir "${ARGN}") 141 if("${add_llvm_external_dir}" STREQUAL "") 142 set(add_llvm_external_dir ${name}) 143 endif() 144 string(REPLACE "-" "_" nameUNDERSCORE ${name}) 145 string(TOUPPER ${nameUNDERSCORE} nameUPPER) 146 set(LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}" 147 CACHE PATH "Path to ${name} source directory") 148 if (NOT ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} STREQUAL "" 149 AND EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}/CMakeLists.txt) 150 option(LLVM_EXTERNAL_${nameUPPER}_BUILD 151 "Whether to build ${name} as part of LLVM" ON) 152 if (LLVM_EXTERNAL_${nameUPPER}_BUILD) 153 add_subdirectory(${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} ${add_llvm_external_dir}) 154 endif() 155 endif() 156 endmacro(add_llvm_external_project) 157 158 # Generic support for adding a unittest. 159 function(add_unittest test_suite test_name) 160 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) 161 if( NOT LLVM_BUILD_TESTS ) 162 set(EXCLUDE_FROM_ALL ON) 163 endif() 164 165 add_llvm_executable(${test_name} ${ARGN}) 166 target_link_libraries(${test_name} 167 gtest 168 gtest_main 169 LLVMSupport # gtest needs it for raw_ostream. 170 ) 171 172 add_dependencies(${test_suite} ${test_name}) 173 get_target_property(test_suite_folder ${test_suite} FOLDER) 174 if (NOT ${test_suite_folder} STREQUAL "NOTFOUND") 175 set_property(TARGET ${test_name} PROPERTY FOLDER "${test_suite_folder}") 176 endif () 177 178 # Visual Studio 2012 only supports up to 8 template parameters in 179 # std::tr1::tuple by default, but gtest requires 10 180 if (MSVC AND MSVC_VERSION EQUAL 1700) 181 set_property(TARGET ${test_name} APPEND PROPERTY COMPILE_DEFINITIONS _VARIADIC_MAX=10) 182 endif () 183 184 include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include) 185 set_property(TARGET ${test_name} APPEND PROPERTY COMPILE_DEFINITIONS GTEST_HAS_RTTI=0) 186 if (NOT LLVM_ENABLE_THREADS) 187 set_property(TARGET ${test_name} APPEND PROPERTY COMPILE_DEFINITIONS GTEST_HAS_PTHREAD=0) 188 endif () 189 190 get_property(target_compile_flags TARGET ${test_name} PROPERTY COMPILE_FLAGS) 191 if (LLVM_COMPILER_IS_GCC_COMPATIBLE) 192 set(target_compile_flags "${target_compile_flags} -fno-rtti") 193 elseif (MSVC) 194 llvm_replace_compiler_option(target_compile_flags "/GR" "/GR-") 195 endif () 196 197 if (SUPPORTS_NO_VARIADIC_MACROS_FLAG) 198 set(target_compile_flags "${target_compile_flags} -Wno-variadic-macros") 199 endif () 200 set_property(TARGET ${test_name} PROPERTY COMPILE_FLAGS "${target_compile_flags}") 201 endfunction() 202 203 # This function provides an automatic way to 'configure'-like generate a file 204 # based on a set of common and custom variables, specifically targetting the 205 # variables needed for the 'lit.site.cfg' files. This function bundles the 206 # common variables that any Lit instance is likely to need, and custom 207 # variables can be passed in. 208 function(configure_lit_site_cfg input output) 209 foreach(c ${LLVM_TARGETS_TO_BUILD}) 210 set(TARGETS_BUILT "${TARGETS_BUILT} ${c}") 211 endforeach(c) 212 set(TARGETS_TO_BUILD ${TARGETS_BUILT}) 213 214 set(SHLIBEXT "${LTDL_SHLIB_EXT}") 215 set(SHLIBDIR "${LLVM_BINARY_DIR}/lib/${CMAKE_CFG_INTDIR}") 216 217 if(BUILD_SHARED_LIBS) 218 set(LLVM_SHARED_LIBS_ENABLED "1") 219 else() 220 set(LLVM_SHARED_LIBS_ENABLED "0") 221 endif(BUILD_SHARED_LIBS) 222 223 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") 224 set(SHLIBPATH_VAR "DYLD_LIBRARY_PATH") 225 else() # Default for all other unix like systems. 226 # CMake hardcodes the library locaction using rpath. 227 # Therefore LD_LIBRARY_PATH is not required to run binaries in the 228 # build dir. We pass it anyways. 229 set(SHLIBPATH_VAR "LD_LIBRARY_PATH") 230 endif() 231 232 # Configuration-time: See Unit/lit.site.cfg.in 233 set(LLVM_BUILD_MODE "%(build_mode)s") 234 235 set(LLVM_SOURCE_DIR ${LLVM_MAIN_SRC_DIR}) 236 set(LLVM_BINARY_DIR ${LLVM_BINARY_DIR}) 237 set(LLVM_TOOLS_DIR "${LLVM_TOOLS_BINARY_DIR}/%(build_mode)s") 238 set(LLVM_LIBS_DIR "${LLVM_BINARY_DIR}/lib/%(build_mode)s") 239 set(PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE}) 240 set(ENABLE_SHARED ${LLVM_SHARED_LIBS_ENABLED}) 241 set(SHLIBPATH_VAR ${SHLIBPATH_VAR}) 242 243 if(LLVM_ENABLE_ASSERTIONS AND NOT MSVC_IDE) 244 set(ENABLE_ASSERTIONS "1") 245 else() 246 set(ENABLE_ASSERTIONS "0") 247 endif() 248 249 set(HOST_OS ${CMAKE_SYSTEM_NAME}) 250 set(HOST_ARCH ${CMAKE_SYSTEM_PROCESSOR}) 251 252 configure_file(${input} ${output} @ONLY) 253 endfunction() 254 255 # A raw function to create a lit target. This is used to implement the testuite 256 # management functions. 257 function(add_lit_target target comment) 258 parse_arguments(ARG "PARAMS;DEPENDS;ARGS" "" ${ARGN}) 259 set(LIT_ARGS "${ARG_ARGS} ${LLVM_LIT_ARGS}") 260 separate_arguments(LIT_ARGS) 261 set(LIT_COMMAND 262 ${PYTHON_EXECUTABLE} 263 ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py 264 --param build_mode=${CMAKE_CFG_INTDIR} 265 ${LIT_ARGS} 266 ) 267 foreach(param ${ARG_PARAMS}) 268 list(APPEND LIT_COMMAND --param ${param}) 269 endforeach() 270 if( ARG_DEPENDS ) 271 add_custom_target(${target} 272 COMMAND ${LIT_COMMAND} ${ARG_DEFAULT_ARGS} 273 COMMENT "${comment}" 274 ) 275 add_dependencies(${target} ${ARG_DEPENDS}) 276 else() 277 add_custom_target(${target} 278 COMMAND cmake -E echo "${target} does nothing, no tools built.") 279 message(STATUS "${target} does nothing.") 280 endif() 281 endfunction() 282 283 # A function to add a set of lit test suites to be driven through 'check-*' targets. 284 function(add_lit_testsuite target comment) 285 parse_arguments(ARG "PARAMS;DEPENDS;ARGS" "" ${ARGN}) 286 287 # EXCLUDE_FROM_ALL excludes the test ${target} out of check-all. 288 if(NOT EXCLUDE_FROM_ALL) 289 # Register the testsuites, params and depends for the global check rule. 290 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_TESTSUITES ${ARG_DEFAULT_ARGS}) 291 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_PARAMS ${ARG_PARAMS}) 292 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_DEPENDS ${ARG_DEPENDS}) 293 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_EXTRA_ARGS ${ARG_ARGS}) 294 endif() 295 296 # Produce a specific suffixed check rule. 297 add_lit_target(${target} ${comment} 298 ${ARG_DEFAULT_ARGS} 299 PARAMS ${ARG_PARAMS} 300 DEPENDS ${ARG_DEPENDS} 301 ARGS ${ARG_ARGS} 302 ) 303 endfunction() 304