xref: /aosp_15_r20/external/stg/CMakeLists.txt (revision 9e3b08ae94a55201065475453d799e8b1378bea6)
1# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2#
3# Copyright 2023 Google LLC
4#
5# Licensed under the Apache License v2.0 with LLVM Exceptions (the
6# "License"); you may not use this file except in compliance with the
7# License.  You may obtain a copy of the License at
8#
9#     https://llvm.org/LICENSE.txt
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17# Author: Aleksei Vetrov
18
19cmake_minimum_required(VERSION 3.14)
20
21project(
22  stg
23  VERSION 0.0.1
24  LANGUAGES CXX)
25
26set(CMAKE_CXX_STANDARD 20)
27
28add_compile_options(-fstrict-enums -Wall -Wextra)
29
30set(MINIMUM_GNU_VERSION 11)
31set(MINIMUM_Clang_VERSION 15)
32
33# Note, the quotes around the variable are significant. If we use a compiler
34# that does not resolve to a definition above, the empty string corresponds to
35# a version where all components are omitted and hence treated as zero.
36if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "${MINIMUM_${CMAKE_CXX_COMPILER_ID}_VERSION}")
37  message(FATAL_ERROR "Unsupported Compiler Version!\n"
38          "Need at least ${CMAKE_CXX_COMPILER_ID} ${MINIMUM_${CMAKE_CXX_COMPILER_ID}_VERSION}")
39endif()
40
41if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
42  # GCC has problems detecting "no-return" switches and destructors.
43  add_compile_options(-Wno-return-type)
44endif()
45
46# Enable LTO for release builds
47set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE)
48set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO TRUE)
49
50list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
51
52find_package(LibElf 0.189 REQUIRED)
53find_package(LibDw 0.189 REQUIRED)
54find_package(LibXml2 2.9 REQUIRED)
55find_package(LinuxUAPI 5.19 REQUIRED)
56find_package(Protobuf 3.19 REQUIRED)
57
58if(NOT Protobuf_PROTOC_EXECUTABLE)
59  message(FATAL_ERROR "Could NOT find protobuf::protoc.
60Please install protobuf-compiler or set Protobuf_PROTOC_EXECUTABLE to the location of the \"protoc\" binary.")
61endif()
62
63set(COMMON_LIBRARIES
64    LibElf::LibElf
65    LibDw::LibDw
66    LibXml2::LibXml2
67    protobuf::libprotobuf)
68
69if(NOT Jemalloc_DISABLE)
70  find_package(Jemalloc 5)
71  if(Jemalloc_FOUND)
72    list(APPEND COMMON_LIBRARIES Jemalloc::Jemalloc)
73  else()
74    message(WARNING "jemalloc significantly improves performance, but is not functionally required to build STG.
75Use -DJemalloc_DISABLE=TRUE to disable jemalloc and suppress this warning.")
76  endif()
77endif()
78
79protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS stg.proto)
80
81add_library(libstg OBJECT
82  abigail_reader.cc
83  btf_reader.cc
84  comparison.cc
85  deduplication.cc
86  dwarf_processor.cc
87  dwarf_wrappers.cc
88  elf_dwarf_handle.cc
89  elf_loader.cc
90  elf_reader.cc
91  fidelity.cc
92  file_descriptor.cc
93  filter.cc
94  fingerprint.cc
95  graph.cc
96  input.cc
97  naming.cc
98  post_processing.cc
99  proto_reader.cc
100  proto_writer.cc
101  reporting.cc
102  runtime.cc
103  stable_hash.cc
104  type_normalisation.cc
105  type_resolution.cc
106  unification.cc
107  ${PROTO_SRCS}
108  ${PROTO_HDRS})
109# Needed for generated .pb.h files
110target_include_directories(libstg PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
111target_link_libraries(libstg PUBLIC ${COMMON_LIBRARIES})
112
113set(STG_EXECUTABLE_TARGETS stg stgdiff)
114
115foreach(TARGET IN LISTS STG_EXECUTABLE_TARGETS)
116  add_executable("${TARGET}" "${TARGET}.cc")
117  target_link_libraries("${TARGET}" PRIVATE libstg)
118endforeach()
119
120# Testing
121
122find_package(Catch2 2 QUIET)
123
124if(NOT Catch2_FOUND)
125
126  message(NOTICE "Catch2 v2 testing framework not found. Disabling tests.")
127
128else()
129  enable_testing()
130
131  add_custom_command(OUTPUT testdata
132    COMMAND ${CMAKE_COMMAND} -E create_symlink
133            ${CMAKE_SOURCE_DIR}/testdata/
134            ${CMAKE_BINARY_DIR}/testdata
135  )
136  add_custom_target(testdata_symlink DEPENDS testdata)
137
138  set(TEST_FILES
139    abigail_reader_test
140    elf_reader_test
141    error_test
142    file_descriptor_test
143    filter_test
144    hex_test
145    order_test
146    reporting_test
147    runtime_test
148    scc_test
149    scope_test
150    stgdiff_test
151  )
152
153  foreach(test_file ${TEST_FILES})
154    add_executable(${test_file} ${test_file}.cc)
155    target_link_libraries(${test_file} Catch2::Catch2WithMain libstg)
156    add_test(NAME ${test_file} COMMAND ${test_file})
157    add_dependencies(${test_file} testdata_symlink)
158  endforeach()
159
160endif()  # Catch2_FOUND
161
162# Installation and packaging
163
164include(GNUInstallDirs)
165
166install(
167  TARGETS ${STG_EXECUTABLE_TARGETS}
168  COMPONENT Binaries
169  RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
170