1 /*
2 * ----------------------------------------------------------------------------
3 * nmakehlp.c --
4 *
5 * This is used to fix limitations within nmake and the environment.
6 *
7 * Copyright (c) 2002 by David Gravereaux.
8 * Copyright (c) 2006 by Pat Thoyts
9 *
10 * See the file "license.terms" for information on usage and redistribution of
11 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
12 * ----------------------------------------------------------------------------
13 */
14
15 #define _CRT_SECURE_NO_DEPRECATE
16 #include <windows.h>
17 #define NO_SHLWAPI_GDI
18 #define NO_SHLWAPI_STREAM
19 #define NO_SHLWAPI_REG
20 #include <shlwapi.h>
21 #pragma comment (lib, "user32.lib")
22 #pragma comment (lib, "kernel32.lib")
23 #pragma comment (lib, "shlwapi.lib")
24 #include <stdio.h>
25 #include <math.h>
26
27 /*
28 * This library is required for x64 builds with _some_ versions of MSVC
29 */
30 #if defined(_M_IA64) || defined(_M_AMD64)
31 #if _MSC_VER >= 1400 && _MSC_VER < 1500
32 #pragma comment(lib, "bufferoverflowU")
33 #endif
34 #endif
35
36 /* ISO hack for dumb VC++ */
37 #ifdef _MSC_VER
38 #define snprintf _snprintf
39 #endif
40
41
42
43 /* protos */
44
45 static int CheckForCompilerFeature(const char *option);
46 static int CheckForLinkerFeature(const char *option);
47 static int IsIn(const char *string, const char *substring);
48 static int SubstituteFile(const char *substs, const char *filename);
49 static int QualifyPath(const char *path);
50 static const char *GetVersionFromFile(const char *filename, const char *match, int numdots);
51 static DWORD WINAPI ReadFromPipe(LPVOID args);
52
53 /* globals */
54
55 #define CHUNK 25
56 #define STATICBUFFERSIZE 1000
57 typedef struct {
58 HANDLE pipe;
59 char buffer[STATICBUFFERSIZE];
60 } pipeinfo;
61
62 pipeinfo Out = {INVALID_HANDLE_VALUE, '\0'};
63 pipeinfo Err = {INVALID_HANDLE_VALUE, '\0'};
64
65 /*
66 * exitcodes: 0 == no, 1 == yes, 2 == error
67 */
68
69 int
main(int argc,char * argv[])70 main(
71 int argc,
72 char *argv[])
73 {
74 char msg[300];
75 DWORD dwWritten;
76 int chars;
77
78 /*
79 * Make sure children (cl.exe and link.exe) are kept quiet.
80 */
81
82 SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
83
84 /*
85 * Make sure the compiler and linker aren't effected by the outside world.
86 */
87
88 SetEnvironmentVariable("CL", "");
89 SetEnvironmentVariable("LINK", "");
90
91 if (argc > 1 && *argv[1] == '-') {
92 switch (*(argv[1]+1)) {
93 case 'c':
94 if (argc != 3) {
95 chars = snprintf(msg, sizeof(msg) - 1,
96 "usage: %s -c <compiler option>\n"
97 "Tests for whether cl.exe supports an option\n"
98 "exitcodes: 0 == no, 1 == yes, 2 == error\n", argv[0]);
99 WriteFile(GetStdHandle(STD_ERROR_HANDLE), msg, chars,
100 &dwWritten, NULL);
101 return 2;
102 }
103 return CheckForCompilerFeature(argv[2]);
104 case 'l':
105 if (argc != 3) {
106 chars = snprintf(msg, sizeof(msg) - 1,
107 "usage: %s -l <linker option>\n"
108 "Tests for whether link.exe supports an option\n"
109 "exitcodes: 0 == no, 1 == yes, 2 == error\n", argv[0]);
110 WriteFile(GetStdHandle(STD_ERROR_HANDLE), msg, chars,
111 &dwWritten, NULL);
112 return 2;
113 }
114 return CheckForLinkerFeature(argv[2]);
115 case 'f':
116 if (argc == 2) {
117 chars = snprintf(msg, sizeof(msg) - 1,
118 "usage: %s -f <string> <substring>\n"
119 "Find a substring within another\n"
120 "exitcodes: 0 == no, 1 == yes, 2 == error\n", argv[0]);
121 WriteFile(GetStdHandle(STD_ERROR_HANDLE), msg, chars,
122 &dwWritten, NULL);
123 return 2;
124 } else if (argc == 3) {
125 /*
126 * If the string is blank, there is no match.
127 */
128
129 return 0;
130 } else {
131 return IsIn(argv[2], argv[3]);
132 }
133 case 's':
134 if (argc == 2) {
135 chars = snprintf(msg, sizeof(msg) - 1,
136 "usage: %s -s <substitutions file> <file>\n"
137 "Perform a set of string map type substutitions on a file\n"
138 "exitcodes: 0\n",
139 argv[0]);
140 WriteFile(GetStdHandle(STD_ERROR_HANDLE), msg, chars,
141 &dwWritten, NULL);
142 return 2;
143 }
144 return SubstituteFile(argv[2], argv[3]);
145 case 'V':
146 if (argc != 4) {
147 chars = snprintf(msg, sizeof(msg) - 1,
148 "usage: %s -V filename matchstring\n"
149 "Extract a version from a file:\n"
150 "eg: pkgIndex.tcl \"package ifneeded http\"",
151 argv[0]);
152 WriteFile(GetStdHandle(STD_ERROR_HANDLE), msg, chars,
153 &dwWritten, NULL);
154 return 0;
155 }
156 printf("%s\n", GetVersionFromFile(argv[2], argv[3], *(argv[1]+2) - '0'));
157 return 0;
158 case 'Q':
159 if (argc != 3) {
160 chars = snprintf(msg, sizeof(msg) - 1,
161 "usage: %s -Q path\n"
162 "Emit the fully qualified path\n"
163 "exitcodes: 0 == no, 1 == yes, 2 == error\n", argv[0]);
164 WriteFile(GetStdHandle(STD_ERROR_HANDLE), msg, chars,
165 &dwWritten, NULL);
166 return 2;
167 }
168 return QualifyPath(argv[2]);
169 }
170 }
171 chars = snprintf(msg, sizeof(msg) - 1,
172 "usage: %s -c|-f|-l|-Q|-s|-V ...\n"
173 "This is a little helper app to equalize shell differences between WinNT and\n"
174 "Win9x and get nmake.exe to accomplish its job.\n",
175 argv[0]);
176 WriteFile(GetStdHandle(STD_ERROR_HANDLE), msg, chars, &dwWritten, NULL);
177 return 2;
178 }
179
180 static int
CheckForCompilerFeature(const char * option)181 CheckForCompilerFeature(
182 const char *option)
183 {
184 STARTUPINFO si;
185 PROCESS_INFORMATION pi;
186 SECURITY_ATTRIBUTES sa;
187 DWORD threadID;
188 char msg[300];
189 BOOL ok;
190 HANDLE hProcess, h, pipeThreads[2];
191 char cmdline[100];
192
193 hProcess = GetCurrentProcess();
194
195 ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
196 ZeroMemory(&si, sizeof(STARTUPINFO));
197 si.cb = sizeof(STARTUPINFO);
198 si.dwFlags = STARTF_USESTDHANDLES;
199 si.hStdInput = INVALID_HANDLE_VALUE;
200
201 ZeroMemory(&sa, sizeof(SECURITY_ATTRIBUTES));
202 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
203 sa.lpSecurityDescriptor = NULL;
204 sa.bInheritHandle = FALSE;
205
206 /*
207 * Create a non-inheritible pipe.
208 */
209
210 CreatePipe(&Out.pipe, &h, &sa, 0);
211
212 /*
213 * Dupe the write side, make it inheritible, and close the original.
214 */
215
216 DuplicateHandle(hProcess, h, hProcess, &si.hStdOutput, 0, TRUE,
217 DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE);
218
219 /*
220 * Same as above, but for the error side.
221 */
222
223 CreatePipe(&Err.pipe, &h, &sa, 0);
224 DuplicateHandle(hProcess, h, hProcess, &si.hStdError, 0, TRUE,
225 DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE);
226
227 /*
228 * Base command line.
229 */
230
231 lstrcpy(cmdline, "cl.exe -nologo -c -TC -Zs -X -Fp.\\_junk.pch ");
232
233 /*
234 * Append our option for testing
235 */
236
237 lstrcat(cmdline, option);
238
239 /*
240 * Filename to compile, which exists, but is nothing and empty.
241 */
242
243 lstrcat(cmdline, " .\\nul");
244
245 ok = CreateProcess(
246 NULL, /* Module name. */
247 cmdline, /* Command line. */
248 NULL, /* Process handle not inheritable. */
249 NULL, /* Thread handle not inheritable. */
250 TRUE, /* yes, inherit handles. */
251 DETACHED_PROCESS, /* No console for you. */
252 NULL, /* Use parent's environment block. */
253 NULL, /* Use parent's starting directory. */
254 &si, /* Pointer to STARTUPINFO structure. */
255 &pi); /* Pointer to PROCESS_INFORMATION structure. */
256
257 if (!ok) {
258 DWORD err = GetLastError();
259 int chars = snprintf(msg, sizeof(msg) - 1,
260 "Tried to launch: \"%s\", but got error [%u]: ", cmdline, err);
261
262 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS|
263 FORMAT_MESSAGE_MAX_WIDTH_MASK, 0L, err, 0, (LPVOID)&msg[chars],
264 (300-chars), 0);
265 WriteFile(GetStdHandle(STD_ERROR_HANDLE), msg, lstrlen(msg), &err,NULL);
266 return 2;
267 }
268
269 /*
270 * Close our references to the write handles that have now been inherited.
271 */
272
273 CloseHandle(si.hStdOutput);
274 CloseHandle(si.hStdError);
275
276 WaitForInputIdle(pi.hProcess, 5000);
277 CloseHandle(pi.hThread);
278
279 /*
280 * Start the pipe reader threads.
281 */
282
283 pipeThreads[0] = CreateThread(NULL, 0, ReadFromPipe, &Out, 0, &threadID);
284 pipeThreads[1] = CreateThread(NULL, 0, ReadFromPipe, &Err, 0, &threadID);
285
286 /*
287 * Block waiting for the process to end.
288 */
289
290 WaitForSingleObject(pi.hProcess, INFINITE);
291 CloseHandle(pi.hProcess);
292
293 /*
294 * Wait for our pipe to get done reading, should it be a little slow.
295 */
296
297 WaitForMultipleObjects(2, pipeThreads, TRUE, 500);
298 CloseHandle(pipeThreads[0]);
299 CloseHandle(pipeThreads[1]);
300
301 /*
302 * Look for the commandline warning code in both streams.
303 * - in MSVC 6 & 7 we get D4002, in MSVC 8 we get D9002.
304 */
305
306 return !(strstr(Out.buffer, "D4002") != NULL
307 || strstr(Err.buffer, "D4002") != NULL
308 || strstr(Out.buffer, "D9002") != NULL
309 || strstr(Err.buffer, "D9002") != NULL
310 || strstr(Out.buffer, "D2021") != NULL
311 || strstr(Err.buffer, "D2021") != NULL);
312 }
313
314 static int
CheckForLinkerFeature(const char * option)315 CheckForLinkerFeature(
316 const char *option)
317 {
318 STARTUPINFO si;
319 PROCESS_INFORMATION pi;
320 SECURITY_ATTRIBUTES sa;
321 DWORD threadID;
322 char msg[300];
323 BOOL ok;
324 HANDLE hProcess, h, pipeThreads[2];
325 char cmdline[100];
326
327 hProcess = GetCurrentProcess();
328
329 ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
330 ZeroMemory(&si, sizeof(STARTUPINFO));
331 si.cb = sizeof(STARTUPINFO);
332 si.dwFlags = STARTF_USESTDHANDLES;
333 si.hStdInput = INVALID_HANDLE_VALUE;
334
335 ZeroMemory(&sa, sizeof(SECURITY_ATTRIBUTES));
336 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
337 sa.lpSecurityDescriptor = NULL;
338 sa.bInheritHandle = TRUE;
339
340 /*
341 * Create a non-inheritible pipe.
342 */
343
344 CreatePipe(&Out.pipe, &h, &sa, 0);
345
346 /*
347 * Dupe the write side, make it inheritible, and close the original.
348 */
349
350 DuplicateHandle(hProcess, h, hProcess, &si.hStdOutput, 0, TRUE,
351 DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE);
352
353 /*
354 * Same as above, but for the error side.
355 */
356
357 CreatePipe(&Err.pipe, &h, &sa, 0);
358 DuplicateHandle(hProcess, h, hProcess, &si.hStdError, 0, TRUE,
359 DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE);
360
361 /*
362 * Base command line.
363 */
364
365 lstrcpy(cmdline, "link.exe -nologo ");
366
367 /*
368 * Append our option for testing.
369 */
370
371 lstrcat(cmdline, option);
372
373 ok = CreateProcess(
374 NULL, /* Module name. */
375 cmdline, /* Command line. */
376 NULL, /* Process handle not inheritable. */
377 NULL, /* Thread handle not inheritable. */
378 TRUE, /* yes, inherit handles. */
379 DETACHED_PROCESS, /* No console for you. */
380 NULL, /* Use parent's environment block. */
381 NULL, /* Use parent's starting directory. */
382 &si, /* Pointer to STARTUPINFO structure. */
383 &pi); /* Pointer to PROCESS_INFORMATION structure. */
384
385 if (!ok) {
386 DWORD err = GetLastError();
387 int chars = snprintf(msg, sizeof(msg) - 1,
388 "Tried to launch: \"%s\", but got error [%u]: ", cmdline, err);
389
390 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS|
391 FORMAT_MESSAGE_MAX_WIDTH_MASK, 0L, err, 0, (LPVOID)&msg[chars],
392 (300-chars), 0);
393 WriteFile(GetStdHandle(STD_ERROR_HANDLE), msg, lstrlen(msg), &err,NULL);
394 return 2;
395 }
396
397 /*
398 * Close our references to the write handles that have now been inherited.
399 */
400
401 CloseHandle(si.hStdOutput);
402 CloseHandle(si.hStdError);
403
404 WaitForInputIdle(pi.hProcess, 5000);
405 CloseHandle(pi.hThread);
406
407 /*
408 * Start the pipe reader threads.
409 */
410
411 pipeThreads[0] = CreateThread(NULL, 0, ReadFromPipe, &Out, 0, &threadID);
412 pipeThreads[1] = CreateThread(NULL, 0, ReadFromPipe, &Err, 0, &threadID);
413
414 /*
415 * Block waiting for the process to end.
416 */
417
418 WaitForSingleObject(pi.hProcess, INFINITE);
419 CloseHandle(pi.hProcess);
420
421 /*
422 * Wait for our pipe to get done reading, should it be a little slow.
423 */
424
425 WaitForMultipleObjects(2, pipeThreads, TRUE, 500);
426 CloseHandle(pipeThreads[0]);
427 CloseHandle(pipeThreads[1]);
428
429 /*
430 * Look for the commandline warning code in the stderr stream.
431 */
432
433 return !(strstr(Out.buffer, "LNK1117") != NULL ||
434 strstr(Err.buffer, "LNK1117") != NULL ||
435 strstr(Out.buffer, "LNK4044") != NULL ||
436 strstr(Err.buffer, "LNK4044") != NULL);
437 }
438
439 static DWORD WINAPI
ReadFromPipe(LPVOID args)440 ReadFromPipe(
441 LPVOID args)
442 {
443 pipeinfo *pi = (pipeinfo *) args;
444 char *lastBuf = pi->buffer;
445 DWORD dwRead;
446 BOOL ok;
447
448 again:
449 if (lastBuf - pi->buffer + CHUNK > STATICBUFFERSIZE) {
450 CloseHandle(pi->pipe);
451 return (DWORD)-1;
452 }
453 ok = ReadFile(pi->pipe, lastBuf, CHUNK, &dwRead, 0L);
454 if (!ok || dwRead == 0) {
455 CloseHandle(pi->pipe);
456 return 0;
457 }
458 lastBuf += dwRead;
459 goto again;
460
461 return 0; /* makes the compiler happy */
462 }
463
464 static int
IsIn(const char * string,const char * substring)465 IsIn(
466 const char *string,
467 const char *substring)
468 {
469 return (strstr(string, substring) != NULL);
470 }
471
472 /*
473 * GetVersionFromFile --
474 * Looks for a match string in a file and then returns the version
475 * following the match where a version is anything acceptable to
476 * package provide or package ifneeded.
477 */
478
479 static const char *
GetVersionFromFile(const char * filename,const char * match,int numdots)480 GetVersionFromFile(
481 const char *filename,
482 const char *match,
483 int numdots)
484 {
485 size_t cbBuffer = 100;
486 static char szBuffer[100];
487 char *szResult = NULL;
488 FILE *fp = fopen(filename, "rt");
489
490 if (fp != NULL) {
491 /*
492 * Read data until we see our match string.
493 */
494
495 while (fgets(szBuffer, cbBuffer, fp) != NULL) {
496 LPSTR p, q;
497
498 p = strstr(szBuffer, match);
499 if (p != NULL) {
500 /*
501 * Skip to first digit after the match.
502 */
503
504 p += strlen(match);
505 while (*p && !isdigit(*p)) {
506 ++p;
507 }
508
509 /*
510 * Find ending whitespace.
511 */
512
513 q = p;
514 while (*q && (strchr("0123456789.ab", *q)) && ((!strchr(".ab", *q)
515 && (!strchr("ab", q[-1])) || --numdots))) {
516 ++q;
517 }
518
519 memcpy(szBuffer, p, q - p);
520 szBuffer[q-p] = 0;
521 szResult = szBuffer;
522 break;
523 }
524 }
525 fclose(fp);
526 }
527 return szResult;
528 }
529
530 /*
531 * List helpers for the SubstituteFile function
532 */
533
534 typedef struct list_item_t {
535 struct list_item_t *nextPtr;
536 char * key;
537 char * value;
538 } list_item_t;
539
540 /* insert a list item into the list (list may be null) */
541 static list_item_t *
list_insert(list_item_t ** listPtrPtr,const char * key,const char * value)542 list_insert(list_item_t **listPtrPtr, const char *key, const char *value)
543 {
544 list_item_t *itemPtr = malloc(sizeof(list_item_t));
545 if (itemPtr) {
546 itemPtr->key = strdup(key);
547 itemPtr->value = strdup(value);
548 itemPtr->nextPtr = NULL;
549
550 while(*listPtrPtr) {
551 listPtrPtr = &(*listPtrPtr)->nextPtr;
552 }
553 *listPtrPtr = itemPtr;
554 }
555 return itemPtr;
556 }
557
558 static void
list_free(list_item_t ** listPtrPtr)559 list_free(list_item_t **listPtrPtr)
560 {
561 list_item_t *tmpPtr, *listPtr = *listPtrPtr;
562 while (listPtr) {
563 tmpPtr = listPtr;
564 listPtr = listPtr->nextPtr;
565 free(tmpPtr->key);
566 free(tmpPtr->value);
567 free(tmpPtr);
568 }
569 }
570
571 /*
572 * SubstituteFile --
573 * As windows doesn't provide anything useful like sed and it's unreliable
574 * to use the tclsh you are building against (consider x-platform builds -
575 * eg compiling AMD64 target from IX86) we provide a simple substitution
576 * option here to handle autoconf style substitutions.
577 * The substitution file is whitespace and line delimited. The file should
578 * consist of lines matching the regular expression:
579 * \s*\S+\s+\S*$
580 *
581 * Usage is something like:
582 * nmakehlp -S << $** > $@
583 * @PACKAGE_NAME@ $(PACKAGE_NAME)
584 * @PACKAGE_VERSION@ $(PACKAGE_VERSION)
585 * <<
586 */
587
588 static int
SubstituteFile(const char * substitutions,const char * filename)589 SubstituteFile(
590 const char *substitutions,
591 const char *filename)
592 {
593 size_t cbBuffer = 1024;
594 static char szBuffer[1024], szCopy[1024];
595 char *szResult = NULL;
596 list_item_t *substPtr = NULL;
597 FILE *fp, *sp;
598
599 fp = fopen(filename, "rt");
600 if (fp != NULL) {
601
602 /*
603 * Build a list of substutitions from the first filename
604 */
605
606 sp = fopen(substitutions, "rt");
607 if (sp != NULL) {
608 while (fgets(szBuffer, cbBuffer, sp) != NULL) {
609 unsigned char *ks, *ke, *vs, *ve;
610 ks = (unsigned char*)szBuffer;
611 while (ks && *ks && isspace(*ks)) ++ks;
612 ke = ks;
613 while (ke && *ke && !isspace(*ke)) ++ke;
614 vs = ke;
615 while (vs && *vs && isspace(*vs)) ++vs;
616 ve = vs;
617 while (ve && *ve && !(*ve == '\r' || *ve == '\n')) ++ve;
618 *ke = 0, *ve = 0;
619 list_insert(&substPtr, (char*)ks, (char*)vs);
620 }
621 fclose(sp);
622 }
623
624 /* debug: dump the list */
625 #ifdef _DEBUG
626 {
627 int n = 0;
628 list_item_t *p = NULL;
629 for (p = substPtr; p != NULL; p = p->nextPtr, ++n) {
630 fprintf(stderr, "% 3d '%s' => '%s'\n", n, p->key, p->value);
631 }
632 }
633 #endif
634
635 /*
636 * Run the substitutions over each line of the input
637 */
638
639 while (fgets(szBuffer, cbBuffer, fp) != NULL) {
640 list_item_t *p = NULL;
641 for (p = substPtr; p != NULL; p = p->nextPtr) {
642 char *m = strstr(szBuffer, p->key);
643 if (m) {
644 char *cp, *op, *sp;
645 cp = szCopy;
646 op = szBuffer;
647 while (op != m) *cp++ = *op++;
648 sp = p->value;
649 while (sp && *sp) *cp++ = *sp++;
650 op += strlen(p->key);
651 while (*op) *cp++ = *op++;
652 *cp = 0;
653 memcpy(szBuffer, szCopy, sizeof(szCopy));
654 }
655 }
656 printf(szBuffer);
657 }
658
659 list_free(&substPtr);
660 }
661 fclose(fp);
662 return 0;
663 }
664
665 /*
666 * QualifyPath --
667 *
668 * This composes the current working directory with a provided path
669 * and returns the fully qualified and normalized path.
670 * Mostly needed to setup paths for testing.
671 */
672
673 static int
QualifyPath(const char * szPath)674 QualifyPath(
675 const char *szPath)
676 {
677 char szCwd[MAX_PATH + 1];
678 char szTmp[MAX_PATH + 1];
679 char *p;
680 GetCurrentDirectory(MAX_PATH, szCwd);
681 while ((p = strchr(szPath, '/')) && *p)
682 *p = '\\';
683 PathCombine(szTmp, szCwd, szPath);
684 PathCanonicalize(szCwd, szTmp);
685 printf("%s\n", szCwd);
686 return 0;
687 }
688
689 /*
690 * Local variables:
691 * mode: c
692 * c-basic-offset: 4
693 * fill-column: 78
694 * indent-tabs-mode: t
695 * tab-width: 8
696 * End:
697 */
698