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