[英]Erlang Hello World using Bazel as a build system
我想在 Ubuntu 22.04 上使用rules_erlang構建一個 Erlang 你好世界示例。
我的設置如下所示:
構建.bazel
load("@rules_erlang//:erlang_app.bzl", "erlang_app", "test_erlang_app")
load("@rules_erlang//:xref.bzl", "xref")
load("@rules_erlang//:dialyze.bzl", "dialyze", "plt")
load("@rules_erlang//:ct.bzl", "ct_suite", "assert_suites")
APP_NAME = "hello_world"
APP_VERSION = "0.1.0"
erlang_app(
app_name = APP_NAME,
app_version = APP_VERSION,
)
src/hello_world.erl
-module(hello_world).
-compile(export_all).
hello() ->
io:format("hello world~n").
工作區.bazel
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "bazel_skylib",
sha256 = "af87959afe497dc8dfd4c6cb66e1279cb98ccc84284619ebfec27d9c09a903de",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.2.0/bazel-skylib-1.2.0.tar.gz",
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.2.0/bazel-skylib-1.2.0.tar.gz",
],
)
load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")
bazel_skylib_workspace()
http_archive(
name = "rules_erlang",
sha256 = "5e59800ecc786d5375951028c959c6e6275c94eff2a52f5d53ccb1ad8b2ea20a",
strip_prefix = "rules_erlang-3.8.4",
urls = ["https://github.com/rabbitmq/rules_erlang/archive/refs/tags/3.8.4.zip"],
)
load(
"@rules_erlang//:rules_erlang.bzl",
"erlang_config",
"rules_erlang_dependencies",
)
erlang_config()
rules_erlang_dependencies()
load("@erlang_config//:defaults.bzl", "register_defaults")
register_defaults()
代碼也可以在這里找到。
當我執行bazel build //...
我得到
錯誤:/home/vertexwahn/.cache/bazel/_bazel_vertexwahn/b5f945f94177a8ffa6ac0f7108dfc1cd/external/erlang_config/external/BUILD.bazel:12:16:在/usr驗證otp失敗:(退出1):bash失敗:執行命令/bin時出錯/bash -c...(跳過剩余的 1 個參數)
使用 --sandbox_debug 查看來自沙箱的詳細消息並保留沙箱構建根以調試 Erlang 版本不匹配(預期未知,發現 24.2.1)
歡迎任何提示讓這個工作!
bazel build //... --sandbox_debug
給我
compile: warnings being treated as errors
hello_world.erl:2:2: export_all flag enabled - all functions will be exported
使用-export([hello/0]).
而不是-compile(export_all).
有用
-module(hello_world).
-export([hello/0]).
hello() ->
io:format("hello world~n").
❯ bazel build //...
INFO: Analyzed 3 targets (0 packages loaded, 0 targets configured).
INFO: Found 3 targets...
INFO: Elapsed time: 0,326s, Critical Path: 0,25s
INFO: 2 processes: 1 internal, 1 darwin-sandbox.
INFO: Build completed successfully, 2 total actions
要運行它,您可以在 BUILD.bazel 中聲明一條shell
規則,例如:
load("@rules_erlang//:shell.bzl", "shell")
shell(
name = "repl",
deps = [":erlang_app"],
extra_erl_args = ["-eval", "'hello_world:hello()'"],
)
然后你可以 bazel run:repl
。
或者,如果您更改hello_world.erl
以便它導出main/1
而不是hello
,則可以使用escript
規則:
load("@rules_erlang//:escript.bzl", "escript_archive")
escript_archive(
name = "hello_world",
app = ":erlang_app",
)
你好_ hello_world.erl
:
-module(hello_world).
-export([main/1]).
main(_) ->
io:format("hello world~n").
並使用bazel run:hello_world
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.