Skip to content

Commit d583522

Browse files
committed
Add _assemble() and _compile() methods to generate one or more commands per source file
Support multiple commands per compile Reuse _assemble() and _compile() for sequential and parallel compiles Preserve compile(), compile_c(), compile_cpp() and assemble() methods functionality
1 parent 647c961 commit d583522

File tree

4 files changed

+94
-59
lines changed

4 files changed

+94
-59
lines changed

workspace_tools/toolchains/__init__.py

Lines changed: 72 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,20 @@ def print_notify_verbose(event):
6868
print_notify(event) # standard handle
6969

7070
def compile_worker(job):
71-
_, stderr, rc = run_cmd(job['command'], job['work_dir'])
71+
results = []
72+
for command in job['commands']:
73+
_, stderr, rc = run_cmd(command, job['work_dir'])
74+
results.append({
75+
'code': rc,
76+
'output': stderr,
77+
'command': command
78+
})
7279

7380
return {
74-
'code': rc,
75-
'output': stderr,
76-
'command': job['command'],
7781
'source': job['source'],
78-
'object': job['object']
82+
'object': job['object'],
83+
'commands': job['commands'],
84+
'results': results
7985
}
8086

8187
class Resources:
@@ -481,12 +487,12 @@ def compile_sources(self, resources, build_path, inc_dirs=None):
481487
mkdir(work_dir)
482488

483489
# Queue mode (multiprocessing)
484-
command = self._compile_command(source, object, inc_paths)
485-
if command is not None:
490+
commands = self._compile_command(source, object, inc_paths)
491+
if commands is not None:
486492
queue.append({
487493
'source': source,
488494
'object': object,
489-
'command': command,
495+
'commands': commands,
490496
'work_dir': work_dir,
491497
'chroot': self.CHROOT
492498
})
@@ -498,10 +504,23 @@ def compile_sources(self, resources, build_path, inc_dirs=None):
498504
if jobs > CPU_COUNT_MIN and len(queue) > jobs:
499505
return self.compile_queue(queue, objects)
500506
else:
501-
for item in queue:
502-
self.compile(item['source'], item['object'], inc_paths)
503-
objects.append(item['object'])
504-
return objects
507+
return self.compile_seq(queue, objects)
508+
509+
def compile_seq(self, queue, objects):
510+
for item in queue:
511+
result = compile_worker(item)
512+
513+
self.compiled += 1
514+
self.progress("compile", item['source'], build_update=True)
515+
for res in result['results']:
516+
self.debug("Command: %s" % ' '.join(res['command']))
517+
self._compile_output([
518+
res['code'],
519+
res['output'],
520+
res['command']
521+
])
522+
objects.append(result['object'])
523+
return objects
505524

506525
def compile_queue(self, queue, objects):
507526
jobs_count = int(self.jobs if self.jobs else cpu_count())
@@ -525,15 +544,16 @@ def compile_queue(self, queue, objects):
525544
try:
526545
result = r.get()
527546
results.remove(r)
528-
547+
529548
self.compiled += 1
530-
self.progress("compile", result['source'], build_update=True)
531-
self.debug("Command: %s" % ' '.join(result['command']))
532-
self._compile_output([
533-
result['code'],
534-
result['output'],
535-
result['command']
536-
])
549+
self.progress("compile", result['source'], build_update=True)
550+
for res in result['results']:
551+
self.debug("Command: %s" % ' '.join(res['command']))
552+
self._compile_output([
553+
res['code'],
554+
res['output'],
555+
res['command']
556+
])
537557
objects.append(result['object'])
538558
except ToolException, err:
539559
p.terminate()
@@ -555,42 +575,25 @@ def compile_queue(self, queue, objects):
555575
p.join()
556576

557577
return objects
558-
578+
559579
def _compile_command(self, source, object, includes):
560580
# Check dependencies
561581
_, ext = splitext(source)
562582
ext = ext.lower()
563-
base, _ = splitext(object)
564-
dep_path = base + '.d'
565-
asm_mode = False
566583

567-
if ext == '.c':
568-
cc = self.cc
569-
elif ext == '.cpp':
570-
cc = self.cppc
584+
if ext == '.c' or ext == '.cpp':
585+
base, _ = splitext(object)
586+
dep_path = base + '.d'
587+
deps = self.parse_dependencies(dep_path) if (exists(dep_path)) else []
588+
if len(deps) == 0 or self.need_update(object, deps):
589+
return self._compile(source, object, includes)
571590
elif ext == '.s':
572-
cc = self.asm
573-
asm_mode = True
591+
deps = [source]
592+
if self.need_update(object, deps):
593+
return self._assemble(source, object, includes)
574594
else:
575595
return False
576596

577-
deps = []
578-
if asm_mode:
579-
deps = [source]
580-
elif exists(dep_path):
581-
deps = self.parse_dependencies(dep_path)
582-
583-
if len(deps) == 0 or self.need_update(object, deps):
584-
command = cc + ['-D%s' % s for s in self.get_symbols()] + ["-I%s" % i for i in includes] + ["-o", object, source]
585-
586-
if asm_mode is False and hasattr(self, "get_dep_opt"):
587-
command.extend(self.get_dep_opt(dep_path))
588-
589-
if hasattr(self, "cc_extra"):
590-
command.extend(self.cc_extra(base))
591-
592-
return command
593-
594597
return None
595598

596599
def _compile_output(self, output=[]):
@@ -607,16 +610,31 @@ def _compile_output(self, output=[]):
607610
if rc != 0:
608611
raise ToolException(stderr)
609612

613+
def _compile(self, source, object, includes):
614+
_, ext = splitext(source)
615+
ext = ext.lower()
616+
617+
cc = self.cppc if ext == ".cpp" else self.cc
618+
command = cc + ['-D%s' % s for s in self.get_symbols()] + ["-I%s" % i for i in includes] + ["-o", object, source]
619+
620+
if hasattr(self, "get_dep_opt"):
621+
base, _ = splitext(object)
622+
dep_path = base + '.d'
623+
command.extend(self.get_dep_opt(dep_path))
624+
625+
if hasattr(self, "cc_extra"):
626+
command.extend(self.cc_extra(base))
627+
628+
return [command]
629+
610630
def compile(self, source, object, includes):
611-
self.compiled += 1
612631
self.progress("compile", source, build_update=True)
613632

614-
command = self._compile_command(source, object, includes)
615-
if command is None: return True
616-
617-
self.debug("Command: %s" % ' '.join(command))
618-
_, stderr, rc = run_cmd(command, dirname(object))
619-
self._compile_output([rc, stderr, command])
633+
commands = self._compile(source, object, includes)
634+
for command in commands:
635+
self.debug("Command: %s" % ' '.join(command))
636+
_, stderr, rc = run_cmd(command, dirname(object))
637+
self._compile_output([rc, stderr, command])
620638

621639
def compile_c(self, source, object, includes):
622640
self.compile(source, object, includes)

workspace_tools/toolchains/arm.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,18 @@ def remove_option(self, option):
8080
if option in tool:
8181
tool.remove(option)
8282

83-
def assemble(self, source, object, includes):
83+
def _assemble(self, source, object, includes):
8484
# Preprocess first, then assemble
8585
tempfile = object + '.E.s'
86-
self.default_cmd(self.asm + ['-D%s' % s for s in self.get_symbols() + self.macros] + ["-I%s" % i for i in includes] + ["-E", "-o", tempfile, source])
87-
self.default_cmd(self.hook.get_cmdline_assembler(self.asm + ["-o", object, tempfile]))
86+
return [
87+
self.asm + ['-D%s' % s for s in self.get_symbols() + self.macros] + ["-I%s" % i for i in includes] + ["-E", "-o", tempfile, source],
88+
self.hook.get_cmdline_assembler(self.asm + ["-o", object, tempfile])
89+
]
90+
91+
def assemble(self, source, object, includes):
92+
commands = self._assemble(source, object, includes);
93+
for command in commands:
94+
self.default_cmd(command)
8895

8996
def parse_dependencies(self, dep_path):
9097
dependencies = []

workspace_tools/toolchains/gcc.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,13 @@ def __init__(self, target, options=None, notify=None, macros=None, tool_path="")
8181
self.ar = join(tool_path, "arm-none-eabi-ar")
8282
self.elf2bin = join(tool_path, "arm-none-eabi-objcopy")
8383

84+
def _assemble(self, source, object, includes):
85+
return [self.hook.get_cmdline_assembler(self.asm + ['-D%s' % s for s in self.get_symbols() + self.macros] + ["-I%s" % i for i in includes] + ["-o", object, source])]
86+
8487
def assemble(self, source, object, includes):
85-
self.default_cmd(self.hook.get_cmdline_assembler(self.asm + ['-D%s' % s for s in self.get_symbols() + self.macros] + ["-I%s" % i for i in includes] + ["-o", object, source]))
88+
commands = self._assemble(source, object, includes);
89+
for command in commands:
90+
self.default_cmd(command)
8691

8792
def parse_dependencies(self, dep_path):
8893
dependencies = []

workspace_tools/toolchains/iar.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,14 @@ def cc_extra(self, base):
9393
def parse_dependencies(self, dep_path):
9494
return [path.strip() for path in open(dep_path).readlines()
9595
if (path and not path.isspace())]
96+
97+
def _assemble(self, source, object, includes):
98+
return [self.hook.get_cmdline_assembler(self.asm + ['-D%s' % s for s in self.get_symbols() + self.macros] + ["-I%s" % i for i in includes] + ["-o", object, source])]
9699

97100
def assemble(self, source, object, includes):
98-
self.default_cmd(self.hook.get_cmdline_assembler(self.asm + ['-D%s' % s for s in self.get_symbols() + self.macros] + ["-I%s" % i for i in includes] + ["-o", object, source]))
101+
commands = self._assemble(source, object, includes);
102+
for command in commands:
103+
self.default_cmd(command)
99104

100105
def archive(self, objects, lib_path):
101106
if exists(lib_path):

0 commit comments

Comments
 (0)