SharedForward.h 27 KB

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