1425bb815Sopenharmony_ci#!/usr/bin/env python
2425bb815Sopenharmony_ci
3425bb815Sopenharmony_ci# Copyright JS Foundation and other contributors, http://js.foundation
4425bb815Sopenharmony_ci#
5425bb815Sopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
6425bb815Sopenharmony_ci# you may not use this file except in compliance with the License.
7425bb815Sopenharmony_ci# You may obtain a copy of the License at
8425bb815Sopenharmony_ci#
9425bb815Sopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
10425bb815Sopenharmony_ci#
11425bb815Sopenharmony_ci# Unless required by applicable law or agreed to in writing, software
12425bb815Sopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS
13425bb815Sopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14425bb815Sopenharmony_ci# See the License for the specific language governing permissions and
15425bb815Sopenharmony_ci# limitations under the License.
16425bb815Sopenharmony_ci#
17425bb815Sopenharmony_ciimport fnmatch
18425bb815Sopenharmony_ciimport os
19425bb815Sopenharmony_ci
20425bb815Sopenharmony_cidef build_soft_links(project_path, jerry_path):
21425bb815Sopenharmony_ci    """ Creates soft links into the @project_path. """
22425bb815Sopenharmony_ci
23425bb815Sopenharmony_ci    if not os.path.exists(project_path):
24425bb815Sopenharmony_ci        os.makedirs(project_path)
25425bb815Sopenharmony_ci
26425bb815Sopenharmony_ci    links = [
27425bb815Sopenharmony_ci        { # arc
28425bb815Sopenharmony_ci            'src': os.path.join('targets', 'curie_bsp', 'jerry_app', 'arc'),
29425bb815Sopenharmony_ci            'link_name': 'arc'
30425bb815Sopenharmony_ci        },
31425bb815Sopenharmony_ci        { # include
32425bb815Sopenharmony_ci            'src': os.path.join('targets', 'curie_bsp', 'jerry_app', 'include'),
33425bb815Sopenharmony_ci            'link_name': 'include'
34425bb815Sopenharmony_ci        },
35425bb815Sopenharmony_ci        { # quark
36425bb815Sopenharmony_ci            'src': os.path.join('targets', 'curie_bsp', 'jerry_app', 'quark'),
37425bb815Sopenharmony_ci            'link_name': 'quark'
38425bb815Sopenharmony_ci        },
39425bb815Sopenharmony_ci        { # quark/jerryscript
40425bb815Sopenharmony_ci            'src': jerry_path,
41425bb815Sopenharmony_ci            'link_name': os.path.join('quark', 'jerryscript')
42425bb815Sopenharmony_ci        }
43425bb815Sopenharmony_ci    ]
44425bb815Sopenharmony_ci
45425bb815Sopenharmony_ci    for link in links:
46425bb815Sopenharmony_ci        src = os.path.join(jerry_path, link['src'])
47425bb815Sopenharmony_ci        link_name = os.path.join(project_path, link['link_name'])
48425bb815Sopenharmony_ci        if not os.path.islink(link_name):
49425bb815Sopenharmony_ci            os.symlink(src, link_name)
50425bb815Sopenharmony_ci            print("Created symlink '{link_name}' -> '{src}'".format(src=src, link_name=link_name))
51425bb815Sopenharmony_ci
52425bb815Sopenharmony_ci
53425bb815Sopenharmony_cidef find_sources(root_dir, sub_dir):
54425bb815Sopenharmony_ci    """
55425bb815Sopenharmony_ci    Find .c and .S files inside the @root_dir/@sub_dir directory.
56425bb815Sopenharmony_ci    Note: the returned paths will be relative to the @root_dir directory.
57425bb815Sopenharmony_ci    """
58425bb815Sopenharmony_ci    src_dir = os.path.join(root_dir, sub_dir)
59425bb815Sopenharmony_ci
60425bb815Sopenharmony_ci    matches = []
61425bb815Sopenharmony_ci    for root, dirnames, filenames in os.walk(src_dir):
62425bb815Sopenharmony_ci        for filename in fnmatch.filter(filenames, '*.[c|S]'):
63425bb815Sopenharmony_ci            file_path = os.path.join(root, filename)
64425bb815Sopenharmony_ci            relative_path = os.path.relpath(file_path, root_dir)
65425bb815Sopenharmony_ci            matches.append(relative_path)
66425bb815Sopenharmony_ci
67425bb815Sopenharmony_ci    return matches
68425bb815Sopenharmony_ci
69425bb815Sopenharmony_ci
70425bb815Sopenharmony_cidef build_jerry_data(jerry_path):
71425bb815Sopenharmony_ci    """
72425bb815Sopenharmony_ci    Build up a dictionary which contains the following items:
73425bb815Sopenharmony_ci     - sources: list of JerryScript sources which should be built.
74425bb815Sopenharmony_ci     - dirs: list of JerryScript dirs used.
75425bb815Sopenharmony_ci     - cflags: CFLAGS for the build.
76425bb815Sopenharmony_ci    """
77425bb815Sopenharmony_ci    jerry_sources = []
78425bb815Sopenharmony_ci    jerry_dirs = set()
79425bb815Sopenharmony_ci    for sub_dir in ['jerry-core', 'jerry-libm', os.path.join('targets', 'curie_bsp', 'source')]:
80425bb815Sopenharmony_ci        for file in find_sources(os.path.normpath(jerry_path), sub_dir):
81425bb815Sopenharmony_ci            path = os.path.join('jerryscript', file)
82425bb815Sopenharmony_ci            jerry_sources.append(path)
83425bb815Sopenharmony_ci            jerry_dirs.add(os.path.split(path)[0])
84425bb815Sopenharmony_ci
85425bb815Sopenharmony_ci    jerry_cflags = [
86425bb815Sopenharmony_ci        '-DJERRY_GLOBAL_HEAP_SIZE=10',
87425bb815Sopenharmony_ci        '-DJERRY_NDEBUG',
88425bb815Sopenharmony_ci        '-DJERRY_DISABLE_HEAVY_DEBUG',
89425bb815Sopenharmony_ci        '-DJERRY_BUILTIN_NUMBER=0',
90425bb815Sopenharmony_ci        '-DJERRY_BUILTIN_STRING=0',
91425bb815Sopenharmony_ci        '-DJERRY_BUILTIN_BOOLEAN=0',
92425bb815Sopenharmony_ci        #'-DJERRY_BUILTIN_ERRORS=0',
93425bb815Sopenharmony_ci        '-DJERRY_BUILTIN_ARRAY=0',
94425bb815Sopenharmony_ci        '-DJERRY_BUILTIN_MATH=0',
95425bb815Sopenharmony_ci        '-DJERRY_BUILTIN_JSON=0',
96425bb815Sopenharmony_ci        '-DJERRY_BUILTIN_DATE=0',
97425bb815Sopenharmony_ci        '-DJERRY_BUILTIN_REGEXP=0',
98425bb815Sopenharmony_ci        '-DJERRY_BUILTIN_ANNEXB=0',
99425bb815Sopenharmony_ci        '-DJERRY_ES2015=0',
100425bb815Sopenharmony_ci        '-DJERRY_LCACHE=0',
101425bb815Sopenharmony_ci        '-DJERRY_PROPRETY_HASHMAP=0',
102425bb815Sopenharmony_ci    ]
103425bb815Sopenharmony_ci
104425bb815Sopenharmony_ci    return {
105425bb815Sopenharmony_ci        'sources': jerry_sources,
106425bb815Sopenharmony_ci        'dirs': jerry_dirs,
107425bb815Sopenharmony_ci        'cflags': jerry_cflags,
108425bb815Sopenharmony_ci    }
109425bb815Sopenharmony_ci
110425bb815Sopenharmony_ci
111425bb815Sopenharmony_cidef write_file(path, content):
112425bb815Sopenharmony_ci    """ Writes @content into the file at specified by the @path. """
113425bb815Sopenharmony_ci    norm_path = os.path.normpath(path)
114425bb815Sopenharmony_ci    with open(norm_path, "w+") as f:
115425bb815Sopenharmony_ci        f.write(content)
116425bb815Sopenharmony_ci    print("Wrote file '{0}'".format(norm_path))
117425bb815Sopenharmony_ci
118425bb815Sopenharmony_ci
119425bb815Sopenharmony_cidef build_obj_y(source_list):
120425bb815Sopenharmony_ci    """
121425bb815Sopenharmony_ci    Build obj-y additions from the @source_list.
122425bb815Sopenharmony_ci    Note: the input sources should have their file extensions.
123425bb815Sopenharmony_ci    """
124425bb815Sopenharmony_ci    return '\n'.join(['obj-y += {0}.o'.format(os.path.splitext(fname)[0]) for fname in source_list])
125425bb815Sopenharmony_ci
126425bb815Sopenharmony_ci
127425bb815Sopenharmony_cidef build_cflags_y(cflags_list):
128425bb815Sopenharmony_ci    """
129425bb815Sopenharmony_ci    Build cflags-y additions from the @cflags_list.
130425bb815Sopenharmony_ci    Note: the input sources should have their file extensions.
131425bb815Sopenharmony_ci    """
132425bb815Sopenharmony_ci    return '\n'.join(['cflags-y += {0}'.format(cflag) for cflag in cflags_list])
133425bb815Sopenharmony_ci
134425bb815Sopenharmony_ci
135425bb815Sopenharmony_cidef build_mkdir(dir_list):
136425bb815Sopenharmony_ci    """ Build mkdir calls for each dir in the @dir_list. """
137425bb815Sopenharmony_ci    return '\n'.join(['\t$(AT)mkdir -p {0}'.format(os.path.join('$(OUT_SRC)', path)) for path in dir_list])
138425bb815Sopenharmony_ci
139425bb815Sopenharmony_ci
140425bb815Sopenharmony_cidef create_root_kbuild(project_path):
141425bb815Sopenharmony_ci    """ Creates @project_path/Kbuild.mk file. """
142425bb815Sopenharmony_ci
143425bb815Sopenharmony_ci    root_kbuild_path = os.path.join(project_path, 'Kbuild.mk')
144425bb815Sopenharmony_ci    root_kbuild_content = '''
145425bb815Sopenharmony_ciobj-$(CONFIG_QUARK_SE_ARC) += arc/
146425bb815Sopenharmony_ciobj-$(CONFIG_QUARK_SE_QUARK) += quark/
147425bb815Sopenharmony_ci'''
148425bb815Sopenharmony_ci    write_file(root_kbuild_path, root_kbuild_content)
149425bb815Sopenharmony_ci
150425bb815Sopenharmony_ci
151425bb815Sopenharmony_cidef create_root_makefile(project_path):
152425bb815Sopenharmony_ci    """ Creates @project_path/Makefile file. """
153425bb815Sopenharmony_ci
154425bb815Sopenharmony_ci    root_makefile_path = os.path.join(project_path, 'Makefile')
155425bb815Sopenharmony_ci    root_makefile_content = '''
156425bb815Sopenharmony_ciTHIS_DIR   := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
157425bb815Sopenharmony_ciT          := $(abspath $(THIS_DIR)/../..)
158425bb815Sopenharmony_ciPROJECT    := {project_name}
159425bb815Sopenharmony_ciBOARD        := curie_101
160425bb815Sopenharmony_ciifeq ($(filter curie_101, $(BOARD)),)
161425bb815Sopenharmony_ci$(error The curie jerry sample application can only run on the curie_101 Board)
162425bb815Sopenharmony_ciendif
163425bb815Sopenharmony_ciBUILDVARIANT ?= debug
164425bb815Sopenharmony_ciquark_DEFCONFIG = $(PROJECT_PATH)/quark/defconfig
165425bb815Sopenharmony_ciarc_DEFCONFIG = $(PROJECT_PATH)/arc/defconfig
166425bb815Sopenharmony_ci
167425bb815Sopenharmony_ci# Optional: set the default version
168425bb815Sopenharmony_ciVERSION_MAJOR  := 1
169425bb815Sopenharmony_ciVERSION_MINOR  := 0
170425bb815Sopenharmony_ciVERSION_PATCH  := 0
171425bb815Sopenharmony_ciinclude $(T)/build/project.mk
172425bb815Sopenharmony_ci'''.format(project_name=project_name)
173425bb815Sopenharmony_ci
174425bb815Sopenharmony_ci    write_file(root_makefile_path, root_makefile_content)
175425bb815Sopenharmony_ci
176425bb815Sopenharmony_ci
177425bb815Sopenharmony_cidef create_arc_kbuild(project_path):
178425bb815Sopenharmony_ci    """ Creates @project_path/arc/Kbuild.mk file. """
179425bb815Sopenharmony_ci
180425bb815Sopenharmony_ci    arc_path = os.path.join(project_path, 'arc')
181425bb815Sopenharmony_ci    arc_kbuild_path = os.path.join(arc_path, 'Kbuild.mk')
182425bb815Sopenharmony_ci    arc_sources = find_sources(arc_path, '.')
183425bb815Sopenharmony_ci    arc_kbuild_content = build_obj_y(arc_sources)
184425bb815Sopenharmony_ci
185425bb815Sopenharmony_ci    write_file(arc_kbuild_path, arc_kbuild_content)
186425bb815Sopenharmony_ci
187425bb815Sopenharmony_ci
188425bb815Sopenharmony_cidef create_quark_kbuild(project_path, jerry_path):
189425bb815Sopenharmony_ci    """ Creates @project_path/quark/Kbuild.mk file. """
190425bb815Sopenharmony_ci    quark_kbuild_path = os.path.join(project_path, 'quark', 'Kbuild.mk')
191425bb815Sopenharmony_ci
192425bb815Sopenharmony_ci    # Extract a few JerryScript related data
193425bb815Sopenharmony_ci    jerry_data = build_jerry_data(jerry_path)
194425bb815Sopenharmony_ci    jerry_objects = build_obj_y(jerry_data['sources'])
195425bb815Sopenharmony_ci    jerry_defines = jerry_data['cflags']
196425bb815Sopenharmony_ci    jerry_build_dirs = build_mkdir(jerry_data['dirs'])
197425bb815Sopenharmony_ci
198425bb815Sopenharmony_ci    quark_include_paths = [
199425bb815Sopenharmony_ci        'include',
200425bb815Sopenharmony_ci        'jerryscript',
201425bb815Sopenharmony_ci        os.path.join('jerryscript', 'jerry-libm', 'include'),
202425bb815Sopenharmony_ci        os.path.join('jerryscript', 'targets' ,'curie_bsp', 'include')
203425bb815Sopenharmony_ci    ] + list(jerry_data['dirs'])
204425bb815Sopenharmony_ci
205425bb815Sopenharmony_ci    quark_includes = [
206425bb815Sopenharmony_ci        '-Wno-error',
207425bb815Sopenharmony_ci    ] + ['-I%s' % os.path.join(project_path, 'quark', path) for path in quark_include_paths]
208425bb815Sopenharmony_ci
209425bb815Sopenharmony_ci    quark_cflags = build_cflags_y(jerry_defines + quark_includes)
210425bb815Sopenharmony_ci
211425bb815Sopenharmony_ci    quark_kbuild_content = '''
212425bb815Sopenharmony_ci{cflags}
213425bb815Sopenharmony_ci
214425bb815Sopenharmony_ciobj-y += main.o
215425bb815Sopenharmony_ci{objects}
216425bb815Sopenharmony_ci
217425bb815Sopenharmony_cibuild_dirs:
218425bb815Sopenharmony_ci{dirs}
219425bb815Sopenharmony_ci
220425bb815Sopenharmony_ci$(OUT_SRC): build_dirs
221425bb815Sopenharmony_ci'''.format(objects=jerry_objects, cflags=quark_cflags, dirs=jerry_build_dirs)
222425bb815Sopenharmony_ci
223425bb815Sopenharmony_ci    write_file(quark_kbuild_path, quark_kbuild_content)
224425bb815Sopenharmony_ci
225425bb815Sopenharmony_ci
226425bb815Sopenharmony_cidef main(curie_path, project_name, jerry_path):
227425bb815Sopenharmony_ci    project_path = os.path.join(curie_path, 'wearable_device_sw', 'projects', project_name)
228425bb815Sopenharmony_ci
229425bb815Sopenharmony_ci    build_soft_links(project_path, jerry_path)
230425bb815Sopenharmony_ci
231425bb815Sopenharmony_ci    create_root_kbuild(project_path)
232425bb815Sopenharmony_ci    create_root_makefile(project_path)
233425bb815Sopenharmony_ci    create_arc_kbuild(project_path)
234425bb815Sopenharmony_ci    create_quark_kbuild(project_path, jerry_path)
235425bb815Sopenharmony_ci
236425bb815Sopenharmony_ci
237425bb815Sopenharmony_ciif __name__ == '__main__':
238425bb815Sopenharmony_ci    import sys
239425bb815Sopenharmony_ci
240425bb815Sopenharmony_ci    if len(sys.argv) != 2:
241425bb815Sopenharmony_ci        print('Usage:')
242425bb815Sopenharmony_ci        print('{script_name} [full or relative path of Curie_BSP]'.format(script_name=sys.argv[0]))
243425bb815Sopenharmony_ci        sys.exit(1)
244425bb815Sopenharmony_ci
245425bb815Sopenharmony_ci    project_name = 'curie_bsp_jerry'
246425bb815Sopenharmony_ci
247425bb815Sopenharmony_ci    file_dir = os.path.dirname(os.path.abspath(__file__))
248425bb815Sopenharmony_ci    jerry_path = os.path.join(file_dir, "..", "..")
249425bb815Sopenharmony_ci    curie_path = os.path.join(os.getcwd(), sys.argv[1])
250425bb815Sopenharmony_ci
251425bb815Sopenharmony_ci    main(curie_path, project_name, jerry_path)
252