linker.cpp 5.7 KB

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