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