diff --git a/CHANGES.md b/CHANGES.md index 6883cfe429b..70c7642d926 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -23,6 +23,9 @@ - Fix bootstrap on bytecode only switches on windows or where `-j1` is set. (#3112, @xclerc, @rgrinberg) +- Allow `enabled_if` fields in `executable(s)` stanzas (#3137, + fixes #1690 @voodoos) + 2.2.0 (06/02/2020) ------------------ diff --git a/doc/dune-files.rst b/doc/dune-files.rst index 8f476f31488..47c1fcd8580 100644 --- a/doc/dune-files.rst +++ b/doc/dune-files.rst @@ -645,6 +645,8 @@ Executables can also be linked as object or shared object files. See - ``(optional)`` is the same as the corresponding field of `library`_ +- ``(enabled_if )`` is the same as the corresponding field of `library`_ + - ``(promote )`` allows promoting the linked executables to the source tree. The options are the same as for the :ref:`rule promote mode `. Adding ``(promote (until-clean))`` to an diff --git a/src/dune/dune_file.ml b/src/dune/dune_file.ml index 511477994fa..4ae6389420d 100644 --- a/src/dune/dune_file.ml +++ b/src/dune/dune_file.ml @@ -1525,6 +1525,7 @@ module Executables = struct ; install_conf : File_binding.Unexpanded.t Install_conf.t option ; forbidden_libraries : (Loc.t * Lib_name.t) list ; bootstrap_info : string option + ; enabled_if : Blang.t } let bootstrap_info_extension = @@ -1581,6 +1582,7 @@ module Executables = struct User_error.raise ~loc [ Pp.text "This field is reserved for Dune itself" ]; fname) + and+ enabled_if = enabled_if ~since:(Some (2, 3)) in fun names ~multi -> let has_public_name = Names.has_public_name names in @@ -1621,6 +1623,7 @@ module Executables = struct ; install_conf ; forbidden_libraries ; bootstrap_info + ; enabled_if } let single, multi = @@ -2128,6 +2131,7 @@ module Tests = struct ; install_conf = None ; forbidden_libraries ; bootstrap_info = None + ; enabled_if } ; locks ; package diff --git a/src/dune/dune_file.mli b/src/dune/dune_file.mli index d670a9fe7f5..58d6f18589b 100644 --- a/src/dune/dune_file.mli +++ b/src/dune/dune_file.mli @@ -296,6 +296,7 @@ module Executables : sig ; install_conf : File_binding.Unexpanded.t Install_conf.t option ; forbidden_libraries : (Loc.t * Lib_name.t) list ; bootstrap_info : string option + ; enabled_if : Blang.t } (** Check if the executables have any foreign stubs or archives. *) diff --git a/src/dune/gen_rules.ml b/src/dune/gen_rules.ml index 7087c28e153..45415279178 100644 --- a/src/dune/gen_rules.ml +++ b/src/dune/gen_rules.ml @@ -83,7 +83,7 @@ end = struct | Foreign_library lib -> Lib_rules.foreign_rules lib ~sctx ~dir ~dir_contents ~expander; empty_none - | Executables exes -> + | Executables exes when Expander.eval_blang expander exes.enabled_if -> Option.iter exes.install_conf ~f:files_to_install; let cctx, merlin = Exe_rules.rules exes ~sctx ~dir ~scope ~expander ~dir_contents diff --git a/test/blackbox-tests/dune.inc b/test/blackbox-tests/dune.inc index f66af759dc5..e8b4195b5d2 100644 --- a/test/blackbox-tests/dune.inc +++ b/test/blackbox-tests/dune.inc @@ -601,6 +601,16 @@ (run %{exe:cram.exe} run.t -sanitizer %{bin:sanitizer}) (diff? run.t run.t.corrected))))) +(rule + (alias enabled_if-exec) + (deps (package dune) (source_tree test-cases/enabled_if-exec)) + (action + (chdir + test-cases/enabled_if-exec + (progn + (run %{exe:cram.exe} run.t -sanitizer %{bin:sanitizer}) + (diff? run.t run.t.corrected))))) + (rule (alias env-env-and-flags-include) (deps (package dune) (source_tree test-cases/env/env-and-flags-include)) @@ -2687,6 +2697,7 @@ (alias duplicate-target-no-loc) (alias embed-jbuild) (alias enabled_if) + (alias enabled_if-exec) (alias env-env-and-flags-include) (alias env-env-bin-pform) (alias env-env-bins) @@ -2940,6 +2951,7 @@ (alias duplicate-target-no-loc) (alias embed-jbuild) (alias enabled_if) + (alias enabled_if-exec) (alias env-env-and-flags-include) (alias env-env-bin-pform) (alias env-env-bins) diff --git a/test/blackbox-tests/test-cases/enabled_if-exec/dis.ml b/test/blackbox-tests/test-cases/enabled_if-exec/dis.ml new file mode 100644 index 00000000000..f4e5bb3586f --- /dev/null +++ b/test/blackbox-tests/test-cases/enabled_if-exec/dis.ml @@ -0,0 +1 @@ +Printf.eprintf "Ping" diff --git a/test/blackbox-tests/test-cases/enabled_if-exec/dune b/test/blackbox-tests/test-cases/enabled_if-exec/dune new file mode 100644 index 00000000000..9fbc66c3087 --- /dev/null +++ b/test/blackbox-tests/test-cases/enabled_if-exec/dune @@ -0,0 +1,9 @@ +(executable + (name main) + (modules main) + (enabled_if true)) + +(executable + (name dis) + (modules dis) + (enabled_if false)) diff --git a/test/blackbox-tests/test-cases/enabled_if-exec/dune-project b/test/blackbox-tests/test-cases/enabled_if-exec/dune-project new file mode 100644 index 00000000000..79642d909a7 --- /dev/null +++ b/test/blackbox-tests/test-cases/enabled_if-exec/dune-project @@ -0,0 +1 @@ +(lang dune 2.3) diff --git a/test/blackbox-tests/test-cases/enabled_if-exec/main.ml b/test/blackbox-tests/test-cases/enabled_if-exec/main.ml new file mode 100644 index 00000000000..4e2506818bb --- /dev/null +++ b/test/blackbox-tests/test-cases/enabled_if-exec/main.ml @@ -0,0 +1 @@ +Printf.eprintf "Pong" diff --git a/test/blackbox-tests/test-cases/enabled_if-exec/run.t b/test/blackbox-tests/test-cases/enabled_if-exec/run.t new file mode 100644 index 00000000000..453180406f4 --- /dev/null +++ b/test/blackbox-tests/test-cases/enabled_if-exec/run.t @@ -0,0 +1,15 @@ +Test that `enabled_if` fields work as expected for executables. +Since 2.3. + +This executable is disabled, any attempt to build it should fail: + $ dune build dis.exe + Error: Don't know how to build dis.exe + [1] + $ dune exec ./dis.exe + Error: Program "./dis.exe" not found! + [1] + +This one is enabled + $ dune exec ./main.exe + Pong + diff --git a/test/blackbox-tests/test-cases/enabled_if/run.t b/test/blackbox-tests/test-cases/enabled_if/run.t index 8db2d48a2e1..74c5f2fa53d 100644 --- a/test/blackbox-tests/test-cases/enabled_if/run.t +++ b/test/blackbox-tests/test-cases/enabled_if/run.t @@ -19,6 +19,10 @@ This one is enabled: Test the enabled_if field for libraries: + $ dune build foo + Error: Don't know how to build foo + [1] + $ dune build main.exe File "dune", line 33, characters 12-15: 33 | (libraries foo))