| 1 |
// Copyright (c) 2021 Google LLC. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
#include <iostream> |
| 16 |
|
| 17 |
#include "source/opt/log.h" |
| 18 |
#include "spirv-tools/linter.hpp" |
| 19 |
#include "tools/io.h" |
| 20 |
#include "tools/util/cli_consumer.h" |
| 21 |
#include "tools/util/flags.h" |
| 22 |
|
| 23 |
namespace { |
| 24 |
|
| 25 |
constexpr auto kDefaultEnvironment = SPV_ENV_UNIVERSAL_1_6; |
| 26 |
constexpr auto kHelpTextFmt = |
| 27 |
R"(%s - Lint a SPIR-V binary module. |
| 28 |
|
| 29 |
Usage: %s [options] <filename> |
| 30 |
|
| 31 |
Options: |
| 32 |
|
| 33 |
-h, --help Print this help. |
| 34 |
--version Display assembler version information. |
| 35 |
)"; |
| 36 |
|
| 37 |
} // namespace |
| 38 |
|
| 39 |
// clang-format off |
| 40 |
FLAG_SHORT_bool( h, /* default_value= */ false, /* required= */ false); |
| 41 |
FLAG_LONG_bool( help, /* default_value= */ false, /* required= */ false); |
| 42 |
FLAG_LONG_bool( version, /* default_value= */ false, /* required= */ false); |
| 43 |
// clang-format on |
| 44 |
|
| 45 |
int main(int, const char** argv) { |
| 46 |
if (!flags::Parse(argv)) { |
| 47 |
return 1; |
| 48 |
} |
| 49 |
|
| 50 |
if (flags::h.value() || flags::help.value()) { |
| 51 |
printf(kHelpTextFmt, argv[0], argv[0]); |
| 52 |
return 0; |
| 53 |
} |
| 54 |
|
| 55 |
if (flags::version.value()) { |
| 56 |
printf("%s\n", spvSoftwareVersionDetailsString()); |
| 57 |
return 0; |
| 58 |
} |
| 59 |
|
| 60 |
if (flags::positional_arguments.size() != 1) { |
| 61 |
spvtools::Error(spvtools::utils::CLIMessageConsumer, nullptr, {}, |
| 62 |
"expected exactly one input file."); |
| 63 |
return 1; |
| 64 |
} |
| 65 |
|
| 66 |
spvtools::Linter linter(kDefaultEnvironment); |
| 67 |
linter.SetMessageConsumer(spvtools::utils::CLIMessageConsumer); |
| 68 |
std::vector<uint32_t> binary; |
| 69 |
if (!ReadBinaryFile(flags::positional_arguments[0].c_str(), &binary)) { |
| 70 |
return 1; |
| 71 |
} |
| 72 |
|
| 73 |
return linter.Run(binary.data(), binary.size()) ? 0 : 1; |
| 74 |
} |