• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2020 Google LLC
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15# Minimum supported: LLVM 11.0.0
16find_package(LLVM REQUIRED)
17find_package(Clang REQUIRED)
18if(LLVM_VERSION VERSION_LESS "11.0.0")
19  message(FATAL_ERROR "SAPI header generator needs LLVM 11 or newer")
20endif()
21
22add_library(sapi_generator
23  diagnostics.cc
24  diagnostics.h
25  emitter.h
26  emitter.cc
27  generator.h
28  generator.cc
29  types.h
30  types.cc
31)
32add_library(sapi::generator ALIAS sapi_generator)
33target_compile_definitions(sapi_generator PUBLIC
34  ${LLVM_DEFINITIONS}
35)
36target_include_directories(sapi_generator PUBLIC
37  ${LLVM_INCLUDE_DIRS}
38)
39list(APPEND _sapi_generator_llvm_comp
40  Core
41  BinaryFormat
42  Remarks
43  BitstreamReader
44  Support
45  Demangle
46  FrontendOpenMP
47  MC
48  MCParser
49  Option
50  ProfileData
51  Support
52  BinaryFormat
53  Demangle
54)
55if(LLVM_VERSION VERSION_GREATER_EQUAL "15.0.0")
56  list(APPEND _sapi_generator_llvm_comp
57    WindowsDriver # Always needed
58  )
59endif()
60llvm_map_components_to_libnames(_sapi_generator_llvm_libs
61  ${_sapi_generator_llvm_comp}
62)
63if(NOT SAPI_ENABLE_CLANG_TOOL_STATIC)
64  # Use regular library name
65  list(APPEND _sapi_generator_clang_libs clang-cpp)
66else()
67  foreach(_clang_lib IN ITEMS
68    # Required Clang libraries
69    clangBasic
70    clangLex
71    clangParse
72    clangAST
73    clangASTMatchers
74    clangSema
75    clangAnalysis
76    clangEdit
77    clangRewrite
78    clangDriver
79    clangSerialization
80    clangFrontend
81    clangToolingCore
82    clangToolingInclusions
83    clangTooling
84    clangFormat
85    clangSupport # Needed from LLVM 15 onwards
86  )
87    if(TARGET ${_clang_lib})
88      list(APPEND _sapi_generator_clang_libs ${_clang_lib})
89    endif()
90  endforeach()
91  # Remove dependency on dynamic library libLLVM.so, which is added by Clang.
92  # Necessary symbols from LLVM are provided by _sapi_generator_llvm_libs.
93  foreach(_clang_lib IN LISTS _sapi_generator_clang_libs)
94    get_target_property(_clang_link ${_clang_lib} INTERFACE_LINK_LIBRARIES)
95    if(_clang_link)
96      list(REMOVE_ITEM _clang_link LLVM)
97      set_target_properties(${_clang_lib} PROPERTIES
98        INTERFACE_LINK_LIBRARIES "${_clang_link}"
99      )
100    endif()
101  endforeach()
102endif()
103target_link_libraries(sapi_generator PUBLIC
104  sapi::base
105  absl::btree
106  absl::flat_hash_set
107  absl::node_hash_set
108  absl::random_random
109  absl::status
110  absl::statusor
111  absl::strings
112  sapi::fileops
113  sapi::status
114  ${_sapi_generator_clang_libs}
115  ${_sapi_generator_llvm_libs}
116)
117
118add_executable(sapi_generator_tool
119  compilation_database.cc
120  compilation_database.h
121  generator_tool.cc
122)
123target_link_libraries(sapi_generator_tool PRIVATE
124  absl::statusor
125  sapi::base
126  sapi::file_base
127  sapi::file_helpers
128  sapi::fileops
129  sapi::generator
130)
131if(SAPI_ENABLE_CLANG_TOOL_STATIC)
132  target_link_options(sapi_generator_tool PRIVATE
133    # These work for both GCC and Clang
134    -static-libgcc
135    -static-libstdc++
136  )
137endif()
138
139if(BUILD_TESTING AND SAPI_BUILD_TESTING)
140  add_executable(sapi_generator_test
141    frontend_action_test_util.cc
142    frontend_action_test_util.h
143    emitter_test.cc
144  )
145  target_link_libraries(sapi_generator_test PRIVATE
146    absl::flat_hash_map
147    absl::memory
148    absl::statusor
149    benchmark
150    sapi::sapi
151    sapi::generator
152    sapi::status
153    sapi::status_matchers
154    sapi::test_main
155  )
156  gtest_discover_tests_xcompile(sapi_generator_test)
157endif()
158