compile_options.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright (c) 2012-2020 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/dbartolini/crown/blob/master/LICENSE
  4. */
  5. #include "config.h"
  6. #if CROWN_CAN_COMPILE
  7. #include "core/containers/array.h"
  8. #include "core/containers/vector.h"
  9. #include "core/filesystem/file.h"
  10. #include "core/filesystem/filesystem.h"
  11. #include "core/filesystem/path.h"
  12. #include "core/guid.h"
  13. #include "core/memory/temp_allocator.h"
  14. #include "core/os.h"
  15. #include "core/process.h"
  16. #include "core/strings/dynamic_string.h"
  17. #include "core/strings/string_stream.h"
  18. #include "device/log.h"
  19. #include "resource/compile_options.h"
  20. #include "resource/data_compiler.h"
  21. namespace crown
  22. {
  23. CompileOptions::CompileOptions(DataCompiler& dc, Filesystem& data_filesystem, ResourceId res_id, const DynamicString& source_path, Buffer& output, const char* platform)
  24. : _data_compiler(dc)
  25. , _data_filesystem(data_filesystem)
  26. , _source_path(source_path)
  27. , _output(output)
  28. , _platform(platform)
  29. , _resource_id(res_id)
  30. {
  31. }
  32. void CompileOptions::error(const char* msg, va_list args)
  33. {
  34. _data_compiler.error(msg, args);
  35. }
  36. void CompileOptions::error(const char* msg, ...)
  37. {
  38. va_list args;
  39. va_start(args, msg);
  40. error(msg, args);
  41. va_end(args);
  42. }
  43. const char* CompileOptions::source_path()
  44. {
  45. return _source_path.c_str();
  46. }
  47. bool CompileOptions::file_exists(const char* path)
  48. {
  49. TempAllocator256 ta;
  50. DynamicString source_dir(ta);
  51. FilesystemDisk fs(ta);
  52. _data_compiler.source_dir(path, source_dir);
  53. fs.set_prefix(source_dir.c_str());
  54. return fs.exists(path);
  55. }
  56. bool CompileOptions::resource_exists(const char* type, const char* name)
  57. {
  58. TempAllocator1024 ta;
  59. DynamicString path(ta);
  60. path += name;
  61. path += ".";
  62. path += type;
  63. return file_exists(path.c_str());
  64. }
  65. Buffer CompileOptions::read_temporary(const char* path)
  66. {
  67. File* file = _data_filesystem.open(path, FileOpenMode::READ);
  68. u32 size = file->size();
  69. Buffer buf(default_allocator());
  70. array::resize(buf, size);
  71. file->read(array::begin(buf), size);
  72. _data_filesystem.close(*file);
  73. return buf;
  74. }
  75. void CompileOptions::write_temporary(const char* path, const char* data, u32 size)
  76. {
  77. File* file = _data_filesystem.open(path, FileOpenMode::WRITE);
  78. file->write(data, size);
  79. _data_filesystem.close(*file);
  80. }
  81. void CompileOptions::write_temporary(const char* path, const Buffer& data)
  82. {
  83. write_temporary(path, array::begin(data), array::size(data));
  84. }
  85. ///
  86. Buffer CompileOptions::read()
  87. {
  88. return read(_source_path.c_str());
  89. }
  90. Buffer CompileOptions::read(const char* path)
  91. {
  92. _data_compiler.add_dependency(_resource_id, path);
  93. TempAllocator256 ta;
  94. DynamicString source_dir(ta);
  95. _data_compiler.source_dir(path, source_dir);
  96. FilesystemDisk source_filesystem(ta);
  97. source_filesystem.set_prefix(source_dir.c_str());
  98. File* file = source_filesystem.open(path, FileOpenMode::READ);
  99. const u32 size = file->size();
  100. Buffer buf(default_allocator());
  101. array::resize(buf, size);
  102. file->read(array::begin(buf), size);
  103. source_filesystem.close(*file);
  104. return buf;
  105. }
  106. void CompileOptions::fake_read(const char* path)
  107. {
  108. _data_compiler.add_dependency(_resource_id, path);
  109. }
  110. void CompileOptions::add_requirement(const char* type, const char* name)
  111. {
  112. TempAllocator256 ta;
  113. DynamicString path(ta);
  114. path = name;
  115. path += ".";
  116. path += type;
  117. _data_compiler.add_requirement(_resource_id, path.c_str());
  118. }
  119. void CompileOptions::get_absolute_path(const char* path, DynamicString& abs)
  120. {
  121. TempAllocator256 ta;
  122. DynamicString source_dir(ta);
  123. _data_compiler.source_dir(path, source_dir);
  124. FilesystemDisk source_filesystem(ta);
  125. source_filesystem.set_prefix(source_dir.c_str());
  126. source_filesystem.get_absolute_path(path, abs);
  127. }
  128. void CompileOptions::get_temporary_path(const char* suffix, DynamicString& abs)
  129. {
  130. TempAllocator1024 ta;
  131. DynamicString str(ta);
  132. DynamicString prefix(ta);
  133. prefix.from_guid(guid::new_guid());
  134. _data_filesystem.get_absolute_path(CROWN_TEMP_DIRECTORY, str);
  135. path::join(abs, str.c_str(), prefix.c_str());
  136. abs += '.';
  137. abs += suffix;
  138. }
  139. DeleteResult CompileOptions::delete_file(const char* path)
  140. {
  141. return _data_filesystem.delete_file(path);
  142. }
  143. void CompileOptions::write(const void* data, u32 size)
  144. {
  145. array::push(_output, (const char*)data, size);
  146. }
  147. void CompileOptions::write(const Buffer& data)
  148. {
  149. array::push(_output, array::begin(data), array::size(data));
  150. }
  151. const char* CompileOptions::platform() const
  152. {
  153. return _platform;
  154. }
  155. const char* CompileOptions::exe_path(const char* const* paths, u32 num)
  156. {
  157. for (u32 ii = 0; ii < num; ++ii)
  158. {
  159. if (os::access(paths[ii], AccessFlags::EXECUTE) == 0)
  160. return paths[ii];
  161. }
  162. return NULL;
  163. }
  164. } // namespace crown
  165. #endif // CROWN_CAN_COMPILE