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