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 c38244f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
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 <[email protected]>
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 <[email protected]>
---
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 ()
10 changes: 8 additions & 2 deletions pkgs/development/ocaml-modules/dune-configurator/default.nix
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
{ lib, buildDunePackage, dune_3, csexp }:
{ lib, buildDunePackage, dune_3, fetchpatch, csexp }:

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 c38244f

Please sign in to comment.