[llvm] [OCaml] Build OCaml bindings using Dune (PR #67272)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 25 14:53:25 PDT 2023
https://github.com/alan-j-hu updated https://github.com/llvm/llvm-project/pull/67272
>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/15] 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/15] 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/15] 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/15] 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/15] 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/15] 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/15] 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/15] 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/15] 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/15] 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/15] 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")
>From 93a4acbc4c472cb84c46246aaf2dda7e9816a55b Mon Sep 17 00:00:00 2001
From: Alan Hu <alanh at ccs.neu.edu>
Date: Sun, 24 Sep 2023 22:12:57 -0400
Subject: [PATCH 12/15] Fix formatting
---
llvm/bindings/ocaml/llvm/llvm_ocaml.c | 16 ++++++++--------
1 file changed, 8 insertions(+), 8 deletions(-)
diff --git a/llvm/bindings/ocaml/llvm/llvm_ocaml.c b/llvm/bindings/ocaml/llvm/llvm_ocaml.c
index b83db58d9d3de20..4813ff1a76a3fae 100644
--- a/llvm/bindings/ocaml/llvm/llvm_ocaml.c
+++ b/llvm/bindings/ocaml/llvm/llvm_ocaml.c
@@ -15,16 +15,16 @@
|* *|
\*===----------------------------------------------------------------------===*/
-#include <assert.h>
-#include <stdlib.h>
-#include <string.h>
+#include "llvm_ocaml.h"
+#include "caml/callback.h"
+#include "caml/fail.h"
+#include "caml/memory.h"
#include "llvm-c/Core.h"
#include "llvm-c/Support.h"
#include "llvm/Config/llvm-config.h"
-#include "caml/memory.h"
-#include "caml/fail.h"
-#include "caml/callback.h"
-#include "llvm_ocaml.h"
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
#if OCAML_VERSION < 41200
value caml_alloc_some(value v) {
@@ -180,7 +180,7 @@ static value alloc_variant(int tag, value Value) {
/*===-- Context error handling --------------------------------------------===*/
static void llvm_diagnostic_handler_trampoline(LLVMDiagnosticInfoRef DI,
- void *DiagnosticContext) {
+ void *DiagnosticContext) {
caml_callback(*((value *)DiagnosticContext), to_val(DI));
}
>From 62a4182d735e5e9d771513275ae25388687fc6e6 Mon Sep 17 00:00:00 2001
From: Alan Hu <alanh at ccs.neu.edu>
Date: Sun, 24 Sep 2023 23:50:55 -0400
Subject: [PATCH 13/15] Make sure OCaml bindings build cleanly by adding
dependencies on components
---
llvm/bindings/ocaml/CMakeLists.txt | 25 ++++
llvm/bindings/ocaml/setup.sh | 4 +-
llvm/cmake/config-ix.cmake | 1 -
llvm/cmake/modules/AddOCaml.cmake | 232 -----------------------------
4 files changed, 27 insertions(+), 235 deletions(-)
delete mode 100644 llvm/cmake/modules/AddOCaml.cmake
diff --git a/llvm/bindings/ocaml/CMakeLists.txt b/llvm/bindings/ocaml/CMakeLists.txt
index e25a480b4d7d609..36b85f832832c91 100644
--- a/llvm/bindings/ocaml/CMakeLists.txt
+++ b/llvm/bindings/ocaml/CMakeLists.txt
@@ -1,3 +1,9 @@
+add_custom_target(ocaml_make_directory
+ COMMAND "${CMAKE_COMMAND}" "-E" "make_directory" "${LLVM_LIBRARY_DIR}/ocaml/llvm")
+add_custom_target("ocaml_all")
+set_target_properties(ocaml_all PROPERTIES FOLDER "Misc")
+set_target_properties(ocaml_make_directory PROPERTIES FOLDER "Misc")
+
add_custom_target(ocaml_copy_files)
foreach( file dune-project setup.sh discover.sh llvm.opam )
@@ -25,6 +31,25 @@ add_custom_target(dune_workspace ALL
ocaml_copy_files
"${CMAKE_BINARY_DIR}/bin/llvm-config")
+explicit_map_components_to_libraries(llvm_libs
+ Core Support
+ Analysis
+ BitReader
+ BitWriter
+ ExecutionEngine MCJIT native
+ IRReader
+ Linker
+ Target
+ Passes
+ TransformUtils)
+foreach( llvm_lib ${llvm_libs} )
+ add_dependencies(dune_workspace "${llvm_lib}")
+endforeach()
+explicit_map_components_to_libraries(llvm_libs ${LLVM_TARGETS_TO_BUILD})
+foreach( llvm_lib ${llvm_libs} )
+ add_dependencies(dune_workspace "${llvm_lib}")
+endforeach()
+
add_custom_target(dune_build_all
COMMAND "dune" "build" "--release"
COMMAND "dune" "install" "--libdir" "${CMAKE_BINARY_DIR}/lib/ocaml"
diff --git a/llvm/bindings/ocaml/setup.sh b/llvm/bindings/ocaml/setup.sh
index 2333a3d957665f8..73a484169f3c66b 100755
--- a/llvm/bindings/ocaml/setup.sh
+++ b/llvm/bindings/ocaml/setup.sh
@@ -48,7 +48,7 @@ echo "(lang dune 3.2)
" > "dune-workspace"
if [ $mode = "static" ]; then
- $llvm_config --link-static --libs
+ $llvm_config --link-static
if [ $? -ne 0 ]; then
echo "Static mode is not supported."
exit 1
@@ -56,7 +56,7 @@ if [ $mode = "static" ]; then
append_context static --link-static
fi
if [ $mode = "shared" ]; then
- $llvm_config --link-shared --libs
+ $llvm_config --link-shared
if [ $? -ne 0 ]; then
echo "Shared mode is not supported."
exit 1
diff --git a/llvm/cmake/config-ix.cmake b/llvm/cmake/config-ix.cmake
index a01e17de8677aa7..df419b0f4d3b737 100644
--- a/llvm/cmake/config-ix.cmake
+++ b/llvm/cmake/config-ix.cmake
@@ -665,7 +665,6 @@ endif()
# Keep the version requirements in sync with bindings/ocaml/README.txt.
set(LLVM_BINDINGS "")
include(FindOCaml)
-include(AddOCaml)
if(WIN32 OR NOT LLVM_ENABLE_BINDINGS)
message(STATUS "OCaml bindings disabled.")
else()
diff --git a/llvm/cmake/modules/AddOCaml.cmake b/llvm/cmake/modules/AddOCaml.cmake
deleted file mode 100644
index 891c9e6d618c06c..000000000000000
--- a/llvm/cmake/modules/AddOCaml.cmake
+++ /dev/null
@@ -1,232 +0,0 @@
-# CMake build rules for the OCaml language.
-# Assumes FindOCaml is used.
-# http://ocaml.org/
-#
-# Example usage:
-#
-# add_ocaml_library(pkg_a OCAML mod_a OCAMLDEP pkg_b C mod_a_stubs PKG ctypes LLVM core)
-#
-# Unnamed parameters:
-#
-# * Library name.
-#
-# Named parameters:
-#
-# OCAML OCaml module names. Imply presence of a corresponding .ml and .mli files.
-# OCAMLDEP Names of libraries this library depends on.
-# C C stub sources. Imply presence of a corresponding .c file.
-# CFLAGS Additional arguments passed when compiling C stubs.
-# PKG Names of ocamlfind packages this library depends on.
-# LLVM Names of LLVM libraries this library depends on.
-# NOCOPY Do not automatically copy sources (.c, .ml, .mli) from the source directory,
-# e.g. if they are generated.
-#
-
-function(add_ocaml_library name)
- CMAKE_PARSE_ARGUMENTS(ARG "NOCOPY" "" "OCAML;OCAMLDEP;C;CFLAGS;PKG;LLVM" ${ARGN})
-
- set(src ${CMAKE_CURRENT_SOURCE_DIR})
- set(bin ${CMAKE_CURRENT_BINARY_DIR})
-
- set(ocaml_pkgs)
- foreach( ocaml_pkg ${ARG_PKG} )
- list(APPEND ocaml_pkgs "-package" "${ocaml_pkg}")
- endforeach()
-
- set(sources)
-
- set(ocaml_inputs)
-
- set(ocaml_outputs "${bin}/${name}.cma")
- if( ARG_C )
- list(APPEND ocaml_outputs
- "${bin}/lib${name}${CMAKE_STATIC_LIBRARY_SUFFIX}")
- if ( BUILD_SHARED_LIBS )
- list(APPEND ocaml_outputs
- "${bin}/dll${name}${CMAKE_SHARED_LIBRARY_SUFFIX}")
- endif()
- endif()
- if( HAVE_OCAMLOPT )
- list(APPEND ocaml_outputs
- "${bin}/${name}.cmxa"
- "${bin}/${name}${CMAKE_STATIC_LIBRARY_SUFFIX}")
- endif()
-
- set(ocaml_flags "-lstdc++" "-ldopt" "-L${LLVM_LIBRARY_DIR}"
- "-ccopt" "-L\\$CAMLORIGIN/../.."
- "-ccopt" "-Wl,-rpath,\\$CAMLORIGIN/../.."
- ${ocaml_pkgs})
-
- foreach( ocaml_dep ${ARG_OCAMLDEP} )
- get_target_property(dep_ocaml_flags "ocaml_${ocaml_dep}" OCAML_FLAGS)
- list(APPEND ocaml_flags ${dep_ocaml_flags})
- endforeach()
-
- if( NOT BUILD_SHARED_LIBS )
- list(APPEND ocaml_flags "-custom")
- endif()
-
- if(LLVM_LINK_LLVM_DYLIB)
- list(APPEND ocaml_flags "-lLLVM")
- else()
- explicit_map_components_to_libraries(llvm_libs ${ARG_LLVM})
- foreach( llvm_lib ${llvm_libs} )
- list(APPEND ocaml_flags "-l${llvm_lib}" )
- endforeach()
-
- get_property(system_libs TARGET LLVMSupport PROPERTY LLVM_SYSTEM_LIBS)
- foreach(system_lib ${system_libs})
- if (system_lib MATCHES "^-")
- # If it's an option, pass it without changes.
- list(APPEND ocaml_flags "${system_lib}" )
- else()
- # Otherwise assume it's a library name we need to link with.
- list(APPEND ocaml_flags "-l${system_lib}" )
- endif()
- endforeach()
- endif()
-
- string(REPLACE ";" " " ARG_CFLAGS "${ARG_CFLAGS}")
- set(c_flags "${ARG_CFLAGS} ${LLVM_DEFINITIONS}")
- foreach( include_dir ${LLVM_INCLUDE_DIR} ${LLVM_MAIN_INCLUDE_DIR} )
- set(c_flags "${c_flags} -I${include_dir}")
- endforeach()
- # include -D/-UNDEBUG to match dump function visibility
- # regex from HandleLLVMOptions.cmake
- string(REGEX MATCH "(^| )[/-][UD] *NDEBUG($| )" flag_matches
- "${CMAKE_C_FLAGS_${uppercase_CMAKE_BUILD_TYPE}} ${CMAKE_C_FLAGS}")
- set(c_flags "${c_flags} ${flag_matches}")
-
- foreach( ocaml_file ${ARG_OCAML} )
- list(APPEND sources "${ocaml_file}.mli" "${ocaml_file}.ml")
-
- list(APPEND ocaml_inputs "${bin}/${ocaml_file}.mli" "${bin}/${ocaml_file}.ml")
-
- list(APPEND ocaml_outputs "${bin}/${ocaml_file}.cmi" "${bin}/${ocaml_file}.cmo")
-
- list(APPEND ocaml_outputs "${bin}/${ocaml_file}.cmti" "${bin}/${ocaml_file}.cmt")
-
- if( HAVE_OCAMLOPT )
- list(APPEND ocaml_outputs
- "${bin}/${ocaml_file}.cmx"
- "${bin}/${ocaml_file}${CMAKE_C_OUTPUT_EXTENSION}")
- endif()
- endforeach()
-
- foreach( c_file ${ARG_C} )
- list(APPEND sources "${c_file}.c")
-
- list(APPEND c_inputs "${bin}/${c_file}.c")
- list(APPEND c_outputs "${bin}/${c_file}${CMAKE_C_OUTPUT_EXTENSION}")
- endforeach()
-
- if( NOT ARG_NOCOPY )
- foreach( source ${sources} )
- add_custom_command(
- OUTPUT "${bin}/${source}"
- COMMAND "${CMAKE_COMMAND}" "-E" "copy" "${src}/${source}" "${bin}"
- DEPENDS "${src}/${source}"
- COMMENT "Copying ${source} to build area")
- endforeach()
- endif()
-
- foreach( c_input ${c_inputs} )
- get_filename_component(basename "${c_input}" NAME_WE)
- add_custom_command(
- OUTPUT "${basename}${CMAKE_C_OUTPUT_EXTENSION}"
- COMMAND "${OCAMLFIND}" "ocamlc" "-c" "${c_input}" -ccopt ${c_flags}
- DEPENDS "${c_input}"
- COMMENT "Building OCaml stub object file ${basename}${CMAKE_C_OUTPUT_EXTENSION}"
- VERBATIM)
- endforeach()
-
- set(ocaml_params)
- foreach( ocaml_input ${ocaml_inputs} ${c_outputs})
- get_filename_component(filename "${ocaml_input}" NAME)
- list(APPEND ocaml_params "${filename}")
- endforeach()
-
- if( APPLE )
- set(ocaml_rpath "@executable_path/../../../lib${LLVM_LIBDIR_SUFFIX}")
- elseif( UNIX )
- set(ocaml_rpath "\\$ORIGIN/../../../lib${LLVM_LIBDIR_SUFFIX}")
- endif()
- list(APPEND ocaml_flags "-ldopt" "-Wl,-rpath,${ocaml_rpath}")
-
- add_custom_command(
- OUTPUT ${ocaml_outputs}
- COMMAND "${OCAMLFIND}" "ocamlmklib" "-ocamlcflags" "-bin-annot"
- "-o" "${name}" ${ocaml_flags} ${ocaml_params}
- DEPENDS ${ocaml_inputs} ${c_outputs}
- COMMENT "Building OCaml library ${name}"
- VERBATIM)
-
- add_custom_command(
- OUTPUT "${bin}/${name}.odoc"
- COMMAND "${OCAMLFIND}" "ocamldoc"
- "-I" "${bin}"
- "-I" "${LLVM_LIBRARY_DIR}/ocaml/llvm/"
- "-dump" "${bin}/${name}.odoc"
- ${ocaml_pkgs} ${ocaml_inputs}
- DEPENDS ${ocaml_inputs} ${ocaml_outputs}
- COMMENT "Building OCaml documentation for ${name}"
- VERBATIM)
-
- add_custom_target("ocaml_${name}" ALL DEPENDS ${ocaml_outputs} "${bin}/${name}.odoc")
-
- set_target_properties("ocaml_${name}" PROPERTIES
- OCAML_FLAGS "-I;${bin}")
- set_target_properties("ocaml_${name}" PROPERTIES
- OCAML_ODOC "${bin}/${name}.odoc")
-
- foreach( ocaml_dep ${ARG_OCAMLDEP} )
- add_dependencies("ocaml_${name}" "ocaml_${ocaml_dep}")
- endforeach()
-
- if( NOT LLVM_OCAML_OUT_OF_TREE )
- foreach( llvm_lib ${llvm_libs} )
- add_dependencies("ocaml_${name}" "${llvm_lib}")
- endforeach()
- endif()
-
- add_dependencies("ocaml_all" "ocaml_${name}")
-
- set(install_files)
- set(install_shlibs)
- foreach( ocaml_output ${ocaml_inputs} ${ocaml_outputs} )
- get_filename_component(ext "${ocaml_output}" EXT)
-
- if( NOT (ext STREQUAL ".cmo" OR
- ext STREQUAL ".ml" OR
- ext STREQUAL CMAKE_C_OUTPUT_EXTENSION OR
- ext STREQUAL CMAKE_SHARED_LIBRARY_SUFFIX) )
- list(APPEND install_files "${ocaml_output}")
- elseif( ext STREQUAL CMAKE_SHARED_LIBRARY_SUFFIX)
- list(APPEND install_shlibs "${ocaml_output}")
- endif()
- endforeach()
-
- install(FILES ${install_files}
- DESTINATION "${LLVM_OCAML_INSTALL_PATH}/llvm")
- install(FILES ${install_shlibs}
- PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE
- GROUP_READ GROUP_EXECUTE
- WORLD_READ WORLD_EXECUTE
- DESTINATION "${LLVM_OCAML_INSTALL_PATH}/stublibs")
-
- foreach( install_file ${install_files} ${install_shlibs} )
- get_filename_component(filename "${install_file}" NAME)
- add_custom_command(TARGET "ocaml_${name}" POST_BUILD
- COMMAND "${CMAKE_COMMAND}" "-E" "copy" "${install_file}"
- "${LLVM_LIBRARY_DIR}/ocaml/llvm/"
- COMMENT "Copying OCaml library component ${filename} to intermediate area"
- VERBATIM)
- add_dependencies("ocaml_${name}" ocaml_make_directory)
- endforeach()
-endfunction()
-
-add_custom_target(ocaml_make_directory
- COMMAND "${CMAKE_COMMAND}" "-E" "make_directory" "${LLVM_LIBRARY_DIR}/ocaml/llvm")
-add_custom_target("ocaml_all")
-set_target_properties(ocaml_all PROPERTIES FOLDER "Misc")
-set_target_properties(ocaml_make_directory PROPERTIES FOLDER "Misc")
>From 813cec7890154ce5915f77ae29f05aa1e50846f3 Mon Sep 17 00:00:00 2001
From: Alan Hu <alanh at ccs.neu.edu>
Date: Mon, 25 Sep 2023 00:31:03 -0400
Subject: [PATCH 14/15] Ensure a clean build from scratch
---
llvm/bindings/ocaml/CMakeLists.txt | 17 ++++++++++-------
1 file changed, 10 insertions(+), 7 deletions(-)
diff --git a/llvm/bindings/ocaml/CMakeLists.txt b/llvm/bindings/ocaml/CMakeLists.txt
index 36b85f832832c91..b36535e773cdf27 100644
--- a/llvm/bindings/ocaml/CMakeLists.txt
+++ b/llvm/bindings/ocaml/CMakeLists.txt
@@ -31,6 +31,7 @@ add_custom_target(dune_workspace ALL
ocaml_copy_files
"${CMAKE_BINARY_DIR}/bin/llvm-config")
+add_custom_target(ocaml_llvm_deps)
explicit_map_components_to_libraries(llvm_libs
Core Support
Analysis
@@ -41,18 +42,20 @@ explicit_map_components_to_libraries(llvm_libs
Linker
Target
Passes
- TransformUtils)
+ TransformUtils
+ AllTargetsCodeGens
+ AllTargetsAsmParsers
+ AllTargetsDescs
+ AllTargetsDisassemblers
+ AllTargetsInfos
+ AllTargetsMCAs)
foreach( llvm_lib ${llvm_libs} )
- add_dependencies(dune_workspace "${llvm_lib}")
-endforeach()
-explicit_map_components_to_libraries(llvm_libs ${LLVM_TARGETS_TO_BUILD})
-foreach( llvm_lib ${llvm_libs} )
- add_dependencies(dune_workspace "${llvm_lib}")
+ add_dependencies(ocaml_llvm_deps "${llvm_lib}")
endforeach()
add_custom_target(dune_build_all
COMMAND "dune" "build" "--release"
COMMAND "dune" "install" "--libdir" "${CMAKE_BINARY_DIR}/lib/ocaml"
- DEPENDS ocaml_copy_files dune_workspace)
+ DEPENDS dune_workspace ocaml_copy_files ocaml_llvm_deps)
add_dependencies(ocaml_all dune_build_all)
>From ab692812479faf1e8603d5a5e4252894d8a4b511 Mon Sep 17 00:00:00 2001
From: Alan Hu <alanh at ccs.neu.edu>
Date: Mon, 25 Sep 2023 17:53:04 -0400
Subject: [PATCH 15/15] Get path to Dune with find_program
---
llvm/bindings/ocaml/CMakeLists.txt | 12 +++++-------
llvm/bindings/ocaml/{setup.sh => configure.sh} | 7 +++----
llvm/cmake/modules/FindOCaml.cmake | 5 ++++-
llvm/test/CMakeLists.txt | 16 ++--------------
4 files changed, 14 insertions(+), 26 deletions(-)
rename llvm/bindings/ocaml/{setup.sh => configure.sh} (92%)
diff --git a/llvm/bindings/ocaml/CMakeLists.txt b/llvm/bindings/ocaml/CMakeLists.txt
index b36535e773cdf27..b1c23bad3be671d 100644
--- a/llvm/bindings/ocaml/CMakeLists.txt
+++ b/llvm/bindings/ocaml/CMakeLists.txt
@@ -6,7 +6,7 @@ set_target_properties(ocaml_make_directory PROPERTIES FOLDER "Misc")
add_custom_target(ocaml_copy_files)
-foreach( file dune-project setup.sh discover.sh llvm.opam )
+foreach( file dune-project configure.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}"
@@ -26,10 +26,8 @@ foreach ( dir
endforeach()
add_custom_target(dune_workspace ALL
- COMMAND "./setup.sh" "${CMAKE_BINARY_DIR}/bin/llvm-config" "static"
- DEPENDS
- ocaml_copy_files
- "${CMAKE_BINARY_DIR}/bin/llvm-config")
+ COMMAND "./configure.sh" "${LLVM_TOOLS_BINARY_DIR}/llvm-config" "static"
+ DEPENDS ocaml_copy_files "${LLVM_TOOLS_BINARY_DIR}/llvm-config")
add_custom_target(ocaml_llvm_deps)
explicit_map_components_to_libraries(llvm_libs
@@ -54,8 +52,8 @@ foreach( llvm_lib ${llvm_libs} )
endforeach()
add_custom_target(dune_build_all
- COMMAND "dune" "build" "--release"
- COMMAND "dune" "install" "--libdir" "${CMAKE_BINARY_DIR}/lib/ocaml"
+ COMMAND "${DUNE}" "build" "--release"
+ COMMAND "${DUNE}" "install" "--libdir" "${LLVM_LIBRARY_DIR}/ocaml"
DEPENDS dune_workspace ocaml_copy_files ocaml_llvm_deps)
add_dependencies(ocaml_all dune_build_all)
diff --git a/llvm/bindings/ocaml/setup.sh b/llvm/bindings/ocaml/configure.sh
similarity index 92%
rename from llvm/bindings/ocaml/setup.sh
rename to llvm/bindings/ocaml/configure.sh
index 73a484169f3c66b..f48a90e74e1c1be 100755
--- a/llvm/bindings/ocaml/setup.sh
+++ b/llvm/bindings/ocaml/configure.sh
@@ -31,8 +31,7 @@ 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
+ linking_mode=$1
echo "(context (default
(env
(_
@@ -53,7 +52,7 @@ if [ $mode = "static" ]; then
echo "Static mode is not supported."
exit 1
fi
- append_context static --link-static
+ append_context --link-static
fi
if [ $mode = "shared" ]; then
$llvm_config --link-shared
@@ -61,5 +60,5 @@ if [ $mode = "shared" ]; then
echo "Shared mode is not supported."
exit 1
fi
- append_context shared --link-shared
+ append_context --link-shared
fi
diff --git a/llvm/cmake/modules/FindOCaml.cmake b/llvm/cmake/modules/FindOCaml.cmake
index 8eba2127df1b361..6e56fe4880b2772 100644
--- a/llvm/cmake/modules/FindOCaml.cmake
+++ b/llvm/cmake/modules/FindOCaml.cmake
@@ -53,10 +53,13 @@ if( OCAMLFIND )
endif()
endif()
+find_program(DUNE NAMES dune)
+
find_package_handle_standard_args( OCaml DEFAULT_MSG
OCAMLFIND
OCAML_VERSION
- OCAML_STDLIB_PATH)
+ OCAML_STDLIB_PATH
+ DUNE)
mark_as_advanced(
OCAMLFIND)
diff --git a/llvm/test/CMakeLists.txt b/llvm/test/CMakeLists.txt
index ddfe7107ce68d85..d0f1d71a846988d 100644
--- a/llvm/test/CMakeLists.txt
+++ b/llvm/test/CMakeLists.txt
@@ -194,25 +194,13 @@ if(LLVM_BUILD_EXAMPLES)
endif()
endif()
-if(TARGET ocaml_llvm)
+if(TARGET ocaml_all)
# Clear all non-OCaml cross-target dependencies when building out-of-tree.
if(LLVM_OCAML_OUT_OF_TREE)
set(LLVM_TEST_DEPENDS)
endif()
- set(LLVM_TEST_DEPENDS ${LLVM_TEST_DEPENDS}
- ocaml_llvm
- ocaml_llvm_all_backends
- ocaml_llvm_analysis
- ocaml_llvm_bitreader
- ocaml_llvm_bitwriter
- ocaml_llvm_executionengine
- ocaml_llvm_irreader
- ocaml_llvm_linker
- ocaml_llvm_passbuilder
- ocaml_llvm_target
- ocaml_llvm_transform_utils
- )
+ set(LLVM_TEST_DEPENDS ${LLVM_TEST_DEPENDS} ocaml_all)
endif()
if (LLVM_INCLUDE_DXIL_TESTS)
More information about the llvm-commits
mailing list