blob: e41b3e7f51d55a9fc2d13f9b11f1219cbaecbd6e [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} )
Oscar Fuentes00a96182010-10-14 15:54:41 +000015 endif()
16
Oscar Fuentes23f0bfe2011-03-29 20:51:08 +000017 # 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
Nick Lewyckybcffb1f2011-04-29 02:12:06 +000021 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()
Oscar Fuentes0b85d072011-02-20 22:06:10 +000028 set_target_properties(${name} PROPERTIES FOLDER "Libraries")
Daniel Dunbar288bc5c2011-11-29 01:31:52 +000029
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})
Oscar Fuentes3d01fc72008-09-22 01:08:49 +000037endmacro(add_llvm_library name)
38
Oscar Fuentesc5e1ae12009-11-10 02:45:37 +000039macro(add_llvm_loadable_module name)
Oscar Fuentes01a575e2010-10-22 19:03:24 +000040 if( NOT LLVM_ON_UNIX OR CYGWIN )
Oscar Fuentesc5e1ae12009-11-10 02:45:37 +000041 message(STATUS "Loadable modules not supported on this platform.
42${name} ignored.")
NAKAMURA Takumi51c06bf2010-12-10 02:15:36 +000043 # Add empty "phony" target
44 add_custom_target(${name})
Oscar Fuentesc5e1ae12009-11-10 02:45:37 +000045 else()
Oscar Fuentesc5e1ae12009-11-10 02:45:37 +000046 llvm_process_sources( ALL_FILES ${ARGN} )
Oscar Fuentes71104282010-12-22 08:30:17 +000047 if (MODULE)
48 set(libkind MODULE)
49 else()
50 set(libkind SHARED)
51 endif()
52
53 add_library( ${name} ${libkind} ${ALL_FILES} )
Oscar Fuentesc5e1ae12009-11-10 02:45:37 +000054 set_target_properties( ${name} PROPERTIES PREFIX "" )
Douglas Gregor87814162009-11-10 15:30:33 +000055
Oscar Fuentes879d3a92011-03-12 16:48:54 +000056 llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
Oscar Fuentes23f0bfe2011-03-29 20:51:08 +000057 link_system_libs( ${name} )
Oscar Fuentes879d3a92011-03-12 16:48:54 +000058
Douglas Gregor87814162009-11-10 15:30:33 +000059 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
Oscar Fuentesab92d1e2011-04-26 14:55:27 +000065 if( EXCLUDE_FROM_ALL )
Nick Lewyckybcffb1f2011-04-29 02:12:06 +000066 set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
Oscar Fuentesab92d1e2011-04-26 14:55:27 +000067 else()
68 install(TARGETS ${name}
Gabor Greif9ae2dfd2012-07-12 13:18:13 +000069 LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
70 ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
Oscar Fuentesab92d1e2011-04-26 14:55:27 +000071 endif()
Oscar Fuentesc5e1ae12009-11-10 02:45:37 +000072 endif()
Oscar Fuentes0b85d072011-02-20 22:06:10 +000073
74 set_target_properties(${name} PROPERTIES FOLDER "Loadable modules")
Oscar Fuentesc5e1ae12009-11-10 02:45:37 +000075endmacro(add_llvm_loadable_module name)
76
77
Oscar Fuentes3d01fc72008-09-22 01:08:49 +000078macro(add_llvm_executable name)
Oscar Fuentes50925fb2008-11-15 02:08:08 +000079 llvm_process_sources( ALL_FILES ${ARGN} )
Oscar Fuentesb8352de2009-11-23 00:21:43 +000080 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)
Oscar Fuentes23f0bfe2011-03-29 20:51:08 +000086 llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
Oscar Fuentesd8b1b9a2009-08-14 04:38:57 +000087 if( LLVM_COMMON_DEPENDS )
88 add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
89 endif( LLVM_COMMON_DEPENDS )
Oscar Fuentes23f0bfe2011-03-29 20:51:08 +000090 link_system_libs( ${name} )
Oscar Fuentes3d01fc72008-09-22 01:08:49 +000091endmacro(add_llvm_executable name)
92
93
94macro(add_llvm_tool name)
95 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LLVM_TOOLS_BINARY_DIR})
Oscar Fuentes7be498e2009-11-23 00:32:42 +000096 if( NOT LLVM_BUILD_TOOLS )
Oscar Fuentesb8352de2009-11-23 00:21:43 +000097 set(EXCLUDE_FROM_ALL ON)
98 endif()
Oscar Fuentes066de852010-09-25 20:25:25 +000099 add_llvm_executable(${name} ${ARGN})
Oscar Fuentes7be498e2009-11-23 00:32:42 +0000100 if( LLVM_BUILD_TOOLS )
101 install(TARGETS ${name} RUNTIME DESTINATION bin)
102 endif()
Oscar Fuentes0b85d072011-02-20 22:06:10 +0000103 set_target_properties(${name} PROPERTIES FOLDER "Tools")
Oscar Fuentes3d01fc72008-09-22 01:08:49 +0000104endmacro(add_llvm_tool name)
105
106
107macro(add_llvm_example name)
108# set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LLVM_EXAMPLES_BINARY_DIR})
Oscar Fuentes7be498e2009-11-23 00:32:42 +0000109 if( NOT LLVM_BUILD_EXAMPLES )
Oscar Fuentesb8352de2009-11-23 00:21:43 +0000110 set(EXCLUDE_FROM_ALL ON)
111 endif()
Oscar Fuentes066de852010-09-25 20:25:25 +0000112 add_llvm_executable(${name} ${ARGN})
Oscar Fuentes7be498e2009-11-23 00:32:42 +0000113 if( LLVM_BUILD_EXAMPLES )
114 install(TARGETS ${name} RUNTIME DESTINATION examples)
115 endif()
Oscar Fuentes0b85d072011-02-20 22:06:10 +0000116 set_target_properties(${name} PROPERTIES FOLDER "Examples")
Oscar Fuentes3d01fc72008-09-22 01:08:49 +0000117endmacro(add_llvm_example name)
Oscar Fuentese1ad0872008-09-26 04:40:32 +0000118
119
Oscar Fuentes0b85d072011-02-20 22:06:10 +0000120macro(add_llvm_utility name)
121 add_llvm_executable(${name} ${ARGN})
122 set_target_properties(${name} PROPERTIES FOLDER "Utils")
123endmacro(add_llvm_utility name)
124
125
Oscar Fuentese1ad0872008-09-26 04:40:32 +0000126macro(add_llvm_target target_name)
Oscar Fuentesd7808d22011-07-25 20:17:01 +0000127 include_directories(BEFORE
128 ${CMAKE_CURRENT_BINARY_DIR}
129 ${CMAKE_CURRENT_SOURCE_DIR})
Douglas Gregor7e9e36a2009-06-23 17:57:35 +0000130 add_llvm_library(LLVM${target_name} ${ARGN} ${TABLEGEN_OUTPUT})
Oscar Fuentes00d78f12011-02-20 02:55:27 +0000131 set( CURRENT_LLVM_TARGET LLVM${target_name} )
Oscar Fuentese1ad0872008-09-26 04:40:32 +0000132endmacro(add_llvm_target)
Michael J. Spencer3c00db72012-04-26 19:43:35 +0000133
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.
NAKAMURA Takumi1e10bd62012-10-05 14:10:17 +0000138# Additional parameter can be specified as the name of directory.
Michael J. Spencer3c00db72012-04-26 19:43:35 +0000139macro(add_llvm_external_project name)
NAKAMURA Takumi1e10bd62012-10-05 14:10:17 +0000140 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}"
Michael J. Spencer3c00db72012-04-26 19:43:35 +0000147 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)
NAKAMURA Takumi1e10bd62012-10-05 14:10:17 +0000153 add_subdirectory(${LLVM_EXTERNAL_${nameUPPER}_SOURCE_DIR} ${add_llvm_external_dir})
Michael J. Spencer3c00db72012-04-26 19:43:35 +0000154 endif()
155 endif()
156endmacro(add_llvm_external_project)
Chandler Carruthb60182e2012-06-21 05:16:58 +0000157
Alexey Samsonov828df0f2012-12-19 12:30:33 +0000158# Returns directory where unittest should reside.
159function(get_unittest_directory dir)
NAKAMURA Takumi1917aaf2013-01-27 12:20:50 +0000160 set(result ${CMAKE_CURRENT_BINARY_DIR})
Alexey Samsonov828df0f2012-12-19 12:30:33 +0000161 set(${dir} ${result} PARENT_SCOPE)
162endfunction()
163
Chandler Carruthb60182e2012-06-21 05:16:58 +0000164# Generic support for adding a unittest.
Chandler Carruth7c888ee2012-06-21 09:51:26 +0000165function(add_unittest test_suite test_name)
Alexey Samsonov828df0f2012-12-19 12:30:33 +0000166 get_unittest_directory(OUTPUT_DIR)
167 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${OUTPUT_DIR})
Chandler Carruthb60182e2012-06-21 05:16:58 +0000168 if( NOT LLVM_BUILD_TESTS )
169 set(EXCLUDE_FROM_ALL ON)
170 endif()
171
172 add_llvm_executable(${test_name} ${ARGN})
173 target_link_libraries(${test_name}
174 gtest
175 gtest_main
176 LLVMSupport # gtest needs it for raw_ostream.
177 )
178
179 add_dependencies(${test_suite} ${test_name})
180 get_target_property(test_suite_folder ${test_suite} FOLDER)
181 if (NOT ${test_suite_folder} STREQUAL "NOTFOUND")
182 set_property(TARGET ${test_name} PROPERTY FOLDER "${test_suite_folder}")
183 endif ()
184
185 # Visual Studio 2012 only supports up to 8 template parameters in
186 # std::tr1::tuple by default, but gtest requires 10
187 if (MSVC AND MSVC_VERSION EQUAL 1700)
188 set_property(TARGET ${test_name} APPEND PROPERTY COMPILE_DEFINITIONS _VARIADIC_MAX=10)
189 endif ()
190
191 include_directories(${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include)
192 set_property(TARGET ${test_name} APPEND PROPERTY COMPILE_DEFINITIONS GTEST_HAS_RTTI=0)
Chandler Carruthb60182e2012-06-21 05:16:58 +0000193 if (NOT LLVM_ENABLE_THREADS)
194 set_property(TARGET ${test_name} APPEND PROPERTY COMPILE_DEFINITIONS GTEST_HAS_PTHREAD=0)
195 endif ()
196
Chandler Carruth343c32a2012-06-21 18:44:24 +0000197 get_property(target_compile_flags TARGET ${test_name} PROPERTY COMPILE_FLAGS)
198 if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
199 set(target_compile_flags "${target_compile_flags} -fno-rtti")
200 elseif (MSVC)
201 set(target_compile_flags "${target_compile_flags} /GR-")
Chandler Carruthb60182e2012-06-21 05:16:58 +0000202 endif ()
Chandler Carruth343c32a2012-06-21 18:44:24 +0000203
204 if (SUPPORTS_NO_VARIADIC_MACROS_FLAG)
205 set(target_compile_flags "${target_compile_flags} -Wno-variadic-macros")
206 endif ()
207 set_property(TARGET ${test_name} PROPERTY COMPILE_FLAGS "${target_compile_flags}")
Chandler Carruthb60182e2012-06-21 05:16:58 +0000208endfunction()
Chandler Carruthc502ed62012-06-28 06:36:24 +0000209
210# This function provides an automatic way to 'configure'-like generate a file
211# based on a set of common and custom variables, specifically targetting the
212# variables needed for the 'lit.site.cfg' files. This function bundles the
213# common variables that any Lit instance is likely to need, and custom
214# variables can be passed in.
215function(configure_lit_site_cfg input output)
216 foreach(c ${LLVM_TARGETS_TO_BUILD})
217 set(TARGETS_BUILT "${TARGETS_BUILT} ${c}")
218 endforeach(c)
219 set(TARGETS_TO_BUILD ${TARGETS_BUILT})
220
221 set(SHLIBEXT "${LTDL_SHLIB_EXT}")
222 set(SHLIBDIR "${LLVM_BINARY_DIR}/lib/${CMAKE_CFG_INTDIR}")
223
224 if(BUILD_SHARED_LIBS)
225 set(LLVM_SHARED_LIBS_ENABLED "1")
226 else()
227 set(LLVM_SHARED_LIBS_ENABLED "0")
228 endif(BUILD_SHARED_LIBS)
229
230 if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
231 set(SHLIBPATH_VAR "DYLD_LIBRARY_PATH")
232 else() # Default for all other unix like systems.
233 # CMake hardcodes the library locaction using rpath.
234 # Therefore LD_LIBRARY_PATH is not required to run binaries in the
235 # build dir. We pass it anyways.
236 set(SHLIBPATH_VAR "LD_LIBRARY_PATH")
237 endif()
238
239 # Configuration-time: See Unit/lit.site.cfg.in
240 set(LLVM_BUILD_MODE "%(build_mode)s")
241
242 set(LLVM_SOURCE_DIR ${LLVM_MAIN_SRC_DIR})
243 set(LLVM_BINARY_DIR ${LLVM_BINARY_DIR})
NAKAMURA Takumi1917aaf2013-01-27 12:20:50 +0000244 set(LLVM_TOOLS_DIR "${LLVM_TOOLS_BINARY_DIR}/%(build_mode)s")
245 set(LLVM_LIBS_DIR "${LLVM_BINARY_DIR}/lib/%(build_mode)s")
Chandler Carruthc502ed62012-06-28 06:36:24 +0000246 set(PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE})
247 set(ENABLE_SHARED ${LLVM_SHARED_LIBS_ENABLED})
248 set(SHLIBPATH_VAR ${SHLIBPATH_VAR})
249
250 if(LLVM_ENABLE_ASSERTIONS AND NOT MSVC_IDE)
251 set(ENABLE_ASSERTIONS "1")
252 else()
253 set(ENABLE_ASSERTIONS "0")
254 endif()
255
256 set(HOST_OS ${CMAKE_HOST_SYSTEM_NAME})
257 set(HOST_ARCH ${CMAKE_HOST_SYSTEM_PROCESSOR})
258
259 configure_file(${input} ${output} @ONLY)
260endfunction()
Chandler Carruthcf23bd32012-06-30 10:14:14 +0000261
262# A raw function to create a lit target. This is used to implement the testuite
263# management functions.
264function(add_lit_target target comment)
265 parse_arguments(ARG "PARAMS;DEPENDS;ARGS" "" ${ARGN})
266 set(LIT_ARGS "${ARG_ARGS} ${LLVM_LIT_ARGS}")
267 separate_arguments(LIT_ARGS)
268 set(LIT_COMMAND
269 ${PYTHON_EXECUTABLE}
Chandler Carruth20d8c162012-07-02 20:14:54 +0000270 ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py
NAKAMURA Takumi1917aaf2013-01-27 12:20:50 +0000271 --param build_mode=${CMAKE_CFG_INTDIR}
Chandler Carruthcf23bd32012-06-30 10:14:14 +0000272 ${LIT_ARGS}
273 )
274 foreach(param ${ARG_PARAMS})
275 list(APPEND LIT_COMMAND --param ${param})
276 endforeach()
NAKAMURA Takumi3d662d52012-12-24 22:43:59 +0000277 if( ARG_DEPENDS )
278 add_custom_target(${target}
279 COMMAND ${LIT_COMMAND} ${ARG_DEFAULT_ARGS}
280 COMMENT "${comment}"
281 )
282 add_dependencies(${target} ${ARG_DEPENDS})
283 else()
284 add_custom_target(${target}
285 COMMAND cmake -E echo "${target} does nothing, no tools built.")
286 message(STATUS "${target} does nothing.")
287 endif()
Chandler Carruthcf23bd32012-06-30 10:14:14 +0000288endfunction()
289
290# A function to add a set of lit test suites to be driven through 'check-*' targets.
291function(add_lit_testsuite target comment)
292 parse_arguments(ARG "PARAMS;DEPENDS;ARGS" "" ${ARGN})
293
NAKAMURA Takumi64835d22012-10-10 13:32:55 +0000294 # EXCLUDE_FROM_ALL excludes the test ${target} out of check-all.
295 if(NOT EXCLUDE_FROM_ALL)
296 # Register the testsuites, params and depends for the global check rule.
297 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_TESTSUITES ${ARG_DEFAULT_ARGS})
298 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_PARAMS ${ARG_PARAMS})
299 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_DEPENDS ${ARG_DEPENDS})
300 set_property(GLOBAL APPEND PROPERTY LLVM_LIT_EXTRA_ARGS ${ARG_ARGS})
301 endif()
Chandler Carruthcf23bd32012-06-30 10:14:14 +0000302
303 # Produce a specific suffixed check rule.
304 add_lit_target(${target} ${comment}
305 ${ARG_DEFAULT_ARGS}
306 PARAMS ${ARG_PARAMS}
307 DEPENDS ${ARG_DEPENDS}
308 ARGS ${ARG_ARGS}
309 )
310endfunction()