| 1 |
// Copyright (c) 2017 Pierre Moreau |
| 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 <cstring> |
| 16 |
#include <iostream> |
| 17 |
#include <vector> |
| 18 |
|
| 19 |
#include "source/spirv_target_env.h" |
| 20 |
#include "source/table.h" |
| 21 |
#include "spirv-tools/libspirv.hpp" |
| 22 |
#include "spirv-tools/linker.hpp" |
| 23 |
#include "tools/io.h" |
| 24 |
|
| 25 |
void print_usage(char* argv0) { |
| 26 |
std::string target_env_list = spvTargetEnvList(27, 95); |
| 27 |
printf( |
| 28 |
R"(%s - Link SPIR-V binary files together. |
| 29 |
|
| 30 |
USAGE: %s [options] <filename> [<filename> ...] |
| 31 |
|
| 32 |
The SPIR-V binaries are read from the different <filename>. |
| 33 |
|
| 34 |
NOTE: The linker is a work in progress. |
| 35 |
|
| 36 |
Options: |
| 37 |
-h, --help Print this help. |
| 38 |
-o Name of the resulting linked SPIR-V binary. |
| 39 |
--create-library Link the binaries into a library, keeping all exported symbols. |
| 40 |
--allow-partial-linkage Allow partial linkage by accepting imported symbols to be unresolved. |
| 41 |
--verify-ids Verify that IDs in the resulting modules are truly unique. |
| 42 |
--version Display linker version information |
| 43 |
--target-env {%s} |
| 44 |
Use validation rules from the specified environment. |
| 45 |
)", |
| 46 |
argv0, argv0, target_env_list.c_str()); |
| 47 |
} |
| 48 |
|
| 49 |
int main(int argc, char** argv) { |
| 50 |
std::vector<const char*> inFiles; |
| 51 |
const char* outFile = nullptr; |
| 52 |
spv_target_env target_env = SPV_ENV_UNIVERSAL_1_0; |
| 53 |
spvtools::LinkerOptions options; |
| 54 |
bool continue_processing = true; |
| 55 |
int return_code = 0; |
| 56 |
|
| 57 |
for (int argi = 1; continue_processing && argi < argc; ++argi) { |
| 58 |
const char* cur_arg = argv[argi]; |
| 59 |
if ('-' == cur_arg[0]) { |
| 60 |
if (0 == strcmp(cur_arg, "-o")) { |
| 61 |
if (argi + 1 < argc) { |
| 62 |
if (!outFile) { |
| 63 |
outFile = argv[++argi]; |
| 64 |
} else { |
| 65 |
fprintf(stderr, "error: More than one output file specified\n"); |
| 66 |
continue_processing = false; |
| 67 |
return_code = 1; |
| 68 |
} |
| 69 |
} else { |
| 70 |
fprintf(stderr, "error: Missing argument to %s\n", cur_arg); |
| 71 |
continue_processing = false; |
| 72 |
return_code = 1; |
| 73 |
} |
| 74 |
} else if (0 == strcmp(cur_arg, "--create-library")) { |
| 75 |
options.SetCreateLibrary(true); |
| 76 |
} else if (0 == strcmp(cur_arg, "--verify-ids")) { |
| 77 |
options.SetVerifyIds(true); |
| 78 |
} else if (0 == strcmp(cur_arg, "--allow-partial-linkage")) { |
| 79 |
options.SetAllowPartialLinkage(true); |
| 80 |
} else if (0 == strcmp(cur_arg, "--version")) { |
| 81 |
printf("%s\n", spvSoftwareVersionDetailsString()); |
| 82 |
// TODO(dneto): Add OpenCL 2.2 at least. |
| 83 |
printf("Targets:\n %s\n %s\n %s\n", |
| 84 |
spvTargetEnvDescription(SPV_ENV_UNIVERSAL_1_1), |
| 85 |
spvTargetEnvDescription(SPV_ENV_VULKAN_1_0), |
| 86 |
spvTargetEnvDescription(SPV_ENV_UNIVERSAL_1_2)); |
| 87 |
continue_processing = false; |
| 88 |
return_code = 0; |
| 89 |
} else if (0 == strcmp(cur_arg, "--help") || 0 == strcmp(cur_arg, "-h")) { |
| 90 |
print_usage(argv[0]); |
| 91 |
continue_processing = false; |
| 92 |
return_code = 0; |
| 93 |
} else if (0 == strcmp(cur_arg, "--target-env")) { |
| 94 |
if (argi + 1 < argc) { |
| 95 |
const auto env_str = argv[++argi]; |
| 96 |
if (!spvParseTargetEnv(env_str, &target_env)) { |
| 97 |
fprintf(stderr, "error: Unrecognized target env: %s\n", env_str); |
| 98 |
continue_processing = false; |
| 99 |
return_code = 1; |
| 100 |
} |
| 101 |
} else { |
| 102 |
fprintf(stderr, "error: Missing argument to --target-env\n"); |
| 103 |
continue_processing = false; |
| 104 |
return_code = 1; |
| 105 |
} |
| 106 |
} |
| 107 |
} else { |
| 108 |
inFiles.push_back(cur_arg); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
// Exit if command line parsing was not successful. |
| 113 |
if (!continue_processing) { |
| 114 |
return return_code; |
| 115 |
} |
| 116 |
|
| 117 |
if (inFiles.empty()) { |
| 118 |
fprintf(stderr, "error: No input file specified\n"); |
| 119 |
return 1; |
| 120 |
} |
| 121 |
|
| 122 |
std::vector<std::vector<uint32_t>> contents(inFiles.size()); |
| 123 |
for (size_t i = 0u; i < inFiles.size(); ++i) { |
| 124 |
if (!ReadFile<uint32_t>(inFiles[i], "rb", &contents[i])) return 1; |
| 125 |
} |
| 126 |
|
| 127 |
const spvtools::MessageConsumer consumer = [](spv_message_level_t level, |
| 128 |
const char*, |
| 129 |
const spv_position_t& position, |
| 130 |
const char* message) { |
| 131 |
switch (level) { |
| 132 |
case SPV_MSG_FATAL: |
| 133 |
case SPV_MSG_INTERNAL_ERROR: |
| 134 |
case SPV_MSG_ERROR: |
| 135 |
std::cerr << "error: " << position.index << ": " << message |
| 136 |
<< std::endl; |
| 137 |
break; |
| 138 |
case SPV_MSG_WARNING: |
| 139 |
std::cout << "warning: " << position.index << ": " << message |
| 140 |
<< std::endl; |
| 141 |
break; |
| 142 |
case SPV_MSG_INFO: |
| 143 |
std::cout << "info: " << position.index << ": " << message << std::endl; |
| 144 |
break; |
| 145 |
default: |
| 146 |
break; |
| 147 |
} |
| 148 |
}; |
| 149 |
spvtools::Context context(target_env); |
| 150 |
context.SetMessageConsumer(consumer); |
| 151 |
|
| 152 |
std::vector<uint32_t> linkingResult; |
| 153 |
spv_result_t status = Link(context, contents, &linkingResult, options); |
| 154 |
|
| 155 |
if (!WriteFile<uint32_t>(outFile, "wb", linkingResult.data(), |
| 156 |
linkingResult.size())) |
| 157 |
return 1; |
| 158 |
|
| 159 |
return status == SPV_SUCCESS ? 0 : 1; |
| 160 |
} |