[llvm] [OCaml] Build OCaml bindings using Dune (PR #67272)
via llvm-commits
llvm-commits at lists.llvm.org
Sun Sep 24 13:34:19 PDT 2023
https://github.com/alan-j-hu created https://github.com/llvm/llvm-project/pull/67272
This PR still needs a lot of cleanup. Work to do include:
- Deleting the now unused CMake OCaml modules
- Deciding what to do with the OCaml documentation generation, which I currently surround with `if(FALSE) endif()`
- Detecting Dune on the build machine
One problem of note:
- The function `llvm_diagnostic_handler_trampoline` in `llvm_ocaml.c` needs to be marked as `static` or else the linker will produce an error about non-relocatable code. Even though the error message suggests adding `-fPIC`, adding this flag doesn't seem to fix the issue. This issue does not happen with the current CMake+OCamlfind build system.
However, the OCaml unit tests currently pass on my computer (if you build `ocaml_all` and then `check-llvm-bindings-ocaml`). Therefore, the PR is in a state in which I would appreciate other pairs of eyes to see if this work is on the right track, and if there is possibility that the OCaml bindings could use Dune.
>From d33189cb8dbbf3f0dc0e270ac231b7bd15d3ff68 Mon Sep 17 00:00:00 2001
From: Alan Hu <alanh at ccs.neu.edu>
Date: Sat, 23 Sep 2023 01:52:48 -0400
Subject: [PATCH 01/11] Have a go at trying to build bindings with Dune
---
llvm/bindings/ocaml/all_backends/dune | 9 ++
llvm/bindings/ocaml/analysis/dune | 9 ++
llvm/bindings/ocaml/backends/dune.in | 10 +++
llvm/bindings/ocaml/bitreader/dune | 9 ++
llvm/bindings/ocaml/bitwriter/dune | 9 ++
llvm/bindings/ocaml/debuginfo/dune | 9 ++
llvm/bindings/ocaml/dune-project | 1 +
llvm/bindings/ocaml/executionengine/dune | 9 ++
llvm/bindings/ocaml/irreader/dune | 9 ++
llvm/bindings/ocaml/linker/dune | 9 ++
llvm/bindings/ocaml/llvm.opam | 0
llvm/bindings/ocaml/llvm/dune | 7 ++
llvm/bindings/ocaml/setup.sh | 88 +++++++++++++++++++
llvm/bindings/ocaml/target/dune | 9 ++
.../ocaml/transforms/passbuilder/dune | 10 +++
llvm/bindings/ocaml/transforms/utils/dune | 9 ++
16 files changed, 206 insertions(+)
create mode 100644 llvm/bindings/ocaml/all_backends/dune
create mode 100644 llvm/bindings/ocaml/analysis/dune
create mode 100644 llvm/bindings/ocaml/backends/dune.in
create mode 100644 llvm/bindings/ocaml/bitreader/dune
create mode 100644 llvm/bindings/ocaml/bitwriter/dune
create mode 100644 llvm/bindings/ocaml/debuginfo/dune
create mode 100644 llvm/bindings/ocaml/dune-project
create mode 100644 llvm/bindings/ocaml/executionengine/dune
create mode 100644 llvm/bindings/ocaml/irreader/dune
create mode 100644 llvm/bindings/ocaml/linker/dune
create mode 100644 llvm/bindings/ocaml/llvm.opam
create mode 100644 llvm/bindings/ocaml/llvm/dune
create mode 100755 llvm/bindings/ocaml/setup.sh
create mode 100644 llvm/bindings/ocaml/target/dune
create mode 100644 llvm/bindings/ocaml/transforms/passbuilder/dune
create mode 100644 llvm/bindings/ocaml/transforms/utils/dune
diff --git a/llvm/bindings/ocaml/all_backends/dune b/llvm/bindings/ocaml/all_backends/dune
new file mode 100644
index 000000000000000..0cf0fa4a91d644c
--- /dev/null
+++ b/llvm/bindings/ocaml/all_backends/dune
@@ -0,0 +1,9 @@
+(library
+ (name llvm_all_backends)
+ (public_name llvm.all_backends)
+ (libraries llvm)
+ (foreign_stubs
+ (language c)
+ (names all_backends_ocaml)
+ (extra_deps ../llvm/llvm_ocaml.h))
+ (c_library_flags %{env:LLVM_All_backends_LIB=}))
diff --git a/llvm/bindings/ocaml/analysis/dune b/llvm/bindings/ocaml/analysis/dune
new file mode 100644
index 000000000000000..3b4bef267dfa6bd
--- /dev/null
+++ b/llvm/bindings/ocaml/analysis/dune
@@ -0,0 +1,9 @@
+(library
+ (name llvm_analysis)
+ (public_name llvm.analysis)
+ (libraries llvm)
+ (foreign_stubs
+ (language c)
+ (names analysis_ocaml)
+ (extra_deps ../llvm/llvm_ocaml.h))
+ (c_library_flags %{env:LLVMAnalysis_LIB=}))
diff --git a/llvm/bindings/ocaml/backends/dune.in b/llvm/bindings/ocaml/backends/dune.in
new file mode 100644
index 000000000000000..b4ac88f49291ce6
--- /dev/null
+++ b/llvm/bindings/ocaml/backends/dune.in
@@ -0,0 +1,10 @@
+(library
+ (name llvm_ at TARGET@)
+ (public_name llvm. at TARGET@)
+ (libraries llvm)
+ (foreign_stubs
+ (language c)
+ (names @TARGET at _ocaml)
+ (extra_deps ../llvm/llvm_ocaml.h)
+ (flags (:include ../c_flags.sexp)))
+ (c_library_flags (:include ../c_library_flags.sexp)))
diff --git a/llvm/bindings/ocaml/bitreader/dune b/llvm/bindings/ocaml/bitreader/dune
new file mode 100644
index 000000000000000..e3aabee53e4540f
--- /dev/null
+++ b/llvm/bindings/ocaml/bitreader/dune
@@ -0,0 +1,9 @@
+(library
+ (name llvm_bitreader)
+ (public_name llvm.bitreader)
+ (libraries llvm)
+ (foreign_stubs
+ (language c)
+ (names bitreader_ocaml)
+ (extra_deps ../llvm/llvm_ocaml.h))
+ (c_library_flags %{env:LLVMBitReader_LIB=}))
diff --git a/llvm/bindings/ocaml/bitwriter/dune b/llvm/bindings/ocaml/bitwriter/dune
new file mode 100644
index 000000000000000..53e37a6df31662a
--- /dev/null
+++ b/llvm/bindings/ocaml/bitwriter/dune
@@ -0,0 +1,9 @@
+(library
+ (name llvm_bitwriter)
+ (public_name llvm.bitwriter)
+ (libraries llvm unix)
+ (foreign_stubs
+ (language c)
+ (names bitwriter_ocaml)
+ (extra_deps ../llvm/llvm_ocaml.h))
+ (c_library_flags %{env:LLVMBitWriter_LIB=}))
diff --git a/llvm/bindings/ocaml/debuginfo/dune b/llvm/bindings/ocaml/debuginfo/dune
new file mode 100644
index 000000000000000..f06640c9601663f
--- /dev/null
+++ b/llvm/bindings/ocaml/debuginfo/dune
@@ -0,0 +1,9 @@
+(library
+ (name llvm_debuginfo)
+ (public_name llvm.debuginfo)
+ (libraries llvm)
+ (foreign_stubs
+ (language c)
+ (names debuginfo_ocaml)
+ (extra_deps ../llvm/llvm_ocaml.h))
+ (c_library_flags %{env:LLVMCore_LIB=}))
diff --git a/llvm/bindings/ocaml/dune-project b/llvm/bindings/ocaml/dune-project
new file mode 100644
index 000000000000000..37f995d6492934b
--- /dev/null
+++ b/llvm/bindings/ocaml/dune-project
@@ -0,0 +1 @@
+(lang dune 3.0)
diff --git a/llvm/bindings/ocaml/executionengine/dune b/llvm/bindings/ocaml/executionengine/dune
new file mode 100644
index 000000000000000..97f4e387d9746f8
--- /dev/null
+++ b/llvm/bindings/ocaml/executionengine/dune
@@ -0,0 +1,9 @@
+(library
+ (name llvm_executionengine)
+ (public_name llvm.executionengine)
+ (libraries llvm llvm.target ctypes.foreign)
+ (foreign_stubs
+ (language c)
+ (names executionengine_ocaml)
+ (extra_deps ../llvm/llvm_ocaml.h))
+ (c_library_flags %{env:LLVMExecutionEngine_LIB=}))
diff --git a/llvm/bindings/ocaml/irreader/dune b/llvm/bindings/ocaml/irreader/dune
new file mode 100644
index 000000000000000..d46c40f43ba975a
--- /dev/null
+++ b/llvm/bindings/ocaml/irreader/dune
@@ -0,0 +1,9 @@
+(library
+ (name llvm_irreader)
+ (public_name llvm.irreader)
+ (libraries llvm)
+ (foreign_stubs
+ (language c)
+ (names irreader_ocaml)
+ (extra_deps ../llvm/llvm_ocaml.h))
+ (c_library_flags %{env:LLVMIRReader_LIB=}))
diff --git a/llvm/bindings/ocaml/linker/dune b/llvm/bindings/ocaml/linker/dune
new file mode 100644
index 000000000000000..ee4cf32b4b8d50c
--- /dev/null
+++ b/llvm/bindings/ocaml/linker/dune
@@ -0,0 +1,9 @@
+(library
+ (name llvm_linker)
+ (public_name llvm.linker)
+ (libraries llvm)
+ (foreign_stubs
+ (language c)
+ (names linker_ocaml)
+ (extra_deps ../llvm/llvm_ocaml.h))
+ (c_library_flags %{env:LLVMLinker_LIB=}))
diff --git a/llvm/bindings/ocaml/llvm.opam b/llvm/bindings/ocaml/llvm.opam
new file mode 100644
index 000000000000000..e69de29bb2d1d64
diff --git a/llvm/bindings/ocaml/llvm/dune b/llvm/bindings/ocaml/llvm/dune
new file mode 100644
index 000000000000000..b202faae72df867
--- /dev/null
+++ b/llvm/bindings/ocaml/llvm/dune
@@ -0,0 +1,7 @@
+(library
+ (name llvm)
+ (public_name llvm)
+ (foreign_stubs
+ (language c)
+ (names llvm_ocaml))
+ (c_library_flags %{env:LLVMCore_LIB=}))
diff --git a/llvm/bindings/ocaml/setup.sh b/llvm/bindings/ocaml/setup.sh
new file mode 100755
index 000000000000000..a264dae44db764a
--- /dev/null
+++ b/llvm/bindings/ocaml/setup.sh
@@ -0,0 +1,88 @@
+#!/bin/sh
+
+set -e
+set -x
+
+if test ! "$(dirname $0)" -ef '.'; then
+ echo "The script must be executed from its current directory."
+ exit 1
+fi
+
+if test "$#" -ne 1; then
+ echo "Usage: $0 <llvm-config>"
+ exit 1
+fi
+
+llvm_config=$1
+default_mode=
+support_static_mode=false
+support_shared_mode=false
+
+llvm_config() {
+ "$llvm_config" $@
+}
+
+if llvm_config --link-static --libs; then
+ default_mode=static
+ support_static_mode=true
+fi
+
+if llvm_config --link-shared --libs; then
+ default_mode=shared
+ support_shared_mode=true
+fi
+
+if test -z "$default_mode"; then
+ echo "Something is wrong with the llvm-config command provided."
+ exit 1
+fi
+
+base_cflags=$(llvm_config --cflags)
+ldflags="$(llvm_config --ldflags) -lstdc++ -fPIC"
+llvm_targets=$(llvm_config --targets-built)
+
+append_context() {
+ context_name=$1
+ linking_mode=$2
+
+ core_libs=$(llvm_config $linking_mode --libs core support)
+ analysis_libs=$(llvm_config $linking_mode --libs analysis)
+ bitreader_libs=$(llvm_config $linking_mode --libs bitreader)
+ bitwriter_libs=$(llvm_config $linking_mode --libs bitwriter)
+ executionengine_libs=$(llvm_config $linking_mode --libs executionengine mcjit native)
+ irreader_libs=$(llvm_config $linking_mode --libs irreader)
+ transformutils_libs=$(llvm_config $linking_mode --libs transformutils)
+ passes_libs=$(llvm_config $linking_mode --libs passes)
+ target_libs=$(llvm_config $linking_mode --libs target)
+ linker_libs=$(llvm_config $linking_mode --libs linker)
+ all_backend_libs=$(llvm_config $linking_mode --libs $llvm_targets)
+
+ echo "(context (default
+ (name ${context_name})
+ (env
+ (_
+ (c_flags $base_cflags)
+ (env-vars
+ (LLVMCore_LIB \"$ldflags $core_libs\")
+ (LLVMAnalysis_LIB \"$ldflags $analysis_libs\")
+ (LLVMBitReader_LIB \"$ldflags $bitreader_libs\")
+ (LLVMBitWriter_LIB \"$ldflags $bitwriter_libs\")
+ (LLVMExecutionEngine_LIB \"$ldflags $executionengine_libs\")
+ (LLVMIRReader_LIB \"$ldflags $irreader_libs\")
+ (LLVMTransformUtils_LIB \"$ldflags $transformutils_libs\")
+ (LLVMPasses_LIB \"$ldflags $passes_libs\")
+ (LLVMTarget_LIB \"$ldflags $target_libs\")
+ (LLVMLinker_LIB \"$ldflags $linker_libs\")
+ (LLVMAll_backends_LIB \"$ldflags $all_backend_libs\"))))))
+" >> "dune-workspace"
+}
+
+echo "(lang dune 3.2)
+" > "dune-workspace"
+
+if $support_shared_mode; then
+ append_context shared --link-shared
+fi
+if $support_static_mode; then
+ append_context static --link-static
+fi
diff --git a/llvm/bindings/ocaml/target/dune b/llvm/bindings/ocaml/target/dune
new file mode 100644
index 000000000000000..13ebf944a66478b
--- /dev/null
+++ b/llvm/bindings/ocaml/target/dune
@@ -0,0 +1,9 @@
+(library
+ (name llvm_target)
+ (public_name llvm.target)
+ (libraries llvm)
+ (foreign_stubs
+ (language c)
+ (names target_ocaml)
+ (extra_deps ../llvm/llvm_ocaml.h))
+ (c_library_flags %{env:LLVMTarget_LIB=}))
diff --git a/llvm/bindings/ocaml/transforms/passbuilder/dune b/llvm/bindings/ocaml/transforms/passbuilder/dune
new file mode 100644
index 000000000000000..99bab0184c0347c
--- /dev/null
+++ b/llvm/bindings/ocaml/transforms/passbuilder/dune
@@ -0,0 +1,10 @@
+(library
+ (name llvm_passbuilder)
+ (public_name llvm.passbuilder)
+ (libraries llvm llvm.target)
+ (foreign_stubs
+ (language c)
+ (names passbuilder_ocaml)
+ (flags :standard -I../../target)
+ (extra_deps ../../llvm/llvm_ocaml.h ../../target/target_ocaml.h))
+ (c_library_flags %{env:LLVMPasses_LIB=}))
diff --git a/llvm/bindings/ocaml/transforms/utils/dune b/llvm/bindings/ocaml/transforms/utils/dune
new file mode 100644
index 000000000000000..188f3a06698f4df
--- /dev/null
+++ b/llvm/bindings/ocaml/transforms/utils/dune
@@ -0,0 +1,9 @@
+(library
+ (name llvm_transform_utils)
+ (public_name llvm.transform_utils)
+ (libraries llvm)
+ (foreign_stubs
+ (language c)
+ (names transform_utils_ocaml)
+ (extra_deps ../../llvm/llvm_ocaml.h))
+ (c_library_flags %{env:LLVMTransformUtils_LIB=}))
>From 15e6bdcd09cd8bd37c544863b906070a83ace61a Mon Sep 17 00:00:00 2001
From: Alan Hu <alanh at ccs.neu.edu>
Date: Sat, 23 Sep 2023 09:58:47 -0400
Subject: [PATCH 02/11] Fix build by making trampoline function static
---
llvm/bindings/ocaml/llvm/llvm_ocaml.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/bindings/ocaml/llvm/llvm_ocaml.c b/llvm/bindings/ocaml/llvm/llvm_ocaml.c
index f0e47a31af03d7b..b83db58d9d3de20 100644
--- a/llvm/bindings/ocaml/llvm/llvm_ocaml.c
+++ b/llvm/bindings/ocaml/llvm/llvm_ocaml.c
@@ -179,7 +179,7 @@ static value alloc_variant(int tag, value Value) {
/*===-- Context error handling --------------------------------------------===*/
-void llvm_diagnostic_handler_trampoline(LLVMDiagnosticInfoRef DI,
+static void llvm_diagnostic_handler_trampoline(LLVMDiagnosticInfoRef DI,
void *DiagnosticContext) {
caml_callback(*((value *)DiagnosticContext), to_val(DI));
}
>From 96f851cb851b43619f5dfb827b67acfa27cc68c2 Mon Sep 17 00:00:00 2001
From: Alan Hu <alanh at ccs.neu.edu>
Date: Sat, 23 Sep 2023 12:24:25 -0400
Subject: [PATCH 03/11] Use (:include c_library_flags.sexp) instead of env vars
---
llvm/bindings/ocaml/analysis/dune | 7 ++++-
llvm/bindings/ocaml/bitreader/dune | 7 ++++-
llvm/bindings/ocaml/debuginfo/dune | 7 ++++-
llvm/bindings/ocaml/discover.sh | 11 ++++++++
llvm/bindings/ocaml/executionengine/dune | 8 +++++-
llvm/bindings/ocaml/irreader/dune | 7 ++++-
llvm/bindings/ocaml/linker/dune | 7 ++++-
llvm/bindings/ocaml/llvm/dune | 7 ++++-
llvm/bindings/ocaml/setup.sh | 26 ++-----------------
llvm/bindings/ocaml/target/dune | 7 ++++-
.../ocaml/transforms/passbuilder/dune | 7 ++++-
llvm/bindings/ocaml/transforms/utils/dune | 7 ++++-
12 files changed, 74 insertions(+), 34 deletions(-)
create mode 100755 llvm/bindings/ocaml/discover.sh
diff --git a/llvm/bindings/ocaml/analysis/dune b/llvm/bindings/ocaml/analysis/dune
index 3b4bef267dfa6bd..946b60318381634 100644
--- a/llvm/bindings/ocaml/analysis/dune
+++ b/llvm/bindings/ocaml/analysis/dune
@@ -6,4 +6,9 @@
(language c)
(names analysis_ocaml)
(extra_deps ../llvm/llvm_ocaml.h))
- (c_library_flags %{env:LLVMAnalysis_LIB=}))
+ (c_library_flags (:include c_library_flags.sexp)))
+
+(rule
+ (targets c_library_flags.sexp)
+ (action
+ (run ../discover.sh %{env:LLVM_CONFIG=} %{env:LINK_MODE=} analysis)))
diff --git a/llvm/bindings/ocaml/bitreader/dune b/llvm/bindings/ocaml/bitreader/dune
index e3aabee53e4540f..4899320a08f961b 100644
--- a/llvm/bindings/ocaml/bitreader/dune
+++ b/llvm/bindings/ocaml/bitreader/dune
@@ -6,4 +6,9 @@
(language c)
(names bitreader_ocaml)
(extra_deps ../llvm/llvm_ocaml.h))
- (c_library_flags %{env:LLVMBitReader_LIB=}))
+ (c_library_flags (:include c_library_flags.sexp)))
+
+(rule
+ (targets c_library_flags.sexp)
+ (action
+ (run ../discover.sh %{env:LLVM_CONFIG=} %{env:LINK_MODE=} bitreader)))
diff --git a/llvm/bindings/ocaml/debuginfo/dune b/llvm/bindings/ocaml/debuginfo/dune
index f06640c9601663f..4405f87bd97b764 100644
--- a/llvm/bindings/ocaml/debuginfo/dune
+++ b/llvm/bindings/ocaml/debuginfo/dune
@@ -6,4 +6,9 @@
(language c)
(names debuginfo_ocaml)
(extra_deps ../llvm/llvm_ocaml.h))
- (c_library_flags %{env:LLVMCore_LIB=}))
+ (c_library_flags (:include c_library_flags.sexp)))
+
+(rule
+ (targets c_library_flags.sexp)
+ (action
+ (run ../discover.sh %{env:LLVM_CONFIG=} %{env:LINK_MODE=} core support)))
diff --git a/llvm/bindings/ocaml/discover.sh b/llvm/bindings/ocaml/discover.sh
new file mode 100755
index 000000000000000..91b78c42c875af9
--- /dev/null
+++ b/llvm/bindings/ocaml/discover.sh
@@ -0,0 +1,11 @@
+#!/bin/sh
+
+llvm_config=$1
+shift 1
+link_libs=$($llvm_config --libs $@)
+ld_flags=$($llvm_config --ldflags)
+echo "(" > c_library_flags.sexp
+echo $ld_flags >> c_library_flags.sexp
+echo " " >> c_library_flags.sexp
+echo $link_libs >> c_library_flags.sexp
+echo ")" >> c_library_flags.sexp
diff --git a/llvm/bindings/ocaml/executionengine/dune b/llvm/bindings/ocaml/executionengine/dune
index 97f4e387d9746f8..899c1a49a828417 100644
--- a/llvm/bindings/ocaml/executionengine/dune
+++ b/llvm/bindings/ocaml/executionengine/dune
@@ -6,4 +6,10 @@
(language c)
(names executionengine_ocaml)
(extra_deps ../llvm/llvm_ocaml.h))
- (c_library_flags %{env:LLVMExecutionEngine_LIB=}))
+ (c_library_flags (:include c_library_flags.sexp)))
+
+(rule
+ (targets c_library_flags.sexp)
+ (action
+ (run ../discover.sh %{env:LLVM_CONFIG=} %{env:LINK_MODE=}
+ executionengine mcjit native)))
diff --git a/llvm/bindings/ocaml/irreader/dune b/llvm/bindings/ocaml/irreader/dune
index d46c40f43ba975a..21a65d910d6a835 100644
--- a/llvm/bindings/ocaml/irreader/dune
+++ b/llvm/bindings/ocaml/irreader/dune
@@ -6,4 +6,9 @@
(language c)
(names irreader_ocaml)
(extra_deps ../llvm/llvm_ocaml.h))
- (c_library_flags %{env:LLVMIRReader_LIB=}))
+ (c_library_flags (:include c_library_flags.sexp)))
+
+(rule
+ (targets c_library_flags.sexp)
+ (action
+ (run ../discover.sh %{env:LLVM_CONFIG=} %{env:LINK_MODE=} irreader)))
diff --git a/llvm/bindings/ocaml/linker/dune b/llvm/bindings/ocaml/linker/dune
index ee4cf32b4b8d50c..4acee59728c15bd 100644
--- a/llvm/bindings/ocaml/linker/dune
+++ b/llvm/bindings/ocaml/linker/dune
@@ -6,4 +6,9 @@
(language c)
(names linker_ocaml)
(extra_deps ../llvm/llvm_ocaml.h))
- (c_library_flags %{env:LLVMLinker_LIB=}))
+ (c_library_flags (:include c_library_flags.sexp)))
+
+(rule
+ (targets c_library_flags.sexp)
+ (action
+ (run ../discover.sh %{env:LLVM_CONFIG=} %{env:LINK_MODE=} linker)))
diff --git a/llvm/bindings/ocaml/llvm/dune b/llvm/bindings/ocaml/llvm/dune
index b202faae72df867..62ef3f1fb12b28a 100644
--- a/llvm/bindings/ocaml/llvm/dune
+++ b/llvm/bindings/ocaml/llvm/dune
@@ -4,4 +4,9 @@
(foreign_stubs
(language c)
(names llvm_ocaml))
- (c_library_flags %{env:LLVMCore_LIB=}))
+ (c_library_flags (:include c_library_flags.sexp)))
+
+(rule
+ (targets c_library_flags.sexp)
+ (action
+ (run ../discover.sh %{env:LLVM_CONFIG=} %{env:LINK_MODE=} core support)))
diff --git a/llvm/bindings/ocaml/setup.sh b/llvm/bindings/ocaml/setup.sh
index a264dae44db764a..fc96b22f6e60d86 100755
--- a/llvm/bindings/ocaml/setup.sh
+++ b/llvm/bindings/ocaml/setup.sh
@@ -44,36 +44,14 @@ llvm_targets=$(llvm_config --targets-built)
append_context() {
context_name=$1
linking_mode=$2
-
- core_libs=$(llvm_config $linking_mode --libs core support)
- analysis_libs=$(llvm_config $linking_mode --libs analysis)
- bitreader_libs=$(llvm_config $linking_mode --libs bitreader)
- bitwriter_libs=$(llvm_config $linking_mode --libs bitwriter)
- executionengine_libs=$(llvm_config $linking_mode --libs executionengine mcjit native)
- irreader_libs=$(llvm_config $linking_mode --libs irreader)
- transformutils_libs=$(llvm_config $linking_mode --libs transformutils)
- passes_libs=$(llvm_config $linking_mode --libs passes)
- target_libs=$(llvm_config $linking_mode --libs target)
- linker_libs=$(llvm_config $linking_mode --libs linker)
- all_backend_libs=$(llvm_config $linking_mode --libs $llvm_targets)
-
echo "(context (default
(name ${context_name})
(env
(_
(c_flags $base_cflags)
(env-vars
- (LLVMCore_LIB \"$ldflags $core_libs\")
- (LLVMAnalysis_LIB \"$ldflags $analysis_libs\")
- (LLVMBitReader_LIB \"$ldflags $bitreader_libs\")
- (LLVMBitWriter_LIB \"$ldflags $bitwriter_libs\")
- (LLVMExecutionEngine_LIB \"$ldflags $executionengine_libs\")
- (LLVMIRReader_LIB \"$ldflags $irreader_libs\")
- (LLVMTransformUtils_LIB \"$ldflags $transformutils_libs\")
- (LLVMPasses_LIB \"$ldflags $passes_libs\")
- (LLVMTarget_LIB \"$ldflags $target_libs\")
- (LLVMLinker_LIB \"$ldflags $linker_libs\")
- (LLVMAll_backends_LIB \"$ldflags $all_backend_libs\"))))))
+ (LLVM_CONFIG $llvm_config)
+ (LINK_MODE $linking_mode))))))
" >> "dune-workspace"
}
diff --git a/llvm/bindings/ocaml/target/dune b/llvm/bindings/ocaml/target/dune
index 13ebf944a66478b..77e2159ce586a56 100644
--- a/llvm/bindings/ocaml/target/dune
+++ b/llvm/bindings/ocaml/target/dune
@@ -6,4 +6,9 @@
(language c)
(names target_ocaml)
(extra_deps ../llvm/llvm_ocaml.h))
- (c_library_flags %{env:LLVMTarget_LIB=}))
+ (c_library_flags (:include c_library_flags.sexp)))
+
+(rule
+ (targets c_library_flags.sexp)
+ (action
+ (run ../discover.sh %{env:LLVM_CONFIG=} %{env:LINK_MODE=} target)))
diff --git a/llvm/bindings/ocaml/transforms/passbuilder/dune b/llvm/bindings/ocaml/transforms/passbuilder/dune
index 99bab0184c0347c..3ad9785127f8478 100644
--- a/llvm/bindings/ocaml/transforms/passbuilder/dune
+++ b/llvm/bindings/ocaml/transforms/passbuilder/dune
@@ -7,4 +7,9 @@
(names passbuilder_ocaml)
(flags :standard -I../../target)
(extra_deps ../../llvm/llvm_ocaml.h ../../target/target_ocaml.h))
- (c_library_flags %{env:LLVMPasses_LIB=}))
+ (c_library_flags (:include c_library_flags.sexp)))
+
+(rule
+ (targets c_library_flags.sexp)
+ (action
+ (run ../../discover.sh %{env:LLVM_CONFIG=} %{env:LINK_MODE=} passes)))
diff --git a/llvm/bindings/ocaml/transforms/utils/dune b/llvm/bindings/ocaml/transforms/utils/dune
index 188f3a06698f4df..3b96bfb0b6d12c5 100644
--- a/llvm/bindings/ocaml/transforms/utils/dune
+++ b/llvm/bindings/ocaml/transforms/utils/dune
@@ -6,4 +6,9 @@
(language c)
(names transform_utils_ocaml)
(extra_deps ../../llvm/llvm_ocaml.h))
- (c_library_flags %{env:LLVMTransformUtils_LIB=}))
+ (c_library_flags (:include c_library_flags.sexp)))
+
+(rule
+ (targets c_library_flags.sexp)
+ (action
+ (run ../../discover.sh %{env:LLVM_CONFIG=} %{env:LINK_MODE=} transformutils)))
>From 49836ba42edcdf1996c490916d596871781dc961 Mon Sep 17 00:00:00 2001
From: Alan Hu <alanh at ccs.neu.edu>
Date: Sat, 23 Sep 2023 12:39:38 -0400
Subject: [PATCH 04/11] Simplify dune files
---
llvm/bindings/ocaml/analysis/dune | 2 +-
llvm/bindings/ocaml/bitreader/dune | 3 +--
llvm/bindings/ocaml/bitwriter/dune | 6 +++++-
llvm/bindings/ocaml/debuginfo/dune | 3 +--
llvm/bindings/ocaml/discover.sh | 6 ++----
llvm/bindings/ocaml/executionengine/dune | 4 +---
llvm/bindings/ocaml/irreader/dune | 3 +--
llvm/bindings/ocaml/linker/dune | 3 +--
llvm/bindings/ocaml/llvm/dune | 3 +--
llvm/bindings/ocaml/target/dune | 3 +--
llvm/bindings/ocaml/transforms/passbuilder/dune | 3 +--
llvm/bindings/ocaml/transforms/utils/dune | 3 +--
12 files changed, 17 insertions(+), 25 deletions(-)
diff --git a/llvm/bindings/ocaml/analysis/dune b/llvm/bindings/ocaml/analysis/dune
index 946b60318381634..6d04966e00b6d28 100644
--- a/llvm/bindings/ocaml/analysis/dune
+++ b/llvm/bindings/ocaml/analysis/dune
@@ -11,4 +11,4 @@
(rule
(targets c_library_flags.sexp)
(action
- (run ../discover.sh %{env:LLVM_CONFIG=} %{env:LINK_MODE=} analysis)))
+ (run ../discover.sh analysis)))
diff --git a/llvm/bindings/ocaml/bitreader/dune b/llvm/bindings/ocaml/bitreader/dune
index 4899320a08f961b..08a579c6dd59dfa 100644
--- a/llvm/bindings/ocaml/bitreader/dune
+++ b/llvm/bindings/ocaml/bitreader/dune
@@ -10,5 +10,4 @@
(rule
(targets c_library_flags.sexp)
- (action
- (run ../discover.sh %{env:LLVM_CONFIG=} %{env:LINK_MODE=} bitreader)))
+ (action (run ../discover.sh bitreader)))
diff --git a/llvm/bindings/ocaml/bitwriter/dune b/llvm/bindings/ocaml/bitwriter/dune
index 53e37a6df31662a..2c1673d3fc13fd1 100644
--- a/llvm/bindings/ocaml/bitwriter/dune
+++ b/llvm/bindings/ocaml/bitwriter/dune
@@ -6,4 +6,8 @@
(language c)
(names bitwriter_ocaml)
(extra_deps ../llvm/llvm_ocaml.h))
- (c_library_flags %{env:LLVMBitWriter_LIB=}))
+ (c_library_flags (:include c_library_flags.sexp)))
+
+(rule
+ (targets c_library_flags.sexp)
+ (action (run ../discover.sh bitwriter)))
diff --git a/llvm/bindings/ocaml/debuginfo/dune b/llvm/bindings/ocaml/debuginfo/dune
index 4405f87bd97b764..e23120c4720a9f9 100644
--- a/llvm/bindings/ocaml/debuginfo/dune
+++ b/llvm/bindings/ocaml/debuginfo/dune
@@ -10,5 +10,4 @@
(rule
(targets c_library_flags.sexp)
- (action
- (run ../discover.sh %{env:LLVM_CONFIG=} %{env:LINK_MODE=} core support)))
+ (action (run ../discover.sh core support)))
diff --git a/llvm/bindings/ocaml/discover.sh b/llvm/bindings/ocaml/discover.sh
index 91b78c42c875af9..980f7b241f23efe 100755
--- a/llvm/bindings/ocaml/discover.sh
+++ b/llvm/bindings/ocaml/discover.sh
@@ -1,9 +1,7 @@
#!/bin/sh
-llvm_config=$1
-shift 1
-link_libs=$($llvm_config --libs $@)
-ld_flags=$($llvm_config --ldflags)
+link_libs=$($LLVM_CONFIG $LINK_MODE --libs $@)
+ld_flags=$($LLVM_CONFIG --ldflags)
echo "(" > c_library_flags.sexp
echo $ld_flags >> c_library_flags.sexp
echo " " >> c_library_flags.sexp
diff --git a/llvm/bindings/ocaml/executionengine/dune b/llvm/bindings/ocaml/executionengine/dune
index 899c1a49a828417..8c1dc0b1fb3470f 100644
--- a/llvm/bindings/ocaml/executionengine/dune
+++ b/llvm/bindings/ocaml/executionengine/dune
@@ -10,6 +10,4 @@
(rule
(targets c_library_flags.sexp)
- (action
- (run ../discover.sh %{env:LLVM_CONFIG=} %{env:LINK_MODE=}
- executionengine mcjit native)))
+ (action (run ../discover.sh executionengine mcjit native)))
diff --git a/llvm/bindings/ocaml/irreader/dune b/llvm/bindings/ocaml/irreader/dune
index 21a65d910d6a835..b4b6e95708d4e0d 100644
--- a/llvm/bindings/ocaml/irreader/dune
+++ b/llvm/bindings/ocaml/irreader/dune
@@ -10,5 +10,4 @@
(rule
(targets c_library_flags.sexp)
- (action
- (run ../discover.sh %{env:LLVM_CONFIG=} %{env:LINK_MODE=} irreader)))
+ (action (run ../discover.sh irreader)))
diff --git a/llvm/bindings/ocaml/linker/dune b/llvm/bindings/ocaml/linker/dune
index 4acee59728c15bd..6ec291d787289ac 100644
--- a/llvm/bindings/ocaml/linker/dune
+++ b/llvm/bindings/ocaml/linker/dune
@@ -10,5 +10,4 @@
(rule
(targets c_library_flags.sexp)
- (action
- (run ../discover.sh %{env:LLVM_CONFIG=} %{env:LINK_MODE=} linker)))
+ (action (run ../discover.sh linker)))
diff --git a/llvm/bindings/ocaml/llvm/dune b/llvm/bindings/ocaml/llvm/dune
index 62ef3f1fb12b28a..8e0ab86f0c758fd 100644
--- a/llvm/bindings/ocaml/llvm/dune
+++ b/llvm/bindings/ocaml/llvm/dune
@@ -8,5 +8,4 @@
(rule
(targets c_library_flags.sexp)
- (action
- (run ../discover.sh %{env:LLVM_CONFIG=} %{env:LINK_MODE=} core support)))
+ (action (run ../discover.sh core support)))
diff --git a/llvm/bindings/ocaml/target/dune b/llvm/bindings/ocaml/target/dune
index 77e2159ce586a56..ac6741e5b6c915b 100644
--- a/llvm/bindings/ocaml/target/dune
+++ b/llvm/bindings/ocaml/target/dune
@@ -10,5 +10,4 @@
(rule
(targets c_library_flags.sexp)
- (action
- (run ../discover.sh %{env:LLVM_CONFIG=} %{env:LINK_MODE=} target)))
+ (action (run ../discover.sh target)))
diff --git a/llvm/bindings/ocaml/transforms/passbuilder/dune b/llvm/bindings/ocaml/transforms/passbuilder/dune
index 3ad9785127f8478..b04fc76f5178f85 100644
--- a/llvm/bindings/ocaml/transforms/passbuilder/dune
+++ b/llvm/bindings/ocaml/transforms/passbuilder/dune
@@ -11,5 +11,4 @@
(rule
(targets c_library_flags.sexp)
- (action
- (run ../../discover.sh %{env:LLVM_CONFIG=} %{env:LINK_MODE=} passes)))
+ (action (run ../../discover.sh passes)))
diff --git a/llvm/bindings/ocaml/transforms/utils/dune b/llvm/bindings/ocaml/transforms/utils/dune
index 3b96bfb0b6d12c5..b946a6656287bcc 100644
--- a/llvm/bindings/ocaml/transforms/utils/dune
+++ b/llvm/bindings/ocaml/transforms/utils/dune
@@ -10,5 +10,4 @@
(rule
(targets c_library_flags.sexp)
- (action
- (run ../../discover.sh %{env:LLVM_CONFIG=} %{env:LINK_MODE=} transformutils)))
+ (action (run ../../discover.sh transformutils)))
>From f5564a3a25c61b57c95bb7b0b679e6ffcedd658e Mon Sep 17 00:00:00 2001
From: Alan Hu <alanh at ccs.neu.edu>
Date: Sat, 23 Sep 2023 17:20:28 -0400
Subject: [PATCH 05/11] Invoke Dune from CMake
---
llvm/bindings/ocaml/CMakeLists.txt | 58 +++++++++++++++++++++++-------
llvm/docs/CMakeLists.txt | 2 ++
2 files changed, 48 insertions(+), 12 deletions(-)
diff --git a/llvm/bindings/ocaml/CMakeLists.txt b/llvm/bindings/ocaml/CMakeLists.txt
index 7fe960b67f272b6..d5d4974e1e91179 100644
--- a/llvm/bindings/ocaml/CMakeLists.txt
+++ b/llvm/bindings/ocaml/CMakeLists.txt
@@ -1,12 +1,46 @@
-add_subdirectory(llvm)
-add_subdirectory(all_backends)
-add_subdirectory(analysis)
-add_subdirectory(backends)
-add_subdirectory(bitreader)
-add_subdirectory(bitwriter)
-add_subdirectory(debuginfo)
-add_subdirectory(irreader)
-add_subdirectory(linker)
-add_subdirectory(target)
-add_subdirectory(transforms)
-add_subdirectory(executionengine)
+foreach( source dune-project setup.sh discover.sh llvm.opam )
+ add_custom_command(
+ OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${source}"
+ COMMAND
+ "${CMAKE_COMMAND}" "-E" "copy"
+ "${CMAKE_CURRENT_SOURCE_DIR}/${source}" "${CMAKE_CURRENT_BINARY_DIR}"
+ DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/${source}"
+ COMMENT "Copying ${source} to build area")
+endforeach()
+foreach ( dir
+ all_backends analysis backends bitreader bitwriter debuginfo executionengine
+ irreader linker llvm target transforms )
+ file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/${dir} DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
+endforeach()
+
+add_custom_target(ocaml_copied_files
+ DEPENDS dune-project setup.sh discover.sh llvm.opam)
+
+add_custom_command(
+ OUTPUT dune-workspace
+ COMMAND
+ ./setup.sh ${CMAKE_BINARY_DIR}/bin/llvm-config
+ DEPENDS
+ ${CMAKE_CURRENT_BINARY_DIR}/setup.sh
+ ${CMAKE_BINARY_DIR}/bin/llvm-config)
+
+add_custom_target(dune_workspace_target DEPENDS dune-workspace)
+
+add_custom_target(dune_build_all
+ COMMAND dune build @all --release
+ DEPENDS ocaml_copied_files dune_workspace_target)
+
+add_dependencies(ocaml_all dune_build_all)
+
+#add_subdirectory(llvm)
+#add_subdirectory(all_backends)
+#add_subdirectory(analysis)
+#add_subdirectory(backends)
+#add_subdirectory(bitreader)
+#add_subdirectory(bitwriter)
+#add_subdirectory(debuginfo)
+#add_subdirectory(irreader)
+#add_subdirectory(linker)
+#add_subdirectory(target)
+#add_subdirectory(transforms)
+#add_subdirectory(executionengine)
diff --git a/llvm/docs/CMakeLists.txt b/llvm/docs/CMakeLists.txt
index 5e420a2696329eb..d13b3bf26a1da06 100644
--- a/llvm/docs/CMakeLists.txt
+++ b/llvm/docs/CMakeLists.txt
@@ -119,6 +119,7 @@ if (LLVM_ENABLE_SPHINX)
endif()
endif()
+if(FALSE)
list(FIND LLVM_BINDINGS_LIST ocaml uses_ocaml)
if( NOT uses_ocaml LESS 0 AND LLVM_ENABLE_OCAMLDOC )
set(doc_targets
@@ -162,3 +163,4 @@ if( NOT uses_ocaml LESS 0 AND LLVM_ENABLE_OCAMLDOC )
DESTINATION "${LLVM_INSTALL_OCAMLDOC_HTML_DIR}")
endif()
endif()
+endif()
>From aa3694c3ccf121174c1cbe392d3ba2d209a0605d Mon Sep 17 00:00:00 2001
From: Alan Hu <alanh at ccs.neu.edu>
Date: Sat, 23 Sep 2023 18:23:36 -0400
Subject: [PATCH 06/11] Add copying directories as build rule
---
llvm/bindings/ocaml/CMakeLists.txt | 32 ++++++++++---------
.../ocaml/all_backends/CMakeLists.txt | 5 ---
llvm/bindings/ocaml/analysis/CMakeLists.txt | 6 ----
llvm/bindings/ocaml/backends/CMakeLists.txt | 27 ----------------
llvm/bindings/ocaml/bitreader/CMakeLists.txt | 6 ----
llvm/bindings/ocaml/bitwriter/CMakeLists.txt | 7 ----
llvm/bindings/ocaml/debuginfo/CMakeLists.txt | 6 ----
.../ocaml/executionengine/CMakeLists.txt | 7 ----
llvm/bindings/ocaml/irreader/CMakeLists.txt | 6 ----
llvm/bindings/ocaml/linker/CMakeLists.txt | 6 ----
llvm/bindings/ocaml/llvm/CMakeLists.txt | 12 -------
llvm/bindings/ocaml/target/CMakeLists.txt | 6 ----
.../transforms/passbuilder/CMakeLists.txt | 6 ----
.../ocaml/transforms/utils/CMakeLists.txt | 6 ----
14 files changed, 17 insertions(+), 121 deletions(-)
delete mode 100644 llvm/bindings/ocaml/all_backends/CMakeLists.txt
delete mode 100644 llvm/bindings/ocaml/analysis/CMakeLists.txt
delete mode 100644 llvm/bindings/ocaml/backends/CMakeLists.txt
delete mode 100644 llvm/bindings/ocaml/bitreader/CMakeLists.txt
delete mode 100644 llvm/bindings/ocaml/bitwriter/CMakeLists.txt
delete mode 100644 llvm/bindings/ocaml/debuginfo/CMakeLists.txt
delete mode 100644 llvm/bindings/ocaml/executionengine/CMakeLists.txt
delete mode 100644 llvm/bindings/ocaml/irreader/CMakeLists.txt
delete mode 100644 llvm/bindings/ocaml/linker/CMakeLists.txt
delete mode 100644 llvm/bindings/ocaml/llvm/CMakeLists.txt
delete mode 100644 llvm/bindings/ocaml/target/CMakeLists.txt
delete mode 100644 llvm/bindings/ocaml/transforms/passbuilder/CMakeLists.txt
delete mode 100644 llvm/bindings/ocaml/transforms/utils/CMakeLists.txt
diff --git a/llvm/bindings/ocaml/CMakeLists.txt b/llvm/bindings/ocaml/CMakeLists.txt
index d5d4974e1e91179..ac6d7d4d35f7c09 100644
--- a/llvm/bindings/ocaml/CMakeLists.txt
+++ b/llvm/bindings/ocaml/CMakeLists.txt
@@ -10,11 +10,26 @@ endforeach()
foreach ( dir
all_backends analysis backends bitreader bitwriter debuginfo executionengine
irreader linker llvm target transforms )
- file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/${dir} DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
+ add_custom_target(ocaml_copy_${dir} ALL
+ COMMAND ${CMAKE_COMMAND} -E copy_directory
+ ${CMAKE_CURRENT_SOURCE_DIR}/${dir} ${CMAKE_CURRENT_BINARY_DIR}/${dir}
+ DEPENDS ${MY_TARGET})
endforeach()
add_custom_target(ocaml_copied_files
- DEPENDS dune-project setup.sh discover.sh llvm.opam)
+ DEPENDS dune-project setup.sh discover.sh llvm.opam
+ ocaml_copy_all_backends
+ ocaml_copy_analysis
+ ocaml_copy_backends
+ ocaml_copy_bitreader
+ ocaml_copy_bitwriter
+ ocaml_copy_debuginfo
+ ocaml_copy_executionengine
+ ocaml_copy_irreader
+ ocaml_copy_linker
+ ocaml_copy_llvm
+ ocaml_copy_target
+ ocaml_copy_transforms)
add_custom_command(
OUTPUT dune-workspace
@@ -31,16 +46,3 @@ add_custom_target(dune_build_all
DEPENDS ocaml_copied_files dune_workspace_target)
add_dependencies(ocaml_all dune_build_all)
-
-#add_subdirectory(llvm)
-#add_subdirectory(all_backends)
-#add_subdirectory(analysis)
-#add_subdirectory(backends)
-#add_subdirectory(bitreader)
-#add_subdirectory(bitwriter)
-#add_subdirectory(debuginfo)
-#add_subdirectory(irreader)
-#add_subdirectory(linker)
-#add_subdirectory(target)
-#add_subdirectory(transforms)
-#add_subdirectory(executionengine)
diff --git a/llvm/bindings/ocaml/all_backends/CMakeLists.txt b/llvm/bindings/ocaml/all_backends/CMakeLists.txt
deleted file mode 100644
index 716a49cc3281b34..000000000000000
--- a/llvm/bindings/ocaml/all_backends/CMakeLists.txt
+++ /dev/null
@@ -1,5 +0,0 @@
-add_ocaml_library(llvm_all_backends
- OCAML llvm_all_backends
- OCAMLDEP llvm
- C all_backends_ocaml
- LLVM ${LLVM_TARGETS_TO_BUILD})
diff --git a/llvm/bindings/ocaml/analysis/CMakeLists.txt b/llvm/bindings/ocaml/analysis/CMakeLists.txt
deleted file mode 100644
index 622ecdfca615eb3..000000000000000
--- a/llvm/bindings/ocaml/analysis/CMakeLists.txt
+++ /dev/null
@@ -1,6 +0,0 @@
-add_ocaml_library(llvm_analysis
- OCAML llvm_analysis
- OCAMLDEP llvm
- C analysis_ocaml
- CFLAGS "-I${CMAKE_CURRENT_SOURCE_DIR}/../llvm"
- LLVM Analysis)
diff --git a/llvm/bindings/ocaml/backends/CMakeLists.txt b/llvm/bindings/ocaml/backends/CMakeLists.txt
deleted file mode 100644
index 18d62a857381491..000000000000000
--- a/llvm/bindings/ocaml/backends/CMakeLists.txt
+++ /dev/null
@@ -1,27 +0,0 @@
-foreach(TARGET ${LLVM_TARGETS_TO_BUILD})
- set(OCAML_LLVM_TARGET ${TARGET})
-
- foreach( ext ml mli )
- configure_file(
- "${CMAKE_CURRENT_SOURCE_DIR}/llvm_backend.${ext}.in"
- "${CMAKE_CURRENT_BINARY_DIR}/llvm_${TARGET}.${ext}")
- endforeach()
-
- configure_file(
- "${CMAKE_CURRENT_SOURCE_DIR}/backend_ocaml.c"
- "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}_ocaml.c")
-
- add_ocaml_library(llvm_${TARGET}
- OCAML llvm_${TARGET}
- C ${TARGET}_ocaml
- CFLAGS -DTARGET=${TARGET}
- LLVM ${TARGET}
- NOCOPY)
-
- configure_file(
- "${CMAKE_CURRENT_SOURCE_DIR}/META.llvm_backend.in"
- "${LLVM_LIBRARY_DIR}/ocaml/META.llvm_${TARGET}")
-
- install(FILES "${LLVM_LIBRARY_DIR}/ocaml/META.llvm_${TARGET}"
- DESTINATION "${LLVM_OCAML_INSTALL_PATH}")
-endforeach()
diff --git a/llvm/bindings/ocaml/bitreader/CMakeLists.txt b/llvm/bindings/ocaml/bitreader/CMakeLists.txt
deleted file mode 100644
index 90e444b9657efde..000000000000000
--- a/llvm/bindings/ocaml/bitreader/CMakeLists.txt
+++ /dev/null
@@ -1,6 +0,0 @@
-add_ocaml_library(llvm_bitreader
- OCAML llvm_bitreader
- OCAMLDEP llvm
- C bitreader_ocaml
- CFLAGS "-I${CMAKE_CURRENT_SOURCE_DIR}/../llvm"
- LLVM BitReader)
diff --git a/llvm/bindings/ocaml/bitwriter/CMakeLists.txt b/llvm/bindings/ocaml/bitwriter/CMakeLists.txt
deleted file mode 100644
index ec7d0a5127b153b..000000000000000
--- a/llvm/bindings/ocaml/bitwriter/CMakeLists.txt
+++ /dev/null
@@ -1,7 +0,0 @@
-add_ocaml_library(llvm_bitwriter
- OCAML llvm_bitwriter
- OCAMLDEP llvm
- C bitwriter_ocaml
- CFLAGS "-I${CMAKE_CURRENT_SOURCE_DIR}/../llvm"
- PKG unix
- LLVM BitWriter)
diff --git a/llvm/bindings/ocaml/debuginfo/CMakeLists.txt b/llvm/bindings/ocaml/debuginfo/CMakeLists.txt
deleted file mode 100644
index 07f4956cccf2f3d..000000000000000
--- a/llvm/bindings/ocaml/debuginfo/CMakeLists.txt
+++ /dev/null
@@ -1,6 +0,0 @@
-add_ocaml_library(llvm_debuginfo
- OCAML llvm_debuginfo
- OCAMLDEP llvm
- C debuginfo_ocaml
- CFLAGS "-I${CMAKE_CURRENT_SOURCE_DIR}/../llvm"
- LLVM Core)
diff --git a/llvm/bindings/ocaml/executionengine/CMakeLists.txt b/llvm/bindings/ocaml/executionengine/CMakeLists.txt
deleted file mode 100644
index c35d80ed6cd581b..000000000000000
--- a/llvm/bindings/ocaml/executionengine/CMakeLists.txt
+++ /dev/null
@@ -1,7 +0,0 @@
-add_ocaml_library(llvm_executionengine
- OCAML llvm_executionengine
- OCAMLDEP llvm llvm_target
- C executionengine_ocaml
- CFLAGS "-I${CMAKE_CURRENT_SOURCE_DIR}/../llvm"
- LLVM ExecutionEngine MCJIT native
- PKG ctypes)
diff --git a/llvm/bindings/ocaml/irreader/CMakeLists.txt b/llvm/bindings/ocaml/irreader/CMakeLists.txt
deleted file mode 100644
index d545723a1783c14..000000000000000
--- a/llvm/bindings/ocaml/irreader/CMakeLists.txt
+++ /dev/null
@@ -1,6 +0,0 @@
-add_ocaml_library(llvm_irreader
- OCAML llvm_irreader
- OCAMLDEP llvm
- C irreader_ocaml
- CFLAGS "-I${CMAKE_CURRENT_SOURCE_DIR}/../llvm"
- LLVM IRReader)
diff --git a/llvm/bindings/ocaml/linker/CMakeLists.txt b/llvm/bindings/ocaml/linker/CMakeLists.txt
deleted file mode 100644
index add5a59297e0d44..000000000000000
--- a/llvm/bindings/ocaml/linker/CMakeLists.txt
+++ /dev/null
@@ -1,6 +0,0 @@
-add_ocaml_library(llvm_linker
- OCAML llvm_linker
- OCAMLDEP llvm
- C linker_ocaml
- CFLAGS "-I${CMAKE_CURRENT_SOURCE_DIR}/../llvm"
- LLVM Linker)
diff --git a/llvm/bindings/ocaml/llvm/CMakeLists.txt b/llvm/bindings/ocaml/llvm/CMakeLists.txt
deleted file mode 100644
index 5e6f74ec9c5945c..000000000000000
--- a/llvm/bindings/ocaml/llvm/CMakeLists.txt
+++ /dev/null
@@ -1,12 +0,0 @@
-add_ocaml_library(llvm
- OCAML llvm
- C llvm_ocaml
- CFLAGS "-I${CMAKE_CURRENT_SOURCE_DIR}/"
- LLVM Core Support)
-
-configure_file(
- "${CMAKE_CURRENT_SOURCE_DIR}/META.llvm.in"
- "${LLVM_LIBRARY_DIR}/ocaml/META.llvm")
-
-install(FILES "${LLVM_LIBRARY_DIR}/ocaml/META.llvm"
- DESTINATION "${LLVM_OCAML_INSTALL_PATH}")
diff --git a/llvm/bindings/ocaml/target/CMakeLists.txt b/llvm/bindings/ocaml/target/CMakeLists.txt
deleted file mode 100644
index 7fe896c43616c07..000000000000000
--- a/llvm/bindings/ocaml/target/CMakeLists.txt
+++ /dev/null
@@ -1,6 +0,0 @@
-add_ocaml_library(llvm_target
- OCAML llvm_target
- OCAMLDEP llvm
- C target_ocaml
- CFLAGS "-I${CMAKE_CURRENT_SOURCE_DIR}/ -I${CMAKE_CURRENT_SOURCE_DIR}/../llvm"
- LLVM Target)
diff --git a/llvm/bindings/ocaml/transforms/passbuilder/CMakeLists.txt b/llvm/bindings/ocaml/transforms/passbuilder/CMakeLists.txt
deleted file mode 100644
index 0004afd914a46ed..000000000000000
--- a/llvm/bindings/ocaml/transforms/passbuilder/CMakeLists.txt
+++ /dev/null
@@ -1,6 +0,0 @@
-add_ocaml_library(llvm_passbuilder
- OCAML llvm_passbuilder
- OCAMLDEP llvm llvm_target
- C passbuilder_ocaml
- CFLAGS "-I${CMAKE_CURRENT_SOURCE_DIR}/../../llvm -I${CMAKE_CURRENT_SOURCE_DIR}/../../target"
- LLVM Passes)
diff --git a/llvm/bindings/ocaml/transforms/utils/CMakeLists.txt b/llvm/bindings/ocaml/transforms/utils/CMakeLists.txt
deleted file mode 100644
index eecc1f80aa7a70e..000000000000000
--- a/llvm/bindings/ocaml/transforms/utils/CMakeLists.txt
+++ /dev/null
@@ -1,6 +0,0 @@
-add_ocaml_library(llvm_transform_utils
- OCAML llvm_transform_utils
- OCAMLDEP llvm
- C transform_utils_ocaml
- CFLAGS "-I${CMAKE_CURRENT_SOURCE_DIR}/../../llvm"
- LLVM TransformUtils)
>From c897d5778afba123b4f5e1a7846bf67275995607 Mon Sep 17 00:00:00 2001
From: Alan Hu <alanh at ccs.neu.edu>
Date: Sat, 23 Sep 2023 23:27:03 -0400
Subject: [PATCH 07/11] Commit what I have so far
---
llvm/bindings/ocaml/CMakeLists.txt | 53 +++++++++++-------------------
llvm/bindings/ocaml/discover.sh | 4 +--
llvm/bindings/ocaml/setup.sh | 52 +++++++++++------------------
llvm/test/lit.cfg.py | 3 +-
4 files changed, 43 insertions(+), 69 deletions(-)
diff --git a/llvm/bindings/ocaml/CMakeLists.txt b/llvm/bindings/ocaml/CMakeLists.txt
index ac6d7d4d35f7c09..e25a480b4d7d609 100644
--- a/llvm/bindings/ocaml/CMakeLists.txt
+++ b/llvm/bindings/ocaml/CMakeLists.txt
@@ -1,48 +1,33 @@
-foreach( source dune-project setup.sh discover.sh llvm.opam )
- add_custom_command(
- OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${source}"
- COMMAND
- "${CMAKE_COMMAND}" "-E" "copy"
- "${CMAKE_CURRENT_SOURCE_DIR}/${source}" "${CMAKE_CURRENT_BINARY_DIR}"
- DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/${source}"
+add_custom_target(ocaml_copy_files)
+
+foreach( file dune-project setup.sh discover.sh llvm.opam )
+ add_custom_target(ocaml_copy_${file} ALL
+ COMMAND "${CMAKE_COMMAND}" "-E" "copy"
+ "${CMAKE_CURRENT_SOURCE_DIR}/${file}" "${CMAKE_CURRENT_BINARY_DIR}"
+ DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/${file}"
COMMENT "Copying ${source} to build area")
+ add_dependencies(ocaml_copy_files ocaml_copy_${file})
endforeach()
+
foreach ( dir
all_backends analysis backends bitreader bitwriter debuginfo executionengine
irreader linker llvm target transforms )
add_custom_target(ocaml_copy_${dir} ALL
- COMMAND ${CMAKE_COMMAND} -E copy_directory
- ${CMAKE_CURRENT_SOURCE_DIR}/${dir} ${CMAKE_CURRENT_BINARY_DIR}/${dir}
+ COMMAND "${CMAKE_COMMAND}" "-E" "copy_directory"
+ "${CMAKE_CURRENT_SOURCE_DIR}/${dir}" "${CMAKE_CURRENT_BINARY_DIR}/${dir}"
DEPENDS ${MY_TARGET})
+ add_dependencies(ocaml_copy_files ocaml_copy_${dir})
endforeach()
-add_custom_target(ocaml_copied_files
- DEPENDS dune-project setup.sh discover.sh llvm.opam
- ocaml_copy_all_backends
- ocaml_copy_analysis
- ocaml_copy_backends
- ocaml_copy_bitreader
- ocaml_copy_bitwriter
- ocaml_copy_debuginfo
- ocaml_copy_executionengine
- ocaml_copy_irreader
- ocaml_copy_linker
- ocaml_copy_llvm
- ocaml_copy_target
- ocaml_copy_transforms)
-
-add_custom_command(
- OUTPUT dune-workspace
- COMMAND
- ./setup.sh ${CMAKE_BINARY_DIR}/bin/llvm-config
+add_custom_target(dune_workspace ALL
+ COMMAND "./setup.sh" "${CMAKE_BINARY_DIR}/bin/llvm-config" "static"
DEPENDS
- ${CMAKE_CURRENT_BINARY_DIR}/setup.sh
- ${CMAKE_BINARY_DIR}/bin/llvm-config)
-
-add_custom_target(dune_workspace_target DEPENDS dune-workspace)
+ ocaml_copy_files
+ "${CMAKE_BINARY_DIR}/bin/llvm-config")
add_custom_target(dune_build_all
- COMMAND dune build @all --release
- DEPENDS ocaml_copied_files dune_workspace_target)
+ COMMAND "dune" "build" "--release"
+ COMMAND "dune" "install" "--libdir" "${CMAKE_BINARY_DIR}/lib/ocaml"
+ DEPENDS ocaml_copy_files dune_workspace)
add_dependencies(ocaml_all dune_build_all)
diff --git a/llvm/bindings/ocaml/discover.sh b/llvm/bindings/ocaml/discover.sh
index 980f7b241f23efe..15fae1b092e9faf 100755
--- a/llvm/bindings/ocaml/discover.sh
+++ b/llvm/bindings/ocaml/discover.sh
@@ -1,7 +1,7 @@
#!/bin/sh
-link_libs=$($LLVM_CONFIG $LINK_MODE --libs $@)
-ld_flags=$($LLVM_CONFIG --ldflags)
+link_libs=$($LLVM_CONFIG --system-libs $LINK_MODE --libs $@)
+ld_flags="$($LLVM_CONFIG --ldflags) -lstdc++"
echo "(" > c_library_flags.sexp
echo $ld_flags >> c_library_flags.sexp
echo " " >> c_library_flags.sexp
diff --git a/llvm/bindings/ocaml/setup.sh b/llvm/bindings/ocaml/setup.sh
index fc96b22f6e60d86..642237ee60021f5 100755
--- a/llvm/bindings/ocaml/setup.sh
+++ b/llvm/bindings/ocaml/setup.sh
@@ -8,44 +8,22 @@ if test ! "$(dirname $0)" -ef '.'; then
exit 1
fi
-if test "$#" -ne 1; then
- echo "Usage: $0 <llvm-config>"
+if test "$#" -ne 2; then
+ echo "Usage: $0 <llvm-config> <linking mode>"
exit 1
fi
llvm_config=$1
-default_mode=
-support_static_mode=false
-support_shared_mode=false
+mode=$2
-llvm_config() {
- "$llvm_config" $@
-}
-
-if llvm_config --link-static --libs; then
- default_mode=static
- support_static_mode=true
-fi
-
-if llvm_config --link-shared --libs; then
- default_mode=shared
- support_shared_mode=true
-fi
-
-if test -z "$default_mode"; then
- echo "Something is wrong with the llvm-config command provided."
- exit 1
-fi
-
-base_cflags=$(llvm_config --cflags)
-ldflags="$(llvm_config --ldflags) -lstdc++ -fPIC"
-llvm_targets=$(llvm_config --targets-built)
+base_cflags=$($llvm_config --cflags)
+ldflags="$($llvm_config --ldflags) -lstdc++ -fPIC"
+llvm_targets=$($llvm_config --targets-built)
append_context() {
context_name=$1
linking_mode=$2
echo "(context (default
- (name ${context_name})
(env
(_
(c_flags $base_cflags)
@@ -58,9 +36,19 @@ append_context() {
echo "(lang dune 3.2)
" > "dune-workspace"
-if $support_shared_mode; then
- append_context shared --link-shared
-fi
-if $support_static_mode; then
+if [ $mode = "static" ]; then
+ $llvm_config --link-static --libs
+ if [ $? -ne 0 ]; then
+ echo "Static mode is not supported."
+ exit 1
+ fi
append_context static --link-static
fi
+if [ $mode = "shared" ]; then
+ $llvm_config --link-shared --libs
+ if [ $? -ne 0 ]; then
+ echo "Shared mode is not supported."
+ exit 1
+ fi
+ append_context shared --link-shared
+fi
diff --git a/llvm/test/lit.cfg.py b/llvm/test/lit.cfg.py
index 7a1e30b49b0c3d1..9091ee7ed7aca58 100644
--- a/llvm/test/lit.cfg.py
+++ b/llvm/test/lit.cfg.py
@@ -45,13 +45,14 @@
# Set up OCAMLPATH to include newly built OCaml libraries.
top_ocaml_lib = os.path.join(config.llvm_lib_dir, "ocaml")
llvm_ocaml_lib = os.path.join(top_ocaml_lib, "llvm")
+stublibs_ocaml_lib = os.path.join(top_ocaml_lib, "stublibs")
llvm_config.with_system_environment("OCAMLPATH")
llvm_config.with_environment("OCAMLPATH", top_ocaml_lib, append_path=True)
llvm_config.with_environment("OCAMLPATH", llvm_ocaml_lib, append_path=True)
llvm_config.with_system_environment("CAML_LD_LIBRARY_PATH")
-llvm_config.with_environment("CAML_LD_LIBRARY_PATH", llvm_ocaml_lib, append_path=True)
+llvm_config.with_environment("CAML_LD_LIBRARY_PATH", stublibs_ocaml_lib, append_path=True)
# Set up OCAMLRUNPARAM to enable backtraces in OCaml tests.
llvm_config.with_environment("OCAMLRUNPARAM", "b")
>From 3631614752bca459b173da4e02817831a2b10555 Mon Sep 17 00:00:00 2001
From: Alan Hu <alanh at ccs.neu.edu>
Date: Sun, 24 Sep 2023 12:57:32 -0400
Subject: [PATCH 08/11] Add -custom flag to ocamlc to fix bytecode test errors
---
llvm/bindings/ocaml/setup.sh | 1 +
1 file changed, 1 insertion(+)
diff --git a/llvm/bindings/ocaml/setup.sh b/llvm/bindings/ocaml/setup.sh
index 642237ee60021f5..b7b5a122ec9d2c8 100755
--- a/llvm/bindings/ocaml/setup.sh
+++ b/llvm/bindings/ocaml/setup.sh
@@ -26,6 +26,7 @@ append_context() {
echo "(context (default
(env
(_
+ (ocamlc_flags -custom)
(c_flags $base_cflags)
(env-vars
(LLVM_CONFIG $llvm_config)
>From 926846dc42e9f27f172eabb2cef2ff23d5297e62 Mon Sep 17 00:00:00 2001
From: Alan Hu <alanh at ccs.neu.edu>
Date: Sun, 24 Sep 2023 16:12:36 -0400
Subject: [PATCH 09/11] Successfully pass OCaml tests using Dune to build
bindings
---
llvm/bindings/ocaml/all_backends/{dune => dune.in} | 6 +++++-
llvm/bindings/ocaml/backends/dune.in | 10 +++++++---
llvm/bindings/ocaml/setup.sh | 11 +++++++++++
3 files changed, 23 insertions(+), 4 deletions(-)
rename llvm/bindings/ocaml/all_backends/{dune => dune.in} (55%)
diff --git a/llvm/bindings/ocaml/all_backends/dune b/llvm/bindings/ocaml/all_backends/dune.in
similarity index 55%
rename from llvm/bindings/ocaml/all_backends/dune
rename to llvm/bindings/ocaml/all_backends/dune.in
index 0cf0fa4a91d644c..0163da1d175cae2 100644
--- a/llvm/bindings/ocaml/all_backends/dune
+++ b/llvm/bindings/ocaml/all_backends/dune.in
@@ -6,4 +6,8 @@
(language c)
(names all_backends_ocaml)
(extra_deps ../llvm/llvm_ocaml.h))
- (c_library_flags %{env:LLVM_All_backends_LIB=}))
+ (c_library_flags (:include c_library_flags.sexp)))
+
+(rule
+ (targets c_library_flags.sexp)
+ (action (run ../discover.sh @LLVM_TARGETS_TO_BUILD@)))
diff --git a/llvm/bindings/ocaml/backends/dune.in b/llvm/bindings/ocaml/backends/dune.in
index b4ac88f49291ce6..da0f649ba1b0bde 100644
--- a/llvm/bindings/ocaml/backends/dune.in
+++ b/llvm/bindings/ocaml/backends/dune.in
@@ -5,6 +5,10 @@
(foreign_stubs
(language c)
(names @TARGET at _ocaml)
- (extra_deps ../llvm/llvm_ocaml.h)
- (flags (:include ../c_flags.sexp)))
- (c_library_flags (:include ../c_library_flags.sexp)))
+ (extra_deps ../../llvm/llvm_ocaml.h)
+ (flags :standard @CFLAGS@))
+ (c_library_flags (:include c_library_flags.sexp)))
+
+(rule
+ (targets c_library_flags.sexp)
+ (action (run ../../discover.sh @TARGET@)))
diff --git a/llvm/bindings/ocaml/setup.sh b/llvm/bindings/ocaml/setup.sh
index b7b5a122ec9d2c8..544a7fda119d0a8 100755
--- a/llvm/bindings/ocaml/setup.sh
+++ b/llvm/bindings/ocaml/setup.sh
@@ -20,6 +20,17 @@ base_cflags=$($llvm_config --cflags)
ldflags="$($llvm_config --ldflags) -lstdc++ -fPIC"
llvm_targets=$($llvm_config --targets-built)
+for target in $llvm_targets; do
+ touch "llvm_${target}.opam"
+ mkdir -p backends/$target
+ sed -e "s/@TARGET@/$target/g" \
+ -e "s/@CFLAGS@/-DTARGET=$target/g" "backends/dune.in" > backends/$target/dune
+ sed "s/@TARGET@/$target/g" "backends/llvm_backend.mli.in" > backends/$target/llvm_${target}.mli
+ sed "s/@TARGET@/$target/g" "backends/llvm_backend.ml.in" > backends/$target/llvm_${target}.ml
+ sed "s/@TARGET@/$target/g" "backends/backend_ocaml.c" > backends/$target/${target}_ocaml.c
+done
+sed "s/@LLVM_TARGETS_TO_BUILD@/$llvm_targets/g" "all_backends/dune.in" > all_backends/dune
+
append_context() {
context_name=$1
linking_mode=$2
>From 7aec8214937a1a7a62f7b7e32462f05e192abee3 Mon Sep 17 00:00:00 2001
From: Alan Hu <alanh at ccs.neu.edu>
Date: Sun, 24 Sep 2023 16:24:33 -0400
Subject: [PATCH 10/11] Cleanup
---
llvm/bindings/ocaml/setup.sh | 1 -
llvm/test/lit.cfg.py | 2 --
2 files changed, 3 deletions(-)
diff --git a/llvm/bindings/ocaml/setup.sh b/llvm/bindings/ocaml/setup.sh
index 544a7fda119d0a8..2333a3d957665f8 100755
--- a/llvm/bindings/ocaml/setup.sh
+++ b/llvm/bindings/ocaml/setup.sh
@@ -17,7 +17,6 @@ llvm_config=$1
mode=$2
base_cflags=$($llvm_config --cflags)
-ldflags="$($llvm_config --ldflags) -lstdc++ -fPIC"
llvm_targets=$($llvm_config --targets-built)
for target in $llvm_targets; do
diff --git a/llvm/test/lit.cfg.py b/llvm/test/lit.cfg.py
index 9091ee7ed7aca58..b7c946387c029a1 100644
--- a/llvm/test/lit.cfg.py
+++ b/llvm/test/lit.cfg.py
@@ -45,14 +45,12 @@
# Set up OCAMLPATH to include newly built OCaml libraries.
top_ocaml_lib = os.path.join(config.llvm_lib_dir, "ocaml")
llvm_ocaml_lib = os.path.join(top_ocaml_lib, "llvm")
-stublibs_ocaml_lib = os.path.join(top_ocaml_lib, "stublibs")
llvm_config.with_system_environment("OCAMLPATH")
llvm_config.with_environment("OCAMLPATH", top_ocaml_lib, append_path=True)
llvm_config.with_environment("OCAMLPATH", llvm_ocaml_lib, append_path=True)
llvm_config.with_system_environment("CAML_LD_LIBRARY_PATH")
-llvm_config.with_environment("CAML_LD_LIBRARY_PATH", stublibs_ocaml_lib, append_path=True)
# Set up OCAMLRUNPARAM to enable backtraces in OCaml tests.
llvm_config.with_environment("OCAMLRUNPARAM", "b")
>From 57ac358869d1e4db7e8b6117e62c28cdb02b117b Mon Sep 17 00:00:00 2001
From: Alan Hu <alanh at ccs.neu.edu>
Date: Sun, 24 Sep 2023 16:27:13 -0400
Subject: [PATCH 11/11] Cleanup
---
llvm/test/lit.cfg.py | 1 +
1 file changed, 1 insertion(+)
diff --git a/llvm/test/lit.cfg.py b/llvm/test/lit.cfg.py
index b7c946387c029a1..7a1e30b49b0c3d1 100644
--- a/llvm/test/lit.cfg.py
+++ b/llvm/test/lit.cfg.py
@@ -51,6 +51,7 @@
llvm_config.with_environment("OCAMLPATH", llvm_ocaml_lib, append_path=True)
llvm_config.with_system_environment("CAML_LD_LIBRARY_PATH")
+llvm_config.with_environment("CAML_LD_LIBRARY_PATH", llvm_ocaml_lib, append_path=True)
# Set up OCAMLRUNPARAM to enable backtraces in OCaml tests.
llvm_config.with_environment("OCAMLRUNPARAM", "b")
More information about the llvm-commits
mailing list