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 "spirv-tools/linker.hpp"
16
17 #include <cassert>
18 #include <cstring>
19 #include <iostream>
20 #include <vector>
21
22 #include "source/spirv_target_env.h"
23 #include "source/table.h"
24 #include "spirv-tools/libspirv.hpp"
25 #include "tools/io.h"
26 #include "tools/util/flags.h"
27
28 namespace {
29
30 constexpr auto kDefaultEnvironment = "spv1.6";
31
print_usage(const char * program)32 void print_usage(const char* program) {
33 std::string target_env_list = spvTargetEnvList(16, 80);
34 // NOTE: Please maintain flags in lexicographical order.
35 printf(
36 R"(%s - Link SPIR-V binary files together.
37
38 USAGE: %s [options] [-o <output>] <input>...
39
40 The SPIR-V binaries are read from the different <input>(s).
41 The SPIR-V resulting linked binary module is written to the file "out.spv"
42 unless the -o option is used; if <output> is "-", it is written to the standard
43 output.
44
45 NOTE: The linker is a work in progress.
46
47 Options (in lexicographical order):
48 --allow-partial-linkage
49 Allow partial linkage by accepting imported symbols to be
50 unresolved.
51 --create-library
52 Link the binaries into a library, keeping all exported symbols.
53 -h, --help
54 Print this help.
55 --target-env <env>
56 Set the environment used for interpreting the inputs. Without
57 this option the environment defaults to spv1.6. <env> must be
58 one of {%s}.
59 NOTE: The SPIR-V version used by the linked binary module
60 depends only on the version of the inputs, and is not affected
61 by this option.
62 --verify-ids
63 Verify that IDs in the resulting modules are truly unique.
64 --version
65 Display linker version information.
66 )",
67 program, program, target_env_list.c_str());
68 }
69
70 } // namespace
71
72 // clang-format off
73 FLAG_SHORT_bool( h, /* default_value= */ false, /* required= */ false);
74 FLAG_LONG_bool( help, /* default_value= */ false, /* required= */false);
75 FLAG_LONG_bool( version, /* default_value= */ false, /* required= */ false);
76 FLAG_LONG_bool( verify_ids, /* default_value= */ false, /* required= */ false);
77 FLAG_LONG_bool( create_library, /* default_value= */ false, /* required= */ false);
78 FLAG_LONG_bool( allow_partial_linkage, /* default_value= */ false, /* required= */ false);
79 FLAG_SHORT_string(o, /* default_value= */ "", /* required= */ false);
80 FLAG_LONG_string( target_env, /* default_value= */ kDefaultEnvironment, /* required= */ false);
81 // clang-format on
82
main(int,const char * argv[])83 int main(int, const char* argv[]) {
84 if (!flags::Parse(argv)) {
85 return 1;
86 }
87
88 if (flags::h.value() || flags::help.value()) {
89 print_usage(argv[0]);
90 return 0;
91 }
92
93 if (flags::version.value()) {
94 spv_target_env target_env;
95 bool success = spvParseTargetEnv(kDefaultEnvironment, &target_env);
96 assert(success && "Default environment should always parse.");
97 if (!success) {
98 fprintf(stderr,
99 "error: invalid default target environment. Please report this "
100 "issue.");
101 return 1;
102 }
103 printf("%s\n", spvSoftwareVersionDetailsString());
104 printf("Target: %s\n", spvTargetEnvDescription(target_env));
105 return 0;
106 }
107
108 spv_target_env target_env;
109 if (!spvParseTargetEnv(flags::target_env.value().c_str(), &target_env)) {
110 fprintf(stderr, "error: Unrecognized target env: %s\n",
111 flags::target_env.value().c_str());
112 return 1;
113 }
114
115 const std::string outFile =
116 flags::o.value().empty() ? "out.spv" : flags::o.value();
117 const std::vector<std::string>& inFiles = flags::positional_arguments;
118
119 spvtools::LinkerOptions options;
120 options.SetAllowPartialLinkage(flags::allow_partial_linkage.value());
121 options.SetCreateLibrary(flags::create_library.value());
122 options.SetVerifyIds(flags::verify_ids.value());
123
124 if (inFiles.empty()) {
125 fprintf(stderr, "error: No input file specified\n");
126 return 1;
127 }
128
129 std::vector<std::vector<uint32_t>> contents(inFiles.size());
130 for (size_t i = 0u; i < inFiles.size(); ++i) {
131 if (!ReadBinaryFile<uint32_t>(inFiles[i].c_str(), &contents[i])) return 1;
132 }
133
134 const spvtools::MessageConsumer consumer = [](spv_message_level_t level,
135 const char*,
136 const spv_position_t& position,
137 const char* message) {
138 switch (level) {
139 case SPV_MSG_FATAL:
140 case SPV_MSG_INTERNAL_ERROR:
141 case SPV_MSG_ERROR:
142 std::cerr << "error: " << position.index << ": " << message
143 << std::endl;
144 break;
145 case SPV_MSG_WARNING:
146 std::cout << "warning: " << position.index << ": " << message
147 << std::endl;
148 break;
149 case SPV_MSG_INFO:
150 std::cout << "info: " << position.index << ": " << message << std::endl;
151 break;
152 default:
153 break;
154 }
155 };
156 spvtools::Context context(target_env);
157 context.SetMessageConsumer(consumer);
158
159 std::vector<uint32_t> linkingResult;
160 spv_result_t status = Link(context, contents, &linkingResult, options);
161 if (status != SPV_SUCCESS && status != SPV_WARNING) return 1;
162
163 if (!WriteFile<uint32_t>(outFile.c_str(), "wb", linkingResult.data(),
164 linkingResult.size()))
165 return 1;
166
167 return 0;
168 }
169