Skip to content

Commit

Permalink
Move jbuild support into a separate library (#2607)
Browse files Browse the repository at this point in the history
Signed-off-by: Jeremie Dimino <jeremie@dimino.org>
  • Loading branch information
jeremiedimino authored Sep 5, 2019
1 parent aba4a0a commit 591ce4d
Show file tree
Hide file tree
Showing 53 changed files with 642 additions and 894 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@
files. This is useful for installing only the binaries in a workspace for
example. (#2609, fixes #2554, @rgrinberg)

- Drop support for `jbuild` and `jbuild-ignore` files (#2607, @diml)

1.11.3 (23/08/2019)
-------------------

Expand Down
4 changes: 2 additions & 2 deletions bin/upgrade.ml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ let term =
Common.set_common common ~targets:[];
Scheduler.go ~common (fun () ->
Dune.Upgrader.upgrade
(Dune.File_tree.load Path.Source.root
~warn_when_seeing_jbuild_file:false ~ancestor_vcs:None)
(Dune.File_tree.load Path.Source.root ~recognize_jbuilder_projects:true
~ancestor_vcs:None)
|> Fiber.return)

let command = (term, info)
9 changes: 0 additions & 9 deletions src/dune/action_dune_lang.ml
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,6 @@ include Action_ast.Make (String_with_vars) (String_with_vars)
(Uast)
module Mapper = Action_mapper.Make (Uast) (Uast)

let upgrade_to_dune =
let id ~dir:_ p = p in
let dir = String_with_vars.make_text Loc.none "" in
Mapper.map ~dir ~f_program:id ~f_path:id ~f_target:id
~f_string:(fun ~dir:_ sw ->
String_with_vars.upgrade_to_dune sw ~allow_first_dep_var:false)

let encode_and_upgrade a = encode (upgrade_to_dune a)

let remove_locs =
let dir = String_with_vars.make_text Loc.none "" in
let f_program ~dir:_ = String_with_vars.remove_locs in
Expand Down
2 changes: 0 additions & 2 deletions src/dune/action_dune_lang.mli
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ include

include Dune_lang.Conv with type t := t

val encode_and_upgrade : t Dune_lang.Encoder.t

include
Action_intf.Helpers
with type t := t
Expand Down
2 changes: 1 addition & 1 deletion src/dune/dune
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
(name dune)
(libraries unix stdune fiber incremental_cycles dag memo xdg dune_re
threads.posix opam_file_format dune_lang dune_manager dune_memory
ocaml_config catapult)
ocaml_config catapult jbuild_support)
(synopsis "Internal Dune library, do not use!")
(preprocess future_syntax))

Expand Down
9 changes: 3 additions & 6 deletions src/dune/dune_file.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1347,7 +1347,6 @@ module Executables = struct
; project : Dune_project.t
; loc : Loc.t
; multi : bool
; file_kind : Dune_lang.File_syntax.t
}

let names t = t.names
Expand Down Expand Up @@ -1403,7 +1402,6 @@ module Executables = struct
single_fields
and+ loc = loc
and+ dune_syntax = Syntax.get_exn Stanza.syntax
and+ file_kind = Stanza.file_kind ()
and+ package =
field_o "package"
(let+ loc = loc
Expand Down Expand Up @@ -1456,13 +1454,12 @@ module Executables = struct
Pkg.default_exn ~loc project (pluralize "executable" ~multi)
}
| Some (loc, _), None ->
User_warning.emit ~is_error:(file_kind = Dune) ~loc
User_error.raise ~loc
[ Pp.textf "This field is useless without a (%s ...) field."
(pluralize "public_name" ~multi)
];
None
]
in
{ names; public; project; stanza; loc; multi; file_kind }
{ names; public; project; stanza; loc; multi }

let install_conf t ~ext =
Option.map t.public ~f:(fun { package; public_names } ->
Expand Down
3 changes: 2 additions & 1 deletion src/dune/dune_init.ml
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,10 @@ module Init_context = struct
let project =
match
Dune_project.load ~dir:Path.Source.root ~files:String.Set.empty
~infer_from_opam_files:true
with
| Some p -> p
| None -> Lazy.force Dune_project.anonymous
| None -> Dune_project.anonymous ~dir:Path.Source.root
in
let dir =
match path with
Expand Down
116 changes: 38 additions & 78 deletions src/dune/dune_load.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ module Dune_file = struct
{ dir : Path.Source.t
; project : Dune_project.t
; stanzas : Stanzas.t
; kind : Dune_lang.File_syntax.t
}

let parse sexps ~dir ~file ~project ~kind =
let parse sexps ~dir ~file ~project =
let stanzas = Stanzas.parse ~file project sexps in
let stanzas =
if !Clflags.ignore_promoted_rules then
Expand All @@ -22,7 +21,7 @@ module Dune_file = struct
else
stanzas
in
{ dir; project; stanzas; kind }
{ dir; project; stanzas }

let rec fold_stanzas l ~init ~f =
match l with
Expand All @@ -40,7 +39,6 @@ module Dune_files = struct
{ dir : Path.Source.t
; file : Path.Source.t
; project : Dune_project.t
; kind : Dune_lang.File_syntax.t
}

type one =
Expand All @@ -54,56 +52,32 @@ module Dune_files = struct
let ensure_parent_dir_exists path =
Path.build path |> Path.parent |> Option.iter ~f:Path.mkdir_p

type requires =
| No_requires
| Unix

let extract_requires path str ~kind =
let rec loop n lines acc =
match lines with
| [] -> acc
| line :: lines ->
let acc =
match Scanf.sscanf line "#require %S" (fun x -> x) with
| exception _ -> acc
| s -> (
let loc : Loc.t =
let start : Lexing.position =
{ pos_fname = Path.to_string path
; pos_lnum = n
; pos_cnum = 0
; pos_bol = 0
}
in
{ start; stop = { start with pos_cnum = String.length line } }
let check_no_requires path str =
List.iteri (String.split str ~on:'\n') ~f:(fun n line ->
match Scanf.sscanf line "#require %S" (fun x -> x) with
| exception _ -> ()
| (_ : string) ->
let loc : Loc.t =
let start : Lexing.position =
{ pos_fname = Path.to_string path
; pos_lnum = n
; pos_cnum = 0
; pos_bol = 0
}
in
( match (kind : Dune_lang.File_syntax.t) with
| Jbuild -> ()
| Dune ->
User_error.raise ~loc
[ Pp.text "#require is no longer supported in dune files."
; Pp.text
"You can use the following function instead of \
Unix.open_process_in:\n\n\
\ (** Execute a command and read it's output *)\n\
\ val run_and_read_lines : string -> string list"
] );
match String.split s ~on:',' with
| [] -> acc
| [ "unix" ] -> Unix
| _ ->
User_error.raise ~loc
[ Pp.text
"Using libraries other that \"unix\" is not supported."
; Pp.text "See the manual for details."
] )
in
loop (n + 1) lines acc
in
loop 1 (String.split str ~on:'\n') No_requires
{ start; stop = { start with pos_cnum = String.length line } }
in
User_error.raise ~loc
[ Pp.text "#require is no longer supported in dune files."
; Pp.text
"You can use the following function instead of \
Unix.open_process_in:\n\n\
\ (** Execute a command and read it's output *)\n\
\ val run_and_read_lines : string -> string list"
])

let create_plugin_wrapper (context : Context.t) ~exec_dir ~plugin ~wrapper
~target ~kind =
~target =
let plugin_contents = Io.read_file plugin in
Io.with_file_out (Path.build wrapper) ~f:(fun oc ->
let ocamlc_config =
Expand Down Expand Up @@ -168,7 +142,7 @@ end
context.name context.version_string ocamlc_config
(Path.reach ~from:exec_dir (Path.build target))
(Path.to_string plugin) plugin_contents);
extract_requires plugin plugin_contents ~kind
check_no_requires plugin plugin_contents

let eval dune_files ~(context : Context.t) =
let open Fiber.O in
Expand All @@ -177,7 +151,7 @@ end
| Literal x -> Left x
| Script x -> Right x)
in
Fiber.parallel_map dynamic ~f:(fun { dir; file; project; kind } ->
Fiber.parallel_map dynamic ~f:(fun { dir; file; project } ->
let generated_dune_file =
Path.Build.append_source
(Path.Build.relative generated_dune_files_dir context.name)
Expand All @@ -187,27 +161,15 @@ end
Path.Build.extend_basename generated_dune_file ~suffix:".ml"
in
ensure_parent_dir_exists generated_dune_file;
let requires =
create_plugin_wrapper context ~exec_dir:(Path.source dir)
~plugin:(Path.source file) ~wrapper ~target:generated_dune_file
~kind
in
create_plugin_wrapper context ~exec_dir:(Path.source dir)
~plugin:(Path.source file) ~wrapper ~target:generated_dune_file;
let context = Option.value context.for_host ~default:context in
let cmas =
match requires with
| No_requires -> []
| Unix -> [ "unix.cma" ]
in
let args =
List.concat
[ [ "-I"; "+compiler-libs" ]
; cmas
; [ Path.to_absolute_filename (Path.build wrapper) ]
]
in
(* CR-someday jdimino: if we want to allow plugins to use findlib: {[
let args = match context.toplevel_path with | None -> args | Some
path -> "-I" :: Path.reach ~from:dir path :: args in ]} *)
let* () =
Process.run Strict ~dir:(Path.source dir) ~env:context.env
context.ocaml args
Expand All @@ -219,11 +181,8 @@ end
; Pp.textf "Did you forgot to call [Jbuild_plugin.V*.send]?"
];
Fiber.return
( Dune_lang.Io.load
(Path.build generated_dune_file)
~mode:Many
~lexer:(Dune_lang.Lexer.of_syntax kind)
|> Dune_file.parse ~dir ~file ~project ~kind ))
( Dune_lang.Io.load (Path.build generated_dune_file) ~mode:Many
|> Dune_file.parse ~dir ~file ~project ))
>>| fun dynamic -> static @ dynamic
end

Expand All @@ -235,19 +194,20 @@ type conf =
}

let interpret ~dir ~project ~(dune_file : File_tree.Dune_file.t) =
match dune_file.contents with
match dune_file with
| Plain p ->
let dune_file =
Dune_files.Literal
(Dune_file.parse p.sexps ~dir ~file:p.path ~project
~kind:dune_file.kind)
Dune_files.Literal (Dune_file.parse p.sexps ~dir ~file:p.path ~project)
in
p.sexps <- [];
dune_file
| Ocaml_script file -> Script { dir; project; file; kind = dune_file.kind }
| Ocaml_script file -> Script { dir; project; file }

let load ~ancestor_vcs () =
let ftree = File_tree.load Path.Source.root ~ancestor_vcs in
let ftree =
File_tree.load Path.Source.root ~ancestor_vcs
~recognize_jbuilder_projects:false
in
let projects =
File_tree.fold ftree
~traverse:{ data_only = false; vendored = true; normal = true } ~init:[]
Expand Down
1 change: 0 additions & 1 deletion src/dune/dune_load.mli
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ module Dune_file : sig
{ dir : Path.Source.t
; project : Dune_project.t
; stanzas : Dune_file.Stanzas.t
; kind : Dune_lang.File_syntax.t
}

val fold_stanzas :
Expand Down
Loading

0 comments on commit 591ce4d

Please sign in to comment.