Skip to content

Commit 97430be

Browse files
michalmuskalafacebook-github-bot
authored andcommitted
erlang: simplify generated sources handling
Summary: We weren't using the map result - a simple list is enough, we can also simplify the code by moving where the output path is generated (and how we generate it now that we don't have multiple toolchains). The rejection of modules from `src` was a noop, unless there was a duplicate module - one generated and one in sources, but we should absolutely error if that is the case, rather than silently ignoring it as before. Reviewed By: TheGeorge Differential Revision: D81781359 fbshipit-source-id: e57765d723e40546c862697e75b4859f407d1c7b
1 parent c643756 commit 97430be

File tree

2 files changed

+15
-27
lines changed

2 files changed

+15
-27
lines changed

erlang/erlang_application.bzl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,18 +115,18 @@ def _build_erlang_application(ctx: AnalysisContext, name: str, toolchain: Toolch
115115
build_environment = erlang_build.prepare_build_environment(dep_info, include_info)
116116

117117
# build generated inputs
118-
generated_source_artifacts = erlang_build.build_steps.generated_source_artifacts(ctx, toolchain, name)
118+
generated_source_artifacts = erlang_build.build_steps.generated_source_artifacts(ctx, toolchain)
119119

120120
# Put all sources in a src folder, both for hermeticity (erlc will try to load .hrl files next to the .erl file)
121121
# and to ultimately put it in the app folder
122-
src_dir = _link_src_dir(ctx, extra_srcs = generated_source_artifacts.values())
122+
src_dir = _link_src_dir(ctx, extra_srcs = generated_source_artifacts)
123123

124124
# collect all inputs
125125
src_artifacts = [
126126
src
127127
for src in ctx.attrs.srcs
128-
if erlang_build.utils.is_erl(src) and erlang_build.utils.module_name(src) not in generated_source_artifacts
129-
] + generated_source_artifacts.values()
128+
if erlang_build.utils.is_erl(src)
129+
] + generated_source_artifacts
130130

131131
private_header_artifacts = [header for header in ctx.attrs.srcs if erlang_build.utils.is_hrl(header)]
132132

erlang/erlang_build.bzl

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ load(
2626
_BUILD_DIR = "__build"
2727
_DEP_FILES_DIR = paths.join(_BUILD_DIR, "__dep_files")
2828
_DEP_INFO_FILE = paths.join(_DEP_FILES_DIR, "app.info.dep")
29+
_GENERATED_DIR = paths.join(_BUILD_DIR, "__generated")
2930

3031
BuildEnvironment = record(
3132
includes = field(IncludesMapping, {}),
@@ -86,21 +87,16 @@ def _prepare_build_environment(
8687
beams = beams,
8788
)
8889

89-
def _generated_source_artifacts(ctx: AnalysisContext, toolchain: Toolchain, name: str) -> PathArtifactMapping:
90+
def _generated_source_artifacts(ctx: AnalysisContext, toolchain: Toolchain) -> list[Artifact]:
9091
"""Generate source output artifacts and build actions for generated erl files."""
9192

92-
def build(src, custom_include_opt):
93-
return _build_xyrl(
94-
ctx,
95-
toolchain,
96-
src,
97-
custom_include_opt,
98-
ctx.actions.declare_output(generated_erl_path(name, src)),
99-
)
100-
101-
yrl_outputs = {module_name(src): build(src, "yrl_includefile") for src in ctx.attrs.srcs if _is_yrl(src)}
102-
xrl_outputs = {module_name(src): build(src, "xrl_includefile") for src in ctx.attrs.srcs if _is_xrl(src)}
103-
return yrl_outputs | xrl_outputs
93+
results = []
94+
for src in ctx.attrs.srcs:
95+
if _is_yrl(src):
96+
results.append(_build_xyrl(ctx, toolchain, src, "yrl_includefile"))
97+
elif _is_xrl(src):
98+
results.append(_build_xyrl(ctx, toolchain, src, "xrl_includefile"))
99+
return results
104100

105101
# mutates build_environment in place
106102
def _generate_include_artifacts(
@@ -245,9 +241,9 @@ def _build_xyrl(
245241
ctx: AnalysisContext,
246242
toolchain: Toolchain,
247243
xyrl: Artifact,
248-
custom_include_opt: str,
249-
output: Artifact) -> Artifact:
244+
custom_include_opt: str) -> Artifact:
250245
"""Generate an erl file out of an xrl or yrl input file."""
246+
output = ctx.actions.declare_output(_GENERATED_DIR, "{}.erl".format(module_name(xyrl)))
251247
erlc = toolchain.otp_binaries.erlc
252248
custom_include = getattr(ctx.attrs, custom_include_opt, None)
253249
cmd = cmd_args(erlc)
@@ -446,14 +442,6 @@ def _preserved_opts(opts: list[str]) -> cmd_args:
446442
joined = cmd_args(preserved, delimiter = ", ")
447443
return cmd_args(joined, format = "{options, [{}]}")
448444

449-
def generated_erl_path(appname: str, src: Artifact) -> str:
450-
"""The output path for generated erl files."""
451-
return paths.join(
452-
_BUILD_DIR,
453-
"__generated_%s" % (appname,),
454-
"%s.erl" % (module_name(src),),
455-
)
456-
457445
def module_name(in_file: Artifact) -> str:
458446
""" Returns the basename of the artifact without extension """
459447
end = in_file.basename.rfind(".")

0 commit comments

Comments
 (0)