cmScanDepFormat.cxx 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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(cmStrCat("-E cmake_ninja_dyndep failed to parse ", \
  48. arg_pp, ": invalid blob")); \
  49. return false; \
  50. } \
  51. } while (0)
  52. #define PARSE_FILENAME(val, res) \
  53. do { \
  54. if (!ParseFilename(val, res)) { \
  55. cmSystemTools::Error(cmStrCat("-E cmake_ninja_dyndep failed to parse ", \
  56. arg_pp, ": invalid filename")); \
  57. return false; \
  58. } \
  59. \
  60. if (!work_directory.empty() && !cmSystemTools::FileIsFullPath(res)) { \
  61. res = cmStrCat(work_directory, '/', res); \
  62. } \
  63. } while (0)
  64. bool cmScanDepFormat_P1689_Parse(std::string const& arg_pp, cmSourceInfo* info)
  65. {
  66. Json::Value ppio;
  67. Json::Value const& ppi = ppio;
  68. cmsys::ifstream ppf(arg_pp.c_str(), std::ios::in | std::ios::binary);
  69. {
  70. Json::Reader reader;
  71. if (!reader.parse(ppf, ppio, false)) {
  72. cmSystemTools::Error(cmStrCat("-E cmake_ninja_dyndep failed to parse ",
  73. arg_pp,
  74. reader.getFormattedErrorMessages()));
  75. return false;
  76. }
  77. }
  78. Json::Value const& version = ppi["version"];
  79. if (version.asUInt() != 0) {
  80. cmSystemTools::Error(cmStrCat("-E cmake_ninja_dyndep failed to parse ",
  81. arg_pp, ": version ", version.asString()));
  82. return false;
  83. }
  84. Json::Value const& rules = ppi["rules"];
  85. if (rules.isArray()) {
  86. if (rules.size() != 1) {
  87. cmSystemTools::Error(cmStrCat("-E cmake_ninja_dyndep failed to parse ",
  88. arg_pp, ": expected 1 source entry"));
  89. return false;
  90. }
  91. for (auto const& rule : rules) {
  92. std::string work_directory;
  93. Json::Value const& workdir = rule["work-directory"];
  94. if (workdir.isString()) {
  95. PARSE_BLOB(workdir, work_directory);
  96. } else if (!workdir.isNull()) {
  97. cmSystemTools::Error(cmStrCat("-E cmake_ninja_dyndep failed to parse ",
  98. arg_pp,
  99. ": work-directory is not a string"));
  100. return false;
  101. }
  102. Json::Value const& depends = rule["depends"];
  103. if (depends.isArray()) {
  104. std::string depend_filename;
  105. for (auto const& depend : depends) {
  106. PARSE_FILENAME(depend, depend_filename);
  107. info->Includes.push_back(depend_filename);
  108. }
  109. }
  110. if (rule.isMember("future-compile")) {
  111. Json::Value const& future_compile = rule["future-compile"];
  112. if (future_compile.isMember("outputs")) {
  113. Json::Value const& outputs = future_compile["outputs"];
  114. if (outputs.isArray()) {
  115. if (outputs.empty()) {
  116. cmSystemTools::Error(
  117. cmStrCat("-E cmake_ninja_dyndep failed to parse ", arg_pp,
  118. ": expected at least one 1 output"));
  119. return false;
  120. }
  121. PARSE_FILENAME(outputs[0], info->PrimaryOutput);
  122. }
  123. }
  124. if (future_compile.isMember("provides")) {
  125. Json::Value const& provides = future_compile["provides"];
  126. if (provides.isArray()) {
  127. for (auto const& provide : provides) {
  128. cmSourceReqInfo provide_info;
  129. Json::Value const& logical_name = provide["logical-name"];
  130. PARSE_BLOB(logical_name, provide_info.LogicalName);
  131. if (provide.isMember("compiled-module-path")) {
  132. Json::Value const& compiled_module_path =
  133. provide["compiled-module-path"];
  134. PARSE_FILENAME(compiled_module_path,
  135. provide_info.CompiledModulePath);
  136. } else {
  137. provide_info.CompiledModulePath =
  138. cmStrCat(provide_info.LogicalName, ".mod");
  139. }
  140. info->Provides.push_back(provide_info);
  141. }
  142. }
  143. }
  144. if (future_compile.isMember("requires")) {
  145. Json::Value const& reqs = future_compile["requires"];
  146. if (reqs.isArray()) {
  147. for (auto const& require : reqs) {
  148. cmSourceReqInfo require_info;
  149. Json::Value const& logical_name = require["logical-name"];
  150. PARSE_BLOB(logical_name, require_info.LogicalName);
  151. if (require.isMember("compiled-module-path")) {
  152. Json::Value const& compiled_module_path =
  153. require["compiled-module-path"];
  154. PARSE_FILENAME(compiled_module_path,
  155. require_info.CompiledModulePath);
  156. }
  157. info->Requires.push_back(require_info);
  158. }
  159. }
  160. }
  161. }
  162. }
  163. }
  164. return true;
  165. }
  166. bool cmScanDepFormat_P1689_Write(std::string const& path,
  167. std::string const& input,
  168. cmSourceInfo const& info)
  169. {
  170. Json::Value ddi(Json::objectValue);
  171. ddi["version"] = 0;
  172. ddi["revision"] = 0;
  173. Json::Value& rules = ddi["rules"] = Json::arrayValue;
  174. Json::Value rule(Json::objectValue);
  175. Json::Value& inputs = rule["inputs"] = Json::arrayValue;
  176. inputs.append(EncodeFilename(input));
  177. Json::Value& rule_outputs = rule["outputs"] = Json::arrayValue;
  178. rule_outputs.append(EncodeFilename(path));
  179. Json::Value& depends = rule["depends"] = Json::arrayValue;
  180. for (auto const& include : info.Includes) {
  181. depends.append(EncodeFilename(include));
  182. }
  183. Json::Value& future_compile = rule["future-compile"] = Json::objectValue;
  184. Json::Value& outputs = future_compile["outputs"] = Json::arrayValue;
  185. outputs.append(info.PrimaryOutput);
  186. Json::Value& provides = future_compile["provides"] = Json::arrayValue;
  187. for (auto const& provide : info.Provides) {
  188. Json::Value provide_obj(Json::objectValue);
  189. auto const encoded = EncodeFilename(provide.LogicalName);
  190. provide_obj["logical-name"] = encoded;
  191. if (provide.CompiledModulePath.empty()) {
  192. provide_obj["compiled-module-path"] = encoded;
  193. } else {
  194. provide_obj["compiled-module-path"] =
  195. EncodeFilename(provide.CompiledModulePath);
  196. }
  197. // TODO: Source file tracking. See below.
  198. provides.append(provide_obj);
  199. }
  200. Json::Value& reqs = future_compile["requires"] = Json::arrayValue;
  201. for (auto const& require : info.Requires) {
  202. Json::Value require_obj(Json::objectValue);
  203. auto const encoded = EncodeFilename(require.LogicalName);
  204. require_obj["logical-name"] = encoded;
  205. if (require.CompiledModulePath.empty()) {
  206. require_obj["compiled-module-path"] = encoded;
  207. } else {
  208. require_obj["compiled-module-path"] =
  209. EncodeFilename(require.CompiledModulePath);
  210. }
  211. // TODO: Source filename inclusion. Requires collating with the provides
  212. // filenames (as a sanity check if available on both sides).
  213. reqs.append(require_obj);
  214. }
  215. rules.append(rule);
  216. cmGeneratedFileStream ddif(path);
  217. ddif << ddi;
  218. return !!ddif;
  219. }