SharedForward.h.in 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. /*=========================================================================
  2. Program: KWSys - Kitware System Library
  3. Module: $RCSfile$
  4. Copyright (c) Kitware, Inc., Insight Consortium. All rights reserved.
  5. See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even
  7. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  8. PURPOSE. See the above copyright notices for more information.
  9. =========================================================================*/
  10. #ifndef @KWSYS_NAMESPACE@_SharedForward_h
  11. #define @KWSYS_NAMESPACE@_SharedForward_h
  12. /*
  13. This header is used to create a forwarding executable sets up the
  14. shared library search path and replaces itself with a real
  15. executable. This is useful when creating installations on UNIX with
  16. shared libraries that will run from any install directory. Typical
  17. usage:
  18. #define @KWSYS_NAMESPACE@_SHARED_FORWARD_DIR_BUILD "/path/to/foo-build/bin"
  19. #define @KWSYS_NAMESPACE@_SHARED_FORWARD_PATH_BUILD "."
  20. #define @KWSYS_NAMESPACE@_SHARED_FORWARD_PATH_INSTALL "../lib/foo-1.2"
  21. #define @KWSYS_NAMESPACE@_SHARED_FORWARD_EXE_BUILD "foo-real"
  22. #define @KWSYS_NAMESPACE@_SHARED_FORWARD_EXE_INSTALL "../lib/foo-1.2/foo-real"
  23. #define @KWSYS_NAMESPACE@_SHARED_FORWARD_OPTION_PRINT "--print"
  24. #define @KWSYS_NAMESPACE@_SHARED_FORWARD_OPTION_LDD "--ldd"
  25. #if defined(CMAKE_INTDIR)
  26. # define @KWSYS_NAMESPACE@_SHARED_FORWARD_CONFIG_NAME CMAKE_INTDIR
  27. #endif
  28. #include <@KWSYS_NAMESPACE@/SharedForward.h>
  29. int main(int argc, char** argv)
  30. {
  31. return @KWSYS_NAMESPACE@_shared_forward_to_real(argc, argv);
  32. }
  33. */
  34. /*--------------------------------------------------------------------------*/
  35. /* Configuration for this executable. Specify search and executable
  36. paths relative to the forwarding executable location or as full
  37. paths. Include no trailing slash. */
  38. /* Full path to the directory in which this executable is built. Do
  39. not include a trailing slash. */
  40. #if !defined(@KWSYS_NAMESPACE@_SHARED_FORWARD_DIR_BUILD)
  41. # error "Must define @KWSYS_NAMESPACE@_SHARED_FORWARD_DIR_BUILD"
  42. #endif
  43. #if !defined(KWSYS_SHARED_FORWARD_DIR_BUILD)
  44. # define KWSYS_SHARED_FORWARD_DIR_BUILD @KWSYS_NAMESPACE@_SHARED_FORWARD_DIR_BUILD
  45. #endif
  46. /* Library search path for build tree. */
  47. #if !defined(@KWSYS_NAMESPACE@_SHARED_FORWARD_PATH_BUILD)
  48. # error "Must define @KWSYS_NAMESPACE@_SHARED_FORWARD_PATH_BUILD"
  49. #endif
  50. #if !defined(KWSYS_SHARED_FORWARD_PATH_BUILD)
  51. # define KWSYS_SHARED_FORWARD_PATH_BUILD @KWSYS_NAMESPACE@_SHARED_FORWARD_PATH_BUILD
  52. #endif
  53. /* Library search path for install tree. */
  54. #if !defined(@KWSYS_NAMESPACE@_SHARED_FORWARD_PATH_INSTALL)
  55. # error "Must define @KWSYS_NAMESPACE@_SHARED_FORWARD_PATH_INSTALL"
  56. #endif
  57. #if !defined(KWSYS_SHARED_FORWARD_PATH_INSTALL)
  58. # define KWSYS_SHARED_FORWARD_PATH_INSTALL @KWSYS_NAMESPACE@_SHARED_FORWARD_PATH_INSTALL
  59. #endif
  60. /* The real executable to which to forward in the build tree. */
  61. #if !defined(@KWSYS_NAMESPACE@_SHARED_FORWARD_EXE_BUILD)
  62. # error "Must define @KWSYS_NAMESPACE@_SHARED_FORWARD_EXE_BUILD"
  63. #endif
  64. #if !defined(KWSYS_SHARED_FORWARD_EXE_BUILD)
  65. # define KWSYS_SHARED_FORWARD_EXE_BUILD @KWSYS_NAMESPACE@_SHARED_FORWARD_EXE_BUILD
  66. #endif
  67. /* The real executable to which to forward in the install tree. */
  68. #if !defined(@KWSYS_NAMESPACE@_SHARED_FORWARD_EXE_INSTALL)
  69. # error "Must define @KWSYS_NAMESPACE@_SHARED_FORWARD_EXE_INSTALL"
  70. #endif
  71. #if !defined(KWSYS_SHARED_FORWARD_EXE_INSTALL)
  72. # define KWSYS_SHARED_FORWARD_EXE_INSTALL @KWSYS_NAMESPACE@_SHARED_FORWARD_EXE_INSTALL
  73. #endif
  74. /* The configuration name with which this executable was built (Debug/Release). */
  75. #if defined(@KWSYS_NAMESPACE@_SHARED_FORWARD_CONFIG_NAME)
  76. # define KWSYS_SHARED_FORWARD_CONFIG_NAME @KWSYS_NAMESPACE@_SHARED_FORWARD_CONFIG_NAME
  77. #else
  78. # undef KWSYS_SHARED_FORWARD_CONFIG_NAME
  79. #endif
  80. /* Create command line option to print environment setting and exit. */
  81. #if defined(@KWSYS_NAMESPACE@_SHARED_FORWARD_OPTION_PRINT)
  82. # if !defined(KWSYS_SHARED_FORWARD_OPTION_PRINT)
  83. # define KWSYS_SHARED_FORWARD_OPTION_PRINT @KWSYS_NAMESPACE@_SHARED_FORWARD_OPTION_PRINT
  84. # endif
  85. #else
  86. # undef KWSYS_SHARED_FORWARD_OPTION_PRINT
  87. #endif
  88. /* Create command line option to run ldd or equivalent. */
  89. #if defined(@KWSYS_NAMESPACE@_SHARED_FORWARD_OPTION_LDD)
  90. # if !defined(KWSYS_SHARED_FORWARD_OPTION_LDD)
  91. # define KWSYS_SHARED_FORWARD_OPTION_LDD @KWSYS_NAMESPACE@_SHARED_FORWARD_OPTION_LDD
  92. # endif
  93. #else
  94. # undef KWSYS_SHARED_FORWARD_OPTION_LDD
  95. #endif
  96. /*--------------------------------------------------------------------------*/
  97. /* Include needed system headers. */
  98. #include <limits.h>
  99. #include <stdlib.h>
  100. #include <string.h>
  101. #include <errno.h>
  102. #include <stdio.h>
  103. #if defined(_WIN32) && !defined(__CYGWIN__)
  104. # include <io.h>
  105. # include <windows.h>
  106. # include <process.h>
  107. #else
  108. # include <unistd.h>
  109. #endif
  110. /*--------------------------------------------------------------------------*/
  111. /* Configuration for this platform. */
  112. /* The path separator for this platform. */
  113. #if defined(_WIN32) && !defined(__CYGWIN__)
  114. # define KWSYS_SHARED_FORWARD_PATH_SEP ';'
  115. # define KWSYS_SHARED_FORWARD_PATH_SLASH '\\'
  116. #else
  117. # define KWSYS_SHARED_FORWARD_PATH_SEP ':'
  118. # define KWSYS_SHARED_FORWARD_PATH_SLASH '/'
  119. #endif
  120. static const char kwsys_shared_forward_path_sep[2] = {KWSYS_SHARED_FORWARD_PATH_SEP, 0};
  121. static const char kwsys_shared_forward_path_slash[2] = {KWSYS_SHARED_FORWARD_PATH_SLASH, 0};
  122. /* The maximum length of a file name. */
  123. #if defined(PATH_MAX)
  124. # define KWSYS_SHARED_FORWARD_MAXPATH PATH_MAX
  125. #elif defined(MAXPATHLEN)
  126. # define KWSYS_SHARED_FORWARD_MAXPATH MAXPATHLEN
  127. #else
  128. # define KWSYS_SHARED_FORWARD_MAXPATH 16384
  129. #endif
  130. /* Select the environment variable holding the shared library runtime
  131. search path for this platform and build configuration. Also select
  132. ldd command equivalent. */
  133. /* Linux */
  134. #if defined(__linux)
  135. # define KWSYS_SHARED_FORWARD_LDD "ldd"
  136. # define KWSYS_SHARED_FORWARD_LDD_N 1
  137. # define KWSYS_SHARED_FORWARD_LDPATH "LD_LIBRARY_PATH"
  138. #endif
  139. /* FreeBSD */
  140. #if defined(__FreeBSD__)
  141. # define KWSYS_SHARED_FORWARD_LDD "ldd"
  142. # define KWSYS_SHARED_FORWARD_LDD_N 1
  143. # define KWSYS_SHARED_FORWARD_LDPATH "LD_LIBRARY_PATH"
  144. #endif
  145. /* OSX */
  146. #if defined(__APPLE__)
  147. # define KWSYS_SHARED_FORWARD_LDD "otool", "-L"
  148. # define KWSYS_SHARED_FORWARD_LDD_N 2
  149. # define KWSYS_SHARED_FORWARD_LDPATH "DYLD_LIBRARY_PATH"
  150. #endif
  151. /* AIX */
  152. #if defined(_AIX)
  153. # define KWSYS_SHARED_FORWARD_LDD "dump", "-H"
  154. # define KWSYS_SHARED_FORWARD_LDD_N 2
  155. # define KWSYS_SHARED_FORWARD_LDPATH "LIBPATH"
  156. #endif
  157. /* SUN */
  158. #if defined(__sparc)
  159. # define KWSYS_SHARED_FORWARD_LDD "ldd"
  160. # define KWSYS_SHARED_FORWARD_LDD_N 1
  161. # include <sys/isa_defs.h>
  162. # if defined(_ILP32)
  163. # define KWSYS_SHARED_FORWARD_LDPATH "LD_LIBRARY_PATH"
  164. # elif defined(_LP64)
  165. # define KWSYS_SHARED_FORWARD_LDPATH "LD_LIBRARY_PATH_64"
  166. # endif
  167. #endif
  168. /* HP-UX */
  169. #if defined(__hpux)
  170. # define KWSYS_SHARED_FORWARD_LDD "chatr"
  171. # define KWSYS_SHARED_FORWARD_LDD_N 1
  172. # if defined(__LP64__)
  173. # define KWSYS_SHARED_FORWARD_LDPATH "LD_LIBRARY_PATH"
  174. # else
  175. # define KWSYS_SHARED_FORWARD_LDPATH "SHLIB_PATH"
  176. # endif
  177. #endif
  178. /* SGI MIPS */
  179. #if defined(__sgi) && defined(_MIPS_SIM)
  180. # define KWSYS_SHARED_FORWARD_LDD "ldd"
  181. # define KWSYS_SHARED_FORWARD_LDD_N 1
  182. # if _MIPS_SIM == _ABIO32
  183. # define KWSYS_SHARED_FORWARD_LDPATH "LD_LIBRARY_PATH"
  184. # elif _MIPS_SIM == _ABIN32
  185. # define KWSYS_SHARED_FORWARD_LDPATH "LD_LIBRARYN32_PATH"
  186. # elif _MIPS_SIM == _ABI64
  187. # define KWSYS_SHARED_FORWARD_LDPATH "LD_LIBRARY64_PATH"
  188. # endif
  189. #endif
  190. /* Windows */
  191. #if defined(_WIN32)
  192. # if defined(__CYGWIN__)
  193. # define KWSYS_SHARED_FORWARD_LDD "cygcheck"
  194. # define KWSYS_SHARED_FORWARD_LDD_N 1
  195. # endif
  196. # define KWSYS_SHARED_FORWARD_LDPATH "PATH"
  197. #endif
  198. /* Guess on this unknown system. */
  199. #if !defined(KWSYS_SHARED_FORWARD_LDPATH)
  200. # define KWSYS_SHARED_FORWARD_LDD "ldd"
  201. # define KWSYS_SHARED_FORWARD_LDD_N 1
  202. # define KWSYS_SHARED_FORWARD_LDPATH "LD_LIBRARY_PATH"
  203. #endif
  204. /*--------------------------------------------------------------------------*/
  205. /* Function to convert a logical or relative path to a physical full path. */
  206. static int kwsys_shared_forward_realpath(const char* in_path, char* out_path)
  207. {
  208. #if defined(_WIN32) && !defined(__CYGWIN__)
  209. /* Implementation for Windows. */
  210. DWORD n = GetFullPathName(in_path, KWSYS_SHARED_FORWARD_MAXPATH,
  211. out_path, 0);
  212. return n > 0 && n <= KWSYS_SHARED_FORWARD_MAXPATH;
  213. #else
  214. /* Implementation for UNIX. */
  215. return realpath(in_path, out_path) != 0;
  216. #endif
  217. }
  218. /*--------------------------------------------------------------------------*/
  219. /* Function to report a system error message. */
  220. static void kwsys_shared_forward_strerror(char* message)
  221. {
  222. #if defined(_WIN32) && !defined(__CYGWIN__)
  223. /* Implementation for Windows. */
  224. DWORD original = GetLastError();
  225. DWORD length = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
  226. FORMAT_MESSAGE_IGNORE_INSERTS, 0, original,
  227. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  228. message, KWSYS_SHARED_FORWARD_MAXPATH, 0);
  229. if(length < 1 || length > KWSYS_SHARED_FORWARD_MAXPATH)
  230. {
  231. /* FormatMessage failed. Use a default message. */
  232. _snprintf(message, KWSYS_SHARED_FORWARD_MAXPATH,
  233. "Error 0x%X (FormatMessage failed with error 0x%X)",
  234. original, GetLastError());
  235. }
  236. #else
  237. /* Implementation for UNIX. */
  238. strcpy(message, strerror(errno));
  239. #endif
  240. }
  241. /*--------------------------------------------------------------------------*/
  242. /* Functions to execute a child process. */
  243. static void kwsys_shared_forward_execv(const char* cmd, char* const argv[])
  244. {
  245. #if defined(_MSC_VER)
  246. _execv(cmd, argv);
  247. #else
  248. execv(cmd, argv);
  249. #endif
  250. }
  251. static void kwsys_shared_forward_execvp(const char* cmd, char* const argv[])
  252. {
  253. #if defined(_MSC_VER)
  254. _execvp(cmd, argv);
  255. #else
  256. execvp(cmd, argv);
  257. #endif
  258. }
  259. /*--------------------------------------------------------------------------*/
  260. /* Function to get the directory containing the given file or directory. */
  261. static void kwsys_shared_forward_dirname(const char* begin, char* result)
  262. {
  263. /* Find the location of the last slash. */
  264. int last_slash_index = -1;
  265. const char* end = begin + strlen(begin);
  266. for(;begin <= end && last_slash_index < 0; --end)
  267. {
  268. if(*end == '/' || *end == '\\')
  269. {
  270. last_slash_index = end-begin;
  271. }
  272. }
  273. /* Handle each case of the index of the last slash. */
  274. if(last_slash_index < 0)
  275. {
  276. /* No slashes. */
  277. strcpy(result, ".");
  278. }
  279. else if(last_slash_index == 0)
  280. {
  281. /* Only one leading slash. */
  282. strcpy(result, kwsys_shared_forward_path_slash);
  283. }
  284. #if defined(_WIN32)
  285. else if(last_slash_index == 2 && begin[1] == ':')
  286. {
  287. /* Only one leading drive letter and slash. */
  288. strncpy(result, begin, last_slash_index);
  289. result[last_slash_index] = KWSYS_SHARED_FORWARD_PATH_SLASH;
  290. result[last_slash_index+1] = 0;
  291. }
  292. #endif
  293. else
  294. {
  295. /* A non-leading slash. */
  296. strncpy(result, begin, last_slash_index);
  297. result[last_slash_index] = 0;
  298. }
  299. }
  300. /*--------------------------------------------------------------------------*/
  301. /* Function to check if a file exists and is executable. */
  302. static int kwsys_shared_forward_is_executable(const char* f)
  303. {
  304. #if defined(_MSC_VER)
  305. # define KWSYS_SHARED_FORWARD_ACCESS _access
  306. #else
  307. # define KWSYS_SHARED_FORWARD_ACCESS access
  308. #endif
  309. #if defined(X_OK)
  310. # define KWSYS_SHARED_FORWARD_ACCESS_OK X_OK
  311. #else
  312. # define KWSYS_SHARED_FORWARD_ACCESS_OK 04
  313. #endif
  314. if(KWSYS_SHARED_FORWARD_ACCESS(f, KWSYS_SHARED_FORWARD_ACCESS_OK) == 0)
  315. {
  316. return 1;
  317. }
  318. else
  319. {
  320. return 0;
  321. }
  322. }
  323. /*--------------------------------------------------------------------------*/
  324. /* Function to locate the executable currently running. */
  325. static int kwsys_shared_forward_self_path(const char* argv0, char* result)
  326. {
  327. /* Check whether argv0 has a slash. */
  328. int has_slash = 0;
  329. const char* p = argv0;
  330. for(;*p && !has_slash; ++p)
  331. {
  332. if(*p == '/' || *p == '\\')
  333. {
  334. has_slash = 1;
  335. }
  336. }
  337. if(has_slash)
  338. {
  339. /* There is a slash. Use the dirname of the given location. */
  340. kwsys_shared_forward_dirname(argv0, result);
  341. return 1;
  342. }
  343. else
  344. {
  345. /* There is no slash. Search the PATH for the executable. */
  346. const char* path = getenv("PATH");
  347. const char* begin = path;
  348. const char* end = begin + (begin?strlen(begin):0);
  349. const char* first = begin;
  350. while(first != end)
  351. {
  352. /* Store the end of this path entry. */
  353. const char* last;
  354. /* Skip all path separators. */
  355. for(;*first && *first == KWSYS_SHARED_FORWARD_PATH_SEP; ++first);
  356. /* Find the next separator. */
  357. for(last = first;*last && *last != KWSYS_SHARED_FORWARD_PATH_SEP; ++last);
  358. /* If we got a non-empty directory, look for the executable there. */
  359. if(first < last)
  360. {
  361. /* Determine the length without trailing slash. */
  362. int length = last-first;
  363. if(*(last-1) == '/' || *(last-1) == '\\')
  364. {
  365. --length;
  366. }
  367. /* Construct the name of the executable in this location. */
  368. strncpy(result, first, length);
  369. result[length] = KWSYS_SHARED_FORWARD_PATH_SLASH;
  370. strcpy(result+(length)+1, argv0);
  371. /* Check if it exists and is executable. */
  372. if(kwsys_shared_forward_is_executable(result))
  373. {
  374. /* Found it. */
  375. result[length] = 0;
  376. return 1;
  377. }
  378. }
  379. /* Move to the next directory in the path. */
  380. first = last;
  381. }
  382. }
  383. /* We could not find the executable. */
  384. return 0;
  385. }
  386. /*--------------------------------------------------------------------------*/
  387. /* Function to convert a specified path to a full path. If it is not
  388. already full, it is taken relative to the self path. */
  389. static int kwsys_shared_forward_fullpath(const char* self_path,
  390. const char* in_path,
  391. char* result,
  392. const char* desc)
  393. {
  394. /* Check the specified path type. */
  395. if(in_path[0] == '/')
  396. {
  397. /* Already a full path. */
  398. strcpy(result, in_path);
  399. }
  400. #if defined(_WIN32)
  401. else if(in_path[0] && in_path[1] == ':')
  402. {
  403. /* Already a full path. */
  404. strcpy(result, in_path);
  405. }
  406. #endif
  407. else
  408. {
  409. /* Relative to self path. */
  410. char temp_path[KWSYS_SHARED_FORWARD_MAXPATH];
  411. strcpy(temp_path, self_path);
  412. strcat(temp_path, kwsys_shared_forward_path_slash);
  413. strcat(temp_path, in_path);
  414. if(!kwsys_shared_forward_realpath(temp_path, result))
  415. {
  416. if(desc)
  417. {
  418. char msgbuf[KWSYS_SHARED_FORWARD_MAXPATH];
  419. kwsys_shared_forward_strerror(msgbuf);
  420. fprintf(stderr, "Error converting %s \"%s\" to real path: %s\n",
  421. desc, temp_path, msgbuf);
  422. }
  423. return 0;
  424. }
  425. }
  426. return 1;
  427. }
  428. /*--------------------------------------------------------------------------*/
  429. /* Function to compute the library search path and executable name
  430. based on the self path. */
  431. static int kwsys_shared_forward_get_settings(const char* self_path,
  432. char* ldpath, char* exe)
  433. {
  434. /* Possible search paths. */
  435. static const char* search_path_build[] = {KWSYS_SHARED_FORWARD_PATH_BUILD, 0};
  436. static const char* search_path_install[] = {KWSYS_SHARED_FORWARD_PATH_INSTALL, 0};
  437. /* Chosen paths. */
  438. const char** search_path;
  439. const char* exe_path;
  440. /* Get the real name of the build and self paths. */
  441. #if defined(KWSYS_SHARED_FORWARD_CONFIG_NAME)
  442. char build_path[] = KWSYS_SHARED_FORWARD_DIR_BUILD "/" KWSYS_SHARED_FORWARD_CONFIG_NAME;
  443. char self_path_logical[KWSYS_SHARED_FORWARD_MAXPATH];
  444. #else
  445. char build_path[] = KWSYS_SHARED_FORWARD_DIR_BUILD;
  446. const char* self_path_logical = self_path;
  447. #endif
  448. char build_path_real[KWSYS_SHARED_FORWARD_MAXPATH];
  449. char self_path_real[KWSYS_SHARED_FORWARD_MAXPATH];
  450. if(!kwsys_shared_forward_realpath(self_path, self_path_real))
  451. {
  452. char msgbuf[KWSYS_SHARED_FORWARD_MAXPATH];
  453. kwsys_shared_forward_strerror(msgbuf);
  454. fprintf(stderr, "Error converting self path \"%s\" to real path: %s\n",
  455. self_path, msgbuf);
  456. return 0;
  457. }
  458. /* Check whether we are running in the build tree or an install tree. */
  459. if(kwsys_shared_forward_realpath(build_path, build_path_real) &&
  460. strcmp(self_path_real, build_path_real) == 0)
  461. {
  462. /* Running in build tree. Use the build path and exe. */
  463. search_path = search_path_build;
  464. #if defined(_WIN32)
  465. exe_path = KWSYS_SHARED_FORWARD_EXE_BUILD ".exe";
  466. #else
  467. exe_path = KWSYS_SHARED_FORWARD_EXE_BUILD;
  468. #endif
  469. #if defined(KWSYS_SHARED_FORWARD_CONFIG_NAME)
  470. /* Remove the configuration directory from self_path. */
  471. kwsys_shared_forward_dirname(self_path, self_path_logical);
  472. #endif
  473. }
  474. else
  475. {
  476. /* Running in install tree. Use the install path and exe. */
  477. search_path = search_path_install;
  478. #if defined(_WIN32)
  479. exe_path = KWSYS_SHARED_FORWARD_EXE_INSTALL ".exe";
  480. #else
  481. exe_path = KWSYS_SHARED_FORWARD_EXE_INSTALL;
  482. #endif
  483. #if defined(KWSYS_SHARED_FORWARD_CONFIG_NAME)
  484. /* Use the original self path directory. */
  485. strcpy(self_path_logical, self_path);
  486. #endif
  487. }
  488. /* Construct the runtime search path. */
  489. {
  490. const char** dir;
  491. for(dir = search_path; *dir; ++dir)
  492. {
  493. /* Add seperator between path components. */
  494. if(dir != search_path)
  495. {
  496. strcat(ldpath, kwsys_shared_forward_path_sep);
  497. }
  498. /* Add this path component. */
  499. if(!kwsys_shared_forward_fullpath(self_path_logical, *dir,
  500. ldpath+strlen(ldpath),
  501. "runtime path entry"))
  502. {
  503. return 0;
  504. }
  505. }
  506. }
  507. /* Construct the executable location. */
  508. if(!kwsys_shared_forward_fullpath(self_path_logical, exe_path, exe,
  509. "executable file"))
  510. {
  511. return 0;
  512. }
  513. return 1;
  514. }
  515. /*--------------------------------------------------------------------------*/
  516. /* Function to print why execution of a command line failed. */
  517. static void kwsys_shared_forward_print_failure(char** argv)
  518. {
  519. char msg[KWSYS_SHARED_FORWARD_MAXPATH];
  520. char** arg = argv;
  521. kwsys_shared_forward_strerror(msg);
  522. fprintf(stderr, "Error running");
  523. for(; *arg; ++arg)
  524. {
  525. fprintf(stderr, " \"%s\"", *arg);
  526. }
  527. fprintf(stderr, ": %s\n", msg);
  528. }
  529. /* Static storage space to store the updated environment variable. */
  530. static char kwsys_shared_forward_ldpath[KWSYS_SHARED_FORWARD_MAXPATH*16] = KWSYS_SHARED_FORWARD_LDPATH "=";
  531. /*--------------------------------------------------------------------------*/
  532. /* Main driver function to be called from main. */
  533. static int @KWSYS_NAMESPACE@_shared_forward_to_real(int argc, char** argv)
  534. {
  535. /* Get the directory containing this executable. */
  536. char self_path[KWSYS_SHARED_FORWARD_MAXPATH];
  537. if(kwsys_shared_forward_self_path(argv[0], self_path))
  538. {
  539. /* Found this executable. Use it to get the library directory. */
  540. char exe[KWSYS_SHARED_FORWARD_MAXPATH];
  541. if(kwsys_shared_forward_get_settings(self_path,
  542. kwsys_shared_forward_ldpath, exe))
  543. {
  544. /* Append the old runtime search path. */
  545. const char* old_ldpath = getenv(KWSYS_SHARED_FORWARD_LDPATH);
  546. if(old_ldpath)
  547. {
  548. strcat(kwsys_shared_forward_ldpath, kwsys_shared_forward_path_sep);
  549. strcat(kwsys_shared_forward_ldpath, old_ldpath);
  550. }
  551. /* Store the environment variable. */
  552. putenv(kwsys_shared_forward_ldpath);
  553. #if defined(KWSYS_SHARED_FORWARD_OPTION_PRINT)
  554. /* Look for the print command line option. */
  555. if(argc > 1 && strcmp(argv[1], KWSYS_SHARED_FORWARD_OPTION_PRINT) == 0)
  556. {
  557. fprintf(stdout, "%s\n", kwsys_shared_forward_ldpath);
  558. fprintf(stdout, "%s\n", exe);
  559. return 0;
  560. }
  561. #endif
  562. #if defined(KWSYS_SHARED_FORWARD_OPTION_LDD)
  563. /* Look for the ldd command line option. */
  564. if(argc > 1 && strcmp(argv[1], KWSYS_SHARED_FORWARD_OPTION_LDD) == 0)
  565. {
  566. # if defined(KWSYS_SHARED_FORWARD_LDD)
  567. /* Use the named ldd-like executable and arguments. */
  568. char* ldd_argv[] = {KWSYS_SHARED_FORWARD_LDD, 0, 0};
  569. ldd_argv[KWSYS_SHARED_FORWARD_LDD_N] = exe;
  570. kwsys_shared_forward_execvp(ldd_argv[0], ldd_argv);
  571. /* Report why execution failed. */
  572. kwsys_shared_forward_print_failure(ldd_argv);
  573. return 1;
  574. # else
  575. /* We have no ldd-like executable available on this platform. */
  576. fprintf(stderr, "No ldd-like tool is known to this executable.\n");
  577. return 1;
  578. # endif
  579. }
  580. #endif
  581. /* Replace this process with the real executable. */
  582. argv[0] = exe;
  583. kwsys_shared_forward_execv(argv[0], argv);
  584. /* Report why execution failed. */
  585. kwsys_shared_forward_print_failure(argv);
  586. }
  587. else
  588. {
  589. /* Could not convert self path to the library directory. */
  590. }
  591. }
  592. else
  593. {
  594. /* Could not find this executable. */
  595. fprintf(stderr, "Error locating executable \"%s\".\n", argv[0]);
  596. }
  597. /* Avoid unused argument warning. */
  598. (void)argc;
  599. /* Exit with failure. */
  600. return 1;
  601. }
  602. #else
  603. # error "@KWSYS_NAMESPACE@/SharedForward.h should be included only once."
  604. #endif