SharedForward.h.in 28 KB

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