[llvm] [CMake][OCaml] Make OCaml bindings suitable for out-of-tree install (PR #123478)

via llvm-commits llvm-commits at lists.llvm.org
Sat Jan 18 20:16:44 PST 2025


https://github.com/alan-j-hu updated https://github.com/llvm/llvm-project/pull/123478

>From 1644b1b49d898cf9230011b95037d3b1d2f6510c Mon Sep 17 00:00:00 2001
From: Alan Hu <ahulambda at gmail.com>
Date: Sat, 18 Jan 2025 16:20:20 -0500
Subject: [PATCH 1/3] [CMake][OCaml] Make OCaml bindings suitable for
 out-of-tree install

- CMAKE_{STATIC,SHARED}_LIBRARY_SUFFIX reports incorrect suffixes for Mac.
  Use .so and .a for both Mac and Linux.
- Add LLVM_OCAML_EXTERNAL_LLVM_LIBDIR option.
- LLVM dynamic library is always suffixed with major version
---
 llvm/bindings/ocaml/README.txt    |  1 +
 llvm/cmake/modules/AddOCaml.cmake | 13 +++++++++----
 2 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/llvm/bindings/ocaml/README.txt b/llvm/bindings/ocaml/README.txt
index 08b5514e0a6642..053931df40919a 100644
--- a/llvm/bindings/ocaml/README.txt
+++ b/llvm/bindings/ocaml/README.txt
@@ -19,6 +19,7 @@ The bindings can also be built out-of-tree, i.e. targeting a preinstalled
 LLVM. To do this, configure the LLVM build tree as follows:
 
     $ cmake -DLLVM_OCAML_OUT_OF_TREE=TRUE \
+            -DLLVM_OCAML_EXTERNAL_LLVM_LIBDIR=[llvm-config --libdir] \
             -DCMAKE_INSTALL_PREFIX=[Preinstalled LLVM path] \
             -DLLVM_OCAML_INSTALL_PATH=[OCaml install prefix] \
             [... any other options]
diff --git a/llvm/cmake/modules/AddOCaml.cmake b/llvm/cmake/modules/AddOCaml.cmake
index 2d9116b08a5261..8a7720d5dc3e38 100644
--- a/llvm/cmake/modules/AddOCaml.cmake
+++ b/llvm/cmake/modules/AddOCaml.cmake
@@ -40,10 +40,10 @@ function(add_ocaml_library name)
   set(ocaml_outputs "${bin}/${name}.cma")
   if( ARG_C )
     list(APPEND ocaml_outputs
-         "${bin}/lib${name}${CMAKE_STATIC_LIBRARY_SUFFIX}")
+         "${bin}/lib${name}.a")
     if ( BUILD_SHARED_LIBS )
       list(APPEND ocaml_outputs
-           "${bin}/dll${name}${CMAKE_SHARED_LIBRARY_SUFFIX}")
+           "${bin}/dll${name}.so")
     endif()
   endif()
   if( HAVE_OCAMLOPT )
@@ -52,7 +52,12 @@ function(add_ocaml_library name)
          "${bin}/${name}${CMAKE_STATIC_LIBRARY_SUFFIX}")
   endif()
 
-  set(ocaml_flags "-lstdc++" "-ldopt" "-L${LLVM_LIBRARY_DIR}"
+  if ( LLVM_OCAML_OUT_OF_TREE )
+    set(ocaml_llvm_libdir "-L${LLVM_OCAML_EXTERNAL_LLVM_LIBDIR}")
+  else()
+    set(ocaml_llvm_libdir "-L${LLVM_LIBRARY_DIR}")
+  endif()
+  set(ocaml_flags "-lstdc++" "${ocaml_llvm_libdir}"
                   "-ccopt" "-L\\$CAMLORIGIN/../.."
                   "-ccopt" "-Wl,-rpath,\\$CAMLORIGIN/../.."
                   ${ocaml_pkgs})
@@ -67,7 +72,7 @@ function(add_ocaml_library name)
   endif()
 
   if(LLVM_LINK_LLVM_DYLIB)
-    list(APPEND ocaml_flags "-lLLVM")
+    list(APPEND ocaml_flags "-lLLVM-${LLVM_VERSION_MAJOR}")
   else()
     explicit_map_components_to_libraries(llvm_libs ${ARG_LLVM})
     foreach( llvm_lib ${llvm_libs} )

>From 583a0e1ffce240af295f9e511ae258592a8f7ce1 Mon Sep 17 00:00:00 2001
From: Alan Hu <ahulambda at gmail.com>
Date: Sat, 18 Jan 2025 21:43:35 -0500
Subject: [PATCH 2/3] Remove usage of CMAKE_SHARED_LIBRARY_SUFFIX elsewhere in
 AddOCaml.cmake

---
 llvm/cmake/modules/AddOCaml.cmake | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/cmake/modules/AddOCaml.cmake b/llvm/cmake/modules/AddOCaml.cmake
index 8a7720d5dc3e38..64244718b676c0 100644
--- a/llvm/cmake/modules/AddOCaml.cmake
+++ b/llvm/cmake/modules/AddOCaml.cmake
@@ -206,9 +206,9 @@ function(add_ocaml_library name)
     if( NOT (ext STREQUAL ".cmo" OR
              ext STREQUAL ".ml" OR
              ext STREQUAL CMAKE_C_OUTPUT_EXTENSION OR
-             ext STREQUAL CMAKE_SHARED_LIBRARY_SUFFIX) )
+             ext STREQUAL ".so") )
       list(APPEND install_files "${ocaml_output}")
-    elseif( ext STREQUAL CMAKE_SHARED_LIBRARY_SUFFIX)
+    elseif( ext STREQUAL ".so")
       list(APPEND install_shlibs "${ocaml_output}")
     endif()
   endforeach()

>From 8ae9be93e5ed6e6eb69ac6452e7c9079bdadd5b3 Mon Sep 17 00:00:00 2001
From: Alan Hu <ahulambda at gmail.com>
Date: Sat, 18 Jan 2025 23:15:59 -0500
Subject: [PATCH 3/3] In AddOCaml.cmake, replace broken BUILD_SHARED_LIBS logic

---
 llvm/cmake/modules/AddOCaml.cmake | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/llvm/cmake/modules/AddOCaml.cmake b/llvm/cmake/modules/AddOCaml.cmake
index 64244718b676c0..92570c8a6fd5ac 100644
--- a/llvm/cmake/modules/AddOCaml.cmake
+++ b/llvm/cmake/modules/AddOCaml.cmake
@@ -38,10 +38,18 @@ function(add_ocaml_library name)
   set(ocaml_inputs)
 
   set(ocaml_outputs "${bin}/${name}.cma")
+  # Always use -custom when building in-tree
+  if( (NOT LLVM_OCAML_OUT_OF_TREE) OR LLVM_OCAML_CUSTOM )
+    set(ocaml_custom TRUE)
+  else()
+    set(ocaml_custom FALSE)
+  endif()
+
   if( ARG_C )
+    # ocamlmklib outputs .a and .so
     list(APPEND ocaml_outputs
          "${bin}/lib${name}.a")
-    if ( BUILD_SHARED_LIBS )
+    if ( NOT ocaml_custom )
       list(APPEND ocaml_outputs
            "${bin}/dll${name}.so")
     endif()
@@ -67,7 +75,7 @@ function(add_ocaml_library name)
     list(APPEND ocaml_flags ${dep_ocaml_flags})
   endforeach()
 
-  if( NOT BUILD_SHARED_LIBS )
+  if( ocaml_custom )
     list(APPEND ocaml_flags "-custom")
   endif()
 



More information about the llvm-commits mailing list