From 6093e2935375961a61b584d5df83492c7336d421 Mon Sep 17 00:00:00 2001 From: Keno Fischer Date: Sun, 2 May 2021 22:27:57 -0400 Subject: [PATCH 1/4] Revert "Reverted about half of pull request #41 because it breaks compatibility" This reverts commit c0867ef71e46274b397b253bbcb0d9631d5e2d15. --- tcltk/Makefile | 11 ++++++++--- tcltk/ext2sim.sh.in | 10 ++++++---- tcltk/ext2spice.sh.in | 10 ++++++---- tcltk/magic.sh.in | 20 +++++++++++--------- tcltk/relpath.sh | 40 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 71 insertions(+), 20 deletions(-) create mode 100755 tcltk/relpath.sh diff --git a/tcltk/Makefile b/tcltk/Makefile index 97f214ad..283f8293 100644 --- a/tcltk/Makefile +++ b/tcltk/Makefile @@ -32,6 +32,11 @@ TCL_FILES = \ readspice.tcl \ magic.tcl +TCL_DIR_REL_OR_ABS = $(shell ${MAGICDIR}/tcltk/relpath.sh "$(DESTDIR)${INSTALL_BINDIR}" ${TCLDIR}) +ifeq ($(TCL_DIR_REL_OR_ABS),) +TCL_DIR_REL_OR_ABS = ${TCLDIR} +endif + BIN_FILES = \ $(DESTDIR)${INSTALL_BINDIR}/magic.sh \ $(DESTDIR)${INSTALL_BINDIR}/ext2spice.sh \ @@ -65,16 +70,16 @@ magic.tcl: magic.tcl.in ${MAGICDIR}/defs.mak ${MAGICDIR}/VERSION magic.tcl.in > magic.tcl magic.sh: magic.sh.in ${MAGICDIR}/defs.mak - sed -e /TCL_DIR/s%TCL_DIR%${TCLDIR}%g \ + sed -e /TCL_DIR_REL_OR_ABS/s%TCL_DIR_REL_OR_ABS%${TCL_DIR_REL_OR_ABS}%g \ -e /TCLLIB_DIR/s%TCLLIB_DIR%${TCL_LIB_DIR}%g \ -e /WISH_EXE/s%WISH_EXE%${WISH_EXE}%g magic.sh.in > magic.sh ext2spice.sh: ext2spice.sh.in ${MAGICDIR}/defs.mak - sed -e /TCL_DIR/s%TCL_DIR%${TCLDIR}%g \ + sed -e /TCL_DIR_REL_OR_ABS/s%TCL_DIR_REL_OR_ABS%${TCL_DIR_REL_OR_ABS}%g \ ext2spice.sh.in > ext2spice.sh ext2sim.sh: ext2sim.sh.in ${MAGICDIR}/defs.mak - sed -e /TCL_DIR/s%TCL_DIR%${TCLDIR}%g \ + sed -e /TCL_DIR_REL_OR_ABS/s%TCL_DIR_REL_OR_ABS%${TCL_DIR_REL_OR_ABS}%g \ ext2sim.sh.in > ext2sim.sh $(DESTDIR)${INSTALL_TCLDIR}/%: % diff --git a/tcltk/ext2sim.sh.in b/tcltk/ext2sim.sh.in index d0740098..61d69cf8 100755 --- a/tcltk/ext2sim.sh.in +++ b/tcltk/ext2sim.sh.in @@ -14,12 +14,14 @@ for i in $@; do *) esargs="$esargs $i" ;; esac done -TCL_MAG_DIR=${CAD_ROOT}/magic/tcl -if [ "${TCL_MAG_DIR}" = "/magic/tcl" ]; then - TCL_MAG_DIR=TCL_DIR +TCL_REL_OR_ABS=TCL_DIR_REL_OR_ABS +if [ "${TCL_REL_OR_ABS:0:1}" = "/" ]; then + TCL_DIR=$TCL_REL_OR_ABS +else + TCL_DIR=$(cd $(dirname ${BASH_SOURCE[0]})/$TCL_REL_OR_ABS; pwd -P) fi # -eval ${TCL_MAG_DIR}/magicdnull -dnull -noconsole -nowrapper $mgargs < Date: Sun, 2 May 2021 22:28:56 -0400 Subject: [PATCH 2/4] Account for symlinks --- tcltk/magic.sh.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tcltk/magic.sh.in b/tcltk/magic.sh.in index 745bd7ad..af3228d5 100755 --- a/tcltk/magic.sh.in +++ b/tcltk/magic.sh.in @@ -15,7 +15,7 @@ TCL_REL_OR_ABS=TCL_DIR_REL_OR_ABS if [ "${TCL_REL_OR_ABS:0:1}" = "/" ]; then TCL_DIR=$TCL_REL_OR_ABS else - TCL_DIR=$(cd $(dirname ${BASH_SOURCE[0]})/$TCL_REL_OR_ABS; pwd -P) + TCL_DIR=$(cd $(dirname $(readlink -f ${BASH_SOURCE[0]}))/$TCL_REL_OR_ABS; pwd -P) fi TKCON=true DNULL= From 9a46ac6c62eae882de1710bf9005db2c5a0529f4 Mon Sep 17 00:00:00 2001 From: Keno Fischer Date: Sun, 2 May 2021 23:01:30 -0400 Subject: [PATCH 3/4] Restore relative setting of CAD_ROOT In merging the original pull request, the C side was changed to respect an originally set CAD_ROOT rather than defaulting to CAD_ROOT_DEFAULT. However, this was left, so the code never actually took effect. Rename the variable being set to CAD_ROOT, but make it conditional to respect an external user's override of CAD_ROOT. --- tcltk/magic.tcl.in | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tcltk/magic.tcl.in b/tcltk/magic.tcl.in index a7f0dd8e..59ee06d7 100755 --- a/tcltk/magic.tcl.in +++ b/tcltk/magic.tcl.in @@ -9,7 +9,10 @@ global Opts # the environment variable HOME to its original value. variable MAGIC_TCL_DIR [file dirname [file normalize [info script]]] -variable CAD_ROOT_DEFAULT [file normalize [file join $MAGIC_TCL_DIR ../..]] + +if {![info exists CAD_ROOT]} { + variable CAD_ROOT [file normalize [file join $MAGIC_TCL_DIR ../..]] +} if {${tcl_version} >= 8.6} { load -lazy [file join $MAGIC_TCL_DIR tclmagic[info sharedlibextension]] From 7e68c94744cd36c193536ba8a5cc3a931b640b8b Mon Sep 17 00:00:00 2001 From: Keno Fischer Date: Sun, 2 May 2021 23:03:16 -0400 Subject: [PATCH 4/4] Support relocation of magicdnull and magicexec executables By making the magic.tcl path relative to the path of the executable itself rather than embedding the path during build time. --- tcltk/magicdnull.c | 13 +++++++++++-- tcltk/magicexec.c | 12 +++++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/tcltk/magicdnull.c b/tcltk/magicdnull.c index b75531b2..263f19b6 100644 --- a/tcltk/magicdnull.c +++ b/tcltk/magicdnull.c @@ -28,8 +28,17 @@ magic_AppInit(interp) /* This is where we replace the home ".tclshrc" file with */ /* magic's startup script. */ - - Tcl_SetVar(interp, "tcl_rcFileName", TCL_DIR "/magic.tcl", TCL_GLOBAL_ONLY); + /* Try to find magic.tcl relative to the current executable. + If that fails, try to load it from the path where it was located + at build time. + */ + const char *magic_tcl_path = NULL; + if (Tcl_ExprString(interp, "[file join [file dirname [info nameofexecutable]] magic.tcl]") == TCL_OK) { + magic_tcl_path = Tcl_GetStringResult(interp); + } else { + magic_tcl_path = TCL_DIR "/magic.tcl"; + } + Tcl_SetVar(interp, "tcl_rcFileName", magic_tcl_path, TCL_GLOBAL_ONLY); /* Additional variable can be used to tell if magic is in batch mode */ Tcl_SetVar(interp, "batch_mode", "true", TCL_GLOBAL_ONLY); diff --git a/tcltk/magicexec.c b/tcltk/magicexec.c index 73abbd3a..621dbd2f 100644 --- a/tcltk/magicexec.c +++ b/tcltk/magicexec.c @@ -59,7 +59,17 @@ magic_AppInit(interp) /* This is where we replace the home ".wishrc" file with */ /* magic's startup script. */ - Tcl_SetVar(interp, "tcl_rcFileName", TCL_DIR "/magic.tcl", TCL_GLOBAL_ONLY); + /* Try to find magic.tcl relative to the current executable. + If that fails, try to load it from the path where it was located + at build time. + */ + const char *magic_tcl_path = NULL; + if (Tcl_ExprString(interp, "[file join [file dirname [info nameofexecutable]] magic.tcl]") == TCL_OK) { + magic_tcl_path = Tcl_GetStringResult(interp); + } else { + magic_tcl_path = TCL_DIR "/magic.tcl"; + } + Tcl_SetVar(interp, "tcl_rcFileName", magic_tcl_path, TCL_GLOBAL_ONLY); return TCL_OK; }