Search in sources :

Example 16 with IConfiguration

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

the class ArduinoGnuMakefileGenerator method populateDummyTargets.

public static boolean populateDummyTargets(IResourceInfo rcInfo, IFile makefile, boolean force) throws CoreException, IOException {
    if (makefile == null || !makefile.exists())
        return false;
    // Get the contents of the dependency file
    InputStream contentStream = makefile.getContents(false);
    StringBuilder inBuilder = null;
    try (Reader in = new InputStreamReader(contentStream)) {
        int chunkSize = contentStream.available();
        inBuilder = new StringBuilder(chunkSize);
        char[] readBuilder = new char[chunkSize];
        int n = in.read(readBuilder);
        while (n > 0) {
            inBuilder.append(readBuilder);
            n = in.read(readBuilder);
        }
        contentStream.close();
        in.close();
    }
    // The rest of this operation is equally expensive, so
    // if we are doing an incremental build, only update the
    // files that do not have a comment
    String inBuilderString = inBuilder.toString();
    if (!force && inBuilderString.startsWith(COMMENT_SYMBOL)) {
        return false;
    }
    // Try to determine if this file already has dummy targets defined.
    // If so, we will only add the comment.
    // 
    String[] builderLines = inBuilderString.split("[\\r\\n]");
    for (String builderLine : builderLines) {
        if (builderLine.endsWith(":")) {
            // 
            StringBuilder outBuilder = addDefaultHeader();
            outBuilder.append(inBuilder);
            save(outBuilder, makefile);
            return true;
        }
    }
    // Reconstruct the buffer tokens into useful chunks of dependency
    // information
    Vector<String> bufferTokens = new Vector<>(Arrays.asList(inBuilderString.split("\\s")));
    Vector<String> deps = new Vector<>(bufferTokens.size());
    Iterator<String> tokenIter = bufferTokens.iterator();
    while (tokenIter.hasNext()) {
        String token = tokenIter.next();
        if (token.lastIndexOf("\\") == token.length() - 1 && token.length() > 1) {
            // end
            while (tokenIter.hasNext()) {
                String nextToken = tokenIter.next();
                token += WHITESPACE + nextToken;
                if (!nextToken.endsWith("\\")) {
                    break;
                }
            }
        }
        deps.add(token);
    }
    deps.trimToSize();
    // Now find the header file dependencies and make dummy targets for them
    boolean save = false;
    StringBuilder outBuilder = null;
    // If we are doing an incremental build, only update the files that do
    // not have a comment
    String firstToken;
    try {
        firstToken = deps.get(0);
    } catch (ArrayIndexOutOfBoundsException e) {
        // This makes no sense so bail
        return false;
    }
    // Put the generated comments in the output buffer
    if (!firstToken.startsWith(COMMENT_SYMBOL)) {
        outBuilder = addDefaultHeader();
    } else {
        outBuilder = new StringBuilder();
    }
    // output
    if (firstToken.startsWith("-n")) {
        // Now let's parse:
        // Win32 outputs -n '<path>/<file>.d <path>/'
        // POSIX outputs -n <path>/<file>.d <path>/
        // Get the dep file name
        String secondToken;
        try {
            secondToken = deps.get(1);
        } catch (ArrayIndexOutOfBoundsException e) {
            secondToken = new String();
        }
        if (secondToken.startsWith("'")) {
            // This is the Win32 implementation of echo (MinGW without MSYS)
            outBuilder.append(secondToken.substring(1) + WHITESPACE);
        } else {
            outBuilder.append(secondToken + WHITESPACE);
        }
        // The relative path to the build goal comes next
        String thirdToken;
        try {
            thirdToken = deps.get(2);
        } catch (ArrayIndexOutOfBoundsException e) {
            thirdToken = new String();
        }
        int lastIndex = thirdToken.lastIndexOf("'");
        if (lastIndex != -1) {
            if (lastIndex == 0) {
                outBuilder.append(WHITESPACE);
            } else {
                outBuilder.append(thirdToken.substring(0, lastIndex - 1));
            }
        } else {
            outBuilder.append(thirdToken);
        }
        // Followed by the target output by the compiler plus ':'
        // If we see any empty tokens here, assume they are the result of
        // a line feed output by "echo" and skip them
        String fourthToken;
        int nToken = 3;
        try {
            do {
                fourthToken = deps.get(nToken++);
            } while (fourthToken.length() == 0);
        } catch (ArrayIndexOutOfBoundsException e) {
            fourthToken = new String();
        }
        outBuilder.append(fourthToken + WHITESPACE);
        // Followed by the actual dependencies
        try {
            for (String nextElement : deps) {
                if (nextElement.endsWith("\\")) {
                    outBuilder.append(nextElement + NEWLINE + WHITESPACE);
                } else {
                    outBuilder.append(nextElement + WHITESPACE);
                }
            }
        } catch (IndexOutOfBoundsException e) {
        /* JABA is not going to write this code */
        }
    } else {
        outBuilder.append(inBuilder);
    }
    outBuilder.append(NEWLINE);
    save = true;
    IFolderInfo fo = null;
    if (rcInfo instanceof IFolderInfo) {
        fo = (IFolderInfo) rcInfo;
    } else {
        IConfiguration c = rcInfo.getParent();
        fo = (IFolderInfo) c.getResourceInfo(rcInfo.getPath().removeLastSegments(1), false);
    }
    // Dummy targets to add to the makefile
    for (String dummy : deps) {
        IPath dep = new Path(dummy);
        String extension = dep.getFileExtension();
        if (fo.isHeaderFile(extension)) {
            /*
				 * The formatting here is <dummy_target>:
				 */
            outBuilder.append(dummy + COLON + NEWLINE + NEWLINE);
        }
    }
    // Write them out to the makefile
    if (save) {
        save(outBuilder, makefile);
        return true;
    }
    return false;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) InputStreamReader(java.io.InputStreamReader) IPath(org.eclipse.core.runtime.IPath) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IFolderInfo(org.eclipse.cdt.managedbuilder.core.IFolderInfo) IConfiguration(org.eclipse.cdt.managedbuilder.core.IConfiguration) Vector(java.util.Vector)

Example 17 with IConfiguration

use of org.eclipse.cdt.managedbuilder.core.IConfiguration 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)

Example 18 with IConfiguration

use of org.eclipse.cdt.managedbuilder.core.IConfiguration in project usbdm-eclipse-plugins by podonoghue.

the class KSDKLibraryImportWizard method updateConfigurations.

public void updateConfigurations(IProject project, IProgressMonitor monitor) {
    final int WORK_SCALE = 1000;
    ManagedBuildManager.saveBuildInfo(project, true);
    IConfiguration[] projectConfigs = ManagedBuildManager.getBuildInfo(project).getManagedProject().getConfigurations();
    try {
        monitor.beginTask("Update Configurations", WORK_SCALE * projectConfigs.length);
        for (IConfiguration config : projectConfigs) {
            ScannerConfigBuilder.build(config, ScannerConfigBuilder.PERFORM_CORE_UPDATE, monitor);
            monitor.worked(WORK_SCALE);
        }
    } finally {
        monitor.done();
    }
}
Also used : IConfiguration(org.eclipse.cdt.managedbuilder.core.IConfiguration)

Example 19 with IConfiguration

use of org.eclipse.cdt.managedbuilder.core.IConfiguration in project usbdm-eclipse-plugins by podonoghue.

the class UsbdmGCCSpecsRunSIProvider method getToolPrefix.

/**
 * @param project       The project to look in for options
 * @return              The prefix for the build tools
 */
private String getToolPrefix(IProject project) {
    IManagedBuildInfo info = ManagedBuildManager.getBuildInfo(project);
    if (info == null) {
        return "";
    }
    IConfiguration cfg = info.getDefaultConfiguration();
    // System.err.println("UsbdmGCCSpecsRunSIProvider.initialize() Found IConfiguration = " + cfg.getName());
    IToolChain toolChain = cfg.getToolChain();
    // System.err.println("UsbdmGCCSpecsRunSIProvider.initialize() Found toolChain = " + toolChain.getName());
    // Find selected build tool (either ARM or Coldfire)
    IOption buildToolOption = toolChain.getOptionBySuperClassId(UsbdmConstants.ARM_BUILDTOOLS_OPTIONS);
    if (buildToolOption == null) {
        buildToolOption = toolChain.getOptionBySuperClassId(UsbdmConstants.COLDFIRE_BUILDTOOLS_OPTIONS);
    }
    if (buildToolOption == null) {
        return "";
    }
    // Get build path variable
    ToolInformationData toolData = ToolInformationData.getToolInformationTable().get(buildToolOption.getValue().toString());
    if (toolData == null) {
        return "";
    }
    String toolPrefixVariableId = toolData.getPrefixVariableName();
    if (toolPrefixVariableId == null) {
        return "";
    }
    UsbdmSharedSettings settings = UsbdmSharedSettings.getSharedSettings();
    String toolPrefix = null;
    if (settings != null) {
        toolPrefix = settings.get(toolPrefixVariableId);
    }
    if (toolPrefix == null) {
        toolPrefix = "Tool Prefix not set";
        return "";
    }
    // System.err.println("UsbdmGCCSpecsRunSIProvider.initialize() Found tool prefix = " + toolPrefix);
    return toolPrefix;
}
Also used : UsbdmSharedSettings(net.sourceforge.usbdm.constants.UsbdmSharedSettings) IManagedBuildInfo(org.eclipse.cdt.managedbuilder.core.IManagedBuildInfo) ToolInformationData(net.sourceforge.usbdm.constants.ToolInformationData) IToolChain(org.eclipse.cdt.managedbuilder.core.IToolChain) IOption(org.eclipse.cdt.managedbuilder.core.IOption) IConfiguration(org.eclipse.cdt.managedbuilder.core.IConfiguration)

Example 20 with IConfiguration

use of org.eclipse.cdt.managedbuilder.core.IConfiguration in project usbdm-eclipse-plugins by podonoghue.

the class ProjectUtilities method createIncludeFolder.

public static void createIncludeFolder(IProject project, String targetPath, IProgressMonitor progressMonitor) throws CoreException, BuildException {
    // createSourceFolder(project, targetPath, progressMonitor);
    createFolder(project, targetPath, progressMonitor);
    IManagedBuildInfo info = ManagedBuildManager.getBuildInfo(project);
    // Add to Include search paths
    IConfiguration[] configs = info.getManagedProject().getConfigurations();
    for (IConfiguration config : configs) {
        // Creates include folder path that is portable (e.g. rename project doesn't break paths)
        String path = "\"${ProjDirPath}/" + project.getFolder(targetPath).getProjectRelativePath().toOSString() + "\"";
        IToolChain toolChain = config.getToolChain();
        setIncludePathOptionForConfig(path, config, toolChain.getOptions(), toolChain);
        ITool[] tools = config.getTools();
        for (ITool tool : tools) {
            setIncludePathOptionForConfig(path, config, tool.getOptions(), tool);
        }
    }
    ManagedBuildManager.saveBuildInfo(project, true);
}
Also used : IManagedBuildInfo(org.eclipse.cdt.managedbuilder.core.IManagedBuildInfo) IToolChain(org.eclipse.cdt.managedbuilder.core.IToolChain) IConfiguration(org.eclipse.cdt.managedbuilder.core.IConfiguration) ITool(org.eclipse.cdt.managedbuilder.core.ITool)

Aggregations

IConfiguration (org.eclipse.cdt.managedbuilder.core.IConfiguration)27 ITool (org.eclipse.cdt.managedbuilder.core.ITool)11 IOption (org.eclipse.cdt.managedbuilder.core.IOption)9 IToolChain (org.eclipse.cdt.managedbuilder.core.IToolChain)9 IManagedBuildInfo (org.eclipse.cdt.managedbuilder.core.IManagedBuildInfo)6 BuildException (org.eclipse.cdt.managedbuilder.core.BuildException)5 CoreException (org.eclipse.core.runtime.CoreException)5 ICConfigurationDescription (org.eclipse.cdt.core.settings.model.ICConfigurationDescription)4 ICProjectDescription (org.eclipse.cdt.core.settings.model.ICProjectDescription)4 IFolderInfo (org.eclipse.cdt.managedbuilder.core.IFolderInfo)4 BuildConfigurationData (org.eclipse.cdt.managedbuilder.internal.dataprovider.BuildConfigurationData)4 ArrayList (java.util.ArrayList)3 ToolInformationData (net.sourceforge.usbdm.constants.ToolInformationData)3 IPath (org.eclipse.core.runtime.IPath)3 UsbdmSharedSettings (net.sourceforge.usbdm.constants.UsbdmSharedSettings)2 CoreModel (org.eclipse.cdt.core.model.CoreModel)2 CConfigurationData (org.eclipse.cdt.core.settings.model.extension.CConfigurationData)2 IBuilder (org.eclipse.cdt.managedbuilder.core.IBuilder)2 IManagedProject (org.eclipse.cdt.managedbuilder.core.IManagedProject)2 Configuration (org.eclipse.cdt.managedbuilder.internal.core.Configuration)2