Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6cc9a02e012e09e275973af335936060f5782e85 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*******************************************************************************
 * Copyright (c) 2017 QNX Software Systems and others.
 * 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
 *******************************************************************************/
package org.eclipse.cdt.cmake.ui.internal;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.cdt.cmake.core.internal.CMakeBuildConfiguration;
import org.eclipse.cdt.debug.core.launch.CoreBuildLaunchConfigDelegate;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.ui.AbstractLaunchConfigurationTab;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
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.Label;
import org.eclipse.swt.widgets.Text;

public class CMakeBuildTab extends AbstractLaunchConfigurationTab {

	private Button unixGenButton;
	private Button ninjaGenButton;
	private Text cmakeArgsText;
	private Text buildCommandText;
	private Text cleanCommandText;

	@Override
	public void createControl(Composite parent) {
		Composite comp = new Composite(parent, SWT.NONE);
		comp.setLayout(new GridLayout());
		setControl(comp);

		Label label = new Label(comp, SWT.NONE);
		label.setText(Messages.CMakeBuildTab_Generator);

		Composite genComp = new Composite(comp, SWT.BORDER);
		genComp.setLayout(new GridLayout(2, true));

		unixGenButton = new Button(genComp, SWT.RADIO);
		unixGenButton.setText(Messages.CMakeBuildTab_UnixMakefiles);
		unixGenButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				updateLaunchConfigurationDialog();
			}
		});

		ninjaGenButton = new Button(genComp, SWT.RADIO);
		ninjaGenButton.setText(Messages.CMakeBuildTab_Ninja);
		ninjaGenButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				updateLaunchConfigurationDialog();
			}
		});

		label = new Label(comp, SWT.NONE);
		label.setText(Messages.CMakeBuildTab_CMakeArgs);

		cmakeArgsText = new Text(comp, SWT.BORDER);
		cmakeArgsText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		cmakeArgsText.addModifyListener(e -> updateLaunchConfigurationDialog());

		label = new Label(comp, SWT.NONE);
		label.setText(Messages.CMakeBuildTab_BuildCommand);

		buildCommandText = new Text(comp, SWT.BORDER);
		buildCommandText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		buildCommandText.addModifyListener(e -> updateLaunchConfigurationDialog());

		label = new Label(comp, SWT.NONE);
		label.setText(Messages.CMakeBuildTab_CleanCommand);

		cleanCommandText = new Text(comp, SWT.BORDER);
		cleanCommandText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		cleanCommandText.addModifyListener(e -> updateLaunchConfigurationDialog());
	}

	@Override
	public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
		String mode = getLaunchConfigurationDialog().getMode();
		configuration.removeAttribute("COREBUILD_" + mode); //$NON-NLS-1$
	}

	@Override
	public void initializeFrom(ILaunchConfiguration configuration) {
		try {
			String mode = getLaunchConfigurationDialog().getMode();
			// TODO find a home for the attribute name
			Map<String, String> properties = configuration.getAttribute("COREBUILD_" + mode, //$NON-NLS-1$
					new HashMap<>());

			String generator = properties.get(CMakeBuildConfiguration.CMAKE_GENERATOR);
			updateGeneratorButtons(generator);

			String cmakeArgs = properties.get(CMakeBuildConfiguration.CMAKE_ARGUMENTS);
			if (cmakeArgs != null) {
				cmakeArgsText.setText(cmakeArgs);
			} else {
				cmakeArgsText.setText(""); //$NON-NLS-1$
			}

			String buildCommand = properties.get(CMakeBuildConfiguration.BUILD_COMMAND);
			if (buildCommand != null) {
				buildCommandText.setText(buildCommand);
			} else {
				buildCommandText.setText(""); //$NON-NLS-1$
			}

			String cleanCommand = properties.get(CMakeBuildConfiguration.CLEAN_COMMAND);
			if (cleanCommand != null) {
				cleanCommandText.setText(buildCommand);
			} else {
				cleanCommandText.setText(""); //$NON-NLS-1$
			}
		} catch (CoreException e) {
			Activator.log(e);
		}
	}

	private void updateGeneratorButtons(String generator) {
		if (generator == null || generator.equals("Ninja")) { //$NON-NLS-1$
			ninjaGenButton.setSelection(true);
		} else {
			unixGenButton.setSelection(true);
		}
	}

	@Override
	public void performApply(ILaunchConfigurationWorkingCopy configuration) {
		Map<String, String> properties = new HashMap<>();

		properties.put(CMakeBuildConfiguration.CMAKE_GENERATOR,
				ninjaGenButton.getSelection() ? "Ninja" : "Unix Makefiles"); //$NON-NLS-1$ //$NON-NLS-2$

		String cmakeArgs = cmakeArgsText.getText().trim();
		if (!cmakeArgs.isEmpty()) {
			properties.put(CMakeBuildConfiguration.CMAKE_ARGUMENTS, cmakeArgs);
		}

		String buildCommand = buildCommandText.getText().trim();
		if (!buildCommand.isEmpty()) {
			properties.put(CMakeBuildConfiguration.BUILD_COMMAND, buildCommand);
		}

		String cleanCommand = cleanCommandText.getText().trim();
		if (!cleanCommand.isEmpty()) {
			properties.put(CMakeBuildConfiguration.CLEAN_COMMAND, cleanCommand);
		}

		String buildAttribute = CoreBuildLaunchConfigDelegate
				.getBuildAttributeName(getLaunchConfigurationDialog().getMode());
		if (!properties.isEmpty()) {
			configuration.setAttribute(buildAttribute, properties);
		} else {
			configuration.removeAttribute(buildAttribute);
		}
	}

	@Override
	public String getName() {
		return Messages.CMakeBuildTab_Cmake;
	}

}

Back to the top