Skip to content

Commit 75a18ff

Browse files
committed
Add config system macros to exporters
Support various exporter features
1 parent 9f62d70 commit 75a18ff

File tree

4 files changed

+59
-22
lines changed

4 files changed

+59
-22
lines changed

tools/export/__init__.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def online_build_url_resolver(url):
5757

5858

5959
def export(project_path, project_name, ide, target, destination='/tmp/',
60-
tempdir=None, clean=True, extra_symbols=None, build_url_resolver=online_build_url_resolver):
60+
tempdir=None, clean=True, extra_symbols=None, zip=True, relative=False, build_url_resolver=online_build_url_resolver):
6161
# Convention: we are using capitals for toolchain and target names
6262
if target is not None:
6363
target = target.upper()
@@ -74,7 +74,7 @@ def export(project_path, project_name, ide, target, destination='/tmp/',
7474
try:
7575
ide = "zip"
7676
exporter = zip.ZIP(target, tempdir, project_name, build_url_resolver, extra_symbols=extra_symbols)
77-
exporter.scan_and_copy_resources(project_path, tempdir)
77+
exporter.scan_and_copy_resources(project_path, tempdir, relative)
7878
exporter.generate()
7979
report['success'] = True
8080
except OldLibrariesException, e:
@@ -101,7 +101,7 @@ def export(project_path, project_name, ide, target, destination='/tmp/',
101101
# target checked, export
102102
try:
103103
exporter = Exporter(target, tempdir, project_name, build_url_resolver, extra_symbols=extra_symbols)
104-
exporter.scan_and_copy_resources(project_path, tempdir)
104+
exporter.scan_and_copy_resources(project_path, tempdir, relative)
105105
exporter.generate()
106106
report['success'] = True
107107
except OldLibrariesException, e:
@@ -133,8 +133,12 @@ def export(project_path, project_name, ide, target, destination='/tmp/',
133133
# add readme file to every offline export.
134134
open(os.path.join(tempdir, 'GettingStarted.htm'),'w').write('<meta http-equiv="refresh" content="0; url=http://mbed.org/handbook/Getting-Started-mbed-Exporters#%s"/>'% (ide))
135135
# copy .hgignore file to exported direcotry as well.
136-
copy(os.path.join(exporter.TEMPLATE_DIR,'.hgignore'),tempdir)
137-
zip_path = zip_working_directory_and_clean_up(tempdir, destination, project_name, clean)
136+
if exists(os.path.join(exporter.TEMPLATE_DIR,'.hgignore')):
137+
copy(os.path.join(exporter.TEMPLATE_DIR,'.hgignore'), tempdir)
138+
if zip:
139+
zip_path = zip_working_directory_and_clean_up(tempdir, destination, project_name, clean)
140+
else:
141+
zip_path = destination
138142

139143
return zip_path, report
140144

tools/export/exporters.py

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from jinja2.environment import Environment
88
from contextlib import closing
99
from zipfile import ZipFile, ZIP_DEFLATED
10+
from operator import add
1011

1112
from tools.utils import mkdir
1213
from tools.toolchains import TOOLCHAIN_CLASSES
@@ -16,6 +17,8 @@
1617
from project_generator.project import Project
1718
from project_generator.settings import ProjectSettings
1819

20+
from tools.config import Config
21+
1922
class OldLibrariesException(Exception): pass
2023

2124
class Exporter(object):
@@ -31,6 +34,7 @@ def __init__(self, target, inputDir, program_name, build_url_resolver, extra_sym
3134
jinja_loader = FileSystemLoader(os.path.dirname(os.path.abspath(__file__)))
3235
self.jinja_environment = Environment(loader=jinja_loader)
3336
self.extra_symbols = extra_symbols
37+
self.config_macros = []
3438

3539
def get_toolchain(self):
3640
return self.TOOLCHAIN
@@ -46,24 +50,40 @@ def __scan_and_copy(self, src_path, trg_path):
4650
self.toolchain.copy_files(r, trg_path, rel_path=src_path)
4751
return resources
4852

53+
@staticmethod
54+
def _get_dir_grouped_files(files):
55+
""" Get grouped files based on the dirname """
56+
files_grouped = {}
57+
for file in files:
58+
rel_path = os.path.relpath(file, os.getcwd())
59+
dir_path = os.path.dirname(rel_path)
60+
if dir_path == '':
61+
# all files within the current dir go into Source_Files
62+
dir_path = 'Source_Files'
63+
if not dir_path in files_grouped.keys():
64+
files_grouped[dir_path] = []
65+
files_grouped[dir_path].append(file)
66+
return files_grouped
67+
4968
def progen_get_project_data(self):
5069
""" Get ProGen project data """
5170
# provide default data, some tools don't require any additional
5271
# tool specific settings
53-
sources = []
72+
code_files = []
5473
for r_type in ['c_sources', 'cpp_sources', 's_sources']:
5574
for file in getattr(self.resources, r_type):
56-
sources.append(file)
75+
code_files.append(file)
76+
77+
sources_files = code_files + self.resources.hex_files + self.resources.objects + \
78+
self.resources.libraries
79+
sources_grouped = Exporter._get_dir_grouped_files(sources_files)
80+
headers_grouped = Exporter._get_dir_grouped_files(self.resources.headers)
5781

5882
project_data = {
5983
'common': {
60-
'sources': {
61-
'Source Files': sources + self.resources.hex_files +
62-
self.resources.objects + self.resources.libraries,
63-
},
64-
'includes': {
65-
'Include Files': self.resources.headers,
66-
},
84+
'sources': sources_grouped,
85+
'includes': headers_grouped,
86+
'build_dir':'.build',
6787
'target': [TARGET_MAP[self.target].progen['target']],
6888
'macros': self.get_symbols(),
6989
'export_dir': [self.inputDir],
@@ -73,7 +93,7 @@ def progen_get_project_data(self):
7393
return project_data
7494

7595
def progen_gen_file(self, tool_name, project_data):
76-
""" Generate project using ProGen Project API """
96+
"""" Generate project using ProGen Project API """
7797
settings = ProjectSettings()
7898
project = Project(self.program_name, [project_data], settings)
7999
# TODO: Fix this, the inc_dirs are not valid (our scripts copy files), therefore progen
@@ -95,17 +115,20 @@ def __scan_all(self, path):
95115

96116
return resources
97117

98-
def scan_and_copy_resources(self, prj_path, trg_path):
118+
def scan_and_copy_resources(self, prj_paths, trg_path, relative=False):
99119
# Copy only the file for the required target and toolchain
100120
lib_builds = []
121+
# Create the configuration object
122+
cfg = Config(self.target, prj_paths)
101123
for src in ['lib', 'src']:
102-
resources = self.__scan_and_copy(join(prj_path, src), trg_path)
124+
resources = reduce(add, [self.__scan_and_copy(join(path, src), trg_path) for path in prj_paths])
103125
lib_builds.extend(resources.lib_builds)
104126

105127
# The repository files
106128
for repo_dir in resources.repo_dirs:
107129
repo_files = self.__scan_all(repo_dir)
108-
self.toolchain.copy_files(repo_files, trg_path, rel_path=join(prj_path, src))
130+
for path in proj_paths :
131+
self.toolchain.copy_files(repo_files, trg_path, rel_path=join(path, src))
109132

110133
# The libraries builds
111134
for bld in lib_builds:
@@ -120,9 +143,17 @@ def scan_and_copy_resources(self, prj_path, trg_path):
120143
fhandle = file(join(hgdir, 'keep.me'), 'a')
121144
fhandle.close()
122145

123-
# Final scan of the actual exported resources
124-
self.resources = self.toolchain.scan_resources(trg_path)
125-
self.resources.relative_to(trg_path, self.DOT_IN_RELATIVE_PATH)
146+
if not relative:
147+
# Final scan of the actual exported resources
148+
self.resources = self.toolchain.scan_resources(trg_path)
149+
self.resources.relative_to(trg_path, self.DOT_IN_RELATIVE_PATH)
150+
else:
151+
# use the prj_dir (source, not destination)
152+
self.resources = reduce(add, [self.toolchain.scan_resources(path) for path in prj_paths])
153+
# Add all JSON files discovered during scanning to the configuration object
154+
cfg.add_config_files(self.resources.json_files)
155+
# Get data from the configuration system
156+
self.config_macros = cfg.get_config_data_macros()
126157
# Check the existence of a binary build of the mbed library for the desired target
127158
# This prevents exporting the mbed libraries from source
128159
# if not self.toolchain.mbed_libs:
@@ -141,7 +172,7 @@ def get_symbols(self, add_extra_symbols=True):
141172
""" This function returns symbols which must be exported.
142173
Please add / overwrite symbols in each exporter separately
143174
"""
144-
symbols = self.toolchain.get_symbols()
175+
symbols = self.toolchain.get_symbols() + self.config_macros
145176
# We have extra symbols from e.g. libraries, we want to have them also added to export
146177
if add_extra_symbols:
147178
if self.extra_symbols is not None:

tools/export/iar.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ def generate(self):
6767

6868
project_data['tool_specific'] = {}
6969
project_data['tool_specific'].update(tool_specific)
70+
project_data['common']['build_dir'] = os.path.join(project_data['common']['build_dir'], 'iar_arm')
7071
self.progen_gen_file('iar_arm', project_data)
7172

7273
# Currently not used, we should reuse folder_name to create virtual folders

tools/export/uvision4.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,6 @@ def generate(self):
8585
project_data['common']['macros'].pop(i)
8686
i += 1
8787
project_data['common']['macros'].append('__ASSERT_MSG')
88+
project_data['common']['build_dir'] = join(project_data['common']['build_dir'], 'uvision4')
8889
self.progen_gen_file('uvision', project_data)
8990

0 commit comments

Comments
 (0)