1 //
2 // Copyright 2019 The Abseil Authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // https://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15
16 #include "absl/flags/usage_config.h"
17
18 #include <functional>
19 #include <iostream>
20 #include <string>
21
22 #include "absl/base/attributes.h"
23 #include "absl/base/config.h"
24 #include "absl/base/const_init.h"
25 #include "absl/base/thread_annotations.h"
26 #include "absl/flags/internal/path_util.h"
27 #include "absl/flags/internal/program_name.h"
28 #include "absl/strings/match.h"
29 #include "absl/strings/string_view.h"
30 #include "absl/strings/strip.h"
31 #include "absl/synchronization/mutex.h"
32
33 extern "C" {
34
35 // Additional report of fatal usage error message before we std::exit. Error is
36 // fatal if is_fatal argument to ReportUsageError is true.
ABSL_INTERNAL_C_SYMBOL(AbslInternalReportFatalUsageError)37 ABSL_ATTRIBUTE_WEAK void ABSL_INTERNAL_C_SYMBOL(
38 AbslInternalReportFatalUsageError)(absl::string_view) {}
39
40 } // extern "C"
41
42 namespace absl {
43 ABSL_NAMESPACE_BEGIN
44 namespace flags_internal {
45
46 namespace {
47
48 // --------------------------------------------------------------------
49 // Returns true if flags defined in the filename should be reported with
50 // -helpshort flag.
51
ContainsHelpshortFlags(absl::string_view filename)52 bool ContainsHelpshortFlags(absl::string_view filename) {
53 // By default we only want flags in binary's main. We expect the main
54 // routine to reside in <program>.cc or <program>-main.cc or
55 // <program>_main.cc, where the <program> is the name of the binary
56 // (without .exe on Windows).
57 auto suffix = flags_internal::Basename(filename);
58 auto program_name = flags_internal::ShortProgramInvocationName();
59 absl::string_view program_name_ref = program_name;
60 #if defined(_WIN32)
61 absl::ConsumeSuffix(&program_name_ref, ".exe");
62 #endif
63 if (!absl::ConsumePrefix(&suffix, program_name_ref))
64 return false;
65 return absl::StartsWith(suffix, ".") || absl::StartsWith(suffix, "-main.") ||
66 absl::StartsWith(suffix, "_main.");
67 }
68
69 // --------------------------------------------------------------------
70 // Returns true if flags defined in the filename should be reported with
71 // -helppackage flag.
72
ContainsHelppackageFlags(absl::string_view filename)73 bool ContainsHelppackageFlags(absl::string_view filename) {
74 // TODO(rogeeff): implement properly when registry is available.
75 return ContainsHelpshortFlags(filename);
76 }
77
78 // --------------------------------------------------------------------
79 // Generates program version information into supplied output.
80
VersionString()81 std::string VersionString() {
82 std::string version_str(flags_internal::ShortProgramInvocationName());
83
84 version_str += "\n";
85
86 #if !defined(NDEBUG)
87 version_str += "Debug build (NDEBUG not #defined)\n";
88 #endif
89
90 return version_str;
91 }
92
93 // --------------------------------------------------------------------
94 // Normalizes the filename specific to the build system/filesystem used.
95
NormalizeFilename(absl::string_view filename)96 std::string NormalizeFilename(absl::string_view filename) {
97 // Skip any leading slashes
98 auto pos = filename.find_first_not_of("\\/");
99 if (pos == absl::string_view::npos) return "";
100
101 filename.remove_prefix(pos);
102 return std::string(filename);
103 }
104
105 // --------------------------------------------------------------------
106
107 ABSL_CONST_INIT absl::Mutex custom_usage_config_guard(absl::kConstInit);
108 ABSL_CONST_INIT FlagsUsageConfig* custom_usage_config
109 ABSL_GUARDED_BY(custom_usage_config_guard) = nullptr;
110
111 } // namespace
112
GetUsageConfig()113 FlagsUsageConfig GetUsageConfig() {
114 absl::MutexLock l(&custom_usage_config_guard);
115
116 if (custom_usage_config) return *custom_usage_config;
117
118 FlagsUsageConfig default_config;
119 default_config.contains_helpshort_flags = &ContainsHelpshortFlags;
120 default_config.contains_help_flags = &ContainsHelppackageFlags;
121 default_config.contains_helppackage_flags = &ContainsHelppackageFlags;
122 default_config.version_string = &VersionString;
123 default_config.normalize_filename = &NormalizeFilename;
124
125 return default_config;
126 }
127
ReportUsageError(absl::string_view msg,bool is_fatal)128 void ReportUsageError(absl::string_view msg, bool is_fatal) {
129 std::cerr << "ERROR: " << msg << std::endl;
130
131 if (is_fatal) {
132 ABSL_INTERNAL_C_SYMBOL(AbslInternalReportFatalUsageError)(msg);
133 }
134 }
135
136 } // namespace flags_internal
137
SetFlagsUsageConfig(FlagsUsageConfig usage_config)138 void SetFlagsUsageConfig(FlagsUsageConfig usage_config) {
139 absl::MutexLock l(&flags_internal::custom_usage_config_guard);
140
141 if (!usage_config.contains_helpshort_flags)
142 usage_config.contains_helpshort_flags =
143 flags_internal::ContainsHelpshortFlags;
144
145 if (!usage_config.contains_help_flags)
146 usage_config.contains_help_flags = flags_internal::ContainsHelppackageFlags;
147
148 if (!usage_config.contains_helppackage_flags)
149 usage_config.contains_helppackage_flags =
150 flags_internal::ContainsHelppackageFlags;
151
152 if (!usage_config.version_string)
153 usage_config.version_string = flags_internal::VersionString;
154
155 if (!usage_config.normalize_filename)
156 usage_config.normalize_filename = flags_internal::NormalizeFilename;
157
158 if (flags_internal::custom_usage_config)
159 *flags_internal::custom_usage_config = usage_config;
160 else
161 flags_internal::custom_usage_config = new FlagsUsageConfig(usage_config);
162 }
163
164 ABSL_NAMESPACE_END
165 } // namespace absl
166