|
| 1 | +//===--- swift-def-to-strings-converter.cpp -------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +// |
| 13 | +// Create a .strings file from the diagnostic messages text in `.def` files. |
| 14 | +// |
| 15 | +//===----------------------------------------------------------------------===// |
| 16 | + |
| 17 | +#include "swift/Basic/LLVMInitialize.h" |
| 18 | +#include "swift/Basic/Compiler.h" |
| 19 | +#include "swift/Localization/LocalizationFormat.h" |
| 20 | +#include "llvm/ADT/ArrayRef.h" |
| 21 | +#include "llvm/ADT/SmallString.h" |
| 22 | +#include "llvm/ADT/StringExtras.h" |
| 23 | +#include "llvm/Support/CommandLine.h" |
| 24 | +#include "llvm/Support/Compiler.h" |
| 25 | +#include "llvm/Support/EndianStream.h" |
| 26 | +#include "llvm/Support/FileSystem.h" |
| 27 | +#include "llvm/Support/MemoryBuffer.h" |
| 28 | +#include "llvm/Support/Path.h" |
| 29 | +#include "llvm/Support/raw_ostream.h" |
| 30 | +#include <cstdlib> |
| 31 | +#include <string> |
| 32 | +#include <system_error> |
| 33 | + |
| 34 | +static constexpr const char *const diagnosticID[] = { |
| 35 | +#define DIAG(KIND, ID, Options, Text, Signature) #ID, |
| 36 | +#include "swift/AST/DiagnosticsAll.def" |
| 37 | +}; |
| 38 | + |
| 39 | +static constexpr const char *const diagnosticMessages[] = { |
| 40 | +#define DIAG(KIND, ID, Options, Text, Signature) Text, |
| 41 | +#include "swift/AST/DiagnosticsAll.def" |
| 42 | +}; |
| 43 | + |
| 44 | +enum LocalDiagID : uint32_t { |
| 45 | +#define DIAG(KIND, ID, Options, Text, Signature) ID, |
| 46 | +#include "swift/AST/DiagnosticsAll.def" |
| 47 | + NumDiags |
| 48 | +}; |
| 49 | + |
| 50 | +namespace options { |
| 51 | + |
| 52 | +static llvm::cl::OptionCategory |
| 53 | + Category("swift-def-to-strings-converter Options"); |
| 54 | + |
| 55 | +static llvm::cl::opt<std::string> |
| 56 | + OutputDirectory("output-directory", |
| 57 | + llvm::cl::desc("Directory for the output file"), |
| 58 | + llvm::cl::cat(Category)); |
| 59 | + |
| 60 | +static llvm::cl::opt<std::string> |
| 61 | + OutputFilename("output-filename", |
| 62 | + llvm::cl::desc("Filename for the output file"), |
| 63 | + llvm::cl::cat(Category)); |
| 64 | + |
| 65 | +} // namespace options |
| 66 | + |
| 67 | +int main(int argc, char *argv[]) { |
| 68 | + PROGRAM_START(argc, argv); |
| 69 | + |
| 70 | + llvm::cl::HideUnrelatedOptions(options::Category); |
| 71 | + llvm::cl::ParseCommandLineOptions(argc, argv, |
| 72 | + "Swift `.def` to `.strings` Converter\n"); |
| 73 | + |
| 74 | + llvm::SmallString<128> LocalizedFilePath; |
| 75 | + if (options::OutputFilename.empty()) { |
| 76 | + // The default language for localization is English |
| 77 | + std::string defaultLocaleCode = "en"; |
| 78 | + LocalizedFilePath = options::OutputDirectory; |
| 79 | + llvm::sys::path::append(LocalizedFilePath, defaultLocaleCode); |
| 80 | + llvm::sys::path::replace_extension(LocalizedFilePath, ".strings"); |
| 81 | + } else { |
| 82 | + LocalizedFilePath = options::OutputFilename; |
| 83 | + } |
| 84 | + |
| 85 | + std::error_code error; |
| 86 | + llvm::raw_fd_ostream OS(LocalizedFilePath.str(), error, |
| 87 | + llvm::sys::fs::OF_None); |
| 88 | + |
| 89 | + if (OS.has_error() || error) { |
| 90 | + llvm::errs() << "Error has occurred while trying to write to " |
| 91 | + << LocalizedFilePath.str() |
| 92 | + << " with error code: " << error.message() << "\n"; |
| 93 | + return EXIT_FAILURE; |
| 94 | + } |
| 95 | + |
| 96 | + llvm::ArrayRef<const char *> ids(diagnosticID, LocalDiagID::NumDiags); |
| 97 | + llvm::ArrayRef<const char *> messages(diagnosticMessages, |
| 98 | + LocalDiagID::NumDiags); |
| 99 | + |
| 100 | + swift::diag::DefToStringsConverter converter(ids, messages); |
| 101 | + converter.convert(OS); |
| 102 | + |
| 103 | + return EXIT_SUCCESS; |
| 104 | +} |
0 commit comments