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