linker.cpp 5.5 KB

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