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_5;
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 target environment. Without this flag the target
55                environment defaults to spv1.5. <env> must be one of
56                {%s}
57   --verify-ids
58                Verify that IDs in the resulting modules are truly unique.
59   --version
60                Display linker version information
61 )",
62       program, program, target_env_list.c_str());
63 }
64 
65 }  // namespace
66 
main(int argc,char ** argv)67 int main(int argc, char** argv) {
68   std::vector<const char*> inFiles;
69   const char* outFile = nullptr;
70   spv_target_env target_env = kDefaultEnvironment;
71   spvtools::LinkerOptions options;
72 
73   for (int argi = 1; argi < argc; ++argi) {
74     const char* cur_arg = argv[argi];
75     if ('-' == cur_arg[0]) {
76       if (0 == strcmp(cur_arg, "-o")) {
77         if (argi + 1 < argc) {
78           if (!outFile) {
79             outFile = argv[++argi];
80           } else {
81             fprintf(stderr, "error: More than one output file specified\n");
82             return 1;
83           }
84         } else {
85           fprintf(stderr, "error: Missing argument to %s\n", cur_arg);
86           return 1;
87         }
88       } else if (0 == strcmp(cur_arg, "--allow-partial-linkage")) {
89         options.SetAllowPartialLinkage(true);
90       } else if (0 == strcmp(cur_arg, "--create-library")) {
91         options.SetCreateLibrary(true);
92       } else if (0 == strcmp(cur_arg, "--help") || 0 == strcmp(cur_arg, "-h")) {
93         print_usage(argv[0]);
94         return 0;
95       } else if (0 == strcmp(cur_arg, "--target-env")) {
96         if (argi + 1 < argc) {
97           const auto env_str = argv[++argi];
98           if (!spvParseTargetEnv(env_str, &target_env)) {
99             fprintf(stderr, "error: Unrecognized target env: %s\n", env_str);
100             return 1;
101           }
102         } else {
103           fprintf(stderr, "error: Missing argument to --target-env\n");
104           return 1;
105         }
106       } else if (0 == strcmp(cur_arg, "--verify-ids")) {
107         options.SetVerifyIds(true);
108       } else if (0 == strcmp(cur_arg, "--version")) {
109         printf("%s\n", spvSoftwareVersionDetailsString());
110         printf("Target: %s\n", spvTargetEnvDescription(target_env));
111         return 0;
112       } else {
113         fprintf(stderr, "error: Unrecognized option: %s\n\n", argv[argi]);
114         print_usage(argv[0]);
115         return 1;
116       }
117     } else {
118       inFiles.push_back(cur_arg);
119     }
120   }
121 
122   if (!outFile) {
123     outFile = "out.spv";
124   }
125 
126   if (inFiles.empty()) {
127     fprintf(stderr, "error: No input file specified\n");
128     return 1;
129   }
130 
131   std::vector<std::vector<uint32_t>> contents(inFiles.size());
132   for (size_t i = 0u; i < inFiles.size(); ++i) {
133     if (!ReadBinaryFile<uint32_t>(inFiles[i], &contents[i])) return 1;
134   }
135 
136   const spvtools::MessageConsumer consumer = [](spv_message_level_t level,
137                                                 const char*,
138                                                 const spv_position_t& position,
139                                                 const char* message) {
140     switch (level) {
141       case SPV_MSG_FATAL:
142       case SPV_MSG_INTERNAL_ERROR:
143       case SPV_MSG_ERROR:
144         std::cerr << "error: " << position.index << ": " << message
145                   << std::endl;
146         break;
147       case SPV_MSG_WARNING:
148         std::cout << "warning: " << position.index << ": " << message
149                   << std::endl;
150         break;
151       case SPV_MSG_INFO:
152         std::cout << "info: " << position.index << ": " << message << std::endl;
153         break;
154       default:
155         break;
156     }
157   };
158   spvtools::Context context(target_env);
159   context.SetMessageConsumer(consumer);
160 
161   std::vector<uint32_t> linkingResult;
162   spv_result_t status = Link(context, contents, &linkingResult, options);
163 
164   if (!WriteFile<uint32_t>(outFile, "wb", linkingResult.data(),
165                            linkingResult.size()))
166     return 1;
167 
168   return status == SPV_SUCCESS ? 0 : 1;
169 }
170