(executable
 (name main)
 (modules main)
 (libraries js_of_ocaml)
 (link_flags
  (:standard -linkall))
 (modes js byte))

(executable
 (name effects_flags)
 (modules effects_flags))

(rule
 (target effects_flags.txt)
 (action
  (with-stdout-to
   %{target}
   (run ./effects_flags.exe txt %{profile}))))

(rule
 (target main.js)
 (action
  (run
   %{bin:js_of_ocaml}
   --linkall
   %{read-strings:effects_flags.txt}
   -w
   no-missing-effects-backend
   -o
   %{target}
   %{dep:main.bc})))

(rule
 (target plugin.cmo)
 (action
  (run %{bin:ocamlc} -c %{dep:./plugin.ml})))

(rule
 (target plugin.js)
 (action
  (run
   %{bin:js_of_ocaml}
   %{read-strings:effects_flags.txt}
   %{dep:./plugin.cmo})))

(rule
 (target plugin2.cma)
 (action
  (run
   %{bin:ocamlc}
   -a
   %{dep:./plugin2a.ml}
   %{dep:./plugin2b.ml}
   -o
   plugin2.cma)))

(rule
 (target plugin2.js)
 (action
  (run
   %{bin:js_of_ocaml}
   %{read-strings:effects_flags.txt}
   %{dep:./plugin2.cma})))

; Dynlink uses CommonJS-style relative-path `require("./plugin.js")`,
; which the QuickJS shim does not implement.

(rule
 (target main.out)
 (enabled_if
  (<> %{profile} quickjs))
 (deps plugin.js plugin2.js)
 (action
  (with-outputs-to
   %{target}
   (run %{bin:node} %{dep:./main.js}))))

(rule
 (target main.out2)
 (enabled_if
  (<> %{profile} quickjs))
 (deps plugin.js plugin2.js)
 (action
  (with-outputs-to
   %{target}
   (run %{bin:node} %{dep:./main.bc.js}))))

(rule
 (alias runtest-js)
 (enabled_if
  (<> %{profile} quickjs))
 (action
  (diff main.out.expected main.out)))

(rule
 (alias runtest-js)
 (enabled_if
  (<> %{profile} quickjs))
 (action
  (diff main.out.expected main.out2)))
