1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-30 04:15:35 +02:00

Set tool prefix for spawning GNU tool processes

Allows the GNU tool prefix to be specified by a CDT build variable.

Modifies the Cross GCC toolchain description to provide the GNU tool
prefix.

Part of #361
This commit is contained in:
John Dallaway 2023-04-15 17:07:50 +01:00
parent fe65ab6287
commit 0d9fa844bf
6 changed files with 156 additions and 10 deletions

View file

@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2 Bundle-ManifestVersion: 2
Bundle-Name: %pluginName Bundle-Name: %pluginName
Bundle-SymbolicName: org.eclipse.cdt.managedbuilder.core; singleton:=true Bundle-SymbolicName: org.eclipse.cdt.managedbuilder.core; singleton:=true
Bundle-Version: 9.5.100.qualifier Bundle-Version: 9.6.0.qualifier
Bundle-Activator: org.eclipse.cdt.managedbuilder.core.ManagedBuilderCorePlugin Bundle-Activator: org.eclipse.cdt.managedbuilder.core.ManagedBuilderCorePlugin
Bundle-Vendor: %providerName Bundle-Vendor: %providerName
Bundle-Localization: plugin Bundle-Localization: plugin

View file

@ -0,0 +1,50 @@
package org.eclipse.cdt.managedbuilder.macros;
import org.eclipse.cdt.core.cdtvariables.CdtVariableException;
import org.eclipse.cdt.core.cdtvariables.ICdtVariableStatus;
import org.eclipse.cdt.managedbuilder.core.BuildException;
import org.eclipse.cdt.managedbuilder.core.IOption;
import org.eclipse.cdt.utils.IGnuToolFactory;
import org.eclipse.core.runtime.Status;
/**
* A CDT build variable defining the GNU tool prefix for use by
* an {@link org.eclipse.cdt.utils.IGnuToolFactory} implementation.
*
* @since 9.6
*/
public abstract class AbstractGnuToolPrefixMacro implements IBuildMacro {
@Override
public String getName() {
return IGnuToolFactory.GNU_TOOL_PREFIX_VARIABLE;
}
@Override
public int getValueType() {
return IBuildMacro.VALUE_TEXT;
}
@Override
public int getMacroValueType() {
return getValueType();
}
@Override
public abstract String getStringValue() throws BuildMacroException;
@Override
public String[] getStringListValue() throws BuildMacroException {
throw new BuildMacroException(
new CdtVariableException(ICdtVariableStatus.TYPE_MACRO_NOT_STRINGLIST, getName(), null, getName()));
}
protected String getStringValue(IOption option) throws BuildMacroException {
try {
return option.getStringValue();
} catch (BuildException e) {
throw new BuildMacroException(Status.error("Error getting macro value: " + getName(), e)); //$NON-NLS-1$
}
}
}

View file

@ -10,7 +10,7 @@
* *
* Contributors: * Contributors:
* QNX Software Systems - initial API and implementation * QNX Software Systems - initial API and implementation
* John Dallaway - set environment for spawning GNU tool processes (#361) * John Dallaway - set environment and tool prefix (#361)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.utils; package org.eclipse.cdt.utils;
@ -19,11 +19,15 @@ import java.util.Arrays;
import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.ICExtension; import org.eclipse.cdt.core.ICExtension;
import org.eclipse.cdt.core.cdtvariables.CdtVariableException;
import org.eclipse.cdt.core.cdtvariables.ICdtVariable;
import org.eclipse.cdt.core.envvar.IEnvironmentVariable; import org.eclipse.cdt.core.envvar.IEnvironmentVariable;
import org.eclipse.cdt.core.settings.model.ICConfigExtensionReference; import org.eclipse.cdt.core.settings.model.ICConfigExtensionReference;
import org.eclipse.cdt.core.settings.model.ICConfigurationDescription; import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
public class DefaultGnuToolFactory implements IGnuToolFactory { public class DefaultGnuToolFactory implements IGnuToolFactory {
protected ICExtension fExtension; protected ICExtension fExtension;
@ -94,7 +98,7 @@ public class DefaultGnuToolFactory implements IGnuToolFactory {
ICConfigExtensionReference ref = fExtension.getConfigExtensionReference(); ICConfigExtensionReference ref = fExtension.getConfigExtensionReference();
String value = ref.getExtensionData("addr2line"); //$NON-NLS-1$ String value = ref.getExtensionData("addr2line"); //$NON-NLS-1$
if (value == null || value.length() == 0) { if (value == null || value.length() == 0) {
value = "addr2line"; //$NON-NLS-1$ value = getToolPrefix() + "addr2line"; //$NON-NLS-1$
} }
return new Path(value); return new Path(value);
} }
@ -103,7 +107,7 @@ public class DefaultGnuToolFactory implements IGnuToolFactory {
ICConfigExtensionReference ref = fExtension.getConfigExtensionReference(); ICConfigExtensionReference ref = fExtension.getConfigExtensionReference();
String value = ref.getExtensionData("objdump"); //$NON-NLS-1$ String value = ref.getExtensionData("objdump"); //$NON-NLS-1$
if (value == null || value.length() == 0) { if (value == null || value.length() == 0) {
value = "objdump"; //$NON-NLS-1$ value = getToolPrefix() + "objdump"; //$NON-NLS-1$
} }
return new Path(value); return new Path(value);
} }
@ -121,7 +125,7 @@ public class DefaultGnuToolFactory implements IGnuToolFactory {
ICConfigExtensionReference ref = fExtension.getConfigExtensionReference(); ICConfigExtensionReference ref = fExtension.getConfigExtensionReference();
String value = ref.getExtensionData("c++filt"); //$NON-NLS-1$ String value = ref.getExtensionData("c++filt"); //$NON-NLS-1$
if (value == null || value.length() == 0) { if (value == null || value.length() == 0) {
value = "c++filt"; //$NON-NLS-1$ value = getToolPrefix() + "c++filt"; //$NON-NLS-1$
} }
return new Path(value); return new Path(value);
} }
@ -130,7 +134,7 @@ public class DefaultGnuToolFactory implements IGnuToolFactory {
ICConfigExtensionReference ref = fExtension.getConfigExtensionReference(); ICConfigExtensionReference ref = fExtension.getConfigExtensionReference();
String value = ref.getExtensionData("strip"); //$NON-NLS-1$ String value = ref.getExtensionData("strip"); //$NON-NLS-1$
if (value == null || value.length() == 0) { if (value == null || value.length() == 0) {
value = "strip"; //$NON-NLS-1$ value = getToolPrefix() + "strip"; //$NON-NLS-1$
} }
return new Path(value); return new Path(value);
} }
@ -139,7 +143,7 @@ public class DefaultGnuToolFactory implements IGnuToolFactory {
ICConfigExtensionReference ref = fExtension.getConfigExtensionReference(); ICConfigExtensionReference ref = fExtension.getConfigExtensionReference();
String value = ref.getExtensionData("nm"); //$NON-NLS-1$ String value = ref.getExtensionData("nm"); //$NON-NLS-1$
if (value == null || value.length() == 0) { if (value == null || value.length() == 0) {
value = "nm"; //$NON-NLS-1$ value = getToolPrefix() + "nm"; //$NON-NLS-1$
} }
return new Path(value); return new Path(value);
} }
@ -161,4 +165,28 @@ public class DefaultGnuToolFactory implements IGnuToolFactory {
return Arrays.stream(vars).map(v -> String.format("%s=%s", v.getName(), v.getValue())) //$NON-NLS-1$ return Arrays.stream(vars).map(v -> String.format("%s=%s", v.getName(), v.getValue())) //$NON-NLS-1$
.toArray(String[]::new); .toArray(String[]::new);
} }
/** @since 8.2 */
protected String getToolPrefix() {
ICConfigExtensionReference ref = fExtension.getConfigExtensionReference();
ICConfigurationDescription cfg = ref.getConfiguration();
ICdtVariable[] userVars = CCorePlugin.getUserVarSupplier().getMacros(cfg);
ICdtVariable var = Arrays.stream(userVars).filter(v -> v.getName().equals(GNU_TOOL_PREFIX_VARIABLE)).findFirst()
.orElse(null);
// if user-defined variable not found, look for system variable provided by toolchain integration
if (var == null) {
var = CCorePlugin.getDefault().getCdtVariableManager().getVariable(GNU_TOOL_PREFIX_VARIABLE, cfg);
}
if (var != null) {
try {
return var.getStringValue();
} catch (CdtVariableException e) {
Platform.getLog(getClass())
.log(Status.error("Error getting CDT variable string value: " + GNU_TOOL_PREFIX_VARIABLE, e)); //$NON-NLS-1$
}
}
return ""; //$NON-NLS-1$
}
} }

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2005, 2009 QNX Software Systems and others. * Copyright (c) 2005, 2023 QNX Software Systems and others.
* *
* This program and the accompanying materials * This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0 * are made available under the terms of the Eclipse Public License 2.0
@ -10,6 +10,7 @@
* *
* Contributors: * Contributors:
* QNX Software Systems - initial API and implementation * QNX Software Systems - initial API and implementation
* John Dallaway - support GNU tool prefix lookup (#361)
*******************************************************************************/ *******************************************************************************/
/* /*
* Created on Jul 5, 2004 * Created on Jul 5, 2004
@ -27,6 +28,9 @@ import org.eclipse.core.runtime.IPath;
*/ */
public interface IGnuToolFactory { public interface IGnuToolFactory {
/** @since 8.2 */
public static final String GNU_TOOL_PREFIX_VARIABLE = "gnu_tool_prefix"; //$NON-NLS-1$
Addr2line getAddr2line(IPath path); Addr2line getAddr2line(IPath path);
CPPFilt getCPPFilt(); CPPFilt getCPPFilt();

View file

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?> <?eclipse version="3.4"?>
<!-- <!--
# Copyright (c) 2009, 2011 Wind River Systems, Inc. and others. # Copyright (c) 2009, 2023 Wind River Systems, Inc. and others.
# #
# This program and the accompanying materials # This program and the accompanying materials
# are made available under the terms of the Eclipse Public License 2.0 # are made available under the terms of the Eclipse Public License 2.0
@ -13,6 +13,7 @@
# Contributors: # Contributors:
# Doug Schaefer (Wind River) - initial API and implementation # Doug Schaefer (Wind River) - initial API and implementation
# Anna Dushistova (Mentor Graphics)- [329531][crossgcc] crossgcc fails to build a project # Anna Dushistova (Mentor Graphics)- [329531][crossgcc] crossgcc fails to build a project
# John Dallaway - enable GNU tool prefix lookup (#361)
--> -->
<plugin> <plugin>
@ -24,6 +25,7 @@
<toolChain <toolChain
archList="all" archList="all"
configurationEnvironmentSupplier="org.eclipse.cdt.internal.build.crossgcc.CrossEnvironmentVariableSupplier" configurationEnvironmentSupplier="org.eclipse.cdt.internal.build.crossgcc.CrossEnvironmentVariableSupplier"
configurationMacroSupplier="org.eclipse.cdt.internal.build.crossgcc.CrossBuildMacroSupplier"
id="cdt.managedbuild.toolchain.gnu.cross.base" id="cdt.managedbuild.toolchain.gnu.cross.base"
isAbstract="false" isAbstract="false"
languageSettingsProviders="org.eclipse.cdt.managedbuilder.core.GCCBuildCommandParser;org.eclipse.cdt.build.crossgcc.CrossGCCBuiltinSpecsDetector" languageSettingsProviders="org.eclipse.cdt.managedbuilder.core.GCCBuildCommandParser;org.eclipse.cdt.build.crossgcc.CrossGCCBuiltinSpecsDetector"
@ -32,7 +34,7 @@
targetTool="cdt.managedbuild.tool.gnu.cross.c.linker;cdt.managedbuild.tool.gnu.cross.cpp.linker;cdt.managedbuild.tool.gnu.archiver"> targetTool="cdt.managedbuild.tool.gnu.cross.c.linker;cdt.managedbuild.tool.gnu.cross.cpp.linker;cdt.managedbuild.tool.gnu.archiver">
<targetPlatform <targetPlatform
archList="all" archList="all"
binaryParser="org.eclipse.cdt.core.ELF" binaryParser="org.eclipse.cdt.core.GNU_ELF"
id="cdt.managedbuild.targetPlatform.gnu.cross" id="cdt.managedbuild.targetPlatform.gnu.cross"
isAbstract="false" isAbstract="false"
osList="all"> osList="all">

View file

@ -0,0 +1,62 @@
/*******************************************************************************
* Copyright (c) 2023 John Dallaway and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* John Dallaway - initial API and implementation (#361)
*******************************************************************************/
package org.eclipse.cdt.internal.build.crossgcc;
import org.eclipse.cdt.managedbuilder.core.IConfiguration;
import org.eclipse.cdt.managedbuilder.core.IOption;
import org.eclipse.cdt.managedbuilder.macros.AbstractGnuToolPrefixMacro;
import org.eclipse.cdt.managedbuilder.macros.BuildMacroException;
import org.eclipse.cdt.managedbuilder.macros.IBuildMacro;
import org.eclipse.cdt.managedbuilder.macros.IBuildMacroProvider;
import org.eclipse.cdt.managedbuilder.macros.IConfigurationBuildMacroSupplier;
import org.eclipse.cdt.utils.IGnuToolFactory;
import org.eclipse.core.runtime.Status;
public class CrossBuildMacroSupplier implements IConfigurationBuildMacroSupplier {
private static class GnuToolPrefixMacro extends AbstractGnuToolPrefixMacro {
private static final String GNU_TOOL_PREFIX_OPTION = "cdt.managedbuild.option.gnu.cross.prefix"; //$NON-NLS-1$
private final IConfiguration configuration;
public GnuToolPrefixMacro(IConfiguration configuration) {
this.configuration = configuration;
}
@Override
public String getStringValue() throws BuildMacroException {
final IOption option = configuration.getToolChain().getOptionBySuperClassId(GNU_TOOL_PREFIX_OPTION);
if (null == option) {
throw new BuildMacroException(Status.error("Toolchain option not found: " + GNU_TOOL_PREFIX_OPTION)); //$NON-NLS-1$
}
return getStringValue(option);
}
}
@Override
public IBuildMacro getMacro(String macroName, IConfiguration configuration, IBuildMacroProvider provider) {
if (IGnuToolFactory.GNU_TOOL_PREFIX_VARIABLE.equals(macroName)) {
return new GnuToolPrefixMacro(configuration);
}
return null;
}
@Override
public IBuildMacro[] getMacros(IConfiguration configuration, IBuildMacroProvider provider) {
final IBuildMacro macro = getMacro(IGnuToolFactory.GNU_TOOL_PREFIX_VARIABLE, configuration, provider);
return (null == macro) ? new IBuildMacro[0] : new IBuildMacro[] { macro };
}
}