ocaml / ocaml/dune

Allow optional libraries without explicit use

Open
#4,369 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

build question
Dominant language
OCaml
Stars
1.9k
Forks
500
Avg merge
15h 21m
Merged PRs (30d)
277

Description

Desired Behavior

In Liquidsoap, optional features are historically registered via a global hash, typically:

root.ml

let registered_features = Hashtbl.create 10

optional_feature.ml

let () =
  Hashtbl.add Root.registered_features optional_feature

This means that, in order to compile optional features, we just need to pass the corresponding module to the linker.

In the process of migrating to dune, our approach so far has been to define a main library, an optional library and use them to build the final executable:

(library
 (name root)
 (modules root))
 
(library
  (name optional_feature)
  (libraries some-dependency)
  (optional)
  (modules optional_feature))

However, in order to make sure that the optional module is linked we have to do a couple of annoying tricks:

(executable
(name application)
(modules application)
(link_flags -linkall)
(libraries
 root
 (select
  noop.ml
  from
  (optional_feature -> noop.dep.ml)
  (-> noop.dep.ml))))

Here we:

  • Force all modules to be linked
  • Use an empty noop.ml file to take advantage of alternative dependencies.

Is there an existing better way to do this? Alternatively, could it be possible for dune to support linking an optional library without having to specify a file using it?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the issue's root.ml, optional_feature.ml, noop.ml, and executable Dune snippets to understand the current linking workaround. Then inspect how optional libraries and -linkall are handled in Dune. Done means establishing whether a supported configuration already exists or defining and validating the requested way to link an optional library without an explicit file dependency.

Written by the indexing model from the issue text.

Assessment

Tech stack
ocaml
Domain
build-system
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.