SDL_dlopennote.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. /* WIKI CATEGORY: DlopenNotes */
  19. /**
  20. * # CategoryDlopenNotes
  21. *
  22. * This header allows you to annotate your code so external tools know about
  23. * dynamic shared library dependencies.
  24. *
  25. * If you determine that your toolchain doesn't support dlopen notes, you can
  26. * disable this feature by defining `SDL_DISABLE_DLOPEN_NOTES`. You can use
  27. * this CMake snippet to check for support:
  28. *
  29. * ```cmake
  30. * set(CHECK_ELF_DLNOTES_SRC [==[
  31. * #ifndef __ELF__
  32. * ELF DL notes is only supported on ELF platforms
  33. * #endif
  34. * __attribute__ ((used,aligned(4),section(".note.dlopen"))) static const struct {
  35. * struct { int a; int b; int c; } hdr; char name[4]; __attribute__((aligned(4))) char json[24];
  36. * } dlnote = { { 4, 0x407c0c0aU, 16 }, "FDO", "[\\"a\\":{\\"a\\":\\"1\\",\\"b\\":\\"2\\"}]" };
  37. * int main(int argc, char *argv[]) {
  38. * return argc + dlnote.hdr.a;
  39. * }
  40. * ]==])
  41. * check_c_source_compiles("${CHECK_ELF_DLNOTES_SRC}" COMPILER_SUPPORTS_ELFNOTES)
  42. * if(NOT COMPILER_SUPPORTS_ELFNOTES)
  43. * set(SDL_DISABLE_DLOPEN_NOTES TRUE)
  44. * endif()
  45. * ```
  46. */
  47. #ifndef SDL_dlopennote_h
  48. #define SDL_dlopennote_h
  49. /**
  50. * Use this macro with SDL_ELF_NOTE_DLOPEN() to note that a dynamic shared
  51. * library dependency is optional.
  52. *
  53. * Optional functionality uses the dependency, the binary will work and the
  54. * dependency is only needed for full-featured installations.
  55. *
  56. * \since This macro is available since SDL 3.4.0.
  57. *
  58. * \sa SDL_ELF_NOTE_DLOPEN
  59. * \sa SDL_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED
  60. * \sa SDL_ELF_NOTE_DLOPEN_PRIORITY_REQUIRED
  61. */
  62. #define SDL_ELF_NOTE_DLOPEN_PRIORITY_SUGGESTED "suggested"
  63. /**
  64. * Use this macro with SDL_ELF_NOTE_DLOPEN() to note that a dynamic shared
  65. * library dependency is recommended.
  66. *
  67. * Important functionality needs the dependency, the binary will work but in
  68. * most cases the dependency should be provided.
  69. *
  70. * \since This macro is available since SDL 3.4.0.
  71. *
  72. * \sa SDL_ELF_NOTE_DLOPEN
  73. * \sa SDL_ELF_NOTE_DLOPEN_PRIORITY_SUGGESTED
  74. * \sa SDL_ELF_NOTE_DLOPEN_PRIORITY_REQUIRED
  75. */
  76. #define SDL_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED "recommended"
  77. /**
  78. * Use this macro with SDL_ELF_NOTE_DLOPEN() to note that a dynamic shared
  79. * library dependency is required.
  80. *
  81. * Core functionality needs the dependency, the binary will not work if it
  82. * cannot be found.
  83. *
  84. * \since This macro is available since SDL 3.4.0.
  85. *
  86. * \sa SDL_ELF_NOTE_DLOPEN
  87. * \sa SDL_ELF_NOTE_DLOPEN_PRIORITY_SUGGESTED
  88. * \sa SDL_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED
  89. */
  90. #define SDL_ELF_NOTE_DLOPEN_PRIORITY_REQUIRED "required"
  91. #if !defined(SDL_PLATFORM_UNIX) || defined(SDL_PLATFORM_ANDROID)
  92. /* The dlopen note functionality isn't used on this platform */
  93. #ifndef SDL_DISABLE_DLOPEN_NOTES
  94. #define SDL_DISABLE_DLOPEN_NOTES
  95. #endif
  96. #elif defined(__GNUC__) && (__GNUC__ < 3 || (__GNUC__ == 3 && __GNUC_MINOR__ < 1))
  97. /* gcc < 3.1 too old */
  98. #ifndef SDL_DISABLE_DLOPEN_NOTES
  99. #define SDL_DISABLE_DLOPEN_NOTES
  100. #endif
  101. #endif /* SDL_PLATFORM_UNIX || SDL_PLATFORM_ANDROID */
  102. #if defined(__ELF__) && !defined(SDL_DISABLE_DLOPEN_NOTES)
  103. #include <SDL3/SDL_stdinc.h>
  104. #define SDL_ELF_NOTE_DLOPEN_VENDOR "FDO"
  105. #define SDL_ELF_NOTE_DLOPEN_TYPE 0x407c0c0aU
  106. #define SDL_ELF_NOTE_INTERNAL2(json, variable_name) \
  107. __attribute__((aligned(4), used, section(".note.dlopen"))) \
  108. static const struct { \
  109. struct { \
  110. Uint32 n_namesz; \
  111. Uint32 n_descsz; \
  112. Uint32 n_type; \
  113. } nhdr; \
  114. char name[4]; \
  115. __attribute__((aligned(4))) char dlopen_json[sizeof(json)]; \
  116. } variable_name = { \
  117. { \
  118. sizeof(SDL_ELF_NOTE_DLOPEN_VENDOR), \
  119. sizeof(json), \
  120. SDL_ELF_NOTE_DLOPEN_TYPE \
  121. }, \
  122. SDL_ELF_NOTE_DLOPEN_VENDOR, \
  123. json \
  124. }
  125. #define SDL_ELF_NOTE_INTERNAL(json, variable_name) \
  126. SDL_ELF_NOTE_INTERNAL2(json, variable_name)
  127. #define SDL_SONAME_ARRAY1(N1) "[\"" N1 "\"]"
  128. #define SDL_SONAME_ARRAY2(N1,N2) "[\"" N1 "\",\"" N2 "\"]"
  129. #define SDL_SONAME_ARRAY3(N1,N2,N3) "[\"" N1 "\",\"" N2 "\",\"" N3 "\"]"
  130. #define SDL_SONAME_ARRAY4(N1,N2,N3,N4) "[\"" N1 "\",\"" N2 "\",\"" N3 "\",\"" N4 "\"]"
  131. #define SDL_SONAME_ARRAY5(N1,N2,N3,N4,N5) "[\"" N1 "\",\"" N2 "\",\"" N3 "\",\"" N4 "\",\"" N5 "\"]"
  132. #define SDL_SONAME_ARRAY6(N1,N2,N3,N4,N5,N6) "[\"" N1 "\",\"" N2 "\",\"" N3 "\",\"" N4 "\",\"" N5 "\",\"" N6 "\"]"
  133. #define SDL_SONAME_ARRAY7(N1,N2,N3,N4,N5,N6,N7) "[\"" N1 "\",\"" N2 "\",\"" N3 "\",\"" N4 "\",\"" N5 "\",\"" N6 "\",\"" N7 "\"]"
  134. #define SDL_SONAME_ARRAY8(N1,N2,N3,N4,N5,N6,N7,N8) "[\"" N1 "\",\"" N2 "\",\"" N3 "\",\"" N4 "\",\"" N5 "\",\"" N6 "\",\"" N7 "\",\"" N8 "\"]"
  135. #define SDL_SONAME_ARRAY_GET(N1,N2,N3,N4,N5,N6,N7,N8,NAME,...) NAME
  136. #define SDL_SONAME_ARRAY(...) \
  137. SDL_SONAME_ARRAY_GET(__VA_ARGS__, \
  138. SDL_SONAME_ARRAY8, \
  139. SDL_SONAME_ARRAY7, \
  140. SDL_SONAME_ARRAY6, \
  141. SDL_SONAME_ARRAY5, \
  142. SDL_SONAME_ARRAY4, \
  143. SDL_SONAME_ARRAY3, \
  144. SDL_SONAME_ARRAY2, \
  145. SDL_SONAME_ARRAY1 \
  146. )(__VA_ARGS__)
  147. /* Create "unique" variable name using __LINE__,
  148. * so creating elf notes on the same line is not supported
  149. */
  150. #define SDL_ELF_NOTE_JOIN2(A,B) A##B
  151. #define SDL_ELF_NOTE_JOIN(A,B) SDL_ELF_NOTE_JOIN2(A,B)
  152. #define SDL_ELF_NOTE_UNIQUE_NAME SDL_ELF_NOTE_JOIN(s_SDL_dlopen_note_, __LINE__)
  153. /**
  154. * Note that your application has dynamic shared library dependencies.
  155. *
  156. * You can do this by adding the following to the global scope:
  157. *
  158. * ```c
  159. * SDL_ELF_NOTE_DLOPEN(
  160. * "png",
  161. * "Support for loading PNG images using libpng (required for APNG)",
  162. * SDL_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED,
  163. * "libpng12.so.0"
  164. * );
  165. * ```
  166. *
  167. * Or if you support multiple versions of a library, you can list them:
  168. *
  169. * ```c
  170. * // Our app supports SDL1, SDL2, and SDL3 by dynamically loading them
  171. * SDL_ELF_NOTE_DLOPEN(
  172. * "SDL",
  173. * "Create windows through SDL video backend",
  174. * SDL_ELF_NOTE_DLOPEN_PRIORITY_REQUIRED
  175. * "libSDL-1.2.so.0", "libSDL2-2.0.so.0", "libSDL3.so.0"
  176. * );
  177. * ```
  178. *
  179. * \since This macro is available since SDL 3.4.0.
  180. *
  181. * \sa SDL_ELF_NOTE_DLOPEN_PRIORITY_SUGGESTED
  182. * \sa SDL_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED
  183. * \sa SDL_ELF_NOTE_DLOPEN_PRIORITY_REQUIRED
  184. */
  185. #define SDL_ELF_NOTE_DLOPEN(feature, description, priority, ...) \
  186. SDL_ELF_NOTE_INTERNAL( \
  187. "[{\"feature\":\"" feature \
  188. "\",\"description\":\"" description \
  189. "\",\"priority\":\"" priority \
  190. "\",\"soname\":" SDL_SONAME_ARRAY(__VA_ARGS__) "}]", \
  191. SDL_ELF_NOTE_UNIQUE_NAME)
  192. #elif defined(__GNUC__) && __GNUC__ < 3
  193. #define SDL_ELF_NOTE_DLOPEN(args...)
  194. #elif defined(_MSC_VER) && _MSC_VER < 1400
  195. /* Variadic macros are not supported */
  196. #define SDL_ELF_NOTE_DLOPEN
  197. #else
  198. #define SDL_ELF_NOTE_DLOPEN(...)
  199. #endif
  200. #endif /* SDL_dlopennote_h */