1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-23 06:32:10 +02:00

Bug 467771 - add basis to support autotools option as NAME=VALUE

It is not obvious in autotools preferences UI how to set variables like CC=/sbin/gcc
Introduces the basis to allow extend the UI to include such as kind of variables.

Change-Id: Ife0aada50d8c253f3fff39e7087f5fd54803ba48
Signed-off-by: Wainer dos Santos Moschetta <wainersm@linux.vnet.ibm.com>
This commit is contained in:
Wainer dos Santos Moschetta 2016-02-19 21:02:40 -02:00 committed by Gerrit Code Review @ Eclipse.org
parent 9d492879be
commit 880b1b606a
10 changed files with 344 additions and 2 deletions

View file

@ -58,5 +58,10 @@ public class AutotoolsOptionConstants {
public static final String TOOL_AUTOGEN = "autogen"; // $NON-NLS-1$
public static final String CATEGORY_OPTIONS = "options"; // $NON-NLS-1$
public static final String OPT_AUTOGENOPTS = "autogenOpts"; // $NON-NLS-1$
/**
* @since 2.0
*/
public static final String CATEGORY_ENVVAR = "cat_envvar"; // $NON-NLS-1$
public final static String OPT_ENVVAR = "env_vars"; // $NON-NLS-1$
}

View file

@ -22,6 +22,10 @@ public interface IAutotoolsOption {
int TOOL = 5;
int FLAG = 6;
int FLAGVALUE = 7;
/**
* @since 2.0
*/
int ENVVAR = 8;
int getType();

View file

@ -102,6 +102,8 @@ public class AutotoolsConfiguration implements IAConfiguration {
new Option(AutotoolsOptionConstants.OPT_PROGRAM_PREFIX, "program_prefix", IConfigureOption.STRING), // $NON-NLS-1$
new Option(AutotoolsOptionConstants.OPT_PROGRAM_SUFFIX, "program_suffix", IConfigureOption.STRING), // $NON-NLS-1$
new Option(AutotoolsOptionConstants.OPT_PROGRAM_TRANSFORM_NAME, "program_transform_name", IConfigureOption.STRING), // $NON-NLS-1$
new Option(AutotoolsOptionConstants.CATEGORY_ENVVAR, IConfigureOption.CATEGORY),
new Option(AutotoolsOptionConstants.OPT_ENVVAR, IConfigureOption.ENVVAR),
new Option(AutotoolsOptionConstants.CATEGORY_FEATURES, IConfigureOption.CATEGORY),
new Option(AutotoolsOptionConstants.OPT_ENABLE_MAINTAINER_MODE, "enable_maintainer_mode", IConfigureOption.BIN), // $NON-NLS-1$
new Option(AutotoolsOptionConstants.FLAG_CFLAGS, "cflags", AutotoolsOptionConstants.FLAG_CFLAGS_FLAGS, IConfigureOption.FLAG), // $NON-NLS-1$
@ -193,6 +195,12 @@ public class AutotoolsConfiguration implements IAConfiguration {
lastFlag.addChild(opt.name);
configOptions.put(opt.name, fv);
break;
case IConfigureOption.ENVVAR:
VariableConfigureOption v = new VariableConfigureOption(opt.name, opt.transformedName, this);
if (defaultValue != null)
v.setValue(defaultValue);
configOptions.put(opt.name, v);
break;
}
}
toolList = tools.toArray(new Option[tools.size()]);

View file

@ -566,6 +566,7 @@ public class AutotoolsConfigurationManager implements IResourceChangeListener {
case FLAGVALUE:
case MULTIARG:
case INTERNAL:
case ENVVAR:
return true;
}
return false;

View file

@ -91,6 +91,10 @@ Option.configure.cflags_gcov.parm=-fprofile-arcs -ftest-coverage
Option.configure.cflags=Compiler Flags:
Option.configure.cat_envvar=Environment variables
Option.configure.env_vars=Export environment variables
Option.configure.env_vars.tip=Environment variables to be used during configuration
Option.configure.autogen=autogen
Option.configure.options=Options
Option.configure.autogenOpts=Additional command-line options

View file

@ -24,6 +24,10 @@ public interface IConfigureOption {
int TOOL = IAutotoolsOption.TOOL;
int FLAG = IAutotoolsOption.FLAG;
int FLAGVALUE = IAutotoolsOption.FLAGVALUE;
/**
* @since 2.0
*/
int ENVVAR = IAutotoolsOption.ENVVAR;
String getName();

View file

@ -0,0 +1,73 @@
/*******************************************************************************
* Copyright (c) 2016 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Wainer dos Santos Moschetta - initial implementation
*******************************************************************************/
package org.eclipse.cdt.internal.autotools.core.configure;
/**
* This class represents a a list of environment variables as NAME="VALUE"
*
* @since 2.0
*
*/
public class VariableConfigureOption extends AbstractConfigurationOption {
private String value;
public VariableConfigureOption(String name, AutotoolsConfiguration cfg) {
super(name, cfg);
this.value = ""; // $NON-NLS-1$
}
public VariableConfigureOption(String name, String transformedName, AutotoolsConfiguration autotoolsConfiguration) {
super(name, transformedName, autotoolsConfiguration);
this.value = ""; // $NON-NLS-1$
}
public VariableConfigureOption(String name, AutotoolsConfiguration cfg, String value) {
super(name, cfg);
this.value = value;
}
@Override
public String getParameter() {
if (isParmSet())
return this.value;
return ""; //$NON-NLS-1$
}
@Override
public boolean isParmSet() {
return !this.value.isEmpty();
}
@Override
public IConfigureOption copy(AutotoolsConfiguration cfg) {
return new VariableConfigureOption(name, cfg, value);
}
@Override
public void setValue(String newValue) {
if (!newValue.equals(value)) {
cfg.setDirty(true);
value = newValue;
}
}
@Override
public String getValue() {
return value;
}
@Override
public int getType() {
return ENVVAR;
}
}

View file

@ -22,7 +22,8 @@ Require-Bundle: org.eclipse.ui,
org.eclipse.core.filesystem;bundle-version="1.2.0",
org.eclipse.cdt.make.ui;bundle-version="6.0.0",
org.eclipse.ui.views;bundle-version="3.4.0",
org.eclipse.cdt.remote.core;bundle-version="1.0.0"
org.eclipse.cdt.remote.core;bundle-version="1.0.0",
org.eclipse.swt
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Bundle-ActivationPolicy: lazy
Export-Package: org.eclipse.cdt.autotools.ui,

View file

@ -16,15 +16,26 @@ import java.util.List;
import org.eclipse.cdt.internal.autotools.core.configure.AutotoolsConfiguration;
import org.eclipse.cdt.internal.autotools.core.configure.IAConfiguration;
import org.eclipse.cdt.internal.autotools.core.configure.IConfigureOption;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.preference.BooleanFieldEditor;
import org.eclipse.jface.preference.FieldEditor;
import org.eclipse.jface.preference.ListEditor;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.FontMetrics;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class AutotoolsCategoryPropertyOptionPage extends
AbstractConfigurePropertyOptionsPage {
@ -70,7 +81,225 @@ public class AutotoolsCategoryPropertyOptionPage extends
@Override
protected void doStore() {}
}
static class VariableListEditor extends ListEditor {
Composite fParent;
String fName;
String fLabelText;
boolean isLoaded;
public VariableListEditor(String name, String labelText, Composite parent) {
fName = name;
fLabelText = labelText;
fParent = parent;
isLoaded = false;
init(fName, fLabelText);
createControl(fParent);
}
@Override
protected void selectionChanged() {
super.selectionChanged();
super.fireValueChanged(getPreferenceName(), "", ""); //$NON-NLS-1$ //$NON-NLS-2$
}
@Override
protected String createList(String[] arg0) {
StringBuffer sb = new StringBuffer();
for (String item : arg0) {
sb.append(item);
sb.append("\\s"); //$NON-NLS-1$
}
return sb.toString();
}
@Override
protected void doLoad() {
if (!isLoaded) {
super.doLoad();
isLoaded = true;
}
};
public void setToolTipText(String toolTip) {
this.getLabelControl().setToolTipText(toolTip);
}
/**
* Dialog user inputs variable's name and value.
*/
class DialogNewVar extends Dialog {
private String name;
private Text fTextName;
private String value;
private Text fTextValue;
private Button fOkButton;
public DialogNewVar(Shell shell) {
super(shell);
}
@Override
protected void configureShell(Shell newShell) {
super.configureShell(newShell);
newShell.setText(AutotoolsPropertyMessages.getString("NewEnvVarDialog.title"));
};
@Override
protected Control createDialogArea(Composite parent) {
Composite composite = new Composite(parent, SWT.NONE);
GridLayout layout = new GridLayout(2, false);
layout.marginWidth = 5;
layout.numColumns = 2;
composite.setLayout(layout);
GC gc = new GC(composite);
gc.setFont(composite.getFont());
FontMetrics metrics = gc.getFontMetrics();
gc.dispose();
int fieldWidthHint = convertWidthInCharsToPixels(metrics, 50);
Label label = new Label(composite, SWT.NONE);
label.setText(AutotoolsPropertyMessages.getString("NewEnvVarDialog.name_field"));
fTextName = new Text(composite, SWT.SINGLE | SWT.BORDER);
GridData gd = new GridData(GridData.FILL_BOTH);
gd.grabExcessHorizontalSpace = true;
gd.widthHint = fieldWidthHint;
fTextName.setLayoutData(gd);
// Name field cannot be empty.
fTextName.addModifyListener(new ModifyListener() {
@Override
public void modifyText(ModifyEvent e) {
if (fOkButton != null) {
fOkButton.setEnabled(fTextName.getText().length() > 0);
}
}
});
label = new Label(composite, SWT.NONE);
label.setText(AutotoolsPropertyMessages.getString("NewEnvVarDialog.value_field"));
fTextValue = new Text(composite, SWT.SINGLE | SWT.BORDER);
gd = new GridData(GridData.FILL_BOTH);
gd.grabExcessHorizontalSpace = true;
gd.widthHint = fieldWidthHint;
fTextValue.setLayoutData(gd);
return composite;
};
// Obtain instance of OK button and set disabled.
@Override
protected Control createButtonBar(Composite parent) {
Control control = super.createButtonBar(parent);
fOkButton = getButton(IDialogConstants.OK_ID);
fOkButton.setEnabled(false);
return control;
}
@Override
protected void okPressed() {
name = fTextName.getText().trim();
value = fTextValue.getText();
if (value != null) {
value = value.trim();
} else {
value = ""; //$NON-NLS-1$
}
super.okPressed();
}
public String getName() {
return name;
}
public String getValue() {
return value;
}
};
@Override
protected String getNewInputObject() {
DialogNewVar newDialog = new DialogNewVar(getShell());
newDialog.open();
String name = newDialog.getName();
// Create quoted string like CFLAGS="-q -O3"
if (name != null) {
String quote = "\""; //$NON-NLS-1$
StringBuilder sb = new StringBuilder(name.trim());
sb.append("="); //$NON-NLS-1$
String value = newDialog.getValue();
if (value != null) {
value = value.trim();
if (value.length() == 0) {
// Check empty value
sb.append(quote);
sb.append(quote);
} else if (value.charAt(0) == '"' && value.charAt(value.length() - 1) == '"') {
// Check user has already quoted it.
sb.append(value);
} else {
sb.append(quote);
sb.append(value);
sb.append(quote);
}
}
return sb.toString();
}
return null;
}
/*
* Expect string with format: VAR1="VALUE1" VAR2="VALUE2". Count quotes
* to mark end of a variable.
*
* @see
* org.eclipse.jface.preference.ListEditor#parseString(java.lang.String)
*/
@Override
protected String[] parseString(String str) {
if (str == null) {
return new String[] {};
}
ArrayList<String> variables = new ArrayList<>();
StringBuilder sb = new StringBuilder();
int i = 0;
int quote = 0; // 0 = begin variable,
// 1 = looking for end of variable.
while (i < str.length()) {
char c = str.charAt(i);
sb.append(c);
if (c == '"') {
quote++;
}
if (quote == 2) {
// Found end of variable.
quote = 0;
variables.add(sb.toString());
sb.delete(0, sb.length());
i++; // Skip whitespace char separating variables.
}
i++;
}
return variables.toArray(new String[0]);
}
/**
* Get the list of environment variables in a single line.
*
* @return environment variables
*/
public String getVariablesValue() {
org.eclipse.swt.widgets.List list = super.getList();
StringBuilder sb = new StringBuilder();
for (String var : list.getItems()) {
sb.append(var);
sb.append(" "); //$NON-NLS-1$
}
return sb.toString().trim();
}
};
private List<FieldEditor> fieldEditors;
public AutotoolsCategoryPropertyOptionPage(ToolListElement element, IAConfiguration cfg) {
@ -120,6 +349,12 @@ public class AutotoolsCategoryPropertyOptionPage extends
addField(l);
fieldEditors.add(l);
break;
case IConfigureOption.ENVVAR:
VariableListEditor listEditor = new VariableListEditor(option.getName(), option.getDescription(), area);
listEditor.setToolTipText(option.getToolTip());
addField(listEditor);
fieldEditors.add(listEditor);
break;
}
}
}
@ -150,6 +385,9 @@ public class AutotoolsCategoryPropertyOptionPage extends
} else if (event.getSource() instanceof BooleanFieldEditor) {
BooleanFieldEditor b = (BooleanFieldEditor)event.getSource();
cfg.setOption(b.getPreferenceName(), Boolean.toString(b.getBooleanValue()));
} else if (event.getSource() instanceof VariableListEditor) {
VariableListEditor v = (VariableListEditor) event.getSource();
cfg.setOption(v.getPreferenceName(), v.getVariablesValue());
}
}

View file

@ -42,3 +42,7 @@ CleanMakeTarget.tooltip=Specify a top-level make target to clean build directory
AutoBuildName.label=Automatically generate build directory names for additional configurations
AutoBuildName.tooltip=When a configuration other than the first configuration is created, generate a unique build directory using the configuration name.
NewEnvVarDialog.title=New environment variable
NewEnvVarDialog.name_field=Name:
NewEnvVarDialog.value_field=Value: