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