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