diff options
Diffstat (limited to 'win/nmakehlp.c')
| -rw-r--r-- | win/nmakehlp.c | 238 |
1 files changed, 59 insertions, 179 deletions
diff --git a/win/nmakehlp.c b/win/nmakehlp.c index b0799f8..84cf75c 100644 --- a/win/nmakehlp.c +++ b/win/nmakehlp.c @@ -4,8 +4,8 @@ * * This is used to fix limitations within nmake and the environment. * - * Copyright (c) 2002 David Gravereaux. - * Copyright (c) 2006 Pat Thoyts + * Copyright (c) 2002 by David Gravereaux. + * Copyright (c) 2006 by Pat Thoyts * * See the file "license.terms" for information on usage and redistribution of * this file, and for a DISCLAIMER OF ALL WARRANTIES. @@ -14,11 +14,15 @@ #define _CRT_SECURE_NO_DEPRECATE #include <windows.h> -#ifdef _MSC_VER +#define NO_SHLWAPI_GDI +#define NO_SHLWAPI_STREAM +#define NO_SHLWAPI_REG +#include <shlwapi.h> #pragma comment (lib, "user32.lib") #pragma comment (lib, "kernel32.lib") -#endif +#pragma comment (lib, "shlwapi.lib") #include <stdio.h> +#include <math.h> /* * This library is required for x64 builds with _some_ versions of MSVC @@ -30,19 +34,19 @@ #endif /* ISO hack for dumb VC++ */ -#if defined(_WIN32) && defined(_MSC_VER) && _MSC_VER < 1900 +#ifdef _MSC_VER #define snprintf _snprintf #endif + /* protos */ static int CheckForCompilerFeature(const char *option); -static int CheckForLinkerFeature(char **options, int count); +static int CheckForLinkerFeature(const char *option); static int IsIn(const char *string, const char *substring); static int SubstituteFile(const char *substs, const char *filename); static int QualifyPath(const char *path); -static int LocateDependency(const char *keyfile); static const char *GetVersionFromFile(const char *filename, const char *match, int numdots); static DWORD WINAPI ReadFromPipe(LPVOID args); @@ -55,8 +59,8 @@ typedef struct { char buffer[STATICBUFFERSIZE]; } pipeinfo; -pipeinfo Out = {INVALID_HANDLE_VALUE, ""}; -pipeinfo Err = {INVALID_HANDLE_VALUE, ""}; +pipeinfo Out = {INVALID_HANDLE_VALUE, '\0'}; +pipeinfo Err = {INVALID_HANDLE_VALUE, '\0'}; /* * exitcodes: 0 == no, 1 == yes, 2 == error @@ -70,7 +74,6 @@ main( char msg[300]; DWORD dwWritten; int chars; - const char *s; /* * Make sure children (cl.exe and link.exe) are kept quiet. @@ -99,16 +102,16 @@ main( } return CheckForCompilerFeature(argv[2]); case 'l': - if (argc < 3) { + if (argc != 3) { chars = snprintf(msg, sizeof(msg) - 1, - "usage: %s -l <linker option> ?<mandatory option> ...?\n" + "usage: %s -l <linker option>\n" "Tests for whether link.exe supports an option\n" "exitcodes: 0 == no, 1 == yes, 2 == error\n", argv[0]); WriteFile(GetStdHandle(STD_ERROR_HANDLE), msg, chars, &dwWritten, NULL); return 2; } - return CheckForLinkerFeature(&argv[2], argc-2); + return CheckForLinkerFeature(argv[2]); case 'f': if (argc == 2) { chars = snprintf(msg, sizeof(msg) - 1, @@ -150,13 +153,8 @@ main( &dwWritten, NULL); return 0; } - s = GetVersionFromFile(argv[2], argv[3], *(argv[1]+2) - '0'); - if (s && *s) { - printf("%s\n", s); - return 0; - } else - return 1; /* Version not found. Return non-0 exit code */ - + printf("%s\n", GetVersionFromFile(argv[2], argv[3], *(argv[1]+2) - '0')); + return 0; case 'Q': if (argc != 3) { chars = snprintf(msg, sizeof(msg) - 1, @@ -168,18 +166,6 @@ main( return 2; } return QualifyPath(argv[2]); - - case 'L': - if (argc != 3) { - chars = snprintf(msg, sizeof(msg) - 1, - "usage: %s -L keypath\n" - "Emit the fully qualified path of directory containing keypath\n" - "exitcodes: 0 == success, 1 == not found, 2 == error\n", argv[0]); - WriteFile(GetStdHandle(STD_ERROR_HANDLE), msg, chars, - &dwWritten, NULL); - return 2; - } - return LocateDependency(argv[2]); } } chars = snprintf(msg, sizeof(msg) - 1, @@ -206,25 +192,25 @@ CheckForCompilerFeature( hProcess = GetCurrentProcess(); - memset(&pi, 0, sizeof(PROCESS_INFORMATION)); - memset(&si, 0, sizeof(STARTUPINFO)); + ZeroMemory(&pi, sizeof(PROCESS_INFORMATION)); + ZeroMemory(&si, sizeof(STARTUPINFO)); si.cb = sizeof(STARTUPINFO); si.dwFlags = STARTF_USESTDHANDLES; si.hStdInput = INVALID_HANDLE_VALUE; - memset(&sa, 0, sizeof(SECURITY_ATTRIBUTES)); + ZeroMemory(&sa, sizeof(SECURITY_ATTRIBUTES)); sa.nLength = sizeof(SECURITY_ATTRIBUTES); sa.lpSecurityDescriptor = NULL; sa.bInheritHandle = FALSE; /* - * Create a non-inheritable pipe. + * Create a non-inheritible pipe. */ CreatePipe(&Out.pipe, &h, &sa, 0); /* - * Dupe the write side, make it inheritable, and close the original. + * Dupe the write side, make it inheritible, and close the original. */ DuplicateHandle(hProcess, h, hProcess, &si.hStdOutput, 0, TRUE, @@ -271,10 +257,10 @@ CheckForCompilerFeature( if (!ok) { DWORD err = GetLastError(); int chars = snprintf(msg, sizeof(msg) - 1, - "Tried to launch: \"%s\", but got error [%lu]: ", cmdline, err); + "Tried to launch: \"%s\", but got error [%u]: ", cmdline, err); FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS| - FORMAT_MESSAGE_MAX_WIDTH_MASK, 0L, err, 0, (LPSTR)&msg[chars], + FORMAT_MESSAGE_MAX_WIDTH_MASK, 0L, err, 0, (LPVOID)&msg[chars], (300-chars), 0); WriteFile(GetStdHandle(STD_ERROR_HANDLE), msg, lstrlen(msg), &err,NULL); return 2; @@ -327,8 +313,7 @@ CheckForCompilerFeature( static int CheckForLinkerFeature( - char **options, - int count) + const char *option) { STARTUPINFO si; PROCESS_INFORMATION pi; @@ -337,18 +322,17 @@ CheckForLinkerFeature( char msg[300]; BOOL ok; HANDLE hProcess, h, pipeThreads[2]; - int i; - char cmdline[255]; + char cmdline[100]; hProcess = GetCurrentProcess(); - memset(&pi, 0, sizeof(PROCESS_INFORMATION)); - memset(&si, 0, sizeof(STARTUPINFO)); + ZeroMemory(&pi, sizeof(PROCESS_INFORMATION)); + ZeroMemory(&si, sizeof(STARTUPINFO)); si.cb = sizeof(STARTUPINFO); si.dwFlags = STARTF_USESTDHANDLES; si.hStdInput = INVALID_HANDLE_VALUE; - memset(&sa, 0, sizeof(SECURITY_ATTRIBUTES)); + ZeroMemory(&sa, sizeof(SECURITY_ATTRIBUTES)); sa.nLength = sizeof(SECURITY_ATTRIBUTES); sa.lpSecurityDescriptor = NULL; sa.bInheritHandle = TRUE; @@ -360,7 +344,7 @@ CheckForLinkerFeature( CreatePipe(&Out.pipe, &h, &sa, 0); /* - * Dupe the write side, make it inheritable, and close the original. + * Dupe the write side, make it inheritible, and close the original. */ DuplicateHandle(hProcess, h, hProcess, &si.hStdOutput, 0, TRUE, @@ -384,11 +368,7 @@ CheckForLinkerFeature( * Append our option for testing. */ - for (i = 0; i < count; i++) { - lstrcat(cmdline, " \""); - lstrcat(cmdline, options[i]); - lstrcat(cmdline, "\""); - } + lstrcat(cmdline, option); ok = CreateProcess( NULL, /* Module name. */ @@ -405,10 +385,10 @@ CheckForLinkerFeature( if (!ok) { DWORD err = GetLastError(); int chars = snprintf(msg, sizeof(msg) - 1, - "Tried to launch: \"%s\", but got error [%lu]: ", cmdline, err); + "Tried to launch: \"%s\", but got error [%u]: ", cmdline, err); FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS| - FORMAT_MESSAGE_MAX_WIDTH_MASK, 0L, err, 0, (LPSTR)&msg[chars], + FORMAT_MESSAGE_MAX_WIDTH_MASK, 0L, err, 0, (LPVOID)&msg[chars], (300-chars), 0); WriteFile(GetStdHandle(STD_ERROR_HANDLE), msg, lstrlen(msg), &err,NULL); return 2; @@ -453,9 +433,7 @@ CheckForLinkerFeature( return !(strstr(Out.buffer, "LNK1117") != NULL || strstr(Err.buffer, "LNK1117") != NULL || strstr(Out.buffer, "LNK4044") != NULL || - strstr(Err.buffer, "LNK4044") != NULL || - strstr(Out.buffer, "LNK4224") != NULL || - strstr(Err.buffer, "LNK4224") != NULL); + strstr(Err.buffer, "LNK4044") != NULL); } static DWORD WINAPI @@ -504,6 +482,7 @@ GetVersionFromFile( const char *match, int numdots) { + size_t cbBuffer = 100; static char szBuffer[100]; char *szResult = NULL; FILE *fp = fopen(filename, "rt"); @@ -513,7 +492,7 @@ GetVersionFromFile( * Read data until we see our match string. */ - while (fgets(szBuffer, sizeof(szBuffer), fp) != NULL) { + while (fgets(szBuffer, cbBuffer, fp) != NULL) { LPSTR p, q; p = strstr(szBuffer, match); @@ -523,7 +502,7 @@ GetVersionFromFile( */ p += strlen(match); - while (*p && !isdigit((unsigned char)*p)) { + while (*p && !isdigit(*p)) { ++p; } @@ -532,13 +511,14 @@ GetVersionFromFile( */ q = p; - while (*q && (strchr("0123456789.ab", *q)) && (((!strchr(".ab", *q) - && !strchr("ab", q[-1])) || --numdots))) { + while (*q && (strchr("0123456789.ab", *q)) && ((!strchr(".ab", *q) + && (!strchr("ab", q[-1])) || --numdots))) { ++q; } - *q = 0; - szResult = p; + memcpy(szBuffer, p, q - p); + szBuffer[q-p] = 0; + szResult = szBuffer; break; } } @@ -561,7 +541,7 @@ typedef struct list_item_t { static list_item_t * list_insert(list_item_t **listPtrPtr, const char *key, const char *value) { - list_item_t *itemPtr = (list_item_t *)malloc(sizeof(list_item_t)); + list_item_t *itemPtr = malloc(sizeof(list_item_t)); if (itemPtr) { itemPtr->key = strdup(key); itemPtr->value = strdup(value); @@ -592,7 +572,7 @@ list_free(list_item_t **listPtrPtr) * SubstituteFile -- * As windows doesn't provide anything useful like sed and it's unreliable * to use the tclsh you are building against (consider x-platform builds - - * e.g. compiling AMD64 target from IX86) we provide a simple substitution + * eg compiling AMD64 target from IX86) we provide a simple substitution * option here to handle autoconf style substitutions. * The substitution file is whitespace and line delimited. The file should * consist of lines matching the regular expression: @@ -610,7 +590,9 @@ SubstituteFile( const char *substitutions, const char *filename) { + size_t cbBuffer = 1024; static char szBuffer[1024], szCopy[1024]; + char *szResult = NULL; list_item_t *substPtr = NULL; FILE *fp, *sp; @@ -618,12 +600,12 @@ SubstituteFile( if (fp != NULL) { /* - * Build a list of substitutions from the first filename + * Build a list of substutitions from the first filename */ sp = fopen(substitutions, "rt"); if (sp != NULL) { - while (fgets(szBuffer, sizeof(szBuffer), sp) != NULL) { + while (fgets(szBuffer, cbBuffer, sp) != NULL) { unsigned char *ks, *ke, *vs, *ve; ks = (unsigned char*)szBuffer; while (ks && *ks && isspace(*ks)) ++ks; @@ -640,7 +622,7 @@ SubstituteFile( } /* debug: dump the list */ -#ifndef NDEBUG +#ifdef _DEBUG { int n = 0; list_item_t *p = NULL; @@ -654,7 +636,7 @@ SubstituteFile( * Run the substitutions over each line of the input */ - while (fgets(szBuffer, sizeof(szBuffer), fp) != NULL) { + while (fgets(szBuffer, cbBuffer, fp) != NULL) { list_item_t *p = NULL; for (p = substPtr; p != NULL; p = p->nextPtr) { char *m = strstr(szBuffer, p->key); @@ -671,7 +653,7 @@ SubstituteFile( memcpy(szBuffer, szCopy, sizeof(szCopy)); } } - printf("%s", szBuffer); + printf(szBuffer); } list_free(&substPtr); @@ -680,17 +662,6 @@ SubstituteFile( return 0; } -BOOL FileExists(LPCTSTR szPath) -{ -#ifndef INVALID_FILE_ATTRIBUTES - #define INVALID_FILE_ATTRIBUTES ((DWORD)-1) -#endif - DWORD pathAttr = GetFileAttributes(szPath); - return (pathAttr != INVALID_FILE_ATTRIBUTES && - !(pathAttr & FILE_ATTRIBUTE_DIRECTORY)); -} - - /* * QualifyPath -- * @@ -704,109 +675,18 @@ QualifyPath( const char *szPath) { char szCwd[MAX_PATH + 1]; - - GetFullPathName(szPath, sizeof(szCwd)-1, szCwd, NULL); + char szTmp[MAX_PATH + 1]; + char *p; + GetCurrentDirectory(MAX_PATH, szCwd); + while ((p = strchr(szPath, '/')) && *p) + *p = '\\'; + PathCombine(szTmp, szCwd, szPath); + PathCanonicalize(szCwd, szTmp); printf("%s\n", szCwd); return 0; } /* - * Implements LocateDependency for a single directory. See that command - * for an explanation. - * Returns 0 if found after printing the directory. - * Returns 1 if not found but no errors. - * Returns 2 on any kind of error - * Basically, these are used as exit codes for the process. - */ -static int LocateDependencyHelper(const char *dir, const char *keypath) -{ - HANDLE hSearch; - char path[MAX_PATH+1]; - size_t dirlen; - int keylen, ret; - WIN32_FIND_DATA finfo; - - if (dir == NULL || keypath == NULL) { - return 2; /* Have no real error reporting mechanism into nmake */ - } - dirlen = strlen(dir); - if (dirlen > sizeof(path) - 3) { - return 2; - } - strncpy(path, dir, dirlen); - strncpy(path+dirlen, "\\*", 3); /* Including terminating \0 */ - keylen = strlen(keypath); - -#if 0 /* This function is not available in Visual C++ 6 */ - /* - * Use numerics 0 -> FindExInfoStandard, - * 1 -> FindExSearchLimitToDirectories, - * as these are not defined in Visual C++ 6 - */ - hSearch = FindFirstFileEx(path, 0, &finfo, 1, NULL, 0); -#else - hSearch = FindFirstFile(path, &finfo); -#endif - if (hSearch == INVALID_HANDLE_VALUE) - return 1; /* Not found */ - - /* Loop through all subdirs checking if the keypath is under there */ - ret = 1; /* Assume not found */ - do { - int sublen; - /* - * We need to check it is a directory despite the - * FindExSearchLimitToDirectories in the above call. See SDK docs - */ - if ((finfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) - continue; - sublen = strlen(finfo.cFileName); - if ((dirlen+1+sublen+1+keylen+1) > sizeof(path)) - continue; /* Path does not fit, assume not matched */ - strncpy(path+dirlen+1, finfo.cFileName, sublen); - path[dirlen+1+sublen] = '\\'; - strncpy(path+dirlen+1+sublen+1, keypath, keylen+1); - if (FileExists(path)) { - /* Found a match, print to stdout */ - path[dirlen+1+sublen] = '\0'; - QualifyPath(path); - ret = 0; - break; - } - } while (FindNextFile(hSearch, &finfo)); - FindClose(hSearch); - return ret; -} - -/* - * LocateDependency -- - * - * Locates a dependency for a package. - * keypath - a relative path within the package directory - * that is used to confirm it is the correct directory. - * The search path for the package directory is currently only - * the parent and grandparent of the current working directory. - * If found, the command prints - * name_DIRPATH=<full path of located directory> - * and returns 0. If not found, does not print anything and returns 1. - */ -static int LocateDependency(const char *keypath) -{ - size_t i; - int ret; - static const char *paths[] = {"..", "..\\..", "..\\..\\.."}; - - for (i = 0; i < (sizeof(paths)/sizeof(paths[0])); ++i) { - ret = LocateDependencyHelper(paths[i], keypath); - if (ret == 0) { - return ret; - } - } - return ret; -} - - -/* * Local variables: * mode: c * c-basic-offset: 4 |
