blob: 81df3e264f5222d984b46c736c78cf9604ab025b [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001From a4a0440a644c6c5e5da096efe3cf05ba309a284f Mon Sep 17 00:00:00 2001
2From: "NODA, Kai" <nodakai@gmail.com>
3Date: Sun, 22 Apr 2012 17:01:02 +0900
4Subject: [PATCH] Use /proc/self/exe for "swig -swiglib" on non-Win32
5 platforms.
6
7If it wasn't found, then fall back to a fixed string just as before.
8
9Upstream-Status: Submitted
10http://sourceforge.net/mailarchive/message.php?msg_id=29179733
11
12---
13 Source/Modules/main.cxx | 24 ++++++++++++++++++++++--
14 1 file changed, 22 insertions(+), 2 deletions(-)
15
16diff --git a/Source/Modules/main.cxx b/Source/Modules/main.cxx
17index d2f5d3b..cbb0a12 100644
18--- a/Source/Modules/main.cxx
19+++ b/Source/Modules/main.cxx
20@@ -26,6 +26,11 @@ char cvsroot_main_cxx[] = "$Id$";
21 #include "cparse.h"
22 #include <ctype.h>
23 #include <limits.h> // for INT_MAX
24+#ifndef _WIN32
25+#include <cstddef>
26+#include <unistd.h> // for readlink
27+#include <sys/stat.h> // for stat
28+#endif
29
30 // Global variables
31
32@@ -902,9 +907,9 @@ int SWIG_main(int argc, char *argv[], Language *l) {
33
34 // Check for SWIG_LIB environment variable
35 if ((c = getenv("SWIG_LIB")) == (char *) 0) {
36+ char *p;
37 #if defined(_WIN32)
38 char buf[MAX_PATH];
39- char *p;
40 if (!(GetModuleFileName(0, buf, MAX_PATH) == 0 || (p = strrchr(buf, '\\')) == 0)) {
41 *(p + 1) = '\0';
42 SwigLib = NewStringf("%sLib", buf); // Native windows installation path
43@@ -914,7 +919,22 @@ int SWIG_main(int argc, char *argv[], Language *l) {
44 if (Len(SWIG_LIB_WIN_UNIX) > 0)
45 SwigLibWinUnix = NewString(SWIG_LIB_WIN_UNIX); // Unix installation path using a drive letter (for msys/mingw)
46 #else
47- SwigLib = NewString(SWIG_LIB);
48+ char buf[PATH_MAX];
49+ if (0 < ::readlink("/proc/self/exe", buf, sizeof(buf)) &&
50+ (p = ::strstr(buf, "/bin/swig"))) {
51+ int major, minor, patch;
52+ const int ret = ::sscanf(VERSION, "%d.%d.%d", &major, &minor, &patch);
53+ if (3 == ret) {
54+ const ::ptrdiff_t dir_part_len = p - buf;
55+ ::snprintf(p, PATH_MAX - dir_part_len, "/share/swig/%d.%d.%d", major, minor, patch);
56+ struct ::stat stat_res;
57+ if (0 == ::stat(buf, &stat_res) && S_ISDIR(stat_res.st_mode)) {
58+ SwigLib = NewString(buf);
59+ }
60+ }
61+ }
62+ if (NULL == SwigLib)
63+ SwigLib = NewString(SWIG_LIB);
64 #endif
65 } else {
66 SwigLib = NewString(c);
67--
681.7.9.5
69