Skip to content

Commit

Permalink
ocamlPackages.dune-configurator: workaround clang arg order bug
Browse files Browse the repository at this point in the history
reorder compiler arguments so the c code file name is last. this works
around a bug in clang which can indicate the compiler supports an
argument when it does not.

`https://github.com/ocaml/dune/pull/11123`
`https://github.com/llvm/llvm-project/issues/116278`
  • Loading branch information
paparodeo committed Nov 15, 2024
1 parent dc74870 commit 40018ec
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
From 3e9f115a8e931452d74e37da6f279e8bdd057088 Mon Sep 17 00:00:00 2001
From: Reno Dakota <paparodeo@proton.me>
Date: Thu, 14 Nov 2024 23:27:41 +0000
Subject: [PATCH] configurator: c file name is last in compiler args

Re-order the compiler arguments so the c file name is last. This works
around a clang bug which will suppress errors for unsupported flags if
linker arguments come after the c file name. This suppression is a
problem because it indicates that some argument is supported when it is
not which can result in build errors.

`https://github.com/llvm/llvm-project/issues/116278`

eg: on aarch64 darwin and clang > 16 `-mpopcnt` will generate an error

```
$ clang -mpopcnt hello.c
clang: error: unsupported option '-mpopcnt' for target 'aarch64-apple-darwin'
$ echo $?
1
```

however, if appending `-lpthread`:

```
$ clang -mpopcnt hello.c -lpthread
$ echo $?
0
```

Which is a problem as code will assume `-mpopcnt` is supported and will
then break later when compiling with the unsupported flag

Signed-off-by: Reno Dakota <paparodeo@proton.me>
---
otherlibs/configurator/src/v1.ml | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/otherlibs/configurator/src/v1.ml b/otherlibs/configurator/src/v1.ml
index ddd8446fb14..0bfd8ab4071 100644
--- a/otherlibs/configurator/src/v1.ml
+++ b/otherlibs/configurator/src/v1.ml
@@ -418,9 +418,9 @@ let compile_and_link_c_prog t ?(c_flags = []) ?(link_flags = []) code =
[ c_flags
; [ "-I"; t.stdlib_dir ]
; output_flag
- ; [ c_fname ]
; t.c_libraries
; link_flags
+ ; [ c_fname ]
])
in
if ok then Ok () else Error ()
8 changes: 7 additions & 1 deletion pkgs/development/ocaml-modules/dune-configurator/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
buildDunePackage rec {
pname = "dune-configurator";

inherit (dune_3) src version patches;
inherit (dune_3) src version;

patches = dune_3.patches or [] ++ [
# https://github.com/ocaml/dune/pull/11123
# https://github.com/llvm/llvm-project/issues/116278
./clang-arg-order-workaround.patch
];

# This fixes finding csexp
postPatch = ''
Expand Down

0 comments on commit 40018ec

Please sign in to comment.