diff options
-rw-r--r-- | Makefile | 10 | ||||
-rw-r--r-- | support/asymptote/__main__.py | 62 |
2 files changed, 50 insertions, 22 deletions
@@ -55,7 +55,7 @@ SVG_ASY_FILES := $(call filter_compiled,.svg,.asy,$(SRC_FILES)) ASY_PDF_FILES := $(call filter_compiled,.asy,.pdf,$(filter-out $(SVG_ASY_FILES),$(SRC_FILES))) # Makefiles which are generated while compiling to record dependencies. -DEPENDENCY_FILES := $(foreach i,.scad,$(call filter_compiled,$i,.d,$(filter-out $(SVG_ASY_FILES),$(SRC_FILES)))) +DEPENDENCY_FILES := $(patsubst %,%.d,$(SCAD_STL_FILES) $(SCAD_DXF_FILES) $(ASY_PDF_FILES)) # Files that may be used from OpenSCAD files and thus must exist before OpenSCAD is called. SCAD_ORDER_DEPS := $(filter %.scad %.dxf,$(SRC_FILES)) $(SVG_DXF_FILES) @@ -95,17 +95,17 @@ $(SVG_ASY_FILES): %.asy: %.svg $(GLOBAL_DEPS) # Rule to compile an OpenSCAD file to a DXF file. $(SCAD_DXF_FILES): %.dxf: %.scad $(GLOBAL_DEPS) | $(SCAD_ORDER_DEPS) echo [openscad] $@ - $(OPENSCAD_CMD) $< $@ $*.d + $(OPENSCAD_CMD) $< $@ # Rule to compile an OpenSCAD file to an STL file. $(SCAD_STL_FILES): %.stl: %.scad $(GLOBAL_DEPS) | $(SCAD_ORDER_DEPS) echo [openscad] $@ - $(OPENSCAD_CMD) $< $@ $*.d + $(OPENSCAD_CMD) $< $@ # Rule to export an SVG file to an Asymptote file. -$(ASY_PDF_FILES): %.pdf: $(ASY_DEPS) $(GLOBAL_DEPS) +$(ASY_PDF_FILES): %.pdf: %.asy $(GLOBAL_DEPS) | $(ASY_DEPS) echo [asymptote] $@ - $(ASYMPTOTE_CMD) $*.asy $@ + $(ASYMPTOTE_CMD) $< $@ # Rule for automaticaly generated OpenSCAD files. $(GENERATED_FILES): generate_sources.sh $(GLOBAL_DEPS) diff --git a/support/asymptote/__main__.py b/support/asymptote/__main__.py index 238332f..9d712ef 100644 --- a/support/asymptote/__main__.py +++ b/support/asymptote/__main__.py @@ -1,26 +1,54 @@ -import os, shutil -from lib import util +import sys, os, shutil +from lib import util, make def _asymptote(in_path, out_path, asymptote_dir, working_dir): - util.command([os.environ['ASYMPTOTE'], '-f', 'pdf', '-o', out_path, in_path], set_env = { 'ASYMPTOTE_DIR': asymptote_dir }, working_dir = working_dir) + args = [os.environ['ASYMPTOTE'], '-vv', '-f', 'pdf', '-o', out_path, in_path] + + with util.command_context(args, set_env = { 'ASYMPTOTE_DIR': asymptote_dir }, working_dir = working_dir, use_stderr = True) as process: + def get_loaded_file(line): + if any(line.startswith(j) for j in ['Loading ', 'Including ']): + parts = line.rstrip('\n').split(' ') + + if len(parts) == 4: + _, _, from_, path = parts + + if from_ == 'from': + return path + + return None + + def iter_loaded_files(): + for i in process.stderr: + loaded_file = get_loaded_file(i) + + if loaded_file is not None: + yield loaded_file + elif not any(i.startswith(j) for j in ['cd ', 'Using configuration ']): + print >> sys.stderr, i, + + loaded_files = list(iter_loaded_files()) + + return loaded_files @util.main def main(in_path, out_path): _, out_suffix = os.path.splitext(out_path) - if out_suffix == '.pdf': - with util.TemporaryDirectory() as temp_dir: - absolute_in_path = os.path.abspath(in_path) - temp_out_path = os.path.join(temp_dir, 'out.pdf') - - # Asymptote creates A LOT of temp files (presumably when invoking LaTeX) and leaves some of them behind. Thus we run asymptote in a temporary directory. - _asymptote(absolute_in_path, 'out', os.path.dirname(absolute_in_path), temp_dir) - - if not os.path.exists(temp_out_path): - raise util.UserError('Asymptote did not generate a PDF file for input file {}.', in_path) - - shutil.copyfile(temp_out_path, out_path) - else: - raise Exception('Unknown file type: {}'.format(out_suffix)) + with util.TemporaryDirectory() as temp_dir: + absolute_in_path = os.path.abspath(in_path) + temp_out_path = os.path.join(temp_dir, 'out.pdf') + + # Asymptote creates A LOT of temp files (presumably when invoking LaTeX) and leaves some of them behind. Thus we run asymptote in a temporary directory. + loaded_files = _asymptote(absolute_in_path, 'out', os.path.dirname(absolute_in_path), temp_dir) + + if not os.path.exists(temp_out_path): + raise util.UserError('Asymptote did not generate a PDF file for input file {}.', in_path) + + # All dependencies as paths relative to the project root. + dependencies = set(map(os.path.relpath, loaded_files)) + + # Write output files. + make.write_dependencies(out_path + '.d', out_path, dependencies - { in_path }) + shutil.copyfile(temp_out_path, out_path) |