Skip to content

Commit

Permalink
feat(mdns): WIP support for rust API
Browse files Browse the repository at this point in the history
  • Loading branch information
david-cermak committed Nov 22, 2024
1 parent b4ed1f4 commit 67639b5
Show file tree
Hide file tree
Showing 16 changed files with 746 additions and 149 deletions.
84 changes: 0 additions & 84 deletions .github/workflows/mdns__build-target-test.yml

This file was deleted.

64 changes: 0 additions & 64 deletions .github/workflows/mdns__host-tests.yml

This file was deleted.

34 changes: 34 additions & 0 deletions .github/workflows/mdns__rust.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: "mdns: rust-tests"

on:
push:
branches:
- master
pull_request:
types: [opened, synchronize, reopened, labeled]

jobs:
host_test_mdns:
if: contains(github.event.pull_request.labels.*.name, 'mdns') || github.event_name == 'push'
name: Host test build
runs-on: ubuntu-22.04
container: espressif/idf:latest

steps:
- name: Checkout esp-protocols
uses: actions/checkout@v4

- name: Build and Test
shell: bash
run: |
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
# Add Rust to the current PATH
echo "$HOME/.cargo/bin" >> $GITHUB_PATH
rustc --version
cargo --version
. ${IDF_PATH}/export.sh
cd components/mdns/examples/simple_query/
idf.py build
cd ../..
rustup
COMPILE_COMMANDS_DIR=examples/simple_query/build/ cargo run --example usage
2 changes: 1 addition & 1 deletion components/mdns/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ idf_build_get_property(target IDF_TARGET)
if(${target} STREQUAL "linux")
set(dependencies esp_netif_linux esp_event)
set(private_dependencies esp_timer console esp_system)
set(srcs ${MDNS_NETWORKING} ${MDNS_CONSOLE})
set(srcs "mdns_stub.c" ${MDNS_NETWORKING} ${MDNS_CONSOLE})
else()
set(dependencies lwip console esp_netif)
set(private_dependencies esp_timer esp_wifi)
Expand Down
13 changes: 13 additions & 0 deletions components/mdns/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "mdns"
version = "0.1.0"
edition = "2021"

[dependencies]
libc = "0.2"
dns-parser = "0.8"

[build-dependencies]
cc = "1.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
104 changes: 104 additions & 0 deletions components/mdns/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
use std::env;
use std::fs;
use std::path::{Path, PathBuf};
use serde::Deserialize;

#[derive(Debug, Deserialize)]
struct CompileCommand {
directory: String,
command: String,
file: String,
}

fn main() {
// Get the directory for compile_commands.json from an environment variable
let compile_commands_dir = env::var("COMPILE_COMMANDS_DIR")
.unwrap_or_else(|_| ".".to_string()); // Default to current directory

// Construct the path to the compile_commands.json file
let compile_commands_path = Path::new(&compile_commands_dir).join("compile_commands.json");

// Parse compile_commands.json
let compile_commands: Vec<CompileCommand> = {
let data = fs::read_to_string(&compile_commands_path)
.expect("Failed to read compile_commands.json");
serde_json::from_str(&data)
.expect("Failed to parse compile_commands.json")
};

// Directory of compile_commands.json, used to resolve relative paths
let base_dir = compile_commands_path
.parent()
.expect("Failed to get base directory of compile_commands.json");

// List of C files to compile (only base names)
let files_to_compile = vec![
"mdns_networking_socket.c",
"log_write.c",
"log_timestamp.c",
"esp_netif_linux.c",
"freertos_linux.c",
"tag_log_level.c",
"log_linked_list.c",
"log_lock.c",
"log_level.c",
"log_binary_heap.c",
"esp_system_linux2.c",
"heap_caps_linux.c",
"mdns_stub.c",
"log_buffers.c",
"util.c"
];

// Initialize the build
let mut build = cc::Build::new();

for file in &files_to_compile {
// Extract the base name from `file` for comparison
let target_base_name = Path::new(file)
.file_name()
.expect("Failed to extract base name from target file")
.to_str()
.expect("Target file name is not valid UTF-8");

// Find the entry in compile_commands.json by matching the base name
let cmd = compile_commands.iter()
.find(|entry| {
let full_path = Path::new(&entry.directory).join(&entry.file); // Resolve relative paths
if let Some(base_name) = full_path.file_name().and_then(|name| name.to_str()) {
// println!("Checking file: {} against {}", base_name, target_base_name); // Debug information
base_name == target_base_name
} else {
false
}
})
.unwrap_or_else(|| panic!("{} not found in compile_commands.json", target_base_name));

// Add the file to the build
build.file(&cmd.file);

// Parse flags and include paths from the command
for part in cmd.command.split_whitespace() {
if part.starts_with("-I") {
// Handle include directories
let include_path = &part[2..];
let full_include_path = if Path::new(include_path).is_relative() {
base_dir.join(include_path).canonicalize()
.expect("Failed to resolve relative include path")
} else {
PathBuf::from(include_path)
};
build.include(full_include_path);
} else if part.starts_with("-D") || part.starts_with("-std") {
// Add other compilation flags
build.flag(part);
}
}
}




// Compile with the gathered information
build.compile("mdns");
}
16 changes: 16 additions & 0 deletions components/mdns/examples/simple_query/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 3.5)


include($ENV{IDF_PATH}/tools/cmake/project.cmake)
if(${IDF_TARGET} STREQUAL "linux")
set(EXTRA_COMPONENT_DIRS "../../../../common_components/linux_compat" "../../tests/host_test/components/")
set(COMPONENTS main)
endif()

project(mdns_host)

# Enable sanitizers only without console (we'd see some leaks on argtable when console exits)
if(NOT CONFIG_TEST_CONSOLE AND CONFIG_IDF_TARGET_LINUX)
idf_component_get_property(mdns mdns COMPONENT_LIB)
target_link_options(${mdns} INTERFACE -fsanitize=address -fsanitize=undefined)
endif()
4 changes: 4 additions & 0 deletions components/mdns/examples/simple_query/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
idf_component_register(SRCS "main.c" "esp_system_linux2.c"
INCLUDE_DIRS
"."
REQUIRES mdns console nvs_flash)
21 changes: 21 additions & 0 deletions components/mdns/examples/simple_query/main/Kconfig.projbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
menu "Test Configuration"

config TEST_HOSTNAME
string "mDNS Hostname"
default "esp32-mdns"
help
mDNS Hostname for example to use

config TEST_NETIF_NAME
string "Network interface name"
default "eth2"
help
Name/ID if the network interface on which we run the mDNS host test

config TEST_CONSOLE
bool "Start console"
default n
help
Test uses esp_console for interactive testing.

endmenu
Loading

0 comments on commit 67639b5

Please sign in to comment.