package.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #
  2. # File : package.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-04-10 Bernard First version
  23. #
  24. # this script is used to build group with package.json instead of SConscript
  25. import os
  26. import json
  27. from building import *
  28. def ExtendPackageVar(package, var):
  29. v = []
  30. if var not in package:
  31. return v
  32. for item in package[var]:
  33. v = v + [item]
  34. return v
  35. def BuildPackage(package = None):
  36. if package is None:
  37. package = os.path.join(GetCurrentDir(), 'package.json')
  38. elif os.path.isdir(package):
  39. # support directory path
  40. package = os.path.join(package, 'package.json')
  41. # get package.json path
  42. cwd = os.path.dirname(os.path.abspath(package))
  43. if not os.path.isfile(package):
  44. # silent return for conditional usage
  45. return []
  46. with open(package, 'r') as f:
  47. package_json = f.read()
  48. package = json.loads(package_json)
  49. # check package name
  50. if 'name' not in package or 'type' not in package or package['type'] != 'rt-thread-component':
  51. return []
  52. # get depends
  53. depend = []
  54. if 'dependencies' in package:
  55. depend = ExtendPackageVar(package, 'dependencies')
  56. # check dependencies
  57. if depend:
  58. group_enable = False
  59. for item in depend:
  60. if GetDepend(item):
  61. group_enable = True
  62. break
  63. if not group_enable:
  64. return []
  65. CPPDEFINES = []
  66. if 'defines' in package:
  67. CPPDEFINES = ExtendPackageVar(package, 'defines')
  68. src = []
  69. CPPPATH = []
  70. if 'sources' in package:
  71. src_depend = []
  72. src_CPPPATH = []
  73. for item in package['sources']:
  74. if 'includes' in item:
  75. includes = item['includes']
  76. for include in includes:
  77. if include.startswith('/') and os.path.isdir(include):
  78. src_CPPPATH = src_CPPPATH + [include]
  79. else:
  80. path = os.path.abspath(os.path.join(cwd, include))
  81. src_CPPPATH = src_CPPPATH + [path]
  82. if 'dependencies' in item:
  83. src_depend = src_depend + ExtendPackageVar(item, 'dependencies')
  84. src_enable = False
  85. if src_depend == []:
  86. src_enable = True
  87. else:
  88. for d in src_depend:
  89. if GetDepend(d):
  90. src_enable = True
  91. break
  92. if src_enable:
  93. files = []
  94. src_files = []
  95. if 'files' in item:
  96. files += ExtendPackageVar(item, 'files')
  97. for item in files:
  98. # handle glob patterns relative to package.json directory
  99. old_dir = os.getcwd()
  100. os.chdir(cwd)
  101. try:
  102. src_files += Glob(item)
  103. finally:
  104. os.chdir(old_dir)
  105. src += src_files
  106. CPPPATH += src_CPPPATH
  107. objs = DefineGroup(package['name'], src, depend = depend, CPPPATH = CPPPATH, CPPDEFINES = CPPDEFINES)
  108. return objs