From e40419eaf2b10b463cd46781f747ec3c28dd7e7d Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Thu, 6 Apr 2023 20:08:07 -0600 Subject: [PATCH] fix: preserve directory hierarchy for installed headers If we have foo/bar.h, we'll preserve the hierarchy relative to the library directory Signed-off-by: Rudi Grinberg --- CHANGES.md | 4 ++++ src/dune_rules/install_rules.ml | 20 ++++++++++++++++--- .../foreign-stubs/installed-headers.t | 17 +++------------- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 80da99ac8587..63be3b029dfa 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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) diff --git a/src/dune_rules/install_rules.ml b/src/dune_rules/install_rules.ml index 18dbd4ddc93d..ebcd59c0bacc 100644 --- a/src/dune_rules/install_rules.ml +++ b/src/dune_rules/install_rules.ml @@ -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 @@ -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 = diff --git a/test/blackbox-tests/test-cases/foreign-stubs/installed-headers.t b/test/blackbox-tests/test-cases/foreign-stubs/installed-headers.t index 3333ad178252..a75b28ee0aab 100644 --- a/test/blackbox-tests/test-cases/foreign-stubs/installed-headers.t +++ b/test/blackbox-tests/test-cases/foreign-stubs/installed-headers.t @@ -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 @@ -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: @@ -61,11 +58,3 @@ Now we try to use the installed headers: > #include > 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 - | ^~~~~~~~~~~ - compilation terminated. - [1]