Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

melange - add melange.emit targets to @all alias as well by default #7926

Merged
merged 2 commits into from
Jun 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ Unreleased
- Allow multiple globs in library's `(stdlib (internal_modules ..))`
(@anmonteiro, #7878)

- Attach melange rules to the default alias (#7926, @haochenx)

3.8.1 (2023-06-05)
------------------

Expand Down
31 changes: 13 additions & 18 deletions src/dune_rules/melange/melange_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,17 @@ let build_js ~loc ~dir ~pkg_name ~mode ~module_systems ~output ~obj_dir ~sctx
; Dep src
]))

(* attach [deps] to the specified [alias] AND the (dune default) [all] alias.

when [alias] is not supplied, {!Melange_stanzas.Emit.implicit_alias} is
assumed. *)
let add_deps_to_aliases ?(alias = Melange_stanzas.Emit.implicit_alias) ~dir deps
=
let alias = Alias.make alias ~dir in
let dune_default_alias = Alias.all ~dir in
let attach alias = Rules.Produce.Alias.add_deps alias deps in
Memo.parallel_iter ~f:attach [ alias; dune_default_alias ]

let setup_emit_cmj_rules ~sctx ~dir ~scope ~expander ~dir_contents
(mel : Melange_stanzas.Emit.t) =
let open Memo.O in
Expand Down Expand Up @@ -235,13 +246,7 @@ let setup_emit_cmj_rules ~sctx ~dir ~scope ~expander ~dir_contents
in
()
in
match mel.alias with
| None ->
let alias = Alias.make Melange_stanzas.Emit.implicit_alias ~dir in
Rules.Produce.Alias.add_deps alias emit_and_libs_deps
| Some alias_name ->
let alias = Alias.make alias_name ~dir in
Rules.Produce.Alias.add_deps alias emit_and_libs_deps
add_deps_to_aliases ?alias:mel.alias emit_and_libs_deps ~dir
in
( cctx
, Merlin.make ~requires:requires_compile ~stdlib_dir ~flags ~modules
Expand Down Expand Up @@ -323,17 +328,7 @@ let setup_runtime_assets_rules sctx ~dir ~target_dir ~mode ~output ~for_ mel =
Memo.parallel_iter copy ~f:(fun (src, dst) ->
Super_context.add_rule ~loc ~dir ~mode sctx
(Action_builder.copy ~src ~dst))
and+ () =
match mel.alias with
| None ->
let alias =
Alias.make Melange_stanzas.Emit.implicit_alias ~dir:target_dir
in
Rules.Produce.Alias.add_deps alias deps
| Some alias_name ->
let alias = Alias.make alias_name ~dir:target_dir in
Rules.Produce.Alias.add_deps alias deps
in
and+ () = add_deps_to_aliases ?alias:mel.alias deps ~dir:target_dir in
()

let modules_for_js_and_obj_dir ~sctx ~dir_contents ~scope
Expand Down
51 changes: 48 additions & 3 deletions test/blackbox-tests/test-cases/melange/aliases.t
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ Test alias field on melange.emit stanzas
$ cat > dune <<EOF
> (melange.emit
> (target output)
> (emit_stdlib false)
> (alias app))
> EOF

$ cat > main.ml <<EOF
> let () =
> print_endline "hello"
> Js.log "hello"
> EOF

$ dune build @app
Expand All @@ -25,15 +26,17 @@ Default alias melange works
$ cat > dune <<EOF
> (melange.emit
> (target output)
> (emit_stdlib false)
> (modules main))
> (melange.emit
> (target output2)
> (emit_stdlib false)
> (modules main2))
> EOF

$ cat > main2.ml <<EOF
> let () =
> print_endline "hello"
> Js.log "hello"
> EOF

$ dune clean
Expand All @@ -43,22 +46,49 @@ Default alias melange works
$ node _build/default/output2/main2.js
hello

Dune default alias works

$ cat > dune <<EOF
> (melange.emit
> (target output)
> (modules main)
> (emit_stdlib false))
> (melange.emit
> (target output2)
> (modules main2)
> (emit_stdlib false))
> EOF

$ cat > main2.ml <<EOF
> let () =
> Js.log "hello"
> EOF

$ dune clean
anmonteiro marked this conversation as resolved.
Show resolved Hide resolved
$ dune build
$ node _build/default/output/main.js
hello
$ node _build/default/output2/main2.js
hello

Users can override melange alias (even if useless)

$ cat > dune <<EOF
> (melange.emit
> (target output)
> (emit_stdlib false)
> (alias melange))
> EOF

$ dune clean
$ dune build @melange

If user defines an alias, the default alias is not used for that stanza
If user defines an alias, the default alias is not used for that stanza

$ cat > dune <<EOF
> (melange.emit
> (target output)
> (emit_stdlib false)
> (alias foo))
> EOF

Expand All @@ -67,3 +97,18 @@ If user defines an alias, the default alias is not used for that stanza
Error: Alias "melange" specified on the command line is empty.
It is not defined in . or any of its descendants.
[1]

Even if user defines an alias, dune default alias should still work

$ cat > dune <<EOF
> (melange.emit
> (target output)
anmonteiro marked this conversation as resolved.
Show resolved Hide resolved
> (modules main)
> (emit_stdlib false)
> (alias foo))
> EOF

$ dune clean
$ dune build
$ node _build/default/output/main.js
hello
10 changes: 8 additions & 2 deletions test/blackbox-tests/test-cases/melange/dune-rules.t
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ Test dune rules
> EOF

$ cat > main.ml <<EOF
> print_endline "hello"
> Js.log "hello"
> EOF

Calling dune rules with the 'all' alias works fine

$ dune rules @all | grep In_build_dir
(In_build_dir _build/default/.output.mobjs/melange/melange__Main.cmj))))

Calling dune rules with the alias works fine

$ dune rules @melange | grep In_build_dir
Expand All @@ -31,5 +36,6 @@ Creating dir fixes the problem

$ mkdir output

$ dune rules output | grep In_build_dir
$ dune rules output | grep "\.cmj"
(In_build_dir _build/default/.output.mobjs/melange/melange__Main.cmj))))
.output.mobjs/melange/melange__Main.cmj))))
Loading