2016-01-07 18:44:22 +00:00
|
|
|
// Copyright (c) 2015-2016 The Khronos Group Inc.
|
2015-05-22 17:26:19 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2015-05-22 17:26:19 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2015-05-22 17:26:19 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2015-05-22 17:26:19 +00:00
|
|
|
|
2016-02-18 17:21:10 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
2015-05-22 17:26:19 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2016-06-30 18:24:04 +00:00
|
|
|
#include "source/spirv_target_env.h"
|
2016-02-17 19:44:00 +00:00
|
|
|
#include "spirv-tools/libspirv.h"
|
2016-06-30 18:24:04 +00:00
|
|
|
#include "tools/io.h"
|
2015-11-11 17:45:23 +00:00
|
|
|
|
2015-11-02 14:41:20 +00:00
|
|
|
void print_usage(char* argv0) {
|
2019-06-20 13:41:28 +00:00
|
|
|
std::string target_env_list = spvTargetEnvList(19, 80);
|
2015-05-22 17:26:19 +00:00
|
|
|
printf(
|
2015-11-13 01:42:16 +00:00
|
|
|
R"(%s - Create a SPIR-V binary module from SPIR-V assembly text
|
|
|
|
|
2016-02-18 17:21:10 +00:00
|
|
|
Usage: %s [options] [<filename>]
|
2015-11-13 01:42:16 +00:00
|
|
|
|
2016-02-18 17:21:10 +00:00
|
|
|
The SPIR-V assembly text is read from <filename>. If no file is specified,
|
2018-08-22 14:12:07 +00:00
|
|
|
or if the filename is "-", then the assembly text is read from standard input.
|
2016-02-18 17:21:10 +00:00
|
|
|
The SPIR-V binary module is written to file "out.spv", unless the -o option
|
|
|
|
is used.
|
2015-11-13 01:42:16 +00:00
|
|
|
|
|
|
|
Options:
|
|
|
|
|
2016-03-17 18:04:32 +00:00
|
|
|
-h, --help Print this help.
|
2015-11-13 01:42:16 +00:00
|
|
|
|
2016-02-18 22:43:23 +00:00
|
|
|
-o <filename> Set the output filename. Use '-' to mean stdout.
|
2016-02-22 21:07:19 +00:00
|
|
|
--version Display assembler version information.
|
2017-04-11 23:46:15 +00:00
|
|
|
--preserve-numeric-ids
|
|
|
|
Numeric IDs in the binary will have the same values as in the
|
|
|
|
source. Non-numeric IDs are allocated by filling in the gaps,
|
|
|
|
starting with 1 and going up.
|
2019-06-20 13:41:28 +00:00
|
|
|
--target-env {%s}
|
|
|
|
Use specified environment.
|
2015-11-13 01:42:16 +00:00
|
|
|
)",
|
2019-06-20 13:41:28 +00:00
|
|
|
argv0, argv0, target_env_list.c_str());
|
2015-05-22 17:26:19 +00:00
|
|
|
}
|
|
|
|
|
2019-09-13 18:59:02 +00:00
|
|
|
static const auto kDefaultEnvironment = SPV_ENV_UNIVERSAL_1_5;
|
2017-03-14 16:43:41 +00:00
|
|
|
|
2015-11-02 14:41:20 +00:00
|
|
|
int main(int argc, char** argv) {
|
|
|
|
const char* inFile = nullptr;
|
|
|
|
const char* outFile = nullptr;
|
2017-04-11 23:46:15 +00:00
|
|
|
uint32_t options = 0;
|
2017-03-14 16:43:41 +00:00
|
|
|
spv_target_env target_env = kDefaultEnvironment;
|
2015-05-22 17:26:19 +00:00
|
|
|
for (int argi = 1; argi < argc; ++argi) {
|
|
|
|
if ('-' == argv[argi][0]) {
|
2015-11-05 22:45:09 +00:00
|
|
|
switch (argv[argi][1]) {
|
2015-11-13 01:42:16 +00:00
|
|
|
case 'h': {
|
|
|
|
print_usage(argv[0]);
|
|
|
|
return 0;
|
|
|
|
}
|
2015-11-05 22:45:09 +00:00
|
|
|
case 'o': {
|
|
|
|
if (!outFile && argi + 1 < argc) {
|
|
|
|
outFile = argv[++argi];
|
|
|
|
} else {
|
2015-09-10 21:42:41 +00:00
|
|
|
print_usage(argv[0]);
|
|
|
|
return 1;
|
2015-11-05 22:45:09 +00:00
|
|
|
}
|
|
|
|
} break;
|
2016-02-18 17:21:10 +00:00
|
|
|
case 0: {
|
|
|
|
// Setting a filename of "-" to indicate stdin.
|
|
|
|
if (!inFile) {
|
|
|
|
inFile = argv[argi];
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "error: More than one input file specified\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} break;
|
2016-02-22 21:07:19 +00:00
|
|
|
case '-': {
|
|
|
|
// Long options
|
|
|
|
if (0 == strcmp(argv[argi], "--version")) {
|
2016-04-22 00:50:11 +00:00
|
|
|
printf("%s\n", spvSoftwareVersionDetailsString());
|
2016-03-31 21:26:31 +00:00
|
|
|
printf("Target: %s\n",
|
2017-03-14 16:43:41 +00:00
|
|
|
spvTargetEnvDescription(kDefaultEnvironment));
|
2016-02-22 21:07:19 +00:00
|
|
|
return 0;
|
2017-09-01 22:12:15 +00:00
|
|
|
} else if (0 == strcmp(argv[argi], "--help")) {
|
2016-03-17 18:04:32 +00:00
|
|
|
print_usage(argv[0]);
|
|
|
|
return 0;
|
2017-09-01 22:12:15 +00:00
|
|
|
} else if (0 == strcmp(argv[argi], "--preserve-numeric-ids")) {
|
2017-04-11 23:46:15 +00:00
|
|
|
options |= SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS;
|
2017-09-01 22:12:15 +00:00
|
|
|
} else if (0 == strcmp(argv[argi], "--target-env")) {
|
2016-05-11 13:48:52 +00:00
|
|
|
if (argi + 1 < argc) {
|
|
|
|
const auto env_str = argv[++argi];
|
|
|
|
if (!spvParseTargetEnv(env_str, &target_env)) {
|
|
|
|
fprintf(stderr, "error: Unrecognized target env: %s\n",
|
|
|
|
env_str);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "error: Missing argument to --target-env\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2017-09-01 22:12:15 +00:00
|
|
|
} else {
|
|
|
|
fprintf(stderr, "error: Unrecognized option: %s\n\n", argv[argi]);
|
|
|
|
print_usage(argv[0]);
|
|
|
|
return 1;
|
2016-05-11 13:48:52 +00:00
|
|
|
}
|
2016-02-22 21:07:19 +00:00
|
|
|
} break;
|
2015-11-05 22:45:09 +00:00
|
|
|
default:
|
2017-09-01 22:12:15 +00:00
|
|
|
fprintf(stderr, "error: Unrecognized option: %s\n\n", argv[argi]);
|
2015-11-05 22:45:09 +00:00
|
|
|
print_usage(argv[0]);
|
|
|
|
return 1;
|
2015-05-22 17:26:19 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!inFile) {
|
|
|
|
inFile = argv[argi];
|
|
|
|
} else {
|
2016-02-18 17:21:10 +00:00
|
|
|
fprintf(stderr, "error: More than one input file specified\n");
|
2015-05-22 17:26:19 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!outFile) {
|
|
|
|
outFile = "out.spv";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<char> contents;
|
2016-06-30 18:24:04 +00:00
|
|
|
if (!ReadFile<char>(inFile, "r", &contents)) return 1;
|
2015-05-22 17:26:19 +00:00
|
|
|
|
|
|
|
spv_binary binary;
|
|
|
|
spv_diagnostic diagnostic = nullptr;
|
2016-05-11 13:48:52 +00:00
|
|
|
spv_context context = spvContextCreate(target_env);
|
2017-04-11 23:46:15 +00:00
|
|
|
spv_result_t error = spvTextToBinaryWithOptions(
|
|
|
|
context, contents.data(), contents.size(), options, &binary, &diagnostic);
|
2015-11-12 18:48:30 +00:00
|
|
|
spvContextDestroy(context);
|
2015-09-11 18:31:27 +00:00
|
|
|
if (error) {
|
|
|
|
spvDiagnosticPrint(diagnostic);
|
|
|
|
spvDiagnosticDestroy(diagnostic);
|
|
|
|
return error;
|
|
|
|
}
|
2015-05-22 17:26:19 +00:00
|
|
|
|
2016-06-30 19:44:36 +00:00
|
|
|
if (!WriteFile<uint32_t>(outFile, "wb", binary->code, binary->wordCount)) {
|
|
|
|
spvBinaryDestroy(binary);
|
2015-05-22 17:26:19 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
spvBinaryDestroy(binary);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|