xref: /netbsd-src/external/apache2/llvm/dist/clang/cmake/modules/AddClang.cmake (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1function(clang_tablegen)
2  # Syntax:
3  # clang_tablegen output-file [tablegen-arg ...] SOURCE source-file
4  # [[TARGET cmake-target-name] [DEPENDS extra-dependency ...]]
5  #
6  # Generates a custom command for invoking tblgen as
7  #
8  # tblgen source-file -o=output-file tablegen-arg ...
9  #
10  # and, if cmake-target-name is provided, creates a custom target for
11  # executing the custom command depending on output-file. It is
12  # possible to list more files to depend after DEPENDS.
13
14  cmake_parse_arguments(CTG "" "SOURCE;TARGET" "" ${ARGN})
15
16  if( NOT CTG_SOURCE )
17    message(FATAL_ERROR "SOURCE source-file required by clang_tablegen")
18  endif()
19
20  set( LLVM_TARGET_DEFINITIONS ${CTG_SOURCE} )
21  tablegen(CLANG ${CTG_UNPARSED_ARGUMENTS})
22
23  if(CTG_TARGET)
24    add_public_tablegen_target(${CTG_TARGET})
25    set_target_properties( ${CTG_TARGET} PROPERTIES FOLDER "Clang tablegenning")
26    set_property(GLOBAL APPEND PROPERTY CLANG_TABLEGEN_TARGETS ${CTG_TARGET})
27  endif()
28endfunction(clang_tablegen)
29
30macro(set_clang_windows_version_resource_properties name)
31  if(DEFINED windows_resource_file)
32    set_windows_version_resource_properties(${name} ${windows_resource_file}
33      VERSION_MAJOR ${CLANG_VERSION_MAJOR}
34      VERSION_MINOR ${CLANG_VERSION_MINOR}
35      VERSION_PATCHLEVEL ${CLANG_VERSION_PATCHLEVEL}
36      VERSION_STRING "${CLANG_VERSION} (${BACKEND_PACKAGE_STRING})"
37      PRODUCT_NAME "clang")
38  endif()
39endmacro()
40
41macro(add_clang_subdirectory name)
42  add_llvm_subdirectory(CLANG TOOL ${name})
43endmacro()
44
45macro(add_clang_library name)
46  cmake_parse_arguments(ARG
47    "SHARED;INSTALL_WITH_TOOLCHAIN"
48    ""
49    "ADDITIONAL_HEADERS"
50    ${ARGN})
51  set(srcs)
52  if(MSVC_IDE OR XCODE)
53    # Add public headers
54    file(RELATIVE_PATH lib_path
55      ${CLANG_SOURCE_DIR}/lib/
56      ${CMAKE_CURRENT_SOURCE_DIR}
57    )
58    if(NOT lib_path MATCHES "^[.][.]")
59      file( GLOB_RECURSE headers
60        ${CLANG_SOURCE_DIR}/include/clang/${lib_path}/*.h
61        ${CLANG_SOURCE_DIR}/include/clang/${lib_path}/*.def
62      )
63      set_source_files_properties(${headers} PROPERTIES HEADER_FILE_ONLY ON)
64
65      file( GLOB_RECURSE tds
66        ${CLANG_SOURCE_DIR}/include/clang/${lib_path}/*.td
67      )
68      source_group("TableGen descriptions" FILES ${tds})
69      set_source_files_properties(${tds}} PROPERTIES HEADER_FILE_ONLY ON)
70
71      if(headers OR tds)
72        set(srcs ${headers} ${tds})
73      endif()
74    endif()
75  endif(MSVC_IDE OR XCODE)
76  if(srcs OR ARG_ADDITIONAL_HEADERS)
77    set(srcs
78      ADDITIONAL_HEADERS
79      ${srcs}
80      ${ARG_ADDITIONAL_HEADERS} # It may contain unparsed unknown args.
81      )
82  endif()
83  if(ARG_SHARED)
84    set(LIBTYPE SHARED)
85  else()
86    # llvm_add_library ignores BUILD_SHARED_LIBS if STATIC is explicitly set,
87    # so we need to handle it here.
88    if(BUILD_SHARED_LIBS)
89      set(LIBTYPE SHARED)
90    else()
91      set(LIBTYPE STATIC)
92    endif()
93    if(NOT XCODE)
94      # The Xcode generator doesn't handle object libraries correctly.
95      list(APPEND LIBTYPE OBJECT)
96    endif()
97    set_property(GLOBAL APPEND PROPERTY CLANG_STATIC_LIBS ${name})
98  endif()
99  llvm_add_library(${name} ${LIBTYPE} ${ARG_UNPARSED_ARGUMENTS} ${srcs})
100
101  if(TARGET ${name})
102    target_link_libraries(${name} INTERFACE ${LLVM_COMMON_LIBS})
103
104    if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ARG_INSTALL_WITH_TOOLCHAIN)
105      set(export_to_clangtargets)
106      if(${name} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR
107          "clang-libraries" IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR
108          NOT LLVM_DISTRIBUTION_COMPONENTS)
109        set(export_to_clangtargets EXPORT ClangTargets)
110        set_property(GLOBAL PROPERTY CLANG_HAS_EXPORTS True)
111      endif()
112
113      install(TARGETS ${name}
114        COMPONENT ${name}
115        ${export_to_clangtargets}
116        LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
117        ARCHIVE DESTINATION lib${LLVM_LIBDIR_SUFFIX}
118        RUNTIME DESTINATION bin)
119
120      if (NOT LLVM_ENABLE_IDE)
121        add_llvm_install_targets(install-${name}
122                                 DEPENDS ${name}
123                                 COMPONENT ${name})
124      endif()
125
126      set_property(GLOBAL APPEND PROPERTY CLANG_LIBS ${name})
127    endif()
128    set_property(GLOBAL APPEND PROPERTY CLANG_EXPORTS ${name})
129  else()
130    # Add empty "phony" target
131    add_custom_target(${name})
132  endif()
133
134  set_target_properties(${name} PROPERTIES FOLDER "Clang libraries")
135  set_clang_windows_version_resource_properties(${name})
136endmacro(add_clang_library)
137
138macro(add_clang_executable name)
139  add_llvm_executable( ${name} ${ARGN} )
140  set_target_properties(${name} PROPERTIES FOLDER "Clang executables")
141  set_clang_windows_version_resource_properties(${name})
142endmacro(add_clang_executable)
143
144macro(add_clang_tool name)
145  if (NOT CLANG_BUILD_TOOLS)
146    set(EXCLUDE_FROM_ALL ON)
147  endif()
148
149  add_clang_executable(${name} ${ARGN})
150  add_dependencies(${name} clang-resource-headers)
151
152  if (CLANG_BUILD_TOOLS)
153    set(export_to_clangtargets)
154    if(${name} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR
155        NOT LLVM_DISTRIBUTION_COMPONENTS)
156      set(export_to_clangtargets EXPORT ClangTargets)
157      set_property(GLOBAL PROPERTY CLANG_HAS_EXPORTS True)
158    endif()
159
160    install(TARGETS ${name}
161      ${export_to_clangtargets}
162      RUNTIME DESTINATION bin
163      COMPONENT ${name})
164
165    if(NOT LLVM_ENABLE_IDE)
166      add_llvm_install_targets(install-${name}
167                               DEPENDS ${name}
168                               COMPONENT ${name})
169    endif()
170    set_property(GLOBAL APPEND PROPERTY CLANG_EXPORTS ${name})
171  endif()
172endmacro()
173
174macro(add_clang_symlink name dest)
175  add_llvm_tool_symlink(${name} ${dest} ALWAYS_GENERATE)
176  # Always generate install targets
177  llvm_install_symlink(${name} ${dest} ALWAYS_GENERATE)
178endmacro()
179
180function(clang_target_link_libraries target type)
181  if (CLANG_LINK_CLANG_DYLIB)
182    target_link_libraries(${target} ${type} clang-cpp)
183  else()
184    target_link_libraries(${target} ${type} ${ARGN})
185  endif()
186
187endfunction()
188