|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2024 John Dallaway and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + * |
| 11 | + * Contributors: |
| 12 | + * John Dallaway - initial implementation (#666) |
| 13 | + *******************************************************************************/ |
| 14 | +package org.eclipse.cdt.managedbuilder.gnu.ui; |
| 15 | + |
| 16 | +import java.util.regex.Pattern; |
| 17 | + |
| 18 | +import org.eclipse.cdt.core.cdtvariables.CdtVariableException; |
| 19 | +import org.eclipse.cdt.managedbuilder.core.BuildException; |
| 20 | +import org.eclipse.cdt.managedbuilder.core.IConfiguration; |
| 21 | +import org.eclipse.cdt.managedbuilder.core.IOption; |
| 22 | +import org.eclipse.cdt.managedbuilder.core.IOptionCommandGenerator; |
| 23 | +import org.eclipse.cdt.managedbuilder.core.ITool; |
| 24 | +import org.eclipse.cdt.managedbuilder.internal.buildmodel.FileMacroExplicitSubstitutor; |
| 25 | +import org.eclipse.cdt.managedbuilder.internal.macros.BuildfileMacroSubstitutor; |
| 26 | +import org.eclipse.cdt.utils.cdtvariables.CdtVariableResolver; |
| 27 | +import org.eclipse.cdt.utils.cdtvariables.IVariableSubstitutor; |
| 28 | +import org.eclipse.core.runtime.Platform; |
| 29 | +import org.eclipse.core.runtime.Status; |
| 30 | + |
| 31 | +/** |
| 32 | + * Assembler flags command generator. |
| 33 | + * This command generator supports managed build projects that were |
| 34 | + * created using older versions of the GNU toolchain build description |
| 35 | + * where assembly files were built by invoking the GNU "as" tool directly. |
| 36 | + * @noextend This class is not intended to be subclassed by clients. |
| 37 | + * @noinstantiate This class is not intended to be instantiated by clients. |
| 38 | + * @since 8.7 |
| 39 | + */ |
| 40 | +public class GnuAsmFlagsCommandGenerator implements IOptionCommandGenerator { |
| 41 | + |
| 42 | + private static final String DO_NOT_LINK_FLAG = "-c"; //$NON-NLS-1$ |
| 43 | + private static final Pattern DO_NOT_LINK_PATTERN = Pattern.compile("(^|\\s)-c($|\\s)"); //$NON-NLS-1$ |
| 44 | + private static final Pattern ASM_FLAG_PATTERN = Pattern.compile("(?<=^|\\s)-[aDKLR]\\S*"); //$NON-NLS-1$ |
| 45 | + |
| 46 | + @Override |
| 47 | + public String generateCommand(IOption option, IVariableSubstitutor macroSubstitutor) { |
| 48 | + String toolCommand = getToolCommand(option, macroSubstitutor); |
| 49 | + try { |
| 50 | + if (null != toolCommand && IOption.STRING == option.getValueType() && option.getCommand().isEmpty()) { |
| 51 | + String optionValue = option.getStringValue(); |
| 52 | + if (toolCommand.equals("gcc")) { //$NON-NLS-1$ |
| 53 | + // if the default assembler tool command has not been overridden |
| 54 | + String command = CdtVariableResolver.resolveToString(optionValue, macroSubstitutor); |
| 55 | + if (!DO_NOT_LINK_PATTERN.matcher(command).find()) { |
| 56 | + // if the "-c" flag is not already present on the command line we |
| 57 | + // assume the flags target the GNU "as" command line rather than the |
| 58 | + // "gcc" command line so we add the "-c" flag and apply the "-Wa," |
| 59 | + // prefix to those flags that are intended only for the assembler |
| 60 | + return DO_NOT_LINK_FLAG + " " + ASM_FLAG_PATTERN.matcher(command).replaceAll("-Wa,$0"); //$NON-NLS-1$ //$NON-NLS-2$ |
| 61 | + } |
| 62 | + } else if (toolCommand.endsWith("as") && optionValue.equals(DO_NOT_LINK_FLAG)) { //$NON-NLS-1$ |
| 63 | + // if GNU "as" is called directly and the default assembler flags have |
| 64 | + // not been overridden we remove the "-c" flag |
| 65 | + return ""; //$NON-NLS-1$ |
| 66 | + } |
| 67 | + } |
| 68 | + } catch (BuildException | CdtVariableException e) { |
| 69 | + Platform.getLog(getClass()).log(Status.error("Error generating GNU assembler command", e)); //$NON-NLS-1$ |
| 70 | + } |
| 71 | + return null; // fallback to default command generator |
| 72 | + } |
| 73 | + |
| 74 | + private static String getToolCommand(IOption option, IVariableSubstitutor macroSubstitutor) { |
| 75 | + // the option holder may be a super class of the assembler tool so we must |
| 76 | + // locate the tool from the build configuration to obtain the correct tool command |
| 77 | + IConfiguration config = getConfiguration(macroSubstitutor); |
| 78 | + if (config != null) { |
| 79 | + String optionHolderId = option.getOptionHolder().getId(); |
| 80 | + ITool[] tools = config.getToolsBySuperClassId(optionHolderId); |
| 81 | + if (1 == tools.length) { |
| 82 | + return tools[0].getToolCommand(); |
| 83 | + } |
| 84 | + } |
| 85 | + return null; |
| 86 | + } |
| 87 | + |
| 88 | + private static IConfiguration getConfiguration(IVariableSubstitutor macroSubstitutor) { |
| 89 | + if (macroSubstitutor instanceof BuildfileMacroSubstitutor bms) { // case ToolSettingsPrefStore |
| 90 | + return bms.getConfiguration(); |
| 91 | + } else if (macroSubstitutor instanceof FileMacroExplicitSubstitutor fmes) { // case BuildStep |
| 92 | + return fmes.getConfiguration(); |
| 93 | + } |
| 94 | + return null; |
| 95 | + } |
| 96 | + |
| 97 | +} |
0 commit comments