to_c_header.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. # Copyright (C) 2024 Grenoble INP - ESISAR. All rights reserved.
  2. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  3. # Python script to convert wasm file to byte array in a .h file
  4. import os
  5. CWD = os.getcwd()
  6. CMAKE_CURRENT_BINARY_DIR = os.getenv('CMAKE_CURRENT_BINARY_DIR', CWD)
  7. CMAKE_CURRENT_SOURCE_DIR = os.getenv('CMAKE_CURRENT_SOURCE_DIR', f'{CWD}/../src')
  8. LICENCE_HEADER = """/*
  9. * Copyright (c) 2017 Linaro Limited
  10. * Copyright (C) 2024 Grenoble INP - ESISAR Limited
  11. *
  12. * SPDX-License-Identifier: Apache-2.0
  13. */
  14. """
  15. print('CMAKE_CURRENT_BINARY_DIR:', CMAKE_CURRENT_BINARY_DIR)
  16. print('CMAKE_CURRENT_SOURCE_DIR:', CMAKE_CURRENT_SOURCE_DIR)
  17. # Open the wasm file in binary mode and read the data
  18. with open(f'{CWD}/wasm-apps/http_get.wasm', 'rb') as f:
  19. wasm_bytes = f.read()
  20. # Convert the bytes to a comma-separated string of hex values
  21. byte_array = ', '.join(f'0x{byte:02x}' for byte in wasm_bytes)
  22. # Create the output string
  23. output = f'unsigned char __aligned(4) wasm_test_file[] = {{ {byte_array} }};'
  24. # Write the output string to the .h file
  25. with open(f'{CWD}/src/http_get.h', 'w') as f:
  26. f.write(output)