SharedForward.h.in 29 KB

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