Debian Codegraph @

cmake/tribits/common_tpls/FindTPLBinUtils.cmake

raw
1 # @HEADER
2 # *****************************************************************************
3 # Trilinos: An Object-Oriented Solver Framework
4 #
5 # Copyright 2001-2024 NTESS and the Trilinos contributors.
6 # SPDX-License-Identifier: BSD-3-Clause
7 # *****************************************************************************
8 # @HEADER
9
10
11 #
12 # A) Start out by just finding the headers and libraries
13 #
14
15
16
17
18 tribits_tpl_find_include_dirs_and_libraries( BinUtils
19 REQUIRED_HEADERS link.h bfd.h
20 MUST_FIND_ALL_HEADERS
21 REQUIRED_LIBS_NAMES bfd iberty
22 MUST_FIND_ALL_LIBS
23 NO_PRINT_ENABLE_SUCCESS_FAIL
24 )
25
26
27 #
28 # B) Now make sure that you can actually link a program
29 #
30
31
32 include(CheckCXXSourceCompiles)
33 include(MultilineSet)
34 include(PrintVar)
35
36
37 function(check_for_binutils_stacktrace VARNAME)
38
39 set(SOURCE
40 "
41 #include <string>
42 #include <iostream>
43 #include <fstream>
44 #include <cstdlib>
45 #include <cstdarg>
46 #include <csignal>
47 #include <execinfo.h>
48 #include <cxxabi.h>
49 #include <link.h>
50 #include <bfd.h>
51
52
53 int shared_lib_callback(struct dl_phdr_info *info,
54 size_t size, void *_data)
55 {
56 (void)info;
57 (void)size;
58 (void)_data;
59 return 0;
60 }
61
62
63 void process_section(bfd *abfd, asection *section, void *_data)
64 {}
65
66
67 void call_a_bunch_of_functions()
68 {
69
70 void **stacktrace_array = 0;
71 const size_t stacktrace_size = backtrace(stacktrace_array, 0);
72
73 int status_demangle = 0;
74 char *filename_blob = 0;
75 char *d = abi::__cxa_demangle(filename_blob, 0, 0, &status_demangle);
76
77 asection *section = 0;
78 bfd *abfd = 0;
79
80 const int status_bfd_gsf = (bfd_get_section_flags(abfd, section) & SEC_ALLOC);
81
82 bfd_vma section_vma = bfd_get_section_vma(abfd, section);
83
84 bfd_size_type section_size = bfd_section_size(abfd, section);
85
86 bfd_vma offset;
87 asymbol **symbol_table = 0;
88 unsigned int line;
89
90 const char *filename=NULL, *function_name=NULL;
91 const int line_found = bfd_find_nearest_line(abfd, section, symbol_table,
92 offset, &filename, &function_name, &line);
93
94 const int status_bfd_gff = (bfd_get_file_flags(abfd) & HAS_SYMS);
95 void **symbol_table_ptr = 0;
96 long n_symbols;
97 unsigned int symbol_size;
98 n_symbols = bfd_read_minisymbols(abfd, false, symbol_table_ptr, &symbol_size);
99
100 abfd = bfd_openr(filename_blob, NULL);
101
102 const int status_bfd_cf = bfd_check_format(abfd, bfd_archive);
103
104 char **matching;
105 const int status_bfd_cfm = bfd_check_format_matches(abfd, bfd_object, &matching);
106
107 void *data = 0;
108 bfd_map_over_sections(abfd, process_section, &data);
109
110 bfd_close(abfd);
111
112 struct match_data *match;
113 const int status_dl_iphdr = dl_iterate_phdr(shared_lib_callback, &(*match));
114
115 }
116
117
118 int main()
119 {
120 call_a_bunch_of_functions();
121 return 0;
122 }
123 "
124 )
125
126 set(CMAKE_REQUIRED_INCLUDES ${TPL_BinUtils_INCLUDE_DIRS})
127 set(CMAKE_REQUIRED_LIBRARIES ${TPL_BinUtils_LIBRARIES})
128 check_cxx_source_compiles("${SOURCE}" ${VARNAME})
129
130 endfunction()
131
132
133 if (TPL_ENABLE_BinUtils)
134
135 check_for_binutils_stacktrace(HAS_TPL_BINUNTILS_STACKTRACE)
136
137 if (HAS_TPL_BINUNTILS_STACKTRACE)
138 message(STATUS "Extended attempt to enable tentatively enabled TPL 'BinUtils' passed!")
139 else()
140 message(STATUS "Extended attempt to enable tentatively enabled TPL 'BinUtils' failed! Setting TPL_ENABLE_BinUtils=OFF")
141 set(TPL_ENABLE_BinUtils OFF CACHE STRING "autoset" FORCE)
142 endif()
143
144 endif()
145
146
147 print_var(TPL_ENABLE_BinUtils)