SharedForward.h.in 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  1. /*============================================================================
  2. KWSys - Kitware System Library
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License 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 <stddef.h> /* size_t */
  129. #include <limits.h>
  130. #include <stdlib.h>
  131. #include <string.h>
  132. #include <errno.h>
  133. #include <stdio.h>
  134. #if defined(_WIN32) && !defined(__CYGWIN__)
  135. # include <io.h>
  136. # include <windows.h>
  137. # include <process.h>
  138. # define KWSYS_SHARED_FORWARD_ESCAPE_ARGV /* re-escape argv for execvp */
  139. #else
  140. # include <unistd.h>
  141. # include <sys/stat.h>
  142. #endif
  143. /*--------------------------------------------------------------------------*/
  144. /* Configuration for this platform. */
  145. /* The path separator for this platform. */
  146. #if defined(_WIN32) && !defined(__CYGWIN__)
  147. # define KWSYS_SHARED_FORWARD_PATH_SEP ';'
  148. # define KWSYS_SHARED_FORWARD_PATH_SLASH '\\'
  149. #else
  150. # define KWSYS_SHARED_FORWARD_PATH_SEP ':'
  151. # define KWSYS_SHARED_FORWARD_PATH_SLASH '/'
  152. #endif
  153. static const char kwsys_shared_forward_path_sep[2] = {KWSYS_SHARED_FORWARD_PATH_SEP, 0};
  154. static const char kwsys_shared_forward_path_slash[2] = {KWSYS_SHARED_FORWARD_PATH_SLASH, 0};
  155. /* The maximum length of a file name. */
  156. #if defined(PATH_MAX)
  157. # define KWSYS_SHARED_FORWARD_MAXPATH PATH_MAX
  158. #elif defined(MAXPATHLEN)
  159. # define KWSYS_SHARED_FORWARD_MAXPATH MAXPATHLEN
  160. #else
  161. # define KWSYS_SHARED_FORWARD_MAXPATH 16384
  162. #endif
  163. /* Select the environment variable holding the shared library runtime
  164. search path for this platform and build configuration. Also select
  165. ldd command equivalent. */
  166. /* Linux */
  167. #if defined(__linux)
  168. # define KWSYS_SHARED_FORWARD_LDD "ldd"
  169. # define KWSYS_SHARED_FORWARD_LDD_N 1
  170. # define KWSYS_SHARED_FORWARD_LDPATH "LD_LIBRARY_PATH"
  171. /* FreeBSD */
  172. #elif defined(__FreeBSD__)
  173. # define KWSYS_SHARED_FORWARD_LDD "ldd"
  174. # define KWSYS_SHARED_FORWARD_LDD_N 1
  175. # define KWSYS_SHARED_FORWARD_LDPATH "LD_LIBRARY_PATH"
  176. /* OpenBSD */
  177. #elif defined(__OpenBSD__)
  178. # define KWSYS_SHARED_FORWARD_LDD "ldd"
  179. # define KWSYS_SHARED_FORWARD_LDD_N 1
  180. # define KWSYS_SHARED_FORWARD_LDPATH "LD_LIBRARY_PATH"
  181. /* OSX */
  182. #elif defined(__APPLE__)
  183. # define KWSYS_SHARED_FORWARD_LDD "otool", "-L"
  184. # define KWSYS_SHARED_FORWARD_LDD_N 2
  185. # define KWSYS_SHARED_FORWARD_LDPATH "DYLD_LIBRARY_PATH"
  186. /* AIX */
  187. #elif defined(_AIX)
  188. # define KWSYS_SHARED_FORWARD_LDD "dump", "-H"
  189. # define KWSYS_SHARED_FORWARD_LDD_N 2
  190. # define KWSYS_SHARED_FORWARD_LDPATH "LIBPATH"
  191. /* SUN */
  192. #elif defined(__sun)
  193. # define KWSYS_SHARED_FORWARD_LDD "ldd"
  194. # define KWSYS_SHARED_FORWARD_LDD_N 1
  195. # include <sys/isa_defs.h>
  196. # if defined(_ILP32)
  197. # define KWSYS_SHARED_FORWARD_LDPATH "LD_LIBRARY_PATH"
  198. # elif defined(_LP64)
  199. # define KWSYS_SHARED_FORWARD_LDPATH "LD_LIBRARY_PATH_64"
  200. # endif
  201. /* HP-UX */
  202. #elif defined(__hpux)
  203. # define KWSYS_SHARED_FORWARD_LDD "chatr"
  204. # define KWSYS_SHARED_FORWARD_LDD_N 1
  205. # if defined(__LP64__)
  206. # define KWSYS_SHARED_FORWARD_LDPATH "LD_LIBRARY_PATH"
  207. # else
  208. # define KWSYS_SHARED_FORWARD_LDPATH "SHLIB_PATH"
  209. # endif
  210. /* SGI MIPS */
  211. #elif defined(__sgi) && defined(_MIPS_SIM)
  212. # define KWSYS_SHARED_FORWARD_LDD "ldd"
  213. # define KWSYS_SHARED_FORWARD_LDD_N 1
  214. # if _MIPS_SIM == _ABIO32
  215. # define KWSYS_SHARED_FORWARD_LDPATH "LD_LIBRARY_PATH"
  216. # elif _MIPS_SIM == _ABIN32
  217. # define KWSYS_SHARED_FORWARD_LDPATH "LD_LIBRARYN32_PATH"
  218. # elif _MIPS_SIM == _ABI64
  219. # define KWSYS_SHARED_FORWARD_LDPATH "LD_LIBRARY64_PATH"
  220. # endif
  221. /* Cygwin */
  222. #elif defined(__CYGWIN__)
  223. # define KWSYS_SHARED_FORWARD_LDD "cygcheck" /* TODO: cygwin 1.7 has ldd */
  224. # define KWSYS_SHARED_FORWARD_LDD_N 1
  225. # define KWSYS_SHARED_FORWARD_LDPATH "PATH"
  226. /* Windows */
  227. #elif defined(_WIN32)
  228. # define KWSYS_SHARED_FORWARD_LDPATH "PATH"
  229. /* Guess on this unknown system. */
  230. #else
  231. # define KWSYS_SHARED_FORWARD_LDD "ldd"
  232. # define KWSYS_SHARED_FORWARD_LDD_N 1
  233. # define KWSYS_SHARED_FORWARD_LDPATH "LD_LIBRARY_PATH"
  234. #endif
  235. #ifdef KWSYS_SHARED_FORWARD_ESCAPE_ARGV
  236. /*--------------------------------------------------------------------------*/
  237. typedef struct kwsys_sf_arg_info_s
  238. {
  239. const char* arg;
  240. int size;
  241. int quote;
  242. } kwsys_sf_arg_info;
  243. /*--------------------------------------------------------------------------*/
  244. static kwsys_sf_arg_info kwsys_sf_get_arg_info(const char* in)
  245. {
  246. /* Initialize information. */
  247. kwsys_sf_arg_info info;
  248. /* String iterator. */
  249. const char* c;
  250. /* Keep track of how many backslashes have been encountered in a row. */
  251. int windows_backslashes = 0;
  252. /* Start with the length of the original argument, plus one for
  253. either a terminating null or a separating space. */
  254. info.arg = in;
  255. info.size = (int)strlen(in) + 1;
  256. info.quote = 0;
  257. /* Scan the string for characters that require escaping or quoting. */
  258. for(c=in; *c; ++c)
  259. {
  260. /* Check whether this character needs quotes. */
  261. if(strchr(" \t?'#&<>|^", *c))
  262. {
  263. info.quote = 1;
  264. }
  265. /* On Windows only backslashes and double-quotes need escaping. */
  266. if(*c == '\\')
  267. {
  268. /* Found a backslash. It may need to be escaped later. */
  269. ++windows_backslashes;
  270. }
  271. else if(*c == '"')
  272. {
  273. /* Found a double-quote. We need to escape it and all
  274. immediately preceding backslashes. */
  275. info.size += windows_backslashes + 1;
  276. windows_backslashes = 0;
  277. }
  278. else
  279. {
  280. /* Found another character. This eliminates the possibility
  281. that any immediately preceding backslashes will be
  282. escaped. */
  283. windows_backslashes = 0;
  284. }
  285. }
  286. /* Check whether the argument needs surrounding quotes. */
  287. if(info.quote)
  288. {
  289. /* Surrounding quotes are needed. Allocate space for them. */
  290. info.size += 2;
  291. /* We must escape all ending backslashes when quoting on windows. */
  292. info.size += windows_backslashes;
  293. }
  294. return info;
  295. }
  296. /*--------------------------------------------------------------------------*/
  297. static char* kwsys_sf_get_arg(kwsys_sf_arg_info info, char* out)
  298. {
  299. /* String iterator. */
  300. const char* c;
  301. /* Keep track of how many backslashes have been encountered in a row. */
  302. int windows_backslashes = 0;
  303. /* Whether the argument must be quoted. */
  304. if(info.quote)
  305. {
  306. /* Add the opening quote for this argument. */
  307. *out++ = '"';
  308. }
  309. /* Scan the string for characters that require escaping or quoting. */
  310. for(c=info.arg; *c; ++c)
  311. {
  312. /* On Windows only backslashes and double-quotes need escaping. */
  313. if(*c == '\\')
  314. {
  315. /* Found a backslash. It may need to be escaped later. */
  316. ++windows_backslashes;
  317. }
  318. else if(*c == '"')
  319. {
  320. /* Found a double-quote. Escape all immediately preceding
  321. backslashes. */
  322. while(windows_backslashes > 0)
  323. {
  324. --windows_backslashes;
  325. *out++ = '\\';
  326. }
  327. /* Add the backslash to escape the double-quote. */
  328. *out++ = '\\';
  329. }
  330. else
  331. {
  332. /* We encountered a normal character. This eliminates any
  333. escaping needed for preceding backslashes. */
  334. windows_backslashes = 0;
  335. }
  336. /* Store this character. */
  337. *out++ = *c;
  338. }
  339. if(info.quote)
  340. {
  341. /* Add enough backslashes to escape any trailing ones. */
  342. while(windows_backslashes > 0)
  343. {
  344. --windows_backslashes;
  345. *out++ = '\\';
  346. }
  347. /* Add the closing quote for this argument. */
  348. *out++ = '"';
  349. }
  350. /* Store a terminating null without incrementing. */
  351. *out = 0;
  352. return out;
  353. }
  354. #endif
  355. /*--------------------------------------------------------------------------*/
  356. /* Function to convert a logical or relative path to a physical full path. */
  357. static int kwsys_shared_forward_realpath(const char* in_path, char* out_path)
  358. {
  359. #if defined(_WIN32) && !defined(__CYGWIN__)
  360. /* Implementation for Windows. */
  361. DWORD n = GetFullPathName(in_path, KWSYS_SHARED_FORWARD_MAXPATH,
  362. out_path, 0);
  363. return n > 0 && n <= KWSYS_SHARED_FORWARD_MAXPATH;
  364. #else
  365. /* Implementation for UNIX. */
  366. return realpath(in_path, out_path) != 0;
  367. #endif
  368. }
  369. /*--------------------------------------------------------------------------*/
  370. static int kwsys_shared_forward_samepath(const char* file1, const char* file2)
  371. {
  372. #if defined(_WIN32)
  373. int result = 0;
  374. HANDLE h1 = CreateFile(file1, GENERIC_READ, FILE_SHARE_READ, NULL,
  375. OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
  376. HANDLE h2 = CreateFile(file2, GENERIC_READ, FILE_SHARE_READ, NULL,
  377. OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
  378. if(h1 != INVALID_HANDLE_VALUE && h2 != INVALID_HANDLE_VALUE)
  379. {
  380. BY_HANDLE_FILE_INFORMATION fi1;
  381. BY_HANDLE_FILE_INFORMATION fi2;
  382. GetFileInformationByHandle(h1, &fi1);
  383. GetFileInformationByHandle(h2, &fi2);
  384. result = (fi1.dwVolumeSerialNumber == fi2.dwVolumeSerialNumber &&
  385. fi1.nFileIndexHigh == fi2.nFileIndexHigh &&
  386. fi1.nFileIndexLow == fi2.nFileIndexLow);
  387. }
  388. CloseHandle(h1);
  389. CloseHandle(h2);
  390. return result;
  391. #else
  392. struct stat fs1, fs2;
  393. return (stat(file1, &fs1) == 0 && stat(file2, &fs2) == 0 &&
  394. memcmp(&fs2.st_dev, &fs1.st_dev, sizeof(fs1.st_dev)) == 0 &&
  395. memcmp(&fs2.st_ino, &fs1.st_ino, sizeof(fs1.st_ino)) == 0 &&
  396. fs2.st_size == fs1.st_size);
  397. #endif
  398. }
  399. /*--------------------------------------------------------------------------*/
  400. /* Function to report a system error message. */
  401. static void kwsys_shared_forward_strerror(char* message)
  402. {
  403. #if defined(_WIN32) && !defined(__CYGWIN__)
  404. /* Implementation for Windows. */
  405. DWORD original = GetLastError();
  406. DWORD length = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
  407. FORMAT_MESSAGE_IGNORE_INSERTS, 0, original,
  408. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  409. message, KWSYS_SHARED_FORWARD_MAXPATH, 0);
  410. if(length < 1 || length > KWSYS_SHARED_FORWARD_MAXPATH)
  411. {
  412. /* FormatMessage failed. Use a default message. */
  413. _snprintf(message, KWSYS_SHARED_FORWARD_MAXPATH,
  414. "Error 0x%X (FormatMessage failed with error 0x%X)",
  415. original, GetLastError());
  416. }
  417. #else
  418. /* Implementation for UNIX. */
  419. strcpy(message, strerror(errno));
  420. #endif
  421. }
  422. /*--------------------------------------------------------------------------*/
  423. /* Functions to execute a child process. */
  424. static void kwsys_shared_forward_execvp(const char* cmd,
  425. char const* const* argv)
  426. {
  427. #ifdef KWSYS_SHARED_FORWARD_ESCAPE_ARGV
  428. /* Count the number of arguments. */
  429. int argc = 0;
  430. {
  431. char const* const* argvc;
  432. for(argvc = argv; *argvc; ++argvc,++argc) {}
  433. }
  434. /* Create the escaped arguments. */
  435. {
  436. char** nargv = (char**)malloc((argc+1) * sizeof(char*));
  437. int i;
  438. for(i=0; i < argc; ++i)
  439. {
  440. kwsys_sf_arg_info info = kwsys_sf_get_arg_info(argv[i]);
  441. nargv[i] = (char*)malloc(info.size);
  442. kwsys_sf_get_arg(info, nargv[i]);
  443. }
  444. nargv[argc] = 0;
  445. /* Replace the command line to be used. */
  446. argv = (char const* const*)nargv;
  447. }
  448. #endif
  449. /* Invoke the child process. */
  450. #if defined(_MSC_VER)
  451. _execvp(cmd, argv);
  452. #elif defined(__MINGW32__)
  453. execvp(cmd, argv);
  454. #else
  455. execvp(cmd, (char* const*)argv);
  456. #endif
  457. }
  458. /*--------------------------------------------------------------------------*/
  459. /* Function to get the directory containing the given file or directory. */
  460. static void kwsys_shared_forward_dirname(const char* begin, char* result)
  461. {
  462. /* Find the location of the last slash. */
  463. int last_slash_index = -1;
  464. const char* end = begin + strlen(begin);
  465. for(;begin <= end && last_slash_index < 0; --end)
  466. {
  467. if(*end == '/' || *end == '\\')
  468. {
  469. last_slash_index = (int)(end-begin);
  470. }
  471. }
  472. /* Handle each case of the index of the last slash. */
  473. if(last_slash_index < 0)
  474. {
  475. /* No slashes. */
  476. strcpy(result, ".");
  477. }
  478. else if(last_slash_index == 0)
  479. {
  480. /* Only one leading slash. */
  481. strcpy(result, kwsys_shared_forward_path_slash);
  482. }
  483. #if defined(_WIN32)
  484. else if(last_slash_index == 2 && begin[1] == ':')
  485. {
  486. /* Only one leading drive letter and slash. */
  487. strncpy(result, begin, (size_t)last_slash_index);
  488. result[last_slash_index] = KWSYS_SHARED_FORWARD_PATH_SLASH;
  489. result[last_slash_index+1] = 0;
  490. }
  491. #endif
  492. else
  493. {
  494. /* A non-leading slash. */
  495. strncpy(result, begin, (size_t)last_slash_index);
  496. result[last_slash_index] = 0;
  497. }
  498. }
  499. /*--------------------------------------------------------------------------*/
  500. /* Function to check if a file exists and is executable. */
  501. static int kwsys_shared_forward_is_executable(const char* f)
  502. {
  503. #if defined(_MSC_VER)
  504. # define KWSYS_SHARED_FORWARD_ACCESS _access
  505. #else
  506. # define KWSYS_SHARED_FORWARD_ACCESS access
  507. #endif
  508. #if defined(X_OK)
  509. # define KWSYS_SHARED_FORWARD_ACCESS_OK X_OK
  510. #else
  511. # define KWSYS_SHARED_FORWARD_ACCESS_OK 04
  512. #endif
  513. if(KWSYS_SHARED_FORWARD_ACCESS(f, KWSYS_SHARED_FORWARD_ACCESS_OK) == 0)
  514. {
  515. return 1;
  516. }
  517. else
  518. {
  519. return 0;
  520. }
  521. }
  522. /*--------------------------------------------------------------------------*/
  523. /* Function to locate the executable currently running. */
  524. static int kwsys_shared_forward_self_path(const char* argv0, char* result)
  525. {
  526. /* Check whether argv0 has a slash. */
  527. int has_slash = 0;
  528. const char* p = argv0;
  529. for(;*p && !has_slash; ++p)
  530. {
  531. if(*p == '/' || *p == '\\')
  532. {
  533. has_slash = 1;
  534. }
  535. }
  536. if(has_slash)
  537. {
  538. /* There is a slash. Use the dirname of the given location. */
  539. kwsys_shared_forward_dirname(argv0, result);
  540. return 1;
  541. }
  542. else
  543. {
  544. /* There is no slash. Search the PATH for the executable. */
  545. const char* path = getenv("PATH");
  546. const char* begin = path;
  547. const char* end = begin + (begin?strlen(begin):0);
  548. const char* first = begin;
  549. while(first != end)
  550. {
  551. /* Store the end of this path entry. */
  552. const char* last;
  553. /* Skip all path separators. */
  554. for(;*first && *first == KWSYS_SHARED_FORWARD_PATH_SEP; ++first);
  555. /* Find the next separator. */
  556. for(last = first;*last && *last != KWSYS_SHARED_FORWARD_PATH_SEP; ++last);
  557. /* If we got a non-empty directory, look for the executable there. */
  558. if(first < last)
  559. {
  560. /* Determine the length without trailing slash. */
  561. size_t length = (size_t)(last-first);
  562. if(*(last-1) == '/' || *(last-1) == '\\')
  563. {
  564. --length;
  565. }
  566. /* Construct the name of the executable in this location. */
  567. strncpy(result, first, length);
  568. result[length] = KWSYS_SHARED_FORWARD_PATH_SLASH;
  569. strcpy(result+(length)+1, argv0);
  570. /* Check if it exists and is executable. */
  571. if(kwsys_shared_forward_is_executable(result))
  572. {
  573. /* Found it. */
  574. result[length] = 0;
  575. return 1;
  576. }
  577. }
  578. /* Move to the next directory in the path. */
  579. first = last;
  580. }
  581. }
  582. /* We could not find the executable. */
  583. return 0;
  584. }
  585. /*--------------------------------------------------------------------------*/
  586. /* Function to convert a specified path to a full path. If it is not
  587. already full, it is taken relative to the self path. */
  588. static int kwsys_shared_forward_fullpath(const char* self_path,
  589. const char* in_path,
  590. char* result,
  591. const char* desc)
  592. {
  593. /* Check the specified path type. */
  594. if(in_path[0] == '/')
  595. {
  596. /* Already a full path. */
  597. strcpy(result, in_path);
  598. }
  599. #if defined(_WIN32)
  600. else if(in_path[0] && in_path[1] == ':')
  601. {
  602. /* Already a full path. */
  603. strcpy(result, in_path);
  604. }
  605. #endif
  606. else
  607. {
  608. /* Relative to self path. */
  609. char temp_path[KWSYS_SHARED_FORWARD_MAXPATH];
  610. strcpy(temp_path, self_path);
  611. strcat(temp_path, kwsys_shared_forward_path_slash);
  612. strcat(temp_path, in_path);
  613. if(!kwsys_shared_forward_realpath(temp_path, result))
  614. {
  615. if(desc)
  616. {
  617. char msgbuf[KWSYS_SHARED_FORWARD_MAXPATH];
  618. kwsys_shared_forward_strerror(msgbuf);
  619. fprintf(stderr, "Error converting %s \"%s\" to real path: %s\n",
  620. desc, temp_path, msgbuf);
  621. }
  622. return 0;
  623. }
  624. }
  625. return 1;
  626. }
  627. /*--------------------------------------------------------------------------*/
  628. /* Function to compute the library search path and executable name
  629. based on the self path. */
  630. static int kwsys_shared_forward_get_settings(const char* self_path,
  631. char* ldpath, char* exe)
  632. {
  633. /* Possible search paths. */
  634. static const char* search_path_build[] = {KWSYS_SHARED_FORWARD_PATH_BUILD, 0};
  635. static const char* search_path_install[] = {KWSYS_SHARED_FORWARD_PATH_INSTALL, 0};
  636. /* Chosen paths. */
  637. const char** search_path;
  638. const char* exe_path;
  639. /* Get the real name of the build and self paths. */
  640. #if defined(KWSYS_SHARED_FORWARD_CONFIG_NAME)
  641. char build_path[] = KWSYS_SHARED_FORWARD_DIR_BUILD "/" KWSYS_SHARED_FORWARD_CONFIG_NAME;
  642. char self_path_logical[KWSYS_SHARED_FORWARD_MAXPATH];
  643. #else
  644. char build_path[] = KWSYS_SHARED_FORWARD_DIR_BUILD;
  645. const char* self_path_logical = self_path;
  646. #endif
  647. char build_path_real[KWSYS_SHARED_FORWARD_MAXPATH];
  648. char self_path_real[KWSYS_SHARED_FORWARD_MAXPATH];
  649. if(!kwsys_shared_forward_realpath(self_path, self_path_real))
  650. {
  651. char msgbuf[KWSYS_SHARED_FORWARD_MAXPATH];
  652. kwsys_shared_forward_strerror(msgbuf);
  653. fprintf(stderr, "Error converting self path \"%s\" to real path: %s\n",
  654. self_path, msgbuf);
  655. return 0;
  656. }
  657. /* Check whether we are running in the build tree or an install tree. */
  658. if(kwsys_shared_forward_realpath(build_path, build_path_real) &&
  659. kwsys_shared_forward_samepath(self_path_real, build_path_real))
  660. {
  661. /* Running in build tree. Use the build path and exe. */
  662. search_path = search_path_build;
  663. #if defined(_WIN32)
  664. exe_path = KWSYS_SHARED_FORWARD_EXE_BUILD ".exe";
  665. #else
  666. exe_path = KWSYS_SHARED_FORWARD_EXE_BUILD;
  667. #endif
  668. #if defined(KWSYS_SHARED_FORWARD_CONFIG_NAME)
  669. /* Remove the configuration directory from self_path. */
  670. kwsys_shared_forward_dirname(self_path, self_path_logical);
  671. #endif
  672. }
  673. else
  674. {
  675. /* Running in install tree. Use the install path and exe. */
  676. search_path = search_path_install;
  677. #if defined(_WIN32)
  678. exe_path = KWSYS_SHARED_FORWARD_EXE_INSTALL ".exe";
  679. #else
  680. exe_path = KWSYS_SHARED_FORWARD_EXE_INSTALL;
  681. #endif
  682. #if defined(KWSYS_SHARED_FORWARD_CONFIG_NAME)
  683. /* Use the original self path directory. */
  684. strcpy(self_path_logical, self_path);
  685. #endif
  686. }
  687. /* Construct the runtime search path. */
  688. {
  689. const char** dir;
  690. for(dir = search_path; *dir; ++dir)
  691. {
  692. /* Add seperator between path components. */
  693. if(dir != search_path)
  694. {
  695. strcat(ldpath, kwsys_shared_forward_path_sep);
  696. }
  697. /* Add this path component. */
  698. if(!kwsys_shared_forward_fullpath(self_path_logical, *dir,
  699. ldpath+strlen(ldpath),
  700. "runtime path entry"))
  701. {
  702. return 0;
  703. }
  704. }
  705. }
  706. /* Construct the executable location. */
  707. if(!kwsys_shared_forward_fullpath(self_path_logical, exe_path, exe,
  708. "executable file"))
  709. {
  710. return 0;
  711. }
  712. return 1;
  713. }
  714. /*--------------------------------------------------------------------------*/
  715. /* Function to print why execution of a command line failed. */
  716. static void kwsys_shared_forward_print_failure(char const* const* argv)
  717. {
  718. char msg[KWSYS_SHARED_FORWARD_MAXPATH];
  719. char const* const* arg = argv;
  720. kwsys_shared_forward_strerror(msg);
  721. fprintf(stderr, "Error running");
  722. for(; *arg; ++arg)
  723. {
  724. fprintf(stderr, " \"%s\"", *arg);
  725. }
  726. fprintf(stderr, ": %s\n", msg);
  727. }
  728. /* Static storage space to store the updated environment variable. */
  729. static char kwsys_shared_forward_ldpath[KWSYS_SHARED_FORWARD_MAXPATH*16] = KWSYS_SHARED_FORWARD_LDPATH "=";
  730. /*--------------------------------------------------------------------------*/
  731. /* Main driver function to be called from main. */
  732. static int @KWSYS_NAMESPACE@_shared_forward_to_real(int argc, char** argv_in)
  733. {
  734. char const** argv = (char const**)argv_in;
  735. /* Get the directory containing this executable. */
  736. char self_path[KWSYS_SHARED_FORWARD_MAXPATH];
  737. if(kwsys_shared_forward_self_path(argv[0], self_path))
  738. {
  739. /* Found this executable. Use it to get the library directory. */
  740. char exe[KWSYS_SHARED_FORWARD_MAXPATH];
  741. if(kwsys_shared_forward_get_settings(self_path,
  742. kwsys_shared_forward_ldpath, exe))
  743. {
  744. /* Append the old runtime search path. */
  745. const char* old_ldpath = getenv(KWSYS_SHARED_FORWARD_LDPATH);
  746. if(old_ldpath)
  747. {
  748. strcat(kwsys_shared_forward_ldpath, kwsys_shared_forward_path_sep);
  749. strcat(kwsys_shared_forward_ldpath, old_ldpath);
  750. }
  751. /* Store the environment variable. */
  752. putenv(kwsys_shared_forward_ldpath);
  753. #if defined(KWSYS_SHARED_FORWARD_OPTION_COMMAND)
  754. /* Look for the command line replacement option. */
  755. if(argc > 1 && strcmp(argv[1], KWSYS_SHARED_FORWARD_OPTION_COMMAND) == 0)
  756. {
  757. if(argc > 2)
  758. {
  759. /* Use the command line given. */
  760. strcpy(exe, argv[2]);
  761. argv += 2;
  762. argc -= 2;
  763. }
  764. else
  765. {
  766. /* The option was not given an executable. */
  767. fprintf(stderr, "Option " KWSYS_SHARED_FORWARD_OPTION_COMMAND
  768. " must be followed by a command line.\n");
  769. return 1;
  770. }
  771. }
  772. #endif
  773. #if defined(KWSYS_SHARED_FORWARD_OPTION_PRINT)
  774. /* Look for the print command line option. */
  775. if(argc > 1 && strcmp(argv[1], KWSYS_SHARED_FORWARD_OPTION_PRINT) == 0)
  776. {
  777. fprintf(stdout, "%s\n", kwsys_shared_forward_ldpath);
  778. fprintf(stdout, "%s\n", exe);
  779. return 0;
  780. }
  781. #endif
  782. #if defined(KWSYS_SHARED_FORWARD_OPTION_LDD)
  783. /* Look for the ldd command line option. */
  784. if(argc > 1 && strcmp(argv[1], KWSYS_SHARED_FORWARD_OPTION_LDD) == 0)
  785. {
  786. # if defined(KWSYS_SHARED_FORWARD_LDD)
  787. /* Use the named ldd-like executable and arguments. */
  788. char const* ldd_argv[] = {KWSYS_SHARED_FORWARD_LDD, 0, 0};
  789. ldd_argv[KWSYS_SHARED_FORWARD_LDD_N] = exe;
  790. kwsys_shared_forward_execvp(ldd_argv[0], ldd_argv);
  791. /* Report why execution failed. */
  792. kwsys_shared_forward_print_failure(ldd_argv);
  793. return 1;
  794. # else
  795. /* We have no ldd-like executable available on this platform. */
  796. fprintf(stderr, "No ldd-like tool is known to this executable.\n");
  797. return 1;
  798. # endif
  799. }
  800. #endif
  801. /* Replace this process with the real executable. */
  802. argv[0] = exe;
  803. kwsys_shared_forward_execvp(argv[0], argv);
  804. /* Report why execution failed. */
  805. kwsys_shared_forward_print_failure(argv);
  806. }
  807. else
  808. {
  809. /* Could not convert self path to the library directory. */
  810. }
  811. }
  812. else
  813. {
  814. /* Could not find this executable. */
  815. fprintf(stderr, "Error locating executable \"%s\".\n", argv[0]);
  816. }
  817. /* Avoid unused argument warning. */
  818. (void)argc;
  819. /* Exit with failure. */
  820. return 1;
  821. }
  822. #else
  823. # error "@KWSYS_NAMESPACE@/SharedForward.h should be included only once."
  824. #endif