Rewrite the CMake build to use explicit dependencies between libraries,
[oota-llvm.git] / cmake / modules / AddLLVM.cmake
1 include(LLVMProcessSources)
2 include(LLVM-Config)
3
4 macro(add_llvm_library name)
5   llvm_process_sources( ALL_FILES ${ARGN} )
6   add_library( ${name} ${ALL_FILES} )
7   set_property( GLOBAL APPEND PROPERTY LLVM_LIBS ${name} )
8   if( LLVM_COMMON_DEPENDS )
9     add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
10   endif( LLVM_COMMON_DEPENDS )
11
12   if( BUILD_SHARED_LIBS )
13     llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
14   endif()
15
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
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()
27   # The LLVM Target library shall be built before its sublibraries
28   # (asmprinter, etc) because those may use tablegenned files which
29   # generation is triggered by the main LLVM target library. Necessary
30   # for parallel builds:
31   if( CURRENT_LLVM_TARGET )
32     add_dependencies(${name} ${CURRENT_LLVM_TARGET})
33   endif()
34   set_target_properties(${name} PROPERTIES FOLDER "Libraries")
35 endmacro(add_llvm_library name)
36
37 macro(add_llvm_library_dependencies name)
38   # Save the dependencies of the LLVM library in a variable so that we can
39   # query it when resolve llvm-config-style component -> library mappings.
40   set(LLVM_LIB_DEPS_${name} ${ARGN})
41
42   # Then add the actual dependencies to the library target.
43   target_link_libraries(${name} ${ARGN})
44 endmacro(add_llvm_library_dependencies name)
45
46 macro(add_llvm_loadable_module name)
47   if( NOT LLVM_ON_UNIX OR CYGWIN )
48     message(STATUS "Loadable modules not supported on this platform.
49 ${name} ignored.")
50     # Add empty "phony" target
51     add_custom_target(${name})
52   else()
53     llvm_process_sources( ALL_FILES ${ARGN} )
54     if (MODULE)
55       set(libkind MODULE)
56     else()
57       set(libkind SHARED)
58     endif()
59
60     add_library( ${name} ${libkind} ${ALL_FILES} )
61     set_target_properties( ${name} PROPERTIES PREFIX "" )
62
63     llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
64     link_system_libs( ${name} )
65
66     if (APPLE)
67       # Darwin-specific linker flags for loadable modules.
68       set_target_properties(${name} PROPERTIES
69         LINK_FLAGS "-Wl,-flat_namespace -Wl,-undefined -Wl,suppress")
70     endif()
71
72     if( EXCLUDE_FROM_ALL )
73       set_target_properties( ${name} PROPERTIES EXCLUDE_FROM_ALL ON)
74     else()
75       install(TARGETS ${name}
76         LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
77         ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX})
78     endif()
79   endif()
80
81   set_target_properties(${name} PROPERTIES FOLDER "Loadable modules")
82 endmacro(add_llvm_loadable_module name)
83
84
85 macro(add_llvm_executable name)
86   llvm_process_sources( ALL_FILES ${ARGN} )
87   if( EXCLUDE_FROM_ALL )
88     add_executable(${name} EXCLUDE_FROM_ALL ${ALL_FILES})
89   else()
90     add_executable(${name} ${ALL_FILES})
91   endif()
92   set(EXCLUDE_FROM_ALL OFF)
93   target_link_libraries( ${name} ${LLVM_USED_LIBS} )
94   llvm_config( ${name} ${LLVM_LINK_COMPONENTS} )
95   if( LLVM_COMMON_DEPENDS )
96     add_dependencies( ${name} ${LLVM_COMMON_DEPENDS} )
97   endif( LLVM_COMMON_DEPENDS )
98   link_system_libs( ${name} )
99 endmacro(add_llvm_executable name)
100
101
102 macro(add_llvm_tool name)
103   set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LLVM_TOOLS_BINARY_DIR})
104   if( NOT LLVM_BUILD_TOOLS )
105     set(EXCLUDE_FROM_ALL ON)
106   endif()
107   add_llvm_executable(${name} ${ARGN})
108   if( LLVM_BUILD_TOOLS )
109     install(TARGETS ${name} RUNTIME DESTINATION bin)
110   endif()
111   set_target_properties(${name} PROPERTIES FOLDER "Tools")
112 endmacro(add_llvm_tool name)
113
114
115 macro(add_llvm_example name)
116 #  set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LLVM_EXAMPLES_BINARY_DIR})
117   if( NOT LLVM_BUILD_EXAMPLES )
118     set(EXCLUDE_FROM_ALL ON)
119   endif()
120   add_llvm_executable(${name} ${ARGN})
121   if( LLVM_BUILD_EXAMPLES )
122     install(TARGETS ${name} RUNTIME DESTINATION examples)
123   endif()
124   set_target_properties(${name} PROPERTIES FOLDER "Examples")
125 endmacro(add_llvm_example name)
126
127
128 macro(add_llvm_utility name)
129   add_llvm_executable(${name} ${ARGN})
130   set_target_properties(${name} PROPERTIES FOLDER "Utils")
131 endmacro(add_llvm_utility name)
132
133
134 macro(add_llvm_target target_name)
135   include_directories(BEFORE
136     ${CMAKE_CURRENT_BINARY_DIR}
137     ${CMAKE_CURRENT_SOURCE_DIR})
138   add_llvm_library(LLVM${target_name} ${ARGN} ${TABLEGEN_OUTPUT})
139   set( CURRENT_LLVM_TARGET LLVM${target_name} )
140 endmacro(add_llvm_target)