From 55667b94fb78939e3322991f148d2d5610daba50 Mon Sep 17 00:00:00 2001 From: Oliver Schmidt Date: Mon, 6 May 2013 23:20:56 +0200 Subject: [PATCH] Added search path relative to running binary on Windows. In contrast to *IX it doesn't make much sense to add compile time defined search paths to Windows binaries: There's no standard path like /usr/local/bin (and there are no symbolic links to link from there to another location). On the other hand it's (again in contrast to *IX) easy for Windows binaries to determine their own paths. Therefore it's appropriate to make use of that to add run time defined default search paths. --- src/ca65/incpath.c | 3 +++ src/cc65/incpath.c | 3 +++ src/common/searchpath.c | 42 +++++++++++++++++++++++++++++++++++++++++ src/common/searchpath.h | 6 ++++++ src/ld65/filepath.c | 7 ++++++- 5 files changed, 60 insertions(+), 1 deletion(-) diff --git a/src/ca65/incpath.c b/src/ca65/incpath.c index a5968cb57..88323dc53 100644 --- a/src/ca65/incpath.c +++ b/src/ca65/incpath.c @@ -87,6 +87,9 @@ void FinishIncludePaths (void) #ifdef CA65_INC AddSearchPath (IncSearchPath, STRINGIZE (CA65_INC)); #endif + + /* Add paths relative to the parent directory of the Windows binary. */ + AddSubSearchPathFromWinBin (IncSearchPath, "asminc"); } diff --git a/src/cc65/incpath.c b/src/cc65/incpath.c index 6e07f8f77..5f26eec57 100644 --- a/src/cc65/incpath.c +++ b/src/cc65/incpath.c @@ -88,6 +88,9 @@ void FinishIncludePaths (void) #ifdef CC65_INC AddSearchPath (SysIncSearchPath, STRINGIZE (CC65_INC)); #endif + + /* Add paths relative to the parent directory of the Windows binary. */ + AddSubSearchPathFromWinBin (SysIncSearchPath, "include"); } diff --git a/src/common/searchpath.c b/src/common/searchpath.c index 78d2cce4c..47dc3dfc9 100644 --- a/src/common/searchpath.c +++ b/src/common/searchpath.c @@ -157,6 +157,48 @@ void AddSubSearchPathFromEnv (SearchPath* P, const char* EnvVar, const char* Sub +void AddSubSearchPathFromWinBin (SearchPath* P, const char* SubDir) +{ +/* Windows only: + * Add a search path from the running binary, adding a subdirectory to + * the parent directory of the directory containing the binary. + */ +#if defined(_MSC_VER) + + char Dir[_MAX_PATH]; + char* Ptr; + + if (_get_pgmptr (&Ptr) != 0) { + return; + } + strcpy (Dir, Ptr); + + /* Remove binary name */ + Ptr = strrchr (Dir, '\\'); + if (Ptr == 0) { + return; + } + *Ptr = '\0'; + + /* Check for 'bin' directory */ + Ptr = strrchr (Dir, '\\'); + if (Ptr == 0) { + return; + } + if (strcmp (Ptr++, "\\bin") != 0) { + return; + } + + /* Append SubDir */ + strcpy (Ptr, SubDir); + + /* Add the search path */ + AddSearchPath (P, Dir); + +#endif +} + + int PushSearchPath (SearchPath* P, const char* NewPath) /* Add a new search path to the head of an existing search path list, provided * that it's not already there. If the path is already at the first position, diff --git a/src/common/searchpath.h b/src/common/searchpath.h index f162b06ee..8b7f23e3b 100644 --- a/src/common/searchpath.h +++ b/src/common/searchpath.h @@ -79,6 +79,12 @@ void AddSubSearchPathFromEnv (SearchPath* P, const char* EnvVar, const char* Sub * the environment variable value. */ +void AddSubSearchPathFromWinBin (SearchPath* P, const char* SubDir); +/* Windows only: + * Add a search path from the running binary, adding a subdirectory to + * the parent directory of the directory containing the binary. + */ + int PushSearchPath (SearchPath* P, const char* NewPath); /* Add a new search path to the head of an existing search path list, provided * that it's not already there. If the path is already at the first position, diff --git a/src/ld65/filepath.c b/src/ld65/filepath.c index d112c0733..803efbf2d 100644 --- a/src/ld65/filepath.c +++ b/src/ld65/filepath.c @@ -84,7 +84,7 @@ void InitSearchPaths (void) /* Add paths relative to a main directory defined in an env. var. */ AddSubSearchPathFromEnv (LibDefaultPath, "CC65_HOME", "lib"); - AddSubSearchPathFromEnv (ObjDefaultPath, "CC65_HOME", "obj"); + AddSubSearchPathFromEnv (ObjDefaultPath, "CC65_HOME", "lib"); AddSubSearchPathFromEnv (CfgDefaultPath, "CC65_HOME", "cfg"); /* Add some compiled-in search paths if defined at compile time. */ @@ -97,6 +97,11 @@ void InitSearchPaths (void) #if defined(LD65_CFG) AddSearchPath (CfgDefaultPath, STRINGIZE (LD65_CFG)); #endif + + /* Add paths relative to the parent directory of the Windows binary. */ + AddSubSearchPathFromWinBin (LibDefaultPath, "lib"); + AddSubSearchPathFromWinBin (ObjDefaultPath, "lib"); + AddSubSearchPathFromWinBin (CfgDefaultPath, "cfg"); } -- 2.39.5