Disable C++ exception handling on MSVC.
[oota-llvm.git] / CMakeLists.txt
1 # See docs/CMake.html for instructions about how to build LLVM with CMake.
2
3 project(LLVM)
4 cmake_minimum_required(VERSION 2.8)
5
6 # Add path for custom modules
7 set(CMAKE_MODULE_PATH
8   ${CMAKE_MODULE_PATH}
9   "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
10   "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules"
11   )
12
13 set(PACKAGE_VERSION "2.9")
14 include(VersionFromVCS)
15 add_version_info_from_vcs(PACKAGE_VERSION)
16
17 set(PACKAGE_NAME llvm)
18 set(PACKAGE_STRING "${PACKAGE_NAME} ${PACKAGE_VERSION}")
19 set(PACKAGE_BUGREPORT "llvmbugs@cs.uiuc.edu")
20
21 if( CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR AND NOT MSVC_IDE )
22   message(FATAL_ERROR "In-source builds are not allowed.
23 CMake would overwrite the makefiles distributed with LLVM.
24 Please create a directory and run cmake from there, passing the path
25 to this source directory as the last argument.
26 This process created the file `CMakeCache.txt' and the directory `CMakeFiles'.
27 Please delete them.")
28 endif()
29
30 # Run-time build mode; It is used for unittests.
31 if(MSVC_IDE)
32   # Expect "$(Configuration)", "$(OutDir)", etc.
33   # It is expanded by msbuild or similar.
34   set(RUNTIME_BUILD_MODE "${CMAKE_CFG_INTDIR}")
35 elseif(NOT CMAKE_BUILD_TYPE STREQUAL "")
36   # Expect "Release" "Debug", etc.
37   # Or unittests could not run.
38   set(RUNTIME_BUILD_MODE ${CMAKE_BUILD_TYPE})
39 else()
40   # It might be "."
41   set(RUNTIME_BUILD_MODE "${CMAKE_CFG_INTDIR}")
42 endif()
43
44 string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE)
45
46 set(LLVM_MAIN_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR})
47 set(LLVM_MAIN_INCLUDE_DIR ${LLVM_MAIN_SRC_DIR}/include)
48 set(LLVM_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
49 set(LLVM_TOOLS_BINARY_DIR ${LLVM_BINARY_DIR}/bin)
50 set(LLVM_EXAMPLES_BINARY_DIR ${LLVM_BINARY_DIR}/examples)
51 set(LLVM_LIBDIR_SUFFIX "" CACHE STRING "Define suffix of library directory name (32/64)" )
52
53 if( NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR )
54   file(GLOB_RECURSE
55     tablegenned_files_on_include_dir
56     "${LLVM_MAIN_SRC_DIR}/include/llvm/*.gen")
57   file(GLOB_RECURSE
58     tablegenned_files_on_lib_dir
59     "${LLVM_MAIN_SRC_DIR}/lib/Target/*.inc")
60   if( tablegenned_files_on_include_dir OR tablegenned_files_on_lib_dir)
61     message(FATAL_ERROR "Apparently there is a previous in-source build,
62 probably as the result of running `configure' and `make' on
63 ${LLVM_MAIN_SRC_DIR}.
64 This may cause problems. The suspicious files are:
65 ${tablegenned_files_on_lib_dir}
66 ${tablegenned_files_on_include_dir}
67 Please clean the source directory.")
68   endif()
69 endif()
70
71 set(LLVM_ALL_TARGETS
72   Alpha
73   ARM
74   Blackfin
75   CBackend
76   CellSPU
77   CppBackend
78   Mips
79   MBlaze
80   MSP430
81   PowerPC
82   PTX
83   Sparc
84   SystemZ
85   X86
86   XCore
87   )
88
89 if( MSVC )
90   set(LLVM_TARGETS_TO_BUILD X86
91     CACHE STRING "Semicolon-separated list of targets to build, or \"all\".")
92 else( MSVC )
93   set(LLVM_TARGETS_TO_BUILD ${LLVM_ALL_TARGETS}
94     CACHE STRING "Semicolon-separated list of targets to build, or \"all\".")
95 endif( MSVC )
96
97 set(CLANG_RESOURCE_DIR "" CACHE STRING
98   "Relative directory from the Clang binary to its resource files.")
99
100 set(C_INCLUDE_DIRS "" CACHE STRING
101   "Colon separated list of directories clang will search for headers.")
102
103 set(LLVM_TARGET_ARCH "host"
104   CACHE STRING "Set target to use for LLVM JIT or use \"host\" for automatic detection.")
105
106 set(LIT_ARGS_DEFAULT "-sv")
107 if (MSVC OR XCODE)
108   set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
109 endif()
110 set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}"
111     CACHE STRING "Default options for lit")
112
113 option(LLVM_ENABLE_THREADS "Use threads if available." ON)
114
115 if( uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE" )
116   option(LLVM_ENABLE_ASSERTIONS "Enable assertions" OFF)
117 else()
118   option(LLVM_ENABLE_ASSERTIONS "Enable assertions" ON)
119 endif()
120
121 if( LLVM_ENABLE_ASSERTIONS )
122   # MSVC doesn't like _DEBUG on release builds. See PR 4379.
123   if( NOT MSVC )
124     add_definitions( -D_DEBUG )
125   endif()
126   # On Release builds cmake automatically defines NDEBUG, so we
127   # explicitly undefine it:
128   if( uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE" )
129     add_definitions( -UNDEBUG )
130   endif()
131 else()
132   if( NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE" )
133     add_definitions( -DNDEBUG )
134   endif()
135 endif()
136
137 if( LLVM_TARGETS_TO_BUILD STREQUAL "all" )
138   set( LLVM_TARGETS_TO_BUILD ${LLVM_ALL_TARGETS} )
139 endif()
140
141 set(LLVM_ENUM_TARGETS "")
142 foreach(c ${LLVM_TARGETS_TO_BUILD})
143   list(FIND LLVM_ALL_TARGETS ${c} idx)
144   if( idx LESS 0 )
145     message(FATAL_ERROR "The target `${c}' does not exist.
146     It should be one of\n${LLVM_ALL_TARGETS}")
147   else()
148     set(LLVM_ENUM_TARGETS "${LLVM_ENUM_TARGETS}LLVM_TARGET(${c})\n")
149   endif()
150 endforeach(c)
151
152 # Produce llvm/Config/Targets.def
153 configure_file(
154   ${LLVM_MAIN_INCLUDE_DIR}/llvm/Config/Targets.def.in
155   ${LLVM_BINARY_DIR}/include/llvm/Config/Targets.def
156   )
157
158 set(llvm_builded_incs_dir ${LLVM_BINARY_DIR}/include/llvm)
159
160 include(AddLLVMDefinitions)
161
162 if(WIN32)
163   if(CYGWIN)
164     set(LLVM_ON_WIN32 0)
165     set(LLVM_ON_UNIX 1)
166   else(CYGWIN)
167     set(LLVM_ON_WIN32 1)
168     set(LLVM_ON_UNIX 0)
169   endif(CYGWIN)
170   set(LTDL_SHLIB_EXT ".dll")
171   set(EXEEXT ".exe")
172   # Maximum path length is 160 for non-unicode paths
173   set(MAXPATHLEN 160)
174 else(WIN32)
175   if(UNIX)
176     set(LLVM_ON_WIN32 0)
177     set(LLVM_ON_UNIX 1)
178     if(APPLE)
179       set(LTDL_SHLIB_EXT ".dylib")
180     else(APPLE)
181       set(LTDL_SHLIB_EXT ".so")
182     endif(APPLE)
183     set(EXEEXT "")
184     # FIXME: Maximum path length is currently set to 'safe' fixed value
185     set(MAXPATHLEN 2024)
186   else(UNIX)
187     MESSAGE(SEND_ERROR "Unable to determine platform")
188   endif(UNIX)
189 endif(WIN32)
190
191 include(config-ix)
192
193 option(LLVM_ENABLE_PIC "Build Position-Independent Code" ON)
194
195 set(ENABLE_PIC 0)
196 if( LLVM_ENABLE_PIC )
197  if( XCODE )
198    # Xcode has -mdynamic-no-pic on by default, which overrides -fPIC. I don't
199    # know how to disable this, so just force ENABLE_PIC off for now.
200    message(STATUS "Warning: -fPIC not supported with Xcode.")
201  else( XCODE )
202    if( SUPPORTS_FPIC_FLAG )
203       message(STATUS "Building with -fPIC")
204       add_llvm_definitions(-fPIC)
205       set(ENABLE_PIC 1)
206    else( SUPPORTS_FPIC_FLAG )
207       message(STATUS "Warning: -fPIC not supported.")
208    endif()
209  endif()
210 endif()
211
212 set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LLVM_TOOLS_BINARY_DIR} )
213 set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${LLVM_BINARY_DIR}/lib )
214 set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${LLVM_BINARY_DIR}/lib )
215
216 # set(CMAKE_VERBOSE_MAKEFILE true)
217
218 add_llvm_definitions( -D__STDC_LIMIT_MACROS )
219 add_llvm_definitions( -D__STDC_CONSTANT_MACROS )
220
221 # MSVC has a gazillion warnings with this.
222 if( MSVC )
223   option(LLVM_ENABLE_WARNINGS "Enable compiler warnings." OFF)
224 else( MSVC )
225   option(LLVM_ENABLE_WARNINGS "Enable compiler warnings." ON)
226 endif()
227
228 option(LLVM_ENABLE_PEDANTIC "Compile with pedantic enabled." ON)
229 option(LLVM_ENABLE_WERROR "Fail and stop if a warning is triggered." OFF)
230
231 if( CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT WIN32 )
232   # TODO: support other platforms and toolchains.
233   option(LLVM_BUILD_32_BITS "Build 32 bits executables and libraries." OFF)
234   if( LLVM_BUILD_32_BITS )
235     message(STATUS "Building 32 bits executables and libraries.")
236     add_llvm_definitions( -m32 )
237     list(APPEND CMAKE_EXE_LINKER_FLAGS -m32)
238     list(APPEND CMAKE_SHARED_LINKER_FLAGS -m32)
239   endif( LLVM_BUILD_32_BITS )
240 endif( CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT WIN32 )
241
242 if( MSVC )
243   include(ChooseMSVCCRT)
244
245   add_llvm_definitions( -D_CRT_SECURE_NO_DEPRECATE -D_CRT_SECURE_NO_WARNINGS )
246   add_llvm_definitions( -D_SCL_SECURE_NO_WARNINGS -DCRT_NONSTDC_NO_WARNINGS )
247   add_llvm_definitions( -D_SCL_SECURE_NO_DEPRECATE )
248   add_llvm_definitions( -wd4146 -wd4503 -wd4996 -wd4800 -wd4244 -wd4624 )
249   add_llvm_definitions( -wd4355 -wd4715 -wd4180 -wd4345 -wd4224 -wd4267 -wd4291 )
250   add_llvm_definitions( -wd4275)
251
252   # Suppress 'new behavior: elements of array 'array' will be default initialized'
253   add_llvm_definitions( -wd4351 )
254
255   # Enable warnings
256   if (LLVM_ENABLE_WARNINGS)
257     add_llvm_definitions( /W4 /Wall )
258     if (LLVM_ENABLE_PEDANTIC)
259       # No MSVC equivalent available
260     endif (LLVM_ENABLE_PEDANTIC)
261   endif (LLVM_ENABLE_WARNINGS)
262   if (LLVM_ENABLE_WERROR)
263     add_llvm_definitions( /WX )
264   endif (LLVM_ENABLE_WERROR)
265 elseif( CMAKE_COMPILER_IS_GNUCXX )
266   if (LLVM_ENABLE_WARNINGS)
267     add_llvm_definitions( -Wall -W -Wno-unused-parameter -Wwrite-strings )
268     if (LLVM_ENABLE_PEDANTIC)
269       add_llvm_definitions( -pedantic -Wno-long-long )
270     endif (LLVM_ENABLE_PEDANTIC)
271   endif (LLVM_ENABLE_WARNINGS)
272   if (LLVM_ENABLE_WERROR)
273     add_llvm_definitions( -Werror )
274   endif (LLVM_ENABLE_WERROR)
275 endif( MSVC )
276
277 include_directories( ${LLVM_BINARY_DIR}/include ${LLVM_MAIN_INCLUDE_DIR})
278
279 if( ${CMAKE_SYSTEM_NAME} MATCHES SunOS )
280    SET(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "-include llvm/Support/Solaris.h")
281 endif( ${CMAKE_SYSTEM_NAME} MATCHES SunOS )
282
283 include(AddLLVM)
284 include(TableGen)
285
286 if( MINGW )
287   get_system_libs(LLVM_SYSTEM_LIBS_LIST)
288   foreach(l ${LLVM_SYSTEM_LIBS_LIST})
289     set(LLVM_SYSTEM_LIBS "${LLVM_SYSTEM_LIBS} -l${l}")
290   endforeach()
291   set(CMAKE_CXX_STANDARD_LIBRARIES "${CMAKE_CXX_STANDARD_LIBRARIES}${LLVM_SYSTEM_LIBS}")
292   set(CMAKE_C_STANDARD_LIBRARIES "${CMAKE_C_STANDARD_LIBRARIES}${LLVM_SYSTEM_LIBS}")
293 endif()
294
295 add_subdirectory(lib/Support)
296
297 # Everything else depends on Support:
298 set(LLVM_COMMON_DEPENDS ${LLVM_COMMON_DEPENDS} ${LLVM_LIBS} )
299
300 set(LLVM_TABLEGEN "tblgen" CACHE
301   STRING "Native TableGen executable. Saves building one when cross-compiling.")
302 # Effective tblgen executable to be used:
303 set(LLVM_TABLEGEN_EXE ${LLVM_TABLEGEN})
304
305 add_subdirectory(utils/TableGen)
306
307 if( CMAKE_CROSSCOMPILING )
308   # This adds a dependency on target `tblgen', so must go after utils/TableGen
309   include( CrossCompileLLVM )
310 endif( CMAKE_CROSSCOMPILING )
311
312 add_subdirectory(include/llvm)
313
314 add_subdirectory(lib/VMCore)
315 add_subdirectory(lib/CodeGen)
316 add_subdirectory(lib/CodeGen/SelectionDAG)
317 add_subdirectory(lib/CodeGen/AsmPrinter)
318 add_subdirectory(lib/Bitcode/Reader)
319 add_subdirectory(lib/Bitcode/Writer)
320 add_subdirectory(lib/Transforms/Utils)
321 add_subdirectory(lib/Transforms/Instrumentation)
322 add_subdirectory(lib/Transforms/InstCombine)
323 add_subdirectory(lib/Transforms/Scalar)
324 add_subdirectory(lib/Transforms/IPO)
325 add_subdirectory(lib/Transforms/Hello)
326 add_subdirectory(lib/Linker)
327 add_subdirectory(lib/Analysis)
328 add_subdirectory(lib/Analysis/IPA)
329 add_subdirectory(lib/MC)
330 add_subdirectory(lib/MC/MCParser)
331 add_subdirectory(lib/MC/MCDisassembler)
332 add_subdirectory(lib/Object)
333
334 add_subdirectory(utils/FileCheck)
335 add_subdirectory(utils/count)
336 add_subdirectory(utils/not)
337 add_subdirectory(utils/llvm-lit)
338
339 set(LLVM_ENUM_ASM_PRINTERS "")
340 set(LLVM_ENUM_ASM_PARSERS "")
341 set(LLVM_ENUM_DISASSEMBLERS "")
342 foreach(t ${LLVM_TARGETS_TO_BUILD})
343   message(STATUS "Targeting ${t}")
344   add_subdirectory(lib/Target/${t})
345   add_subdirectory(lib/Target/${t}/TargetInfo)
346   set( td ${LLVM_MAIN_SRC_DIR}/lib/Target/${t} )
347   file(GLOB asmp_file "${td}/*AsmPrinter.cpp")
348   if( asmp_file )
349     set(LLVM_ENUM_ASM_PRINTERS
350       "${LLVM_ENUM_ASM_PRINTERS}LLVM_ASM_PRINTER(${t})\n")
351   endif()
352   if( EXISTS ${td}/InstPrinter/CMakeLists.txt )
353     add_subdirectory(lib/Target/${t}/InstPrinter)
354   endif()
355   if( EXISTS ${td}/AsmParser/CMakeLists.txt )
356     add_subdirectory(lib/Target/${t}/AsmParser)
357     set(LLVM_ENUM_ASM_PARSERS
358       "${LLVM_ENUM_ASM_PARSERS}LLVM_ASM_PARSER(${t})\n")
359   endif()
360   if( EXISTS ${td}/Disassembler/CMakeLists.txt )
361     add_subdirectory(lib/Target/${t}/Disassembler)
362     set(LLVM_ENUM_DISASSEMBLERS
363       "${LLVM_ENUM_DISASSEMBLERS}LLVM_DISASSEMBLER(${t})\n")
364   endif()
365   set(CURRENT_LLVM_TARGET)
366 endforeach(t)
367
368 # Produce llvm/Config/AsmPrinters.def
369 configure_file(
370   ${LLVM_MAIN_INCLUDE_DIR}/llvm/Config/AsmPrinters.def.in
371   ${LLVM_BINARY_DIR}/include/llvm/Config/AsmPrinters.def
372   )
373
374 # Produce llvm/Config/AsmParsers.def
375 configure_file(
376   ${LLVM_MAIN_INCLUDE_DIR}/llvm/Config/AsmParsers.def.in
377   ${LLVM_BINARY_DIR}/include/llvm/Config/AsmParsers.def
378   )
379
380 # Produce llvm/Config/Disassemblers.def
381 configure_file(
382   ${LLVM_MAIN_INCLUDE_DIR}/llvm/Config/Disassemblers.def.in
383   ${LLVM_BINARY_DIR}/include/llvm/Config/Disassemblers.def
384   )
385
386 add_subdirectory(lib/ExecutionEngine)
387 add_subdirectory(lib/ExecutionEngine/Interpreter)
388 add_subdirectory(lib/ExecutionEngine/JIT)
389 add_subdirectory(lib/ExecutionEngine/MCJIT)
390 add_subdirectory(lib/Target)
391 add_subdirectory(lib/AsmParser)
392 add_subdirectory(lib/Archive)
393
394 add_subdirectory(projects)
395
396 option(LLVM_BUILD_TOOLS
397   "Build the LLVM tools. If OFF, just generate build targets." ON)
398 option(LLVM_INCLUDE_TOOLS "Generate build targets for the LLVM tools." ON)
399 if( LLVM_INCLUDE_TOOLS )
400   add_subdirectory(tools)
401 endif()
402
403 option(LLVM_BUILD_EXAMPLES
404   "Build the LLVM example programs. If OFF, just generate build targets." OFF)
405 option(LLVM_INCLUDE_EXAMPLES "Generate build targets for the LLVM examples" ON)
406 if( LLVM_INCLUDE_EXAMPLES )
407   add_subdirectory(examples)
408 endif()
409
410 option(LLVM_BUILD_TESTS
411   "Build LLVM unit tests. If OFF, just generate build targes." OFF)
412 option(LLVM_INCLUDE_TESTS "Generate build targets for the LLVM unit tests." ON)
413 if( LLVM_INCLUDE_TESTS )
414   add_subdirectory(test)
415   add_subdirectory(utils/unittest)
416   add_subdirectory(unittests)
417   if (MSVC)
418     # This utility is used to prevent chrashing tests from calling Dr. Watson on
419     # Windows.
420     add_subdirectory(utils/KillTheDoctor)
421   endif()
422 endif()
423
424 add_subdirectory(cmake/modules)
425
426 install(DIRECTORY include/
427   DESTINATION include
428   FILES_MATCHING
429   PATTERN "*.def"
430   PATTERN "*.h"
431   PATTERN "*.td"
432   PATTERN "*.inc"
433   PATTERN ".svn" EXCLUDE
434   )
435
436 install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/
437   DESTINATION include
438   FILES_MATCHING
439   PATTERN "*.def"
440   PATTERN "*.h"
441   PATTERN "*.gen"
442   PATTERN "*.inc"
443   # Exclude include/llvm/CMakeFiles/intrinsics_gen.dir, matched by "*.def"
444   PATTERN "CMakeFiles" EXCLUDE
445   PATTERN ".svn" EXCLUDE
446   )
447
448 # TODO: make and install documentation.
449
450 set(CPACK_PACKAGE_VENDOR "LLVM")
451 set(CPACK_PACKAGE_VERSION_MAJOR 2)
452 set(CPACK_PACKAGE_VERSION_MINOR 9)
453 add_version_info_from_vcs(CPACK_PACKAGE_VERSION_PATCH)
454 include(CPack)
455
456 # Workaround for MSVS10 to avoid the Dialog Hell
457 # FIXME: This could be removed with future version of CMake.
458 if(MSVC_VERSION EQUAL 1600)
459   set(LLVM_SLN_FILENAME "${CMAKE_CURRENT_BINARY_DIR}/LLVM.sln")
460   if( EXISTS "${LLVM_SLN_FILENAME}" )
461     file(APPEND "${LLVM_SLN_FILENAME}" "\n# This should be regenerated!\n")
462   endif()
463 endif()