generate_toolchain.py 4.4 KB

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