Skip to content

Commit

Permalink
Separate compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
vouillon committed Apr 2, 2024
1 parent 1e6a86a commit 76fcdaa
Show file tree
Hide file tree
Showing 12 changed files with 1,000 additions and 116 deletions.
33 changes: 33 additions & 0 deletions compiler/bin-wasm_of_ocaml/build_runtime.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
(* Js_of_ocaml compiler
* http://www.ocsigen.org/js_of_ocaml/
* Copyright (C) 2020 Hugo Heuzard
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, with linking exception;
* either version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*)

open! Js_of_ocaml_compiler.Stdlib

let info =
Info.make
~name:"build-runtime"
~doc:"Build standalone runtime. Used for separate compilation."
~description:
"Js_of_ocaml is a compiler from OCaml bytecode to Javascript. It makes it possible \
to run pure OCaml programs in JavaScript environments like web browsers and \
Node.js."

let command =
let t = Cmdliner.Term.(const Compile.run $ Cmd_arg.options_runtime_only) in
Cmdliner.Cmd.v info t
20 changes: 20 additions & 0 deletions compiler/bin-wasm_of_ocaml/build_runtime.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
(* Js_of_ocaml compiler
* http://www.ocsigen.org/js_of_ocaml/
* Copyright (C) 2020 Hugo Heuzard
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, with linking exception;
* either version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*)

val command : unit Cmdliner.Cmd.t
95 changes: 92 additions & 3 deletions compiler/bin-wasm_of_ocaml/cmd_arg.ml
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ type t =
; (* compile option *)
profile : Driver.profile option
; runtime_files : string list
; runtime_only : bool
; output_file : string * bool
; input_file : string
; input_file : string option
; enable_source_maps : bool
; sourcemap_root : string option
; sourcemap_don't_inline_content : bool
Expand Down Expand Up @@ -113,9 +114,17 @@ let options =
runtime_files =
let chop_extension s = try Filename.chop_extension s with Invalid_argument _ -> s in
let output_file =
let ext =
try
snd
(List.find
~f:(fun (ext, _) -> Filename.check_suffix input_file ext)
[ ".cmo", ".wasmo"; ".cma", ".wasma" ])
with Not_found -> ".js"
in
match output_file with
| Some s -> s, true
| None -> chop_extension input_file ^ ".js", false
| None -> chop_extension input_file ^ ext, false
in
let params : (string * string) list = List.flatten set_param in
let enable_source_maps = (not no_sourcemap) && sourcemap in
Expand All @@ -126,8 +135,9 @@ let options =
; include_dirs
; profile
; output_file
; input_file
; input_file = Some input_file
; runtime_files
; runtime_only = false
; enable_source_maps
; sourcemap_root
; sourcemap_don't_inline_content
Expand All @@ -149,3 +159,82 @@ let options =
$ runtime_files)
in
Term.ret t

let options_runtime_only =
let runtime_files =
let doc = "Link JavaScript and WebAssembly files [$(docv)]. " in
Arg.(value & pos_all string [] & info [] ~docv:"RUNTIME_FILES" ~doc)
in
let output_file =
let doc = "Set output file name to [$(docv)]." in
Arg.(required & opt (some string) None & info [ "o" ] ~docv:"FILE" ~doc)
in
let no_sourcemap =
let doc = "Currently ignored (for compatibility with Js_of_ocaml)." in
Arg.(value & flag & info [ "no-sourcemap"; "no-source-map" ] ~doc)
in
let sourcemap =
let doc = "Currently ignored (for compatibility with Js_of_ocaml)." in
Arg.(value & flag & info [ "sourcemap"; "source-map"; "source-map-inline" ] ~doc)
in
let sourcemap_don't_inline_content =
let doc = "Do not inline sources in source map." in
Arg.(value & flag & info [ "source-map-no-source" ] ~doc)
in
let sourcemap_root =
let doc = "root dir for source map." in
Arg.(value & opt (some string) None & info [ "source-map-root" ] ~doc)
in
let include_dirs =
let doc = "Add [$(docv)] to the list of include directories." in
Arg.(value & opt_all string [] & info [ "I" ] ~docv:"DIR" ~doc)
in
let set_param =
let doc = "Set compiler options." in
let all = List.map (Config.Param.all ()) ~f:(fun (x, _) -> x, x) in
Arg.(
value
& opt_all (list (pair ~sep:'=' (enum all) string)) []
& info [ "set" ] ~docv:"PARAM=VALUE" ~doc)
in
let build_t
common
set_param
include_dirs
sourcemap
no_sourcemap
sourcemap_don't_inline_content
sourcemap_root
output_file
runtime_files =
let params : (string * string) list = List.flatten set_param in
let enable_source_maps = (not no_sourcemap) && sourcemap in
let include_dirs = normalize_include_dirs include_dirs in
`Ok
{ common
; params
; include_dirs
; profile = None
; output_file = output_file, true
; input_file = None
; runtime_files
; runtime_only = true
; enable_source_maps
; sourcemap_root
; sourcemap_don't_inline_content
}
in
let t =
Term.(
const build_t
$ Jsoo_cmdline.Arg.t
$ set_param
$ include_dirs
$ sourcemap
$ no_sourcemap
$ sourcemap_don't_inline_content
$ sourcemap_root
$ output_file
$ runtime_files)
in
Term.ret t
5 changes: 4 additions & 1 deletion compiler/bin-wasm_of_ocaml/cmd_arg.mli
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ type t =
; (* compile option *)
profile : Driver.profile option
; runtime_files : string list
; runtime_only : bool
; output_file : string * bool
; input_file : string
; input_file : string option
; enable_source_maps : bool
; sourcemap_root : string option
; sourcemap_don't_inline_content : bool
Expand All @@ -34,3 +35,5 @@ type t =
}

val options : t Cmdliner.Term.t

val options_runtime_only : t Cmdliner.Term.t
Loading

0 comments on commit 76fcdaa

Please sign in to comment.