summaryrefslogtreecommitdiff
path: root/tools/package.py
diff options
context:
space:
mode:
authorbernard <[email protected]>2025-08-03 03:57:54 +0800
committerR b b666 <[email protected]>2025-08-07 09:32:45 +0800
commitfbdab95299f0de68079978f73fac33c1de8a81ab (patch)
treef077b70dfb9adb00f6c911bf3e78d1ecd46f05ca /tools/package.py
parent5c568f0280179253d52c41cd23bf7f1ff9925746 (diff)
[Feature][Tools] Add support for package.json, refactor BuildPackage function to handle new format.
Diffstat (limited to 'tools/package.py')
-rw-r--r--tools/package.py96
1 files changed, 72 insertions, 24 deletions
diff --git a/tools/package.py b/tools/package.py
index f7a2ffa745..37a53f47bd 100644
--- a/tools/package.py
+++ b/tools/package.py
@@ -41,41 +41,89 @@ def ExtendPackageVar(package, var):
def BuildPackage(package = None):
if package is None:
package = os.path.join(GetCurrentDir(), 'package.json')
+ elif os.path.isdir(package):
+ # support directory path
+ package = os.path.join(package, 'package.json')
+
+ # get package.json path
+ cwd = os.path.dirname(os.path.abspath(package))
if not os.path.isfile(package):
- print("%s/package.json not found" % GetCurrentDir())
+ # silent return for conditional usage
return []
- f = open(package)
- package_json = f.read()
+ with open(package, 'r') as f:
+ package_json = f.read()
+ package = json.loads(package_json)
- # get package.json path
- cwd = os.path.dirname(package)
+ # check package name
+ if 'name' not in package or 'type' not in package or package['type'] != 'rt-thread-component':
+ return []
- package = json.loads(package_json)
+ # get depends
+ depend = []
+ if 'dependencies' in package:
+ depend = ExtendPackageVar(package, 'dependencies')
- # check package name
- if 'name' not in package or 'type' not in package or package['type'] != 'rt-package':
- return []
+ # check dependencies
+ if depend:
+ group_enable = False
+ for item in depend:
+ if GetDepend(item):
+ group_enable = True
+ break
+ if not group_enable:
+ return []
+
+ CPPDEFINES = []
+ if 'defines' in package:
+ CPPDEFINES = ExtendPackageVar(package, 'defines')
+
+ src = []
+ CPPPATH = []
+ if 'sources' in package:
+ src_depend = []
+ src_CPPPATH = []
+ for item in package['sources']:
+ if 'includes' in item:
+ includes = item['includes']
+ for include in includes:
+ if include.startswith('/') and os.path.isdir(include):
+ src_CPPPATH = src_CPPPATH + [include]
+ else:
+ path = os.path.abspath(os.path.join(cwd, include))
+ src_CPPPATH = src_CPPPATH + [path]
+
+ if 'dependencies' in item:
+ src_depend = src_depend + ExtendPackageVar(item, 'dependencies')
+
+ src_enable = False
+ if src_depend == []:
+ src_enable = True
+ else:
+ for d in src_depend:
+ if GetDepend(d):
+ src_enable = True
+ break
- # get depends
- depend = ExtendPackageVar(package, 'depends')
+ if src_enable:
+ files = []
+ src_files = []
+ if 'files' in item:
+ files += ExtendPackageVar(item, 'files')
- src = []
- if 'source_files' in package:
- for src_file in package['source_files']:
- src_file = os.path.join(cwd, src_file)
- src += Glob(src_file)
+ for item in files:
+ # handle glob patterns relative to package.json directory
+ old_dir = os.getcwd()
+ os.chdir(cwd)
+ try:
+ src_files += Glob(item)
+ finally:
+ os.chdir(old_dir)
- CPPPATH = []
- if 'CPPPATH' in package:
- for path in package['CPPPATH']:
- if path.startswith('/') and os.path.isdir(path):
- CPPPATH = CPPPATH + [path]
- else:
- CPPPATH = CPPPATH + [os.path.join(cwd, path)]
+ src += src_files
- CPPDEFINES = ExtendPackageVar(package, 'CPPDEFINES')
+ CPPPATH += src_CPPPATH
objs = DefineGroup(package['name'], src, depend = depend, CPPPATH = CPPPATH, CPPDEFINES = CPPDEFINES)