SDL_dlopennote.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
  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 this
  27. * 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 library dependency is optional.
  51. *
  52. * Optional functionality uses the dependency, the binary will work and the dependency is only needed for full-featured installations.
  53. *
  54. * \since This macro is available since SDL 3.4.0.
  55. *
  56. * \sa SDL_ELF_NOTE_DLOPEN
  57. * \sa SDL_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED
  58. * \sa SDL_ELF_NOTE_DLOPEN_PRIORITY_REQUIRED
  59. */
  60. #define SDL_ELF_NOTE_DLOPEN_PRIORITY_SUGGESTED "suggested"
  61. /**
  62. * Use this macro with SDL_ELF_NOTE_DLOPEN() to note that a dynamic shared library dependency is recommended.
  63. *
  64. * Important functionality needs the dependency, the binary will work but in most cases the dependency should be provided.
  65. *
  66. * \since This macro is available since SDL 3.4.0.
  67. *
  68. * \sa SDL_ELF_NOTE_DLOPEN
  69. * \sa SDL_ELF_NOTE_DLOPEN_PRIORITY_SUGGESTED
  70. * \sa SDL_ELF_NOTE_DLOPEN_PRIORITY_REQUIRED
  71. */
  72. #define SDL_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED "recommended"
  73. /**
  74. * Use this macro with SDL_ELF_NOTE_DLOPEN() to note that a dynamic shared library dependency is required.
  75. *
  76. * Core functionality needs the dependency, the binary will not work if it cannot be found.
  77. *
  78. * \since This macro is available since SDL 3.4.0.
  79. *
  80. * \sa SDL_ELF_NOTE_DLOPEN
  81. * \sa SDL_ELF_NOTE_DLOPEN_PRIORITY_SUGGESTED
  82. * \sa SDL_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED
  83. */
  84. #define SDL_ELF_NOTE_DLOPEN_PRIORITY_REQUIRED "required"
  85. #if !defined(SDL_PLATFORM_UNIX) || defined(SDL_PLATFORM_ANDROID)
  86. /* The dlopen note functionality isn't used on this platform */
  87. #ifndef SDL_DISABLE_DLOPEN_NOTES
  88. #define SDL_DISABLE_DLOPEN_NOTES
  89. #endif
  90. #endif /* SDL_PLATFORM_UNIX || SDL_PLATFORM_ANDROID */
  91. #if defined(__ELF__) && !defined(SDL_DISABLE_DLOPEN_NOTES)
  92. #include <SDL3/SDL_stdinc.h>
  93. #define SDL_ELF_NOTE_DLOPEN_VENDOR "FDO"
  94. #define SDL_ELF_NOTE_DLOPEN_TYPE 0x407c0c0aU
  95. #define SDL_ELF_NOTE_INTERNAL2(json, variable_name) \
  96. __attribute__((aligned(4), used, section(".note.dlopen"))) \
  97. static const struct { \
  98. struct { \
  99. Uint32 n_namesz; \
  100. Uint32 n_descsz; \
  101. Uint32 n_type; \
  102. } nhdr; \
  103. char name[4]; \
  104. __attribute__((aligned(4))) char dlopen_json[sizeof(json)]; \
  105. } variable_name = { \
  106. { \
  107. sizeof(SDL_ELF_NOTE_DLOPEN_VENDOR), \
  108. sizeof(json), \
  109. SDL_ELF_NOTE_DLOPEN_TYPE \
  110. }, \
  111. SDL_ELF_NOTE_DLOPEN_VENDOR, \
  112. json \
  113. }
  114. #define SDL_ELF_NOTE_INTERNAL(json, variable_name) \
  115. SDL_ELF_NOTE_INTERNAL2(json, variable_name)
  116. #define SDL_SONAME_ARRAY1(N1) "[\"" N1 "\"]"
  117. #define SDL_SONAME_ARRAY2(N1,N2) "[\"" N1 "\",\"" N2 "\"]"
  118. #define SDL_SONAME_ARRAY3(N1,N2,N3) "[\"" N1 "\",\"" N2 "\",\"" N3 "\"]"
  119. #define SDL_SONAME_ARRAY4(N1,N2,N3,N4) "[\"" N1 "\",\"" N2 "\",\"" N3 "\",\"" N4 "\"]"
  120. #define SDL_SONAME_ARRAY5(N1,N2,N3,N4,N5) "[\"" N1 "\",\"" N2 "\",\"" N3 "\",\"" N4 "\",\"" N5 "\"]"
  121. #define SDL_SONAME_ARRAY6(N1,N2,N3,N4,N5,N6) "[\"" N1 "\",\"" N2 "\",\"" N3 "\",\"" N4 "\",\"" N5 "\",\"" N6 "\"]"
  122. #define SDL_SONAME_ARRAY7(N1,N2,N3,N4,N5,N6,N7) "[\"" N1 "\",\"" N2 "\",\"" N3 "\",\"" N4 "\",\"" N5 "\",\"" N6 "\",\"" N7 "\"]"
  123. #define SDL_SONAME_ARRAY8(N1,N2,N3,N4,N5,N6,N7,N8) "[\"" N1 "\",\"" N2 "\",\"" N3 "\",\"" N4 "\",\"" N5 "\",\"" N6 "\",\"" N7 "\",\"" N8 "\"]"
  124. #define SDL_SONAME_ARRAY_GET(N1,N2,N3,N4,N5,N6,N7,N8,NAME,...) NAME
  125. #define SDL_SONAME_ARRAY(...) \
  126. SDL_SONAME_ARRAY_GET(__VA_ARGS__, \
  127. SDL_SONAME_ARRAY8, \
  128. SDL_SONAME_ARRAY7, \
  129. SDL_SONAME_ARRAY6, \
  130. SDL_SONAME_ARRAY5, \
  131. SDL_SONAME_ARRAY4, \
  132. SDL_SONAME_ARRAY3, \
  133. SDL_SONAME_ARRAY2, \
  134. SDL_SONAME_ARRAY1 \
  135. )(__VA_ARGS__)
  136. /* Create "unique" variable name using __LINE__,
  137. * so creating elf notes on the same line is not supported
  138. */
  139. #define SDL_ELF_NOTE_JOIN2(A,B) A##B
  140. #define SDL_ELF_NOTE_JOIN(A,B) SDL_ELF_NOTE_JOIN2(A,B)
  141. #define SDL_ELF_NOTE_UNIQUE_NAME SDL_ELF_NOTE_JOIN(s_SDL_dlopen_note_, __LINE__)
  142. /**
  143. * Note that your application has dynamic shared library dependencies.
  144. *
  145. * You can do this by adding the following to the global scope:
  146. *
  147. * ```c
  148. * SDL_ELF_NOTE_DLOPEN(
  149. * "png",
  150. * "Support for loading PNG images using libpng (required for APNG)",
  151. * SDL_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED,
  152. * "libpng12.so.0"
  153. * );
  154. * ```
  155. *
  156. * Or if you support multiple versions of a library, you can list them:
  157. * ```c
  158. * // Our app supports SDL1, SDL2, and SDL3 by dynamically loading them
  159. * SDL_ELF_NOTE_DLOPEN(
  160. * "SDL",
  161. * "Create windows through SDL video backend",
  162. * SDL_ELF_NOTE_DLOPEN_PRIORITY_REQUIRED
  163. * "libSDL-1.2.so.0", "libSDL2-2.0.so.0", "libSDL3.so.0"
  164. * );
  165. * ```
  166. *
  167. * \since This macro is available since SDL 3.4.0.
  168. *
  169. * \sa SDL_ELF_NOTE_DLOPEN_PRIORITY_SUGGESTED
  170. * \sa SDL_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED
  171. * \sa SDL_ELF_NOTE_DLOPEN_PRIORITY_REQUIRED
  172. */
  173. #define SDL_ELF_NOTE_DLOPEN(feature, description, priority, ...) \
  174. SDL_ELF_NOTE_INTERNAL( \
  175. "[{\"feature\":\"" feature \
  176. "\",\"description\":\"" description \
  177. "\",\"priority\":\"" priority \
  178. "\",\"soname\":" SDL_SONAME_ARRAY(__VA_ARGS__) "}]", \
  179. SDL_ELF_NOTE_UNIQUE_NAME)
  180. #elif defined (__GNUC__) && __GNUC__ < 3
  181. /* Variadic macros are not supported */
  182. #define SDL_ELF_NOTE_DLOPEN
  183. #else
  184. #define SDL_ELF_NOTE_DLOPEN(...)
  185. #endif
  186. #endif /* SDL_dlopennote_h */