Skip to content

Commit e819f8a

Browse files
Add UI for user to control build type (#1090)
User can control the value of the CMake built type (CMAKE_BUILD_TYPE), for example Debug or Release via new UI in CMakeBuildTab Added UT for ICMakeProperties.getBuildType() Addresses Issue: CDT CMake Improvements #1000, IDE-82683-REQ-013
1 parent 47b1978 commit e819f8a

File tree

6 files changed

+177
-8
lines changed

6 files changed

+177
-8
lines changed

cmake/org.eclipse.cdt.cmake.core.tests/src/org/eclipse/cdt/cmake/core/CMakeBuildConfigurationTests.java

+101
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.eclipse.core.resources.IBuildConfiguration;
3636
import org.eclipse.core.resources.IProject;
3737
import org.eclipse.core.resources.IProjectDescription;
38+
import org.eclipse.debug.core.ILaunchManager;
3839
import org.eclipse.launchbar.core.target.ILaunchTarget;
3940
import org.eclipse.launchbar.core.target.ILaunchTargetManager;
4041
import org.junit.jupiter.api.BeforeEach;
@@ -91,6 +92,106 @@ public ICMakeProperties getCMakeProperties() {
9192
assertThat(cMakeProperties.getGenerator(), is(CMakeGenerator.WatcomWMake));
9293
}
9394

95+
/**
96+
* Test for IDE_82683_REQ_013 part of #1000
97+
* <br>
98+
* Testing {@link ICMakeProperties#getBuildType()} <br>
99+
* <br>
100+
* This test verify default build type is used in case:
101+
* {@link ICMakeBuildConfiguration#CMAKE_USE_DEFAULT_CMAKE_SETTINGS} is <code>true<code>
102+
*/
103+
@Test
104+
public void getCMakePropertiesTestGetDefaultBuildType() {
105+
// CMAKE_USE_DEFAULT_CMAKE_SETTINGS = "true"
106+
CMakeBuildConfiguration cmBuildConfig;
107+
ICMakeProperties cMakeProperties;
108+
// Test for ILaunchManager.RUN_MODE
109+
cmBuildConfig = new CMakeBuildConfiguration(buildConfig, "cmBuildConfigName", mockToolchain, null,
110+
ILaunchManager.RUN_MODE, LOCAL_LAUNCH_TARGET);
111+
cMakeProperties = cmBuildConfig.getCMakeProperties();
112+
assertThat(cMakeProperties.getBuildType(), is("Release"));
113+
114+
// Test for ILaunchManager.DEBUG_MODE
115+
cmBuildConfig = new CMakeBuildConfiguration(buildConfig, "cmBuildConfigName", mockToolchain, null,
116+
ILaunchManager.DEBUG_MODE, LOCAL_LAUNCH_TARGET);
117+
cMakeProperties = cmBuildConfig.getCMakeProperties();
118+
assertThat(cMakeProperties.getBuildType(), is("Debug"));
119+
120+
// Test for ILaunchManager.PROFILE_MODE
121+
cmBuildConfig = new CMakeBuildConfiguration(buildConfig, "cmBuildConfigName", mockToolchain, null,
122+
ILaunchManager.PROFILE_MODE, LOCAL_LAUNCH_TARGET);
123+
cMakeProperties = cmBuildConfig.getCMakeProperties();
124+
assertThat(cMakeProperties.getBuildType(), is("Release"));
125+
}
126+
127+
/**
128+
* Test for IDE_82683_REQ_013 part of #1000
129+
* <br>
130+
* This test verify default build type is used in case:
131+
* {@link ICMakeBuildConfiguration#CMAKE_USE_DEFAULT_CMAKE_SETTINGS} is <code>true<code>
132+
*/
133+
@Test
134+
public void getCMakePropertiesLoadISVSelectBuildType_UseDefaultBuildType_1() {
135+
ICMakeProperties cMakeProperties;
136+
CMakeBuildConfiguration cmBuildConfig = new CMakeBuildConfiguration(buildConfig, "cmBuildConfigName",
137+
mockToolchain, null, ILaunchManager.RUN_MODE, LOCAL_LAUNCH_TARGET);
138+
// Setup ISV properties for CMakeBuildConfiguration
139+
// CMAKE_USE_DEFAULT_CMAKE_SETTINGS = "true"
140+
// CMAKE_BUILD_TYPE = "RelWithDebInfo"
141+
cmBuildConfig.removeProperty(CMakeBuildConfiguration.CMAKE_BUILD_TYPE);
142+
cmBuildConfig.setProperty(CMakeBuildConfiguration.CMAKE_USE_DEFAULT_CMAKE_SETTINGS, "true");
143+
cmBuildConfig.setProperty(CMakeBuildConfiguration.CMAKE_BUILD_TYPE, "RelWithDebInfo");
144+
// Expected: default build type is used (in this case: "Release" for ILaunchManager.RUN_MODE)
145+
cMakeProperties = cmBuildConfig.getCMakeProperties();
146+
assertThat(cMakeProperties.getBuildType(), is("Release"));
147+
}
148+
149+
/**
150+
* Test for IDE_82683_REQ_013 part of #1000
151+
* <br>
152+
* This test verify default build type is used in case ISV build type is blank:
153+
* {@link ICMakeBuildConfiguration#CMAKE_USE_DEFAULT_CMAKE_SETTINGS} is <code>false<code> and
154+
* {@link ICMakeBuildConfiguration#CMAKE_BUILD_TYPE} is blank
155+
*/
156+
@Test
157+
public void getCMakePropertiesLoadISVSelectBuildType_ISVBuildTypeIsBlank() {
158+
ICMakeProperties cMakeProperties;
159+
CMakeBuildConfiguration cmBuildConfig = new CMakeBuildConfiguration(buildConfig, "cmBuildConfigName",
160+
mockToolchain, null, ILaunchManager.RUN_MODE, LOCAL_LAUNCH_TARGET);
161+
// Setup ISV properties for CMakeBuildConfiguration
162+
// CMAKE_USE_DEFAULT_CMAKE_SETTINGS = "false"
163+
// CMAKE_BUILD_TYPE = ""
164+
cmBuildConfig.removeProperty(CMakeBuildConfiguration.CMAKE_BUILD_TYPE);
165+
cmBuildConfig.setProperty(CMakeBuildConfiguration.CMAKE_USE_DEFAULT_CMAKE_SETTINGS, "false");
166+
cmBuildConfig.setProperty(CMakeBuildConfiguration.CMAKE_BUILD_TYPE, "");
167+
// Expected: "Release" build type is used (in this case: "Release" for ILaunchManager.RUN_MODE)
168+
cMakeProperties = cmBuildConfig.getCMakeProperties();
169+
assertThat(cMakeProperties.getBuildType(), is("Release"));
170+
}
171+
172+
/**
173+
* Test for IDE_82683_REQ_013 part of #1000
174+
* <br>
175+
* This test verify ISV's selected build type is used in case:
176+
* {@link ICMakeBuildConfiguration#CMAKE_USE_DEFAULT_CMAKE_SETTINGS} is <code>false<code> and
177+
* {@link ICMakeBuildConfiguration#CMAKE_BUILD_TYPE} is NOT blank
178+
*/
179+
@Test
180+
public void getCMakePropertiesLoadISVSelectBuildType_UseISVBuildTypeNotBlank() {
181+
ICMakeProperties cMakeProperties;
182+
CMakeBuildConfiguration cmBuildConfig = new CMakeBuildConfiguration(buildConfig, "cmBuildConfigName",
183+
mockToolchain, null, ILaunchManager.RUN_MODE, LOCAL_LAUNCH_TARGET);
184+
// Setup ISV properties for CMakeBuildConfiguration
185+
// CMAKE_USE_DEFAULT_CMAKE_SETTINGS = "false"
186+
// CMAKE_BUILD_TYPE = "RelWithDebInfo"
187+
cmBuildConfig.removeProperty(CMakeBuildConfiguration.CMAKE_BUILD_TYPE);
188+
cmBuildConfig.setProperty(CMakeBuildConfiguration.CMAKE_USE_DEFAULT_CMAKE_SETTINGS, "false");
189+
cmBuildConfig.setProperty(CMakeBuildConfiguration.CMAKE_BUILD_TYPE, "RelWithDebInfo");
190+
// Expected: "RelWithDebInfo" build type is used
191+
cMakeProperties = cmBuildConfig.getCMakeProperties();
192+
assertThat(cMakeProperties.getBuildType(), is("RelWithDebInfo"));
193+
}
194+
94195
/**
95196
* Test for {@link ICMakeProperties#setExtraArguments()}
96197
*

cmake/org.eclipse.cdt.cmake.core/src/org/eclipse/cdt/cmake/core/CMakeBuildConfiguration.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@ public ICMakeProperties getCMakeProperties() {
139139
cmakeProperties.setAllTarget(allTarget);
140140
}
141141

142+
String buildType = properties.get(CMAKE_BUILD_TYPE);
143+
if (buildType == null || buildType.isBlank()) {
144+
buildType = getDefaultProperties().get(CMAKE_BUILD_TYPE);
145+
}
146+
cmakeProperties.setBuildType(buildType);
147+
142148
return cmakeProperties;
143149
}
144150

@@ -178,13 +184,6 @@ public IProject[] build(int kind, Map<String, String> args, IConsole console, IP
178184

179185
runCMake |= !Files.exists(buildDir.resolve("CMakeCache.txt")); //$NON-NLS-1$
180186

181-
// Causes CMAKE_BUILD_TYPE to be set according to the launch mode
182-
if (ILaunchManager.DEBUG_MODE.equals(getLaunchMode())) {
183-
cmakeProperties.setBuildType("Debug"); //$NON-NLS-1$
184-
} else {
185-
cmakeProperties.setBuildType("Release"); //$NON-NLS-1$
186-
}
187-
188187
if (!runCMake) {
189188
ICMakeGenerator generator = cmakeProperties.getGenerator();
190189
String makefileName = generator.getMakefileName();
@@ -571,7 +570,8 @@ public Map<String, String> getDefaultProperties() {
571570
CMAKE_ARGUMENTS, CMAKE_ARGUMENTS_DEFAULT, //
572571
CMAKE_BUILD_COMMAND, CMAKE_BUILD_COMMAND_DEFAULT, //
573572
CMAKE_ALL_TARGET, CMAKE_ALL_TARGET_DEFAULT, //
574-
CMAKE_CLEAN_TARGET, CMAKE_CLEAN_TARGET_DEFAULT //
573+
CMAKE_CLEAN_TARGET, CMAKE_CLEAN_TARGET_DEFAULT, //
574+
CMAKE_BUILD_TYPE, ILaunchManager.DEBUG_MODE.equals(getLaunchMode()) ? "Debug" : "Release" //$NON-NLS-1$ //$NON-NLS-2$
575575
);
576576
}
577577

cmake/org.eclipse.cdt.cmake.core/src/org/eclipse/cdt/cmake/core/ICMakeBuildConfiguration.java

+6
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ public interface ICMakeBuildConfiguration {
7070
public static final String CMAKE_CLEAN_TARGET = "cmake.target.clean"; //$NON-NLS-1$
7171
public static final String CMAKE_CLEAN_TARGET_DEFAULT = "clean"; //$NON-NLS-1$
7272

73+
/**
74+
* Name of the build type set by user. Default of this value will be set based on Launch Mode
75+
* (i.e. Debug for Debug launch mode, Release for Run and other launch modes)
76+
*/
77+
public static final String CMAKE_BUILD_TYPE = "cmake.build.type"; //$NON-NLS-1$
78+
7379
/**
7480
* Converts the {@link ICBuildConfiguration}'s properties, using the keys defined above
7581
* into an {@link ICMakeProperties} that configures the CMake build processes.

cmake/org.eclipse.cdt.cmake.ui/src/org/eclipse/cdt/cmake/ui/internal/CMakeBuildTab.java

+54
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*******************************************************************************/
1111
package org.eclipse.cdt.cmake.ui.internal;
1212

13+
import java.text.MessageFormat;
1314
import java.util.Arrays;
1415
import java.util.Map;
1516

@@ -18,8 +19,11 @@
1819
import org.eclipse.cdt.cmake.core.properties.CMakeGenerator;
1920
import org.eclipse.cdt.core.build.ICBuildConfiguration;
2021
import org.eclipse.cdt.launch.ui.corebuild.CommonBuildTab;
22+
import org.eclipse.debug.core.DebugPlugin;
2123
import org.eclipse.debug.core.ILaunchConfiguration;
2224
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
25+
import org.eclipse.debug.core.ILaunchManager;
26+
import org.eclipse.debug.core.ILaunchMode;
2327
import org.eclipse.swt.SWT;
2428
import org.eclipse.swt.events.SelectionAdapter;
2529
import org.eclipse.swt.events.SelectionEvent;
@@ -35,6 +39,8 @@
3539

3640
public class CMakeBuildTab extends CommonBuildTab {
3741

42+
private static final String[] buildTypes = { "Debug", "Release", "RelWithDebInfo", "MinSizeRel" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
43+
3844
private Button useDefaultCmakeSettings;
3945
private Combo generatorCombo;
4046
private Text cmakeArgsText;
@@ -46,6 +52,9 @@ public class CMakeBuildTab extends CommonBuildTab {
4652
private Label buildCommandLabel;
4753
private Label allTargetLabel;
4854
private Label cleanTargetLabel;
55+
private Combo buildTypeCombo;
56+
private Label buildTypeLabel;
57+
private Label usedForLaunchModeLabel;
4958

5059
@Override
5160
protected String getBuildConfigProviderId() {
@@ -121,6 +130,25 @@ public void widgetSelected(SelectionEvent e) {
121130
cleanTargetText = new Text(cmakeGroup, SWT.BORDER);
122131
cleanTargetText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
123132
cleanTargetText.addModifyListener(e -> updateLaunchConfigurationDialog());
133+
134+
Composite buildTypeComp = new Composite(cmakeGroup, SWT.NONE);
135+
buildTypeComp.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
136+
buildTypeComp.setLayout(new GridLayout(3, false));
137+
138+
buildTypeLabel = new Label(buildTypeComp, SWT.NONE);
139+
buildTypeLabel.setText(Messages.CMakeBuildTab_BuildType);
140+
buildTypeLabel.setToolTipText(Messages.CMakeBuildTab_BuildType_Tooltip);
141+
142+
buildTypeCombo = new Combo(buildTypeComp, SWT.DROP_DOWN);
143+
buildTypeCombo.setItems(buildTypes);
144+
buildTypeCombo.setToolTipText(Messages.CMakeBuildTab_BuildTypeCombo_Tooltip);
145+
buildTypeCombo.addSelectionListener(new SelectionAdapter() {
146+
@Override
147+
public void widgetSelected(SelectionEvent e) {
148+
updateLaunchConfigurationDialog();
149+
}
150+
});
151+
usedForLaunchModeLabel = new Label(buildTypeComp, SWT.NONE);
124152
}
125153

126154
/**
@@ -148,6 +176,9 @@ private void updateEnablement() {
148176
allTargetText.setEnabled(enabled);
149177
cleanTargetLabel.setEnabled(enabled);
150178
cleanTargetText.setEnabled(enabled);
179+
buildTypeLabel.setEnabled(enabled);
180+
buildTypeCombo.setEnabled(enabled);
181+
usedForLaunchModeLabel.setEnabled(enabled);
151182
}
152183

153184
@Override
@@ -217,6 +248,13 @@ public void performApply(ILaunchConfigurationWorkingCopy configuration) {
217248
boolean isDefaultCMakeProperties = useDefaultCmakeSettings.getSelection();
218249
buildConfig.setProperty(CMakeBuildConfiguration.CMAKE_USE_DEFAULT_CMAKE_SETTINGS,
219250
Boolean.toString(isDefaultCMakeProperties));
251+
252+
String buildType = buildTypeCombo.getText().trim();
253+
if (!(buildType.isBlank() || isDefaultCMakeProperties)) {
254+
buildConfig.setProperty(CMakeBuildConfiguration.CMAKE_BUILD_TYPE, buildType);
255+
} else {
256+
buildConfig.removeProperty(CMakeBuildConfiguration.CMAKE_BUILD_TYPE);
257+
}
220258
}
221259

222260
@Override
@@ -227,6 +265,7 @@ protected void saveProperties(Map<String, String> properties) {
227265
properties.put(CMakeBuildConfiguration.CMAKE_BUILD_COMMAND, buildCommandText.getText().trim());
228266
properties.put(CMakeBuildConfiguration.CMAKE_ALL_TARGET, allTargetText.getText().trim());
229267
properties.put(CMakeBuildConfiguration.CMAKE_CLEAN_TARGET, cleanTargetText.getText().trim());
268+
properties.put(CMakeBuildConfiguration.CMAKE_BUILD_TYPE, buildTypeCombo.getText().trim());
230269
}
231270

232271
@Override
@@ -252,11 +291,26 @@ protected void restoreProperties(Map<String, String> properties) {
252291
String cleanTarget = properties.getOrDefault(CMakeBuildConfiguration.CMAKE_CLEAN_TARGET,
253292
CMakeBuildConfiguration.CMAKE_CLEAN_TARGET_DEFAULT);
254293
cleanTargetText.setText(cleanTarget);
294+
295+
// Default build type: Debug for Debug launch mode, Release for Run and other launch modes.
296+
String defaultBuildType = getDefaultBuildType();
297+
String buildType = properties.getOrDefault(CMakeBuildConfiguration.CMAKE_BUILD_TYPE, defaultBuildType);
298+
buildTypeCombo.setText(buildType);
299+
300+
ILaunchMode launchMode = DebugPlugin.getDefault().getLaunchManager()
301+
.getLaunchMode(getBuildConfiguration().getLaunchMode());
302+
String launchModeLabel = launchMode != null ? launchMode.getLabel() : getBuildConfiguration().getLaunchMode();
303+
usedForLaunchModeLabel.setText(MessageFormat.format(Messages.CMakeBuildTab_UsedForLaunchMode, launchModeLabel));
255304
}
256305

257306
@Override
258307
public String getName() {
259308
return Messages.CMakeBuildTab_Cmake;
260309
}
261310

311+
private String getDefaultBuildType() {
312+
return ILaunchManager.DEBUG_MODE.equals(getBuildConfiguration().getLaunchMode()) ? buildTypes[0]
313+
: buildTypes[1];
314+
}
315+
262316
}

cmake/org.eclipse.cdt.cmake.ui/src/org/eclipse/cdt/cmake/ui/internal/Messages.java

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
public class Messages extends NLS {
1616

1717
public static String CMakeBuildTab_BuildCommand;
18+
public static String CMakeBuildTab_BuildType;
19+
public static String CMakeBuildTab_BuildType_Tooltip;
20+
public static String CMakeBuildTab_BuildTypeCombo_Tooltip;
1821
public static String CMakeBuildTab_AllTarget;
1922
public static String CMakeBuildTab_CleanTarget;
2023
public static String CMakeBuildTab_Cmake;
@@ -25,6 +28,7 @@ public class Messages extends NLS {
2528
public static String CMakeBuildTab_Toolchain;
2629
public static String CMakeBuildTab_useDefaultCmakeSettings;
2730
public static String CMakeBuildTab_useDefaultCmakeSettingsTip;
31+
public static String CMakeBuildTab_UsedForLaunchMode;
2832
public static String CMakePreferencePage_Add;
2933
public static String CMakePreferencePage_ConfirmRemoveDesc;
3034
public static String CMakePreferencePage_ConfirmRemoveTitle;

cmake/org.eclipse.cdt.cmake.ui/src/org/eclipse/cdt/cmake/ui/internal/messages.properties

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
CMakeBuildTab_BuildCommand=Build command
2+
CMakeBuildTab_BuildType=Build type:
3+
CMakeBuildTab_BuildType_Tooltip=The build type (CMAKE_BUILD_TYPE) used by the generator.
4+
CMakeBuildTab_BuildTypeCombo_Tooltip=Typical build types (CMAKE_BUILD_TYPE) are listed. A custom build type can be entered.
25
CMakeBuildTab_AllTarget=Build all target
36
CMakeBuildTab_CleanTarget=Clean target
47
CMakeBuildTab_Cmake=CMake
@@ -9,6 +12,7 @@ CMakeBuildTab_Settings=CMake Settings
912
CMakeBuildTab_Toolchain=Toolchain
1013
CMakeBuildTab_useDefaultCmakeSettings=Use default CMake settings
1114
CMakeBuildTab_useDefaultCmakeSettingsTip=Use the default CMake settings that are provided by the toolchain and Core Build System
15+
CMakeBuildTab_UsedForLaunchMode=used for launch mode: {0}
1216
CMakePreferencePage_Add=Add...
1317
CMakePreferencePage_ConfirmRemoveDesc=Do you wish to deregister the selected files?
1418
CMakePreferencePage_ConfirmRemoveTitle=Deregister CMake ToolChain File

0 commit comments

Comments
 (0)