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

Switch to jbuilder #38

Merged
merged 7 commits into from
Jan 2, 2018
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
11 changes: 3 additions & 8 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
*.a
*.o
*.so
*.cmi
*.cmx
*.cmxa
*.cmo
*.cma
_build
.merlin
*.install
25 changes: 0 additions & 25 deletions META

This file was deleted.

116 changes: 12 additions & 104 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,111 +1,19 @@
CFLAGS = -Wall -O3 -funroll-loops -fPIC -DPIC
OCAMLC = ocamlc
OCAMLOPT = ocamlopt
OCAMLMKLIB = ocamlmklib
all:
jbuilder build --dev @install

# on unix architecture we just use the default value
EXE=
OBJ=o
A=a
SO=so
test:
jbuilder runtest

# on windows architecture redefine some values
ifeq "$(shell ocamlc -config | fgrep 'os_type:')" "os_type: Win32"
EXE=.exe
SO=dll
ifeq "$(shell ocamlc -config | fgrep 'ccomp_type:')" "ccomp_type: msvc"
OBJ=obj
A=lib
endif
endif

OCAMLOPTFLAGS =

OCAML_TEST_INC = -I `ocamlfind query oUnit`
OCAML_TEST_LIB = `ocamlfind query oUnit`/oUnit.cmxa

PROGRAMS_BINS = sha1sum sha256sum sha512sum
PROGRAMS = $(addsuffix $(EXE), $(PROGRAMS_BINS))

allshabytes = $(foreach n, 1 256 512, sha$(n).lib.$(OBJ) sha$(n)_stubs.$(OBJ) sha$(n).cmo)
allshaopts = $(foreach n, 1 256 512, sha$(n).lib.$(OBJ) sha$(n)_stubs.$(OBJ) sha$(n).cmx)

all: sha1.cmi sha1.cma sha1.cmxa sha256.cma sha256.cmxa sha512.cma sha512.cmxa sha.cma sha.cmxa

bins: $(PROGRAMS)

sha1sum$(EXE): sha1.cmxa sha256.cmxa sha512.cmxa shasum.cmx
$(OCAMLOPT) $(OCAMLOPTFLAGS) -o $@ -cclib -L. $+

sha256sum$(EXE): sha1sum$(EXE)
cp $< $@

sha512sum$(EXE): sha1sum$(EXE)
cp $< $@

sha.cma: $(allshabytes)
$(OCAMLMKLIB) -o sha $(allshabytes)

sha.cmxa: $(allshaopts)
$(OCAMLMKLIB) -o sha $(allshaopts)

sha1.cma: sha1.cmi sha1.lib.$(OBJ) sha1_stubs.$(OBJ) sha1.cmo
$(OCAMLMKLIB) -o sha1 sha1.lib.$(OBJ) sha1_stubs.$(OBJ) sha1.cmo

sha1.cmxa: sha1.cmi sha1.lib.$(OBJ) sha1_stubs.$(OBJ) sha1.cmx
$(OCAMLMKLIB) -o sha1 sha1.lib.$(OBJ) sha1_stubs.$(OBJ) sha1.cmx

sha256.cma: sha256.cmi sha256.lib.$(OBJ) sha256_stubs.$(OBJ) sha256.cmo
$(OCAMLMKLIB) -o sha256 sha256.lib.$(OBJ) sha256_stubs.$(OBJ) sha256.cmo

sha256.cmxa: sha256.cmi sha256.lib.$(OBJ) sha256_stubs.$(OBJ) sha256.cmx
$(OCAMLMKLIB) -o sha256 sha256.lib.$(OBJ) sha256_stubs.$(OBJ) sha256.cmx

sha512.cma: sha512.cmi sha512.lib.$(OBJ) sha512_stubs.$(OBJ) sha512.cmo
$(OCAMLMKLIB) -o sha512 sha512.lib.$(OBJ) sha512_stubs.$(OBJ) sha512.cmo

sha512.cmxa: sha512.cmi sha512.lib.$(OBJ) sha512_stubs.$(OBJ) sha512.cmx
$(OCAMLMKLIB) -o sha512 sha512.lib.$(OBJ) sha512_stubs.$(OBJ) sha512.cmx

tests: sha.test
./sha.test

sha.test: sha1.cmxa sha256.cmxa sha512.cmxa sha.test.cmx

%.test:
$(OCAMLOPT) -o $@ $(OCAML_BFLAGS) unix.cmxa $(OCAML_TEST_INC) $(OCAML_TEST_LIB) $+

%.test.cmo: %.test.ml
$(OCAMLC) -c -o $@ $(OCAML_BFLAGS) -custom $(OCAML_TEST_INC) $<

%.test.cmx: %.test.ml
$(OCAMLOPT) -c -o $@ $(OCAML_BFLAGS) $(OCAML_TEST_INC) $<

%.cmo: %.ml
$(OCAMLC) -c -o $@ $<

%.cmi: %.mli
$(OCAMLC) -c -o $@ $<

%.cmx: %.ml
$(OCAMLOPT) $(OCAMLOPTFLAGS) -c -o $@ $<

%.$(OBJ): %.c
$(OCAMLC) -ccopt "$(CFLAGS)" -c -o $@ $<
doc:
jbuilder build @doc

%.lib.$(OBJ): %.$(OBJ)
mv $< $@
install:
jbuilder install

.PHONY: clean install uninstall doc
doc:
[ -d html ] && rm -f html/* || mkdir html
ocamldoc -html -d html *.mli
uninstall:
jbuilder uninstall

clean:
rm -f *.$(OBJ) *.$(A) *.$(SO) *.cmo *.cmi *.cma *.cmx *.cmxa sha.test $(PROGRAMS)

install: sha1.cma sha1.cmxa sha256.cma sha256.cmxa sha512.cma sha512.cmxa sha.cma sha.cmxa META
ocamlfind install sha META *.cmx sha1.cmi sha1.cma sha1.cmxa sha256.cmi sha256.cma sha256.cmxa sha512.cmi sha512.cma sha512.cmxa sha.cma sha.cmxa *.$(A) *.$(SO)
jbuilder clean

uninstall:
ocamlfind remove sha
.PHONY: all test doc install uninstall clean
10 changes: 10 additions & 0 deletions jbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(jbuild_version 1)

(library
((name sha)
(public_name sha)
(synopsis "SHA-1 and SHA-2 family implementations")
(wrapped false)
(modules (sha1 sha256 sha512))
(c_flags (-Wall -O3 -funroll-loops)) ; Needs adapting for Windows
(c_names (sha1c sha1_stubs sha256c sha256_stubs sha512c sha512_stubs))))
13 changes: 8 additions & 5 deletions sha.opam
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ bug-reports: "https://github.com/djs55/ocaml-sha/issues"
dev-repo: "https://github.com/djs55/ocaml-sha.git"
license: "ISC"

build: make
install: [make "install"]
remove: [["ocamlfind" "remove" "sha"]]
build: [["jbuilder" "build" "-p" name "-j" jobs]]

depends: ["ocamlfind"]
build-test: [["jbuilder" "runtest" "-p" name]]

available: [ ocaml-version >= "4.02.0" ]
depends: [
"jbuilder" {build & >= "1.0+beta13"}
"ounit" {test}
]

available: [ ocaml-version >= "4.02.0" ]
5 changes: 0 additions & 5 deletions sha1.ml
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@ let substring s ofs len =
unsafe_update_substring ctx s ofs len;
finalize ctx

let buffer buf =
let ctx = init () in
update_buffer ctx buf;
finalize ctx

let channel chan len =
let ctx = init ()
and buf = Bytes.create blksize in
Expand Down
1 change: 1 addition & 0 deletions sha1_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ typedef SSIZE_T ssize_t;
#include <unistd.h>
#endif
#include <fcntl.h>
#include <string.h>
#include "sha1.h"

static inline int sha1_file(char *filename, sha1_digest *digest)
Expand Down
File renamed without changes.
2 changes: 0 additions & 2 deletions sha256.ml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ let update_substring ctx s ofs len =
let update_string ctx s =
unsafe_update_substring ctx s 0 (String.length s)

external update_bigarray: ctx -> (char, Bigarray.int8_unsigned_elt, Bigarray.c_layout) Bigarray.Array1.t -> unit = "stub_sha256_update_bigarray"

let string s =
let ctx = init () in
unsafe_update_substring ctx s 0 (String.length s);
Expand Down
1 change: 1 addition & 0 deletions sha256_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ typedef SSIZE_T ssize_t;
#include <unistd.h>
#endif
#include <fcntl.h>
#include <string.h>
#include "sha256.h"

static inline int sha256_file(char *filename, sha256_digest *digest)
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions sha512_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ typedef SSIZE_T ssize_t;
#include <unistd.h>
#endif
#include <fcntl.h>
#include <string.h>
#include "sha512.h"

static inline int sha512_file(char *filename, sha512_digest *digest)
Expand Down
File renamed without changes.
16 changes: 16 additions & 0 deletions test/jbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
(jbuild_version 1)

(executable
((name shasum)
(modules (shasum))
(libraries (sha))))

(executable
((name shatest)
(modules (shatest))
(libraries (sha oUnit))))

(alias
((name runtest)
(deps (shatest.exe sample.txt))
(action (run ${<}))))
1 change: 1 addition & 0 deletions test/sample.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The quick brown fox jumps over the lazy dog
File renamed without changes.
39 changes: 21 additions & 18 deletions sha.test.ml → test/shatest.ml
Original file line number Diff line number Diff line change
Expand Up @@ -18,53 +18,56 @@

open OUnit

let cog = "The quick brown fox jumps over the lazy cog"
let dog = "The quick brown fox jumps over the lazy dog"

let ex_strings_sha1 = [
("",
"da39a3ee5e6b4b0d3255bfef95601890afd80709");
("The quick brown fox jumps over the lazy cog",
(cog,
"de9f2c7fd25e1b3afad3e85a0bd17d9b100db4b3");
("The quick brown fox jumps over the lazy dog",
(dog,
"2fd4e1c67a2d28fced849ee1bb76e7391b93eb12"); ]

let ex_strings_sha256 = [
("",
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
("The quick brown fox jumps over the lazy cog",
(cog,
"e4c4d8f3bf76b692de791a173e05321150f7a345b46484fe427f6acc7ecc81be");
("The quick brown fox jumps over the lazy dog",
(dog,
"d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592"); ]

let ex_strings_sha512 = [
("",
"cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e");
("The quick brown fox jumps over the lazy cog",
(cog,
"3eeee1d0e11733ef152a6c29503b3ae20c4f1f3cda4cb26f1bc1a41f91c7fe4ab3bd86494049e201c4bd5155f31ecb7a3c8606843c4cc8dfcab7da11c8ae5045");
("The quick brown fox jumps over the lazy dog",
(dog,
"07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb642e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6"); ]


let ex_files_sha1 =
[ ("README",
"ebd97ba45cd1668d242d98522dc0004bb70df4a8") ]
[ ("sample.txt",
"2fd4e1c67a2d28fced849ee1bb76e7391b93eb12") ]

let ex_files_sha256 =
[ ("README",
"7f8213da9acc755d57f3650827176dcdf2f101ea486d5fa357e705d728480878") ]
[ ("sample.txt",
"d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592") ]

let ex_files_sha512 =
[ ("README",
"da82cac10002a838aceedc7910b76735ec87b2dce08755680910b8ff287c0f48a1910989f8e3a94f9cb5a391e66d811d190bc3568135514229051c06c6e93f6e") ]
[ ("sample.txt",
"07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb642e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6") ]

let ex_channels_sha1 =
[ ("sha.test.ml", "e13052afa4916d56994378f847f157596f9638a2") ]
[ ("sample.txt", "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12") ]

let ex_channels_sha256 =
[ ("sha.test.ml",
"51598a80bbec8fdcb32c3d180c3e806bf09eb0f5b5bb6f51031a78f46c83caef") ]
[ ("sample.txt",
"d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592") ]

let ex_channels_sha512 =
[ ("sha.test.ml",
"2f8e603774643ce152620e1dd94601393e78d33a3e6f67b86df2ef87eb4ad0cde72b481208eaa0249bbcf64072f2fb03b06abd006c5213c7546936ae9e9a1dc1") ]
[ ("sample.txt",
"07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb642e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6") ]

let stringfct_sha1 s = Sha1.to_hex (Sha1.string s)
let stringfct_sha256 s = Sha256.to_hex (Sha256.string s)
Expand All @@ -87,7 +90,7 @@ let test_file filefct arr _ =
let test_channel channelfct arr _ =
List.iter (fun (c,r) ->
let chan = open_in_bin c in
let digest = channelfct chan 20 in
let digest = channelfct chan (String.length dog) in
close_in chan;
assert_equal r digest) arr

Expand Down