SharedForward.h.in 23 KB

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