# Copyright 2025 - The Android Open Source Project # # Licensed under the Apache License, Version 2.0 (the', help='License'); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an', help='AS IS' BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """netsim bazel build rule.""" load("@protobuf//bazel:cc_proto_library.bzl", "cc_proto_library") load("@rules_cc//cc:defs.bzl", "cc_binary") load("@rules_proto//proto:defs.bzl", "proto_library") load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library", "rust_test") proto_library( name = "netsimd-proto", srcs = [ "proto/netsim/common.proto", "proto/netsim/config.proto", "proto/netsim/frontend.proto", "proto/netsim/hci_packet.proto", "proto/netsim/model.proto", "proto/netsim/packet_streamer.proto", "proto/netsim/startup.proto", "proto/netsim/stats.proto", ], strip_import_prefix = "proto", deps = [ "@protobuf//:empty_proto", "@protobuf//:timestamp_proto", "@rootcanal//:rootcanal-configuration-proto", ], ) cc_proto_library( name = "netsimd_cc_proto", deps = [":netsimd-proto"], ) # BLE Advertisers rust_library( name = "ble_advertisers", srcs = glob(["rust/ble-advertisers/src/**/*.rs"]), deps = [ "@hex", "@thiserror", "@zerocopy", ], ) rust_test( name = "ble_advertisers_tests", crate = ":ble_advertisers", tags = ["general_tests"], ) rust_library( name = "netsim_common", srcs = glob(["rust/common/src/**/*.rs"]), deps = [ "@anyhow", "@chrono", "@env_logger", "@libc", "@log", "@protobuf-json-mapping", "@rand", "@zip", ], ) rust_test( name = "netsim_common_tests", crate = ":netsim_common", tags = ["general_tests"], ) rust_library( name = "netsim_proto", srcs = glob(["rust/proto-cf/src/**/*.rs"]), crate_name = "netsim_proto", deps = [ "@grpcio", "@protobuf-rust", ], ) rust_library( name = "netsim_cli", srcs = glob(["rust/cli/src/**/*.rs"]), crate_name = "netsim_cli", deps = [ ":netsim_common", ":netsim_proto", "@clap", "@futures", "@futures-util", "@grpcio", "@hex", "@log", "@protobuf-rust", ], ) rust_binary( name = "netsim", srcs = ["rust/cli/bin/netsim.rs"], crate_name = "netsim_cli", rustc_flags = [ "-C", "link-arg=-lc", ], deps = [ ":netsim_cli", ], ) rust_test( name = "netsim_cli_tests", crate = ":netsim_cli", rustc_flags = [ "-C", "link-arg=-lc", ], tags = ["general_tests"], ) # PDL generated files genrule( name = "netsim_netlink_rust_gen", srcs = ["pdl/netlink.pdl"], outs = ["netlink_packets.rs"], cmd = "$(location @pdl-compiler//:pdlc) --output-format rust $(SRCS) > $(OUTS)", tools = ["@pdl-compiler//:pdlc"], ) genrule( name = "netsim_mac80211_hwsim_rust_gen", srcs = ["pdl/mac80211_hwsim.pdl"], outs = ["mac80211_hwsim_packets.rs"], cmd = "$(location @pdl-compiler//:pdlc) --output-format rust $(SRCS) > $(OUTS)", tools = ["@pdl-compiler//:pdlc"], ) genrule( name = "netsim_ieee80211_rust_gen", srcs = ["pdl/ieee80211.pdl"], outs = ["ieee80211_packets.rs"], cmd = "$(location @pdl-compiler//:pdlc) --output-format rust $(SRCS) > $(OUTS)", tools = ["@pdl-compiler//:pdlc"], ) genrule( name = "netsim_llc_rust_gen", srcs = ["pdl/llc.pdl"], outs = ["llc_packets.rs"], cmd = "$(location @pdl-compiler//:pdlc) --output-format rust $(SRCS) > $(OUTS)", tools = ["@pdl-compiler//:pdlc"], ) genrule( name = "netsim_link_layer_packets_rust_gen", srcs = ["@rootcanal//:packets/link_layer_packets.pdl"], outs = ["link_layer_packets.rs"], cmd = "$(location @pdl-compiler//:pdlc) --output-format rust $(SRCS) > $(OUTS)", tools = ["@pdl-compiler//:pdlc"], ) rust_library( name = "netsim_packets", srcs = glob(["rust/packets/src/**/*.rs"]) + [ ":netsim_ieee80211_rust_gen", ":netsim_link_layer_packets_rust_gen", ":netsim_llc_rust_gen", ":netsim_mac80211_hwsim_rust_gen", ":netsim_netlink_rust_gen", ], crate_name = "netsim_packets", rustc_env = { "OUT_DIR": "../../..", }, deps = [ "@anyhow", "@bytes", "@pdl-runtime", ], ) rust_library( name = "netsim_libslirp_rs", srcs = glob(["rust/libslirp-rs/src/**/*.rs"]), crate_name = "netsim_libslirp_rs", crate_root = "rust/libslirp-rs/src/lib.rs", deps = [ "@etherparse", "@bytes", "@libc", "@log", # "@winapi", // TODO: Resolve this dependency to build for Windows "@tokio", "@libslirp", "@glib//glib", ], ) rust_test( name = "netsim_libslirp_rs_tests", crate = ":netsim_libslirp_rs", tags = ["general_tests"], ) rust_test( name = "netsim_libslirp_rs_integration_test", srcs = ["rust/libslirp-rs/tests/integration_test.rs"], aliases = { ":netsim_libslirp_rs": "libslirp_rs", }, rustc_flags = [ "-C", "link-arg=-lc", ], tags = ["general_tests"], deps = [ ":netsim_libslirp_rs", "@bytes", ], ) rust_test( name = "netsim_libslirp_rs_integration_udp", srcs = ["rust/libslirp-rs/tests/integration_udp.rs"], aliases = { ":netsim_libslirp_rs": "libslirp_rs", }, rustc_flags = [ "-C", "link-arg=-lc", ], tags = ["general_tests"], deps = [ ":netsim_libslirp_rs", "@bytes", "@etherparse", ], ) rust_library( name = "netsim_hostapd_rs", srcs = glob(["rust/hostapd-rs/src/**/*.rs"]), crate_name = "netsim_hostapd_rs", crate_root = "rust/hostapd-rs/src/lib.rs", deps = [ ":netsim_common", ":netsim_packets", "@aes", "@anyhow", "@bytes", "@ccm", "@log", "@pdl-runtime", "@tokio", ], ) rust_test( name = "netsim_hostapd_rs_tests", crate = ":netsim_hostapd_rs", tags = ["general_tests"], ) rust_test( name = "netsim_hostapd_rs_integration_test", srcs = ["rust/hostapd-rs/tests/integration_test.rs"], aliases = { ":netsim_hostapd_rs": "hostapd_rs", }, tags = ["general_tests"], deps = [ ":netsim_hostapd_rs", ":netsim_packets", "@bytes", "@log", "@pdl-runtime", "@tokio", "@wpa_supplicant_8//:hostapd_c_lib", ], ) rust_library( name = "netsim_http_proxy", srcs = glob(["rust/http-proxy/src/**/*.rs"]), aliases = { ":netsim_libslirp_rs": "libslirp_rs", }, crate_name = "netsim_http_proxy", crate_root = "rust/http-proxy/src/lib.rs", deps = [ ":netsim_libslirp_rs", "@base64", "@bytes", "@etherparse", "@httparse", "@log", "@regex", "@tokio", ], ) rust_test( name = "netsim_http_proxy_tests", crate = ":netsim_http_proxy", rustc_flags = [ "-C", "link-arg=-lc", ], tags = ["general_tests"], ) rust_library( name = "netsim_capture", srcs = glob(["rust/capture/src/**/*.rs"]), aliases = { ":netsim_libslirp_rs": "libslirp_rs", }, crate_name = "netsim_capture", crate_root = "rust/capture/src/lib.rs", proc_macro_deps = [ "@zerocopy-derive", ], deps = [ "@tokio", "@zerocopy", ], ) rust_test( name = "netsim_capture_tests", crate = ":netsim_capture", tags = ["general_tests"], ) rust_test( name = "netsim_capture_integration_test", srcs = ["rust/capture/tests/integration_test.rs"], aliases = { ":netsim_capture": "capture", }, compile_data = ["rust/capture/data/dns.cap"], tags = ["general_tests"], deps = [ ":netsim_capture", "@tokio", ], ) rust_test( name = "netsim_http_proxy_integration_test", srcs = ["rust/http-proxy/tests/integration_test.rs"], aliases = { ":netsim_capture": "capture", ":netsim_http_proxy": "http_proxy", }, compile_data = ["rust/capture/data/dns.cap"], rustc_flags = [ "-C", "link-arg=-lc", ], tags = ["general_tests"], deps = [ ":netsim_capture", ":netsim_http_proxy", "@tokio", ], ) rust_library( name = "netsim_daemon", srcs = glob(["rust/daemon/src/**/*.rs"]), aliases = { ":netsim_http_proxy": "http_proxy", ":netsim_libslirp_rs": "libslirp_rs", ":netsim_hostapd_rs": "hostapd_rs", }, compile_data = [ "rust/daemon/src/uwb/sample_ranging_data.csv", ], crate_name = "netsim_daemon", crate_root = "rust/daemon/src/lib.rs", rustc_flags = [ "--cfg=feature=\"local_ssl\"", ], deps = [ ":netsim_common", ":netsim_hostapd_rs", ":netsim_http_proxy", ":netsim_libslirp_rs", ":netsim_packets", ":netsim_proto", "@anyhow", "@bytes", "@clap", "@cxx.rs//:cxx", "@data-encoding", "@futures", "@futures-channel", "@futures-executor", "@futures-util", "@glam", "@grpcio", "@http", "@httparse", "@libc", "@log", "@pdl-runtime", "@pica", "@protobuf-json-mapping", "@protobuf-rust", "@rand", "@regex", "@socket2", "@thiserror", "@tokio", "@tokio-stream", "@tungstenite", ], ) genrule( name = "netsim_daemon_cc", srcs = ["rust/daemon/src/ffi.rs"], outs = ["netsim-daemon/src/ffi.rs.cc"], cmd = "$(location @cxx.rs//:codegen) $(SRCS) --cfg feature=\\\"local_ssl\\\" >> $(OUTS)", tools = ["@cxx.rs//:codegen"], ) genrule( name = "netsim_daemon_h", srcs = ["rust/daemon/src/ffi.rs"], outs = ["netsim-daemon/src/ffi.rs.h"], cmd = "$(location @cxx.rs//:codegen) $(SRCS) --cfg feature=\\\"local_ssl\\\" --header >> $(OUTS)", tools = ["@cxx.rs//:codegen"], ) genrule( name = "cxx-bridge-header", outs = ["rust/cxx.h"], cmd = "$(location @cxx.rs//:codegen) --header >> $(OUTS)", tools = ["@cxx.rs//:codegen"], ) cc_binary( name = "netsimd", srcs = [ "rust/netsimd.cc", "src/backend/grpc_client.h", "src/hci/bluetooth_facade.cc", "src/hci/bluetooth_facade.h", "src/hci/hci_packet_hub.h", "src/hci/hci_packet_transport.cc", "src/hci/hci_packet_transport.h", "src/hci/rust_device.cc", "src/hci/rust_device.h", "src/util/crash_report.cc", "src/util/crash_report.h", "src/util/filesystem.h", "src/util/ini_file.h", "src/util/log.cc", "src/util/log.h", ":cxx-bridge-header", ":netsim_daemon_cc", ":netsim_daemon_h", ], defines = ["NETSIM_ANDROID_EMULATOR"], includes = [ "src/", ], deps = [ ":netsim_daemon", ":netsimd_cc_proto", "@rootcanal//:libbt-rootcanal", "@wpa_supplicant_8//:hostapd_c_lib", ], ) genrule( name = "netsim-ui", srcs = ["//ui:netsim_ui_files"], outs = [ "netsim-ui/index.html", "netsim-ui/js/device-info.js", "netsim-ui/js/device-list.js", "netsim-ui/js/navigation-bar.js", "netsim-ui/js/packet-info.js", "netsim-ui/js/license-info.js", "netsim-ui/js/customize-map-button.js", "netsim-ui/js/pyramid-sprite.js", "netsim-ui/js/device-dragzone.js", "netsim-ui/js/device-map.js", "netsim-ui/js/device-dropzone.js", "netsim-ui/js/device-observer.js", "netsim-ui/js/netsim-app.js", "netsim-ui/js/cube-sprite.js", "netsim-ui/dev.html", "netsim-ui/node_modules/tslib/tslib.es6.js", "netsim-ui/assets/grid-background.svg", "netsim-ui/assets/netsim-logo.svg", "netsim-ui/assets/netsim-logo-b.svg", "netsim-ui/assets/polar-background.svg", "netsim-ui/assets/hexagonal-background.png", ], cmd = "for f in $(locations //ui:netsim_ui_files); do dest=$(@D)/netsim-ui/$${f#ui/dist/}; mkdir -p $$(dirname $$dest); cp -f $$f $$dest; done", )