SharedForward.h.in 29 KB

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