SharedForward.h
Go to the documentation of this file.
1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2  file Copyright.txt or https://cmake.org/licensing#kwsys for details. */
3 #ifndef vtksys_SharedForward_h
4 # define vtksys_SharedForward_h
5 
6 /*
7  This header is used to create a forwarding executable sets up the
8  shared library search path and replaces itself with a real
9  executable. This is useful when creating installations on UNIX with
10  shared libraries that will run from any install directory. Typical
11  usage:
12 
13  #if defined(CMAKE_INTDIR)
14  # define CONFIG_DIR_PRE CMAKE_INTDIR "/"
15  # define CONFIG_DIR_POST "/" CMAKE_INTDIR
16  #else
17  # define CONFIG_DIR_PRE ""
18  # define CONFIG_DIR_POST ""
19  #endif
20  #define vtksys_SHARED_FORWARD_DIR_BUILD "/path/to/foo-build/bin"
21  #define vtksys_SHARED_FORWARD_PATH_BUILD "." CONFIG_DIR_POST
22  #define vtksys_SHARED_FORWARD_PATH_INSTALL "../lib/foo-1.2"
23  #define vtksys_SHARED_FORWARD_EXE_BUILD CONFIG_DIR_PRE "foo-real"
24  #define vtksys_SHARED_FORWARD_EXE_INSTALL
25  "../lib/foo-1.2/foo-real"
26  #define vtksys_SHARED_FORWARD_OPTION_COMMAND "--command"
27  #define vtksys_SHARED_FORWARD_OPTION_PRINT "--print"
28  #define vtksys_SHARED_FORWARD_OPTION_LDD "--ldd"
29  #if defined(CMAKE_INTDIR)
30  # define vtksys_SHARED_FORWARD_CONFIG_NAME CMAKE_INTDIR
31  #endif
32  #include <vtksys/SharedForward.h>
33  int main(int argc, char** argv)
34  {
35  return vtksys_shared_forward_to_real(argc, argv);
36  }
37 
38  Specify search and executable paths relative to the forwarding
39  executable location or as full paths. Include no trailing slash.
40  In the case of a multi-configuration build, when CMAKE_INTDIR is
41  defined, the DIR_BUILD setting should point at the directory above
42  the executable (the one containing the per-configuration
43  subdirectory specified by CMAKE_INTDIR). Then PATH_BUILD entries
44  and EXE_BUILD should be specified relative to this location and use
45  CMAKE_INTDIR as necessary. In the above example imagine appending
46  the PATH_BUILD or EXE_BUILD setting to the DIR_BUILD setting. The
47  result should form a valid path with per-configuration subdirectory.
48 
49  Additional paths may be specified in the PATH_BUILD and PATH_INSTALL
50  variables by using comma-separated strings. For example:
51 
52  #define vtksys_SHARED_FORWARD_PATH_BUILD \
53  "." CONFIG_DIR_POST, "/path/to/bar-build" CONFIG_DIR_POST
54  #define vtksys_SHARED_FORWARD_PATH_INSTALL \
55  "../lib/foo-1.2", "../lib/bar-4.5"
56 
57  See the comments below for specific explanations of each macro.
58 */
59 
60 /* Disable -Wcast-qual warnings since they are too hard to fix in a
61  cross-platform way. */
62 # if defined(__clang__) && defined(__has_warning)
63 # if __has_warning("-Wcast-qual")
64 # pragma clang diagnostic push
65 # pragma clang diagnostic ignored "-Wcast-qual"
66 # endif
67 # endif
68 
69 /* Full path to the directory in which this executable is built. Do
70  not include a trailing slash. */
71 # if !defined(vtksys_SHARED_FORWARD_DIR_BUILD)
72 # error "Must define vtksys_SHARED_FORWARD_DIR_BUILD"
73 # endif
74 # if !defined(KWSYS_SHARED_FORWARD_DIR_BUILD)
75 # define KWSYS_SHARED_FORWARD_DIR_BUILD \
76  vtksys_SHARED_FORWARD_DIR_BUILD
77 # endif
78 
79 /* Library search path for build tree. */
80 # if !defined(vtksys_SHARED_FORWARD_PATH_BUILD)
81 # error "Must define vtksys_SHARED_FORWARD_PATH_BUILD"
82 # endif
83 # if !defined(KWSYS_SHARED_FORWARD_PATH_BUILD)
84 # define KWSYS_SHARED_FORWARD_PATH_BUILD \
85  vtksys_SHARED_FORWARD_PATH_BUILD
86 # endif
87 
88 /* Library search path for install tree. */
89 # if !defined(vtksys_SHARED_FORWARD_PATH_INSTALL)
90 # error "Must define vtksys_SHARED_FORWARD_PATH_INSTALL"
91 # endif
92 # if !defined(KWSYS_SHARED_FORWARD_PATH_INSTALL)
93 # define KWSYS_SHARED_FORWARD_PATH_INSTALL \
94  vtksys_SHARED_FORWARD_PATH_INSTALL
95 # endif
96 
97 /* The real executable to which to forward in the build tree. */
98 # if !defined(vtksys_SHARED_FORWARD_EXE_BUILD)
99 # error "Must define vtksys_SHARED_FORWARD_EXE_BUILD"
100 # endif
101 # if !defined(KWSYS_SHARED_FORWARD_EXE_BUILD)
102 # define KWSYS_SHARED_FORWARD_EXE_BUILD \
103  vtksys_SHARED_FORWARD_EXE_BUILD
104 # endif
105 
106 /* The real executable to which to forward in the install tree. */
107 # if !defined(vtksys_SHARED_FORWARD_EXE_INSTALL)
108 # error "Must define vtksys_SHARED_FORWARD_EXE_INSTALL"
109 # endif
110 # if !defined(KWSYS_SHARED_FORWARD_EXE_INSTALL)
111 # define KWSYS_SHARED_FORWARD_EXE_INSTALL \
112  vtksys_SHARED_FORWARD_EXE_INSTALL
113 # endif
114 
115 /* The configuration name with which this executable was built (Debug/Release).
116  */
117 # if defined(vtksys_SHARED_FORWARD_CONFIG_NAME)
118 # define KWSYS_SHARED_FORWARD_CONFIG_NAME \
119  vtksys_SHARED_FORWARD_CONFIG_NAME
120 # else
121 # undef KWSYS_SHARED_FORWARD_CONFIG_NAME
122 # endif
123 
124 /* Create command line option to replace executable. */
125 # if defined(vtksys_SHARED_FORWARD_OPTION_COMMAND)
126 # if !defined(KWSYS_SHARED_FORWARD_OPTION_COMMAND)
127 # define KWSYS_SHARED_FORWARD_OPTION_COMMAND \
128  vtksys_SHARED_FORWARD_OPTION_COMMAND
129 # endif
130 # else
131 # undef KWSYS_SHARED_FORWARD_OPTION_COMMAND
132 # endif
133 
134 /* Create command line option to print environment setting and exit. */
135 # if defined(vtksys_SHARED_FORWARD_OPTION_PRINT)
136 # if !defined(KWSYS_SHARED_FORWARD_OPTION_PRINT)
137 # define KWSYS_SHARED_FORWARD_OPTION_PRINT \
138  vtksys_SHARED_FORWARD_OPTION_PRINT
139 # endif
140 # else
141 # undef KWSYS_SHARED_FORWARD_OPTION_PRINT
142 # endif
143 
144 /* Create command line option to run ldd or equivalent. */
145 # if defined(vtksys_SHARED_FORWARD_OPTION_LDD)
146 # if !defined(KWSYS_SHARED_FORWARD_OPTION_LDD)
147 # define KWSYS_SHARED_FORWARD_OPTION_LDD \
148  vtksys_SHARED_FORWARD_OPTION_LDD
149 # endif
150 # else
151 # undef KWSYS_SHARED_FORWARD_OPTION_LDD
152 # endif
153 
154 /* Include needed system headers. */
155 
156 # include <errno.h>
157 # include <limits.h>
158 # include <stddef.h> /* size_t */
159 # include <stdio.h>
160 # include <stdlib.h>
161 # include <string.h>
162 
163 # if defined(_WIN32) && !defined(__CYGWIN__)
164 # include <windows.h>
165 
166 # include <io.h>
167 # include <process.h>
168 # define KWSYS_SHARED_FORWARD_ESCAPE_ARGV /* re-escape argv for execvp */
169 # else
170 # include <sys/stat.h>
171 # include <unistd.h>
172 # endif
173 
174 /* Configuration for this platform. */
175 
176 /* The path separator for this platform. */
177 # if defined(_WIN32) && !defined(__CYGWIN__)
178 # define KWSYS_SHARED_FORWARD_PATH_SEP ';'
179 # define KWSYS_SHARED_FORWARD_PATH_SLASH '\\'
180 # else
181 # define KWSYS_SHARED_FORWARD_PATH_SEP ':'
182 # define KWSYS_SHARED_FORWARD_PATH_SLASH '/'
183 # endif
184 static const char kwsys_shared_forward_path_sep[2] = {
186 };
187 static const char kwsys_shared_forward_path_slash[2] = {
189 };
190 
191 /* The maximum length of a file name. */
192 # if defined(PATH_MAX)
193 # define KWSYS_SHARED_FORWARD_MAXPATH PATH_MAX
194 # elif defined(MAXPATHLEN)
195 # define KWSYS_SHARED_FORWARD_MAXPATH MAXPATHLEN
196 # else
197 # define KWSYS_SHARED_FORWARD_MAXPATH 16384
198 # endif
199 
200 /* Select the environment variable holding the shared library runtime
201  search path for this platform and build configuration. Also select
202  ldd command equivalent. */
203 
204 /* Linux */
205 # if defined(__linux)
206 # define KWSYS_SHARED_FORWARD_LDD "ldd"
207 # define KWSYS_SHARED_FORWARD_LDD_N 1
208 # define KWSYS_SHARED_FORWARD_LDPATH "LD_LIBRARY_PATH"
209 
210 /* FreeBSD */
211 # elif defined(__FreeBSD__)
212 # define KWSYS_SHARED_FORWARD_LDD "ldd"
213 # define KWSYS_SHARED_FORWARD_LDD_N 1
214 # define KWSYS_SHARED_FORWARD_LDPATH "LD_LIBRARY_PATH"
215 
216 /* OpenBSD */
217 # elif defined(__OpenBSD__)
218 # define KWSYS_SHARED_FORWARD_LDD "ldd"
219 # define KWSYS_SHARED_FORWARD_LDD_N 1
220 # define KWSYS_SHARED_FORWARD_LDPATH "LD_LIBRARY_PATH"
221 
222 /* OS X */
223 # elif defined(__APPLE__)
224 # define KWSYS_SHARED_FORWARD_LDD "otool", "-L"
225 # define KWSYS_SHARED_FORWARD_LDD_N 2
226 # define KWSYS_SHARED_FORWARD_LDPATH "DYLD_LIBRARY_PATH"
227 
228 /* AIX */
229 # elif defined(_AIX)
230 # define KWSYS_SHARED_FORWARD_LDD "dump", "-H"
231 # define KWSYS_SHARED_FORWARD_LDD_N 2
232 # define KWSYS_SHARED_FORWARD_LDPATH "LIBPATH"
233 
234 /* SUN */
235 # elif defined(__sun)
236 # define KWSYS_SHARED_FORWARD_LDD "ldd"
237 # define KWSYS_SHARED_FORWARD_LDD_N 1
238 # include <sys/isa_defs.h>
239 # if defined(_ILP32)
240 # define KWSYS_SHARED_FORWARD_LDPATH "LD_LIBRARY_PATH"
241 # elif defined(_LP64)
242 # define KWSYS_SHARED_FORWARD_LDPATH "LD_LIBRARY_PATH_64"
243 # endif
244 
245 /* HP-UX */
246 # elif defined(__hpux)
247 # define KWSYS_SHARED_FORWARD_LDD "chatr"
248 # define KWSYS_SHARED_FORWARD_LDD_N 1
249 # if defined(__LP64__)
250 # define KWSYS_SHARED_FORWARD_LDPATH "LD_LIBRARY_PATH"
251 # else
252 # define KWSYS_SHARED_FORWARD_LDPATH "SHLIB_PATH"
253 # endif
254 
255 /* SGI MIPS */
256 # elif defined(__sgi) && defined(_MIPS_SIM)
257 # define KWSYS_SHARED_FORWARD_LDD "ldd"
258 # define KWSYS_SHARED_FORWARD_LDD_N 1
259 # if _MIPS_SIM == _ABIO32
260 # define KWSYS_SHARED_FORWARD_LDPATH "LD_LIBRARY_PATH"
261 # elif _MIPS_SIM == _ABIN32
262 # define KWSYS_SHARED_FORWARD_LDPATH "LD_LIBRARYN32_PATH"
263 # elif _MIPS_SIM == _ABI64
264 # define KWSYS_SHARED_FORWARD_LDPATH "LD_LIBRARY64_PATH"
265 # endif
266 
267 /* Cygwin */
268 # elif defined(__CYGWIN__)
269 # define KWSYS_SHARED_FORWARD_LDD \
270  "cygcheck" /* TODO: cygwin 1.7 has ldd \
271  */
272 # define KWSYS_SHARED_FORWARD_LDD_N 1
273 # define KWSYS_SHARED_FORWARD_LDPATH "PATH"
274 
275 /* Windows */
276 # elif defined(_WIN32)
277 # define KWSYS_SHARED_FORWARD_LDPATH "PATH"
278 
279 /* Guess on this unknown system. */
280 # else
281 # define KWSYS_SHARED_FORWARD_LDD "ldd"
282 # define KWSYS_SHARED_FORWARD_LDD_N 1
283 # define KWSYS_SHARED_FORWARD_LDPATH "LD_LIBRARY_PATH"
284 # endif
285 
286 # ifdef KWSYS_SHARED_FORWARD_ESCAPE_ARGV
287 typedef struct kwsys_sf_arg_info_s
288 {
289  const char* arg;
290  int size;
291  int quote;
292 } kwsys_sf_arg_info;
293 
294 static kwsys_sf_arg_info kwsys_sf_get_arg_info(const char* in)
295 {
296  /* Initialize information. */
297  kwsys_sf_arg_info info;
298 
299  /* String iterator. */
300  const char* c;
301 
302  /* Keep track of how many backslashes have been encountered in a row. */
303  int windows_backslashes = 0;
304 
305  /* Start with the length of the original argument, plus one for
306  either a terminating null or a separating space. */
307  info.arg = in;
308  info.size = (int)strlen(in) + 1;
309  info.quote = 0;
310 
311  /* Scan the string for characters that require escaping or quoting. */
312  for (c = in; *c; ++c) {
313  /* Check whether this character needs quotes. */
314  if (strchr(" \t?'#&<>|^", *c)) {
315  info.quote = 1;
316  }
317 
318  /* On Windows only backslashes and double-quotes need escaping. */
319  if (*c == '\\') {
320  /* Found a backslash. It may need to be escaped later. */
321  ++windows_backslashes;
322  } else if (*c == '"') {
323  /* Found a double-quote. We need to escape it and all
324  immediately preceding backslashes. */
325  info.size += windows_backslashes + 1;
326  windows_backslashes = 0;
327  } else {
328  /* Found another character. This eliminates the possibility
329  that any immediately preceding backslashes will be
330  escaped. */
331  windows_backslashes = 0;
332  }
333  }
334 
335  /* Check whether the argument needs surrounding quotes. */
336  if (info.quote) {
337  /* Surrounding quotes are needed. Allocate space for them. */
338  info.size += 2;
339 
340  /* We must escape all ending backslashes when quoting on windows. */
341  info.size += windows_backslashes;
342  }
343 
344  return info;
345 }
346 
347 static char* kwsys_sf_get_arg(kwsys_sf_arg_info info, char* out)
348 {
349  /* String iterator. */
350  const char* c;
351 
352  /* Keep track of how many backslashes have been encountered in a row. */
353  int windows_backslashes = 0;
354 
355  /* Whether the argument must be quoted. */
356  if (info.quote) {
357  /* Add the opening quote for this argument. */
358  *out++ = '"';
359  }
360 
361  /* Scan the string for characters that require escaping or quoting. */
362  for (c = info.arg; *c; ++c) {
363  /* On Windows only backslashes and double-quotes need escaping. */
364  if (*c == '\\') {
365  /* Found a backslash. It may need to be escaped later. */
366  ++windows_backslashes;
367  } else if (*c == '"') {
368  /* Found a double-quote. Escape all immediately preceding
369  backslashes. */
370  while (windows_backslashes > 0) {
371  --windows_backslashes;
372  *out++ = '\\';
373  }
374 
375  /* Add the backslash to escape the double-quote. */
376  *out++ = '\\';
377  } else {
378  /* We encountered a normal character. This eliminates any
379  escaping needed for preceding backslashes. */
380  windows_backslashes = 0;
381  }
382 
383  /* Store this character. */
384  *out++ = *c;
385  }
386 
387  if (info.quote) {
388  /* Add enough backslashes to escape any trailing ones. */
389  while (windows_backslashes > 0) {
390  --windows_backslashes;
391  *out++ = '\\';
392  }
393 
394  /* Add the closing quote for this argument. */
395  *out++ = '"';
396  }
397 
398  /* Store a terminating null without incrementing. */
399  *out = 0;
400 
401  return out;
402 }
403 # endif
404 
405 /* Function to convert a logical or relative path to a physical full path. */
406 static int kwsys_shared_forward_realpath(const char* in_path, char* out_path)
407 {
408 # if defined(_WIN32) && !defined(__CYGWIN__)
409  /* Implementation for Windows. */
410  DWORD n =
411  GetFullPathNameA(in_path, KWSYS_SHARED_FORWARD_MAXPATH, out_path, 0);
412  return n > 0 && n <= KWSYS_SHARED_FORWARD_MAXPATH;
413 # else
414  /* Implementation for UNIX. */
415  return realpath(in_path, out_path) != 0;
416 # endif
417 }
418 
419 static int kwsys_shared_forward_samepath(const char* file1, const char* file2)
420 {
421 # if defined(_WIN32)
422  int result = 0;
423  HANDLE h1 = CreateFileA(file1, GENERIC_READ, FILE_SHARE_READ, NULL,
424  OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
425  HANDLE h2 = CreateFileA(file2, GENERIC_READ, FILE_SHARE_READ, NULL,
426  OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
427  if (h1 != INVALID_HANDLE_VALUE && h2 != INVALID_HANDLE_VALUE) {
428  BY_HANDLE_FILE_INFORMATION fi1;
429  BY_HANDLE_FILE_INFORMATION fi2;
430  GetFileInformationByHandle(h1, &fi1);
431  GetFileInformationByHandle(h2, &fi2);
432  result = (fi1.dwVolumeSerialNumber == fi2.dwVolumeSerialNumber &&
433  fi1.nFileIndexHigh == fi2.nFileIndexHigh &&
434  fi1.nFileIndexLow == fi2.nFileIndexLow);
435  }
436  CloseHandle(h1);
437  CloseHandle(h2);
438  return result;
439 # else
440  struct stat fs1, fs2;
441  return (stat(file1, &fs1) == 0 && stat(file2, &fs2) == 0 &&
442  memcmp(&fs2.st_dev, &fs1.st_dev, sizeof(fs1.st_dev)) == 0 &&
443  memcmp(&fs2.st_ino, &fs1.st_ino, sizeof(fs1.st_ino)) == 0 &&
444  fs2.st_size == fs1.st_size);
445 # endif
446 }
447 
448 /* Function to report a system error message. */
449 static void kwsys_shared_forward_strerror(char* message)
450 {
451 # if defined(_WIN32) && !defined(__CYGWIN__)
452  /* Implementation for Windows. */
453  DWORD original = GetLastError();
454  DWORD length =
455  FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
456  0, original, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
457  message, KWSYS_SHARED_FORWARD_MAXPATH, 0);
458  if (length < 1 || length > KWSYS_SHARED_FORWARD_MAXPATH) {
459  /* FormatMessage failed. Use a default message. */
460  snprintf(message, KWSYS_SHARED_FORWARD_MAXPATH,
461  "Error 0x%lX (FormatMessage failed with error 0x%lX)", original,
462  GetLastError());
463  }
464 # else
465  /* Implementation for UNIX. */
466  strcpy(message, strerror(errno));
467 # endif
468 }
469 
470 /* Functions to execute a child process. */
471 static void kwsys_shared_forward_execvp(const char* cmd,
472  char const* const* argv)
473 {
474 # ifdef KWSYS_SHARED_FORWARD_ESCAPE_ARGV
475  /* Count the number of arguments. */
476  int argc = 0;
477  {
478  char const* const* argvc;
479  for (argvc = argv; *argvc; ++argvc, ++argc) {
480  }
481  }
482 
483  /* Create the escaped arguments. */
484  {
485  char** nargv = (char**)malloc((argc + 1) * sizeof(char*));
486  int i;
487  for (i = 0; i < argc; ++i) {
488  kwsys_sf_arg_info info = kwsys_sf_get_arg_info(argv[i]);
489  nargv[i] = (char*)malloc(info.size);
490  kwsys_sf_get_arg(info, nargv[i]);
491  }
492  nargv[argc] = 0;
493 
494  /* Replace the command line to be used. */
495  argv = (char const* const*)nargv;
496  }
497 # endif
498 
499 /* Invoke the child process. */
500 # if defined(_MSC_VER)
501  _execvp(cmd, argv);
502 # elif defined(__MINGW32__) && !defined(__MINGW64__)
503  execvp(cmd, argv);
504 # else
505  execvp(cmd, (char* const*)argv);
506 # endif
507 }
508 
509 /* Function to get the directory containing the given file or directory. */
510 static void kwsys_shared_forward_dirname(const char* begin, char* result)
511 {
512  /* Find the location of the last slash. */
513  int last_slash_index = -1;
514  const char* end = begin + strlen(begin);
515  for (; begin <= end && last_slash_index < 0; --end) {
516  if (*end == '/' || *end == '\\') {
517  last_slash_index = (int)(end - begin);
518  }
519  }
520 
521  /* Handle each case of the index of the last slash. */
522  if (last_slash_index < 0) {
523  /* No slashes. */
524  strcpy(result, ".");
525  } else if (last_slash_index == 0) {
526  /* Only one leading slash. */
527  strcpy(result, kwsys_shared_forward_path_slash);
528  }
529 # if defined(_WIN32)
530  else if (last_slash_index == 2 && begin[1] == ':') {
531  /* Only one leading drive letter and slash. */
532  strncpy(result, begin, (size_t)last_slash_index);
533  result[last_slash_index] = KWSYS_SHARED_FORWARD_PATH_SLASH;
534  result[last_slash_index + 1] = 0;
535  }
536 # endif
537  else {
538  /* A non-leading slash. */
539  strncpy(result, begin, (size_t)last_slash_index);
540  result[last_slash_index] = 0;
541  }
542 }
543 
544 /* Function to check if a file exists and is executable. */
545 static int kwsys_shared_forward_is_executable(const char* f)
546 {
547 # if defined(_MSC_VER)
548 # define KWSYS_SHARED_FORWARD_ACCESS _access
549 # else
550 # define KWSYS_SHARED_FORWARD_ACCESS access
551 # endif
552 # if defined(X_OK)
553 # define KWSYS_SHARED_FORWARD_ACCESS_OK X_OK
554 # else
555 # define KWSYS_SHARED_FORWARD_ACCESS_OK 04
556 # endif
558  return 1;
559  } else {
560  return 0;
561  }
562 }
563 
564 /* Function to locate the executable currently running. */
565 static int kwsys_shared_forward_self_path(const char* argv0, char* result)
566 {
567  /* Check whether argv0 has a slash. */
568  int has_slash = 0;
569  const char* p = argv0;
570  for (; *p && !has_slash; ++p) {
571  if (*p == '/' || *p == '\\') {
572  has_slash = 1;
573  }
574  }
575 
576  if (has_slash) {
577  /* There is a slash. Use the dirname of the given location. */
578  kwsys_shared_forward_dirname(argv0, result);
579  return 1;
580  } else {
581  /* There is no slash. Search the PATH for the executable. */
582  const char* path = getenv("PATH");
583  const char* begin = path;
584  const char* end = begin + (begin ? strlen(begin) : 0);
585  const char* first = begin;
586  while (first != end) {
587  /* Store the end of this path entry. */
588  const char* last;
589 
590  /* Skip all path separators. */
591  for (; *first && *first == KWSYS_SHARED_FORWARD_PATH_SEP; ++first)
592  ;
593 
594  /* Find the next separator. */
595  for (last = first; *last && *last != KWSYS_SHARED_FORWARD_PATH_SEP;
596  ++last)
597  ;
598 
599  /* If we got a non-empty directory, look for the executable there. */
600  if (first < last) {
601  /* Determine the length without trailing slash. */
602  size_t length = (size_t)(last - first);
603  if (*(last - 1) == '/' || *(last - 1) == '\\') {
604  --length;
605  }
606 
607  /* Construct the name of the executable in this location. */
608  strncpy(result, first, length);
609  result[length] = KWSYS_SHARED_FORWARD_PATH_SLASH;
610  strcpy(result + (length) + 1, argv0);
611 
612  /* Check if it exists and is executable. */
614  /* Found it. */
615  result[length] = 0;
616  return 1;
617  }
618  }
619 
620  /* Move to the next directory in the path. */
621  first = last;
622  }
623  }
624 
625  /* We could not find the executable. */
626  return 0;
627 }
628 
629 /* Function to convert a specified path to a full path. If it is not
630  already full, it is taken relative to the self path. */
631 static int kwsys_shared_forward_fullpath(const char* self_path,
632  const char* in_path, char* result,
633  const char* desc)
634 {
635  /* Check the specified path type. */
636  if (in_path[0] == '/') {
637  /* Already a full path. */
638  strcpy(result, in_path);
639  }
640 # if defined(_WIN32)
641  else if (in_path[0] && in_path[1] == ':') {
642  /* Already a full path. */
643  strcpy(result, in_path);
644  }
645 # endif
646  else {
647  /* Relative to self path. */
648  char temp_path[KWSYS_SHARED_FORWARD_MAXPATH];
649  strcpy(temp_path, self_path);
650  strcat(temp_path, kwsys_shared_forward_path_slash);
651  strcat(temp_path, in_path);
652  if (!kwsys_shared_forward_realpath(temp_path, result)) {
653  if (desc) {
654  char msgbuf[KWSYS_SHARED_FORWARD_MAXPATH];
656  fprintf(stderr, "Error converting %s \"%s\" to real path: %s\n", desc,
657  temp_path, msgbuf);
658  }
659  return 0;
660  }
661  }
662  return 1;
663 }
664 
665 /* Function to compute the library search path and executable name
666  based on the self path. */
667 static int kwsys_shared_forward_get_settings(const char* self_path,
668  char* ldpath, char* exe)
669 {
670  /* Possible search paths. */
671  static const char* search_path_build[] = { KWSYS_SHARED_FORWARD_PATH_BUILD,
672  0 };
673  static const char* search_path_install[] = {
675  };
676 
677  /* Chosen paths. */
678  const char** search_path;
679  const char* exe_path;
680 
681 /* Get the real name of the build and self paths. */
682 # if defined(KWSYS_SHARED_FORWARD_CONFIG_NAME)
683  char build_path[] =
684  KWSYS_SHARED_FORWARD_DIR_BUILD "/" KWSYS_SHARED_FORWARD_CONFIG_NAME;
685  char self_path_logical[KWSYS_SHARED_FORWARD_MAXPATH];
686 # else
687  char build_path[] = KWSYS_SHARED_FORWARD_DIR_BUILD;
688  const char* self_path_logical = self_path;
689 # endif
690  char build_path_real[KWSYS_SHARED_FORWARD_MAXPATH];
691  char self_path_real[KWSYS_SHARED_FORWARD_MAXPATH];
692  if (!kwsys_shared_forward_realpath(self_path, self_path_real)) {
693  char msgbuf[KWSYS_SHARED_FORWARD_MAXPATH];
695  fprintf(stderr, "Error converting self path \"%s\" to real path: %s\n",
696  self_path, msgbuf);
697  return 0;
698  }
699 
700  /* Check whether we are running in the build tree or an install tree. */
701  if (kwsys_shared_forward_realpath(build_path, build_path_real) &&
702  kwsys_shared_forward_samepath(self_path_real, build_path_real)) {
703  /* Running in build tree. Use the build path and exe. */
704  search_path = search_path_build;
705 # if defined(_WIN32)
706  exe_path = KWSYS_SHARED_FORWARD_EXE_BUILD ".exe";
707 # else
709 # endif
710 
711 # if defined(KWSYS_SHARED_FORWARD_CONFIG_NAME)
712  /* Remove the configuration directory from self_path. */
713  kwsys_shared_forward_dirname(self_path, self_path_logical);
714 # endif
715  } else {
716  /* Running in install tree. Use the install path and exe. */
717  search_path = search_path_install;
718 # if defined(_WIN32)
719  exe_path = KWSYS_SHARED_FORWARD_EXE_INSTALL ".exe";
720 # else
722 # endif
723 
724 # if defined(KWSYS_SHARED_FORWARD_CONFIG_NAME)
725  /* Use the original self path directory. */
726  strcpy(self_path_logical, self_path);
727 # endif
728  }
729 
730  /* Construct the runtime search path. */
731  {
732  const char** dir;
733  for (dir = search_path; *dir; ++dir) {
734  /* Add separator between path components. */
735  if (dir != search_path) {
736  strcat(ldpath, kwsys_shared_forward_path_sep);
737  }
738 
739  /* Add this path component. */
740  if (!kwsys_shared_forward_fullpath(self_path_logical, *dir,
741  ldpath + strlen(ldpath),
742  "runtime path entry")) {
743  return 0;
744  }
745  }
746  }
747 
748  /* Construct the executable location. */
749  if (!kwsys_shared_forward_fullpath(self_path_logical, exe_path, exe,
750  "executable file")) {
751  return 0;
752  }
753  return 1;
754 }
755 
756 /* Function to print why execution of a command line failed. */
757 static void kwsys_shared_forward_print_failure(char const* const* argv)
758 {
760  char const* const* arg = argv;
762  fprintf(stderr, "Error running");
763  for (; *arg; ++arg) {
764  fprintf(stderr, " \"%s\"", *arg);
765  }
766  fprintf(stderr, ": %s\n", msg);
767 }
768 
769 /* Static storage space to store the updated environment variable. */
770 static char kwsys_shared_forward_ldpath[65535] =
772 
773 /* Main driver function to be called from main. */
774 static int vtksys_shared_forward_to_real(int argc, char** argv_in)
775 {
776  char const** argv = (char const**)argv_in;
777  /* Get the directory containing this executable. */
778  char self_path[KWSYS_SHARED_FORWARD_MAXPATH];
779  if (kwsys_shared_forward_self_path(argv[0], self_path)) {
780  /* Found this executable. Use it to get the library directory. */
782  if (kwsys_shared_forward_get_settings(self_path,
784  /* Append the old runtime search path. */
785  const char* old_ldpath = getenv(KWSYS_SHARED_FORWARD_LDPATH);
786  if (old_ldpath) {
788  strcat(kwsys_shared_forward_ldpath, old_ldpath);
789  }
790 
791  /* Store the environment variable. */
793 
794 # if defined(KWSYS_SHARED_FORWARD_OPTION_COMMAND)
795  /* Look for the command line replacement option. */
796  if (argc > 1 &&
797  strcmp(argv[1], KWSYS_SHARED_FORWARD_OPTION_COMMAND) == 0) {
798  if (argc > 2) {
799  /* Use the command line given. */
800  strcpy(exe, argv[2]);
801  argv += 2;
802  argc -= 2;
803  } else {
804  /* The option was not given an executable. */
805  fprintf(stderr,
806  "Option " KWSYS_SHARED_FORWARD_OPTION_COMMAND
807  " must be followed by a command line.\n");
808  return 1;
809  }
810  }
811 # endif
812 
813 # if defined(KWSYS_SHARED_FORWARD_OPTION_PRINT)
814  /* Look for the print command line option. */
815  if (argc > 1 &&
816  strcmp(argv[1], KWSYS_SHARED_FORWARD_OPTION_PRINT) == 0) {
817  fprintf(stdout, "%s\n", kwsys_shared_forward_ldpath);
818  fprintf(stdout, "%s\n", exe);
819  return 0;
820  }
821 # endif
822 
823 # if defined(KWSYS_SHARED_FORWARD_OPTION_LDD)
824  /* Look for the ldd command line option. */
825  if (argc > 1 && strcmp(argv[1], KWSYS_SHARED_FORWARD_OPTION_LDD) == 0) {
826 # if defined(KWSYS_SHARED_FORWARD_LDD)
827  /* Use the named ldd-like executable and arguments. */
828  char const* ldd_argv[] = { KWSYS_SHARED_FORWARD_LDD, 0, 0 };
829  ldd_argv[KWSYS_SHARED_FORWARD_LDD_N] = exe;
830  kwsys_shared_forward_execvp(ldd_argv[0], ldd_argv);
831 
832  /* Report why execution failed. */
834  return 1;
835 # else
836  /* We have no ldd-like executable available on this platform. */
837  fprintf(stderr, "No ldd-like tool is known to this executable.\n");
838  return 1;
839 # endif
840  }
841 # endif
842 
843  /* Replace this process with the real executable. */
844  argv[0] = exe;
845  kwsys_shared_forward_execvp(argv[0], argv);
846 
847  /* Report why execution failed. */
849  } else {
850  /* Could not convert self path to the library directory. */
851  }
852  } else {
853  /* Could not find this executable. */
854  fprintf(stderr, "Error locating executable \"%s\".\n", argv[0]);
855  }
856 
857  /* Avoid unused argument warning. */
858  (void)argc;
859 
860  /* Exit with failure. */
861  return 1;
862 }
863 
864 /* Restore warning stack. */
865 # if defined(__clang__) && defined(__has_warning)
866 # if __has_warning("-Wcast-qual")
867 # pragma clang diagnostic pop
868 # endif
869 # endif
870 
871 #else
872 # error "vtksys/SharedForward.h should be included only once."
873 #endif
static int kwsys_shared_forward_realpath(const char *in_path, char *out_path)
int
#define KWSYS_SHARED_FORWARD_PATH_SEP
static int kwsys_shared_forward_fullpath(const char *self_path, const char *in_path, char *result, const char *desc)
static void kwsys_shared_forward_print_failure(char const *const *argv)
length
info
static void kwsys_shared_forward_execvp(const char *cmd, char const *const *argv)
#define KWSYS_SHARED_FORWARD_PATH_BUILD
Definition: SharedForward.h:84
#define KWSYS_SHARED_FORWARD_ACCESS_OK
dir
static const char kwsys_shared_forward_path_sep[2]
#define KWSYS_SHARED_FORWARD_LDD_N
#define KWSYS_SHARED_FORWARD_EXE_INSTALL
#define KWSYS_SHARED_FORWARD_EXE_BUILD
size
static void kwsys_shared_forward_strerror(char *message)
#define KWSYS_SHARED_FORWARD_DIR_BUILD
Definition: SharedForward.h:75
static int vtksys_shared_forward_to_real(int argc, char **argv_in)
static int kwsys_shared_forward_samepath(const char *file1, const char *file2)
static void kwsys_shared_forward_dirname(const char *begin, char *result)
#define KWSYS_SHARED_FORWARD_LDD
#define KWSYS_SHARED_FORWARD_ACCESS
static int kwsys_shared_forward_is_executable(const char *f)
static char kwsys_shared_forward_ldpath[65535]
#define KWSYS_SHARED_FORWARD_PATH_SLASH
#define KWSYS_SHARED_FORWARD_PATH_INSTALL
Definition: SharedForward.h:93
static const char kwsys_shared_forward_path_slash[2]
#define KWSYS_SHARED_FORWARD_LDPATH
#define KWSYS_SHARED_FORWARD_MAXPATH
static int kwsys_shared_forward_get_settings(const char *self_path, char *ldpath, char *exe)
static int kwsys_shared_forward_self_path(const char *argv0, char *result)