Access Variables in build.rs

Hi,

I'm looking at creating an ASAP2 (A2L) file for the program where it would to read information about types of variables, such name and type etc. What is the best method for getting this information from the source code into the build script where it can be used to make a custom file?

Thanks!

Your question seems to be quite underspecified.

  1. What crate(s) will you be using for parsing and serializing these files?
  2. What is the data source, maybe some other set of files in a different format? You mention the source code, but surely the data source is not hardcoded in your own crate... is it?

The usual way to go about using your own crate in the build script is to split the crate into one that doesn't need the build script (call it "A") and one that does ("B"). They can share things like type definitions either in a third crate "C" that both "A" and "B" depend on, or by having crate "B" depend on "A".

That can resolve a circular dependency where your crate depends on the build script, but the build script depends on your crate.

Here is an ASCII diagram where the arrow points from dependency to dependent, if you were to split your crate into "A" and "B", where out is the build script output:

┌───┐      ┌──────────┐    ┌─────┐
│ A ├──┬──►│ build.rs ├───►│ out ├──┐
└───┘  │   └──────────┘    └─────┘  │
       │                            │
       │                            │   ┌───┐
       └───────────────────────────►└──►│ B │
                                        └───┘

Or if you want to share type definitions between both "A" and "B" with crate "C":

┌───┐      ┌───┐     ┌──────────┐    ┌─────┐
│ C ├──┬──►│ A ├────►│ build.rs ├───►│ out ├──┐
└───┘  │   └───┘     └──────────┘    └─────┘  │
       │                                      │
       │                                      │   ┌───┐
       └─────────────────────────────────────►└──►│ B │
                                                  └───┘

It's flexible to do this kind of thing, if you are creative enough to paper over the sharp edges.

This is my best effort at interpreting the question based on information provided.

1 Like

Hi, @parasyte,

Sorry if the question is under specified, I'm at the early stages of feasibility and scoping the problem. I'm looking at replacing embedded C for a control system that will communicate with a PC via an automotive protocol (ASAP2) where configurable variables can be changed by the PC and other variables can be logged and viewed. For these variables to be edited/viewed they will be stored at addresses that are read from the compiled code for the ASAP2 file. This file contains names for variables, addresses in memory, data types, upper and lower limits, conversion rules from value in memory to displayed value, ....
image

These values would ideally be defined in the source next to the declaration of the variable so that there is only one place where it's defined. I was imagining that the declaration would be via a macro in the source code to make a static value (either mutable or not depending on usage) such as:

parameter!(BTest, bool, 0, 0, 1, %1d, "This is an example boolean");

Where this declaration defines the name of the variable, datatype, min and max value, display formatting, and description. Most of these would be used for the ASAP2 file generation rather than in the program and I'd look at a way of optimising out most of these. The ASAP2 file would be made during compile time so that it can only contain the variables that aren't optimised out and ideally would have a function to extract the memory address after compilation, though that appears to be a separate issue.

I hope that has made the usage clearer.

As for crates that I might be using there is an A2L creation file (a2lfile - Rust) that could be a good idea or it might be a new crate from scratch.

Thanks!