Introduce version.c to limit recompilation on version changes.
[paraslash.git] / version.c
1 #include "para.h"
2
3 /** \file version.h Macros for printing the version string. */
4
5 #include "git-version.h"
6
7 /**
8 * Get the raw git version string
9 *
10 * \return The string generated by the GIT-VERSION-GEN script. It is passed
11 * as a preprocessor define during compilation.
12 */
13 const char *version_git(void)
14 {
15 return GIT_VERSION;
16 }
17
18 /**
19 * Get the version string for an executable.
20 *
21 * \param pfx The program name (without the leading "para_").
22 *
23 * \return A statically allocated string which contains the program name, the
24 * git version and the codename. It must not be freed by the caller.
25 */
26 const char *version_single_line(const char *pfx)
27 {
28 static char buf[100];
29 snprintf(buf, sizeof(buf) - 1,
30 "para_%s " GIT_VERSION " (" CODENAME ")", pfx);
31 return buf;
32 }
33
34 /**
35 * Get the full version text.
36 *
37 * \param pfx See \ref version_single_line().
38 *
39 * \return A string containing the same text as returned by \ref
40 * version_single_line(), augmented by additional build information, a
41 * copyright text and the email address of the author.
42 *
43 * Like \ref version_single_line(), this string is stored in a statically
44 * allocated buffer and must not be freed.
45 */
46 const char *version_text(const char *pfx)
47 {
48 static char buf[512];
49
50 snprintf(buf, sizeof(buf) - 1, "%s\n"
51 "built: " BUILD_DATE ", " UNAME_RS ", " CC_VERSION "\n"
52 "Copyright (C) 2013 Andre Noll\n"
53 "This is free software with ABSOLUTELY NO WARRANTY."
54 " See COPYING for details.\n"
55 "Report bugs to <maan@systemlinux.org>.\n",
56 version_single_line(pfx)
57 );
58 return buf;
59 }
60
61 /**
62 * Print the version text and exit successfully.
63 *
64 * \param pfx See \ref version_single_line().
65 * \param flag Whether --version was given.
66 *
67 * If \a flag is false, this function does nothing. Otherwise it prints the
68 * full version text as returned by \ref version_text() and exits successfully.
69 */
70 void version_handle_flag(const char *pfx, bool flag)
71 {
72 if (!flag)
73 return;
74 printf("%s", version_text(pfx));
75 exit(EXIT_SUCCESS);
76 }