Skip to content

[BUG] C stubs dependencies are underspecified #13651

@nojb

Description

@nojb

Currently the rules that are used to build C stubs do not declare dependencies on the OCaml header files, only on their path, ie $(ocamlc -where)/caml). This means that if one switches compilers while keeping the same prefix (eg in a monorepo context), Dune will not rebuild C stubs. When using the Dune cache this is doubly bad of course.

One can reproduce the situation by running the following script in an empty directory. The script:

  • installs 5.3.0,
  • builds a simple C stub, and saves the verbose log in build-5.3.0.log
  • installs 5.4.1 in the same prefix
  • rebuilds the C stub (nothing actually gets rebuilt) and saves the verbose log in build-5.4.1.log.

TL;DR: it is not enough to depend on the path to the headers; the rules to compile C stubs should declare dependencies on the actual headers *.h in that directory.

cc @Alizter

#!/bin/bash

set -euxo pipefail

DUNE=$(opam exec -- which dune)

build() {
    rm -rf install
    mkdir install
    PREFIX=$(cd install && pwd)
    if ! [ -d "ocaml-$1" ]; then
        curl -L https://github.com/ocaml/ocaml/releases/download/$1/ocaml-$1.tar.gz | tar xz
    fi
    cd ocaml-$1
    if ! [ -f Makefile.config ]; then
        ./configure --prefix=$PREFIX --disable-debugger --disable-ocamldoc --disable-ocamltest
    fi
    make -j
    make install
    cd ..

    mkdir -p proj
    cd proj
    cat >dune-project <<EOF
(lang dune 3.21)
EOF
    cat >dune <<EOF
(executable
 (name main)
 (foreign_stubs
  (language c)
  (names cstub)))
EOF
    touch main.ml cstub.c
    PATH="$PREFIX/bin:$PATH" $DUNE build --root . --display verbose _build/default/cstub.o > ../build-$1.log 2>&1
    cd ..
}

build 5.3.0
build 5.4.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions