generate_toolchain.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/usr/bin/python3
  2. # Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. import os
  5. import sys
  6. import shutil
  7. from optparse import OptionParser
  8. import re
  9. optParser = OptionParser()
  10. optParser.add_option("-o", "", dest="output_dir", help="the output path of sysroot")
  11. optParser.add_option("-f", "--api_file", dest="api_file_name",
  12. help="append user defined APIs to toolchain")
  13. (options, args) = optParser.parse_args()
  14. optParser.usage = "%prog [options] output_dir"
  15. sysroot_path = os.path.join(os.getcwd(), "sysroot")
  16. if not os.path.isdir(sysroot_path):
  17. print("Error: No sysroot folder in current path.")
  18. exit(0)
  19. if options.output_dir == None:
  20. out_dir = sysroot_path
  21. else:
  22. out_dir = os.path.join(os.path.abspath(options.output_dir), "sysroot")
  23. def check_sysroot():
  24. if out_dir != sysroot_path and not os.path.isdir(out_dir):
  25. try:
  26. shutil.copytree(sysroot_path,out_dir)
  27. except:
  28. print('Error while copy sysroot')
  29. def Search_API(pattern, file):
  30. f = open(file, 'r')
  31. content = f.read()
  32. f.close()
  33. p = re.compile(pattern, re.S | re.M)
  34. return p.findall(content)
  35. def fill_defined_symbols():
  36. wamr_root = os.path.join(os.getcwd(), "../..")
  37. user_lib_dir = os.path.join(wamr_root, "core/iwasm/lib/native/extension")
  38. defined_symbols = []
  39. # WAMR extension APIs
  40. for lib in os.listdir(user_lib_dir):
  41. for file in os.listdir(os.path.join(user_lib_dir, lib)):
  42. if re.match(r'.*.inl', file):
  43. defined_symbols += Search_API(r'EXPORT_WASM_API[(](.*?)[)]', os.path.join(user_lib_dir, lib, file))
  44. # Base lib APIs
  45. defined_symbols += Search_API(r'EXPORT_WASM_API[(](.*?)[)]',
  46. os.path.join(wamr_root, "core/iwasm/lib/native/base", "base_lib_export.c"))
  47. # libc
  48. defined_symbols += Search_API(r'REG_NATIVE_FUNC[(]env, _(.*?)[)]',
  49. os.path.join(wamr_root, "core/iwasm/lib/native/libc", "libc_wrapper.c"))
  50. # User defined APIs
  51. if options.api_file_name != None:
  52. api_file = open(options.api_file_name)
  53. APIs = api_file.read().split('\n')
  54. if APIs != None and APIs != []:
  55. defined_symbols += APIs
  56. defined_symbols = [i for i in defined_symbols if len(i) != 0]
  57. symbol_file = open(os.path.join(out_dir, "share/defined-symbols.txt"), 'w')
  58. symbol_file.write('\n'.join(defined_symbols))
  59. symbol_file.close()
  60. def generate_toolchain_file():
  61. cmake_content = """
  62. SET(CMAKE_SYSTEM_NAME Linux)
  63. SET(CMAKE_SYSTEM_PROCESSOR wasm32)
  64. SET (CMAKE_SYSROOT {})
  65. SET (CMAKE_C_FLAGS "-nostdlib" CACHE INTERNAL "")
  66. SET (CMAKE_C_COMPILER_TARGET "wasm32")
  67. SET (CMAKE_C_COMPILER "clang-8")
  68. SET (CMAKE_CXX_FLAGS "-nostdlib" CACHE INTERNAL "")
  69. SET (CMAKE_CXX_COMPILER_TARGET "wasm32")
  70. SET (CMAKE_CXX_COMPILER "clang++-8")
  71. SET (CMAKE_EXE_LINKER_FLAGS "-Wl,--no-entry,--export-all,--allow-undefined-file=${{CMAKE_SYSROOT}}/share/defined-symbols.txt" CACHE INTERNAL "")
  72. SET (CMAKE_LINKER "/usr/bin/wasm-ld-8" CACHE INTERNAL "")
  73. SET (CMAKE_AR "/usr/bin/llvm-ar-8" CACHE INTERNAL "")
  74. SET (CMAKE_NM "/usr/bin/llvm-nm-8" CACHE INTERNAL "")
  75. SET (CMAKE_OBJDUMP "/usr/bin/llvm-objdump-8" CACHE INTERNAL "")
  76. SET (CMAKE_RANLIB "/usr/bin/llvm-ranlib-8" CACHE INTERNAL "")
  77. """.format(out_dir)
  78. f = open(os.path.join(out_dir, "..", "wamr_toolchain.cmake"), 'w')
  79. f.write(cmake_content)
  80. f.close()
  81. def main():
  82. check_sysroot()
  83. fill_defined_symbols()
  84. generate_toolchain_file()
  85. print("Successfully generate wamr toolchain")
  86. print("Now you can use this command to build your cmake based project:")
  87. print("cmake /path/to/CMakeLists.txt -DCMAKE_TOOLCHAIN_FILE={}"
  88. .format(os.path.abspath(os.path.join(out_dir, "..", "wamr_toolchain.cmake"))))
  89. if __name__ == '__main__':
  90. main()