let run_file (filename : string) (output : string) =
  let cmd = Filename.quote_command filename ~stdout:output ~stderr:output [] in
  match Sys.command cmd with
  | 0 -> ()
  | exit_code -> failwith (Format.sprintf "non zero exit code: %d" exit_code)

let link_and_run (filename : string) (stdlib_dir : string)
    (compile_flags : string list) (stdlib_file : string)
    (target : string option) =
  let executable = Filename.remove_extension filename in
  Link.link_file filename executable stdlib_dir compile_flags stdlib_file target;
  let output_file = executable ^ ".out" in
  run_file executable output_file;
  let output = In_channel.with_open_text output_file In_channel.input_all in
  String.trim output
