SharedForward.h.in 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. #include <@KWSYS_NAMESPACE@/SharedForward.h>
  24. int main(int argc, char** argv)
  25. {
  26. return @KWSYS_NAMESPACE@_shared_forward_to_real(argc, argv);
  27. }
  28. */
  29. /*--------------------------------------------------------------------------*/
  30. /* Configuration for this executable. Specify search and executable
  31. paths relative to the forwarding executable location or as full
  32. paths. Include no trailing slash. */
  33. /* This is not useful on Windows. */
  34. #if defined(_WIN32)
  35. # error "@KWSYS_NAMESPACE@/SharedForward.h is useless on Windows."
  36. #endif
  37. /* Full path to the directory in which this executable is built. Do
  38. not include a trailing slash. */
  39. #if !defined(@KWSYS_NAMESPACE@_SHARED_FORWARD_DIR_BUILD)
  40. # error "Must define @KWSYS_NAMESPACE@_SHARED_FORWARD_DIR_BUILD"
  41. #endif
  42. #if !defined(KWSYS_SHARED_FORWARD_DIR_BUILD)
  43. # define KWSYS_SHARED_FORWARD_DIR_BUILD @KWSYS_NAMESPACE@_SHARED_FORWARD_DIR_BUILD
  44. #endif
  45. /* Library search path for build tree. */
  46. #if !defined(@KWSYS_NAMESPACE@_SHARED_FORWARD_PATH_BUILD)
  47. # error "Must define @KWSYS_NAMESPACE@_SHARED_FORWARD_PATH_BUILD"
  48. #endif
  49. #if !defined(KWSYS_SHARED_FORWARD_PATH_BUILD)
  50. # define KWSYS_SHARED_FORWARD_PATH_BUILD @KWSYS_NAMESPACE@_SHARED_FORWARD_PATH_BUILD
  51. #endif
  52. /* Library search path for install tree. */
  53. #if !defined(@KWSYS_NAMESPACE@_SHARED_FORWARD_PATH_INSTALL)
  54. # error "Must define @KWSYS_NAMESPACE@_SHARED_FORWARD_PATH_INSTALL"
  55. #endif
  56. #if !defined(KWSYS_SHARED_FORWARD_PATH_INSTALL)
  57. # define KWSYS_SHARED_FORWARD_PATH_INSTALL @KWSYS_NAMESPACE@_SHARED_FORWARD_PATH_INSTALL
  58. #endif
  59. /* The real executable to which to forward in the build tree. */
  60. #if !defined(@KWSYS_NAMESPACE@_SHARED_FORWARD_EXE_BUILD)
  61. # error "Must define @KWSYS_NAMESPACE@_SHARED_FORWARD_EXE_BUILD"
  62. #endif
  63. #if !defined(KWSYS_SHARED_FORWARD_EXE_BUILD)
  64. # define KWSYS_SHARED_FORWARD_EXE_BUILD @KWSYS_NAMESPACE@_SHARED_FORWARD_EXE_BUILD
  65. #endif
  66. /* The real executable to which to forward in the install tree. */
  67. #if !defined(@KWSYS_NAMESPACE@_SHARED_FORWARD_EXE_INSTALL)
  68. # error "Must define @KWSYS_NAMESPACE@_SHARED_FORWARD_EXE_INSTALL"
  69. #endif
  70. #if !defined(KWSYS_SHARED_FORWARD_EXE_INSTALL)
  71. # define KWSYS_SHARED_FORWARD_EXE_INSTALL @KWSYS_NAMESPACE@_SHARED_FORWARD_EXE_INSTALL
  72. #endif
  73. /* Create command line option to print environment setting and exit. */
  74. #if defined(@KWSYS_NAMESPACE@_SHARED_FORWARD_OPTION_PRINT)
  75. # if !defined(KWSYS_SHARED_FORWARD_OPTION_PRINT)
  76. # define KWSYS_SHARED_FORWARD_OPTION_PRINT @KWSYS_NAMESPACE@_SHARED_FORWARD_OPTION_PRINT
  77. # endif
  78. #else
  79. # undef KWSYS_SHARED_FORWARD_OPTION_PRINT
  80. #endif
  81. /* Create command line option to run ldd or equivalent. */
  82. #if defined(@KWSYS_NAMESPACE@_SHARED_FORWARD_OPTION_LDD)
  83. # if !defined(KWSYS_SHARED_FORWARD_OPTION_LDD)
  84. # define KWSYS_SHARED_FORWARD_OPTION_LDD @KWSYS_NAMESPACE@_SHARED_FORWARD_OPTION_LDD
  85. # endif
  86. #else
  87. # undef KWSYS_SHARED_FORWARD_OPTION_LDD
  88. #endif
  89. /*--------------------------------------------------------------------------*/
  90. /* Include needed system headers. */
  91. #include <limits.h>
  92. #include <stdlib.h>
  93. #include <string.h>
  94. #include <unistd.h>
  95. #include <errno.h>
  96. #include <stdio.h>
  97. /*--------------------------------------------------------------------------*/
  98. /* Configuration for this platform. */
  99. /* The path separator for this platform. */
  100. #define KWSYS_SHARED_FORWARD_PATH_SEP ':'
  101. static const char kwsys_shared_forward_path_sep[2] = {KWSYS_SHARED_FORWARD_PATH_SEP, 0};
  102. /* The maximum length of a file name. */
  103. #if defined(PATH_MAX)
  104. # define KWSYS_SHARED_FORWARD_MAXPATH PATH_MAX
  105. #elif defined(MAXPATHLEN)
  106. # define KWSYS_SHARED_FORWARD_MAXPATH MAXPATHLEN
  107. #else
  108. # define KWSYS_SHARED_FORWARD_MAXPATH 16384
  109. #endif
  110. /* Select the environment variable holding the shared library runtime
  111. search path for this platform and build configuration. Also select
  112. ldd command equivalent. */
  113. /* Linux */
  114. #if defined(__linux)
  115. # define KWSYS_SHARED_FORWARD_LDD "ldd"
  116. # define KWSYS_SHARED_FORWARD_LDD_N 1
  117. # define KWSYS_SHARED_FORWARD_LDPATH "LD_LIBRARY_PATH"
  118. #endif
  119. /* OSX */
  120. #if defined(__APPLE__)
  121. # define KWSYS_SHARED_FORWARD_LDD "otool", "-L"
  122. # define KWSYS_SHARED_FORWARD_LDD_N 2
  123. # define KWSYS_SHARED_FORWARD_LDPATH "DYLD_LIBRARY_PATH"
  124. #endif
  125. /* AIX */
  126. #if defined(_AIX)
  127. # define KWSYS_SHARED_FORWARD_LDD "dump", "-H"
  128. # define KWSYS_SHARED_FORWARD_LDD_N 2
  129. # define KWSYS_SHARED_FORWARD_LDPATH "LIBPATH"
  130. #endif
  131. /* SUN */
  132. #if defined(__sparc)
  133. # define KWSYS_SHARED_FORWARD_LDD "ldd"
  134. # define KWSYS_SHARED_FORWARD_LDD_N 1
  135. # include <sys/isa_defs.h>
  136. # if defined(_ILP32)
  137. # define KWSYS_SHARED_FORWARD_LDPATH "LD_LIBRARY_PATH"
  138. # elif defined(_LP64)
  139. # define KWSYS_SHARED_FORWARD_LDPATH "LD_LIBRARY_PATH_64"
  140. # endif
  141. #endif
  142. /* HP-UX */
  143. #if defined(__hpux)
  144. # define KWSYS_SHARED_FORWARD_LDD "chatr"
  145. # define KWSYS_SHARED_FORWARD_LDD_N 1
  146. # if defined(__LP64__)
  147. # define KWSYS_SHARED_FORWARD_LDPATH "LD_LIBRARY_PATH"
  148. # else
  149. # define KWSYS_SHARED_FORWARD_LDPATH "SHLIB_PATH"
  150. # endif
  151. #endif
  152. /* SGI MIPS */
  153. #if defined(__sgi) && defined(_MIPS_SIM)
  154. # define KWSYS_SHARED_FORWARD_LDD "ldd"
  155. # define KWSYS_SHARED_FORWARD_LDD_N 1
  156. # if _MIPS_SIM == _ABIO32
  157. # define KWSYS_SHARED_FORWARD_LDPATH "LD_LIBRARY_PATH"
  158. # elif _MIPS_SIM == _ABIN32
  159. # define KWSYS_SHARED_FORWARD_LDPATH "LD_LIBRARYN32_PATH"
  160. # elif _MIPS_SIM == _ABI64
  161. # define KWSYS_SHARED_FORWARD_LDPATH "LD_LIBRARY64_PATH"
  162. # endif
  163. #endif
  164. /* Guess on this unknown system. */
  165. #if !defined(KWSYS_SHARED_FORWARD_LDPATH)
  166. # define KWSYS_SHARED_FORWARD_LDD "ldd"
  167. # define KWSYS_SHARED_FORWARD_LDD_N 1
  168. # define KWSYS_SHARED_FORWARD_LDPATH "LD_LIBRARY_PATH"
  169. #endif
  170. /*--------------------------------------------------------------------------*/
  171. /* Function to get the directory containing the given file or directory. */
  172. static void kwsys_shared_forward_dirname(const char* begin, char* result)
  173. {
  174. /* Find the location of the last slash. */
  175. int last_slash_index = -1;
  176. const char* end = begin + strlen(begin);
  177. for(;begin <= end && last_slash_index < 0; --end)
  178. {
  179. if(*end == '/')
  180. {
  181. last_slash_index = end-begin;
  182. }
  183. }
  184. /* Handle each case of the index of the last slash. */
  185. if(last_slash_index < 0)
  186. {
  187. /* No slashes. */
  188. strcpy(result, ".");
  189. }
  190. else if(last_slash_index == 0)
  191. {
  192. /* Only one leading slash. */
  193. strcpy(result, "/");
  194. }
  195. else
  196. {
  197. /* A non-leading slash. */
  198. strncpy(result, begin, last_slash_index);
  199. result[last_slash_index] = 0;
  200. }
  201. }
  202. /*--------------------------------------------------------------------------*/
  203. /* Function to locate the executable currently running. */
  204. static int kwsys_shared_forward_self_path(const char* argv0, char* result)
  205. {
  206. /* Check whether argv0 has a slash. */
  207. int has_slash = 0;
  208. const char* p = argv0;
  209. for(;*p && !has_slash; ++p)
  210. {
  211. if(*p == '/')
  212. {
  213. has_slash = 1;
  214. }
  215. }
  216. if(has_slash)
  217. {
  218. /* There is a slash. Use the dirname of the given location. */
  219. kwsys_shared_forward_dirname(argv0, result);
  220. return 1;
  221. }
  222. else
  223. {
  224. /* There is no slash. Search the PATH for the executable. */
  225. const char* path = getenv("PATH");
  226. const char* begin = path;
  227. const char* end = begin + (begin?strlen(begin):0);
  228. const char* first = begin;
  229. while(first != end)
  230. {
  231. /* Store the end of this path entry. */
  232. const char* last;
  233. /* Skip all path separators. */
  234. for(;*first && *first == KWSYS_SHARED_FORWARD_PATH_SEP; ++first);
  235. /* Find the next separator. */
  236. for(last = first;*last && *last != KWSYS_SHARED_FORWARD_PATH_SEP; ++last);
  237. /* If we got a non-empty directory, look for the executable there. */
  238. if(first < last)
  239. {
  240. /* Determine the length without trailing slash. */
  241. int length = last-first;
  242. if(*(last-1) == '/')
  243. {
  244. --length;
  245. }
  246. /* Construct the name of the executable in this location. */
  247. strncpy(result, first, length);
  248. result[length] = '/';
  249. strcpy(result+(length)+1, argv0);
  250. /* Check if it exists. */
  251. if(access(result, X_OK) == 0)
  252. {
  253. /* Found it. */
  254. result[length] = 0;
  255. return 1;
  256. }
  257. }
  258. /* Move to the next directory in the path. */
  259. first = last;
  260. }
  261. }
  262. /* We could not find the executable. */
  263. return 0;
  264. }
  265. /*--------------------------------------------------------------------------*/
  266. /* Function to convert a specified path to a full path. If it is not
  267. already full, it is taken relative to the self path. */
  268. static int kwsys_shared_forward_fullpath(const char* self_path,
  269. const char* in_path,
  270. char* result,
  271. const char* desc)
  272. {
  273. /* Check the specified path type. */
  274. if(in_path[0] == '/')
  275. {
  276. /* Already a full path. */
  277. strcpy(result, in_path);
  278. }
  279. else
  280. {
  281. /* Relative to self path. */
  282. char temp_path[KWSYS_SHARED_FORWARD_MAXPATH];
  283. strcpy(temp_path, self_path);
  284. strcat(temp_path, "/");
  285. strcat(temp_path, in_path);
  286. if(!realpath(temp_path, result))
  287. {
  288. if(desc)
  289. {
  290. fprintf(stderr, "Error converting %s \"%s\" to real path: %s\n",
  291. desc, temp_path, strerror(errno));
  292. }
  293. return 0;
  294. }
  295. }
  296. return 1;
  297. }
  298. /*--------------------------------------------------------------------------*/
  299. /* Function to compute the library search path and executable name
  300. based on the self path. */
  301. static int kwsys_shared_forward_get_settings(const char* self_path,
  302. char* ldpath, char* exe)
  303. {
  304. /* Possible search paths. */
  305. static const char* search_path_build[] = {KWSYS_SHARED_FORWARD_PATH_BUILD, 0};
  306. static const char* search_path_install[] = {KWSYS_SHARED_FORWARD_PATH_INSTALL, 0};
  307. /* Chosen paths. */
  308. const char** search_path;
  309. const char* exe_path;
  310. /* Get the real name of the build and self paths. */
  311. char build_path[KWSYS_SHARED_FORWARD_MAXPATH];
  312. char self_path_real[KWSYS_SHARED_FORWARD_MAXPATH];
  313. if(!realpath(self_path, self_path_real))
  314. {
  315. fprintf(stderr, "Error converting self path \"%s\" to real path: %s\n",
  316. self_path, strerror(errno));
  317. return 0;
  318. }
  319. /* Check whether we are running in the build tree or an install tree. */
  320. if(realpath(KWSYS_SHARED_FORWARD_DIR_BUILD, build_path) &&
  321. strcmp(self_path_real, build_path) == 0)
  322. {
  323. /* Running in build tree. Use the build path and exe. */
  324. search_path = search_path_build;
  325. exe_path = KWSYS_SHARED_FORWARD_EXE_BUILD;
  326. }
  327. else
  328. {
  329. /* Running in install tree. Use the install path and exe. */
  330. search_path = search_path_install;
  331. exe_path = KWSYS_SHARED_FORWARD_EXE_INSTALL;
  332. }
  333. /* Construct the runtime search path. */
  334. {
  335. const char** dir;
  336. for(dir = search_path; *dir; ++dir)
  337. {
  338. /* Add seperator between path components. */
  339. if(dir != search_path)
  340. {
  341. strcat(ldpath, kwsys_shared_forward_path_sep);
  342. }
  343. /* Add this path component. */
  344. if(!kwsys_shared_forward_fullpath(self_path, *dir,
  345. ldpath+strlen(ldpath),
  346. "runtime path entry"))
  347. {
  348. return 0;
  349. }
  350. }
  351. }
  352. /* Construct the executable location. */
  353. if(!kwsys_shared_forward_fullpath(self_path, exe_path, exe,
  354. "executable file"))
  355. {
  356. return 0;
  357. }
  358. return 1;
  359. }
  360. /*--------------------------------------------------------------------------*/
  361. /* Function to print why execution of a command line failed. */
  362. static void kwsys_shared_forward_print_failure(char** argv, const char* msg)
  363. {
  364. char** arg = argv;
  365. fprintf(stderr, "Error running");
  366. for(; *arg; ++arg)
  367. {
  368. fprintf(stderr, " \"%s\"", *arg);
  369. }
  370. fprintf(stderr, ": %s\n", msg);
  371. }
  372. /* Static storage space to store the updated environment variable. */
  373. static char kwsys_shared_forward_ldpath[KWSYS_SHARED_FORWARD_MAXPATH*16] = KWSYS_SHARED_FORWARD_LDPATH "=";
  374. /*--------------------------------------------------------------------------*/
  375. /* Main driver function to be called from main. */
  376. static int @KWSYS_NAMESPACE@_shared_forward_to_real(int argc, char** argv)
  377. {
  378. /* Get the directory containing this executable. */
  379. char self_path[KWSYS_SHARED_FORWARD_MAXPATH];
  380. if(kwsys_shared_forward_self_path(argv[0], self_path))
  381. {
  382. /* Found this executable. Use it to get the library directory. */
  383. char exe[KWSYS_SHARED_FORWARD_MAXPATH];
  384. if(kwsys_shared_forward_get_settings(self_path,
  385. kwsys_shared_forward_ldpath, exe))
  386. {
  387. /* Append the old runtime search path. */
  388. const char* old_ldpath = getenv(KWSYS_SHARED_FORWARD_LDPATH);
  389. if(old_ldpath)
  390. {
  391. strcat(kwsys_shared_forward_ldpath, kwsys_shared_forward_path_sep);
  392. strcat(kwsys_shared_forward_ldpath, old_ldpath);
  393. }
  394. /* Store the environment variable. */
  395. putenv(kwsys_shared_forward_ldpath);
  396. #if defined(KWSYS_SHARED_FORWARD_OPTION_PRINT)
  397. /* Look for the print command line option. */
  398. if(argc > 1 && strcmp(argv[1], KWSYS_SHARED_FORWARD_OPTION_PRINT) == 0)
  399. {
  400. fprintf(stdout, "%s\n", kwsys_shared_forward_ldpath);
  401. fprintf(stdout, "%s\n", exe);
  402. return 0;
  403. }
  404. #endif
  405. #if defined(KWSYS_SHARED_FORWARD_OPTION_LDD)
  406. /* Look for the ldd command line option. */
  407. if(argc > 1 && strcmp(argv[1], KWSYS_SHARED_FORWARD_OPTION_LDD) == 0)
  408. {
  409. char* ldd_argv[] = {KWSYS_SHARED_FORWARD_LDD, 0, 0};
  410. ldd_argv[KWSYS_SHARED_FORWARD_LDD_N] = exe;
  411. execvp(ldd_argv[0], ldd_argv);
  412. /* Report why execution failed. */
  413. kwsys_shared_forward_print_failure(ldd_argv, strerror(errno));
  414. return 1;
  415. }
  416. #endif
  417. /* Replace this process with the real executable. */
  418. argv[0] = exe;
  419. execv(argv[0], argv);
  420. /* Report why execution failed. */
  421. kwsys_shared_forward_print_failure(argv, strerror(errno));
  422. }
  423. else
  424. {
  425. /* Could not convert self path to the library directory. */
  426. }
  427. }
  428. else
  429. {
  430. /* Could not find this executable. */
  431. fprintf(stderr, "Error locating executable \"%s\".", argv[0]);
  432. }
  433. /* Avoid unused argument warning. */
  434. (void)argc;
  435. /* Exit with failure. */
  436. return 1;
  437. }
  438. #else
  439. # error "@KWSYS_NAMESPACE@/SharedForward.h should be included only once."
  440. #endif