KERNEL_VERSION_BUILD_RC added in buildno.h.
[reactos.git] / reactos / tools / buildno.c
1 /* $Id: buildno.c,v 1.5 2004/02/21 15:34:51 ea Exp $
2 *
3 * buildno - Generate the build number for ReactOS
4 *
5 * Copyright (c) 1999,2000 Emanuele Aliberti
6 *
7 * The build number is the day on which the build took
8 * place, as YYYYMMDD
9 *
10 * The build number is stored in the file
11 * <reactos/buildno.h> as a set of macros:
12 *
13 * KERNEL_VERSION_BUILD base 10 number
14 * KERNEL_VERSION_BUILD_STR C string
15 * KERNEL_VERSION_BUILD_RC RC string
16 *
17 * REVISIONS
18 * ---------
19 * 2000-01-22 (ea)
20 * Fixed bugs: tm_year is (current_year - 1900),
21 * tm_month is 0-11 not 1-12 and code ignored TZ.
22 * 2000-12-10 (ea)
23 * Added -p option to make it simply print the
24 * version number, but skip buildno.h generation.
25 */
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <time.h>
29 #include <string.h>
30 #include "../include/reactos/version.h"
31
32 #define FALSE 0
33 #define TRUE 1
34
35 /* File to (over)write */
36 #define BUILDNO_INCLUDE_FILE "../include/reactos/buildno.h"
37
38 static char * argv0 = "";
39
40 #ifdef DBG
41 void
42 tm_dump (const char *tag, struct tm * t)
43 {
44 printf ("%s->tm_sec = %d\n", tag, t->tm_sec);
45 printf ("%s->tm_min = %d\n", tag, t->tm_min);
46 printf ("%s->tm_hour = %d\n", tag, t->tm_hour);
47 printf ("%s->tm_mday = %d\n", tag, t->tm_mday);
48 printf ("%s->tm_mon = %d\n", tag, t->tm_mon);
49 printf ("%s->tm_year = %d\n", tag, t->tm_year);
50 printf ("%s->tm_wday = %d\n", tag, t->tm_wday);
51 printf ("%s->tm_yday = %d\n", tag, t->tm_yday);
52 printf ("%s->tm_isdst = %d\n\n", tag, t->tm_isdst);
53 }
54 #endif
55
56
57 int
58 elapsed_days (
59 time_t t_today,
60 time_t t_release_day
61 )
62 {
63 double seconds = difftime (t_today, t_release_day);
64 double days = seconds / (double) 86400.0;
65 char buf [32];
66 char * dot = buf;
67
68 sprintf (buf, "%f", days );
69
70 while ( *dot && *dot != '.') ++dot;
71 *dot = '\0';
72
73 return atol (buf);
74 }
75
76 void
77 write_h (int build)
78 {
79 FILE *h = NULL;
80 char* s;
81 char* s1;
82 int length;
83
84 s1 = s = malloc(256 * 1024);
85
86 s = s + sprintf (s, "/* Do not edit - Machine generated */\n");
87
88 s = s + sprintf (s, "#ifndef _INC_REACTOS_BUILDNO\n" );
89 s = s + sprintf (s, "#define _INC_REACTOS_BUILDNO\n" );
90
91 s = s + sprintf (s, "#define KERNEL_VERSION_BUILD\t%d\n", build);
92 s = s + sprintf (s, "#define KERNEL_VERSION_BUILD_STR\t\"%d\"\n", build);
93 s = s + sprintf (s, "#define KERNEL_VERSION_BUILD_RC\t\"%d\\0\"\n", build);
94 s = s + sprintf (s, "#define KERNEL_RELEASE_RC\t\"%d.%d",
95 KERNEL_VERSION_MAJOR, KERNEL_VERSION_MINOR);
96 if (0 != KERNEL_VERSION_PATCH_LEVEL)
97 {
98 s = s + sprintf (s, ".%d", KERNEL_VERSION_PATCH_LEVEL);
99 }
100 s = s + sprintf (s, "-%S\\0\"\n", KERNEL_VERSION_BUILD_TYPE);
101 s = s + sprintf (s, "#define KERNEL_RELEASE_STR\t\"%d.%d",
102 KERNEL_VERSION_MAJOR,
103 KERNEL_VERSION_MINOR);
104 if (0 != KERNEL_VERSION_PATCH_LEVEL)
105 {
106 s = s + sprintf (s, ".%d", KERNEL_VERSION_PATCH_LEVEL);
107 }
108 s = s + sprintf (s, "-%S\"\n", KERNEL_VERSION_BUILD_TYPE);
109 s = s + sprintf (s, "#define KERNEL_VERSION_RC\t\"%d.%d",
110 KERNEL_VERSION_MAJOR,
111 KERNEL_VERSION_MINOR);
112 if (0 != KERNEL_VERSION_PATCH_LEVEL)
113 {
114 s = s + sprintf (s, ".%d", KERNEL_VERSION_PATCH_LEVEL);
115 }
116 s = s + sprintf (s, "-%S\\0\"\n", KERNEL_VERSION_BUILD_TYPE);
117 s = s + sprintf (s, "#define KERNEL_VERSION_STR\t\"%d.%d",
118 KERNEL_VERSION_MAJOR,
119 KERNEL_VERSION_MINOR);
120 if (0 != KERNEL_VERSION_PATCH_LEVEL)
121 {
122 s = s + sprintf (s, ".%d", KERNEL_VERSION_PATCH_LEVEL);
123 }
124 s = s + sprintf (s, "-%S\"\n", KERNEL_VERSION_BUILD_TYPE);
125 s = s + sprintf (s, "#endif\n/* EOF */\n");
126
127 h = fopen (BUILDNO_INCLUDE_FILE, "rb");
128 if (h != NULL)
129 {
130 fseek(h, 0, SEEK_END);
131 length = ftell(h);
132 if (length == strlen(s1))
133 {
134 char* orig;
135
136 orig = malloc(length);
137 fseek(h, 0, SEEK_SET);
138 fread(orig, 1, length, h);
139 if (memcmp(s1, orig, length) == 0)
140 {
141 fclose(h);
142 return;
143 }
144 }
145 fclose(h);
146 }
147
148 h = fopen (BUILDNO_INCLUDE_FILE, "wb");
149 if (!h)
150 {
151 fprintf (stderr,
152 "%s: can not create file \"%s\"!\n",
153 argv0,
154 BUILDNO_INCLUDE_FILE);
155 return;
156 }
157 fwrite(s1, 1, strlen(s1), h);
158 fclose (h);
159 }
160
161 void
162 usage (void)
163 {
164 fprintf (
165 stderr,
166 "Usage: %s [-{p|q}]\n\n -p print version number and exit\n -q run in quiet mode\n",
167 argv0
168 );
169 exit (EXIT_SUCCESS);
170 }
171
172
173 int
174 main (int argc, char * argv [])
175 {
176 int print_only = FALSE;
177 int quiet = FALSE;
178
179 int build = 0;
180
181 time_t t1 = 0;
182 struct tm * t1_tm = NULL;
183
184 argv0 = argv[0];
185
186 switch (argc)
187 {
188 case 1:
189 break;
190 case 2:
191 if (argv[1][0] == '-')
192 {
193 if (argv[1][1] == 'q')
194 {
195 quiet = TRUE;
196 }
197 else if (argv[1][1] == 'p')
198 {
199 print_only = TRUE;
200 }
201 else
202 {
203 usage ();
204 }
205 }
206 else
207 {
208 usage ();
209 }
210 break;
211 default:
212 usage ();
213 }
214 /*
215 * Set TZ information.
216 */
217 tzset ();
218 /*
219 * We are building TODAY!
220 */
221 if (FALSE == quiet)
222 {
223 printf ( "\nReactOS Build Number Generator\n\n");
224 }
225
226 time (& t1); /* current build time */
227 t1_tm = gmtime (& t1);
228
229 #ifdef DBG
230 tm_dump ("t1", t1_tm);
231 #endif
232 t1_tm->tm_year += 1900;
233 #ifdef DBG
234 tm_dump ("t1", t1_tm);
235 #endif
236 if (FALSE == quiet)
237 {
238 printf (
239 "Current date: %4d-%02d-%02d\n\n",
240 t1_tm->tm_year,
241 (t1_tm->tm_mon + 1),
242 t1_tm->tm_mday
243 );
244 }
245 /*
246 * Compute build number.
247 */
248 build = t1_tm->tm_year * 10000 + (t1_tm->tm_mon + 1) * 100 + t1_tm->tm_mday;
249
250 if (FALSE == quiet)
251 {
252 printf (
253 "ROS Version : %d.%d",
254 KERNEL_VERSION_MAJOR,
255 KERNEL_VERSION_MINOR
256 );
257 if (0 != KERNEL_VERSION_PATCH_LEVEL)
258 {
259 printf(".%d", KERNEL_VERSION_PATCH_LEVEL);
260 }
261 printf("-%S (Build %d)\n\n", KERNEL_VERSION_BUILD_TYPE, build);
262 }
263 /*
264 * (Over)write the include file, unless
265 * the user switched on -p.
266 */
267 if (FALSE == print_only)
268 {
269 write_h (build);
270 }
271 else
272 {
273 printf ("%s: no code generated", argv [0]);
274 }
275
276 return EXIT_SUCCESS;
277 }
278
279
280 /* EOF */