Skip to content

Commit

Permalink
Add link_deps field
Browse files Browse the repository at this point in the history
In some cases, the linking step requires some dependencies. For example,
passing a version script to the linker. The new `(link_deps)` field
uses the dependency DSL already used in other places.

Closes #852

Signed-off-by: Etienne Millon <etienne@cryptosense.com>
  • Loading branch information
emillon committed Jun 13, 2018
1 parent 86b1305 commit 2179c5c
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 2 deletions.
4 changes: 4 additions & 0 deletions doc/jbuild.rst
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,10 @@ Executables can also be linked as object or shared object files. See
- ``(link_flags <flags>)`` specifies additional flags to pass to the linker.
This field supports ``(:include ...)`` forms

- ``(link_deps (<deps-conf list>))`` specifies the dependencies used only by the
linker, for example when using a version script. See the `Dependency
specification`_ section for more details.

- ``(modules <modules>)`` specifies which modules in the current directory
Jbuilder should consider when building this executable. Modules not listed
here will be ignored and cannot be used inside the executable described by
Expand Down
9 changes: 7 additions & 2 deletions src/exe.ml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ let link_exe
~(linkage:Linkage.t)
~top_sorted_modules
?(link_flags=Build.arr (fun _ -> []))
?(link_deps=Build.arr (fun _ -> []))
?(js_of_ocaml=Jbuild.Js_of_ocaml.default)
cctx
=
Expand Down Expand Up @@ -137,7 +138,9 @@ let link_exe
artifacts modules ~ext:ctx.ext_obj))
in
SC.add_rule sctx
(Build.fanout3
(link_deps >>^ ignore
>>>
Build.fanout3
(register_native_objs_deps modules_and_cm_files >>^ snd)
(Ocaml_flags.get (CC.flags cctx) mode)
link_flags
Expand Down Expand Up @@ -173,6 +176,7 @@ let build_and_link_many
~linkages
?already_used
?link_flags
?link_deps
?(js_of_ocaml=Jbuild.Js_of_ocaml.default)
cctx
=
Expand All @@ -194,7 +198,8 @@ let build_and_link_many
~linkage
~top_sorted_modules
~js_of_ocaml
?link_flags))
?link_flags
?link_deps))

let build_and_link ~program =
build_and_link_many ~programs:[program]
5 changes: 5 additions & 0 deletions src/exe.mli
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
open! Import

(** Compilation and linking of executables *)

module Program : sig
Expand Down Expand Up @@ -41,6 +43,7 @@ val build_and_link
-> linkages:Linkage.t list
-> ?already_used:Module.Name.Set.t
-> ?link_flags:(unit, string list) Build.t
-> ?link_deps:(unit, Path.t list) Build.t
-> ?js_of_ocaml:Jbuild.Js_of_ocaml.t
-> Compilation_context.t
-> unit
Expand All @@ -50,6 +53,7 @@ val build_and_link_many
-> linkages:Linkage.t list
-> ?already_used:Module.Name.Set.t
-> ?link_flags:(unit, string list) Build.t
-> ?link_deps:(unit, Path.t list) Build.t
-> ?js_of_ocaml:Jbuild.Js_of_ocaml.t
-> Compilation_context.t
-> unit
Expand All @@ -62,6 +66,7 @@ val link_exe
-> linkage:Linkage.t
-> top_sorted_modules:(unit, Module.t list) Build.t
-> ?link_flags:(unit, string list) Build.t
-> ?link_deps:(unit, Path.t list) Build.t
-> ?js_of_ocaml:Jbuild.Js_of_ocaml.t
-> Compilation_context.t
-> unit
5 changes: 5 additions & 0 deletions src/gen_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -885,11 +885,16 @@ module Gen(P : Install_rules.Params) = struct
~loc:exes.buildable.loc ~modules
in

let link_deps =
SC.Deps.interpret sctx ~scope ~dir exes.link_deps
in

Exe.build_and_link_many cctx
~programs
~already_used
~linkages
~link_flags
~link_deps
~js_of_ocaml:exes.buildable.js_of_ocaml;

Merlin.make ()
Expand Down
3 changes: 3 additions & 0 deletions src/jbuild.ml
Original file line number Diff line number Diff line change
Expand Up @@ -875,13 +875,15 @@ module Executables = struct
{ names : (Loc.t * string) list
; link_executables : bool
; link_flags : Ordered_set_lang.Unexpanded.t
; link_deps : Dep_conf.t list
; modes : Link_mode.Set.t
; buildable : Buildable.t
}

let common_v1 project names public_names ~multi =
Buildable.v1 >>= fun buildable ->
field "link_executables" bool ~default:true >>= fun link_executables ->
field "link_deps" (list Dep_conf.t) ~default:[] >>= fun link_deps ->
field_oslu "link_flags" >>= fun link_flags ->
field "modes" Link_mode.Set.t ~default:Link_mode.Set.default
>>= fun modes ->
Expand All @@ -898,6 +900,7 @@ module Executables = struct
{ names
; link_executables
; link_flags
; link_deps
; modes
; buildable
}
Expand Down
1 change: 1 addition & 0 deletions src/jbuild.mli
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ module Executables : sig
{ names : (Loc.t * string) list
; link_executables : bool
; link_flags : Ordered_set_lang.Unexpanded.t
; link_deps : Dep_conf.t list
; modes : Link_mode.Set.t
; buildable : Buildable.t
}
Expand Down
10 changes: 10 additions & 0 deletions test/blackbox-tests/dune.inc
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,14 @@
test-cases/lib-available
(progn (run ${exe:cram.exe} -test run.t) (diff? run.t run.t.corrected))))))

(alias
((name link-deps)
(deps ((package dune) (files_recursively_in test-cases/link-deps)))
(action
(chdir
test-cases/link-deps
(progn (run ${exe:cram.exe} -test run.t) (diff? run.t run.t.corrected))))))

(alias
((name loop)
(deps ((package dune) (files_recursively_in test-cases/loop)))
Expand Down Expand Up @@ -592,6 +600,7 @@
(alias installable-dup-private-libs)
(alias intf-only)
(alias lib-available)
(alias link-deps)
(alias loop)
(alias menhir)
(alias merlin-tests)
Expand Down Expand Up @@ -656,6 +665,7 @@
(alias installable-dup-private-libs)
(alias intf-only)
(alias lib-available)
(alias link-deps)
(alias loop)
(alias merlin-tests)
(alias meta-gen)
Expand Down
Empty file.
11 changes: 11 additions & 0 deletions test/blackbox-tests/test-cases/link-deps/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(alias
((name message)
(deps (link_deps.cmo))
(action (echo "link\n"))
))

(executable
((name link_deps)
(link_deps ((alias message)))
)
)
1 change: 1 addition & 0 deletions test/blackbox-tests/test-cases/link-deps/dune-project
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(lang dune 1.0)
Empty file.
12 changes: 12 additions & 0 deletions test/blackbox-tests/test-cases/link-deps/run.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
It is possible to add link-time dependencies.

In particular, these can depend on the result of the compilation (like a .cmo
file) and be created just before linking.

$ dune build --display short link_deps.exe
ocamldep another.ml.d
ocamldep link_deps.ml.d
ocamlc .link_deps.eobjs/link_deps.{cmi,cmo,cmt}
link
ocamlopt .link_deps.eobjs/link_deps.{cmx,o}
ocamlopt link_deps.exe

0 comments on commit 2179c5c

Please sign in to comment.