1
0

makefile.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #
  2. # File : makefile.py
  3. # This file is part of RT-Thread RTOS
  4. # COPYRIGHT (C) 2006 - 2015, RT-Thread Development Team
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License along
  17. # with this program; if not, write to the Free Software Foundation, Inc.,
  18. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. #
  20. # Change Logs:
  21. # Date Author Notes
  22. # 2015-01-20 Bernard Add copyright information
  23. import os
  24. import sys
  25. # Add parent directory to path to import utils
  26. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  27. from utils import *
  28. from utils import _make_path_relative
  29. import rtconfig
  30. makefile = '''phony := all
  31. all:
  32. include config.mk
  33. ifneq ($(MAKE_LIB),1)
  34. TARGET := rtthread.elf
  35. include src.mk
  36. endif
  37. $(if $(strip $(RTT_ROOT)),,$(error RTT_ROOT not defined))
  38. include $(RTT_ROOT)/tools/rtthread.mk
  39. '''
  40. def TargetMakefile(env):
  41. project = ProjectInfo(env)
  42. BSP_ROOT = os.path.abspath(env['BSP_ROOT'])
  43. RTT_ROOT = os.path.abspath(env['RTT_ROOT'])
  44. match_bsp = False
  45. if BSP_ROOT.startswith(RTT_ROOT):
  46. match_bsp = True
  47. make = open('config.mk', 'w')
  48. make.write('BSP_ROOT ?= %s\n' % BSP_ROOT.replace('\\', '/'))
  49. make.write('RTT_ROOT ?= %s\n' % RTT_ROOT.replace('\\', '/'))
  50. make.write('\n')
  51. cross = os.path.abspath(rtconfig.EXEC_PATH)
  52. cross = os.path.join(cross, rtconfig.PREFIX)
  53. make.write('CROSS_COMPILE ?=%s' % cross.replace('\\', '\\\\'))
  54. make.write('\n')
  55. make.write('\n')
  56. make.write('CFLAGS :=%s' % (rtconfig.CFLAGS))
  57. make.write('\n')
  58. make.write('AFLAGS :=%s' % (rtconfig.AFLAGS))
  59. make.write('\n')
  60. make.write('LFLAGS :=%s' % (rtconfig.LFLAGS))
  61. make.write('\n')
  62. if 'CXXFLAGS' in dir(rtconfig):
  63. make.write('CXXFLAGS :=%s' % (rtconfig.CXXFLAGS))
  64. make.write('\n')
  65. if ('LIBS' in env):
  66. make.write('EXTERN_LIB := ')
  67. for tlib in env['LIBS']:
  68. make.write('-l%s ' % (tlib))
  69. if ('LIBPATH' in env):
  70. for tlibpath in env['LIBPATH']:
  71. make.write('-L%s ' % (tlibpath))
  72. make.write('\n')
  73. make.write('\n')
  74. Files = project['FILES']
  75. Headers = project['HEADERS']
  76. CPPDEFINES = project['CPPDEFINES']
  77. paths = [os.path.normpath(i) for i in project['CPPPATH']]
  78. CPPPATH = []
  79. for path in paths:
  80. fn = os.path.normpath(path)
  81. if match_bsp:
  82. if fn.startswith(BSP_ROOT):
  83. fn = '$(BSP_ROOT)' + fn.replace(BSP_ROOT, '')
  84. elif fn.startswith(RTT_ROOT):
  85. fn = '$(RTT_ROOT)' + fn.replace(RTT_ROOT, '')
  86. else:
  87. if fn.startswith(RTT_ROOT):
  88. fn = '$(RTT_ROOT)' + fn.replace(RTT_ROOT, '')
  89. elif fn.startswith(BSP_ROOT):
  90. fn = '$(BSP_ROOT)' + fn.replace(BSP_ROOT, '')
  91. CPPPATH.append(fn)
  92. path = ''
  93. paths = CPPPATH
  94. for item in paths:
  95. path += '\t-I%s \\\n' % item
  96. make.write('CPPPATHS :=')
  97. if path[0] == '\t': path = path[1:]
  98. length = len(path)
  99. if path[length - 2] == '\\': path = path[:length - 2]
  100. make.write(path)
  101. make.write('\n')
  102. make.write('\n')
  103. defines = ''
  104. for item in project['CPPDEFINES']:
  105. defines += ' -D%s' % item
  106. make.write('DEFINES :=')
  107. make.write(defines)
  108. make.write('\n')
  109. files = Files
  110. Files = []
  111. for file in files:
  112. fn = os.path.normpath(file)
  113. if match_bsp:
  114. if fn.startswith(BSP_ROOT):
  115. fn = '$(BSP_ROOT)' + fn.replace(BSP_ROOT, '')
  116. elif fn.startswith(RTT_ROOT):
  117. fn = '$(RTT_ROOT)' + fn.replace(RTT_ROOT, '')
  118. else:
  119. if fn.startswith(RTT_ROOT):
  120. fn = '$(RTT_ROOT)' + fn.replace(RTT_ROOT, '')
  121. elif fn.startswith(BSP_ROOT):
  122. fn = '$(BSP_ROOT)' + fn.replace(BSP_ROOT, '')
  123. Files.append(fn)
  124. # print(fn)
  125. src = open('src.mk', 'w')
  126. files = Files
  127. src.write('SRC_FILES :=\n')
  128. for item in files:
  129. src.write('SRC_FILES +=%s\n' % item.replace('\\', '/'))
  130. make = open('Makefile', 'w')
  131. make.write(makefile)
  132. make.close()
  133. return