diff options
author | Victor Westerhuis <victor@westerhu.is> | 2021-12-07 22:41:16 +0100 |
---|---|---|
committer | Adrian Thurston <thurston@colm.net> | 2021-12-11 11:18:08 -0800 |
commit | be2c9cf238ec1d048e3b2d05f1b56a9f2a267668 (patch) | |
tree | 4c87d9d576a2a306b317f7d84a0b18075d0063bf /src | |
parent | 4e5cb4f242c001e0937bf90f0e7c96fa64e2befe (diff) | |
download | colm-be2c9cf238ec1d048e3b2d05f1b56a9f2a267668.tar.gz |
Use /proc/self/exe to detect colm build directory
The canonical identity of a file on Unix is the combination of device
and inode numbers. This code checks both possible paths for the
colm executable: directly in the build directory (when colm is linked
against libcolm statically) or in libtool's `objdir` subdirectory (when
colm is linked against libcolm dynamically).
This fails in two situations I can see: when /proc is not mounted or
doesn't exist, or when colm is installed using a hard link.
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile.am | 4 | ||||
-rw-r--r-- | src/main.cc | 19 |
2 files changed, 16 insertions, 7 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 895a46d6..b336fdfe 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -54,7 +54,8 @@ common_CFLAGS = \ -Wall \ -DINCLUDEDIR='"$(includedir)"' \ -DLIBDIR='"$(libdir)"' \ - -DABS_TOP_BUILDDIR='"$(abs_top_builddir)"' + -DABS_TOP_BUILDDIR='"$(abs_top_builddir)"' \ + -DABS_BUILDDIR='"$(abs_builddir)"' libprog_a_SOURCES = \ buffer.h bytecode.h colm.h debug.h dotgen.h fsmcodegen.h fsmgraph.h \ @@ -214,4 +215,3 @@ colm-wrap: colm-wrap.sh loadinit.cc: gen/if1.h loadboot2.cc: gen/if2.h loadcolm.cc: gen/if3.h - diff --git a/src/main.cc b/src/main.cc index 482fb4a4..a54a6dd8 100644 --- a/src/main.cc +++ b/src/main.cc @@ -688,14 +688,23 @@ void defaultBuildDir() if ( buildDir != 0 ) return; - const char *ldlp = getenv("LD_LIBRARY_PATH"); - if ( ldlp != 0 ) { - size_t len_atbd = strlen(ABS_TOP_BUILDDIR); + struct stat self; + if ( stat( "/proc/self/exe", &self ) == 0 ) + { + struct stat colm; + + if ( stat ( ABS_BUILDDIR "/" LT_OBJDIR "colm", &colm ) == 0 && + self.st_dev == colm.st_dev && self.st_ino == colm.st_ino ) + { + buildDir = ABS_TOP_BUILDDIR; + return; + } - if ( strlen(ldlp) > len_atbd && - memcmp( ldlp, ABS_TOP_BUILDDIR "/", len_atbd+1) == 0 ) + if ( stat ( ABS_BUILDDIR "/colm", &colm ) == 0 && + self.st_dev == colm.st_dev && self.st_ino == colm.st_ino ) { buildDir = ABS_TOP_BUILDDIR; + return; } } } |