From da3a763e977a84edf43c5ca101deae768ae8480d Mon Sep 17 00:00:00 2001 From: waltje Date: Fri, 26 May 2023 20:16:01 -0400 Subject: [PATCH] Small patch to allow for the TCC.EXE (and TCCLIB.DLL) to reside in a (directory under) a "bin/" directory at the same level where the "include/" and "lib/" directories are. This allows one to set the PATH to the (proper) binaries directory and TCC will then "find" itself there. Tested under Windows 7, Windows 8.1 (ARM) and Windows 10+11, but should also be workable for Linux and macOS. --- libtcc.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/libtcc.c b/libtcc.c index 66c2d633..8fc1d1bd 100644 --- a/libtcc.c +++ b/libtcc.c @@ -99,12 +99,50 @@ BOOL WINAPI DllMain (HINSTANCE hDll, DWORD dwReason, LPVOID lpReserved) /* on win32, we suppose the lib and includes are at the location of 'tcc.exe' */ static inline char *config_tccdir_w32(char *path) { + char temp[1024]; + char try[1024]; char *p; + int c; GetModuleFileName(tcc_module, path, MAX_PATH); p = tcc_basename(normalize_slashes(strlwr(path))); if (p > path) --p; + *p = 0; + + /* + * See if we are perhaps in a "bin/" subfolder of the + * installation path, in which case the real root of + * the installation is one level up. We can test this + * by looking for the 'include' folder. + */ + strncpy(temp, path, sizeof(temp)-1); + strcat(temp, "/include"); + + if (_access(temp, 0) != 0) { + /* No 'include' folder found, so go up one level. */ + strncpy(temp, path, sizeof(temp)-1); + + for (c = 0; c < 4; c++) { + p = tcc_basename(temp); + if (p > temp) { + --p; + *p = '\0'; + } + + strncpy(try, temp, sizeof(try)-1); + strcat(try, "/include"); + + if (_access(try, 0) == 0) { + if (p != NULL) + p = '\0'; + path = tcc_malloc(strlen(temp)+1); + strcpy(path, temp); + break; + } + } + } + return path; } #define CONFIG_TCCDIR config_tccdir_w32(alloca(MAX_PATH))