Skip to content

Commit

Permalink
Merge pull request #589 from tleedjarv/ocaml-compat
Browse files Browse the repository at this point in the history
Restore compatibility with older OCaml compilers
  • Loading branch information
gdt authored Oct 21, 2021
2 parents a39c572 + a30b5a3 commit b74f367
Show file tree
Hide file tree
Showing 20 changed files with 534 additions and 39 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/CICD.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,40 @@ jobs:
- { os: macos-10.15 , ocaml-version: 4.09.1 }
- { os: macos-10.15 , ocaml-version: 4.08.1 }
- { os: macos-10.15 , ocaml-version: 4.07.1 }
- { os: macos-10.15 , ocaml-version: 4.06.1 }
- { os: macos-10.15 , ocaml-version: 4.05.0 }
- { os: macos-10.15 , ocaml-version: 4.04.2 }
- { os: macos-10.15 , ocaml-version: 4.03.0 }
- { os: macos-10.15 , ocaml-version: 4.02.3 }
- { os: macos-10.15 , ocaml-version: 4.01.0 }
- { os: ubuntu-18.04 , ocaml-version: 4.12.0 }
- { os: ubuntu-18.04 , ocaml-version: 4.11.2 }
- { os: ubuntu-18.04 , ocaml-version: 4.10.2 }
- { os: ubuntu-18.04 , ocaml-version: 4.10.0+musl+static+flambda }
- { os: ubuntu-18.04 , ocaml-version: 4.09.1 }
- { os: ubuntu-18.04 , ocaml-version: 4.08.1 }
- { os: ubuntu-18.04 , ocaml-version: 4.07.1 }
- { os: ubuntu-18.04 , ocaml-version: 4.06.1 }
- { os: ubuntu-18.04 , ocaml-version: 4.05.0 }
- { os: ubuntu-18.04 , ocaml-version: 4.04.2 }
- { os: ubuntu-18.04 , ocaml-version: 4.03.0 }
- { os: ubuntu-18.04 , ocaml-version: 4.02.3 }
- { os: ubuntu-18.04 , ocaml-version: 4.01.0 }
- { os: windows-latest , ocaml-version: 4.12.0+mingw64c }
- { os: windows-latest , ocaml-version: 4.11.2+mingw64c }
- { os: windows-latest , ocaml-version: 4.10.2+mingw64c }
- { os: windows-latest , ocaml-version: 4.10.2+mingw32c }
- { os: windows-latest , ocaml-version: 4.09.1+mingw64c }
- { os: windows-latest , ocaml-version: 4.08.1+mingw64c }
- { os: windows-latest , ocaml-version: 4.07.1+mingw64c }
- { os: windows-latest , ocaml-version: 4.06.1+mingw64c }
- { os: windows-latest , ocaml-version: 4.06.1+mingw32c }
- { os: windows-latest , ocaml-version: 4.05.0+mingw64c }
- { os: windows-latest , ocaml-version: 4.04.2+mingw32c }
- { os: windows-latest , ocaml-version: 4.03.0+mingw32c }
- { os: windows-latest , ocaml-version: 4.02.3+mingw64c }
- { os: windows-latest , ocaml-version: 4.02.3+mingw32c }
- { os: windows-latest , ocaml-version: 4.01.0+mingw32c }

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

Expand Down
11 changes: 11 additions & 0 deletions src/.depend
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ common.cmi : \
name.cmi \
fspath.cmi \
fileinfo.cmi
compat402.cmo :
compat402.cmx :
compat403.cmo :
compat403.cmx :
compat408.cmo : \
compat408.cmi
compat408.cmx : \
compat408.cmi
compat408.cmi :
configure.cmo :
configure.cmx :
copy.cmo : \
xferhint.cmi \
uutil.cmi \
Expand Down
27 changes: 22 additions & 5 deletions src/Makefile.OCaml
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,15 @@ else
endif
endif

ifeq ($(NATIVE), true)
COMPATCAMLFLAGS=$(subst .cmo,.cmx, $(shell ocaml $(CWD)/configure.ml))
COMPATOCAMLOBJS=$(subst .cmo,.cmx, $(subst .cma,.cmxa, $(shell ocaml $(CWD)/configure.ml objs)))
else
COMPATCAMLFLAGS=$(shell ocaml $(CWD)/configure.ml)
COMPATOCAMLOBJS=$(shell ocaml $(CWD)/configure.ml objs)
endif
OCAMLOBJS+=$(COMPATOCAMLOBJS)

.PHONY: buildexecutable
buildexecutable::
@echo NATIVE = $(NATIVE)
Expand Down Expand Up @@ -420,17 +429,25 @@ win32rc/unison.res.lib: win32rc/unison.res

%.cmi : %.mli
@echo "$(CAMLC): $< ---> $@"
$(CAMLC) $(CAMLFLAGS) -c $(CWD)/$<
$(CAMLC) $(CAMLFLAGS) $(COMPATCAMLFLAGS) -c $(CWD)/$<

fswatch.cmi : ubase/prefs.cmi

%.cmo: %.ml
compat%.cmo: compat%.ml
@echo "$(OCAMLC): $< ---> $@"
$(OCAMLC) $(CAMLFLAGS) -c $(CWD)/$<
$(OCAMLC) $(COMPATCAMLFLAGS) -c $(CWD)/$<

%.cmo: %.ml $(COMPATOCAMLOBJS)
@echo "$(OCAMLC): $< ---> $@"
$(OCAMLC) $(CAMLFLAGS) $(COMPATCAMLFLAGS) -c $(CWD)/$<

compat%.cmx: compat%.ml
@echo "$(OCAMLOPT): $< ---> $@"
$(OCAMLOPT) $(COMPATCAMLFLAGS) -c $(CWD)/$<

%.cmx: %.ml
%.cmx: %.ml $(COMPATOCAMLOBJS)
@echo "$(OCAMLOPT): $< ---> $@"
$(OCAMLOPT) $(CAMLFLAGS) -c $(CWD)/$<
$(OCAMLOPT) $(CAMLFLAGS) $(COMPATCAMLFLAGS) -c $(CWD)/$<

%.o %.obj: %.c
@echo "$(CAMLC): $< ---> $@"
Expand Down
6 changes: 3 additions & 3 deletions src/bytearray.ml
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ let unsafe_blit_to_string a i s j l =
*)

external unsafe_blit_from_string : string -> int -> t -> int -> int -> unit
= "ml_blit_string_to_bigarray" [@@noalloc]
= "ml_blit_string_to_bigarray" "noalloc"

external unsafe_blit_from_bytes : bytes -> int -> t -> int -> int -> unit
= "ml_blit_bytes_to_bigarray" [@@noalloc]
= "ml_blit_bytes_to_bigarray" "noalloc"

external unsafe_blit_to_bytes : t -> int -> bytes -> int -> int -> unit
= "ml_blit_bigarray_to_bytes" [@@noalloc]
= "ml_blit_bigarray_to_bytes" "noalloc"

let to_string a =
let l = length a in
Expand Down
40 changes: 40 additions & 0 deletions src/compat402.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
(* Functions added in OCaml 4.02 *)

type bytes = string

let output_bytes = output_string
let output_substring = output

module Bytes = struct
include String

let of_string x = x
let to_string x = x

let sub_string = sub
end

module Buffer = struct
include Buffer

let add_subbytes = add_substring
end

module Digest = struct
include Digest

let bytes = string
let subbytes = substring
end

module Marshal = struct
include Marshal

let from_bytes = from_string
end

module Unix = struct
include Unix

let write_substring = write
end
20 changes: 20 additions & 0 deletions src/compat403.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
(* Functions added in OCaml 4.03 *)

module String = struct
include String

let lowercase_ascii = lowercase
let capitalize_ascii = capitalize
end

module Unix = struct
include Unix

let has_symlink () = not Sys.win32
end

module Sys = struct
include Sys

let int_size = word_size - 1
end
102 changes: 102 additions & 0 deletions src/compat408.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
module Bytes = struct

include Bytes

(* The following code is taken from OCaml sources.
Authors of the code snippet: Alain Frisch and Daniel Bünzli *)

(**************************************************************************)
(* *)
(* OCaml *)
(* *)
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
(* *)
(* Copyright 1996 Institut National de Recherche en Informatique et *)
(* en Automatique. *)
(* *)
(* All rights reserved. This file is distributed under the terms of *)
(* the GNU Lesser General Public License version 2.1, with the *)
(* special exception on linking described in the file LICENSE. *)
(* *)
(**************************************************************************)

(** {6 Binary encoding/decoding of integers} *)

external get_uint8 : bytes -> int -> int = "%string_safe_get"
external get_uint16_ne : bytes -> int -> int = "%caml_string_get16"
external get_int32_ne : bytes -> int -> int32 = "%caml_string_get32"
external get_int64_ne : bytes -> int -> int64 = "%caml_string_get64"
external set_int8 : bytes -> int -> int -> unit = "%string_safe_set"
external set_int16_ne : bytes -> int -> int -> unit = "%caml_string_set16"
external set_int32_ne : bytes -> int -> int32 -> unit = "%caml_string_set32"
external set_int64_ne : bytes -> int -> int64 -> unit = "%caml_string_set64"
external swap16 : int -> int = "%bswap16"
external swap32 : int32 -> int32 = "%bswap_int32"
external swap64 : int64 -> int64 = "%bswap_int64"

let get_int8 b i =
((get_uint8 b i) lsl (Sys.int_size - 8)) asr (Sys.int_size - 8)

let get_uint16_le b i =
if Sys.big_endian then swap16 (get_uint16_ne b i)
else get_uint16_ne b i

let get_uint16_be b i =
if not Sys.big_endian then swap16 (get_uint16_ne b i)
else get_uint16_ne b i

let get_int16_ne b i =
((get_uint16_ne b i) lsl (Sys.int_size - 16)) asr (Sys.int_size - 16)

let get_int16_le b i =
((get_uint16_le b i) lsl (Sys.int_size - 16)) asr (Sys.int_size - 16)

let get_int16_be b i =
((get_uint16_be b i) lsl (Sys.int_size - 16)) asr (Sys.int_size - 16)

let get_int32_le b i =
if Sys.big_endian then swap32 (get_int32_ne b i)
else get_int32_ne b i

let get_int32_be b i =
if not Sys.big_endian then swap32 (get_int32_ne b i)
else get_int32_ne b i

let get_int64_le b i =
if Sys.big_endian then swap64 (get_int64_ne b i)
else get_int64_ne b i

let get_int64_be b i =
if not Sys.big_endian then swap64 (get_int64_ne b i)
else get_int64_ne b i

let set_int16_le b i x =
if Sys.big_endian then set_int16_ne b i (swap16 x)
else set_int16_ne b i x

let set_int16_be b i x =
if not Sys.big_endian then set_int16_ne b i (swap16 x)
else set_int16_ne b i x

let set_int32_le b i x =
if Sys.big_endian then set_int32_ne b i (swap32 x)
else set_int32_ne b i x

let set_int32_be b i x =
if not Sys.big_endian then set_int32_ne b i (swap32 x)
else set_int32_ne b i x

let set_int64_le b i x =
if Sys.big_endian then set_int64_ne b i (swap64 x)
else set_int64_ne b i x

let set_int64_be b i x =
if not Sys.big_endian then set_int64_ne b i (swap64 x)
else set_int64_ne b i x

let set_uint8 = set_int8
let set_uint16_ne = set_int16_ne
let set_uint16_be = set_int16_be
let set_uint16_le = set_int16_le

end
Loading

0 comments on commit b74f367

Please sign in to comment.