|
| 1 | +// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +#include <stdio.h> |
| 6 | + |
| 7 | +#include <cctype> |
| 8 | +#include <map> |
| 9 | +#include <regex> |
| 10 | + |
| 11 | +#include "bit.h" |
| 12 | +#include "llvm/ADT/StringRef.h" |
| 13 | +#include "llvm/Support/InitLLVM.h" |
| 14 | +#include "llvm/Support/LineIterator.h" |
| 15 | +#include "llvm/Support/MemoryBuffer.h" |
| 16 | +#include "llvm/Support/Path.h" |
| 17 | +#include "llvm/Support/Process.h" |
| 18 | +#include "llvm/Support/Program.h" |
| 19 | +#include "llvm/Support/Regex.h" |
| 20 | +#include "llvm/Support/VirtualFileSystem.h" |
| 21 | +#include "llvm/Support/WithColor.h" |
| 22 | + |
| 23 | +using namespace llvm; |
| 24 | + |
| 25 | +namespace { |
| 26 | + |
| 27 | +StringRef tool_name; |
| 28 | + |
| 29 | +LLVM_ATTRIBUTE_NORETURN void Fail(Twine message) { |
| 30 | + WithColor::error(errs(), tool_name) << message << ".\n"; |
| 31 | + errs().flush(); |
| 32 | + exit(1); |
| 33 | +} |
| 34 | + |
| 35 | +LLVM_ATTRIBUTE_NORETURN void Fail(Error e) { |
| 36 | + assert(E); |
| 37 | + std::string buf; |
| 38 | + raw_string_ostream os(buf); |
| 39 | + logAllUnhandledErrors(std::move(e), os); |
| 40 | + os.flush(); |
| 41 | + WithColor::error(errs(), tool_name) << buf; |
| 42 | + exit(1); |
| 43 | +} |
| 44 | + |
| 45 | +LLVM_ATTRIBUTE_NORETURN void ReportError(StringRef file, std::error_code ec) { |
| 46 | + assert(ec); |
| 47 | + Fail(createFileError(file, ec)); |
| 48 | +} |
| 49 | + |
| 50 | +std::string ReadFile(FILE* file) { |
| 51 | + std::string output; |
| 52 | + constexpr size_t buf_size = 256; |
| 53 | + char buf[buf_size]; |
| 54 | + size_t size; |
| 55 | + while ((size = fread(buf, buf_size, sizeof(buf[0]), file))) |
| 56 | + output.append(buf, buf + buf_size); |
| 57 | + return output; |
| 58 | +} |
| 59 | + |
| 60 | +bool IsPosixFullyPortablePath(StringRef path) { |
| 61 | + const char* extra = "._-/"; |
| 62 | + for (auto c : path) |
| 63 | + if (!isalnum(c) && !strchr(extra, c)) return false; |
| 64 | + return true; |
| 65 | +} |
| 66 | + |
| 67 | +} // namespace |
| 68 | + |
| 69 | +int main(int argc, char** argv) { |
| 70 | + InitLLVM X(argc, argv); |
| 71 | + |
| 72 | + // Make sure we have both arguments. |
| 73 | + tool_name = argv[0]; |
| 74 | + if (argc != 3) Fail("expected exactly 2 arguments"); |
| 75 | + |
| 76 | + // Makes sure that stdin/stdout are setup correctly. |
| 77 | + if (sys::Process::FixupStandardFileDescriptors()) |
| 78 | + Fail("std in/out fixup failed"); |
| 79 | + |
| 80 | + // Set our config. |
| 81 | + Config config; |
| 82 | + config.filename = argv[1]; |
| 83 | + config.out_dir = argv[2]; |
| 84 | + |
| 85 | + // Make sure we have valid filepaths. |
| 86 | + if (!IsPosixFullyPortablePath(config.filename)) |
| 87 | + Fail("'" + config.filename + "' is not a posix fully portable filename"); |
| 88 | + if (!IsPosixFullyPortablePath(config.out_dir)) |
| 89 | + Fail("'" + config.out_dir + "' is not a posix fully portable filename"); |
| 90 | + |
| 91 | + // Compute substitutions. |
| 92 | + auto subs = GetSubstitutions(config); |
| 93 | + |
| 94 | + // The lines we execute are allowed to assume that %p will exist. |
| 95 | + sys::fs::create_directory(subs["p"]); |
| 96 | + |
| 97 | + // Open the file for reading. |
| 98 | + auto buf_or = vfs::getRealFileSystem()->getBufferForFile(config.filename); |
| 99 | + if (!buf_or) ReportError(config.filename, buf_or.getError()); |
| 100 | + auto buf = std::move(*buf_or); |
| 101 | + |
| 102 | + // Now iterate over the lines in the file. |
| 103 | + line_iterator it{*buf}; |
| 104 | + int count = 0; |
| 105 | + for (StringRef line = *it; !it.is_at_end(); line = *++it) { |
| 106 | + auto cmd = GetCommand(line); |
| 107 | + if (!cmd) continue; |
| 108 | + ++count; |
| 109 | + auto subbed = PerformSubstitutions(subs, *cmd); |
| 110 | + FILE* file = popen(subbed.c_str(), "r"); |
| 111 | + std::string output = ReadFile(file); |
| 112 | + if (pclose(file) != 0) { |
| 113 | + errs() << output << "\n"; |
| 114 | + Fail("Failure on line " + Twine(it.line_number()) + "\n\t" + subbed + ""); |
| 115 | + } |
| 116 | + } |
| 117 | + if (count == 0) { |
| 118 | + Fail("No commands to run"); |
| 119 | + } |
| 120 | + outs() << "Commands run: " << count << "\n"; |
| 121 | + return 0; |
| 122 | +} |
0 commit comments