small improvements in XSCHEM_SHAREDIR path building ( Check installed location (/path/to/bin/../share/xschem -> /path/to/share/xschem))

This commit is contained in:
stefan schippers 2025-07-28 00:04:38 +02:00
parent 957dba4c30
commit 9e22068651
1 changed files with 25 additions and 18 deletions

View File

@ -2382,32 +2382,32 @@ int Tcl_AppInit(Tcl_Interp *inter)
Tcl_CreateExitHandler(tclexit, 0);
#ifdef __unix__
/* ROBUST XSCHEM_SHAREDIR DETECTION */
/* Priority 1: XSCHEM_SHAREDIR environment variable */
if ((xschem_sharedir=getenv("XSCHEM_SHAREDIR")) != NULL && !stat(xschem_sharedir, &buf)) {
if((xschem_sharedir=getenv("XSCHEM_SHAREDIR")) != NULL && !stat(xschem_sharedir, &buf)) {
tclsetvar("XSCHEM_SHAREDIR", xschem_sharedir);
found_sharedir = 1;
dbg(1, "Using environment XSCHEM_SHAREDIR = %s\n", xschem_sharedir);
}
/* Priority 2: Executable-relative detection using argv[0] */
if (!found_sharedir && xschem_executable) {
if(!found_sharedir && xschem_executable) {
/* Use argv[0] for C89 compatibility - more portable than /proc/self/exe */
if (strchr(xschem_executable, '/')) {
if(strchr(xschem_executable, '/')) {
/* argv[0] contains a path */
char *last_slash;
my_strncpy(exe_path, xschem_executable, sizeof(exe_path));
last_slash = strrchr(exe_path, '/');
if (last_slash) {
if(last_slash) {
*last_slash = '\0'; /* exe_path now contains directory */
/* Convert relative path to absolute if needed */
if (exe_path[0] == '.' && exe_path[1] == '/') { /* './path/to/xschem' was called */
if(exe_path[0] == '.' && exe_path[1] == '/') { /* './path/to/xschem' was called */
char abs_path[PATH_MAX];
my_snprintf(abs_path, sizeof(abs_path), "%s%s", pwd_dir, exe_path + 1);
my_strncpy(exe_path, abs_path, sizeof(exe_path));
}
else if (exe_path[0] != '/') { /* 'path/to/xschem' was called */
else if(exe_path[0] != '/') { /* 'path/to/xschem' was called */
char abs_path[PATH_MAX];
my_snprintf(abs_path, sizeof(abs_path), "%s/%s", pwd_dir, exe_path);
my_strncpy(exe_path, abs_path, sizeof(exe_path));
@ -2415,9 +2415,9 @@ int Tcl_AppInit(Tcl_Interp *inter)
/* Check if running from source directory */
my_snprintf(tmp, S(tmp), "%s/xschem.tcl", exe_path);
if (!stat(tmp, &buf)) {
if(!stat(tmp, &buf)) {
my_snprintf(tmp, S(tmp), "%s/systemlib", exe_path);
if (!stat(tmp, &buf)) {
if(!stat(tmp, &buf)) {
tclsetvar("XSCHEM_SHAREDIR", exe_path);
found_sharedir = 1;
dbg(1, "Detected source XSCHEM_SHAREDIR = %s\n", exe_path);
@ -2425,13 +2425,20 @@ int Tcl_AppInit(Tcl_Interp *inter)
}
/* Check installed location (bin/../share/xschem) */
if (!found_sharedir) {
my_snprintf(tmp, S(tmp), "%s/../share/xschem/xschem.tcl", exe_path);
if (!stat(tmp, &buf)) {
my_snprintf(tmp, S(tmp), "%s/../share/xschem", exe_path);
tclsetvar("XSCHEM_SHAREDIR", tmp);
found_sharedir = 1;
dbg(1, "Detected installed XSCHEM_SHAREDIR = %s\n", tmp);
if(!found_sharedir) {
last_slash = strrchr(exe_path, '/');
if(last_slash) {
*last_slash = '\0'; /* go up one more level (path/to/bin -> path/to) */
}
my_snprintf(tmp, S(tmp), "%s/share/xschem/xschem.tcl", exe_path);
if(!stat(tmp, &buf)) {
my_snprintf(tmp, S(tmp), "%s/share/xschem/systemlib", exe_path);
if(!stat(tmp, &buf)) {
my_snprintf(tmp, S(tmp), "%s/share/xschem", exe_path);
tclsetvar("XSCHEM_SHAREDIR", tmp);
found_sharedir = 1;
dbg(1, "Detected installed XSCHEM_SHAREDIR = %s\n", tmp);
}
}
}
}
@ -2439,14 +2446,14 @@ int Tcl_AppInit(Tcl_Interp *inter)
}
/* Priority 3: Legacy current directory check */
if (!found_sharedir && !stat("./xschem.tcl", &buf) && !stat("./systemlib", &buf) && !stat("./xschem", &buf)) {
if(!found_sharedir && !stat("./xschem.tcl", &buf) && !stat("./systemlib", &buf) && !stat("./xschem", &buf)) {
tclsetvar("XSCHEM_SHAREDIR",pwd_dir);
found_sharedir = 1;
dbg(1, "Using current directory XSCHEM_SHAREDIR = %s\n", pwd_dir);
}
/* Priority 4: Compile-time fallback */
if (!found_sharedir) {
if(!found_sharedir) {
tclsetvar("XSCHEM_SHAREDIR", XSCHEM_SHAREDIR);
dbg(1, "Using compile-time XSCHEM_SHAREDIR = %s\n", XSCHEM_SHAREDIR);
}