Subject: [bug#50217] [PATCH v6] gnu: Add c2ffi.

From: Attila Lendvai <[email-masked]>
Date: Tue, 15 Feb 2022 20:51:47 +0100

Orignial work by muradm <[email-masked]>, fixes by Attila Lendvai.

* gnu/packages/cpp.scm (c2ffi): New variable.
---

v6: add a comment to clarify the versioning scheme.

 gnu/packages/cpp.scm | 48 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)

diff --git a/gnu/packages/cpp.scm b/gnu/packages/cpp.scm
index 86138531c0..6e690b593d 100644
--- a/gnu/packages/cpp.scm
+++ b/gnu/packages/cpp.scm
@@ -25,6 +25,8 @@
 ;;; Copyright © 2021 jgart <[email-masked]>
 ;;; Copyright © 2021 Julien Lepiller <[email-masked]>
 ;;; Copyright © 2021 Disseminate Dissent <[email-masked]>
+;;; Copyright © 2021 muradm <[email-masked]>
+;;; Copyright © 2021 Attila Lendvai <[email-masked]>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -1717,3 +1719,49 @@ (define-public bloomberg-bde
 multi-threaded applications and network applications.")
       (home-page "https://github.com/bloomberg/bde")
       (license license:asl2.0))))
+
+(define-public c2ffi
+  (package
+    (name "c2ffi")
+    ;; As per the c2ffi README: the first three elements are encoding the
+    ;; required Clang/LLVM version, and the last one is the c2ffi revision.
+    (version "12.0.0.0")
+    (source
+     (origin
+       (method git-fetch)
+       (uri (git-reference
+             (url "https://github.com/rpav/c2ffi")
+             (commit (string-append "v" version))))
+       (file-name (git-file-name name version))
+       (sha256
+        (base32 "1qq8dfismd20d9kfxpfvwz07v9mfvd0y7p5r3c92mk2pm4xnmzfy"))
+       (modules '((guix build utils)))
+       (snippet
+        '(substitute* "CMakeLists.txt"
+           ;; Guix seems to be packaging LLVM libs separately thus -lLLVM
+           ;; won't work, every used library must be specified explicitly.
+           (("c2ffi PUBLIC clang-cpp LLVM")
+            "c2ffi PUBLIC clang-cpp LLVMCore LLVMSupport LLVMMCParser \
+LLVMOption LLVMBitReader LLVMProfileData")))))
+    (build-system cmake-build-system)
+    (arguments
+     '(;; If LLVM was built without RTTI, we need to also be built without
+       ;; it.  See: https://stackoverflow.com/q/11904519
+       #:configure-flags '("-DCMAKE_CXX_FLAGS=-fno-rtti")
+                         #:phases
+                         (modify-phases %standard-phases
+                           (replace 'check
+                             (lambda* (#:key tests? #:allow-other-keys)
+                               (when tests?
+                                 (invoke "./bin/c2ffi" "--help")))))))
+    (native-inputs
+     (list clang-12)) ; CMakeLists.txt invokes `clang -print-resource-dir`
+    (inputs
+     (list clang-12)) ; Compiled with gcc, but links against libclang-cpp.so
+    (home-page "https://github.com/rpav/c2ffi")
+    (synopsis "Clang-based FFI wrapper generator")
+    (description "@code{c2ffi} is a tool for extracting definitions from C, C++, and
+Objective C headers for use with foreign function call interfaces.  It uses the
+@code{Clang/LLVM} infrastructure to extract the data, and emits it in various formats,
+including @code{json}.")
+    (license license:gpl2+)))
-- 
2.34.0