Skip to content

Commit

Permalink
Add only_sources field to copy_files stanza (#9827)
Browse files Browse the repository at this point in the history
* copy_files: add test to show behavior with build files

Signed-off-by: Javier Chávarri <[email protected]>

* add copy_files_src stanza

Signed-off-by: Javier Chávarri <[email protected]>

* format

Signed-off-by: Javier Chávarri <[email protected]>

* copy_files: add only_sources field

Signed-off-by: Javier Chávarri <[email protected]>

* copy_files: guard new field against version check

Signed-off-by: Javier Chávarri <[email protected]>

* copy_files: don't rely on library for the test

Signed-off-by: Javier Chávarri <[email protected]>

* add changelog + docs

Signed-off-by: Javier Chávarri <[email protected]>

* rename only_sources to sources

Signed-off-by: Javier Chávarri <[email protected]>

* simpler test

Signed-off-by: Javier Chávarri <[email protected]>

* format

Signed-off-by: Javier Chávarri <[email protected]>

* replace sources bool with variant

Signed-off-by: Javier Chávarri <[email protected]>

* Revert "rename only_sources to sources"

This reverts commit dd2bd16.

Signed-off-by: Javier Chávarri <[email protected]>

* format

Signed-off-by: Javier Chávarri <[email protected]>

* Allow blang expressions

Signed-off-by: Etienne Millon <[email protected]>

---------

Signed-off-by: Javier Chávarri <[email protected]>
Signed-off-by: Etienne Millon <[email protected]>
Co-authored-by: Etienne Millon <[email protected]>
  • Loading branch information
jchavarri and emillon authored Feb 9, 2024
1 parent 45a7c80 commit fe12953
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 2 deletions.
2 changes: 2 additions & 0 deletions doc/changes/9827.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- add `only_sources` field to `copy_files` stanza (#9827, fixes #9709,
@jchavarri)
3 changes: 3 additions & 0 deletions doc/stanzas/copy_files.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ details.
- ``(enabled_if <blang expression>)`` conditionally disables this stanza. The
condition is specified using the :doc:`reference/boolean-language`.

- ``(only_sources <blang expression>)`` specifies that the glob in ``files``
gets applied over the source tree, and not the build tree.

The short form:

.. code:: dune
Expand Down
10 changes: 9 additions & 1 deletion src/dune_rules/simple_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,15 @@ let copy_files sctx ~dir ~expander ~src_dir (def : Copy_files.t) =
not the current directory."
];
(* add rules *)
let* files = Build_system.eval_pred (File_selector.of_glob ~dir:src_in_build glob) in
let* only_sources = Expander.eval_blang expander def.only_sources in
let* files =
let dir =
match only_sources with
| true -> src_in_src
| false -> src_in_build
in
Build_system.eval_pred (File_selector.of_glob ~dir glob)
in
(* CR-someday amokhov: We currently traverse the set [files] twice: first, to
add the corresponding rules, and then to convert the files to [targets]. To
do only one traversal we need [Memo.parallel_map_set]. *)
Expand Down
27 changes: 26 additions & 1 deletion src/dune_rules/stanzas/copy_files.ml
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
open Import
open Dune_lang.Decoder

type origin =
| Build
| Source

type t =
{ add_line_directive : bool
; alias : Alias.Name.t option
; mode : Rule.Mode.t
; enabled_if : Blang.t
; files : String_with_vars.t
; only_sources : Blang.t
; syntax_version : Dune_lang.Syntax.Version.t
}

Expand All @@ -16,14 +21,33 @@ include Stanza.Make (struct
include Poly
end)

let decode_only_sources =
let* blang = peek in
match blang with
| Some _ -> Blang.decode
| None -> return Blang.true_
;;

let long_form =
let check = Dune_lang.Syntax.since Stanza.syntax (2, 7) in
let+ alias = field_o "alias" (check >>> Dune_lang.Alias.decode)
and+ mode = field "mode" ~default:Rule.Mode.Standard (check >>> Rule_mode_decoder.decode)
and+ enabled_if = Enabled_if.decode ~allowed_vars:Any ~since:(Some (2, 8)) ()
and+ files = field "files" (check >>> String_with_vars.decode)
and+ only_sources =
field_o
"only_sources"
(Dune_lang.Syntax.since Stanza.syntax (3, 14) >>> decode_only_sources)
and+ syntax_version = Dune_lang.Syntax.get_exn Stanza.syntax in
{ add_line_directive = false; alias; mode; enabled_if; files; syntax_version }
let only_sources = Option.value only_sources ~default:Blang.false_ in
{ add_line_directive = false
; alias
; mode
; enabled_if
; files
; only_sources
; syntax_version
}
;;

let decode =
Expand All @@ -38,6 +62,7 @@ let decode =
; mode = Standard
; enabled_if = Blang.true_
; files
; only_sources = Blang.false_
; syntax_version
}
;;
5 changes: 5 additions & 0 deletions src/dune_rules/stanzas/copy_files.mli
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
open Import

type origin =
| Build
| Source

type t =
{ add_line_directive : bool
; alias : Alias.Name.t option
; mode : Rule.Mode.t
; enabled_if : Blang.t
; files : String_with_vars.t
; only_sources : Blang.t
; syntax_version : Dune_lang.Syntax.Version.t
}

Expand Down
45 changes: 45 additions & 0 deletions test/blackbox-tests/test-cases/copy_files/test6.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
Show that copy_files operates on the build folder

$ mkdir -p target foo
$ cat >dune-project <<EOF
> (lang dune 3.14)
> EOF
$ cat >target/dune <<EOF
> (copy_files
> (files ../foo/*.txt))
> EOF
$ cat >foo/dune <<EOF
> (rule
> (write-file in-build.txt ""))
> EOF

$ touch foo/in-source.txt

$ dune build target/in-source.txt
$ dune build target/in-build.txt

Show the difference when `only_sources` is used

$ cat >target/dune <<EOF
> (copy_files
> (only_sources)
> (files ../foo/*.txt))
> EOF

$ dune build target/in-source.txt
$ dune build target/in-build.txt
Error: Don't know how to build target/in-build.txt
[1]

A blang expression can be used:

$ cat >target/dune <<EOF
> (copy_files
> (only_sources (= x x))
> (files ../foo/*.txt))
> EOF

$ dune build target/in-source.txt
$ dune build target/in-build.txt
Error: Don't know how to build target/in-build.txt
[1]

0 comments on commit fe12953

Please sign in to comment.