component_export.h 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_COMPONENT_EXPORT_H_
  5. #define BASE_COMPONENT_EXPORT_H_
  6. // Used to annotate symbols which are exported by the component named
  7. // |component|. Note that this only does the right thing if the corresponding
  8. // component target's sources are compiled with |IS_$component_IMPL| defined
  9. // as 1. For example:
  10. //
  11. // class COMPONENT_EXPORT(FOO) Bar {};
  12. //
  13. // If IS_FOO_IMPL=1 at compile time, then Bar will be annotated using the
  14. // COMPONENT_EXPORT_ANNOTATION macro defined below. Otherwise it will be
  15. // annotated using the COMPONENT_IMPORT_ANNOTATION macro.
  16. #define COMPONENT_EXPORT(component) \
  17. COMPONENT_MACRO_CONDITIONAL_(IS_##component##_IMPL, \
  18. COMPONENT_EXPORT_ANNOTATION, \
  19. COMPONENT_IMPORT_ANNOTATION)
  20. // Indicates whether the current compilation unit is being compiled as part of
  21. // the implementation of the component named |component|. Expands to |1| if
  22. // |IS_$component_IMPL| is defined as |1|; expands to |0| otherwise.
  23. //
  24. // Note in particular that if |IS_$component_IMPL| is not defined at all, it is
  25. // still fine to test INSIDE_COMPONENT_IMPL(component), which expands to |0| as
  26. // expected.
  27. #define INSIDE_COMPONENT_IMPL(component) \
  28. COMPONENT_MACRO_CONDITIONAL_(IS_##component##_IMPL, 1, 0)
  29. // Compiler-specific macros to annotate for export or import of a symbol. No-op
  30. // in non-component builds. These should not see much if any direct use.
  31. // Instead use the COMPONENT_EXPORT macro defined above.
  32. #if defined(COMPONENT_BUILD)
  33. #if defined(WIN32)
  34. #define COMPONENT_EXPORT_ANNOTATION __declspec(dllexport)
  35. #define COMPONENT_IMPORT_ANNOTATION __declspec(dllimport)
  36. #else // defined(WIN32)
  37. #define COMPONENT_EXPORT_ANNOTATION __attribute__((visibility("default")))
  38. #define COMPONENT_IMPORT_ANNOTATION
  39. #endif // defined(WIN32)
  40. #else // defined(COMPONENT_BUILD)
  41. #define COMPONENT_EXPORT_ANNOTATION
  42. #define COMPONENT_IMPORT_ANNOTATION
  43. #endif // defined(COMPONENT_BUILD)
  44. // Below this point are several internal utility macros used for the
  45. // implementation of the above macros. Not intended for external use.
  46. // Helper for conditional expansion to one of two token strings. If |condition|
  47. // expands to |1| then this macro expands to |consequent|; otherwise it expands
  48. // to |alternate|.
  49. #define COMPONENT_MACRO_CONDITIONAL_(condition, consequent, alternate) \
  50. COMPONENT_MACRO_SELECT_THIRD_ARGUMENT_( \
  51. COMPONENT_MACRO_CONDITIONAL_COMMA_(condition), consequent, alternate)
  52. // Expands to a comma (,) iff its first argument expands to |1|. Used in
  53. // conjunction with |COMPONENT_MACRO_SELECT_THIRD_ARGUMENT_()|, as the presence
  54. // or absense of an extra comma can be used to conditionally shift subsequent
  55. // argument positions and thus influence which argument is selected.
  56. #define COMPONENT_MACRO_CONDITIONAL_COMMA_(...) \
  57. COMPONENT_MACRO_CONDITIONAL_COMMA_IMPL_(__VA_ARGS__,)
  58. #define COMPONENT_MACRO_CONDITIONAL_COMMA_IMPL_(x, ...) \
  59. COMPONENT_MACRO_CONDITIONAL_COMMA_##x##_
  60. #define COMPONENT_MACRO_CONDITIONAL_COMMA_1_ ,
  61. // Helper which simply selects its third argument. Used in conjunction with
  62. // |COMPONENT_MACRO_CONDITIONAL_COMMA_()| above to implement conditional macro
  63. // expansion.
  64. #define COMPONENT_MACRO_SELECT_THIRD_ARGUMENT_(...) \
  65. COMPONENT_MACRO_SELECT_THIRD_ARGUMENT_IMPL_(__VA_ARGS__)
  66. #define COMPONENT_MACRO_SELECT_THIRD_ARGUMENT_IMPL_(a, b, c, ...) c
  67. #endif // BASE_COMPONENT_EXPORT_H_