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

[3.17] backport #11252: fix(melange): pass melange.emit's compile_flags to js emission (#… #11370

Open
wants to merge 1 commit into
base: 3.17
Choose a base branch
from
Open
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
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 @@ -474,7 +478,7 @@ let setup_entries_js
ocaml.lib_config
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 @@ -494,6 +498,7 @@ let setup_entries_js
~obj_dir
~sctx
~includes
~compile_flags
~local_modules_and_obj_dir
m)
;;
Expand Down Expand Up @@ -557,7 +562,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 */
Loading