Skip to content

Commit

Permalink
fix: preserve directory hierarchy for installed headers
Browse files Browse the repository at this point in the history
If we have foo/bar.h, we'll preserve the hierarchy relative to the
library directory

Signed-off-by: Rudi Grinberg <me@rgrinberg.com>

<!-- ps-id: cf542cda-1684-4719-bffd-fdb6a8bc2d63 -->
  • Loading branch information
rgrinberg committed Apr 7, 2023
1 parent ab74a71 commit e40419e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Unreleased
----------

- Preserve the directory structure of headers installed with the
`install_c_headers` field. Previously, they would all be flattened. Now the
directories are preserved upto the library stanza. (#7512, @rgrinberg)

- Resolve `ppx_runtime_libraries` in the target context when cross compiling
(#7450, fixes #2794, @anmonteiro)

Expand Down
20 changes: 17 additions & 3 deletions src/dune_rules/install_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,22 @@ end = struct
in
let+ execs = lib_ppxs ctx ~scope ~lib in
let install_c_headers =
List.map lib.install_c_headers ~f:(fun base ->
Path.Build.relative dir (base ^ Foreign_language.header_extension))
let f =
let path base =
Path.Build.relative dir (base ^ Foreign_language.header_extension)
in
let project = Scope.project scope in
if Dune_project.dune_version project < (3, 8) then fun base ->
make_entry Lib (path base)
else fun base ->
let sub_dir =
let dirname = Filename.dirname base in
if String.equal dirname Filename.current_dir_name then None
else in_sub_dir (Some dirname)
in
make_entry ?sub_dir Lib (path base)
in
List.map lib.install_c_headers ~f
in
List.concat
[ sources
Expand All @@ -292,7 +306,7 @@ end = struct
; List.map dll_files ~f:(fun a ->
let entry = Install.Entry.make ~kind:`File Stublibs a in
Install.Entry.Sourced.create ~loc entry)
; List.map ~f:(make_entry Lib) install_c_headers
; install_c_headers
]

let keep_if expander ~scope stanza =
Expand Down
17 changes: 3 additions & 14 deletions test/blackbox-tests/test-cases/foreign-stubs/installed-headers.t
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@ Headers with the same filename cannot be installed together:
$ touch foo.h inc/foo.h

$ dune build mypkg.install && cat _build/default/mypkg.install | grep ".h"
Error: Multiple rules generated for _build/install/default/lib/mypkg/foo.h:
- dune:1
- dune:1
-> required by _build/default/mypkg.install
[1]
"_build/install/default/lib/mypkg/foo.h"
"_build/install/default/lib/mypkg/inc/foo.h" {"inc/foo.h"}

Now we demonstrate that header paths get squashed when installed

Expand All @@ -32,8 +29,8 @@ Now we demonstrate that header paths get squashed when installed
> EOF

$ dune build mypkg.install && cat _build/default/mypkg.install | grep ".h"
"_build/install/default/lib/mypkg/bar.h"
"_build/install/default/lib/mypkg/foo.h"
"_build/install/default/lib/mypkg/inc/bar.h" {"inc/bar.h"}

Now we try to use the installed headers:

Expand Down Expand Up @@ -61,11 +58,3 @@ Now we try to use the installed headers:
> #include <inc/bar.h>
> EOF
$ dune build bar.exe
File "dune", line 6, characters 9-12:
6 | (names foo)))
^^^
foo.c:2:10: fatal error: inc/bar.h: No such file or directory
2 | #include <inc/bar.h>
| ^~~~~~~~~~~
compilation terminated.
[1]

0 comments on commit e40419e

Please sign in to comment.