|
1 | 1 | """ gen_embedded_can.py: Generates embedded code for CAN message parsing using structures with bit fields """ |
2 | 2 |
|
3 | 3 | import generator |
| 4 | +import subprocess |
4 | 5 |
|
5 | 6 | # |
6 | 7 | # GENERATION STRINGS |
7 | 8 | # |
| 9 | +gen_git_hash_start = "BEGIN GIT HASH DEF" |
| 10 | +gen_git_hash_stop = "END GIT HASH DEF" |
8 | 11 | gen_id_start = "BEGIN AUTO ID DEFS" |
9 | 12 | gen_id_stop = "END AUTO ID DEFS" |
10 | 13 | gen_dlc_start = "BEGIN AUTO DLC DEFS" |
|
34 | 37 |
|
35 | 38 | DEFAULT_PERIPHERAL = "CAN1" |
36 | 39 |
|
| 40 | +def get_git_hash(): |
| 41 | + """ |
| 42 | + Returns the short git has of the current commit |
| 43 | + """ |
| 44 | + try: |
| 45 | + result = subprocess.run(['git', 'rev-parse', '--short=7', 'HEAD'], |
| 46 | + capture_output=True, text=True, check=True) |
| 47 | + return result.stdout.strip() |
| 48 | + except (subprocess.CalledProcessError, FileNotFoundError): |
| 49 | + return "unknown" |
| 50 | + |
37 | 51 | def find_rx_messages(rx_names): |
38 | 52 | """ |
39 | 53 | Searches the entire config for the definitions of the specified messages |
@@ -187,6 +201,11 @@ def configure_node(node_config, node_paths): |
187 | 201 | with open(node_paths[0], "r") as h_file: |
188 | 202 | h_lines = h_file.readlines() |
189 | 203 |
|
| 204 | + # Git hash definition |
| 205 | + git_hash = get_git_hash() |
| 206 | + git_hash_lines = [f"#define GIT_HASH \"{git_hash}\"\n"] |
| 207 | + h_lines = generator.insert_lines(h_lines, gen_git_hash_start, gen_git_hash_stop, git_hash_lines) |
| 208 | + |
190 | 209 | # with open(node_paths) |
191 | 210 |
|
192 | 211 | # Message IDs and DLCs |
|
0 commit comments