lua_resource.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright (c) 2012-2017 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/filesystem/file.h"
  8. #include "core/memory/temp_allocator.h"
  9. #include "core/strings/dynamic_string.h"
  10. #include "core/strings/string_stream.h"
  11. #include "resource/compile_options.h"
  12. #include "resource/lua_resource.h"
  13. #define LUAJIT_NAME "./luajit"
  14. #if CROWN_PLATFORM_WINDOWS
  15. #define EXE ".exe"
  16. #else
  17. #define EXE ""
  18. #endif // CROWN_PLATFORM_WINDOWS
  19. #define LUAJIT_EXE LUAJIT_NAME EXE
  20. #if CROWN_DEBUG
  21. #define LUAJIT_FLAGS "-bg" // Keep debug info
  22. #else
  23. #define LUAJIT_FLAGS "-b"
  24. #endif // CROWN_DEBUG
  25. namespace crown
  26. {
  27. namespace lua_resource_internal
  28. {
  29. void compile(CompileOptions& opts)
  30. {
  31. TempAllocator1024 ta;
  32. DynamicString luasrc(ta);
  33. DynamicString luabin(ta);
  34. opts.get_absolute_path(opts.source_path(), luasrc);
  35. opts.get_temporary_path("lua", luabin);
  36. StringStream output(ta);
  37. const char* argv[] =
  38. {
  39. LUAJIT_EXE,
  40. LUAJIT_FLAGS,
  41. luasrc.c_str(),
  42. luabin.c_str(),
  43. NULL
  44. };
  45. int ec = opts.run_external_compiler(argv, output);
  46. DATA_COMPILER_ASSERT(ec == 0
  47. , opts
  48. , "Failed to compile lua:\n%s"
  49. , string_stream::c_str(output)
  50. );
  51. Buffer blob = opts.read_temporary(luabin.c_str());
  52. opts.delete_file(luabin.c_str());
  53. LuaResource lr;
  54. lr.version = RESOURCE_VERSION_SCRIPT;
  55. lr.size = array::size(blob);
  56. opts.write(lr.version);
  57. opts.write(lr.size);
  58. opts.write(blob);
  59. }
  60. } // namespace lua_resource_internal
  61. namespace lua_resource
  62. {
  63. const char* program(const LuaResource* lr)
  64. {
  65. return (char*)&lr[1];
  66. }
  67. } // namespace lua_resource
  68. } // namespace crown