xref: /aosp_15_r20/external/executorch/runtime/platform/targets.bzl (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "runtime")
2load(":log.bzl", "get_et_logging_flags")
3
4def _select_pal(dict_):
5    """Returns an element of `dict_` based on the value of the
6    `executorch.pal_default` build config value. Fails if no corresponding entry
7    exists.
8    """
9    pal_default = native.read_config("executorch", "pal_default", "posix")
10    if not pal_default in dict_:
11        fail("Missing key for executorch.pal_default value '{}' in dict '{}'".format(pal_default, dict_))
12    return dict_[pal_default]
13
14def profiling_enabled():
15    return native.read_config("executorch", "prof_enabled", "false") == "true"
16
17def get_profiling_flags():
18    profiling_flags = []
19    if profiling_enabled():
20        profiling_flags += ["-DPROFILING_ENABLED"]
21    prof_buf_size = native.read_config("executorch", "prof_buf_size", None)
22    if prof_buf_size != None:
23        if not profiling_enabled():
24            fail("Cannot set profiling buffer size without enabling profiling first.")
25        profiling_flags += ["-DMAX_PROFILE_EVENTS={}".format(prof_buf_size), "-DMAX_MEM_PROFILE_EVENTS={}".format(prof_buf_size)]
26    num_prof_blocks = native.read_config("executorch", "num_prof_blocks", None)
27    if num_prof_blocks != None:
28        if not profiling_enabled():
29            fail("Cannot configure number of profiling blocks without enabling profiling first.")
30        profiling_flags += ["-DMAX_PROFILE_BLOCKS={}".format(num_prof_blocks)]
31    return profiling_flags
32
33def define_common_targets():
34    """Defines targets that should be shared between fbcode and xplat.
35
36    The directory containing this targets.bzl file should also contain both
37    TARGETS and BUCK files that call this function.
38    """
39
40    # Default implementations of pal functions. These are weak symbols, so
41    # client defined implementations will overide them.
42    runtime.cxx_library(
43        name = "platform_private",
44        srcs = _select_pal({
45            "minimal": ["default/minimal.cpp"],
46            "posix": ["default/posix.cpp"],
47        }),
48        deps = [
49            ":pal_interface",
50        ],
51        visibility = [
52            "//executorch/core/...",
53        ],
54        # WARNING: using a deprecated API to avoid being built into a shared
55        # library. In the case of dynamically loading .so library we don't want
56        # it to depend on other .so libraries because that way we have to
57        # specify library directory path.
58        force_static = True,
59    )
60
61    # Interfaces for executorch users
62    runtime.cxx_library(
63        name = "platform",
64        exported_headers = [
65            "abort.h",
66            "assert.h",
67            "clock.h",
68            "log.h",
69            "profiler.h",
70            "runtime.h",
71        ],
72        srcs = [
73            "abort.cpp",
74            "log.cpp",
75            "profiler.cpp",
76            "runtime.cpp",
77        ],
78        exported_preprocessor_flags = get_profiling_flags() + get_et_logging_flags(),
79        exported_deps = [
80            "//executorch/runtime/platform:pal_interface",
81            ":compiler",
82            ":platform_private",
83        ],
84        visibility = [
85            "//executorch/...",
86            "@EXECUTORCH_CLIENTS",
87        ],
88        # WARNING: using a deprecated API to avoid being built into a shared
89        # library. In the case of dynamically loading so library we don't want
90        # it to depend on other so libraries because that way we have to
91        # specify library directory path.
92        force_static = True,
93    )
94
95    # Library for backend implementers to define implementations against.
96    runtime.cxx_library(
97        name = "pal_interface",
98        exported_headers = [
99            "platform.h",
100            "system.h",
101            "types.h",
102        ],
103        exported_deps = [
104            ":compiler",
105        ],
106        exported_preprocessor_flags = select(
107            {
108                "DEFAULT": [],
109                "ovr_config//os:linux": ["-DET_USE_LIBDL"],
110                "ovr_config//os:macos": ["-DET_USE_LIBDL"],
111            },
112        ),
113        visibility = [
114            "//executorch/...",
115            "@EXECUTORCH_CLIENTS",
116        ],
117    )
118
119    # Common compiler directives such as 'unlikely' or 'deprecated'
120    runtime.cxx_library(
121        name = "compiler",
122        exported_headers = [
123            "compiler.h",
124        ],
125        visibility = [
126            "//executorch/...",
127            "@EXECUTORCH_CLIENTS",
128        ],
129    )
130