usage_config.cc 5.3 KB

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