Skip to content

Commit

Permalink
fix(melange): pass melange.emit's compile_flags to js emission (o…
Browse files Browse the repository at this point in the history
…caml#11252)

* test(melange): show that .js file emission doesn't respect flags

Signed-off-by: Antonio Nuno Monteiro <[email protected]>

* fix(melange): pass `melange.emit`'s `compile_flags` to js emission

Signed-off-by: Antonio Nuno Monteiro <[email protected]>

* chore: add changelog entry

Signed-off-by: Antonio Nuno Monteiro <[email protected]>

---------

Signed-off-by: Antonio Nuno Monteiro <[email protected]>
  • Loading branch information
anmonteiro authored and ElectreAAS committed Jan 27, 2025
1 parent 7bef0bf commit 8a7ace8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
3 changes: 3 additions & 0 deletions doc/changes/11252.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- Pass `melange.emit`'s `compile_flags` to the JS emission phase. (@amonteiro,
#11252)

25 changes: 15 additions & 10 deletions src/dune_rules/melange/melange_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ let build_js
~obj_dir
~sctx
~includes
~(compile_flags : Ocaml_flags.t)
~local_modules_and_obj_dir
m
=
Expand All @@ -218,6 +219,7 @@ let build_js
compiler
[ Command.Args.S obj_dir
; Command.Args.as_any includes
; Command.Args.dyn (Ocaml_flags.get compile_flags Melange)
; As melange_package_args
; A "-o"
; Target output
Expand Down Expand Up @@ -259,6 +261,13 @@ let add_deps_to_aliases ?(alias = Melange_stanzas.Emit.implicit_alias) ~dir deps
Memo.parallel_iter ~f:attach [ alias; dune_default_alias ]
;;

let melange_compile_flags ~sctx ~dir (mel : Melange_stanzas.Emit.t) =
let specific = Lib_mode.Map.make_all mel.compile_flags in
Ocaml_flags.Spec.make ~common:Ordered_set_lang.Unexpanded.standard ~specific
|> Ocaml_flags_db.ocaml_flags sctx ~dir
>>| Ocaml_flags.allow_only_melange
;;

let setup_emit_cmj_rules
~sctx
~dir
Expand Down Expand Up @@ -297,12 +306,7 @@ let setup_emit_cmj_rules
Modules.With_vlib.modules modules, pp
in
let requires_link = Lib.Compile.requires_link compile_info in
let* flags =
let specific = Lib_mode.Map.make_all mel.compile_flags in
Ocaml_flags.Spec.make ~common:Ordered_set_lang.Unexpanded.standard ~specific
|> Ocaml_flags_db.ocaml_flags sctx ~dir
>>| Ocaml_flags.allow_only_melange
in
let* flags = melange_compile_flags ~sctx ~dir mel in
let* cctx =
let direct_requires = Lib.Compile.direct_requires compile_info in
Compilation_context.create
Expand Down Expand Up @@ -473,7 +477,7 @@ let setup_entries_js
in
let requires_link = Resolve.return requires_link in
cmj_includes ~requires_link ~scope lib_config
in
and* compile_flags = melange_compile_flags ~sctx ~dir mel in
let output = `Private_library_or_emit target_dir in
let obj_dir = Obj_dir.of_local local_obj_dir in
let* () =
Expand All @@ -493,6 +497,7 @@ let setup_entries_js
~obj_dir
~sctx
~includes
~compile_flags
~local_modules_and_obj_dir
m)
;;
Expand Down Expand Up @@ -556,7 +561,7 @@ let setup_js_rules_libraries =
|> Resolve.Memo.map ~f:(with_vlib_implementations lib)
in
cmj_includes ~requires_link ~scope lib_config
in
and* compile_flags = melange_compile_flags ~sctx ~dir mel in
let+ () =
setup_runtime_assets_rules
sctx
Expand Down Expand Up @@ -602,13 +607,13 @@ let setup_js_rules_libraries =
~sctx
~scope
vlib
~f:(build_js ~dir ~output ~includes)
~f:(build_js ~dir ~output ~includes ~compile_flags)
and+ () =
parallel_build_source_modules
~sctx
~scope
lib
~f:(build_js ~dir ~output ~includes)
~f:(build_js ~dir ~output ~includes ~compile_flags)
in
())
;;
Expand Down
31 changes: 31 additions & 0 deletions test/blackbox-tests/test-cases/melange/flags-for-emission.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Test compile_flags are passed to JS file emission

$ cat > dune-project <<EOF
> (lang dune 3.13)
> (using melange 0.1)
> EOF
$ cat > main.ml <<EOF
> let () = Js.log "hello"
> EOF
$ cat > dune <<EOF
> (melange.emit
> (target output)
> (emit_stdlib false)
> (modules main)
> (compile_flags :standard --mel-no-version-header))
> EOF
$ dune build @melange
File is generated with the "Generated by Melange" header even though we
specified the opposite in flags
$ cat _build/default/output/main.js
'use strict';
console.log("hello");
/* Not a pure module */

0 comments on commit 8a7ace8

Please sign in to comment.