Search in sources :

Example 6 with IOutputType

use of org.eclipse.cdt.managedbuilder.core.IOutputType in project arduino-eclipse-plugin by Sloeber.

the class ArduinoGnuMakefileGenerator method calculateOutputsForSource.

/**
 * Returns the output <code>IPath</code>s for this invocation of the tool
 * with the specified source file The priorities for determining the names
 * of the outputs of a tool are: 1. If the tool is the build target and
 * primary output, use artifact name & extension - This case does not apply
 * here... 2. If an option is specified, use the value of the option 3. If a
 * nameProvider is specified, call it 4. If outputNames is specified, use it
 * 5. Use the name pattern to generate a transformation macro so that the
 * source names can be transformed into the target names using the built-in
 * string substitution functions of <code>make</code>.
 *
 * @param relativePath
 *            build output directory relative path of the current output
 *            directory
 * @param ruleOutputs
 *            Vector of rule IPaths that are relative to the build directory
 * @param enumeratedPrimaryOutputs
 *            Vector of IPaths of primary outputs that are relative to the
 *            build directory
 * @param enumeratedSecondaryOutputs
 *            Vector of IPaths of secondary outputs that are relative to the
 *            build directory
 */
protected void calculateOutputsForSource(ITool tool, String relativePath, IResource resource, IPath sourceLocation, List<IPath> ruleOutputs, List<IPath> enumeratedPrimaryOutputs, List<IPath> enumeratedSecondaryOutputs) {
    String inExt = sourceLocation.getFileExtension();
    String outExt = tool.getOutputExtension(inExt);
    // IResourceInfo rcInfo = tool.getParentResourceInfo();
    IOutputType[] outTypes = tool.getOutputTypes();
    if (outTypes != null && outTypes.length > 0) {
        for (IOutputType type : outTypes) {
            boolean primaryOutput = (type == tool.getPrimaryOutputType());
            // if (primaryOutput && ignorePrimary) continue;
            String outputPrefix = type.getOutputPrefix();
            // if (config != null) {
            try {
                if (containsSpecialCharacters(sourceLocation.toOSString())) {
                    outputPrefix = ManagedBuildManager.getBuildMacroProvider().resolveValue(outputPrefix, new String(), " ", IBuildMacroProvider.CONTEXT_CONFIGURATION, this.config);
                } else {
                    outputPrefix = ManagedBuildManager.getBuildMacroProvider().resolveValueToMakefileFormat(outputPrefix, new String(), " ", IBuildMacroProvider.CONTEXT_CONFIGURATION, this.config);
                }
            } catch (BuildMacroException e) {
            /* JABA is not going to write this code */
            }
            // }
            boolean multOfType = type.getMultipleOfType();
            IOption option = tool.getOptionBySuperClassId(type.getOptionId());
            IManagedOutputNameProvider nameProvider = type.getNameProvider();
            String[] outputNames = type.getOutputNames();
            // 2. If an option is specified, use the value of the option
            if (option != null) {
                try {
                    List<String> outputList = new ArrayList<>();
                    int optType = option.getValueType();
                    if (optType == IOption.STRING) {
                        outputList.add(outputPrefix + option.getStringValue());
                    } else if (optType == IOption.STRING_LIST || optType == IOption.LIBRARIES || optType == IOption.OBJECTS || optType == IOption.INCLUDE_FILES || optType == IOption.LIBRARY_PATHS || optType == IOption.LIBRARY_FILES || optType == IOption.MACRO_FILES) {
                        @SuppressWarnings("unchecked") List<String> value = (List<String>) option.getValue();
                        outputList = value;
                        ((Tool) tool).filterValues(optType, outputList);
                        // Add outputPrefix to each if necessary
                        if (outputPrefix.length() > 0) {
                            for (int j = 0; j < outputList.size(); j++) {
                                outputList.set(j, outputPrefix + outputList.get(j));
                            }
                        }
                    }
                    for (int j = 0; j < outputList.size(); j++) {
                        String outputName = outputList.get(j);
                        // names
                        try {
                            String resolved = null;
                            if (containsSpecialCharacters(sourceLocation.toOSString())) {
                                resolved = ManagedBuildManager.getBuildMacroProvider().resolveValue(outputName, new String(), " ", IBuildMacroProvider.CONTEXT_FILE, new FileContextData(sourceLocation, null, option, tool));
                            } else {
                                resolved = ManagedBuildManager.getBuildMacroProvider().resolveValueToMakefileFormat(outputName, new String(), " ", IBuildMacroProvider.CONTEXT_FILE, new FileContextData(sourceLocation, null, option, tool));
                            }
                            if ((resolved = resolved.trim()).length() > 0)
                                outputName = resolved;
                        } catch (BuildMacroException e) {
                        /* JABA is not going to write this code */
                        }
                        IPath outPath = Path.fromOSString(outputName);
                        // relative path of this output directory
                        if (outPath.segmentCount() == 1) {
                            outPath = Path.fromOSString(relativePath + outputList.get(j));
                        }
                        if (primaryOutput) {
                            ruleOutputs.add(j, outPath);
                            enumeratedPrimaryOutputs.add(j, resolvePercent(outPath, sourceLocation));
                        } else {
                            ruleOutputs.add(outPath);
                            enumeratedSecondaryOutputs.add(resolvePercent(outPath, sourceLocation));
                        }
                    }
                } catch (BuildException ex) {
                /* JABA is not going to write this code */
                }
            } else // 3. If a nameProvider is specified, call it
            if (nameProvider != null) {
                IPath[] inPaths = new IPath[1];
                // ;
                inPaths[0] = resource.getProjectRelativePath();
                // sourceLocation.removeFirstSegments(project.getLocation().segmentCount());
                IPath[] outPaths = null;
                try {
                    IManagedOutputNameProviderJaba newNameProvider = (IManagedOutputNameProviderJaba) nameProvider;
                    outPaths = newNameProvider.getOutputNames(this.project, this.config, tool, inPaths);
                } catch (Exception e) {
                    // The provided class is not a
                    // IManagedOutputNameProviderJaba class;
                    Common.log(new Status(IStatus.ERROR, Const.CORE_PLUGIN_ID, // 
                    "The provided class is not of type IManagedOutputNameProviderJaba", e));
                }
                if (outPaths != null) {
                    // MODDED BY JABA ADDED to handle
                    // null as return value
                    // MODDED By JABA added to
                    this.usedOutType = type;
                    // command line
                    for (int j = 0; j < outPaths.length; j++) {
                        IPath outPath = outPaths[j];
                        String outputName = outPaths[j].toOSString();
                        // names
                        try {
                            String resolved = null;
                            if (containsSpecialCharacters(sourceLocation.toOSString())) {
                                resolved = ManagedBuildManager.getBuildMacroProvider().resolveValue(outputName, "", " ", IBuildMacroProvider.CONTEXT_FILE, new FileContextData(sourceLocation, null, option, tool));
                            } else {
                                resolved = ManagedBuildManager.getBuildMacroProvider().resolveValueToMakefileFormat(outputName, "", " ", IBuildMacroProvider.CONTEXT_FILE, new FileContextData(sourceLocation, null, option, tool));
                            }
                            if ((resolved = resolved.trim()).length() > 0)
                                outputName = resolved;
                        } catch (BuildMacroException e) {
                        // JABA is not
                        // going to write
                        // this code
                        }
                        // relative path of this output directory
                        if (outPath.segmentCount() == 1) {
                            outPath = Path.fromOSString(relativePath + outPath.toOSString());
                        }
                        if (type.getPrimaryOutput()) {
                            ruleOutputs.add(j, outPath);
                            enumeratedPrimaryOutputs.add(j, resolvePercent(outPath, sourceLocation));
                        } else {
                            ruleOutputs.add(outPath);
                            enumeratedSecondaryOutputs.add(resolvePercent(outPath, sourceLocation));
                        }
                    }
                }
            // MODDED BY JABA ADDED
            } else // 4. If outputNames is specified, use it
            if (outputNames != null) {
                for (int j = 0; j < outputNames.length; j++) {
                    String outputName = outputNames[j];
                    try {
                        // try to resolve the build macros in the output
                        // names
                        String resolved = ManagedBuildManager.getBuildMacroProvider().resolveValueToMakefileFormat(outputName, new String(), " ", IBuildMacroProvider.CONTEXT_FILE, new FileContextData(sourceLocation, null, option, tool));
                        if ((resolved = resolved.trim()).length() > 0)
                            outputName = resolved;
                    } catch (BuildMacroException e) {
                    /* JABA is not going to write this code */
                    }
                    IPath outPath = Path.fromOSString(outputName);
                    // path of this output directory
                    if (outPath.segmentCount() == 1) {
                        outPath = Path.fromOSString(relativePath + outPath.toOSString());
                    }
                    if (primaryOutput) {
                        ruleOutputs.add(j, outPath);
                        enumeratedPrimaryOutputs.add(j, resolvePercent(outPath, sourceLocation));
                    } else {
                        ruleOutputs.add(outPath);
                        enumeratedSecondaryOutputs.add(resolvePercent(outPath, sourceLocation));
                    }
                }
            } else {
                // <code>make</code>.
                if (multOfType) {
                // This case is not handled - a nameProvider or
                // outputNames must be specified
                // TODO - report error
                } else {
                    String namePattern = type.getNamePattern();
                    IPath namePatternPath = null;
                    if (namePattern == null || namePattern.length() == 0) {
                        namePattern = relativePath + outputPrefix + IManagedBuilderMakefileGenerator.WILDCARD;
                        if (outExt != null && outExt.length() > 0) {
                            namePattern += DOT + outExt;
                        }
                        namePatternPath = Path.fromOSString(namePattern);
                    } else {
                        if (outputPrefix.length() > 0) {
                            namePattern = outputPrefix + namePattern;
                        }
                        namePatternPath = Path.fromOSString(namePattern);
                        // relative path of this output directory
                        if (namePatternPath.segmentCount() == 1) {
                            namePatternPath = Path.fromOSString(relativePath + namePatternPath.toOSString());
                        }
                    }
                    if (primaryOutput) {
                        ruleOutputs.add(0, namePatternPath);
                        enumeratedPrimaryOutputs.add(0, resolvePercent(namePatternPath, sourceLocation));
                    } else {
                        ruleOutputs.add(namePatternPath);
                        enumeratedSecondaryOutputs.add(resolvePercent(namePatternPath, sourceLocation));
                    }
                }
            }
        }
    } else {
        // For support of pre-CDT 3.0 integrations.
        // NOTE WELL: This only supports the case of a single "target tool"
        // that consumes exactly all of the object files, $OBJS, produced
        // by other tools in the build and produces a single output.
        // In this case, the output file name is the input file name with
        // the output extension.
        // if (!ignorePrimary) {
        String outPrefix = tool.getOutputPrefix();
        IPath outPath = Path.fromOSString(relativePath + outPrefix + WILDCARD);
        outPath = outPath.addFileExtension(outExt);
        ruleOutputs.add(0, outPath);
        enumeratedPrimaryOutputs.add(0, resolvePercent(outPath, sourceLocation));
    // }
    }
}
Also used : MultiStatus(org.eclipse.core.runtime.MultiStatus) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IResourceStatus(org.eclipse.core.resources.IResourceStatus) IPath(org.eclipse.core.runtime.IPath) IOption(org.eclipse.cdt.managedbuilder.core.IOption) ArrayList(java.util.ArrayList) FileContextData(org.eclipse.cdt.managedbuilder.internal.macros.FileContextData) CoreException(org.eclipse.core.runtime.CoreException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) BuildException(org.eclipse.cdt.managedbuilder.core.BuildException) IOException(java.io.IOException) BuildMacroException(org.eclipse.cdt.managedbuilder.macros.BuildMacroException) BuildMacroException(org.eclipse.cdt.managedbuilder.macros.BuildMacroException) IManagedOutputNameProvider(org.eclipse.cdt.managedbuilder.core.IManagedOutputNameProvider) List(java.util.List) ArrayList(java.util.ArrayList) BuildException(org.eclipse.cdt.managedbuilder.core.BuildException) IOutputType(org.eclipse.cdt.managedbuilder.core.IOutputType)

Example 7 with IOutputType

use of org.eclipse.cdt.managedbuilder.core.IOutputType in project arduino-eclipse-plugin by Sloeber.

the class ArduinoGnuMakefileGenerator method addTargets.

private StringBuffer addTargets(List<String> outputVarsAdditionsList, boolean rebuild) {
    StringBuffer buffer = new StringBuffer();
    // IConfiguration config = info.getDefaultConfiguration();
    // Assemble the information needed to generate the targets
    String prebuildStep = this.config.getPrebuildStep();
    try {
        // try to resolve the build macros in the prebuild step
        prebuildStep = ManagedBuildManager.getBuildMacroProvider().resolveValueToMakefileFormat(prebuildStep, EMPTY_STRING, WHITESPACE, IBuildMacroProvider.CONTEXT_CONFIGURATION, this.config);
    } catch (BuildMacroException e) {
    /* JABA is not going to write this code */
    }
    prebuildStep = prebuildStep.trim();
    String postbuildStep = this.config.getPostbuildStep();
    try {
        // try to resolve the build macros in the postbuild step
        postbuildStep = ManagedBuildManager.getBuildMacroProvider().resolveValueToMakefileFormat(postbuildStep, EMPTY_STRING, WHITESPACE, IBuildMacroProvider.CONTEXT_CONFIGURATION, this.config);
    } catch (BuildMacroException e) {
    /* JABA is not going to write this code */
    }
    postbuildStep = postbuildStep.trim();
    String preannouncebuildStep = this.config.getPreannouncebuildStep();
    String postannouncebuildStep = this.config.getPostannouncebuildStep();
    String targets = rebuild ? "clean all" : "all";
    ITool targetTool = this.config.calculateTargetTool();
    // if (targetTool == null) {
    // targetTool = info.getToolFromOutputExtension(buildTargetExt);
    // }
    // Get all the projects the build target depends on
    // If this configuration produces a static archive, building the archive
    // doesn't depend on the output
    // from any of the referenced configurations
    IConfiguration[] refConfigs = new IConfiguration[0];
    if (this.config.getBuildArtefactType() == null || !ManagedBuildManager.BUILD_ARTEFACT_TYPE_PROPERTY_STATICLIB.equals(this.config.getBuildArtefactType().getId()))
        refConfigs = ManagedBuildManager.getReferencedConfigurations(this.config);
    /*
		 * try { refdProjects = project.getReferencedProjects(); } catch
		 * (CoreException e) { // There are 2 exceptions; the project does not
		 * exist or it is not open // and neither conditions apply if we are
		 * building for it .... }
		 */
    // If a prebuild step exists, redefine the all target to be
    // all: {pre-build} main-build
    // and then reset the "traditional" all target to main-build
    // This will allow something meaningful to happen if the generated
    // makefile is
    // extracted and run standalone via "make all"
    // 
    String defaultTarget = "all:";
    if (prebuildStep.length() > 0) {
        // Add the comment for the "All" target
        buffer.append(COMMENT_SYMBOL + WHITESPACE + ManagedMakeMessages.getResourceString(ALL_TARGET) + NEWLINE);
        buffer.append(defaultTarget + WHITESPACE);
        buffer.append(PREBUILD + WHITESPACE);
        // Reset defaultTarget for now and for subsequent use, below
        defaultTarget = MAINBUILD;
        buffer.append(defaultTarget);
        // Update the defaultTarget, main-build, by adding a colon, which is
        // needed below
        defaultTarget = defaultTarget.concat(COLON);
        buffer.append(NEWLINE + NEWLINE);
        // Add the comment for the "main-build" target
        buffer.append(COMMENT_SYMBOL + WHITESPACE + ManagedMakeMessages.getResourceString(MAINBUILD_TARGET) + NEWLINE);
    } else
        // Add the comment for the "All" target
        buffer.append(COMMENT_SYMBOL + WHITESPACE + ManagedMakeMessages.getResourceString(ALL_TARGET) + NEWLINE);
    // Write out the all target first in case someone just runs make
    // all: <target_name> or mainbuild: <target_name>
    String outputPrefix = EMPTY_STRING;
    if (targetTool != null) {
        outputPrefix = targetTool.getOutputPrefix();
    }
    buffer.append(defaultTarget + WHITESPACE + outputPrefix + ensurePathIsGNUMakeTargetRuleCompatibleSyntax(this.buildTargetName));
    if (this.buildTargetExt.length() > 0) {
        buffer.append(DOT + this.buildTargetExt);
    }
    // Add the Secondary Outputs to the all target, if any
    IOutputType[] secondaryOutputs = this.config.getToolChain().getSecondaryOutputs();
    if (secondaryOutputs.length > 0) {
        buffer.append(WHITESPACE + SECONDARY_OUTPUTS);
    }
    buffer.append(NEWLINE + NEWLINE);
    /*
		 * The build target may depend on other projects in the workspace. These
		 * are captured in the deps target: deps: <cd <Proj_Dep_1/build_dir>;
		 * $(MAKE) [clean all | all]>
		 */
    // Vector managedProjectOutputs = new Vector(refdProjects.length);
    // if (refdProjects.length > 0) {
    List<String> managedProjectOutputs = new ArrayList<>(refConfigs.length);
    if (refConfigs.length > 0) {
        boolean addDeps = true;
        // if (refdProjects != null) {
        for (IConfiguration depCfg : refConfigs) {
            // IProject dep = refdProjects[i];
            if (!depCfg.isManagedBuildOn())
                continue;
            // if (!dep.exists()) continue;
            if (addDeps) {
                buffer.append("dependents:" + NEWLINE);
                addDeps = false;
            }
            String buildDir = depCfg.getOwner().getLocation().toOSString();
            String depTargets = targets;
            // if (ManagedBuildManager.manages(dep)) {
            // Add the current configuration to the makefile path
            // IManagedBuildInfo depInfo =
            // ManagedBuildManager.getBuildInfo(dep);
            buildDir += FILE_SEPARATOR + depCfg.getName();
            // Extract the build artifact to add to the dependency list
            String depTarget = depCfg.getArtifactName();
            String depExt = depCfg.getArtifactExtension();
            try {
                // try to resolve the build macros in the artifact extension
                depExt = ManagedBuildManager.getBuildMacroProvider().resolveValueToMakefileFormat(depExt, new String(), " ", IBuildMacroProvider.CONTEXT_CONFIGURATION, depCfg);
            } catch (BuildMacroException e) {
            /* JABA is not going to write this code */
            }
            try {
                // try to resolve the build macros in the artifact name
                String resolved = ManagedBuildManager.getBuildMacroProvider().resolveValueToMakefileFormat(depTarget, new String(), " ", IBuildMacroProvider.CONTEXT_CONFIGURATION, depCfg);
                if ((resolved = resolved.trim()).length() > 0)
                    depTarget = resolved;
            } catch (BuildMacroException e) {
            /* JABA is not going to write this code */
            }
            String depPrefix = depCfg.getOutputPrefix(depExt);
            if (depCfg.needsRebuild()) {
                depTargets = "clean all";
            }
            String dependency = buildDir + FILE_SEPARATOR + depPrefix + depTarget;
            if (depExt.length() > 0) {
                dependency += DOT + depExt;
            }
            dependency = escapeWhitespaces(dependency);
            managedProjectOutputs.add(dependency);
            // }
            buffer.append(TAB + "-cd" + WHITESPACE + escapeWhitespaces(buildDir) + WHITESPACE + LOGICAL_AND + WHITESPACE + "$(MAKE) " + depTargets + NEWLINE);
        }
        // }
        buffer.append(NEWLINE);
    }
    // Add the targets tool rules
    buffer.append(addTargetsRules(targetTool, outputVarsAdditionsList, managedProjectOutputs, (postbuildStep.length() > 0)));
    // Add the prebuild step target, if specified
    if (prebuildStep.length() > 0) {
        buffer.append(PREBUILD + COLON + NEWLINE);
        if (preannouncebuildStep.length() > 0) {
            buffer.append(TAB + DASH + AT + escapedEcho(preannouncebuildStep));
        }
        buffer.append(TAB + DASH + prebuildStep + NEWLINE);
        buffer.append(TAB + DASH + AT + ECHO_BLANK_LINE + NEWLINE);
    }
    // Add the postbuild step, if specified
    if (postbuildStep.length() > 0) {
        buffer.append(POSTBUILD + COLON + NEWLINE);
        if (postannouncebuildStep.length() > 0) {
            buffer.append(TAB + DASH + AT + escapedEcho(postannouncebuildStep));
        }
        buffer.append(TAB + DASH + postbuildStep + NEWLINE);
        buffer.append(TAB + DASH + AT + ECHO_BLANK_LINE + NEWLINE);
    }
    // Add the Secondary Outputs target, if needed
    if (secondaryOutputs.length > 0) {
        buffer.append(SECONDARY_OUTPUTS + COLON);
        List<String> outs2 = calculateSecondaryOutputs(secondaryOutputs);
        for (int i = 0; i < outs2.size(); i++) {
            buffer.append(WHITESPACE + "$(" + outs2.get(i) + ")");
        }
        buffer.append(NEWLINE + NEWLINE);
    }
    // Add all the needed dummy and phony targets
    buffer.append(".PHONY: all clean dependents" + NEWLINE);
    buffer.append(".SECONDARY:");
    if (prebuildStep.length() > 0) {
        buffer.append(WHITESPACE + MAINBUILD + WHITESPACE + PREBUILD);
    }
    if (postbuildStep.length() > 0) {
        buffer.append(WHITESPACE + POSTBUILD);
    }
    buffer.append(NEWLINE);
    for (String output : managedProjectOutputs) {
        buffer.append(output + COLON + NEWLINE);
    }
    buffer.append(NEWLINE);
    // Include makefile.targets supplemental makefile
    buffer.append("-include " + ROOT + FILE_SEPARATOR + MAKEFILE_TARGETS + NEWLINE);
    return buffer;
}
Also used : BuildMacroException(org.eclipse.cdt.managedbuilder.macros.BuildMacroException) ArrayList(java.util.ArrayList) IConfiguration(org.eclipse.cdt.managedbuilder.core.IConfiguration) ITool(org.eclipse.cdt.managedbuilder.core.ITool) IOutputType(org.eclipse.cdt.managedbuilder.core.IOutputType)

Aggregations

IOutputType (org.eclipse.cdt.managedbuilder.core.IOutputType)7 ArrayList (java.util.ArrayList)6 ITool (org.eclipse.cdt.managedbuilder.core.ITool)5 IPath (org.eclipse.core.runtime.IPath)4 List (java.util.List)3 BuildMacroException (org.eclipse.cdt.managedbuilder.macros.BuildMacroException)3 Entry (java.util.Map.Entry)2 BuildException (org.eclipse.cdt.managedbuilder.core.BuildException)2 IInputType (org.eclipse.cdt.managedbuilder.core.IInputType)2 IManagedOutputNameProvider (org.eclipse.cdt.managedbuilder.core.IManagedOutputNameProvider)2 IOption (org.eclipse.cdt.managedbuilder.core.IOption)2 IStatus (org.eclipse.core.runtime.IStatus)2 Status (org.eclipse.core.runtime.Status)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Vector (java.util.Vector)1 CSourceEntry (org.eclipse.cdt.core.settings.model.CSourceEntry)1 ICSettingEntry (org.eclipse.cdt.core.settings.model.ICSettingEntry)1