@@ -26,12 +26,12 @@ namespace caffe2 {
26
26
/* *
27
27
* Sets the usage message when a commandline tool is called with "--help".
28
28
*/
29
- void SetUsageMessage (const string& str);
29
+ CAFFE2_API void SetUsageMessage (const string& str);
30
30
31
31
/* *
32
32
* Returns the usage message for the commandline tool set by SetUsageMessage.
33
33
*/
34
- const char * UsageMessage ();
34
+ CAFFE2_API const char * UsageMessage ();
35
35
36
36
/* *
37
37
* Parses the commandline flags.
@@ -41,11 +41,11 @@ const char* UsageMessage();
41
41
* commandline args that caffe2 does not deal with. Note that following
42
42
* convention, argv[0] contains the binary name and is not parsed.
43
43
*/
44
- bool ParseCaffeCommandLineFlags (int * pargc, char *** pargv);
44
+ CAFFE2_API bool ParseCaffeCommandLineFlags (int * pargc, char *** pargv);
45
45
/* *
46
46
* Checks if the commandline flags has already been passed.
47
47
*/
48
- bool CommandLineFlagsHasBeenParsed ();
48
+ CAFFE2_API bool CommandLineFlagsHasBeenParsed ();
49
49
50
50
} // namespace caffe2
51
51
@@ -56,6 +56,10 @@ bool CommandLineFlagsHasBeenParsed();
56
56
57
57
#ifdef CAFFE2_USE_GFLAGS
58
58
59
+ // //////////////////////////////////////////////////////////////////////////////
60
+ // Begin gflags section: most functions are basically rerouted to gflags.
61
+ // //////////////////////////////////////////////////////////////////////////////
62
+
59
63
#include < gflags/gflags.h>
60
64
61
65
// gflags before 2.0 uses namespace google and after 2.1 uses namespace gflags.
@@ -64,41 +68,70 @@ bool CommandLineFlagsHasBeenParsed();
64
68
namespace gflags = google;
65
69
#endif // GFLAGS_GFLAGS_H_
66
70
67
- #define CAFFE2_GFLAGS_DEF_WRAPPER (type, name, default_value, help_str ) \
71
+ // Motivation about the gflags wrapper:
72
+ // (1) We would need to make sure that the gflags version and the non-gflags
73
+ // version of Caffe2 are going to expose the same flags abstraction. One should
74
+ // explicitly use caffe2::FLAGS_flag_name to access the flags.
75
+ // (2) For flag names, it is recommended to start with caffe2_ to distinguish it
76
+ // from regular gflags flags. For example, do
77
+ // CAFFE2_DEFINE_BOOL(caffe2_my_flag, true, "An example");
78
+ // to allow one to use caffe2::FLAGS_caffe2_my_flag.
79
+ // (3) Gflags has a design issue that does not properly expose the global flags,
80
+ // if one builds the library with -fvisibility=hidden. The current gflags (as of
81
+ // Aug 2018) only deals with the Windows case using dllexport, and not the Linux
82
+ // counterparts. As a result, we will explciitly use CAFFE2_EXPORT to export the
83
+ // flags defined in Caffe2. This is done via a global reference, so the flag
84
+ // itself is not duplicated - under the hood it is the same global gflags flag.
85
+ #define CAFFE2_GFLAGS_DEF_WRAPPER ( \
86
+ type, real_type, name, default_value, help_str) \
68
87
DEFINE_##type(name, default_value, help_str); \
69
88
namespace caffe2 { \
70
- using ::FLAGS_##name; \
89
+ CAFFE2_EXPORT real_type& FLAGS_##name = ::FLAGS_##name; \
71
90
}
72
91
73
92
#define CAFFE2_DEFINE_int (name, default_value, help_str ) \
74
- CAFFE2_GFLAGS_DEF_WRAPPER (int32, name, default_value, help_str)
93
+ CAFFE2_GFLAGS_DEF_WRAPPER (int32, gflags::int32, name, default_value, help_str)
75
94
#define CAFFE2_DEFINE_int64 (name, default_value, help_str ) \
76
- CAFFE2_GFLAGS_DEF_WRAPPER (int64, name, default_value, help_str)
95
+ CAFFE2_GFLAGS_DEF_WRAPPER (int64, gflags::int64, name, default_value, help_str)
77
96
#define CAFFE2_DEFINE_double (name, default_value, help_str ) \
78
- CAFFE2_GFLAGS_DEF_WRAPPER (double , name, default_value, help_str)
97
+ CAFFE2_GFLAGS_DEF_WRAPPER (double , double , name, default_value, help_str)
79
98
#define CAFFE2_DEFINE_bool (name, default_value, help_str ) \
80
- CAFFE2_GFLAGS_DEF_WRAPPER (bool , name, default_value, help_str)
81
- #define CAFFE2_DEFINE_string (name, default_value, help_str ) \
82
- CAFFE2_GFLAGS_DEF_WRAPPER (string, name, default_value, help_str)
99
+ CAFFE2_GFLAGS_DEF_WRAPPER (bool , bool , name, default_value, help_str)
100
+ #define CAFFE2_DEFINE_string (name, default_value, help_str ) \
101
+ CAFFE2_GFLAGS_DEF_WRAPPER ( \
102
+ string, ::fLS ::clstring, name, default_value, help_str)
83
103
84
104
// DECLARE_typed_var should be used in header files and in the global namespace.
85
- #define CAFFE2_GFLAGS_DECLARE_WRAPPER (type, name ) \
86
- DECLARE_##type(name); \
87
- namespace caffe2 { \
88
- using :: FLAGS_##name; \
105
+ #define CAFFE2_GFLAGS_DECLARE_WRAPPER (type, real_type, name ) \
106
+ DECLARE_##type(name); \
107
+ namespace caffe2 { \
108
+ extern real_type& FLAGS_##name ; \
89
109
} // namespace caffe2
90
110
91
- #define CAFFE2_DECLARE_int (name ) CAFFE2_GFLAGS_DECLARE_WRAPPER(int32, name)
92
- #define CAFFE2_DECLARE_int64 (name ) CAFFE2_GFLAGS_DECLARE_WRAPPER(int64, name)
93
- #define CAFFE2_DECLARE_double (name ) CAFFE2_GFLAGS_DECLARE_WRAPPER(double , name)
94
- #define CAFFE2_DECLARE_bool (name ) CAFFE2_GFLAGS_DECLARE_WRAPPER(bool , name)
95
- #define CAFFE2_DECLARE_string (name ) CAFFE2_GFLAGS_DECLARE_WRAPPER(string, name)
111
+ #define CAFFE2_DECLARE_int (name ) \
112
+ CAFFE2_GFLAGS_DECLARE_WRAPPER (int32, gflags::int32, name)
113
+ #define CAFFE2_DECLARE_int64 (name ) \
114
+ CAFFE2_GFLAGS_DECLARE_WRAPPER (int64, gflags::int64, name)
115
+ #define CAFFE2_DECLARE_double (name ) \
116
+ CAFFE2_GFLAGS_DECLARE_WRAPPER (double , double , name)
117
+ #define CAFFE2_DECLARE_bool (name ) \
118
+ CAFFE2_GFLAGS_DECLARE_WRAPPER (bool , bool , name)
119
+ #define CAFFE2_DECLARE_string (name ) \
120
+ CAFFE2_GFLAGS_DECLARE_WRAPPER (string, ::fLS ::clstring, name)
121
+
122
+ // //////////////////////////////////////////////////////////////////////////////
123
+ // End gflags section.
124
+ // //////////////////////////////////////////////////////////////////////////////
96
125
97
126
#else // CAFFE2_USE_GFLAGS
98
127
128
+ // //////////////////////////////////////////////////////////////////////////////
129
+ // Begin non-gflags section: providing equivalent functionality.
130
+ // //////////////////////////////////////////////////////////////////////////////
131
+
99
132
namespace caffe2 {
100
133
101
- class Caffe2FlagParser {
134
+ class CAFFE2_API Caffe2FlagParser {
102
135
public:
103
136
Caffe2FlagParser () {}
104
137
bool success () { return success_; }
@@ -117,39 +150,39 @@ CAFFE_DECLARE_REGISTRY(Caffe2FlagsRegistry, Caffe2FlagParser, const string&);
117
150
// write the CAFFE2_DEFINE_* and CAFFE2_DECLARE_* macros outside any namespace
118
151
// as well.
119
152
120
- #define CAFFE2_DEFINE_typed_var (type, name, default_value, help_str ) \
121
- namespace caffe2 { \
122
- CAFFE2_EXPORT type FLAGS_##name = default_value; \
123
- namespace { \
124
- class Caffe2FlagParser_ ##name : public Caffe2FlagParser { \
125
- public: \
126
- explicit Caffe2FlagParser_##name(const string& content) { \
127
- success_ = Caffe2FlagParser::Parse<type>(content, &FLAGS_##name); \
128
- } \
129
- }; \
130
- } \
131
- RegistererCaffe2FlagsRegistry g_Caffe2FlagsRegistry_##name( \
132
- #name, \
133
- Caffe2FlagsRegistry (), \
134
- RegistererCaffe2FlagsRegistry::DefaultCreator<Caffe2FlagParser_##name>, \
135
- " (" #type " , default " #default_value " ) " help_str); \
153
+ #define CAFFE2_DEFINE_typed_var (type, name, default_value, help_str ) \
154
+ namespace caffe2 { \
155
+ CAFFE2_EXPORT type FLAGS_##name = default_value; \
156
+ namespace { \
157
+ class Caffe2FlagParser_ ##name : public Caffe2FlagParser { \
158
+ public: \
159
+ explicit Caffe2FlagParser_##name(const string& content) { \
160
+ success_ = Caffe2FlagParser::Parse<type>(content, &FLAGS_##name); \
161
+ } \
162
+ }; \
163
+ } \
164
+ RegistererCaffe2FlagsRegistry g_Caffe2FlagsRegistry_##name( \
165
+ #name, \
166
+ Caffe2FlagsRegistry (), \
167
+ RegistererCaffe2FlagsRegistry::DefaultCreator<Caffe2FlagParser_##name>, \
168
+ " (" #type " , default " #default_value " ) " help_str); \
136
169
}
137
170
138
171
#define CAFFE2_DEFINE_int (name, default_value, help_str ) \
139
172
CAFFE2_DEFINE_typed_var (int , name, default_value, help_str)
140
- #define CAFFE2_DEFINE_int64 (name, default_value, help_str ) \
173
+ #define CAFFE2_DEFINE_int64 (name, default_value, help_str ) \
141
174
CAFFE2_DEFINE_typed_var (int64_t , name, default_value, help_str)
142
- #define CAFFE2_DEFINE_double (name, default_value, help_str ) \
175
+ #define CAFFE2_DEFINE_double (name, default_value, help_str ) \
143
176
CAFFE2_DEFINE_typed_var (double , name, default_value, help_str)
144
177
#define CAFFE2_DEFINE_bool (name, default_value, help_str ) \
145
178
CAFFE2_DEFINE_typed_var (bool , name, default_value, help_str)
146
179
#define CAFFE2_DEFINE_string (name, default_value, help_str ) \
147
180
CAFFE2_DEFINE_typed_var (string, name, default_value, help_str)
148
181
149
182
// DECLARE_typed_var should be used in header files and in the global namespace.
150
- #define CAFFE2_DECLARE_typed_var (type, name ) \
151
- namespace caffe2 { \
152
- CAFFE2_IMPORT extern type FLAGS_##name; \
183
+ #define CAFFE2_DECLARE_typed_var (type, name ) \
184
+ namespace caffe2 { \
185
+ CAFFE2_IMPORT extern type FLAGS_##name; \
153
186
} // namespace caffe2
154
187
155
188
#define CAFFE2_DECLARE_int (name ) CAFFE2_DECLARE_typed_var(int , name)
@@ -158,6 +191,10 @@ CAFFE_DECLARE_REGISTRY(Caffe2FlagsRegistry, Caffe2FlagParser, const string&);
158
191
#define CAFFE2_DECLARE_bool (name ) CAFFE2_DECLARE_typed_var(bool , name)
159
192
#define CAFFE2_DECLARE_string (name ) CAFFE2_DECLARE_typed_var(string, name)
160
193
194
+ // //////////////////////////////////////////////////////////////////////////////
195
+ // End non-gflags section.
196
+ // //////////////////////////////////////////////////////////////////////////////
197
+
161
198
#endif // CAFFE2_USE_GFLAGS
162
199
163
200
#endif // CAFFE2_CORE_FLAGS_H_
0 commit comments