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 <iostream>
19 #include <string>
20
21 #include "absl/base/attributes.h"
22 #include "absl/base/config.h"
23 #include "absl/base/const_init.h"
24 #include "absl/base/thread_annotations.h"
25 #include "absl/flags/internal/path_util.h"
26 #include "absl/flags/internal/program_name.h"
27 #include "absl/strings/match.h"
28 #include "absl/strings/string_view.h"
29 #include "absl/strings/strip.h"
30 #include "absl/synchronization/mutex.h"
31
32 extern "C" {
33
34 // Additional report of fatal usage error message before we std::exit. Error is
35 // fatal if is_fatal argument to ReportUsageError is true.
AbslInternalReportFatalUsageError(absl::string_view)36 ABSL_ATTRIBUTE_WEAK void AbslInternalReportFatalUsageError(absl::string_view) {}
37
38 } // extern "C"
39
40 namespace absl {
41 ABSL_NAMESPACE_BEGIN
42 namespace flags_internal {
43
44 namespace {
45
46 // --------------------------------------------------------------------
47 // Returns true if flags defined in the filename should be reported with
48 // -helpshort flag.
49
ContainsHelpshortFlags(absl::string_view filename)50 bool ContainsHelpshortFlags(absl::string_view filename) {
51 // By default we only want flags in binary's main. We expect the main
52 // routine to reside in <program>.cc or <program>-main.cc or
53 // <program>_main.cc, where the <program> is the name of the binary.
54 auto suffix = flags_internal::Basename(filename);
55 if (!absl::ConsumePrefix(&suffix,
56 flags_internal::ShortProgramInvocationName()))
57 return false;
58 return absl::StartsWith(suffix, ".") || absl::StartsWith(suffix, "-main.") ||
59 absl::StartsWith(suffix, "_main.");
60 }
61
62 // --------------------------------------------------------------------
63 // Returns true if flags defined in the filename should be reported with
64 // -helppackage flag.
65
ContainsHelppackageFlags(absl::string_view filename)66 bool ContainsHelppackageFlags(absl::string_view filename) {
67 // TODO(rogeeff): implement properly when registry is available.
68 return ContainsHelpshortFlags(filename);
69 }
70
71 // --------------------------------------------------------------------
72 // Generates program version information into supplied output.
73
VersionString()74 std::string VersionString() {
75 std::string version_str(flags_internal::ShortProgramInvocationName());
76
77 version_str += "\n";
78
79 #if !defined(NDEBUG)
80 version_str += "Debug build (NDEBUG not #defined)\n";
81 #endif
82
83 return version_str;
84 }
85
86 // --------------------------------------------------------------------
87 // Normalizes the filename specific to the build system/filesystem used.
88
NormalizeFilename(absl::string_view filename)89 std::string NormalizeFilename(absl::string_view filename) {
90 // Skip any leading slashes
91 auto pos = filename.find_first_not_of("\\/");
92 if (pos == absl::string_view::npos) return "";
93
94 filename.remove_prefix(pos);
95 return std::string(filename);
96 }
97
98 // --------------------------------------------------------------------
99
100 ABSL_CONST_INIT absl::Mutex custom_usage_config_guard(absl::kConstInit);
101 ABSL_CONST_INIT FlagsUsageConfig* custom_usage_config
102 ABSL_GUARDED_BY(custom_usage_config_guard) = nullptr;
103
104 } // namespace
105
GetUsageConfig()106 FlagsUsageConfig GetUsageConfig() {
107 absl::MutexLock l(&custom_usage_config_guard);
108
109 if (custom_usage_config) return *custom_usage_config;
110
111 FlagsUsageConfig default_config;
112 default_config.contains_helpshort_flags = &ContainsHelpshortFlags;
113 default_config.contains_help_flags = &ContainsHelppackageFlags;
114 default_config.contains_helppackage_flags = &ContainsHelppackageFlags;
115 default_config.version_string = &VersionString;
116 default_config.normalize_filename = &NormalizeFilename;
117
118 return default_config;
119 }
120
ReportUsageError(absl::string_view msg,bool is_fatal)121 void ReportUsageError(absl::string_view msg, bool is_fatal) {
122 std::cerr << "ERROR: " << msg << std::endl;
123
124 if (is_fatal) {
125 AbslInternalReportFatalUsageError(msg);
126 }
127 }
128
129 } // namespace flags_internal
130
SetFlagsUsageConfig(FlagsUsageConfig usage_config)131 void SetFlagsUsageConfig(FlagsUsageConfig usage_config) {
132 absl::MutexLock l(&flags_internal::custom_usage_config_guard);
133
134 if (!usage_config.contains_helpshort_flags)
135 usage_config.contains_helpshort_flags =
136 flags_internal::ContainsHelpshortFlags;
137
138 if (!usage_config.contains_help_flags)
139 usage_config.contains_help_flags = flags_internal::ContainsHelppackageFlags;
140
141 if (!usage_config.contains_helppackage_flags)
142 usage_config.contains_helppackage_flags =
143 flags_internal::ContainsHelppackageFlags;
144
145 if (!usage_config.version_string)
146 usage_config.version_string = flags_internal::VersionString;
147
148 if (!usage_config.normalize_filename)
149 usage_config.normalize_filename = flags_internal::NormalizeFilename;
150
151 if (flags_internal::custom_usage_config)
152 *flags_internal::custom_usage_config = usage_config;
153 else
154 flags_internal::custom_usage_config = new FlagsUsageConfig(usage_config);
155 }
156
157 ABSL_NAMESPACE_END
158 } // namespace absl
159