build_web.odin 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // This program builds a Karl2D game as a web version.
  2. //
  3. // Usage:
  4. // odin run build_web -- directory_name
  5. //
  6. // For example:
  7. // odin run build_web -- examples/minimal_web
  8. //
  9. //
  10. //
  11. // This program copies the `odin.js` from `<odin>/core/wasm/js/odin.js` to the build folder. It also
  12. // copies an `index.html` file there than is used to host your web program. The example itself is
  13. // built using the `js_wasm32` target and put next to the index file.
  14. package karl2d_build_web_example
  15. import "core:fmt"
  16. import "core:path/filepath"
  17. import os "core:os/os2"
  18. main :: proc() {
  19. if len(os.args) != 2 {
  20. fmt.eprintfln("Usage: 'odin run build_web -- directory_name'\nExample: 'odin run build_web -- examples/minimal_web'")
  21. return
  22. }
  23. WEB_ENTRY_TEMPLATE :: #load("web_entry_templates/web_entry_template.odin")
  24. WEB_ENTRY_INDEX :: #load("web_entry_templates/index_template.html")
  25. dir := os.args[1]
  26. dir_handle, dir_handle_err := os.open(dir)
  27. fmt.ensuref(dir_handle_err == nil, "Failed finding directory %v. Error: %v", dir, dir_handle_err)
  28. dir_stat, dir_stat_err := os.fstat(dir_handle, context.allocator)
  29. fmt.ensuref(dir_stat_err == nil, "Failed checking status of directory %v. Error: %v", dir, dir_stat_err)
  30. fmt.ensuref(dir_stat.type == .Directory, "%v is not a directory!", dir)
  31. dir_name := dir_stat.name
  32. bin_dir := filepath.join({dir, "bin"})
  33. os.make_directory(bin_dir, 0o644)
  34. bin_web_dir := filepath.join({bin_dir, "web"})
  35. os.make_directory(bin_web_dir, 0o644)
  36. build_dir := filepath.join({dir, "build"})
  37. os.make_directory(build_dir, 0o644)
  38. build_web_dir := filepath.join({build_dir, "web"})
  39. os.make_directory(build_web_dir, 0o644)
  40. entry_odin_file_path := filepath.join({build_web_dir, fmt.tprintf("%v_web_entry.odin", dir_name)})
  41. write_entry_odin_err := os.write_entire_file(entry_odin_file_path, WEB_ENTRY_TEMPLATE)
  42. fmt.ensuref(write_entry_odin_err == nil, "Failed writing %v. Error: %v", entry_odin_file_path, write_entry_odin_err)
  43. entry_html_file_path := filepath.join({bin_web_dir, "index.html"})
  44. write_entry_html_err := os.write_entire_file(entry_html_file_path, WEB_ENTRY_INDEX)
  45. fmt.ensuref(write_entry_html_err == nil, "Failed writing %v. Error: %v", entry_html_file_path, write_entry_html_err)
  46. _, odin_root_stdout, _, odin_root_err := os.process_exec({
  47. command = { "odin", "root" },
  48. }, allocator = context.allocator)
  49. ensure(odin_root_err == nil, "Failed fetching 'odin root' (Odin in PATH needed!)")
  50. odin_root := string(odin_root_stdout)
  51. js_runtime_path := filepath.join({odin_root, "core", "sys", "wasm", "js", "odin.js"})
  52. fmt.ensuref(os.exists(js_runtime_path), "File does not exist: %v -- It is the Odin Javascript runtime that this program needs to copy to the web build output folder!", js_runtime_path)
  53. os.copy_file(filepath.join({bin_web_dir, "odin.js"}), js_runtime_path)
  54. wasm_out_path := filepath.join({bin_web_dir, "main.wasm"})
  55. _, build_std_out, build_std_err, _ := os.process_exec({
  56. command = {
  57. "odin",
  58. "build",
  59. build_web_dir,
  60. fmt.tprintf("-out:%v", wasm_out_path),
  61. "-target:js_wasm32",
  62. "-debug",
  63. "-vet",
  64. "-strict-style",
  65. },
  66. }, allocator = context.allocator)
  67. if len(build_std_out) > 0 {
  68. fmt.println(string(build_std_out))
  69. }
  70. if len(build_std_err) > 0 {
  71. fmt.println(string(build_std_err))
  72. }
  73. }