When openocd is built in a source directory but not installed using
make install it is helpful to search for the tcl/ directory next to
the src/ directory where the openocd binary is created.

On Linux this path can be found using realpath(/proc/self/exe). uname()
is used to learn if the system is Linux; ie. if /proc is available.
realpath() and sys/utsname.h are both checked for by configure.

Signed-off-by: Peter Stuge <pe...@stuge.se>
---
 configure.in         |    2 ++
 src/helper/options.c |   23 +++++++++++++++++++++++
 2 files changed, 25 insertions(+), 0 deletions(-)

diff --git a/configure.in b/configure.in
index a15b80a..c94b3bc 100644
--- a/configure.in
+++ b/configure.in
@@ -121,6 +121,7 @@ AC_CHECK_HEADERS([net/if.h], [], [], [dnl
 # include <sys/socket.h>
 #endif
 ])
+AC_CHECK_HEADERS(sys/utsname.h)
 
 AC_HEADER_ASSERT
 AC_HEADER_STDBOOL
@@ -133,6 +134,7 @@ AC_CHECK_FUNCS(strnlen)
 AC_CHECK_FUNCS(gettimeofday)
 AC_CHECK_FUNCS(usleep)
 AC_CHECK_FUNCS(vasprintf)
+AC_CHECK_FUNCS(realpath)
 
 build_bitbang=no
 build_bitq=no
diff --git a/src/helper/options.c b/src/helper/options.c
index f8db2cd..4f424dd 100644
--- a/src/helper/options.c
+++ b/src/helper/options.c
@@ -30,6 +30,10 @@
 
 #include <getopt.h>
 
+#ifdef HAVE_SYS_UTSNAME_H
+#include <sys/utsname.h>
+#endif
+
 static int help_flag, version_flag;
 
 static const struct option long_options[] =
@@ -104,6 +108,25 @@ static void add_default_dirs(void)
 
        const char *home = getenv("HOME");
 
+#if defined(HAVE_SYS_UTSNAME_H) && defined(HAVE_REALPATH)
+       char real[PATH_MAX];
+       size_t real_len, cmd_pos, cmd_len = strlen("/src/openocd");
+       struct utsname uts;
+
+       if (!uname(&uts) && !strcmp(uts.sysname, "Linux") &&
+           realpath("/proc/self/exe", real))
+       {
+               real_len = strlen(real);
+               cmd_pos = real_len - cmd_len;
+               if (real_len >= cmd_len &&
+                   !strcmp(real + cmd_pos, "/src/openocd"))
+               {
+                       snprintf(real + cmd_pos, sizeof(real) - cmd_pos, 
"/tcl");
+                       add_script_search_dir(real);
+               }
+       }
+#endif
+
        if (home)
        {
                char *path;
-- 
1.7.2

_______________________________________________
Openocd-development mailing list
Openocd-development@lists.berlios.de
https://lists.berlios.de/mailman/listinfo/openocd-development

Reply via email to