compile_options.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Copyright (c) 2012-2023 Daniele Bartolini et al.
  3. * SPDX-License-Identifier: MIT
  4. */
  5. #include "config.h"
  6. #if CROWN_CAN_COMPILE
  7. #include "core/containers/array.inl"
  8. #include "core/containers/hash_map.inl"
  9. #include "core/containers/vector.inl"
  10. #include "core/filesystem/file.h"
  11. #include "core/filesystem/filesystem.h"
  12. #include "core/filesystem/path.h"
  13. #include "core/filesystem/reader_writer.inl"
  14. #include "core/guid.h"
  15. #include "core/memory/temp_allocator.inl"
  16. #include "core/os.h"
  17. #include "core/process.h"
  18. #include "core/strings/dynamic_string.inl"
  19. #include "core/strings/string_stream.inl"
  20. #include "device/log.h"
  21. #include "resource/compile_options.inl"
  22. #include "resource/data_compiler.h"
  23. namespace crown
  24. {
  25. CompileOptions::CompileOptions(File &output
  26. , HashMap<DynamicString, u32> &new_dependencies
  27. , HashMap<DynamicString, u32> &new_requirements
  28. , DataCompiler &dc
  29. , Filesystem &output_filesystem
  30. , Filesystem &data_filesystem
  31. , ResourceId res_id
  32. , const DynamicString &source_path
  33. , const char *platform
  34. , bool bundle
  35. )
  36. : _file(output)
  37. , _binary_writer(_file)
  38. , _new_dependencies(new_dependencies)
  39. , _new_requirements(new_requirements)
  40. , _data_compiler(dc)
  41. , _output_filesystem(output_filesystem)
  42. , _data_filesystem(data_filesystem)
  43. , _source_path(source_path)
  44. , _platform(platform)
  45. , _resource_id(res_id)
  46. , _bundle(bundle)
  47. {
  48. }
  49. void CompileOptions::error(const char *msg, va_list args)
  50. {
  51. _data_compiler.error(msg, args);
  52. }
  53. void CompileOptions::error(const char *msg, ...)
  54. {
  55. va_list args;
  56. va_start(args, msg);
  57. error(msg, args);
  58. va_end(args);
  59. }
  60. const char *CompileOptions::source_path()
  61. {
  62. return _source_path.c_str();
  63. }
  64. bool CompileOptions::file_exists(const char *path)
  65. {
  66. TempAllocator256 ta;
  67. DynamicString source_dir(ta);
  68. FilesystemDisk fs(ta);
  69. _data_compiler.source_dir(path, source_dir);
  70. fs.set_prefix(source_dir.c_str());
  71. return fs.exists(path);
  72. }
  73. bool CompileOptions::resource_exists(const char *type, const char *name)
  74. {
  75. TempAllocator1024 ta;
  76. DynamicString path(ta);
  77. path += name;
  78. path += ".";
  79. path += type;
  80. return file_exists(path.c_str());
  81. }
  82. Buffer CompileOptions::read_all(File *file)
  83. {
  84. const u32 size = file->size();
  85. Buffer buf(default_allocator());
  86. array::resize(buf, size);
  87. if (size != 0)
  88. file->read(array::begin(buf), size);
  89. return buf;
  90. }
  91. Buffer CompileOptions::read_temporary(const char *path)
  92. {
  93. Buffer buf(default_allocator());
  94. File *file = _output_filesystem.open(path, FileOpenMode::READ);
  95. if (file->is_open())
  96. buf = read_all(file);
  97. _output_filesystem.close(*file);
  98. return buf;
  99. }
  100. void CompileOptions::write_temporary(const char *path, const char *data, u32 size)
  101. {
  102. File *file = _output_filesystem.open(path, FileOpenMode::WRITE);
  103. if (file->is_open())
  104. file->write(data, size);
  105. _output_filesystem.close(*file);
  106. }
  107. void CompileOptions::write_temporary(const char *path, const Buffer &data)
  108. {
  109. write_temporary(path, array::begin(data), array::size(data));
  110. }
  111. Buffer CompileOptions::read(const char *path)
  112. {
  113. fake_read(path);
  114. TempAllocator256 ta;
  115. DynamicString source_dir(ta);
  116. _data_compiler.source_dir(path, source_dir);
  117. FilesystemDisk source_filesystem(ta);
  118. source_filesystem.set_prefix(source_dir.c_str());
  119. Buffer buf(default_allocator());
  120. File *file = source_filesystem.open(path, FileOpenMode::READ);
  121. if (file->is_open())
  122. buf = read_all(file);
  123. source_filesystem.close(*file);
  124. return buf;
  125. }
  126. Buffer CompileOptions::read()
  127. {
  128. return read(_source_path.c_str());
  129. }
  130. void CompileOptions::fake_read(const char *path)
  131. {
  132. TempAllocator256 ta;
  133. DynamicString path_str(ta);
  134. path_str = path;
  135. hash_map::set(_new_dependencies, path_str, 0u);
  136. }
  137. void CompileOptions::add_requirement(const char *type, const char *name)
  138. {
  139. TempAllocator256 ta;
  140. DynamicString path(ta);
  141. path = name;
  142. path += ".";
  143. path += type;
  144. hash_map::set(_new_requirements, path, 0u);
  145. }
  146. void CompileOptions::absolute_path(DynamicString &abs, const char *path)
  147. {
  148. TempAllocator256 ta;
  149. DynamicString source_dir(ta);
  150. _data_compiler.source_dir(path, source_dir);
  151. FilesystemDisk source_filesystem(ta);
  152. source_filesystem.set_prefix(source_dir.c_str());
  153. source_filesystem.absolute_path(abs, path);
  154. }
  155. void CompileOptions::temporary_path(DynamicString &abs, const char *suffix)
  156. {
  157. TempAllocator1024 ta;
  158. DynamicString str(ta);
  159. DynamicString prefix(ta);
  160. prefix.from_guid(guid::new_guid());
  161. _output_filesystem.absolute_path(str, CROWN_TEMP_DIRECTORY);
  162. path::join(abs, str.c_str(), prefix.c_str());
  163. abs += '.';
  164. abs += suffix;
  165. }
  166. DeleteResult CompileOptions::delete_file(const char *path)
  167. {
  168. return _output_filesystem.delete_file(path);
  169. }
  170. void CompileOptions::align(const u32 align)
  171. {
  172. _binary_writer.align(align);
  173. }
  174. void CompileOptions::write(const void *data, u32 size)
  175. {
  176. _binary_writer.write(data, size);
  177. }
  178. void CompileOptions::write(const Buffer &data)
  179. {
  180. write(array::begin(data), array::size(data));
  181. }
  182. const char *CompileOptions::platform() const
  183. {
  184. return _platform;
  185. }
  186. const char *CompileOptions::exe_path(const char * const *paths, u32 num)
  187. {
  188. for (u32 ii = 0; ii < num; ++ii) {
  189. if (os::access(paths[ii], AccessFlags::EXECUTE) == 0)
  190. return paths[ii];
  191. }
  192. return NULL;
  193. }
  194. void CompileOptions::read_output(StringStream &output, Process &pr)
  195. {
  196. u32 nbr = 0;
  197. char msg[512];
  198. while (pr.read(&nbr, msg, sizeof(msg) - 1) != NULL) {
  199. msg[nbr] = '\0';
  200. output << msg;
  201. }
  202. }
  203. } // namespace crown
  204. #endif // if CROWN_CAN_COMPILE