build_web_example.odin 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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:os"
  15. import "core:fmt"
  16. import "core:path/filepath"
  17. import "core:os/os2"
  18. main :: proc() {
  19. if len(os.args) != 2 {
  20. fmt.eprintfln("Usage: 'odin run build_web_example -- example_directory_name'\nExample: 'odin run build_web_example -- minimal'")
  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)
  29. fmt.ensuref(dir_stat_err == nil, "Failed checking status of directory %v. Error: %v", dir, dir_stat_err)
  30. fmt.ensuref(dir_stat.is_dir, "%v is not a directory!", dir)
  31. web_dir := filepath.join({dir, "web"})
  32. os.make_directory(web_dir, 0o644)
  33. web_build_dir := filepath.join({web_dir, "build"})
  34. os.make_directory(web_build_dir, 0o644)
  35. os.write_entire_file(
  36. filepath.join(
  37. {web_dir, fmt.tprintf("%v_web_entry.odin", filepath.stem(dir))},
  38. ),
  39. WEB_ENTRY_TEMPLATE,
  40. )
  41. os.write_entire_file(filepath.join({web_build_dir, "index.html"}), WEB_ENTRY_INDEX)
  42. _, odin_root_stdout, _, odin_root_err := os2.process_exec({
  43. command = { "odin", "root" },
  44. }, allocator = context.allocator)
  45. ensure(odin_root_err == nil, "Failed fetching 'odin root' (Odin in PATH needed!)")
  46. odin_root := string(odin_root_stdout)
  47. js_runtime_path := filepath.join({odin_root, "core", "sys", "wasm", "js", "odin.js"})
  48. fmt.ensuref(os2.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)
  49. os2.copy_file(filepath.join({web_build_dir, "odin.js"}), js_runtime_path)
  50. wasm_out_path := filepath.join({web_build_dir, "main.wasm"})
  51. _, build_std_out, build_std_err, _ := os2.process_exec({
  52. command = {
  53. "odin",
  54. "build",
  55. web_dir,
  56. fmt.tprintf("-out:%v", wasm_out_path),
  57. "-target:js_wasm32",
  58. "-debug",
  59. "-vet",
  60. "-strict-style",
  61. },
  62. }, allocator = context.allocator)
  63. if len(build_std_out) > 0 {
  64. fmt.println(string(build_std_out))
  65. }
  66. if len(build_std_err) > 0 {
  67. fmt.println(string(build_std_err))
  68. }
  69. }