Skip to content

Add (instrumentation (libraries ...)) field - #15622

Open
nojb wants to merge 10 commits into
ocaml:mainfrom
nojb:instrumentation_libraries
Open

Add (instrumentation (libraries ...)) field#15622
nojb wants to merge 10 commits into
ocaml:mainfrom
nojb:instrumentation_libraries

Conversation

@nojb

@nojb nojb commented Jul 30, 2026

Copy link
Copy Markdown
Collaborator

This PR extends the (instrumentation (backend XXX)) field to optionally take a (libraries ...) subfield:

(instrumentation (backend XXX) (libraries libA ...))

The semantics is that the libraries libA ... are ignored if the instrumentation backend is disabled, but, if enabled, they are treated as if the user had included them in the (libraries) field of the enclosing stanza.

The motivation is the recent addition of support in the landmarks instrumentation for custom backends (see https://discuss.ocaml.org/t/ann-landmarks-1-7/18319). Custom backends are implemented as external libraries that need to be linked into the final executable in order to be used. However, we would only want to link the library if the instrumentation is active, and there is no easy way to achieve that today. This feature fills that gap.

cc @maroneze who first brough up this issue in LexiFi/landmarks#57.

Signed-off-by: Nicolás Ojeda Bär <n.oje.bar@gmail.com>
Comment thread src/dune_lang/preprocess.ml Outdated
Signed-off-by: Nicolás Ojeda Bär <n.oje.bar@gmail.com>
Comment on lines +47 to +48
>>| List.rev
>>| List.flatten

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
>>| List.rev
>>| List.flatten
>>| List.rev_concat

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I accepted this suggestion, but to be clear, the two forms are not equivalent: rev_concat X is rev (concat X), while what I had before was concat (rev X). I don't think it makes a difference here.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually thinking further about this, maybe its worth being correct rather than slick. Sorry for the back-and-forth.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted in 55ed9ba

@Alizter
Alizter self-requested a review July 30, 2026 13:28
Comment thread doc/instrumentation.rst Outdated
Comment on lines +73 to +74
:doc:`reference/library-dependencies`. This field is available since Dune
3.25.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we have

  .. versionadded:: 3.25

but I'm not sure it works here.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did something in 17e813b:

image

Signed-off-by: Nicolás Ojeda Bär <n.oje.bar@gmail.com>
@nojb
nojb force-pushed the instrumentation_libraries branch from f6d31a3 to 3d0b054 Compare July 30, 2026 13:53
@Alizter

Alizter commented Jul 30, 2026

Copy link
Copy Markdown
Collaborator

I think melange.emit accepts this new field but doesn't do anything with it now. You could have a go at supporting it, or otherwise user error and say its not supported at the moment.

@Alizter

Alizter commented Jul 30, 2026

Copy link
Copy Markdown
Collaborator

dune describe external-lib-deps also takes the --instrumentation-with flag, might be worth honoring the new field.

Signed-off-by: Nicolás Ojeda Bär <n.oje.bar@gmail.com>
@Alizter

Alizter commented Jul 30, 2026

Copy link
Copy Markdown
Collaborator

The libraries field also accepts (select) deps. If its possible might be worth supporting, or just rejecting with a user error, up to you.

@Alizter

Alizter commented Jul 30, 2026

Copy link
Copy Markdown
Collaborator

Optional executables don't build when they have missing libraries. Presumably they don't account for this new field, so likely they will be tricked into being built and then fail.

@Alizter

Alizter commented Jul 30, 2026

Copy link
Copy Markdown
Collaborator

Another consumer of libraries maybe worth pointing out is the utop rules. Presumably something like dune utop --instrument-with ... is possible, but does nothing special at the moment.

I've also thought about ppx, but I don't have a solid grasp on how the ppx infrastructure works, so I couldn't think of any issues there.

@Alizter

Alizter commented Jul 30, 2026

Copy link
Copy Markdown
Collaborator

Another consumer that comes to mind that would be worth checking interaction with is the @unused-libs alias.

nojb added 6 commits July 30, 2026 19:16
Signed-off-by: Nicolás Ojeda Bär <n.oje.bar@gmail.com>
Signed-off-by: Nicolás Ojeda Bär <n.oje.bar@gmail.com>
Signed-off-by: Nicolás Ojeda Bär <n.oje.bar@gmail.com>
Signed-off-by: Nicolás Ojeda Bär <n.oje.bar@gmail.com>
Signed-off-by: Nicolás Ojeda Bär <n.oje.bar@gmail.com>
Signed-off-by: Nicolás Ojeda Bär <n.oje.bar@gmail.com>
@nojb

nojb commented Jul 30, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks for the review @Alizter. I think I addressed all your comments. (optional) executables @unused-libs were already supported, but I added tests to confirm. @unused-libs has some limitations around -linkall but this is orthogonal to the present PR (and is documented in the test).

I think melange.emit accepts this new field but doesn't do anything with it now. You could have a go at supporting it, or otherwise user error and say its not supported at the moment.

42dbfbf

dune describe external-lib-deps also takes the --instrumentation-with flag, might be worth honoring the new field.

f92a50b

The libraries field also accepts (select) deps. If its possible might be worth supporting, or just rejecting with a user error, up to you.

681ef0f

Optional executables don't build when they have missing libraries. Presumably they don't account for this new field, so likely they will be tricked into being built and then fail.

51f9dc8

Another consumer that comes to mind that would be worth checking interaction with is the @unused-libs alias.

6296a8f

Another consumer of libraries maybe worth pointing out is the utop rules. Presumably something like dune utop --instrument-with ... is possible, but does nothing special at the moment.

b611df7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants