Skip to content

Commit

Permalink
ocaml#766 : enable specifying flags when compiling binaries from inli…
Browse files Browse the repository at this point in the history
…ne tests

Signed-off-by: lubegasimon <[email protected]>
  • Loading branch information
lubegasimon committed Aug 26, 2020
1 parent 6d83f12 commit 0d00697
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/dune_rules/exe_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ let executables_rules ~sctx ~dir ~expander ~dir_contents ~scope ~compile_info
else
l
in
let flags = SC.ocaml_flags sctx ~dir exes.buildable in
let flags = SC.ocaml_flags sctx ~dir exes.buildable.flags in
let link_deps = Dep_conf_eval.unnamed ~expander exes.link_deps in
let foreign_archives = exes.buildable.foreign_archives |> List.map ~f:snd in
let link_flags =
Expand Down
9 changes: 7 additions & 2 deletions src/dune_rules/inline_tests.ml
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ include Sub_system.Register_end_point (struct
; deps : Dep_conf.t list
; modes : Mode_conf.Set.t
; flags : Ordered_set_lang.Unexpanded.t
; compile_flags : Ocaml_flags.Spec.t
; backend : (Loc.t * Lib_name.t) option
; libraries : (Loc.t * Lib_name.t) list
}
Expand All @@ -183,6 +184,7 @@ include Sub_system.Register_end_point (struct
(let+ loc = loc
and+ deps = field "deps" (repeat Dep_conf.decode) ~default:[]
and+ flags = Ordered_set_lang.Unexpanded.field "flags"
and+ compile_flags = Ocaml_flags.Spec.decode_compile_flags_field "compile_flags"
and+ backend = field_o "backend" (located Lib_name.decode)
and+ libraries =
field "libraries" (repeat (located Lib_name.decode)) ~default:[]
Expand All @@ -191,7 +193,7 @@ include Sub_system.Register_end_point (struct
(Dune_lang.Syntax.since syntax (1, 11) >>> Mode_conf.Set.decode)
~default:Mode_conf.Set.default
in
{ loc; deps; flags; backend; libraries; modes })
{ loc; deps; flags; compile_flags; backend; libraries; modes })

(* We don't use this at the moment, but we could implement it for debugging
purposes *)
Expand Down Expand Up @@ -281,7 +283,10 @@ include Sub_system.Register_end_point (struct
Compilation_context.create () ~super_context:sctx ~expander ~scope
~obj_dir ~modules ~opaque:(Explicit false) ~requires_compile:runner_libs
~requires_link:(lazy runner_libs)
~flags:(Ocaml_flags.of_list [ "-w"; "-24"; "-g" ])
~flags:
(Ocaml_flags.append_common
(Super_context.ocaml_flags sctx ~dir info.compile_flags)
[ "-w"; "-24"; "-g" ])
~js_of_ocaml:(Some lib.buildable.js_of_ocaml) ~dynlink:false
~package:(Option.map lib.public ~f:Dune_file.Public_lib.package)
in
Expand Down
2 changes: 1 addition & 1 deletion src/dune_rules/lib_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ let cctx (lib : Library.t) ~sctx ~source_modules ~dir ~expander ~scope
else
Required
in
let flags = Super_context.ocaml_flags sctx ~dir lib.buildable in
let flags = Super_context.ocaml_flags sctx ~dir lib.buildable.flags in
let obj_dir = Library.obj_dir ~dir lib in
let vimpl = Virtual_rules.impl sctx ~lib ~scope in
let ctx = Super_context.context sctx in
Expand Down
7 changes: 7 additions & 0 deletions src/dune_rules/ocaml_flags.ml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ module Spec = struct
and+ native = field_oslu "ocamlopt_flags" in
let specific = Mode.Dict.make ~native ~byte in
{ common; specific }

let decode_compile_flags_field field_name =
let open Dune_lang.Decoder in
let field_oslu = Ordered_set_lang.Unexpanded.field in
let+ common = field_oslu field_name in
let specific = Mode.Dict.make_both Ordered_set_lang.Unexpanded.standard in
{ common; specific }
end

type t = string list Build.t t'
Expand Down
2 changes: 2 additions & 0 deletions src/dune_rules/ocaml_flags.mli
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ module Spec : sig
val decode : t Dune_lang.Decoder.fields_parser

val standard : t

val decode_compile_flags_field : string -> t Dune_lang.Decoder.fields_parser
end

val make :
Expand Down
4 changes: 2 additions & 2 deletions src/dune_rules/super_context.ml
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,10 @@ let build_dir_is_vendored build_dir =
in
Option.value ~default:false opt

let ocaml_flags t ~dir (x : Dune_file.Buildable.t) =
let ocaml_flags t ~dir (f : Ocaml_flags.Spec.t) =
let expander = Env_tree.expander t.env_tree ~dir in
let flags =
Ocaml_flags.make ~spec:x.flags
Ocaml_flags.make ~spec:f
~default:(get_node t.env_tree ~dir |> Env_node.ocaml_flags)
~eval:(Expander.expand_and_eval_set expander)
in
Expand Down
2 changes: 1 addition & 1 deletion src/dune_rules/super_context.mli
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ val internal_lib_names : t -> Lib_name.Set.t
(** Compute the ocaml flags based on the directory environment and a buildable
stanza *)
val ocaml_flags :
t -> dir:Path.Build.t -> Dune_file.Buildable.t -> Ocaml_flags.t
t -> dir:Path.Build.t -> Ocaml_flags.Spec.t -> Ocaml_flags.t

val foreign_flags :
t
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(library
(name backend_foo_bar)
(modules ())
(inline_tests.backend
(generate_runner
(echo "let () = print_endline \"backend_foo_bar\""))))

(library
(name compile_flags_test_with_invalid_flag)
(inline_tests
(backend backend_foo_bar)
(compile_flags -flag-that-is-not-accepted-by-ocaml)))
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(lang dune 2.7)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(library
(name backend_foo)
(modules ())
(inline_tests.backend
(generate_runner
(echo "let () = print_endline \"backend_foo\""))))

(library
(name compile_flags_test)
(inline_tests
(backend backend_foo)
(compile_flags -nolabels)))
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(lang dune 2.7)
16 changes: 16 additions & 0 deletions test/blackbox-tests/test-cases/inline_tests/compile-flags.t/run.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
This test ensures that compilation fails when an invalid flag is supplied
to compile_flags field.

First, we pass a valid flag to compile_flags field expecting compilation
to be successful.

$ dune runtest dune-file-with-valid-flag
inline_test_runner_compile_flags_test alias dune-file-with-valid-flag/runtest
backend_foo

Lastly, we pass an invalid flag to compile_flags field expecting compilation
to fail.

$ output=$(dune runtest dune-file-with-invalid-flag 2>&1); result=$?; (echo $output | grep -o "unknown option '-flag-that-is-not-accepted-by-ocaml'."); (exit $result)
unknown option '-flag-that-is-not-accepted-by-ocaml'.
[1]

0 comments on commit 0d00697

Please sign in to comment.