build_web_example.odin 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // This program builds the examples in a "web" version. You can use it as a basis for your own
  2. // Karl2D web build script.
  3. //
  4. // Usage:
  5. // odin run build_web_example -- example_directory_name
  6. //
  7. // Replace `example_directory_name` with the example you wish to build a web version of. The
  8. // resulting web application will be in `example_directory_name/web/build`.
  9. //
  10. // This program copies the `odin.js` from `<odin>/core/wasm/js/odin.js` to the build folder. It also
  11. // copies an `index.html` file there than is used to host your web program. The example itself is
  12. // built using the `js_wasm32` target and put next to the index file.
  13. package karl2d_build_web_example
  14. import "core:fmt"
  15. import "core:path/filepath"
  16. import os "core:os/os2"
  17. main :: proc() {
  18. if len(os.args) != 2 {
  19. fmt.eprintfln("Usage: 'odin run build_web_example -- example_directory_name'\nExample: 'odin run build_web_example -- minimal'")
  20. return
  21. }
  22. WEB_ENTRY_TEMPLATE :: #load("web_entry_templates/web_entry_template.odin")
  23. WEB_ENTRY_INDEX :: #load("web_entry_templates/index_template.html")
  24. dir := os.args[1]
  25. dir_handle, dir_handle_err := os.open(dir)
  26. fmt.ensuref(dir_handle_err == nil, "Failed finding directory %v. Error: %v", dir, dir_handle_err)
  27. dir_stat, dir_stat_err := os.fstat(dir_handle, context.allocator)
  28. fmt.ensuref(dir_stat_err == nil, "Failed checking status of directory %v. Error: %v", dir, dir_stat_err)
  29. fmt.ensuref(dir_stat.type == .Directory, "%v is not a directory!", dir)
  30. web_dir := filepath.join({dir, "web"})
  31. os.make_directory(web_dir, 0o644)
  32. web_build_dir := filepath.join({web_dir, "build"})
  33. os.make_directory(web_build_dir, 0o644)
  34. entry_odin_file_path := filepath.join({web_dir, fmt.tprintf("%v_web_entry.odin", filepath.stem(dir))})
  35. write_entry_odin_err := os.write_entire_file(entry_odin_file_path, WEB_ENTRY_TEMPLATE)
  36. fmt.ensuref(write_entry_odin_err == nil, "Failed writing %v. Error: %v", entry_odin_file_path, write_entry_odin_err)
  37. entry_html_file_path := filepath.join({web_build_dir, "index.html"})
  38. write_entry_html_err := os.write_entire_file(entry_html_file_path, WEB_ENTRY_INDEX)
  39. fmt.ensuref(write_entry_html_err == nil, "Failed writing %v. Error: %v", entry_html_file_path, write_entry_html_err)
  40. _, odin_root_stdout, _, odin_root_err := os.process_exec({
  41. command = { "odin", "root" },
  42. }, allocator = context.allocator)
  43. ensure(odin_root_err == nil, "Failed fetching 'odin root' (Odin in PATH needed!)")
  44. odin_root := string(odin_root_stdout)
  45. js_runtime_path := filepath.join({odin_root, "core", "sys", "wasm", "js", "odin.js"})
  46. 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)
  47. os.copy_file(filepath.join({web_build_dir, "odin.js"}), js_runtime_path)
  48. wasm_out_path := filepath.join({web_build_dir, "main.wasm"})
  49. _, build_std_out, build_std_err, _ := os.process_exec({
  50. command = {
  51. "odin",
  52. "build",
  53. web_dir,
  54. fmt.tprintf("-out:%v", wasm_out_path),
  55. "-target:js_wasm32",
  56. "-debug",
  57. "-vet",
  58. "-strict-style",
  59. },
  60. }, allocator = context.allocator)
  61. if len(build_std_out) > 0 {
  62. fmt.println(string(build_std_out))
  63. }
  64. if len(build_std_err) > 0 {
  65. fmt.println(string(build_std_err))
  66. }
  67. }