1# Copyright (C) 2024 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15load("@rules_rust//rust:defs.bzl", "rust_library")
16
17package(
18    default_visibility = ["//visibility:public"],
19)
20
21# Upstream smoltcp uses a custom script in the cargo build flow to generate a `config.rs`
22# containing a set of configurations. We don't use cargo. Thus we use a pre-generated
23# one.
24genrule(
25    name = "gen_config",
26    outs = ["src/config.rs"],
27    cmd = """cat <<EOT > $@
28    pub const IFACE_MAX_ADDR_COUNT: usize = 2;
29    pub const IFACE_MAX_MULTICAST_GROUP_COUNT: usize = 4;
30    pub const IFACE_MAX_SIXLOWPAN_ADDRESS_CONTEXT_COUNT: usize = 4;
31    pub const IFACE_NEIGHBOR_CACHE_COUNT: usize = 16;
32    pub const IFACE_MAX_ROUTE_COUNT: usize = 2;
33    pub const FRAGMENTATION_BUFFER_SIZE: usize = 1500;
34    pub const ASSEMBLER_MAX_SEGMENT_COUNT: usize = 4;
35    pub const REASSEMBLY_BUFFER_SIZE: usize = 1500;
36    pub const REASSEMBLY_BUFFER_COUNT: usize = 1;
37    pub const IPV6_HBH_MAX_OPTIONS: usize = 1;
38    pub const DNS_MAX_RESULT_COUNT: usize = 1;
39    pub const DNS_MAX_SERVER_COUNT: usize = 1;
40    pub const DNS_MAX_NAME_SIZE: usize = 255;
41    pub const RPL_RELATIONS_BUFFER_COUNT: usize = 16;
42    pub const RPL_PARENTS_BUFFER_COUNT: usize = 8;
43EOT
44    """,
45)
46
47# `smoltcp` depends on crate `heapless`, which is still in the process of being imported to Android
48# (or possibly abandoned http://ag/22200123). For now we use a custom implementation of the APIs as
49# a workaround.
50genrule(
51    name = "heapless_src",
52    srcs = ["@gbl//smoltcp:heapless.rs"],
53    outs = ["src/heapless.rs"],
54    cmd = "cp $(SRCS) $(OUTS)",
55)
56
57rust_library(
58    name = "heapless",
59    srcs = ["src/heapless.rs"],
60    crate_root = "src/heapless.rs",
61    edition = "2021",
62)
63
64# Add fixup.rs to build and use our own crate root that imports it.
65genrule(
66    name = "smoltcp_fixup",
67    srcs = [
68        "@gbl//smoltcp:fixup.rs",
69        "src/lib.rs",
70    ],
71    outs = [
72        "src/fixup.rs",
73        "src/crate_root.rs",
74    ],
75    cmd = """
76        IFS=" " read -a srcs <<< "$(SRCS)" && \
77        IFS=" " read -a outs <<< "$(OUTS)" && \
78        for index in $${!srcs[@]}; do cp $${srcs[$$index]} $${outs[$$index]}; done && \
79        echo "mod fixup;" >> $${outs[1]}
80""",
81)
82
83rust_library(
84    name = "smoltcp",
85    srcs = glob(
86        ["**/*.rs"],
87        exclude = ["src/lib.rs"],
88    ) + [
89        "src/config.rs",
90        "src/crate_root.rs",
91        "src/fixup.rs",
92    ],
93    crate_features = [
94        "medium-ethernet",
95        "proto-ipv4",
96        "proto-ipv6",
97        "socket",
98        "socket-tcp",
99        "socket-icmp",
100    ],
101    crate_root = "src/crate_root.rs",
102    data = [
103        ":gen_config",
104        ":heapless_src",
105        ":smoltcp_fixup",
106    ],
107    edition = "2021",
108    rustc_env = {
109        "OUT_DIR": ".",
110    },
111    rustc_flags = [
112        # Always compile this crate in release mode. Otherwise it is too slow.
113        "-O",
114        "-A",
115        "unused_imports",
116        "-A",
117        "dead_code",
118        "-A",
119        "unreachable_patterns",
120        "-A",
121        "unused_variables",
122    ],
123    deps = [
124        ":heapless",
125        "@bitflags",
126        "@byteorder",
127        "@cfg-if",
128        "@managed",
129    ],
130)
131