Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port OCaml bindings to use Dune and Opam #2319

Open
wants to merge 7 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions .github/workflows/bindings.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Bindings testing

on:
- push
- pull_request

permissions: read-all

jobs:
ocaml:
strategy:
fail-fast: false
matrix:
os:
- ubuntu-latest
- macos-latest
- windows-latest

runs-on: ${{ matrix.os }}

steps:
- name: Checkout tree
uses: actions/checkout@v4

- name: Install Capstone
run: |
mkdir build && cd build
# build static library
cmake -DCAPSTONE_INSTALL=1 -DCMAKE_INSTALL_PREFIX=/usr ..
cmake --build . --config Debug
sudo cmake --build . --config Debug --target install

- name: Set-up OCaml
uses: ocaml/setup-ocaml@v3
with:
ocaml-compiler: 5

- name: Install Opam dependencies
run: opam install . --deps-only --with-test
working-directory: bindings/ocaml

- name: Building OCaml bindings
run: opam exec -- dune build
working-directory: bindings/ocaml

- name: Run tests
run: opam exec -- dune runtest
working-directory: bindings/ocaml
18 changes: 1 addition & 17 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,7 @@ bindings/cython/capstone.egg-info/
bindings/java/capstone.jar

# ocaml
bindings/ocaml/*.cmi
bindings/ocaml/*.cmx
bindings/ocaml/*.cmxa
bindings/ocaml/*.mli
bindings/ocaml/test
bindings/ocaml/test_arm
bindings/ocaml/test_aarch64
bindings/ocaml/test_basic
bindings/ocaml/test_mips
bindings/ocaml/test_x86
bindings/ocaml/test_detail
bindings/ocaml/test_ppc
bindings/ocaml/test_sparc
bindings/ocaml/test_systemz
bindings/ocaml/test_xcore
bindings/ocaml/test_m680x

bindings/ocaml/_build

# test binaries
tests/test_basic
Expand Down
32 changes: 27 additions & 5 deletions bindings/const_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,21 @@
'comment_close': '',
},
'ocaml': {
'header': "(* For Capstone Engine. AUTO-GENERATED FILE, DO NOT EDIT [%s_const.ml] *)\n",
'header': (
"(* For Capstone Engine. AUTO-GENERATED FILE, DO NOT EDIT [%s_const.ml] *)\n"
"let _CS_OP_INVALID, _CS_OP_REG, _CS_OP_IMM, _CS_OP_FP, _CS_OP_PRED,"
" _CS_OP_SPECIAL, _CS_OP_MEM, _CS_OP_MEM_REG, _CS_OP_MEM_IMM"
" = Capstone.(_CS_OP_INVALID, _CS_OP_REG, _CS_OP_IMM, _CS_OP_FP, _CS_OP_PRED,"
" _CS_OP_SPECIAL, _CS_OP_MEM, _CS_OP_MEM_REG, _CS_OP_MEM_IMM)\n"
),
'footer': "",
'line_format': 'let _%s = %s;;\n',
'out_file': './ocaml/%s_const.ml',
'out_file': './ocaml/src/%s_const.ml',
# prefixes for constant filenames of all archs - case sensitive
'arm.h': 'arm',
'mips.h': 'mips',
'aarch64.h': ['AArch64', 'AARCH64'],
'm68k.h': 'm68k',
'mips.h': 'mips',
'x86.h': 'x86',
'ppc.h': 'ppc',
'sparc.h': 'sparc',
Expand All @@ -84,6 +91,15 @@
'm680x.h': 'm680x',
'evm.h': 'evm',
'wasm.h': 'wasm',
'mos65xx.h': 'mos65xx',
'bpf.h': 'bpf',
'riscv.h': 'riscv',
'sh.h': 'sh',
'tricore.h': ['TRICORE', 'TriCore'],
'alpha.h': ['ALPHA', 'Alpha'],
'hppa.h': 'hppa',
'loongarch.h': 'loongarch',
'xtensa.h': 'xtensa',
'comment_open': '(*',
'comment_close': ' *)',
},
Expand Down Expand Up @@ -246,9 +262,15 @@ def has_special_arch_prefix(x):
# ocaml uses lsl for '<<', lor for '|'
rhs = rhs.replace('<<', ' lsl ')
rhs = rhs.replace('|', ' lor ')
# ocaml has no UINT8_MAX, UINT16_MAX
rhs = rhs.replace('UINT8_MAX', '0xFF')
rhs = rhs.replace('UINT16_MAX', '0xFFFF')
# To allow easy tokenisation
rhs = rhs.replace('+', ' + ')
# ocaml variable has _ as prefix
if rhs[0].isalpha():
rhs = '_' + rhs
for rhs_token in rhs.split():
if rhs_token.isidentifier() and rhs_token != 'lsl' and rhs_token != 'lor':
rhs = rhs.replace(rhs_token, '_' + rhs_token)

if lang == 'swift':
value = eval(rhs, None, values)
Expand Down
Loading
Loading