Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for RUST language #281

Open
jozeblackblox opened this issue Sep 23, 2024 · 6 comments
Open

support for RUST language #281

jozeblackblox opened this issue Sep 23, 2024 · 6 comments
Labels
feature New functionality

Comments

@jozeblackblox
Copy link

Hello!

I hope this is still in scope...

I want to use ubxlib in must std RUST project - I started from plain template .

How to build and use ubxlib?

@m-abubakar
Copy link
Contributor

Hi @jozeblackblox,

Thank you for reaching out. Unfortunately, Rust is not within our current scope. However, I encourage you to submit this as a feature request in our forum.

In the meantime, I recommend exploring the official ubxlib documentation and usage examples, which may provide helpful guidance for integrating it into your Rust project. Best of luck with your implementation!

@hkro hkro added the feature New functionality label Sep 24, 2024
@AgafonovVadim
Copy link

AgafonovVadim commented Sep 25, 2024

Hi @jozeblackblox,

If I understood your question correctly, you could do something like this...

So,
clone and Build ubxlib
You'll need to clone the ubxlib C library:

git clone https://github.com/u-blox/ubxlib.git

Now, build the library using your system's C/C++ build toolchain.

cd ubxlib
make   # Adjust this depending on the build system ubxlib uses

Make sure the library builds successfully and locate the generated library files (.a, .so, or .dll depending on your platform).

### Use bindgen to Create Rust Bindings

Open your Cargo.toml and add

[dependencies]
bindgen = "0.63.0"

[build-dependencies]
cc = "1.0"   # For compiling C code if needed

And it's done)

Example

use std::env;
use std::path::PathBuf;
use bindgen;

fn main() {
    // Specify the path to the ubxlib library.
    let ubxlib_dir = "../ubxlib"; // Adjust this to your actual path

    // Tell Cargo to tell rustc to link the system ubxlib
    println!("cargo:rustc-link-search=native={}", ubxlib_dir);
    println!("cargo:rustc-link-lib=static=ubxlib");

    // Generate bindings to ubxlib
    let bindings = bindgen::Builder::default()
        .header(format!("{}/include/ubxlib.h", ubxlib_dir))  // Path to the ubxlib header
        .generate()
        .expect("Unable to generate bindings");

    // Write the bindings to the $OUT_DIR/bindings.rs file.
    let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
    bindings
        .write_to_file(out_path.join("bindings.rs"))
        .expect("Couldn't write bindings!");
}

@jozeblackblox
Copy link
Author

jozeblackblox commented Sep 26, 2024

Hello!

Thank you for the answer!

I still have problems creating .a file to include it in my Rust project.

Since I use ESP32, I tried with CMakeLists from ESP32 Runner, but without success. I tried with Windows and WSL2.

Does UBXLIB rely on esp-idf or can I make standalone build?

Can you specify build process with few more details?

Thanks!

@AgafonovVadim
Copy link

Good afternoon, @jozeblackblox!

Let's start from the beginning.

Does UBXLIB rely on esp-idf or can I make standalone build?

  • Yes, ubxlib typically relies on esp-idf when targeting ESP32 devices. This is because the ESP32 is usually programmed using the ESP-IDF (Espressif IoT Development Framework), which provides the necessary SDK, drivers, and build environment for ESP32.

Can you specify build process with few more details?

  • To build ubxlib for ESP32 and generate the .a static library, you need to set up esp-idf and configure the build process accordingly.

Ok, let me try to explain it step by step.

Step 1:

  1. Install ESP-IDF: Follow the official guide to install ESP-IDF on your machine. For Windows, it's recommended to use WSL2 for better compatibility. (I will tell you about the example of WSL 2, so you can use Ubuntu)

I. Install ESP-IDF

sudo apt update
sudo apt install git wget flex bison gperf python3 python3-pip cmake ninja-build ccache libffi-dev libssl-dev dfu-util

II. Clone ESP-IDF repo

git clone --recursive https://github.com/espressif/esp-idf.git
cd esp-idf
./install.sh

III. Set environment variables

. ./export.sh
  1. Set Up Toolchain: Install the Xtensa
./install.sh

Step 2: Configure ubxlib

  1. Clone ubxlib (like you did)
git clone https://github.com/u-blox/ubxlib.git
cd ubxlib
  1. Configure for ESP32:

Navigate to the port/platform/esp-idf directory:

cd port/platform/esp-idf

You'll find a CMakeLists.txt file here that is configured to work with ESP-IDF. This CMake file ensures that ubxlib is built using ESP-IDF's environment.

Step 3: Build the Static Library

  1. Configure the Project:

Inside the ubxlib/port/platform/esp-idf directory, run the CMake configuration to generate the build files. You can specify where the static library (.a) should be output.

idf.py set-target esp32
idf.py menuconfig
  1. Build
idf.py build

This will generate the necessary .a file under the build directory. Look for the static library, likely named something like libubxlib.a.

I'm happy to help! Please try the instructions I sent, and feel free to reach out anytime. I'll be glad to hear if everything works out!

Good luck! 🍀🍀🍀

@jozeblackblox
Copy link
Author

Hello!

Sorry for late response, I had to jump on another project.

I have successfully created libubxlib.a with your instructions! Thanks!

I can use functions, like uPortInit(), in my RUST code.

Now I have to dig into cellular functionality 👷

I hope you will someday add support for the RUST language or maybe create a working example with an external C library 🙂 🙏

@AgafonovVadim
Copy link

Hi there!!! 👋

I'm glad to hear that I could help you🤗. I wish you the best of luck with your projects!

I hope you will someday add support for the RUST language or maybe create a working example with an external C library 🙂 🙏

We’ll have to wait a bit, I’m still studying:)

I’d like to ask you a couple of questions about your experience. If you don’t mind, let me know how I can reach you, or you can message me wherever it’s convenient for you—all my links are in my profile🙃. Thanks!

Have a good day! 🍀☀️😎

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New functionality
Projects
None yet
Development

No branches or pull requests

4 participants