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

merge 4.0 branch #249

Draft
wants to merge 51 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
5ba710a
Add oneof example
Lupus Jan 17, 2024
9dbbe58
Wrap oneof fields into options
Lupus Jan 17, 2024
29a8921
Drop special-casing for messages with only oneof
Lupus Jan 19, 2024
ed08a32
enable CI
c-cube Jan 29, 2024
90c7c06
format check in ci (#237)
c-cube Feb 10, 2024
b7a1bd7
Expose one-of per-constructor options
Lupus Jan 22, 2024
52d9ee7
Stop special-casing option parsing in field_option
Lupus Feb 26, 2024
2df18e9
Use adequate types to represent option names
Lupus Feb 26, 2024
efb3894
Destructure option names to nested messages
Lupus Feb 28, 2024
b1002fe
WIP: Introduce Pb_raw_option
Lupus Mar 1, 2024
8c3dbd5
Proper compilation of options
Lupus Mar 2, 2024
ba200df
Remove `merge` from Pb_option
Lupus Mar 2, 2024
2e42b6a
Make Pb_raw_option.merge actually override options
Lupus Mar 2, 2024
f6df3fd
Add documentation
Lupus Mar 2, 2024
733f4c9
Even moar documentation
Lupus Mar 2, 2024
86dd22c
Support OCaml 4.08 (missing List.equal)
Lupus Mar 8, 2024
5f3beb0
String.starts_with...
Lupus Mar 8, 2024
04b733b
Merge pull request #242 from Lupus/more-compliant-options-parsing
c-cube Apr 1, 2024
0b2a400
Attempt to repro bug with tests
Lupus Jun 7, 2024
9bf7fb0
Fix duplicate packed options bug
Lupus Jun 7, 2024
13f9559
Merge pull request #247 from Lupus/fix-pb-options-packed-regression
c-cube Jun 7, 2024
7255b2f
silence warning 44 in generated code
c-cube Feb 7, 2024
7be41a6
update tests
c-cube Feb 7, 2024
f7497f7
test: add bytecode-only tests
c-cube Feb 13, 2024
f4a4caa
fix segfault in pbrt in bytecode
c-cube Feb 13, 2024
0589de9
Add oneof example
Lupus Jan 17, 2024
083ca2d
test: fix integration tests
c-cube Mar 22, 2024
83462a3
fix tests
c-cube Apr 1, 2024
92082fc
fix tests
c-cube Apr 1, 2024
694d2d2
fix tests
c-cube Apr 1, 2024
0aecfaf
dune
c-cube Apr 1, 2024
4d512db
stronger CI
c-cube Apr 1, 2024
3a33f0c
attempt fix
c-cube Apr 1, 2024
19146a4
CI
c-cube Apr 1, 2024
8bf4391
CI: enable submodules
c-cube Apr 1, 2024
36cbcac
wip
c-cube Apr 2, 2024
3e262ed
fix tests
c-cube Jun 5, 2024
45a3e21
fix test
c-cube Jun 5, 2024
4906736
fix build
c-cube Jun 7, 2024
b1cf448
format
c-cube Jun 7, 2024
f046012
vendor google .proto files for CI
c-cube Jun 7, 2024
2bb3600
deps
c-cube Jun 7, 2024
b29b9b4
format
c-cube Jun 7, 2024
8722e8d
bumpity bump
c-cube Jun 7, 2024
ee1a30d
fix: crazy link error fixed by moving `-lprotobuf` at the end:
c-cube Jun 7, 2024
9672ed0
chore: try to reinforce CI by running integration tests in docker
c-cube Jun 7, 2024
274fc93
fix: turns out we don't need to vendor google's protos!
c-cube Jun 7, 2024
2e36f5d
Merge pull request #243 from mransan/simon/fix-integration-tests
c-cube Jun 7, 2024
3e2b2a9
Fix compilation of option_processing.proto
Lupus Jun 11, 2024
53bcc47
Parsing of Pb_options according to protobuf schema
Lupus Jun 11, 2024
167505e
Merge pull request #245 from Lupus/generate-pb-option-parsers
c-cube Jun 11, 2024
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
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
_opam
_build
39 changes: 34 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ on:
push:
branches:
- master
- br-4.0
pull_request:
jobs:
run:
Expand All @@ -14,20 +15,48 @@ jobs:
#- macos-latest
#- windows-latest
ocaml-compiler:
- 4.08.x
- 4.12.x
- '4.08'
- '4.14'
- '5.1'
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
#with:
# submodules: true
with:
submodules: true
- uses: ocaml/setup-ocaml@v2
with:
ocaml-compiler: ${{ matrix.ocaml-compiler }}
allow-prerelease-opam: true
dune-cache: true
#- run: sudo apt install protobuf-compiler libprotobuf-dev
- run: opam pin -n .
- run: opam depext -yt ocaml-protoc
- run: opam install -t . --deps-only
- run: opam exec -- dune build @install
- run: opam exec -- dune runtest
#- run: opam exec -- make integration

integration:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
submodules: true
- run: docker build -f dep/Dockerfile . --target=base
- run: docker build -f dep/Dockerfile . --target=run-the-damn-build

format:
name: format
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
#with:
# submodules: true
- uses: ocaml/setup-ocaml@v2
with:
ocaml-compiler: 5.1
allow-prerelease-opam: true
dune-cache: true
#- run: sudo apt install protobuf-compiler libprotobuf-dev
- run: opam install ocamlformat.0.24.1
- run: opam exec -- dune build @fmt --auto-promote
- run: git diff -q
23 changes: 13 additions & 10 deletions Makefile.test
Original file line number Diff line number Diff line change
Expand Up @@ -32,39 +32,42 @@ tests:

# location of where the Google protoc compiler is installed
PB_INSTALL ?= /usr/
PB_HINC = $(PB_INSTALL)/include
PB_LINC = $(PB_INSTALL)/lib
PB_HINC ?= $(PB_INSTALL)/include
PB_LINC ?= $(PB_INSTALL)/lib
PROTOC = $(PB_INSTALL)/bin/protoc

PB_HINC_I = $(addprefix -I, $(PB_HINC))
PB_LINC_L = $(addprefix -L, $(PB_LINC))

export LD_LIBRARY_PATH=$(PB_LINC)

$(OCAMLOPTIONS_HINC)/ocamloptions.pb.cc: $(OCAMLOPTIONS_HINC)/ocamloptions.proto
cd $(OCAMLOPTIONS_HINC) && \
$(PROTOC) --cpp_out ./ -I ./ -I $(abspath $(PB_HINC)) ocamloptions.proto
$(PROTOC) --cpp_out ./ -I ./ $(PB_HINC_I) ocamloptions.proto

%_cpp.tsk: %_cpp.cpp %.pb.cc $(OCAMLOPTIONS_HINC)/ocamloptions.pb.cc
$(CXX) $(CPPFLAGS) $(LDFLAGS) \
-I ./ -I $(INTEGRATION_TESTS_DIR) -I $(OCAMLOPTIONS_HINC) -I $(PB_HINC) \
-L $(PB_LINC) -l protobuf \
-I ./ -I $(INTEGRATION_TESTS_DIR) -I $(OCAMLOPTIONS_HINC) $(PB_HINC_L) \
$(PB_LINC_L) $(PB_HINC_I) \
$? \
-o $@
-o $@ -l protobuf

$(INTEGRATION_TESTS_DIR)/test10_cpp.tsk: \
$(INTEGRATION_TESTS_DIR)/test10_cpp.cpp \
$(INTEGRATION_TESTS_DIR)/test10.pb.cc \
$(INTEGRATION_TESTS_DIR)/test09.pb.cc
$(CXX) $(CPPFLAGS) $(LDFLAGS) \
-I ./ -I $(INTEGRATION_TESTS_DIR) -I $(PB_HINC) \
-L $(PB_LINC) -l protobuf \
-I ./ -I $(INTEGRATION_TESTS_DIR) \
$(PB_LINC_L) $(PB_HINC_I) \
$? \
-o $@
-o $@ -l protobuf

.SECONDARY:

%.pb.cc: %.proto
$(PROTOC) \
--cpp_out $(INTEGRATION_TESTS_DIR) \
-I $(PB_HINC) -I $(OCAMLOPTIONS_HINC) -I $(INTEGRATION_TESTS_DIR) \
$(PB_HINC_I) -I $(OCAMLOPTIONS_HINC) -I $(INTEGRATION_TESTS_DIR) \
$<


Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
compiler**. See [here](https://github.com/mransan/bs-protobuf-demo) for complete example.



### A simple example

> This example generates the binary encoding, if you are more interested in a **JavaScript**
Expand Down
1 change: 0 additions & 1 deletion benchs/bin/dune
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

(executable
(name run)
(ocamlopt_flags :standard -inline 100)
Expand Down
5 changes: 4 additions & 1 deletion benchs/dune
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
(executable
(name benchs)
(ocamlopt_flags :standard -inline 100)
(foreign_stubs (language c) (flags :standard -std=c99 -O2) (names stubs))
(foreign_stubs
(language c)
(flags :standard -std=c99 -O2)
(names stubs))
(libraries ocaml-protoc benchmark))

(rule
Expand Down
22 changes: 22 additions & 0 deletions dep/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM ocaml/opam:debian-ocaml-4.14 AS base

WORKDIR /build/
RUN sudo chown -R opam: /build/
RUN sudo apt install protobuf-compiler libprotobuf-dev -y
COPY --chown=opam ./src/ ./src/
COPY --chown=opam pbrt.opam pbrt_services.opam pbrt_yojson.opam ocaml-protoc.opam \
Makefile Makefile.test dune-project dune .ocamlformat \
.ocamlformat-ignore ./

RUN opam pin -n .
RUN opam depext -yt ocaml-protoc
RUN opam install -t . --deps-only

FROM base AS run-the-damn-build
RUN opam exec -- dune build @install
RUN opam exec -- dune runtest

# integration tests
RUN opam install ppx_deriving -y
RUN opam exec -- make clean build integration PB_LINC=/usr/lib/x86_64-linux-gnu/ PB_HINC=/usr/include/

11 changes: 6 additions & 5 deletions src/compilerlib/dune
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
pb_codegen_make pb_codegen_encode_binary pb_codegen_encode_bs
pb_codegen_encode_yojson pb_codegen_formatting pb_codegen_ocaml_type_dump
pb_codegen_ocaml_type pb_codegen_pp pb_codegen_plugin pb_codegen_types
pb_codegen_services pb_codegen_util pb_exception pb_field_type pb_location
pb_logger pb_option pb_parsing pb_parsing_lexer pb_parsing_parser
pb_parsing_parse_tree pb_parsing_util pb_typing_graph pb_typing
pb_typing_recursion pb_typing_resolution pb_typing_type_tree
pb_typing_util pb_typing_validation pb_util pb_format_util)
Pb_codegen_decode_pb_options pb_codegen_services pb_codegen_util
pb_exception pb_field_type pb_location pb_logger pb_option pb_raw_option
pb_parsing pb_parsing_lexer pb_parsing_parser pb_parsing_parse_tree
pb_parsing_util pb_typing_graph pb_typing pb_typing_recursion
pb_typing_resolution pb_typing_type_tree pb_typing_util
pb_typing_validation pb_util pb_format_util)
(libraries stdlib-shims))
4 changes: 2 additions & 2 deletions src/compilerlib/pb_codegen_all.ml
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ let new_ocaml_mod ~proto_file_options ~proto_file_name () : ocaml_mod =
let self = { ml = F.empty_scope (); mli = F.empty_scope () } in

let print_ppx sc =
match Pb_option.get proto_file_options "ocaml_file_ppx" with
match Pb_raw_option.get_ext proto_file_options "ocaml_file_ppx" with
| None -> ()
| Some Pb_option.(Scalar_value (Constant_string s)) ->
F.linep sc "[@@@%s]" s
| _ -> E.invalid_ppx_extension_option proto_file_name
in

(* write preludes *)
F.line self.ml "[@@@ocaml.warning \"-27-30-39\"]";
F.line self.ml "[@@@ocaml.warning \"-27-30-39-44\"]";
F.empty_line self.ml;
print_ppx self.ml;
F.empty_line self.mli;
Expand Down
2 changes: 1 addition & 1 deletion src/compilerlib/pb_codegen_all.mli
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type ocaml_mod = {
val codegen :
Ot.proto ->
generate_make:bool ->
proto_file_options:Pb_option.set ->
proto_file_options:Pb_raw_option.set ->
proto_file_name:string ->
services:bool ->
Plugin.t list ->
Expand Down
39 changes: 13 additions & 26 deletions src/compilerlib/pb_codegen_backend.ml
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ let encoding_info_of_field_type ~all_types field_type : Ot.payload_kind =
let encoding_of_field ~all_types (field : (Pb_field_type.resolved, 'a) Tt.field)
=
let packed =
match Typing_util.field_option field "packed" with
match Typing_util.field_option field (Pb_option.Simple_name "packed") with
| Some Pb_option.(Scalar_value (Constant_bool x)) -> x
| Some _ -> E.invalid_packed_option (Typing_util.field_name field)
| None -> false
Expand All @@ -209,34 +209,34 @@ let encoding_of_field ~all_types (field : (Pb_field_type.resolved, 'a) Tt.field)
let compile_field_type ~unsigned_tag ~(all_types : _ Tt.proto_type list)
file_options field_options file_name field_type : Ot.field_type =
let ocaml_type =
match Pb_option.get field_options "ocaml_type" with
match Pb_option.get_ext field_options "ocaml_type" with
| Some Pb_option.(Scalar_value (Constant_literal "int_t")) -> `Int_t
| _ -> `None
in

let int32_type =
match Pb_option.get file_options "int32_type" with
match Pb_option.get_ext file_options "int32_type" with
| Some Pb_option.(Scalar_value (Pb_option.Constant_literal "int_t")) ->
Ot.(Ft_basic_type Bt_int)
| _ -> Ot.(Ft_basic_type Bt_int32)
in

let uint32_type =
match Pb_option.get file_options "int32_type" with
match Pb_option.get_ext file_options "int32_type" with
| Some Pb_option.(Scalar_value (Constant_literal "int_t")) ->
Ot.(Ft_basic_type Bt_int)
| _ -> Ot.(Ft_basic_type Bt_uint32)
in

let int64_type =
match Pb_option.get file_options "int64_type" with
match Pb_option.get_ext file_options "int64_type" with
| Some Pb_option.(Scalar_value (Constant_literal "int_t")) ->
Ot.(Ft_basic_type Bt_int)
| _ -> Ot.(Ft_basic_type Bt_int64)
in

let uint64_type =
match Pb_option.get file_options "int64_type" with
match Pb_option.get_ext file_options "int64_type" with
| Some Pb_option.(Scalar_value (Constant_literal "int_t")) ->
Ot.(Ft_basic_type Bt_int)
| _ -> Ot.(Ft_basic_type Bt_uint64)
Expand Down Expand Up @@ -289,13 +289,13 @@ let compile_field_type ~unsigned_tag ~(all_types : _ Tt.proto_type list)
| `User_defined id, _ -> user_defined_type_of_id ~all_types ~file_name id

let is_mutable ?field_name field_options =
match Pb_option.get field_options "ocaml_mutable" with
match Pb_option.get_ext field_options "ocaml_mutable" with
| Some Pb_option.(Scalar_value (Constant_bool v)) -> v
| Some _ -> Pb_exception.invalid_mutable_option ?field_name ()
| None -> false

let ocaml_container field_options =
match Pb_option.get field_options "ocaml_container" with
match Pb_option.get_ext field_options "ocaml_container" with
| None -> None
| Some Pb_option.(Scalar_value (Constant_literal container_name)) ->
Some container_name
Expand Down Expand Up @@ -328,6 +328,7 @@ let variant_of_oneof ?include_oneof_name ~outer_message_names ~unsigned_tag
(match field_type with
| Ft_unit -> Vct_nullary
| _ -> Vct_non_nullary_constructor field_type);
vc_options = field.field_options;
})
oneof_field.Tt.oneof_fields
in
Expand Down Expand Up @@ -370,7 +371,7 @@ let process_all_types_ppx_extension file_name file_options
match type_level_ppx_extension with
| Some x -> Some x
| None ->
Pb_option.get file_options "ocaml_all_types_ppx"
Pb_option.get_ext file_options "ocaml_all_types_ppx"
|> string_of_string_option file_name

let compile_message ~(unsigned_tag : bool) (file_options : Pb_option.set)
Expand All @@ -387,7 +388,8 @@ let compile_message ~(unsigned_tag : bool) (file_options : Pb_option.set)
let { Tt.message_names; _ } = scope in

let type_level_ppx_extension =
Typing_util.message_option message "ocaml_type_ppx"
Typing_util.message_option message
(Pb_option.Extension_name "ocaml_type_ppx")
|> string_of_string_option message_name
|> process_all_types_ppx_extension file_name file_options
in
Expand All @@ -411,21 +413,6 @@ let compile_message ~(unsigned_tag : bool) (file_options : Pb_option.set)
}
in
[ type_ ]
| Tt.Message_oneof_field f :: [] ->
let outer_message_names = message_names @ [ message_name ] in
let variant =
variant_of_oneof ~unsigned_tag ~outer_message_names ~all_types
file_options file_name f
in
[
Ot.
{
module_prefix;
spec = Variant variant;
type_level_ppx_extension;
type_options = message_options;
};
]
| _ ->
let variants, fields =
List.fold_left
Expand Down Expand Up @@ -647,7 +634,7 @@ let compile_enum file_options file_name scope enum =
in

let type_level_ppx_extension =
Typing_util.enum_option enum "ocaml_enum_ppx"
Typing_util.enum_option enum (Pb_option.Extension_name "ocaml_enum_ppx")
|> string_of_string_option enum_name
|> process_all_types_ppx_extension file_name file_options
in
Expand Down
6 changes: 4 additions & 2 deletions src/compilerlib/pb_codegen_decode_binary.ml
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ let gen_rft_variant sc r_name rf_label { Ot.v_constructors; _ } =
vc_field_type;
vc_encoding_number;
vc_payload_kind = pk;
vc_options = _;
} =
variant_constructor
in
Expand All @@ -169,9 +170,9 @@ let gen_rft_variant sc r_name rf_label { Ot.v_constructors; _ } =
match vc_field_type with
| Ot.Vct_nullary ->
F.line sc "Pbrt.Decoder.empty_nested d;";
F.linep sc "v.%s <- %s;" rf_label vc_constructor
F.linep sc "v.%s <- Some %s;" rf_label vc_constructor
| Ot.Vct_non_nullary_constructor field_type ->
F.linep sc "v.%s <- %s (%s);" rf_label vc_constructor
F.linep sc "v.%s <- Some (%s (%s));" rf_label vc_constructor
(decode_field_expression field_type pk)))
v_constructors

Expand Down Expand Up @@ -281,6 +282,7 @@ let gen_variant ?and_ { Ot.v_name; v_constructors } sc =
vc_field_type;
vc_encoding_number;
vc_payload_kind = pk;
vc_options = _;
} =
variant_constructor
in
Expand Down
6 changes: 4 additions & 2 deletions src/compilerlib/pb_codegen_decode_bs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,14 @@ let gen_rft_variant sc ~r_name ~rf_label { Ot.v_constructors; _ } =

match vc_field_type with
| Ot.Vct_nullary ->
F.linep sc "| \"%s\" -> v.%s <- %s" json_label rf_label vc_constructor
F.linep sc "| \"%s\" -> v.%s <- Some %s" json_label rf_label
vc_constructor
| Ot.Vct_non_nullary_constructor field_type ->
let value_expression = value_expression ~r_name ~rf_label field_type in
F.linep sc "| \"%s\" -> " json_label;
F.linep sc " let json = Js.Dict.unsafeGet json \"%s\" in" json_label;
F.linep sc " v.%s <- %s (%s)" rf_label vc_constructor value_expression)
F.linep sc " v.%s <- Some (%s (%s))" rf_label vc_constructor
value_expression)
v_constructors

(* Generate decode function for a record *)
Expand Down
1 change: 0 additions & 1 deletion src/compilerlib/pb_codegen_decode_bs.mli
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

include Pb_codegen_plugin.S

val plugin : Pb_codegen_plugin.t
Loading
Loading