Initial commit of vpp code.
[vpp.git] / vppversion / vlib_version.c
1 /*
2  *------------------------------------------------------------------
3  * vlib_version.c - generate a vlib version stamp
4  * 
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:
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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  *------------------------------------------------------------------
18  */
19
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <time.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <pwd.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>
39
40 typedef struct {
41   u8 * program;
42   u8 * output_filename;
43   u8 * git_branch;
44   int ofd;
45 } version_main_t;
46
47 version_main_t version_main;
48
49 static char * fmt = 
50 "#include <vlib/vlib.h>"
51 "\n"
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"
55 "\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"
60 "{\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"
65 "  }\n"
66 "  return 0;\n"
67 "}\n"
68 "\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"
73 "};\n\n";
74
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}";
79
80 clib_error_t * 
81 write_version_file (version_main_t *vm)
82 {
83   u8 * pgm, * api_fns;
84   u8 * vs;
85   u8 * ts;
86   u8 * ds;
87   u8 * gb;
88   u8 * hostname = 0;
89   u8 * pathname = 0;
90   struct passwd *passwd_file_entry;
91   time_t now = time (0);
92   clib_error_t * error = 0;
93
94   /* kill the newline */
95   ts = format (0, "%s", ctime (&now));
96   ts[vec_len(ts)-1] = 0;
97
98   vec_validate (hostname, 128);
99
100   gethostname (hostname, vec_len (hostname)-1);
101   hostname[128] = 0; /* jic */
102
103   vec_validate (pathname, 256);
104   { char *rv __attribute__((unused)) = 
105       getcwd ((char *)pathname, vec_len(pathname) - 1);
106   }
107
108   passwd_file_entry = getpwuid(geteuid());
109
110   vs = format (0, "%s built by %s on %s at %s%c",
111                vm->program, passwd_file_entry->pw_name, hostname, ts, 0);
112
113   ds = format (0, "in %s%c", pathname, 0);
114
115   gb = format (0, "from git uber-branch %s%c", vm->git_branch, 0);
116
117   pgm = format (0, fmt, 
118                 vm->program, vs, 
119                 vm->program, ds, 
120                 vm->program, gb,
121                 vm->program,
122                 vm->program,
123                 vm->program,
124                 vm->program,
125                 vm->program,
126                 vm->program,
127                 vm->program,
128                 vm->program);
129   
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);
133
134   api_fns = format (0, api_fmt, 
135                     vm->program, pathname,
136                     vm->program, vm->git_branch,
137                     vm->program, ts);
138                     
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);
142
143   return error;
144
145 }
146
147 clib_error_t * version_main_fn (unformat_input_t * input)
148 {
149   version_main_t * vm = &version_main;
150   u8 * fn, * pn, * bn;
151   clib_error_t * error;
152
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) 
157     {
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))
163         {
164           vm->program = pn;
165           vec_add1 (vm->program, 0);
166         }
167       else if (unformat (input, "git-branch %s", &bn)
168                || unformat (input, "-b %s", &bn))
169         {
170           vm->git_branch = bn;
171           vec_add1 (vm->git_branch, 0);
172         }
173       else
174         return clib_error_return (0, "unknown args '%U'", 
175                                   format_unformat_error, input);
176     }
177   vec_add1 (vm->output_filename, 0);
178
179   vm->ofd = creat (vm->output_filename, 0666);
180   if (vm->ofd < 0)
181     return clib_error_return_unix (0, "couldn't create '%s'", 
182                                    vm->output_filename);
183
184   error = write_version_file (vm);
185   close (vm->ofd);
186   return error;
187 }
188
189
190 int main (int argc, char **argv)
191 {
192   unformat_input_t _input, *input = &_input;
193   clib_error_t * error;
194
195   unformat_init_command_line (input, argv);
196   error = version_main_fn (input);
197   unformat_free (input);
198
199   if (error)
200     {
201       clib_error_report (error);
202       exit (1);
203     }
204   exit (0);
205 }
206