The Battle for Wesnoth  1.13.11+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
build_info.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2015 - 2018 by Iris Morelle <shadowm2006@gmail.com>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
17 #include "build_info.hpp"
18 
19 #include "desktop/version.hpp"
20 #include "game_config.hpp"
21 #include "filesystem.hpp"
22 #include "formatter.hpp"
23 #include "gettext.hpp"
24 
25 #include <algorithm>
26 
27 #include <SDL.h>
28 #include <SDL_image.h>
29 #include <SDL_mixer.h>
30 #include <SDL_ttf.h>
31 
32 #include <boost/version.hpp>
33 
34 #include <openssl/crypto.h>
35 #include <openssl/opensslv.h>
36 
37 #include <pango/pangocairo.h>
38 
39 #ifdef HAVE_LIBPNG
40 #include <png.h>
41 #endif
42 
43 #ifdef __APPLE__
44 // apple_notification.mm uses Foundation.h, which is an Objective-C header;
45 // but CoreFoundation.h is a C header which also defines these.
46 #include <CoreFoundation/CoreFoundation.h>
47 
48 #if (defined MAC_OS_X_VERSION_10_8) && (MAC_OS_X_VERSION_10_8 <= MAC_OS_X_VERSION_MAX_ALLOWED)
49 #define HAVE_NS_USER_NOTIFICATION
50 #endif
51 #endif
52 
53 namespace game_config
54 {
55 
56 namespace {
57 
58 struct version_table_manager
59 {
60  std::vector<std::string> compiled, linked, names;
61  std::vector<optional_feature> features;
62 
63  version_table_manager();
64 };
65 
66 const version_table_manager versions;
67 
68 #if 0
69 std::string format_version(unsigned a, unsigned b, unsigned c)
70 {
71  return formatter() << a << '.' << b << '.' << c;
72 }
73 #endif
74 
75 std::string format_version(const SDL_version& v)
76 {
77  return formatter() << unsigned(v.major) << '.'
78  << unsigned(v.minor) << '.'
79  << unsigned(v.patch);
80 }
81 
82 std::string format_openssl_patch_level(uint8_t p)
83 {
84  return p <= 26
85  ? std::string(1, 'a' + char(p) - 1)
86  : "patch" + std::to_string(p);
87 }
88 
89 std::string format_openssl_version(long v)
90 {
91  int major, minor, fix, patch, status;
92  std::ostringstream fmt;
93 
94  //
95  // The people who maintain OpenSSL are not from this world. I suppose it's
96  // only fair that I'm the one who gets to try to make sense of their version
97  // encoding scheme. -- shadowm
98  //
99 
100  if(v < 0x0930L) {
101  // Pre-0.9.3 seems simpler times overall.
102  minor = v & 0x0F00L >> 8;
103  fix = v & 0x00F0L >> 4;
104  patch = v & 0x000FL;
105 
106  fmt << "0." << minor << '.' << fix;
107  if(patch) {
108  fmt << format_openssl_patch_level(patch);
109  }
110  } else {
111  //
112  // Note that they either assume the major version will never be greater than
113  // 9, they plan to use hexadecimal digits for versions 10.x.x through
114  // 15.x.x, or they expect long to be always > 32-bits by then. Who the hell
115  // knows, really.
116  //
117  major = (v & 0xF0000000L) >> 28;
118  minor = (v & 0x0FF00000L) >> 20;
119  fix = (v & 0x000FF000L) >> 12;
120  patch = (v & 0x00000FF0L) >> 4;
121  status = (v & 0x0000000FL);
122 
123  if(v < 0x00905100L) {
124  //
125  // From wiki.openssl.org (also mentioned in opensslv.h, in the most oblique
126  // fashion possible):
127  //
128  // "Versions between 0.9.3 and 0.9.5 had a version identifier with this interpretation:
129  // MMNNFFRBB major minor fix final beta/patch"
130  //
131  // Both the wiki and opensslv.h fail to accurately list actual version
132  // numbers that ended up used in the wild -- e.g. 0.9.3a is supposedly
133  // 0x0090301f when it really was 0x00903101.
134  //
135  const uint8_t is_final = (v & 0xF00L) >> 8;
136  status = is_final ? 0xF : 0;
137  patch = v & 0xFFL;
138  } else if(v < 0x00906000L) {
139  //
140  // Quoth opensslv.h:
141  //
142  // "For continuity reasons (because 0.9.5 is already out, and is coded
143  // 0x00905100), between 0.9.5 and 0.9.6 the coding of the patch level
144  // part is slightly different, by setting the highest bit. This means
145  // that 0.9.5a looks like this: 0x0090581f. At 0.9.6, we can start
146  // with 0x0090600S..."
147  //
148  patch ^= 1 << 7;
149  }
150 
151  fmt << major << '.' << minor << '.' << fix;
152 
153  if(patch) {
154  fmt << format_openssl_patch_level(patch);
155  }
156 
157  if(status == 0x0) {
158  fmt << "-dev";
159  } else if(status < 0xF) {
160  fmt << "-beta" << status;
161  }
162  }
163 
164  return fmt.str();
165 
166 }
167 
168 version_table_manager::version_table_manager()
169  : compiled(LIB_COUNT, "")
170  , linked(LIB_COUNT, "")
171  , names(LIB_COUNT, "")
172  , features()
173 {
174  SDL_version sdl_version;
175 
176 
177  //
178  // SDL
179  //
180 
181  SDL_VERSION(&sdl_version);
182  compiled[LIB_SDL] = format_version(sdl_version);
183 
184  SDL_GetVersion(&sdl_version);
185  linked[LIB_SDL] = format_version(sdl_version);
186 
187  names[LIB_SDL] = "SDL";
188 
189  //
190  // SDL_image
191  //
192 
193  SDL_IMAGE_VERSION(&sdl_version);
194  compiled[LIB_SDL_IMAGE] = format_version(sdl_version);
195 
196  const SDL_version* sdl_rt_version = IMG_Linked_Version();
197  if(sdl_rt_version) {
198  linked[LIB_SDL_IMAGE] = format_version(*sdl_rt_version);
199  }
200 
201  names[LIB_SDL_IMAGE] = "SDL_image";
202 
203  //
204  // SDL_mixer
205  //
206 
207  SDL_MIXER_VERSION(&sdl_version);
208  compiled[LIB_SDL_MIXER] = format_version(sdl_version);
209 
210  sdl_rt_version = Mix_Linked_Version();
211  if(sdl_rt_version) {
212  linked[LIB_SDL_MIXER] = format_version(*sdl_rt_version);
213  }
214 
215  names[LIB_SDL_MIXER] = "SDL_mixer";
216 
217  //
218  // SDL_ttf
219  //
220 
221  SDL_TTF_VERSION(&sdl_version);
222  compiled[LIB_SDL_TTF] = format_version(sdl_version);
223 
224  sdl_rt_version = TTF_Linked_Version();
225  if(sdl_rt_version) {
226  linked[LIB_SDL_TTF] = format_version(*sdl_rt_version);
227  }
228 
229  names[LIB_SDL_TTF] = "SDL_ttf";
230 
231  //
232  // Boost
233  //
234 
235  compiled[LIB_BOOST] = BOOST_LIB_VERSION;
236  std::replace(compiled[LIB_BOOST].begin(), compiled[LIB_BOOST].end(), '_', '.');
237  names[LIB_BOOST] = "Boost";
238 
239  //
240  // OpenSSL/libcrypto
241  //
242 
243  compiled[LIB_CRYPTO] = format_openssl_version(OPENSSL_VERSION_NUMBER);
244  linked[LIB_CRYPTO] = format_openssl_version(SSLeay());
245  names[LIB_CRYPTO] = "OpenSSL/libcrypto";
246 
247  //
248  // Cairo
249  //
250 
251  compiled[LIB_CAIRO] = CAIRO_VERSION_STRING;
252  linked[LIB_CAIRO] = cairo_version_string();
253  names[LIB_CAIRO] = "Cairo";
254 
255  //
256  // Pango
257  //
258 
259  compiled[LIB_PANGO] = PANGO_VERSION_STRING;
260  linked[LIB_PANGO] = pango_version_string();
261  names[LIB_PANGO] = "Pango";
262 
263  //
264  // libpng
265  //
266 
267 #ifdef HAVE_LIBPNG
268  compiled[LIB_PNG] = PNG_LIBPNG_VER_STRING;
269  linked[LIB_PNG] = png_get_libpng_ver(nullptr);
270  names[LIB_PNG] = "libpng";
271 #endif
272 
273  //
274  // Features table.
275  //
276 
277  features.emplace_back(N_("feature^Experimental OpenMP support"));
278 #ifdef _OPENMP
279  features.back().enabled = true;
280 #endif
281 
282  features.emplace_back(N_("feature^PNG screenshots"));
283 #ifdef HAVE_LIBPNG
284  features.back().enabled = true;
285 #endif
286 
287  features.emplace_back(N_("feature^Lua console completion"));
288 #ifdef HAVE_HISTORY
289  features.back().enabled = true;
290 #endif
291 
292  features.emplace_back(N_("feature^Legacy bidirectional rendering"));
293 #ifdef HAVE_FRIBIDI
294  features.back().enabled = true;
295 #endif
296 
297 #ifdef _X11
298 
299  features.emplace_back(N_("feature^D-Bus notifications back end"));
300 #ifdef HAVE_LIBDBUS
301  features.back().enabled = true;
302 #endif
303 
304 #endif /* _X11 */
305 
306 #ifdef _WIN32
307  // Always compiled in.
308  features.emplace_back(N_("feature^Win32 notifications back end"));
309  features.back().enabled = true;
310 #endif
311 
312 #ifdef __APPLE__
313 
314  features.emplace_back(N_("feature^Cocoa notifications back end"));
315 #ifdef HAVE_NS_USER_NOTIFICATION
316  features.back().enabled = true;
317 #endif
318 
319  features.emplace_back(N_("feature^Growl notifications back end"));
320 #ifdef HAVE_GROWL
321  features.back().enabled = true;
322 #endif
323 
324 #endif /* __APPLE__ */
325 }
326 
327 const std::string empty_version = "";
328 
329 } // end anonymous namespace 1
330 
331 std::vector<optional_feature> optional_features_table()
332 {
333  std::vector<optional_feature> res = versions.features;
334 
335  for(size_t k = 0; k < res.size(); ++k) {
336  res[k].name = _(res[k].name.c_str());
337  }
338  return res;
339 }
340 
342 {
343  if(lib >= LIB_COUNT) {
344  return empty_version;
345  }
346 
347  return versions.compiled[lib];
348 }
349 
351 {
352  if(lib >= LIB_COUNT) {
353  return empty_version;
354  }
355 
356  return versions.linked[lib];
357 }
358 
360 {
361  if(lib >= LIB_COUNT) {
362  return empty_version;
363  }
364 
365  return versions.names[lib];
366 }
367 
368 namespace {
369 
370 bool strlen_comparator(const std::string& a, const std::string& b)
371 {
372  return a.length() < b.length();
373 }
374 
375 size_t max_strlen(const std::vector<std::string>& strs)
376 {
377  const std::vector<std::string>::const_iterator it =
378  std::max_element(strs.begin(), strs.end(), strlen_comparator);
379 
380  return it != strs.end() ? it->length() : 0;
381 }
382 
383 } // end anonymous namespace 2
384 
386 {
387  std::ostringstream o;
388 
389  const size_t col2_start = max_strlen(versions.names) + 2;
390  const size_t col3_start = max_strlen(versions.compiled) + 1;
391 
392  for(unsigned n = 0; n < LIB_COUNT; ++n)
393  {
394  const std::string& name = versions.names[n];
395  const std::string& compiled = versions.compiled[n];
396  const std::string& linked = versions.linked[n];
397 
398  if(name.empty()) {
399  continue;
400  }
401 
402  o << name << ": ";
403 
404  const size_t pos2 = name.length() + 2;
405  if(pos2 < col2_start) {
406  o << std::string(col2_start - pos2, ' ');
407  }
408 
409  o << compiled;
410 
411  if(!linked.empty()) {
412  const size_t pos3 = compiled.length() + 1;
413  if(pos3 < col3_start) {
414  o << std::string(col3_start - pos3, ' ');
415  }
416  o << " (runtime " << linked << ")";
417  }
418 
419  o << '\n';
420  }
421 
422  return o.str();
423 }
424 
426 {
427  // Yes, it's for stdout/stderr but we still want the localized version so
428  // that the context prefixes are stripped.
429  const std::vector<optional_feature>& features = optional_features_table();
430 
431  size_t col2_start = 0;
432 
433  for(size_t k = 0; k < features.size(); ++k)
434  {
435  col2_start = std::max(col2_start, features[k].name.length() + 2);
436  }
437 
438  std::ostringstream o;
439 
440  for(size_t k = 0; k < features.size(); ++k)
441  {
442  const optional_feature& f = features[k];
443 
444  o << f.name << ": ";
445 
446  const size_t pos2 = f.name.length() + 2;
447  if(pos2 < col2_start) {
448  o << std::string(col2_start - pos2, ' ');
449  }
450 
451  o << (f.enabled ? "yes" : "no") << '\n';
452  }
453 
454  return o.str();
455 }
456 
458 {
459  std::ostringstream o;
460 
461  o << "The Battle for Wesnoth version " << game_config::revision << '\n'
462  << "Running on " << desktop::os_version() << '\n'
463  << '\n'
464  << "Game paths\n"
465  << "==========\n"
466  << '\n'
467  << "Data dir: " << game_config::path << '\n'
468  << "User config dir: " << filesystem::get_user_config_dir() << '\n'
469  << "User data dir: " << filesystem::get_user_data_dir() << '\n'
470  << "Saves dir: " << filesystem::get_saves_dir() << '\n'
471  << "Add-ons dir: " << filesystem::get_addons_dir() << '\n'
472  << "Cache dir: " << filesystem::get_cache_dir() << '\n'
473  << '\n'
474  << "Libraries\n"
475  << "=========\n"
476  << '\n'
478  << '\n'
479  << "Features\n"
480  << "========\n"
481  << '\n'
483 
484  return o.str();
485 }
486 
487 } // end namespace game_config
std::vector< char_t > string
std::string library_versions_report()
Produce a plain-text report of library versions suitable for stdout/stderr.
Definition: build_info.cpp:385
std::string optional_features_report()
Produce a plain-text report of features suitable for stdout/stderr.
Definition: build_info.cpp:425
#define a
STL namespace.
const std::string & library_build_version(LIBRARY_ID lib)
Retrieve the build-time version number of the given library.
Definition: build_info.cpp:341
std::string get_saves_dir()
#define b
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:89
const std::string & library_name(LIBRARY_ID lib)
Retrieve the user-visible name for the given library.
Definition: build_info.cpp:359
std::vector< optional_feature > optional_features_table()
Return a localized features table.
Definition: build_info.cpp:331
std::vector< optional_feature > features
Definition: build_info.cpp:61
std::ostringstream wrapper.
Definition: formatter.hpp:38
std::string get_user_data_dir()
std::string path
Definition: game_config.cpp:56
Platform identification and version information functions.
std::string get_cache_dir()
std::vector< std::string > linked
Definition: build_info.cpp:60
const std::string revision
Definition: game_config.cpp:50
mock_party p
Game configuration data as global variables.
Definition: build_info.cpp:53
std::string os_version()
Returns a string with the running OS name and version information.
Definition: version.cpp:118
std::vector< std::string > names
Definition: build_info.cpp:60
const std::string & library_runtime_version(LIBRARY_ID lib)
Retrieve the runtime version number of the given library.
Definition: build_info.cpp:350
Declarations for File-IO.
#define N_(String)
Definition: gettext.hpp:97
std::string get_user_config_dir()
#define f
std::string get_addons_dir()
static const char * name(const std::vector< SDL_Joystick * > &joysticks, const size_t index)
Definition: joystick.cpp:48
mock_char c
static map_location::DIRECTION n
std::vector< std::string > compiled
Definition: build_info.cpp:60
std::string full_build_report()
Produce a bug report-style info dump.
Definition: build_info.cpp:457