xmake.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. """
  2. Utils for CMake
  3. Author: https://github.com/klivelinux
  4. """
  5. import os
  6. import utils
  7. from string import Template
  8. import rtconfig
  9. from utils import _make_path_relative
  10. class XmakeProject:
  11. def __init__(self, env, project):
  12. self.env = env
  13. self.project = project
  14. self.sdkdir = ""
  15. self.bindir = ""
  16. self.toolchain = ""
  17. self.src_path = ""
  18. self.inc_path = ""
  19. self.cflags = ""
  20. self.cxxflags = ""
  21. self.ldflags = ""
  22. self.asflags = ""
  23. self.define = ""
  24. def set_toolchain_path(self):
  25. self.bindir = os.path.abspath(rtconfig.EXEC_PATH).replace('\\', "/")
  26. self.sdkdir = self.bindir[:-4]
  27. # delete -
  28. self.toolchain = rtconfig.PREFIX[:-1]
  29. def set_target_config(self):
  30. info = utils.ProjectInfo(self.env)
  31. # 1. config src path
  32. for group in self.project:
  33. for f in group['src']:
  34. # use relative path
  35. path = _make_path_relative(os.getcwd(), os.path.normpath(f.rfile().abspath))
  36. self.src_path += "\t\"{0}\",\n".format(path.replace("\\", "/"))
  37. self.src_path = self.src_path[:-2]
  38. # 2. config dir path
  39. for i in info['CPPPATH']:
  40. # use relative path
  41. path = _make_path_relative(os.getcwd(), i)
  42. self.inc_path += "\t\"{0}\",\n".format(path.replace("\\", "/"))
  43. self.inc_path = self.inc_path[:-2]
  44. # 3. config cflags
  45. self.cflags = rtconfig.CFLAGS.replace('\\', "/").replace('\"', "\\\"")
  46. # 4. config cxxflags
  47. if 'CXXFLAGS' in dir(rtconfig):
  48. self.cxxflags = rtconfig.CXXFLAGS.replace('\\', "/").replace('\"', "\\\"")
  49. else:
  50. self.cxxflags = self.cflags
  51. # 5. config asflags
  52. self.asflags = rtconfig.AFLAGS.replace('\\', "/").replace('\"', "\\\"")
  53. # 6. config lflags
  54. self.ldflags = rtconfig.LFLAGS.replace('\\', "/").replace('\"', "\\\"")
  55. # 7. config define
  56. for i in info['CPPDEFINES']:
  57. self.define += "\t\"{0}\",\n".format(i)
  58. self.define = self.define[:-2]
  59. def generate_xmake_file(self):
  60. if os.getenv('RTT_ROOT'):
  61. RTT_ROOT = os.getenv('RTT_ROOT')
  62. else:
  63. RTT_ROOT = os.path.normpath(os.getcwd() + '/../../..')
  64. template_path = os.path.join(RTT_ROOT, "tools", "xmake.lua")
  65. with open(template_path, "r") as f:
  66. data = f.read()
  67. data = Template(data)
  68. data = data.safe_substitute(toolchain=self.toolchain, sdkdir=self.sdkdir, bindir=self.bindir, src_path=self.src_path, inc_path=self.inc_path,
  69. define=self.define, cflags=self.cflags, cxxflags=self.cxxflags, asflags=self.asflags,
  70. ldflags=self.ldflags, target="rt-thread")
  71. with open("xmake.lua", "w") as f:
  72. f.write(data)
  73. def XMakeProject(env,project):
  74. print('Update setting files for xmake.lua...')
  75. xmake_project = XmakeProject(env, project)
  76. xmake_project.set_toolchain_path()
  77. xmake_project.set_target_config()
  78. xmake_project.generate_xmake_file()
  79. print('Done!')
  80. return