forked from elixir-protobuf/protobuf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mix.exs
292 lines (252 loc) · 9.19 KB
/
mix.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
defmodule Protobuf.Mixfile do
use Mix.Project
@source_url "https://github.com/elixir-protobuf/protobuf"
@version "0.11.0"
@description "A pure Elixir implementation of Google Protobuf."
def project do
[
app: :protobuf,
version: @version,
elixir: "~> 1.6",
build_embedded: Mix.env() == :prod,
start_permanent: Mix.env() == :prod,
dialyzer: [plt_add_apps: [:mix, :jason], flags: [:no_improper_lists]],
elixirc_paths: elixirc_paths(Mix.env()),
test_coverage: [tool: ExCoveralls],
preferred_cli_env: [
coveralls: :test,
"coveralls.html": :test,
conformance_test: :test,
"escript.build": :prod
],
deps: deps(),
escript: escript(),
description: @description,
package: package(),
docs: docs(),
aliases: aliases()
]
end
def application do
[
extra_applications: [:logger, :eex]
]
end
defp elixirc_paths(:test), do: ["lib", "conformance", "test/support", "generated"]
defp elixirc_paths(_env), do: ["lib"]
defp deps do
[
{:jason, "~> 1.2", optional: true},
{:dialyxir, "~> 1.0", only: [:dev, :test], runtime: false},
{:credo, "~> 1.5", only: [:dev, :test], runtime: false},
{:ex_doc, ">= 0.0.0", only: :dev, runtime: false},
{:stream_data, "~> 0.5.0", only: [:dev, :test]},
{:excoveralls, "~> 0.14.4", only: :test},
{:google_protobuf,
github: "protocolbuffers/protobuf",
branch: "main",
submodules: true,
app: false,
compile: false,
only: [:dev, :test]}
]
end
defp escript do
[main_module: Protobuf.Protoc.CLI, name: "protoc-gen-elixir"]
end
defp package do
[
maintainers: ["Bing Han", "Andrea Leopardi"],
licenses: ["MIT"],
files: ~w(
mix.exs
README.md
lib/google
lib/protobuf
lib/*.ex
src
LICENSE
priv/templates
.formatter.exs
),
links: %{"GitHub" => @source_url}
]
end
defp docs do
[
extras: ["README.md"],
main: "readme",
source_url: @source_url,
source_ref: "v#{@version}"
]
end
defp aliases do
[
gen_bootstrap_protos: [&build_escript/1, &gen_bootstrap_protos/1],
gen_test_protos: [&build_escript/1, &create_generated_dir/1, &gen_test_protos/1],
test: [
&build_escript/1,
&create_generated_dir/1,
&gen_test_protos/1,
&gen_google_test_protos/1,
"test"
],
conformance_test: [
&build_escript/1,
&create_generated_dir/1,
&gen_google_test_protos/1,
&gen_conformance_protos/1,
fn _ -> Mix.Task.reenable("escript.build") end,
&build_escript/1,
&run_conformance_tests/1
],
gen_bench_protos: [&build_escript/1, &gen_bench_protos/1],
build_conformance_runner: &build_conformance_runner/1
]
end
# We need to do this in a separate shell because we want to compile "from scratch" when we do
# things like running tests, since we usually generated some .pb.ex files on the fly and stick
# them in a directory. That directory is in the elixirc_paths, but no files are there *before*
# we compile the project the first time to run escript.build. It's a chicken and egg problem.
# This way, we compile the project first to generate the escript and we do so in a subshell.
# Then, we generate the .pb.ex files. Then we actually run stuff (like "mix test"), so that
# compilation happens again "from scratch" and it picks up the generated files. This fixes at
# least one problem related to extensions, because extensions are discovered by looking at
# :application.get_key(app, :modules), and if modules are added after the first compilation and
# loading pass, then they don't get picked up in there.
# There is very likely a better way to do this, but this works for now.
defp build_escript(_args) do
# We wanna pass down MIX_ENV here because we want escript.build to happen in the same Mix
# environment of whoever is calling this function.
Mix.shell().cmd("mix escript.build", env: %{"MIX_ENV" => Atom.to_string(Mix.env())})
end
defp create_generated_dir(_args) do
File.mkdir_p!("generated")
end
# These files are necessary to bootstrap the protoc-gen-elixir plugin that we generate. They
# are committed to version control.
defp gen_bootstrap_protos(_args) do
proto_src = path_in_protobuf_source(["src"])
protoc!("-I \"#{proto_src}\"", "./lib", [
"google/protobuf/descriptor.proto",
"google/protobuf/compiler/plugin.proto"
])
protoc!("-I src -I \"#{proto_src}\"", "./lib", ["elixirpb.proto"])
end
defp gen_test_protos(_args) do
proto_src = path_in_protobuf_source(["src"])
protoc!(
"-I #{proto_src} -I src -I test/protobuf/protoc/proto",
"./generated",
["test/protobuf/protoc/proto/extension.proto"]
)
protoc!(
"-I test/protobuf/protoc/proto --elixir_opt=package_prefix=my",
"./generated",
["test/protobuf/protoc/proto/test.proto"]
)
protoc!(
"-I test/protobuf/protoc/proto -I #{path_in_protobuf_source("src")} " <>
"--elixir_opt=gen_descriptors=true --elixir_opt=include_docs=true",
"./generated",
["test/protobuf/protoc/proto/custom_options.proto"]
)
end
defp gen_bench_protos(_args) do
proto_bench = path_in_protobuf_source(["benchmarks"])
protoc!("-I \"#{proto_bench}\"", "./bench/lib", benchmark_proto_files())
end
defp gen_google_test_protos(_args) do
proto_root = path_in_protobuf_source(["src"])
files = ~w(
google/protobuf/any.proto
google/protobuf/duration.proto
google/protobuf/empty.proto
google/protobuf/field_mask.proto
google/protobuf/struct.proto
google/protobuf/timestamp.proto
google/protobuf/wrappers.proto
google/protobuf/test_messages_proto2.proto
google/protobuf/test_messages_proto3.proto
)
protoc!("-I \"#{proto_root}\"", "./generated", files)
end
defp gen_conformance_protos(_args) do
proto_src = path_in_protobuf_source(["conformance"])
protoc!("-I \"#{proto_src}\"", "./generated", ["conformance.proto"])
end
defp path_in_protobuf_source(path) do
protobuf_root = System.get_env("PROTOBUF_ROOT") || Mix.Project.deps_paths().google_protobuf
Path.join([protobuf_root | List.wrap(path)])
end
defp benchmark_proto_files do
[
"benchmarks.proto",
"datasets/google_message1/proto3/benchmark_message1_proto3.proto",
"datasets/google_message1/proto2/benchmark_message1_proto2.proto",
"datasets/google_message2/benchmark_message2.proto",
"datasets/google_message3/benchmark_message3.proto",
"datasets/google_message3/benchmark_message3_1.proto",
"datasets/google_message3/benchmark_message3_2.proto",
"datasets/google_message3/benchmark_message3_3.proto",
"datasets/google_message3/benchmark_message3_4.proto",
"datasets/google_message3/benchmark_message3_5.proto",
"datasets/google_message3/benchmark_message3_6.proto",
"datasets/google_message3/benchmark_message3_7.proto",
"datasets/google_message3/benchmark_message3_8.proto",
"datasets/google_message4/benchmark_message4.proto",
"datasets/google_message4/benchmark_message4_1.proto",
"datasets/google_message4/benchmark_message4_2.proto",
"datasets/google_message4/benchmark_message4_3.proto"
]
end
defp protoc!(args, elixir_out, files_to_generate)
when is_binary(args) and is_binary(elixir_out) and is_list(files_to_generate) do
args =
[
~s(protoc),
~s(--plugin=./protoc-gen-elixir),
~s(--elixir_out="#{elixir_out}"),
args
] ++ files_to_generate
cmd!(Enum.join(args, " "))
Mix.Task.rerun("format", [Path.join([elixir_out, "**", "*.pb.ex"])])
end
defp run_conformance_tests(args) do
{options, _args} = OptionParser.parse!(args, switches: [verbose: :boolean])
runner = path_in_protobuf_source("conformance_test_runner")
if not File.exists?(runner) do
Mix.raise("""
No conformance runner found at: #{runner}
If you want to build the conformance runner locally from the Google Protobuf
dependency of this library, run:
mix build_conformance_runner
If you have the protocolbuffers/protobuf repository cloned somewhere and want
to use its conformance runner, you'll need to export the PROTOBUF_ROOT environment
variable.
""")
end
runner = Path.expand(runner)
args = [
"--enforce_recommended",
"--failure_list",
"conformance/exemptions.txt",
"./conformance/runner.sh"
]
args = if Keyword.get(options, :verbose, false), do: ["--verbose"] ++ args, else: args
cmd!("#{runner} #{Enum.join(args, " ")}")
end
defp build_conformance_runner(_args) do
File.cd!(Mix.Project.deps_paths().google_protobuf, fn ->
cmd!("cmake . -Dprotobuf_BUILD_CONFORMANCE=ON")
cmd!("cmake --build . --parallel 10")
end)
end
defp cmd!(cmd) do
Mix.shell().info([:cyan, "Running: ", :reset, cmd])
case Mix.shell().cmd(cmd) do
0 -> Mix.shell().info("")
other -> Mix.raise("Command exited with non-zero status: #{other}")
end
end
end