summaryrefslogtreecommitdiff
path: root/tools/gccabicheck/abicheck.c
blob: 4141edf7cf75356a3653d127600b4e9d6197ab0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* Copyright (C) 2015 Robin Gareus <robin@gareus.org>
 *
 * This program is free software. It comes without any warranty, to
 * the extent permitted by applicable law. You can redistribute it
 * and/or modify it under the terms of the Do What The Fuck You Want
 * To Public License, Version 2, as published by Sam Hocevar. See
 * http://www.wtfpl.net/ for more details.
 */

// gcc -Wall -o gcc-glibmm-abi-check abicheck.c -ldl
// help2man -N -n 'glib gcc4/5 C++11 ABI compatibility test' -o gcc-glibmm-abi-check.1 ./gcc-glibmm-abi-check

#include <stdio.h>
#include <dlfcn.h>
#include <getopt.h>

#ifndef VERSION
#define VERSION "0.1"
#endif

static void print_usage (void) {
	printf ("gcc-glibmm-abi-check - gcc4/5 C++11 ABI compatibility test\n\n");

	printf ("Usage: gcc-glibmm-abi-check [ OPTIONS ]\n\n");
	printf (
			"This tool checks for C++ specific symbols in libglimm which are different in\n"
			"the gcc4 and gcc5/c++11 ABI in order to determine system-wide use of gcc5.\n"
			// TODO document error codes,...
			);

	printf ("\nOptions:\n"
			" -f, --fail                fail if system cannot be determined.\n"
			" -h, --help                Display this help and exit.\n"
			" -4, --gcc4                Test succeeds if gcc4 ABI is found.\n"
			" -5, --gcc5                Test succeeds if gcc5 ABI is found.\n"
			" -g <soname>, --glibmm <soname>\n"
			"                           Specify alternative file for libglibmm-2.4.so\n"
			" -v, --verbose             Print information.\n"
			" -V, --version             Print version information and exit.\n"
			);
}

static void print_version (void) {
	printf ("gcc-glibmm-abi-check version %s\n\n", VERSION);
	printf (
			"Copyright (C) 2015 Robin Gareus <robin@gareus.org>\n"
			"This is free software; see the source for copying conditions.  There is NO\n"
			"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
}


int main (int argc, char **argv) {
	int expect = 0;
	int error_fail = 0;
	int verbose = 0;

	char const * glibmm = "libglibmm-2.4.so.1";

	const struct option long_options[] = {
		{ "fail",       no_argument,       0, 'f' },
		{ "help",       no_argument,       0, 'h' },
		{ "gcc4",       no_argument,       0, '4' },
		{ "gcc5",       no_argument,       0, '5' },
		{ "glibmm",     required_argument, 0, 'g' },
		{ "verbose",    no_argument,       0, 'v' },
		{ "version",    no_argument,       0, 'V' },
	};

	const char *optstring = "fh45g:vV";

	int c;
	while ((c = getopt_long (argc, argv, optstring, long_options, NULL)) != -1) {
		switch (c) {
			case 'f':
				error_fail = 1;
				break;
			case 'h':
				print_usage ();
				return 0;
				break;
			case '4':
				expect |= 1;
				break;
			case '5':
				expect |= 2;
				break;
			case 'g':
				glibmm = optarg;
				break;
			case 'v':
				verbose = 1;
				break;
			case 'V':
				print_version ();
				return 0;
				break;
			default:
				fprintf (stderr, "invalid argument.\n");
				print_usage ();
				return -1;
				break;
		}
	}

	int gcc5 = 0;
	int gcc4 = 0;

	dlerror (); // reset error

	void *h = dlopen (glibmm, RTLD_LAZY);
	if (!h) {
		if (verbose) {
			fprintf (stderr, "Cannot open '%s': %s.\n", glibmm, dlerror ());
		}
		return error_fail ? 3 : 0;
	}

	// Glib::ustring::ustring(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)
	if (dlsym (h, "_ZN4Glib7ustringC1ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE")) {
		gcc5 |= 1;
	}

	// Glib::ustring::ustring(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)
	if (dlsym (h, "_ZN4Glib7ustringC1ERKSs")) {
		gcc4 |= 1;
	}


	// Glib::Module::Module(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Glib::ModuleFlags)
	if (dlsym (h, "_ZN4Glib6ModuleC1ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEENS_11ModuleFlagsE")) {
		gcc5 |= 2;
	}

	// Glib::Module::Module(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Glib::ModuleFlags)
	if (dlsym (h, "_ZN4Glib6ModuleC1ERKSsNS_11ModuleFlagsE")) {
		gcc4 |= 2;
	}


	// Glib::ustring::operator=(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)
	if (dlsym (h, "_ZN4Glib7ustringaSERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE")) {
		gcc5 |= 4;
	}

	// Glib::ustring::operator=(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)
	if (dlsym (h, "_ZN4Glib7ustringaSERKSs")) {
		gcc4 |= 4;
	}

	dlclose (h);

	if (7 != (gcc4 ^ gcc5)) {
		if (verbose) {
			fprintf (stderr, "Inconsistent result: gcc4=%x gcc5=%x\n", gcc4, gcc5);
		}
	}
	else if (gcc4 == 7) {
		if (verbose) {
			printf ("System uses gcc4 c++ ABI\n");
		}
		if (expect != 0) {
			return (expect & 1) ? 0 : 1;
		}
	}
	else if (gcc5 == 7) {
		if (verbose) {
			printf ("System uses gcc5 c++11 ABI\n");
		}
		if (expect != 0) {
			return (expect & 2) ? 0 : 1;
		}
	}
	else if (verbose) {
		fprintf (stderr, "Incomplete result: gcc4=%x gcc5=%x\n", gcc4, gcc5);
	}

	return error_fail ? 2 : 0;
}