Skip to content

Commit

Permalink
add option to specify an alternate application name
Browse files Browse the repository at this point in the history
Summary:
We currently hard-code the application name to be equivalent to the target name. However, when generating thrift targets, we can end up in the situation, where we will have duplicated application names in a dependency tree.

This option allows us to generate unique application names, either based on user overwrites, or based on the namespace.

Reviewed By: michalmuskala

Differential Revision: D69687014

fbshipit-source-id: a2b95992fd021e4b6baaabb555e506b9f05744ae
  • Loading branch information
TheGeorge authored and facebook-github-bot committed Feb 18, 2025
1 parent 8a2166c commit 477e340
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 10 deletions.
5 changes: 4 additions & 1 deletion prelude/decls/erlang_rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ common_application_attributes = {

rules_attributes = {
"erlang_app": {
"app_name": attrs.option(attrs.string(), default = None, doc = """
This attribute allows the user to overwrite the Erlang application name, which otherwise defaults to the target name.
"""),
"app_src": attrs.option(attrs.source(), default = None, doc = """
The `app_src` field allows to optionally reference a `*.app.src` template file. This template file will then be used by
buck2 to generate the `*.app` output file in the applications `ebin/` directory. This is useful during the migration from
Expand Down Expand Up @@ -161,7 +164,7 @@ rules_attributes = {
"""),
} | common_application_attributes,
"erlang_app_includes": {
"application_name": attrs.string(),
"app_name": attrs.string(),
"includes": attrs.list(attrs.source(), default = []),
"_toolchain": attrs.toolchain_dep(default = "toolchains//:erlang-default"),
},
Expand Down
4 changes: 3 additions & 1 deletion prelude/erlang/erlang.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def erlang_application(
erlang_app_rule,
erlang_app_includes_rule,
name,
app_name = None,
applications = [],
included_applications = [],
extra_includes = [],
Expand All @@ -57,6 +58,7 @@ def erlang_application(
return [
erlang_app_rule(
name = name,
app_name = app_name,
applications = normalized_applications,
included_applications = normalized_included_applications,
extra_includes = [
Expand All @@ -68,7 +70,7 @@ def erlang_application(
),
erlang_app_includes_rule(
name = _extra_include_name(name),
application_name = name,
app_name = name if app_name == None else app_name,
includes = kwargs.get("includes", []),
visibility = kwargs.get("visibility", None),
labels = ["generated", "app_includes"],
Expand Down
13 changes: 7 additions & 6 deletions prelude/erlang/erlang_application.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ load(
load(
":erlang_utils.bzl",
"action_identifier",
"app_name",
"build_paths",
"multidict_projection",
"multidict_projection_key",
Expand Down Expand Up @@ -60,14 +61,14 @@ def erlang_application_impl(ctx: AnalysisContext) -> list[Provider]:
check_dependencies(ctx.attrs.extra_includes, [ErlangAppIncludeInfo]))
dependencies = flatten_dependencies(ctx, all_direct_dependencies)

name = ctx.attrs.name
name = app_name(ctx)
if name in dependencies and ErlangAppInfo in dependencies[name]:
fail("cannot depend on an application with the same name: %s" % (dependencies[name].label,))

return build_application(ctx, toolchains, dependencies)

def build_application(ctx, toolchains, dependencies) -> list[Provider]:
name = ctx.attrs.name
name = app_name(ctx)

build_environments = {}
app_folders = {}
Expand Down Expand Up @@ -120,7 +121,7 @@ def build_application(ctx, toolchains, dependencies) -> list[Provider]:
]

def _build_erlang_application(ctx: AnalysisContext, toolchain: Toolchain, dependencies: ErlAppDependencies) -> BuildEnvironment:
name = ctx.attrs.name
name = app_name(ctx)

build_environment = erlang_build.prepare_build_environment(ctx, toolchain, dependencies)

Expand Down Expand Up @@ -214,7 +215,7 @@ def _generate_priv_dir(
toolchain: Toolchain,
build_environment: BuildEnvironment) -> BuildEnvironment:
"""Generate the application's priv dir."""
name = ctx.attrs.name
name = app_name(ctx)

resources = ctx.attrs.resources
priv_symlinks = {}
Expand Down Expand Up @@ -332,7 +333,7 @@ def link_output(
link_path: str,
build_environment: BuildEnvironment) -> Artifact:
"""Link application output folder in working dir root folder."""
name = ctx.attrs.name
name = app_name(ctx)

ebin = build_environment.app_beams.values() + [build_environment.app_files[name]]
include = build_environment.app_includes.values()
Expand Down Expand Up @@ -444,7 +445,7 @@ def build_app_info(
app_folders: dict[str, Artifact],
primary_app_folder: Artifact,
start_dependencies: dict[str, list[StartDependencySet]]) -> Provider:
name = ctx.attrs.name
name = app_name(ctx)

version = {
toolchain.name: ctx.attrs.version
Expand Down
2 changes: 1 addition & 1 deletion prelude/erlang/erlang_application_includes.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def erlang_application_includes_impl(ctx: AnalysisContext) -> list[Provider]:
"""

# prepare include directory for current app
name = ctx.attrs.application_name
name = ctx.attrs.app_name

# input mapping
input_mapping = {}
Expand Down
8 changes: 7 additions & 1 deletion prelude/erlang/erlang_utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,15 @@ def to_term_args(data: typing.Any) -> cmd_args:
"",
)

def app_name(ctx: AnalysisContext) -> str:
if ctx.attrs.app_name == None:
return ctx.attrs.name
else:
return ctx.attrs.app_name

# paths
def app_file(ctx: AnalysisContext) -> str:
return paths.join(beam_dir(ctx), ctx.attrs.name + ".app")
return paths.join(beam_dir(ctx), app_name(ctx) + ".app")

def beam_dir(ctx: AnalysisContext) -> str:
return paths.join(ctx.attrs.name, "ebin")
Expand Down

0 comments on commit 477e340

Please sign in to comment.