3
0

platform.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. Copyright 2006, Bernhard Fischer
  4. Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  5. */
  6. #ifndef __PLATFORM_H
  7. #define __PLATFORM_H 1
  8. /* Convenience macros to test the version of gcc. */
  9. #undef __GNUC_PREREQ
  10. #if defined __GNUC__ && defined __GNUC_MINOR__
  11. # define __GNUC_PREREQ(maj, min) \
  12. ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
  13. #else
  14. # define __GNUC_PREREQ(maj, min) 0
  15. #endif
  16. /* __restrict is known in EGCS 1.2 and above. */
  17. #if !__GNUC_PREREQ (2,92)
  18. # ifndef __restrict
  19. # define __restrict /* Ignore */
  20. # endif
  21. #endif
  22. /* Define macros for some gcc attributes. This permits us to use the
  23. macros freely, and know that they will come into play for the
  24. version of gcc in which they are supported. */
  25. #if !__GNUC_PREREQ (2,7)
  26. # ifndef __attribute__
  27. # define __attribute__(x)
  28. # endif
  29. #endif
  30. #undef inline
  31. #if defined(__STDC_VERSION__) && __STDC_VERSION__ > 199901L
  32. /* it's a keyword */
  33. #else
  34. # if __GNUC_PREREQ (2,7)
  35. # define inline __inline__
  36. # else
  37. # define inline
  38. # endif
  39. #endif
  40. #ifndef __const
  41. # define __const const
  42. #endif
  43. # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
  44. # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
  45. # define ATTRIBUTE_PACKED __attribute__ ((__packed__))
  46. # define ATTRIBUTE_ALIGNED(m) __attribute__ ((__aligned__(m)))
  47. # if __GNUC_PREREQ (3,0)
  48. # define ALWAYS_INLINE __attribute__ ((always_inline)) inline
  49. # if !ENABLE_WERROR
  50. # define ATTRIBUTE_DEPRECATED __attribute__ ((__deprecated__))
  51. # define ATTRIBUTE_UNUSED_RESULT __attribute__ ((warn_unused_result))
  52. # else
  53. # define ATTRIBUTE_DEPRECATED /* n/a */
  54. # define ATTRIBUTE_UNUSED_RESULT /* n/a */
  55. # endif
  56. # else
  57. # define ALWAYS_INLINE inline
  58. # define ATTRIBUTE_DEPRECATED /* n/a */
  59. # define ATTRIBUTE_UNUSED_RESULT /* n/a */
  60. # endif
  61. /* -fwhole-program makes all symbols local. The attribute externally_visible
  62. forces a symbol global. */
  63. # if __GNUC_PREREQ (4,1)
  64. # define ATTRIBUTE_EXTERNALLY_VISIBLE __attribute__ ((__externally_visible__))
  65. # else
  66. # define ATTRIBUTE_EXTERNALLY_VISIBLE
  67. # endif /* GNUC >= 4.1 */
  68. /* We use __extension__ in some places to suppress -pedantic warnings
  69. about GCC extensions. This feature didn't work properly before
  70. gcc 2.8. */
  71. #if !__GNUC_PREREQ (2,8)
  72. # ifndef __extension__
  73. # define __extension__
  74. # endif
  75. #endif
  76. /* gcc-2.95 had no va_copy but only __va_copy. */
  77. #if !__GNUC_PREREQ (3,0)
  78. # include <stdarg.h>
  79. # if !defined va_copy && defined __va_copy
  80. # define va_copy(d,s) __va_copy((d),(s))
  81. # endif
  82. #endif
  83. /* ---- Endian Detection ------------------------------------ */
  84. #if (defined __digital__ && defined __unix__)
  85. # include <sex.h>
  86. # define __BIG_ENDIAN__ (BYTE_ORDER == BIG_ENDIAN)
  87. # define __BYTE_ORDER BYTE_ORDER
  88. #elif !defined __APPLE__
  89. # include <byteswap.h>
  90. # include <endian.h>
  91. #endif
  92. #ifdef __BIG_ENDIAN__
  93. # define BB_BIG_ENDIAN 1
  94. # define BB_LITTLE_ENDIAN 0
  95. #elif __BYTE_ORDER == __BIG_ENDIAN
  96. # define BB_BIG_ENDIAN 1
  97. # define BB_LITTLE_ENDIAN 0
  98. #else
  99. # define BB_BIG_ENDIAN 0
  100. # define BB_LITTLE_ENDIAN 1
  101. #endif
  102. #if BB_BIG_ENDIAN
  103. #define SWAP_BE16(x) (x)
  104. #define SWAP_BE32(x) (x)
  105. #define SWAP_BE64(x) (x)
  106. #define SWAP_LE16(x) bswap_16(x)
  107. #define SWAP_LE32(x) bswap_32(x)
  108. #define SWAP_LE64(x) bswap_64(x)
  109. #else
  110. #define SWAP_BE16(x) bswap_16(x)
  111. #define SWAP_BE32(x) bswap_32(x)
  112. #define SWAP_BE64(x) bswap_64(x)
  113. #define SWAP_LE16(x) (x)
  114. #define SWAP_LE32(x) (x)
  115. #define SWAP_LE64(x) (x)
  116. #endif
  117. /* ---- Networking ------------------------------------------ */
  118. #ifndef __APPLE__
  119. # include <arpa/inet.h>
  120. #else
  121. # include <netinet/in.h>
  122. #endif
  123. #ifndef __socklen_t_defined
  124. typedef int socklen_t;
  125. #endif
  126. /* ---- Compiler dependent settings ------------------------- */
  127. #if (defined __digital__ && defined __unix__)
  128. # undef HAVE_MNTENT_H
  129. #else
  130. # define HAVE_MNTENT_H 1
  131. #endif /* ___digital__ && __unix__ */
  132. /* linux/loop.h relies on __u64. Make sure we have that as a proper type
  133. * until userspace is widely fixed. */
  134. #ifndef __GNUC__
  135. #if defined __INTEL_COMPILER
  136. __extension__ typedef __signed__ long long __s64;
  137. __extension__ typedef unsigned long long __u64;
  138. #endif /* __INTEL_COMPILER */
  139. #endif /* ifndef __GNUC__ */
  140. /*----- Kernel versioning ------------------------------------*/
  141. #define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
  142. /* ---- miscellaneous --------------------------------------- */
  143. #if defined(__GNU_LIBRARY__) && __GNU_LIBRARY__ < 5 && \
  144. !defined(__dietlibc__) && \
  145. !defined(_NEWLIB_VERSION) && \
  146. !(defined __digital__ && defined __unix__)
  147. # error "Sorry, this libc version is not supported :("
  148. #endif
  149. /* Don't perpetuate e2fsck crap into the headers. Clean up e2fsck instead. */
  150. #if defined __GLIBC__ || defined __UCLIBC__ \
  151. || defined __dietlibc__ || defined _NEWLIB_VERSION
  152. #include <features.h>
  153. #define HAVE_FEATURES_H
  154. #include <stdint.h>
  155. #define HAVE_STDINT_H
  156. #else
  157. /* Largest integral types. */
  158. #if __BIG_ENDIAN__
  159. typedef long intmax_t;
  160. typedef unsigned long uintmax_t;
  161. #else
  162. __extension__
  163. typedef long long intmax_t;
  164. __extension__
  165. typedef unsigned long long uintmax_t;
  166. #endif
  167. #endif
  168. /* Size-saving "small" ints (arch-dependent) */
  169. #if defined(i386) || defined(__x86_64__) || defined(__mips__) || defined(__cris__)
  170. /* add other arches which benefit from this... */
  171. typedef signed char smallint;
  172. typedef unsigned char smalluint;
  173. #else
  174. /* for arches where byte accesses generate larger code: */
  175. typedef int smallint;
  176. typedef unsigned smalluint;
  177. #endif
  178. /* ISO C Standard: 7.16 Boolean type and values <stdbool.h> */
  179. #if (defined __digital__ && defined __unix__)
  180. /* old system without (proper) C99 support */
  181. #define bool smalluint
  182. #else
  183. /* modern system, so use it */
  184. #include <stdbool.h>
  185. #endif
  186. /* Try to defeat gcc's alignment of "char message[]"-like data */
  187. #if 1 /* if needed: !defined(arch1) && !defined(arch2) */
  188. #define ALIGN1 __attribute__((aligned(1)))
  189. #define ALIGN2 __attribute__((aligned(2)))
  190. #else
  191. /* Arches which MUST have 2 or 4 byte alignment for everything are here */
  192. #define ALIGN1
  193. #define ALIGN2
  194. #endif
  195. /* uclibc does not implement daemon() for no-mmu systems.
  196. * For 0.9.29 and svn, __ARCH_USE_MMU__ indicates no-mmu reliably.
  197. * For earlier versions there is no reliable way to check if we are building
  198. * for a mmu-less system; the user should pass EXTRA_CFLAGS="-DBB_NOMMU"
  199. * on his own.
  200. */
  201. #if defined __UCLIBC__ && __UCLIBC_MAJOR__ >= 0 && __UCLIBC_MINOR__ >= 9 && \
  202. __UCLIBC_SUBLEVEL__ > 28 && !defined __ARCH_USE_MMU__
  203. #define BB_MMU 0
  204. #define BB_NOMMU 1
  205. #define USE_FOR_NOMMU(...) __VA_ARGS__
  206. #define USE_FOR_MMU(...)
  207. #else
  208. #define BB_MMU 1
  209. /* BB_NOMMU is not defined in this case! */
  210. #define USE_FOR_NOMMU(...)
  211. #define USE_FOR_MMU(...) __VA_ARGS__
  212. #endif
  213. /* Platforms that haven't got dprintf need to implement fdprintf() in
  214. * libbb. This would require a platform.c. It's not going to be cleaned
  215. * out of the tree, so stop saying it should be. */
  216. #if !defined(__dietlibc__)
  217. /* Needed for: glibc */
  218. /* Not needed for: dietlibc */
  219. /* Others: ?? (add as needed) */
  220. #define fdprintf dprintf
  221. #endif
  222. #if defined(__dietlibc__)
  223. static ALWAYS_INLINE char* strchrnul(const char *s, char c)
  224. {
  225. while (*s && *s != c) ++s;
  226. return (char*)s;
  227. }
  228. #endif
  229. /* Don't use lchown with glibc older than 2.1.x ... uClibc lacks it */
  230. #if (defined __GLIBC__ && __GLIBC__ <= 2 && __GLIBC_MINOR__ < 1) || \
  231. defined __UC_LIBC__
  232. # define lchown chown
  233. #endif
  234. /* THIS SHOULD BE CLEANED OUT OF THE TREE ENTIRELY */
  235. /* FIXME: fix tar.c! */
  236. #ifndef FNM_LEADING_DIR
  237. #define FNM_LEADING_DIR 0
  238. #endif
  239. #if (defined __digital__ && defined __unix__)
  240. #include <standards.h>
  241. #define HAVE_STANDARDS_H
  242. #include <inttypes.h>
  243. #define HAVE_INTTYPES_H
  244. #define PRIu32 "u"
  245. /* use legacy setpgrp(pid_t,pid_t) for now. move to platform.c */
  246. #define bb_setpgrp() do { pid_t __me = getpid(); setpgrp(__me,__me); } while (0)
  247. #if !defined ADJ_OFFSET_SINGLESHOT && defined MOD_CLKA && defined MOD_OFFSET
  248. #define ADJ_OFFSET_SINGLESHOT (MOD_CLKA | MOD_OFFSET)
  249. #endif
  250. #if !defined ADJ_FREQUENCY && defined MOD_FREQUENCY
  251. #define ADJ_FREQUENCY MOD_FREQUENCY
  252. #endif
  253. #if !defined ADJ_TIMECONST && defined MOD_TIMECONST
  254. #define ADJ_TIMECONST MOD_TIMECONST
  255. #endif
  256. #if !defined ADJ_TICK && defined MOD_CLKB
  257. #define ADJ_TICK MOD_CLKB
  258. #endif
  259. #else
  260. #define bb_setpgrp() setpgrp()
  261. #endif
  262. #if defined(__linux__)
  263. #include <sys/mount.h>
  264. /* Make sure we have all the new mount flags we actually try to use. */
  265. #ifndef MS_BIND
  266. #define MS_BIND (1<<12)
  267. #endif
  268. #ifndef MS_MOVE
  269. #define MS_MOVE (1<<13)
  270. #endif
  271. #ifndef MS_RECURSIVE
  272. #define MS_RECURSIVE (1<<14)
  273. #endif
  274. #ifndef MS_SILENT
  275. #define MS_SILENT (1<<15)
  276. #endif
  277. /* The shared subtree stuff, which went in around 2.6.15. */
  278. #ifndef MS_UNBINDABLE
  279. #define MS_UNBINDABLE (1<<17)
  280. #endif
  281. #ifndef MS_PRIVATE
  282. #define MS_PRIVATE (1<<18)
  283. #endif
  284. #ifndef MS_SLAVE
  285. #define MS_SLAVE (1<<19)
  286. #endif
  287. #ifndef MS_SHARED
  288. #define MS_SHARED (1<<20)
  289. #endif
  290. #if !defined(BLKSSZGET)
  291. #define BLKSSZGET _IO(0x12, 104)
  292. #endif
  293. #if !defined(BLKGETSIZE64)
  294. #define BLKGETSIZE64 _IOR(0x12,114,size_t)
  295. #endif
  296. #endif
  297. #endif /* platform.h */