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

Static ctypes; building with dune #28

Merged
merged 7 commits into from
Dec 19, 2023
Merged
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
8 changes: 3 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,11 @@ jobs:
uses: avsm/setup-ocaml@v2
with:
ocaml-compiler: ${{ matrix.ocaml-compiler }}
dune-cache: true

- name: Install OCaml bindings
run: opam install .
- name: Install and test OCaml bindings
run: opam install --with-test --working-dir .

- name: Test OCaml bindings
run: opam exec -- make -C ocaml test

# we don't reuse the wheels so that all of the CI runs can happen concurrently
- name: Install Python directly
run: sudo pip3 install .
Expand Down
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,15 @@ Makefile
/stamp-h1

# generated by make
/src/builtins.h
/src/nodes.h
/src/syntax.h
/src/token.h
/src/token_vars.h

# generated by dune
_build

# Apple debug symbol bundles
*.dSYM/

Expand Down
36 changes: 36 additions & 0 deletions dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
(data_only_dirs src)

(rule
(deps (source_tree src) configure.ac Makefile.am)
(targets libdash.a dlldash.so
builtins.h nodes.h syntax.h token.h token_vars.h
)
(action
(bash
"\
\n set -e\
\n if [ \"$(uname -s)\" = \"Darwin\" ]; then glibtoolize; else libtoolize; fi\
\n aclocal && autoheader && automake --add-missing && autoconf\
\n ./configure --prefix=\"$(pwd)\"\
\n %{make}\
\n %{make} install\
\n cp lib/libdash.a libdash.a\
\n cp lib/dlldash.so dlldash.so\
\n cp src/{builtins,nodes,syntax,token,token_vars}.h .\
\n")))

(subdir src
(rule
(deps ../builtins.h ../nodes.h ../syntax.h ../token.h ../token_vars.h)
(targets builtins.h nodes.h syntax.h token.h token_vars.h)
(action
(progn
(copy ../builtins.h builtins.h)
(copy ../nodes.h nodes.h)
(copy ../syntax.h syntax.h)
(copy ../token.h token.h)
(copy ../token_vars.h token_vars.h)))))

(library
(name dash)
(foreign_archives dash))
22 changes: 22 additions & 0 deletions dune-project
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
(lang dune 3.12)
(name libdash)
(using ctypes 0.3)

(source (github mgree/libdash))
(license BSD-3-Clause)
(authors "Michael Greenberg")
(maintainers "[email protected]")

(package
(name libdash)
(synopsis "Bindings to the dash shell's parser")
(depends
("ctypes" (>= "0.21.1"))
("ctypes-foreign" (>= "0.21.1"))
("atdgen" (>= "2.15.0"))
("conf-autoconf" (>= 0.1))
("conf-aclocal" (>= 2))
("conf-automake" (>= 1))
("conf-libtool" (>= 1))))

(generate_opam_files)
4 changes: 4 additions & 0 deletions dune-workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(lang dune 3.12)
(env
(dev
(flags (:standard -warn-error -27))))
56 changes: 25 additions & 31 deletions libdash.opam
Original file line number Diff line number Diff line change
@@ -1,40 +1,34 @@
# This file is generated by dune, edit dune-project instead
opam-version: "2.0"
synopsis: "Bindings to the dash shell's parser"
maintainer: "Michael Greenberg <michael[email protected]>"
authors: "Michael Greenberg <[email protected]>"
maintainer: ["michael@greenberg.science"]
authors: ["Michael Greenberg"]
license: "BSD-3-Clause"
homepage: "https://github.com/mgree/libdash"
bug-reports: "https://github.com/mgree/libdash/issues"
depends: [
"ocaml" {>= "4.07"}
"ocamlfind" {>= "1.8.0"}
"ctypes" {>= "0.18.0"}
"ctypes-foreign" {>= "0.18.0"}
"atdgen" {>= "2.3.2"}
"conf-autoconf" {build}
"conf-aclocal" {build}
"conf-automake" {build}
"conf-libtool" {build}
"dune" {>= "3.12"}
"ctypes" {>= "0.21.1"}
"ctypes-foreign" {>= "0.21.1"}
"atdgen" {>= "2.15.0"}
"conf-autoconf" {>= "0.1"}
"conf-aclocal" {>= "2"}
"conf-automake" {>= "1"}
"conf-libtool" {>= "1"}
"odoc" {with-doc}
]
build: [
["libtoolize"] {os != "macos"}
["glibtoolize"] {os = "macos"}
["aclocal"]
["autoheader"]
["automake" "--add-missing"]
["autoconf"]
["mkdir" "_build"]
["./configure" "--prefix=%{build}%/_build"]
[make]
[make "install"] # into _build
["ocaml/mk_meta.sh" "%{_:lib}%"] # pass along the lib directory for the rpath in the META
[make "-C" "ocaml" "all"]
["./mk_dot_install.sh"]
["./ldconfig.sh"] # fix up .so files if ldconfig didn't do it
[make "-C" "ocaml" "test"] {with-test}
["dune" "subst"] {dev}
[
"dune"
"build"
"-p"
name
"-j"
jobs
"@install"
"@runtest" {with-test}
"@doc" {with-doc}
]
]
dev-repo: "git+https:///github.com/mgree/libdash"
url {
src: "https://github.com/mgree/libdash/archive/v0.3.tar.gz"
}

dev-repo: "git+https://github.com/mgree/libdash.git"
12 changes: 4 additions & 8 deletions ocaml/ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -86,25 +86,20 @@ let string_of_var_type = function


open Ctypes
open Foreign
open Dash

let rec last = function
| [] -> None
| [x] -> Some x
| x::xs -> last xs
| _::xs -> last xs

let skip = Command (-1,[],[],[])

let special_chars : char list = explode "|&;<>()$`\\\"'"

type quote_mode =
QUnquoted
| QQuoted
| QHeredoc

let needs_escaping c = List.mem c special_chars

let rec of_node (n : node union ptr) : t =
if nullptr n
then skip
Expand Down Expand Up @@ -225,7 +220,7 @@ and of_binary (n : node union ptr) =
(of_node (getf n nbinary_ch1), of_node (getf n nbinary_ch2))

and to_arg (n : narg structure) : arg =
let a,s,bqlist,stack = parse_arg ~tilde_ok:true ~assign:false (explode (getf n narg_text)) (getf n narg_backquote) [] in
let a,s,bqlist,stack = parse_arg ~assign:false (explode (getf n narg_text)) (getf n narg_backquote) [] in
(* we should have used up the string and have no backquotes left in our list *)
assert (s = []);
assert (nullptr bqlist);
Expand Down Expand Up @@ -304,6 +299,7 @@ and parse_arg ?tilde_ok:(tilde_ok=false) ~assign:(assign:bool) (s : char list) (
then (* we're in arithmetic or double quotes, so tilde is ignored *)
arg_char assign (C '~') s bqlist stack
else
let _ = tilde_ok in (* unused? *)
let uname,s' = parse_tilde [] s in
arg_char assign (T uname) s' bqlist stack
(* ordinary character *)
Expand All @@ -325,7 +321,7 @@ and parse_tilde acc s =
and arg_char assign c s bqlist stack =
let tilde_ok =
match c with
| C c -> assign && (match last s with
| C _ -> assign && (match last s with
| Some ':' -> true
| _ -> false)
| _ -> false
Expand Down
26 changes: 13 additions & 13 deletions ocaml/ast_atd.atd
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
type char <ocaml predef module="Ast"> = int <ocaml repr="char">
type char <ocaml predef module="Libdash.Ast"> = int <ocaml repr="char">

type linno <ocaml predef module="Ast"> = int
type linno <ocaml predef module="Libdash.Ast"> = int

type t <ocaml predef module="Ast"> = [
type t <ocaml predef module="Libdash.Ast"> = [
Command of (linno * assign list * args * redirection list) (* assign, args, redir *)
| Pipe of (bool * t list) (* background?, commands *)
| Redir of (linno * t * redirection list)
Expand All @@ -19,37 +19,37 @@ type t <ocaml predef module="Ast"> = [
| Defun of (linno * string * t) (* name, body *)
] <ocaml repr="classic">

type assign <ocaml predef module="Ast"> = (string * arg) <ocaml repr="classic">
type assign <ocaml predef module="Libdash.Ast"> = (string * arg) <ocaml repr="classic">

type redirection <ocaml predef module="Ast"> = [
type redirection <ocaml predef module="Libdash.Ast"> = [
File of (redir_type * int * arg)
| Dup of (dup_type * int * arg)
| Heredoc of (heredoc_type * int * arg)
] <ocaml repr="classic">

type redir_type <ocaml predef module="Ast"> = [
type redir_type <ocaml predef module="Libdash.Ast"> = [
To
| Clobber
| From
| FromTo
| Append
] <ocaml repr="classic">

type dup_type <ocaml predef module="Ast"> = [
type dup_type <ocaml predef module="Libdash.Ast"> = [
ToFD
| FromFD
] <ocaml repr="classic">

type heredoc_type <ocaml predef module="Ast"> = [
type heredoc_type <ocaml predef module="Libdash.Ast"> = [
Here
| XHere (* for when in a quote... not sure when this comes up *)
] <ocaml repr="classic">

type args <ocaml predef module="Ast"> = arg list
type args <ocaml predef module="Libdash.Ast"> = arg list

type arg <ocaml predef module="Ast"> = arg_char list
type arg <ocaml predef module="Libdash.Ast"> = arg_char list

type arg_char <ocaml predef module="Ast"> = [
type arg_char <ocaml predef module="Libdash.Ast"> = [
C of char
| E of char (* escape... necessary for expansion *)
| T of string option (* tilde *)
Expand All @@ -59,7 +59,7 @@ type arg_char <ocaml predef module="Ast"> = [
| B of t (* backquote *)
] <ocaml repr="classic">

type var_type <ocaml predef module="Ast"> = [
type var_type <ocaml predef module="Libdash.Ast"> = [
Normal
| Minus
| Plus
Expand All @@ -72,7 +72,7 @@ type var_type <ocaml predef module="Ast"> = [
| Length
] <ocaml repr="classic">

type case <ocaml predef module="Ast"> = {
type case <ocaml predef module="Libdash.Ast"> = {
cpattern : arg list;
cbody : t
}
Loading
Loading