kwsysPlatformTestsCXX.cxx 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*============================================================================
  2. KWSys - Kitware System Library
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #ifdef TEST_KWSYS_CXX_HAS_CSTDIO
  11. #include <cstdio>
  12. int main() { return 0; }
  13. #endif
  14. #ifdef TEST_KWSYS_CXX_HAS_LONG_LONG
  15. long long f(long long n) { return n; }
  16. int main()
  17. {
  18. long long n = 0;
  19. return static_cast<int>(f(n));
  20. }
  21. #endif
  22. #ifdef TEST_KWSYS_CXX_HAS___INT64
  23. __int64 f(__int64 n) { return n; }
  24. int main()
  25. {
  26. __int64 n = 0;
  27. return static_cast<int>(f(n));
  28. }
  29. #endif
  30. #ifdef TEST_KWSYS_CXX_STAT_HAS_ST_MTIM
  31. #include <sys/types.h>
  32. #include <sys/stat.h>
  33. #include <unistd.h>
  34. int main()
  35. {
  36. struct stat stat1;
  37. (void)stat1.st_mtim.tv_sec;
  38. (void)stat1.st_mtim.tv_nsec;
  39. return 0;
  40. }
  41. #endif
  42. #ifdef TEST_KWSYS_CXX_STAT_HAS_ST_MTIMESPEC
  43. #include <sys/types.h>
  44. #include <sys/stat.h>
  45. #include <unistd.h>
  46. int main()
  47. {
  48. struct stat stat1;
  49. (void)stat1.st_mtimespec.tv_sec;
  50. (void)stat1.st_mtimespec.tv_nsec;
  51. return 0;
  52. }
  53. #endif
  54. #ifdef TEST_KWSYS_CXX_SAME_LONG_AND___INT64
  55. void function(long**) {}
  56. int main()
  57. {
  58. __int64** p = 0;
  59. function(p);
  60. return 0;
  61. }
  62. #endif
  63. #ifdef TEST_KWSYS_CXX_SAME_LONG_LONG_AND___INT64
  64. void function(long long**) {}
  65. int main()
  66. {
  67. __int64** p = 0;
  68. function(p);
  69. return 0;
  70. }
  71. #endif
  72. #ifdef TEST_KWSYS_CAN_CONVERT_UI64_TO_DOUBLE
  73. void function(double& l, unsigned __int64 const& r)
  74. {
  75. l = static_cast<double>(r);
  76. }
  77. int main()
  78. {
  79. double tTo = 0.0;
  80. unsigned __int64 tFrom = 0;
  81. function(tTo, tFrom);
  82. return 0;
  83. }
  84. #endif
  85. #ifdef TEST_KWSYS_IOS_HAS_ISTREAM_LONG_LONG
  86. # include <iostream>
  87. int test_istream(std::istream& is, long long& x)
  88. {
  89. return (is >> x)? 1:0;
  90. }
  91. int main()
  92. {
  93. long long x = 0;
  94. return test_istream(std::cin, x);
  95. }
  96. #endif
  97. #ifdef TEST_KWSYS_IOS_HAS_OSTREAM_LONG_LONG
  98. # include <iostream>
  99. int test_ostream(std::ostream& os, long long x)
  100. {
  101. return (os << x)? 1:0;
  102. }
  103. int main()
  104. {
  105. long long x = 0;
  106. return test_ostream(std::cout, x);
  107. }
  108. #endif
  109. #ifdef TEST_KWSYS_IOS_HAS_ISTREAM___INT64
  110. # include <iostream>
  111. int test_istream(std::istream& is, __int64& x)
  112. {
  113. return (is >> x)? 1:0;
  114. }
  115. int main()
  116. {
  117. __int64 x = 0;
  118. return test_istream(std::cin, x);
  119. }
  120. #endif
  121. #ifdef TEST_KWSYS_IOS_HAS_OSTREAM___INT64
  122. # include <iostream>
  123. int test_ostream(std::ostream& os, __int64 x)
  124. {
  125. return (os << x)? 1:0;
  126. }
  127. int main()
  128. {
  129. __int64 x = 0;
  130. return test_ostream(std::cout, x);
  131. }
  132. #endif
  133. #ifdef TEST_KWSYS_CHAR_IS_SIGNED
  134. /* Return 0 for char signed and 1 for char unsigned. */
  135. int main()
  136. {
  137. unsigned char uc = 255;
  138. return (*reinterpret_cast<char*>(&uc) < 0)?0:1;
  139. }
  140. #endif
  141. #ifdef TEST_KWSYS_LFS_WORKS
  142. /* Return 0 when LFS is available and 1 otherwise. */
  143. #define _LARGEFILE_SOURCE
  144. #define _LARGEFILE64_SOURCE
  145. #define _LARGE_FILES
  146. #define _FILE_OFFSET_BITS 64
  147. #include <sys/types.h>
  148. #include <sys/stat.h>
  149. #include <assert.h>
  150. #if KWSYS_CXX_HAS_CSTDIO
  151. # include <cstdio>
  152. #endif
  153. #include <stdio.h>
  154. int main(int, char **argv)
  155. {
  156. /* check that off_t can hold 2^63 - 1 and perform basic operations... */
  157. #define OFF_T_64 (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62))
  158. if (OFF_T_64 % 2147483647 != 1)
  159. return 1;
  160. // stat breaks on SCO OpenServer
  161. struct stat buf;
  162. stat( argv[0], &buf );
  163. if (!S_ISREG(buf.st_mode))
  164. return 2;
  165. FILE *file = fopen( argv[0], "r" );
  166. off_t offset = ftello( file );
  167. fseek( file, offset, SEEK_CUR );
  168. fclose( file );
  169. return 0;
  170. }
  171. #endif
  172. #ifdef TEST_KWSYS_CXX_HAS_SETENV
  173. #include <stdlib.h>
  174. int main()
  175. {
  176. return setenv("A", "B", 1);
  177. }
  178. #endif
  179. #ifdef TEST_KWSYS_CXX_HAS_UNSETENV
  180. #include <stdlib.h>
  181. int main()
  182. {
  183. unsetenv("A");
  184. return 0;
  185. }
  186. #endif
  187. #ifdef TEST_KWSYS_CXX_HAS_ENVIRON_IN_STDLIB_H
  188. #include <stdlib.h>
  189. int main()
  190. {
  191. char* e = environ[0];
  192. return e? 0:1;
  193. }
  194. #endif
  195. #ifdef TEST_KWSYS_CXX_HAS_GETLOADAVG
  196. // Match feature definitions from SystemInformation.cxx
  197. #if (defined(__GNUC__) || defined(__PGI)) && !defined(_GNU_SOURCE)
  198. # define _GNU_SOURCE
  199. #endif
  200. #include <stdlib.h>
  201. int main()
  202. {
  203. double loadavg[3] = { 0.0, 0.0, 0.0 };
  204. return getloadavg(loadavg, 3);
  205. }
  206. #endif
  207. #ifdef TEST_KWSYS_CXX_HAS_RLIMIT64
  208. # if defined(KWSYS_HAS_LFS)
  209. # define _LARGEFILE_SOURCE
  210. # define _LARGEFILE64_SOURCE
  211. # define _LARGE_FILES
  212. # define _FILE_OFFSET_BITS 64
  213. # endif
  214. # include <sys/resource.h>
  215. int main()
  216. {
  217. struct rlimit64 rlim;
  218. return getrlimit64(0,&rlim);
  219. }
  220. #endif
  221. #ifdef TEST_KWSYS_CXX_HAS_ATOLL
  222. #include <stdlib.h>
  223. int main()
  224. {
  225. const char *str="1024";
  226. return static_cast<int>(atoll(str));
  227. }
  228. #endif
  229. #ifdef TEST_KWSYS_CXX_HAS_ATOL
  230. #include <stdlib.h>
  231. int main()
  232. {
  233. const char *str="1024";
  234. return static_cast<int>(atol(str));
  235. }
  236. #endif
  237. #ifdef TEST_KWSYS_CXX_HAS__ATOI64
  238. #include <stdlib.h>
  239. int main()
  240. {
  241. const char *str="1024";
  242. return static_cast<int>(_atoi64(str));
  243. }
  244. #endif
  245. #ifdef TEST_KWSYS_CXX_HAS_UTIMES
  246. #include <sys/time.h>
  247. int main()
  248. {
  249. struct timeval* current_time = 0;
  250. return utimes("/example", current_time);
  251. }
  252. #endif
  253. #ifdef TEST_KWSYS_CXX_HAS_UTIMENSAT
  254. #include <fcntl.h>
  255. #include <sys/stat.h>
  256. int main()
  257. {
  258. struct timespec times[2] = {{0,UTIME_OMIT},{0,UTIME_NOW}};
  259. return utimensat(AT_FDCWD, "/example", times, AT_SYMLINK_NOFOLLOW);
  260. }
  261. #endif
  262. #ifdef TEST_KWSYS_CXX_HAS_BACKTRACE
  263. #if defined(__PATHSCALE__) || defined(__PATHCC__) \
  264. || (defined(__LSB_VERSION__) && (__LSB_VERSION__ < 41))
  265. backtrace doesnt work with this compiler or os
  266. #endif
  267. #if (defined(__GNUC__) || defined(__PGI)) && !defined(_GNU_SOURCE)
  268. # define _GNU_SOURCE
  269. #endif
  270. #include <execinfo.h>
  271. int main()
  272. {
  273. void *stackSymbols[256];
  274. backtrace(stackSymbols,256);
  275. backtrace_symbols(&stackSymbols[0],1);
  276. return 0;
  277. }
  278. #endif
  279. #ifdef TEST_KWSYS_CXX_HAS_DLADDR
  280. #if (defined(__GNUC__) || defined(__PGI)) && !defined(_GNU_SOURCE)
  281. # define _GNU_SOURCE
  282. #endif
  283. #include <dlfcn.h>
  284. int main()
  285. {
  286. Dl_info info;
  287. int ierr=dladdr((void*)main,&info);
  288. return 0;
  289. }
  290. #endif
  291. #ifdef TEST_KWSYS_CXX_HAS_CXXABI
  292. #if (defined(__GNUC__) || defined(__PGI)) && !defined(_GNU_SOURCE)
  293. # define _GNU_SOURCE
  294. #endif
  295. #if defined(__SUNPRO_CC) && __SUNPRO_CC >= 0x5130 \
  296. && __linux && __SUNPRO_CC_COMPAT == 'G'
  297. # include <iostream>
  298. #endif
  299. #include <cxxabi.h>
  300. int main()
  301. {
  302. int status = 0;
  303. size_t bufferLen = 512;
  304. char buffer[512] = {'\0'};
  305. const char *function="_ZN5kwsys17SystemInformation15GetProgramStackEii";
  306. char *demangledFunction =
  307. abi::__cxa_demangle(function, buffer, &bufferLen, &status);
  308. return status;
  309. }
  310. #endif
  311. #ifdef TEST_KWSYS_CXX_TYPE_INFO
  312. /* Collect fundamental type information and save it to a CMake script. */
  313. /* Include limits.h to get macros indicating long long and __int64.
  314. Note that certain compilers need special macros to define these
  315. macros in limits.h. */
  316. #if defined(_MSC_VER) && !defined(_MSC_EXTENSIONS)
  317. # define _MSC_EXTENSIONS
  318. #endif
  319. #if defined(__GNUC__) && __GNUC__ < 3
  320. # define _GNU_SOURCE
  321. #endif
  322. #include <limits.h>
  323. #include <stdio.h>
  324. #include <string.h>
  325. /* Due to shell differences and limitations of ADD_DEFINITIONS the
  326. KWSYS_CXX_TYPE_INFO_FILE macro will sometimes have double quotes
  327. and sometimes not. This macro will make sure the value is treated
  328. as a double-quoted string. */
  329. #define TO_STRING(x) TO_STRING0(x)
  330. #define TO_STRING0(x) TO_STRING1(x)
  331. #define TO_STRING1(x) #x
  332. void f() {}
  333. int main()
  334. {
  335. /* Construct the output file name. Some preprocessors will add an
  336. extra level of double quotes, so strip them. */
  337. char fbuf[] = TO_STRING(KWSYS_CXX_TYPE_INFO_FILE);
  338. char* fname = fbuf;
  339. if(fname[0] == '"')
  340. {
  341. ++fname;
  342. int len = static_cast<int>(strlen(fname));
  343. if(len > 0 && fname[len-1] == '"')
  344. {
  345. fname[len-1] = 0;
  346. }
  347. }
  348. /* Try to open the output file. */
  349. if(FILE* fout = fopen(fname, "w"))
  350. {
  351. /* Set the size of standard types. */
  352. fprintf(fout, "SET(KWSYS_SIZEOF_CHAR %d)\n", static_cast<int>(sizeof(char)));
  353. fprintf(fout, "SET(KWSYS_SIZEOF_SHORT %d)\n", static_cast<int>(sizeof(short)));
  354. fprintf(fout, "SET(KWSYS_SIZEOF_INT %d)\n", static_cast<int>(sizeof(int)));
  355. fprintf(fout, "SET(KWSYS_SIZEOF_LONG %d)\n", static_cast<int>(sizeof(long)));
  356. /* Set the size of some non-standard but common types. */
  357. /* Check for a limits.h macro for long long to see if the type exists. */
  358. #if defined(LLONG_MAX) || defined(LONG_LONG_MAX) || defined(LONGLONG_MAX)
  359. fprintf(fout, "SET(KWSYS_SIZEOF_LONG_LONG %d)\n", static_cast<int>(sizeof(long long)));
  360. #else
  361. fprintf(fout, "SET(KWSYS_SIZEOF_LONG_LONG 0) # No long long available.\n");
  362. #endif
  363. /* Check for a limits.h macro for __int64 to see if the type exists. */
  364. #if defined(_I64_MIN)
  365. fprintf(fout, "SET(KWSYS_SIZEOF___INT64 %d)\n", static_cast<int>(sizeof(__int64)));
  366. #else
  367. fprintf(fout, "SET(KWSYS_SIZEOF___INT64 0) # No __int64 available.\n");
  368. #endif
  369. /* Set the size of some pointer types. */
  370. fprintf(fout, "SET(KWSYS_SIZEOF_PDATA %d)\n", static_cast<int>(sizeof(void*)));
  371. fprintf(fout, "SET(KWSYS_SIZEOF_PFUNC %d)\n", static_cast<int>(sizeof(&f)));
  372. /* Set whether the native type "char" is signed or unsigned. */
  373. unsigned char uc = 255;
  374. fprintf(fout, "SET(KWSYS_CHAR_IS_SIGNED %d)\n",
  375. (*reinterpret_cast<char*>(&uc) < 0)?1:0);
  376. fclose(fout);
  377. return 0;
  378. }
  379. else
  380. {
  381. fprintf(stderr, "Failed to write fundamental type info to \"%s\".\n",
  382. fname);
  383. return 1;
  384. }
  385. }
  386. #endif
  387. #ifdef TEST_KWSYS_CXX_HAS_BORLAND_ASM
  388. int main()
  389. {
  390. int a = 1;
  391. __asm {
  392. xor EBX, EBX;
  393. mov a, EBX;
  394. }
  395. return a;
  396. }
  397. #endif
  398. #ifdef TEST_KWSYS_CXX_HAS_BORLAND_ASM_CPUID
  399. int main()
  400. {
  401. int a = 0;
  402. __asm {
  403. xor EAX, EAX;
  404. cpuid;
  405. mov a, EAX;
  406. }
  407. return a;
  408. }
  409. #endif
  410. #ifdef TEST_KWSYS_STL_HAS_WSTRING
  411. #include <string>
  412. void f(std ::wstring*) {}
  413. int main() { return 0; }
  414. #endif