SharedForward.h.in 30 KB

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