1
0

cmScanDepFormat.cxx 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmScanDepFormat.h"
  4. #include <cctype>
  5. #include <cstdio>
  6. #include <cm3p/json/reader.h>
  7. #include <cm3p/json/value.h>
  8. #include <cm3p/json/writer.h>
  9. #include "cmsys/FStream.hxx"
  10. #include "cmGeneratedFileStream.h"
  11. #include "cmStringAlgorithms.h"
  12. #include "cmSystemTools.h"
  13. static bool ParseFilename(Json::Value const& val, std::string& result)
  14. {
  15. if (val.isString()) {
  16. result = val.asString();
  17. } else {
  18. return false;
  19. }
  20. return true;
  21. }
  22. static Json::Value EncodeFilename(std::string const& path)
  23. {
  24. std::string data;
  25. data.reserve(path.size());
  26. for (auto const& byte : path) {
  27. if (std::iscntrl(byte)) {
  28. // Control characters.
  29. data.append("\\u");
  30. char buf[5];
  31. std::snprintf(buf, sizeof(buf), "%04x", byte);
  32. data.append(buf);
  33. } else if (byte == '"' || byte == '\\') {
  34. // Special JSON characters.
  35. data.push_back('\\');
  36. data.push_back(byte);
  37. } else {
  38. // Other data.
  39. data.push_back(byte);
  40. }
  41. }
  42. return data;
  43. }
  44. #define PARSE_BLOB(val, res) \
  45. do { \
  46. if (!ParseFilename(val, res)) { \
  47. cmSystemTools::Error( \
  48. cmStrCat("-E cmake_ninja_depends failed to parse ", arg_pp, \
  49. ": invalid blob")); \
  50. return false; \
  51. } \
  52. } while (0)
  53. #define PARSE_FILENAME(val, res) \
  54. do { \
  55. if (!ParseFilename(val, res)) { \
  56. cmSystemTools::Error( \
  57. cmStrCat("-E cmake_ninja_depends failed to parse ", arg_pp, \
  58. ": invalid filename")); \
  59. return false; \
  60. } \
  61. \
  62. if (!cmSystemTools::FileIsFullPath(res)) { \
  63. res = cmStrCat(work_directory, '/', res); \
  64. } \
  65. } while (0)
  66. bool cmScanDepFormat_P1689_Parse(std::string const& arg_pp, cmSourceInfo* info)
  67. {
  68. Json::Value ppio;
  69. Json::Value const& ppi = ppio;
  70. cmsys::ifstream ppf(arg_pp.c_str(), std::ios::in | std::ios::binary);
  71. {
  72. Json::Reader reader;
  73. if (!reader.parse(ppf, ppio, false)) {
  74. cmSystemTools::Error(cmStrCat("-E cmake_ninja_depends failed to parse ",
  75. arg_pp,
  76. reader.getFormattedErrorMessages()));
  77. return false;
  78. }
  79. }
  80. Json::Value const& version = ppi["version"];
  81. if (version.asUInt() != 0) {
  82. cmSystemTools::Error(cmStrCat("-E cmake_ninja_depends failed to parse ",
  83. arg_pp, ": version ", version.asString()));
  84. return false;
  85. }
  86. Json::Value const& rules = ppi["rules"];
  87. if (rules.isArray()) {
  88. if (rules.size() != 1) {
  89. cmSystemTools::Error(cmStrCat("-E cmake_ninja_depends failed to parse ",
  90. arg_pp, ": expected 1 source entry"));
  91. return false;
  92. }
  93. for (auto const& rule : rules) {
  94. Json::Value const& workdir = rule["work-directory"];
  95. if (!workdir.isString()) {
  96. cmSystemTools::Error(
  97. cmStrCat("-E cmake_ninja_depends failed to parse ", arg_pp,
  98. ": work-directory is not a string"));
  99. return false;
  100. }
  101. std::string work_directory;
  102. PARSE_BLOB(workdir, work_directory);
  103. Json::Value const& depends = rule["depends"];
  104. if (depends.isArray()) {
  105. std::string depend_filename;
  106. for (auto const& depend : depends) {
  107. PARSE_FILENAME(depend, depend_filename);
  108. info->Includes.push_back(depend_filename);
  109. }
  110. }
  111. if (rule.isMember("future-compile")) {
  112. Json::Value const& future_compile = rule["future-compile"];
  113. if (future_compile.isMember("outputs")) {
  114. Json::Value const& outputs = future_compile["outputs"];
  115. if (outputs.isArray()) {
  116. if (outputs.empty()) {
  117. cmSystemTools::Error(
  118. cmStrCat("-E cmake_ninja_depends failed to parse ", arg_pp,
  119. ": expected at least one 1 output"));
  120. return false;
  121. }
  122. PARSE_FILENAME(outputs[0], info->PrimaryOutput);
  123. }
  124. }
  125. if (future_compile.isMember("provides")) {
  126. Json::Value const& provides = future_compile["provides"];
  127. if (provides.isArray()) {
  128. for (auto const& provide : provides) {
  129. cmSourceReqInfo provide_info;
  130. Json::Value const& logical_name = provide["logical-name"];
  131. PARSE_BLOB(logical_name, provide_info.LogicalName);
  132. if (provide.isMember("compiled-module-path")) {
  133. Json::Value const& compiled_module_path =
  134. provide["compiled-module-path"];
  135. PARSE_FILENAME(compiled_module_path,
  136. provide_info.CompiledModulePath);
  137. } else {
  138. provide_info.CompiledModulePath =
  139. cmStrCat(provide_info.LogicalName, ".mod");
  140. }
  141. info->Provides.push_back(provide_info);
  142. }
  143. }
  144. }
  145. if (future_compile.isMember("requires")) {
  146. Json::Value const& reqs = future_compile["requires"];
  147. if (reqs.isArray()) {
  148. for (auto const& require : reqs) {
  149. cmSourceReqInfo require_info;
  150. Json::Value const& logical_name = require["logical-name"];
  151. PARSE_BLOB(logical_name, require_info.LogicalName);
  152. if (require.isMember("compiled-module-path")) {
  153. Json::Value const& compiled_module_path =
  154. require["compiled-module-path"];
  155. PARSE_FILENAME(compiled_module_path,
  156. require_info.CompiledModulePath);
  157. }
  158. info->Requires.push_back(require_info);
  159. }
  160. }
  161. }
  162. }
  163. }
  164. }
  165. return true;
  166. }
  167. bool cmScanDepFormat_P1689_Write(std::string const& path,
  168. std::string const& input,
  169. cmSourceInfo const& info)
  170. {
  171. Json::Value ddi(Json::objectValue);
  172. ddi["version"] = 0;
  173. ddi["revision"] = 0;
  174. Json::Value& rules = ddi["rules"] = Json::arrayValue;
  175. Json::Value rule(Json::objectValue);
  176. rule["work-directory"] =
  177. EncodeFilename(cmSystemTools::GetCurrentWorkingDirectory());
  178. Json::Value& inputs = rule["inputs"] = Json::arrayValue;
  179. inputs.append(EncodeFilename(input));
  180. Json::Value& rule_outputs = rule["outputs"] = Json::arrayValue;
  181. rule_outputs.append(EncodeFilename(path));
  182. Json::Value& depends = rule["depends"] = Json::arrayValue;
  183. for (auto const& include : info.Includes) {
  184. depends.append(EncodeFilename(include));
  185. }
  186. Json::Value& future_compile = rule["future-compile"] = Json::objectValue;
  187. Json::Value& outputs = future_compile["outputs"] = Json::arrayValue;
  188. outputs.append(info.PrimaryOutput);
  189. Json::Value& provides = future_compile["provides"] = Json::arrayValue;
  190. for (auto const& provide : info.Provides) {
  191. Json::Value provide_obj(Json::objectValue);
  192. auto const encoded = EncodeFilename(provide.LogicalName);
  193. provide_obj["logical-name"] = encoded;
  194. if (provide.CompiledModulePath.empty()) {
  195. provide_obj["compiled-module-path"] = encoded;
  196. } else {
  197. provide_obj["compiled-module-path"] =
  198. EncodeFilename(provide.CompiledModulePath);
  199. }
  200. // TODO: Source file tracking. See below.
  201. provides.append(provide_obj);
  202. }
  203. Json::Value& reqs = future_compile["requires"] = Json::arrayValue;
  204. for (auto const& require : info.Requires) {
  205. Json::Value require_obj(Json::objectValue);
  206. auto const encoded = EncodeFilename(require.LogicalName);
  207. require_obj["logical-name"] = encoded;
  208. if (require.CompiledModulePath.empty()) {
  209. require_obj["compiled-module-path"] = encoded;
  210. } else {
  211. require_obj["compiled-module-path"] =
  212. EncodeFilename(require.CompiledModulePath);
  213. }
  214. // TODO: Source filename inclusion. Requires collating with the provides
  215. // filenames (as a sanity check if available on both sides).
  216. reqs.append(require_obj);
  217. }
  218. rules.append(rule);
  219. cmGeneratedFileStream ddif(path);
  220. ddif << ddi;
  221. return !!ddif;
  222. }