SharedForward.h.in 29 KB

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