1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use std::{env, path::PathBuf, process, sync::mpsc, thread, time};

/// Runs make/cmake to ensure native components are built.
/// Even once built this will add a few seconds to your build time.
pub fn build_native() -> std::thread::Result<()> {
    let start = time::Instant::now();
    //let spinner = indicatif::ProgressBar::new_spinner();
    let (tx, rx) = mpsc::sync_channel(1);
    let make = thread::spawn(move || {
        let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
        if cfg!(feature = "build_idf") {
            process::Command::new("idf.py")
                .arg("build")
                .current_dir(&std::path::Path::new(&manifest_dir))
                .status()
                .unwrap();
        } else if cfg!(feature = "build_make") {
            process::Command::new("make")
                .current_dir(&std::path::Path::new(&manifest_dir))
                .status()
                .unwrap();
        } else {
            println!("cargo:warning=No build feature enabled.  Build skipped.");
        }

        tx.send(()).unwrap();
    });
    loop {
        match rx.try_recv() {
            Err(mpsc::TryRecvError::Disconnected) | Ok(_) => break,
            Err(mpsc::TryRecvError::Empty) => {}
        }
        // NOTE: no point in printing anything because it's buffered and no way to flush it.
        //println!("cargo:warning=Building...");
        //spinner.tick();
        std::thread::sleep_ms(1000);
    }
    let ret = make.join();
    //spinner.finish();
    println!("cargo:warning=Build time {:?}", start.elapsed());
    ret
}

/// Add ESP-IDF native library search paths to rustc.
pub fn print_link_search() {
    let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
    if target_arch == "xtensa" {
        let esp_idf =
            PathBuf::from(env::var("IDF_PATH").expect("IDF_PATH environment variable must be set"));
        // Folder containing `build/` output after running `make menuconfig && make`
        let build_subdir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()).join("build");
        if glob::glob(&build_subdir.join("*.bin").to_string_lossy())
            .unwrap()
            .next()
            .is_none()
        {
            panic!("No .bin files, did you run `make menuconfig && make`?");
        }

        let build_dirs = [
            "app_trace",
            "app_update",
            "asio",
            "bootloader_support",
            "bt",
            "coap",
            "console",
            "cxx",
            "driver",
            "efuse",
            "esp-tls",
            "esp32",
            "esp_adc_cal",
            "esp_common",
            "esp_eth",
            "esp_event",
            "esp_gdbstub",
            "esp_http_client",
            "esp_http_server",
            "esp_https_ota",
            "esp_local_ctrl",
            "esp_ringbuf",
            "esp_rom",
            "esp_websocket_client",
            "esp_wifi",
            "espcoredump",
            "expat",
            "fatfs",
            "freemodbus",
            "freertos",
            "heap",
            "idf_test",
            "jsmn",
            "json",
            "libsodium",
            "log",
            "lwip",
            "main",
            "mbedtls",
            "mdns",
            "mqtt",
            "newlib",
            "nghttp",
            "nvs_flash",
            "openssl",
            "protobuf-c",
            "protocomm",
            "pthread",
            "sdmmc",
            "soc",
            "spi_flash",
            "spiffs",
            "tcp_transport",
            "tcpip_adapter",
            "ulp",
            "unity",
            "vfs",
            "wear_levelling",
            "wifi_provisioning",
            "wpa_supplicant",
            "xtensa",
        ]
        .iter()
        .map(|subdir| build_subdir.join(subdir));
        let idf_components = [
            "esp32/ld",
            "esp_rom/esp32/ld",
            "esp_wifi/lib_esp32",
            "xtensa/esp32",
        ]
        .iter()
        .map(|subdir| esp_idf.join("components").join(subdir));

        for dir in build_dirs.chain(idf_components) {
            println!("cargo:rustc-link-search=native={}", dir.display());
        }
    }
}

/// Writes script file that uses esptool.py to create an application image from
/// the Rust ELF.
pub fn esptool_write_script() -> std::io::Result<()> {
    use std::io::Write;
    let root = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
    let mut file = std::fs::File::create(root.join("image.sh"))?;

    // Make it executable
    use std::os::unix::fs::PermissionsExt;
    let perms = std::fs::Permissions::from_mode(0o744);
    file.set_permissions(perms)?;
    let cmd = format!(
        r#""$IDF_PATH/components/esptool_py/esptool/esptool.py" \
    --chip esp32 \
    elf2image \
    -o build/esp-app.bin \
    target/{}/{}/{}"#,
        env::var("TARGET").unwrap(),
        env::var("PROFILE").unwrap(),
        env::var("CARGO_PKG_NAME").unwrap()
    );

    // Write script with she-bang (#!)
    write!(file, "#!/usr/bin/env bash\n\n{}", cmd)
}