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 emitter_base.h 28 emitter_base.cc 29 generator.h 30 generator.cc 31 types.h 32 types.cc 33) 34add_library(sapi::generator ALIAS sapi_generator) 35target_compile_definitions(sapi_generator PUBLIC 36 ${LLVM_DEFINITIONS} 37) 38target_include_directories(sapi_generator PUBLIC 39 ${LLVM_INCLUDE_DIRS} 40) 41list(APPEND _sapi_generator_llvm_comp 42 Core 43 BinaryFormat 44 Remarks 45 BitstreamReader 46 Support 47 Demangle 48 FrontendOpenMP 49 MC 50 MCParser 51 Option 52 ProfileData 53 Support 54 BinaryFormat 55 Demangle 56) 57if(LLVM_VERSION VERSION_GREATER_EQUAL "15.0.0") 58 list(APPEND _sapi_generator_llvm_comp 59 WindowsDriver # Always needed 60 ) 61endif() 62llvm_map_components_to_libnames(_sapi_generator_llvm_libs 63 ${_sapi_generator_llvm_comp} 64) 65if(NOT SAPI_ENABLE_CLANG_TOOL_STATIC) 66 # Use regular library name 67 list(APPEND _sapi_generator_clang_libs clang-cpp) 68else() 69 foreach(_clang_lib IN ITEMS 70 # Required Clang libraries 71 clangBasic 72 clangLex 73 clangParse 74 clangAST 75 clangASTMatchers 76 clangSema 77 clangAnalysis 78 clangEdit 79 clangRewrite 80 clangDriver 81 clangSerialization 82 clangFrontend 83 clangToolingCore 84 clangToolingInclusions 85 clangTooling 86 clangFormat 87 clangSupport # Needed from LLVM 15 onwards 88 ) 89 if(TARGET ${_clang_lib}) 90 list(APPEND _sapi_generator_clang_libs ${_clang_lib}) 91 endif() 92 endforeach() 93 # Remove dependency on dynamic library libLLVM.so, which is added by Clang. 94 # Necessary symbols from LLVM are provided by _sapi_generator_llvm_libs. 95 foreach(_clang_lib IN LISTS _sapi_generator_clang_libs) 96 get_target_property(_clang_link ${_clang_lib} INTERFACE_LINK_LIBRARIES) 97 if(_clang_link) 98 list(REMOVE_ITEM _clang_link LLVM) 99 set_target_properties(${_clang_lib} PROPERTIES 100 INTERFACE_LINK_LIBRARIES "${_clang_link}" 101 ) 102 endif() 103 endforeach() 104endif() 105target_link_libraries(sapi_generator PUBLIC 106 sapi::base 107 absl::btree 108 absl::flat_hash_set 109 absl::node_hash_set 110 absl::random_random 111 absl::status 112 absl::statusor 113 absl::strings 114 sapi::fileops 115 sapi::status 116 ${_sapi_generator_clang_libs} 117 ${_sapi_generator_llvm_libs} 118) 119 120add_executable(sapi_generator_tool 121 compilation_database.cc 122 compilation_database.h 123 generator_tool.cc 124) 125target_link_libraries(sapi_generator_tool PRIVATE 126 absl::base 127 absl::no_destructor 128 absl::status 129 absl::strings 130 sapi::base 131 sapi::file_base 132 sapi::file_helpers 133 sapi::fileops 134 sapi::generator 135) 136if(SAPI_ENABLE_CLANG_TOOL_STATIC) 137 target_link_options(sapi_generator_tool PRIVATE 138 # These work for both GCC and Clang 139 -static-libgcc 140 -static-libstdc++ 141 ) 142endif() 143 144if(BUILD_TESTING AND SAPI_BUILD_TESTING) 145 add_executable(sapi_generator_test 146 frontend_action_test_util.cc 147 frontend_action_test_util.h 148 emitter_test.cc 149 ) 150 target_link_libraries(sapi_generator_test PRIVATE 151 absl::flat_hash_map 152 absl::memory 153 absl::statusor 154 benchmark 155 sapi::sapi 156 sapi::generator 157 sapi::status 158 sapi::status_matchers 159 sapi::test_main 160 ) 161 gtest_discover_tests_xcompile(sapi_generator_test) 162endif() 163