|
| 1 | +import base64 |
| 2 | +import json |
| 3 | +import os |
| 4 | +import sys |
| 5 | +import urllib.request |
| 6 | +from datetime import datetime |
| 7 | + |
| 8 | +""" |
| 9 | +This python module generates esp_stubs.c/h files from a given version and url, with |
| 10 | +<stub_download_url>/v<stub_version>/esp32xx.json as the required format. |
| 11 | +
|
| 12 | +It is also possible to override stub generation to use a local folder for testing purposes. |
| 13 | +""" |
| 14 | + |
| 15 | +# Paths |
| 16 | +stub_version = sys.argv[1] |
| 17 | +stub_download_url = sys.argv[2] |
| 18 | +root_path = sys.argv[3] |
| 19 | +stub_override_path = sys.argv[4] if len(sys.argv) == 5 else None |
| 20 | + |
| 21 | +template_path = os.path.join(root_path, "cmake") |
| 22 | +priv_inc_path = os.path.join(root_path, "private_include") |
| 23 | +src_path = os.path.join(root_path, "src") |
| 24 | +h_template_path = os.path.join(template_path, "esp_stubs.h.template") |
| 25 | +c_template_path = os.path.join(template_path, "esp_stubs.c.template") |
| 26 | +hfile_path = os.path.join(priv_inc_path, "esp_stubs.h") |
| 27 | +cfile_path = os.path.join(src_path, "esp_stubs.c") |
| 28 | +current_year = datetime.now().year |
| 29 | + |
| 30 | +# Matches order of target_chip_t enumeration |
| 31 | +files_to_download = [ |
| 32 | + None, # ESP8266_CHIP |
| 33 | + "esp32.json", # ESP32_CHIP |
| 34 | + "esp32s2.json", # ESP32S2_CHIP |
| 35 | + "esp32c3.json", # ESP32C3_CHIP |
| 36 | + "esp32s3.json", # ESP32S3_CHIP |
| 37 | + "esp32c2.json", # ESP32C2_CHIP |
| 38 | + None, # ESP32_RESERVED0_CHIP |
| 39 | + "esp32h2.json", # ESP32H2_CHIP |
| 40 | + "esp32c6.json", # ESP32C6_CHIP |
| 41 | +] |
| 42 | + |
| 43 | +def read_stub_json(json_file): |
| 44 | + stub = json.load(json_file) |
| 45 | + entry = stub["entry"] |
| 46 | + text = base64.b64decode(stub["text"]) |
| 47 | + text_start = stub["text_start"] |
| 48 | + try: |
| 49 | + data = base64.b64decode(stub["data"]) |
| 50 | + data_start = stub["data_start"] |
| 51 | + except KeyError: |
| 52 | + data = None |
| 53 | + data_start = None |
| 54 | + |
| 55 | + text_str = ", ".join([hex(b) for b in text]) |
| 56 | + text_size = len(text) |
| 57 | + data_str = "" if data is None else ", ".join([hex(b) for b in data]) |
| 58 | + data_size = 0 if data is None else len(data) |
| 59 | + data_start = 0 if data_start is None else data_start |
| 60 | + |
| 61 | + stub_data = f""" // {file_to_download} |
| 62 | + {{ |
| 63 | + .header = {{ |
| 64 | + .entrypoint = {entry}, |
| 65 | + }}, |
| 66 | + .segments = {{ |
| 67 | + {{ |
| 68 | + .addr = {text_start}, |
| 69 | + .size = {text_size}, |
| 70 | + .data = (uint8_t[]){{{text_str}}}, |
| 71 | + }}, |
| 72 | + {{ |
| 73 | + .addr = {data_start}, |
| 74 | + .size = {data_size}, |
| 75 | + .data = (uint8_t[]){{{data_str}}}, |
| 76 | + }}, |
| 77 | + }}, |
| 78 | + }}, |
| 79 | +
|
| 80 | +""" |
| 81 | + return stub_data |
| 82 | + |
| 83 | + |
| 84 | +if __name__ == "__main__": |
| 85 | + # .h and .c file templates |
| 86 | + with open(h_template_path, "r") as h_template_file, open(c_template_path, "r") as c_template_file: |
| 87 | + h_template = h_template_file.read() |
| 88 | + c_template = c_template_file.read() |
| 89 | + |
| 90 | + # .h and .c file to generate |
| 91 | + with open(hfile_path, "w") as hfile, open(cfile_path, "w") as cfile: |
| 92 | + hfile.write( |
| 93 | + h_template.format( |
| 94 | + current_year=current_year, |
| 95 | + stub_version=stub_version, |
| 96 | + ) |
| 97 | + ) |
| 98 | + |
| 99 | + cfile.write( |
| 100 | + c_template.format( |
| 101 | + current_year=current_year, |
| 102 | + stub_version=stub_version, |
| 103 | + max_chip_number=len(files_to_download), |
| 104 | + ) |
| 105 | + ) |
| 106 | + |
| 107 | + for file_to_download in files_to_download: |
| 108 | + if file_to_download is None: |
| 109 | + cfile.write(" // placeholder\n" " {},\n" "\n") |
| 110 | + else: |
| 111 | + if stub_override_path: |
| 112 | + with open(f"{stub_override_path}/{file_to_download}") as file_path: |
| 113 | + cfile.write(read_stub_json(file_path)) |
| 114 | + else: |
| 115 | + with urllib.request.urlopen(f"{stub_download_url}/v{stub_version}/{file_to_download}") as url: |
| 116 | + cfile.write(read_stub_json(url)) |
| 117 | + |
| 118 | + cfile.write("};\n" "\n" "#endif\n") |
0 commit comments