2 *------------------------------------------------------------------
3 * vlib_version.c - generate a vlib version stamp
5 * Copyright (c) 2013 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
23 #include <sys/types.h>
27 #include <vppinfra/clib.h>
28 #include <vppinfra/vec.h>
29 #include <vppinfra/hash.h>
30 #include <vppinfra/bitmap.h>
31 #include <vppinfra/fifo.h>
32 #include <vppinfra/time.h>
33 #include <vppinfra/mheap.h>
34 #include <vppinfra/heap.h>
35 #include <vppinfra/pool.h>
36 #include <vppinfra/format.h>
37 #include <vppinfra/error.h>
38 #include <vppinfra/unix.h>
47 version_main_t version_main;
50 "#include <vlib/vlib.h>"
52 "static char * %s_version_string = \n\"%s\";\n"
53 "static char * %s_dir_string = \n\"%s\";\n"
54 "static char * %s_git_branch = \n\"%s\";\n"
56 "static clib_error_t *\n"
57 "show_%s_version_command_fn (vlib_main_t * vm,\n"
58 " unformat_input_t * input,\n"
59 " vlib_cli_command_t * cmd)\n"
61 " vlib_cli_output (vm, \"%%s\", %s_version_string);\n"
62 " if (unformat (input, \"verbose\")){\n"
63 " vlib_cli_output (vm, \"%%s\", %s_dir_string);\n"
64 " vlib_cli_output (vm, \"%%s\", %s_git_branch);\n"
69 "VLIB_CLI_COMMAND (show_%s_version_command, static) = {\n"
70 " .path = \"show version %s\",\n"
71 " .short_help = \"show version information for %s\",\n"
72 " .function = show_%s_version_command_fn,\n"
75 static char *api_fmt =
76 "char * %s_api_get_build_directory (void) \n{\n return \"%s\";\n}\n\n"
77 "char * %s_api_get_branch (void) \n{\n return \"%s\";\n}\n"
78 "char * %s_api_get_build_date (void) \n{\n return \"%s\";\n}";
81 write_version_file (version_main_t *vm)
90 struct passwd *passwd_file_entry;
91 time_t now = time (0);
92 clib_error_t * error = 0;
94 /* kill the newline */
95 ts = format (0, "%s", ctime (&now));
96 ts[vec_len(ts)-1] = 0;
98 vec_validate (hostname, 128);
100 gethostname (hostname, vec_len (hostname)-1);
101 hostname[128] = 0; /* jic */
103 vec_validate (pathname, 256);
104 { char *rv __attribute__((unused)) =
105 getcwd ((char *)pathname, vec_len(pathname) - 1);
108 passwd_file_entry = getpwuid(geteuid());
110 vs = format (0, "%s built by %s on %s at %s%c",
111 vm->program, passwd_file_entry->pw_name, hostname, ts, 0);
113 ds = format (0, "in %s%c", pathname, 0);
115 gb = format (0, "from git uber-branch %s%c", vm->git_branch, 0);
117 pgm = format (0, fmt,
130 if (write (vm->ofd, pgm, vec_len (pgm)) != vec_len (pgm))
131 error = clib_error_return_unix (0, "write error on %s",
132 vm->output_filename);
134 api_fns = format (0, api_fmt,
135 vm->program, pathname,
136 vm->program, vm->git_branch,
139 if (write (vm->ofd, api_fns, vec_len (api_fns)) != vec_len (api_fns))
140 error = clib_error_return_unix (0, "write error on %s",
141 vm->output_filename);
147 clib_error_t * version_main_fn (unformat_input_t * input)
149 version_main_t * vm = &version_main;
151 clib_error_t * error;
153 vm->output_filename = format (0, "version.c");
154 vm->program = "unknown";
155 vm->git_branch = "unknown";
156 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
158 if (unformat (input, "output-filename %s", &fn)
159 || unformat (input, "-o %s", &fn))
160 vm->output_filename = fn;
161 else if (unformat (input, "program-name %s", &pn)
162 || unformat (input, "-p %s", &pn))
165 vec_add1 (vm->program, 0);
167 else if (unformat (input, "git-branch %s", &bn)
168 || unformat (input, "-b %s", &bn))
171 vec_add1 (vm->git_branch, 0);
174 return clib_error_return (0, "unknown args '%U'",
175 format_unformat_error, input);
177 vec_add1 (vm->output_filename, 0);
179 vm->ofd = creat (vm->output_filename, 0666);
181 return clib_error_return_unix (0, "couldn't create '%s'",
182 vm->output_filename);
184 error = write_version_file (vm);
190 int main (int argc, char **argv)
192 unformat_input_t _input, *input = &_input;
193 clib_error_t * error;
195 unformat_init_command_line (input, argv);
196 error = version_main_fn (input);
197 unformat_free (input);
201 clib_error_report (error);