blob: 277914ff906a907d7c50824e1a1f180238ffddf9 [file] [log] [blame]
Chandler Carruthcf23bd32012-06-30 10:14:14 +00001include(LLVMParseArguments)
Oscar Fuentes1bbdd462008-11-14 22:06:14 +00002include(LLVMProcessSources)
Oscar Fuentese7510c22011-04-05 17:02:48 +00003include(LLVM-Config)
Oscar Fuentes3d01fc72008-09-22 01:08:49 +00004
5macro(add_llvm_library name)
Oscar Fuentes50925fb2008-11-15 02:08:08 +00006 llvm_process_sources( ALL_FILES ${ARGN} )
7 add_library( ${name} ${ALL_FILES} )
Oscar Fuentes6d857ca2011-02-18 22:06:14 +00008 set_property( GLOBAL APPEND PROPERTY LLVM_LIBS ${name} )
Oscar Fuentes1d8e4cf2008-09-22 18:21:51 +00009 if( LLVM_COMMON_DEPENDS )
10 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
11 endif( LLVM_COMMON_DEPENDS )
Oscar Fuentes00a96182010-10-14 15:54:41 +000012
13 if( BUILD_SHARED_LIBS )
Oscar Fuentes879d3a92011-03-12 16:48:54 +000014 llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
NAKAMURA Takumie1bae5f2013-08-14 03:34:49 +000015 if (MSVC)
16 set_target_properties(${name}
17 PROPERTIES
18 IMPORT_SUFFIX ".imp")
19 endif ()
Oscar Fuentes00a96182010-10-14 15:54:41 +000020 endif()
21
Oscar Fuentes23f0bfe2011-03-29 20:51:08 +000022 # Ensure that the system libraries always comes last on the
23 # list. Without this, linking the unit tests on MinGW fails.
24 link_system_libs( ${name} )
25
Nick Lewyckybcffb1f2011-04-29 02:12:06 +000026 if( EXCLUDE_FROM_ALL )
27 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
28 else()
29 install(TARGETS ${name}
30 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
31 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
32 endif()
Oscar Fuentes0b85d072011-02-20 22:06:10 +000033 set_target_properties(${name} PROPERTIES FOLDER "Libraries")
Daniel Dunbar288bc5c2011-11-29 01:31:52 +000034
35 # Add the explicit dependency information for this library.
36 #
37 # It would be nice to verify that we have the dependencies for this library
38 # name, but using get_property(... SET) doesn't suffice to determine if a
39 # property has been set to an empty value.
40 get_property(lib_deps GLOBAL PROPERTY LLVMBUILD_LIB_DEPS_${name})
41 target_link_libraries(${name} ${lib_deps})
Oscar Fuentes3d01fc72008-09-22 01:08:49 +000042endmacro(add_llvm_library name)
43
Oscar Fuentesc5e1ae12009-11-10 02:45:37 +000044macro(add_llvm_loadable_module name)
Oscar Fuentes01a575e2010-10-22 19:03:24 +000045 if( NOT LLVM_ON_UNIX OR CYGWIN )
Oscar Fuentesc5e1ae12009-11-10 02:45:37 +000046 message(STATUS "Loadable modules not supported on this platform.
47${name} ignored.")
NAKAMURA Takumi51c06bf2010-12-10 02:15:36 +000048 # Add empty "phony" target
49 add_custom_target(${name})
Oscar Fuentesc5e1ae12009-11-10 02:45:37 +000050 else()
Oscar Fuentesc5e1ae12009-11-10 02:45:37 +000051 llvm_process_sources( ALL_FILES ${ARGN} )
Oscar Fuentes71104282010-12-22 08:30:17 +000052 if (MODULE)
53 set(libkind MODULE)
54 else()
55 set(libkind SHARED)
56 endif()
57
58 add_library( ${name} ${libkind} ${ALL_FILES} )
Oscar Fuentesc5e1ae12009-11-10 02:45:37 +000059 set_target_properties( ${name} PROPERTIES PREFIX "" )
Douglas Gregor87814162009-11-10 15:30:33 +000060
Oscar Fuentes879d3a92011-03-12 16:48:54 +000061 llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
Oscar Fuentes23f0bfe2011-03-29 20:51:08 +000062 link_system_libs( ${name} )
Oscar Fuentes879d3a92011-03-12 16:48:54 +000063
Douglas Gregor87814162009-11-10 15:30:33 +000064 if (APPLE)
65 # Darwin-specific linker flags for loadable modules.
66 set_target_properties(${name} PROPERTIES
67 LINK_FLAGS "-Wl,-flat_namespace -Wl,-undefined -Wl,suppress")
68 endif()
69
Oscar Fuentesab92d1e2011-04-26 14:55:27 +000070 if( EXCLUDE_FROM_ALL )
Nick Lewyckybcffb1f2011-04-29 02:12:06 +000071 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
Oscar Fuentesab92d1e2011-04-26 14:55:27 +000072 else()
73 install(TARGETS ${name}
Gabor Greif9ae2dfd2012-07-12 13:18:13 +000074 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
75 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
Oscar Fuentesab92d1e2011-04-26 14:55:27 +000076 endif()
Oscar Fuentesc5e1ae12009-11-10 02:45:37 +000077 endif()
Oscar Fuentes0b85d072011-02-20 22:06:10 +000078
79 set_target_properties(${name} PROPERTIES FOLDER "Loadable modules")
Oscar Fuentesc5e1ae12009-11-10 02:45:37 +000080endmacro(add_llvm_loadable_module name)
81
82
Oscar Fuentes3d01fc72008-09-22 01:08:49 +000083macro(add_llvm_executable name)
Oscar Fuentes50925fb2008-11-15 02:08:08 +000084 llvm_process_sources( ALL_FILES ${ARGN} )
Oscar Fuentesb8352de2009-11-23 00:21:43 +000085 if( EXCLUDE_FROM_ALL )
86 add_executable(${name} EXCLUDE_FROM_ALL ${ALL_FILES})
87 else()
88 add_executable(${name} ${ALL_FILES})
89 endif()
90 set(EXCLUDE_FROM_ALL OFF)
Oscar Fuentes23f0bfe2011-03-29 20:51:08 +000091 llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
Oscar Fuentesd8b1b9a2009-08-14 04:38:57 +000092 if( LLVM_COMMON_DEPENDS )
93 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
94 endif( LLVM_COMMON_DEPENDS )
Oscar Fuentes23f0bfe2011-03-29 20:51:08 +000095 link_system_libs( ${name} )
Oscar Fuentes3d01fc72008-09-22 01:08:49 +000096endmacro(add_llvm_executable name)
97
98
99macro(add_llvm_tool name)
100 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LLVM_TOOLS_BINARY_DIR})
Oscar Fuentes7be498e2009-11-23 00:32:42 +0000101 if( NOT LLVM_BUILD_TOOLS )
Oscar Fuentesb8352de2009-11-23 00:21:43 +0000102 set(EXCLUDE_FROM_ALL ON)
103 endif()
Oscar Fuentes066de852010-09-25 20:25:25 +0000104 add_llvm_executable(${name} ${ARGN})
Oscar Fuentes7be498e2009-11-23 00:32:42 +0000105 if( LLVM_BUILD_TOOLS )
106 install(TARGETS ${name} RUNTIME DESTINATION bin)
107 endif()
Oscar Fuentes0b85d072011-02-20 22:06:10 +0000108 set_target_properties(${name} PROPERTIES FOLDER "Tools")
Oscar Fuentes3d01fc72008-09-22 01:08:49 +0000109endmacro(add_llvm_tool name)
110
111
112macro(add_llvm_example name)
113# set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LLVM_EXAMPLES_BINARY_DIR})
Oscar Fuentes7be498e2009-11-23 00:32:42 +0000114 if( NOT LLVM_BUILD_EXAMPLES )
Oscar Fuentesb8352de2009-11-23 00:21:43 +0000115 set(EXCLUDE_FROM_ALL ON)
116 endif()
Oscar Fuentes066de852010-09-25 20:25:25 +0000117 add_llvm_executable(${name} ${ARGN})
Oscar Fuentes7be498e2009-11-23 00:32:42 +0000118 if( LLVM_BUILD_EXAMPLES )
119 install(TARGETS ${name} RUNTIME DESTINATION examples)
120 endif()
Oscar Fuentes0b85d072011-02-20 22:06:10 +0000121 set_target_properties(${name} PROPERTIES FOLDER "Examples")
Oscar Fuentes3d01fc72008-09-22 01:08:49 +0000122endmacro(add_llvm_example name)
Oscar Fuentese1ad0872008-09-26 04:40:32 +0000123
124
Oscar Fuentes0b85d072011-02-20 22:06:10 +0000125macro(add_llvm_utility name)
126 add_llvm_executable(${name} ${ARGN})
127 set_target_properties(${name} PROPERTIES FOLDER "Utils")
128endmacro(add_llvm_utility name)
129
130
Oscar Fuentese1ad0872008-09-26 04:40:32 +0000131macro(add_llvm_target target_name)
Oscar Fuentesd7808d22011-07-25 20:17:01 +0000132 include_directories(BEFORE
133 ${CMAKE_CURRENT_BINARY_DIR}
134 ${CMAKE_CURRENT_SOURCE_DIR})
Douglas Gregor7e9e36a2009-06-23 17:57:35 +0000135 add_llvm_library(LLVM${target_name} ${ARGN} ${TABLEGEN_OUTPUT})
Oscar Fuentes00d78f12011-02-20 02:55:27 +0000136 set( CURRENT_LLVM_TARGET LLVM${target_name} )
Oscar Fuentese1ad0872008-09-26 04:40:32 +0000137endmacro(add_llvm_target)
Michael J. Spencer3c00db72012-04-26 19:43:35 +0000138
139# Add external project that may want to be built as part of llvm such as Clang,
140# lld, and Polly. This adds two options. One for the source directory of the
141# project, which defaults to ${CMAKE_CURRENT_SOURCE_DIR}/${name}. Another to
142# enable or disable building it with everthing else.
NAKAMURA Takumi1e10bd62012-10-05 14:10:17 +0000143# Additional parameter can be specified as the name of directory.
Michael J. Spencer3c00db72012-04-26 19:43:35 +0000144macro(add_llvm_external_project name)
NAKAMURA Takumi1e10bd62012-10-05 14:10:17 +0000145 set(add_llvm_external_dir "${ARGN}")
146 if("${add_llvm_external_dir}" STREQUAL "")
147 set(add_llvm_external_dir ${name})
148 endif()
Argyrios Kyrtzidisd6dbd6b2013-08-21 19:13:44 +0000149 list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}")
NAKAMURA Takumi1e10bd62012-10-05 14:10:17 +0000150 string(REPLACE "-" "_" nameUNDERSCORE ${name})
151 string(TOUPPER ${nameUNDERSCORE} nameUPPER)
152 set(LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${add_llvm_external_dir}"
Michael J. Spencer3c00db72012-04-26 19:43:35 +0000153 CACHE PATH "Path to ${name} source directory")
154 if (NOT ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} STREQUAL ""
155 AND EXISTS ${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR}/CMakeLists.txt)
156 option(LLVM_EXTERNAL_${nameUPPER}_BUILD
157 "Whether to build ${name} as part of LLVM" ON)
158 if (LLVM_EXTERNAL_${nameUPPER}_BUILD)
NAKAMURA Takumi1e10bd62012-10-05 14:10:17 +0000159 add_subdirectory(${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} ${add_llvm_external_dir})
Michael J. Spencer3c00db72012-04-26 19:43:35 +0000160 endif()
161 endif()
162endmacro(add_llvm_external_project)
Chandler Carruthb60182e2012-06-21 05:16:58 +0000163
Argyrios Kyrtzidisd6dbd6b2013-08-21 19:13:44 +0000164macro(add_llvm_tool_subdirectory name)
165 list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${name}")
166 add_subdirectory(${name})
167endmacro(add_llvm_tool_subdirectory)
168
169macro(ignore_llvm_tool_subdirectory name)
170 list(APPEND LLVM_IMPLICIT_PROJECT_IGNORE "${CMAKE_CURRENT_SOURCE_DIR}/${name}")
171endmacro(ignore_llvm_tool_subdirectory)
172
173function(add_llvm_implicit_external_projects)
174 set(list_of_implicit_subdirs "")
175 file(GLOB sub-dirs "${CMAKE_CURRENT_SOURCE_DIR}/*")
176 foreach(dir ${sub-dirs})
177 if(IS_DIRECTORY "${dir}")
178 list(FIND LLVM_IMPLICIT_PROJECT_IGNORE "${dir}" tool_subdir_ignore)
179 if( tool_subdir_ignore EQUAL -1
180 AND EXISTS "${dir}/CMakeLists.txt")
181 get_filename_component(fn "${dir}" NAME)
182 list(APPEND list_of_implicit_subdirs "${fn}")
183 endif()
184 endif()
185 endforeach()
186
187 foreach(external_proj ${list_of_implicit_subdirs})
188 add_llvm_external_project("${external_proj}")
189 endforeach()
190endfunction(add_llvm_implicit_external_projects)
191
Chandler Carruthb60182e2012-06-21 05:16:58 +0000192# Generic support for adding a unittest.
Chandler Carruth7c888ee2012-06-21 09:51:26 +0000193function(add_unittest test_suite test_name)
NAKAMURA Takumi2044adf2013-01-27 12:46:53 +0000194 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
Chandler Carruthb60182e2012-06-21 05:16:58 +0000195 if( NOT LLVM_BUILD_TESTS )
196 set(EXCLUDE_FROM_ALL ON)
197 endif()
198
199 add_llvm_executable(${test_name} ${ARGN})
200 target_link_libraries(${test_name}
201 gtest
202 gtest_main
203 LLVMSupport # gtest needs it for raw_ostream.
204 )
205
206 add_dependencies(${test_suite} ${test_name})
207 get_target_property(test_suite_folder ${test_suite} FOLDER)
208 if (NOT ${test_suite_folder} STREQUAL "NOTFOUND")
209 set_property(TARGET ${test_name} PROPERTY FOLDER "${test_suite_folder}")
210 endif ()
211
212 # Visual Studio 2012 only supports up to 8 template parameters in
213 # std::tr1::tuple by default, but gtest requires 10
214 if (MSVC AND MSVC_VERSION EQUAL 1700)
215 set_property(TARGET ${test_name} APPEND PROPERTY COMPILE_DEFINITIONS _VARIADIC_MAX=10)
216 endif ()
217
218 include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include)
219 set_property(TARGET ${test_name} APPEND PROPERTY COMPILE_DEFINITIONS GTEST_HAS_RTTI=0)
Chandler Carruthb60182e2012-06-21 05:16:58 +0000220 if (NOT LLVM_ENABLE_THREADS)
221 set_property(TARGET ${test_name} APPEND PROPERTY COMPILE_DEFINITIONS GTEST_HAS_PTHREAD=0)
222 endif ()
223
Chandler Carruth343c32a2012-06-21 18:44:24 +0000224 get_property(target_compile_flags TARGET ${test_name} PROPERTY COMPILE_FLAGS)
225 if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
226 set(target_compile_flags "${target_compile_flags} -fno-rtti")
227 elseif (MSVC)
Aaron Ballmanf6599392013-08-06 23:34:04 +0000228 llvm_replace_compiler_option(target_compile_flags "/GR" "/GR-")
Chandler Carruthb60182e2012-06-21 05:16:58 +0000229 endif ()
Chandler Carruth343c32a2012-06-21 18:44:24 +0000230
231 if (SUPPORTS_NO_VARIADIC_MACROS_FLAG)
232 set(target_compile_flags "${target_compile_flags} -Wno-variadic-macros")
233 endif ()
234 set_property(TARGET ${test_name} PROPERTY COMPILE_FLAGS "${target_compile_flags}")
Chandler Carruthb60182e2012-06-21 05:16:58 +0000235endfunction()
Chandler Carruthc502ed62012-06-28 06:36:24 +0000236
237# This function provides an automatic way to 'configure'-like generate a file
238# based on a set of common and custom variables, specifically targetting the
239# variables needed for the 'lit.site.cfg' files. This function bundles the
240# common variables that any Lit instance is likely to need, and custom
241# variables can be passed in.
242function(configure_lit_site_cfg input output)
243 foreach(c ${LLVM_TARGETS_TO_BUILD})
244 set(TARGETS_BUILT "${TARGETS_BUILT} ${c}")
245 endforeach(c)
246 set(TARGETS_TO_BUILD ${TARGETS_BUILT})
247
248 set(SHLIBEXT "${LTDL_SHLIB_EXT}")
249 set(SHLIBDIR "${LLVM_BINARY_DIR}/lib/${CMAKE_CFG_INTDIR}")
250
251 if(BUILD_SHARED_LIBS)
252 set(LLVM_SHARED_LIBS_ENABLED "1")
253 else()
254 set(LLVM_SHARED_LIBS_ENABLED "0")
255 endif(BUILD_SHARED_LIBS)
256
257 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
258 set(SHLIBPATH_VAR "DYLD_LIBRARY_PATH")
259 else() # Default for all other unix like systems.
260 # CMake hardcodes the library locaction using rpath.
261 # Therefore LD_LIBRARY_PATH is not required to run binaries in the
262 # build dir. We pass it anyways.
263 set(SHLIBPATH_VAR "LD_LIBRARY_PATH")
264 endif()
265
266 # Configuration-time: See Unit/lit.site.cfg.in
267 set(LLVM_BUILD_MODE "%(build_mode)s")
268
269 set(LLVM_SOURCE_DIR ${LLVM_MAIN_SRC_DIR})
270 set(LLVM_BINARY_DIR ${LLVM_BINARY_DIR})
NAKAMURA Takumi1917aaf2013-01-27 12:20:50 +0000271 set(LLVM_TOOLS_DIR "${LLVM_TOOLS_BINARY_DIR}/%(build_mode)s")
272 set(LLVM_LIBS_DIR "${LLVM_BINARY_DIR}/lib/%(build_mode)s")
Chandler Carruthc502ed62012-06-28 06:36:24 +0000273 set(PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE})
274 set(ENABLE_SHARED ${LLVM_SHARED_LIBS_ENABLED})
275 set(SHLIBPATH_VAR ${SHLIBPATH_VAR})
276
277 if(LLVM_ENABLE_ASSERTIONS AND NOT MSVC_IDE)
278 set(ENABLE_ASSERTIONS "1")
279 else()
280 set(ENABLE_ASSERTIONS "0")
281 endif()
282
Tim Northover6872de92013-02-14 16:49:32 +0000283 set(HOST_OS ${CMAKE_SYSTEM_NAME})
284 set(HOST_ARCH ${CMAKE_SYSTEM_PROCESSOR})
Chandler Carruthc502ed62012-06-28 06:36:24 +0000285
286 configure_file(${input} ${output} @ONLY)
287endfunction()
Chandler Carruthcf23bd32012-06-30 10:14:14 +0000288
289# A raw function to create a lit target. This is used to implement the testuite
290# management functions.
291function(add_lit_target target comment)
292 parse_arguments(ARG "PARAMS;DEPENDS;ARGS" "" ${ARGN})
293 set(LIT_ARGS "${ARG_ARGS} ${LLVM_LIT_ARGS}")
294 separate_arguments(LIT_ARGS)
295 set(LIT_COMMAND
296 ${PYTHON_EXECUTABLE}
Chandler Carruth20d8c162012-07-02 20:14:54 +0000297 ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py
NAKAMURA Takumi1917aaf2013-01-27 12:20:50 +0000298 --param build_mode=${CMAKE_CFG_INTDIR}
Chandler Carruthcf23bd32012-06-30 10:14:14 +0000299 ${LIT_ARGS}
300 )
301 foreach(param ${ARG_PARAMS})
302 list(APPEND LIT_COMMAND --param ${param})
303 endforeach()
NAKAMURA Takumi3d662d52012-12-24 22:43:59 +0000304 if( ARG_DEPENDS )
305 add_custom_target(${target}
306 COMMAND ${LIT_COMMAND} ${ARG_DEFAULT_ARGS}
307 COMMENT "${comment}"
308 )
309 add_dependencies(${target} ${ARG_DEPENDS})
310 else()
311 add_custom_target(${target}
312 COMMAND cmake -E echo "${target} does nothing, no tools built.")
313 message(STATUS "${target} does nothing.")
314 endif()
Chandler Carruthcf23bd32012-06-30 10:14:14 +0000315endfunction()
316
317# A function to add a set of lit test suites to be driven through 'check-*' targets.
318function(add_lit_testsuite target comment)
319 parse_arguments(ARG "PARAMS;DEPENDS;ARGS" "" ${ARGN})
320
NAKAMURA Takumi64835d22012-10-10 13:32:55 +0000321 # EXCLUDE_FROM_ALL excludes the test ${target} out of check-all.
322 if(NOT EXCLUDE_FROM_ALL)
323 # Register the testsuites, params and depends for the global check rule.
324 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_TESTSUITES ${ARG_DEFAULT_ARGS})
325 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_PARAMS ${ARG_PARAMS})
326 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_DEPENDS ${ARG_DEPENDS})
327 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_EXTRA_ARGS ${ARG_ARGS})
328 endif()
Chandler Carruthcf23bd32012-06-30 10:14:14 +0000329
330 # Produce a specific suffixed check rule.
331 add_lit_target(${target} ${comment}
332 ${ARG_DEFAULT_ARGS}
333 PARAMS ${ARG_PARAMS}
334 DEPENDS ${ARG_DEPENDS}
335 ARGS ${ARG_ARGS}
336 )
337endfunction()