compile_options.cpp 4.5 KB

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