/
CmakeTool.java
183 lines (158 loc) · 5.84 KB
/
CmakeTool.java
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
176
177
178
179
180
181
182
183
package hudson.plugins.cmake;
import static hudson.init.InitMilestone.EXTENSIONS_AUGMENTED;
import hudson.EnvVars;
import hudson.Extension;
import hudson.Util;
import hudson.init.Initializer;
import hudson.model.EnvironmentSpecific;
import hudson.model.TaskListener;
import hudson.model.Node;
import hudson.slaves.NodeSpecific;
import hudson.tools.ToolInstaller;
import hudson.tools.ToolProperty;
import hudson.tools.InstallSourceProperty;
import hudson.tools.ToolDescriptor;
import hudson.tools.ToolInstallation;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import jenkins.model.Jenkins;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;
/**
* Information about Cmake installation. A CmakeTool is used to select between
* different installations of cmake, as in "cmake2.6.4" or "cmake2.8.12".
*
* @author Martin Weber
*/
public class CmakeTool extends ToolInstallation implements
NodeSpecific<CmakeTool>, EnvironmentSpecific<CmakeTool> {
// private static final Logger LOGGER = Logger.getLogger(CmakeTool.class
// .getName());
/**
* Tool name of the default tool (usually found on the executable search
* path). Do not use: Exposed here only for testing purposes.
*/
public static transient final String DEFAULT = "InSearchPath";
private static final long serialVersionUID = 1;
/**
* Constructor for CmakeTool.
*
* @param name
* Tool name (for example, "cmake2.6.4" or "cmake2.8.12")
* @param home
* Tool location (usually "cmake")
* @param properties
* {@link java.util.List} of properties for this tool
*/
@DataBoundConstructor
public CmakeTool(String name, String home,
List<? extends ToolProperty<?>> properties) {
super(Util.fixEmptyAndTrim(name), Util.fixEmptyAndTrim(home),
properties);
}
/**
* @return {@link java.lang.String} that will be used to execute cmake (e.g.
* "cmake" or "/usr/bin/cmake")
*/
public String getCmakeExe() {
// return what was entered on the global config page
return getHome();
}
/**
* Overwritten to add the path to cmake`s bin directory, if tool was
* downloaded.
*/
@Override
public void buildEnvVars(EnvVars env) {
if (getProperties().get(InstallSourceProperty.class) != null) {
// cmake was downloaded and installed
String home = getHome(); // the home on the slave!!!
if (home != null) {
// home= dirname(home) as a cross-platform version...
int idx;
if ((idx = home.lastIndexOf('/')) != -1
|| (idx = home.lastIndexOf('\\')) != -1 && idx > 1) {
env.put("PATH+CMAKE", home.substring(0, idx));
}
}
}
}
public CmakeTool forNode(Node node, TaskListener log) throws IOException,
InterruptedException {
return new CmakeTool(getName(), translateFor(node, log),
getProperties().toList());
}
public CmakeTool forEnvironment(EnvVars environment) {
return new CmakeTool(getName(), environment.expand(getHome()),
getProperties().toList());
}
/**
* Creates a default tool installation if needed. Uses "cmake" or migrates
* data from previous versions
*
*/
@Initializer(after = EXTENSIONS_AUGMENTED)
public static void onLoaded() {
Jenkins jenkinsInstance = Jenkins.getInstance();
if (jenkinsInstance == null) {
return;
}
DescriptorImpl descriptor = (DescriptorImpl) jenkinsInstance
.getDescriptor(CmakeTool.class);
CmakeTool[] installations = getInstallations(descriptor);
if (installations != null && installations.length > 0) {
// No need to initialize if there's already something
return;
}
CmakeTool tool = new CmakeTool(DEFAULT, "cmake",
Collections.<ToolProperty<?>> emptyList());
descriptor.setInstallations(new CmakeTool[] { tool });
descriptor.save();
}
private static CmakeTool[] getInstallations(DescriptorImpl descriptor) {
CmakeTool[] installations = null;
try {
installations = descriptor.getInstallations();
} catch (NullPointerException e) {
installations = new CmakeTool[0];
}
return installations;
}
// //////////////////////////////////////////////////////////////////
// inner classes
// //////////////////////////////////////////////////////////////////
@Extension
public static class DescriptorImpl extends ToolDescriptor<CmakeTool> {
public DescriptorImpl() {
load();
}
@Override
public String getDisplayName() {
return "CMake";
}
@Override
public boolean configure(StaplerRequest req, JSONObject json)
throws FormException {
// reject empty tool names...
List<CmakeTool> cmakes = req.bindJSONToList(CmakeTool.class,
json.get("tool"));
for (CmakeTool tool : cmakes) {
if (Util.fixEmpty(tool.getName()) == null)
throw new FormException(getDisplayName()
+ " installation requires a name", "_.name");
}
super.configure(req, json);
save();
return true;
}
/**
* Overwritten to make cmake auto-installer a default option.
*/
@Override
public List<? extends ToolInstaller> getDefaultInstallers() {
return Collections.singletonList(new CmakeInstaller(null));
}
} // DescriptorImpl
}