-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
InstallExecutable.java
269 lines (242 loc) · 9.01 KB
/
InstallExecutable.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.nativeplatform.tasks;
import org.gradle.api.DefaultTask;
import org.gradle.api.file.ConfigurableFileCollection;
import org.gradle.api.file.Directory;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.file.FileCollection;
import org.gradle.api.file.FileSystemOperations;
import org.gradle.api.file.RegularFile;
import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.provider.Property;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.InputFile;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.Nested;
import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.OutputDirectory;
import org.gradle.api.tasks.OutputFile;
import org.gradle.api.tasks.PathSensitive;
import org.gradle.api.tasks.PathSensitivity;
import org.gradle.api.tasks.SkipWhenEmpty;
import org.gradle.api.tasks.TaskAction;
import org.gradle.internal.nativeintegration.filesystem.FileSystem;
import org.gradle.internal.os.OperatingSystem;
import org.gradle.internal.work.WorkerLeaseService;
import org.gradle.nativeplatform.platform.NativePlatform;
import org.gradle.nativeplatform.toolchain.Gcc;
import org.gradle.nativeplatform.toolchain.NativeToolChain;
import org.gradle.util.internal.GFileUtils;
import org.gradle.work.DisableCachingByDefault;
import javax.annotation.Nullable;
import javax.inject.Inject;
import java.io.File;
import java.util.Collection;
/**
* Installs an executable with it's dependent libraries so it can be easily executed.
*/
@DisableCachingByDefault(because = "Not worth caching")
public abstract class InstallExecutable extends DefaultTask {
private final Property<NativePlatform> targetPlatform;
private final Property<NativeToolChain> toolChain;
private final DirectoryProperty installDirectory;
private final RegularFileProperty executable;
private final RegularFileProperty installedExecutable;
private final ConfigurableFileCollection libs;
private final WorkerLeaseService workerLeaseService;
/**
* Injects a {@link WorkerLeaseService} instance.
*
* @since 4.2
*/
@Inject
public InstallExecutable(WorkerLeaseService workerLeaseService) {
ObjectFactory objectFactory = getProject().getObjects();
this.workerLeaseService = workerLeaseService;
this.libs = getProject().files();
this.installDirectory = objectFactory.directoryProperty();
this.installedExecutable = objectFactory.fileProperty();
this.executable = objectFactory.fileProperty();
this.installedExecutable.set(getLibDirectory().map(directory -> directory.file(executable.getAsFile().get().getName())));
// A further work around for missing ability to skip task when input file is missing (see #getInputFileIfExists below)
getInputs().file(executable);
this.targetPlatform = objectFactory.property(NativePlatform.class);
this.toolChain = objectFactory.property(NativeToolChain.class);
}
/**
* The tool chain used for linking.
*
* @since 4.7
*/
@Internal
public Property<NativeToolChain> getToolChain() {
return toolChain;
}
/**
* The platform being linked for.
*
* @since 4.7
*/
@Nested
public Property<NativePlatform> getTargetPlatform() {
return targetPlatform;
}
/**
* The directory to install files into.
*
* @since 4.1
*/
@OutputDirectory
public DirectoryProperty getInstallDirectory() {
return installDirectory;
}
/**
* The executable file to install.
*
* @since 4.7
*
*/
@Internal("Covered by inputFileIfExists")
public RegularFileProperty getExecutableFile() {
return executable;
}
/**
* The location of the installed executable file.
*
* @since 4.7
*/
@OutputFile
public RegularFileProperty getInstalledExecutable() {
return installedExecutable;
}
/**
* Workaround for when the task is given an input file that doesn't exist
*
* @since 4.3
*/
// TODO - allow @InputFile and @SkipWhenEmpty to be attached to getExecutableFile()
@SkipWhenEmpty
@Nullable
@Optional
@PathSensitive(PathSensitivity.NAME_ONLY)
@InputFile
protected File getInputFileIfExists() {
RegularFileProperty sourceFile = getExecutableFile();
if (sourceFile.isPresent() && sourceFile.get().getAsFile().exists()) {
return sourceFile.get().getAsFile();
} else {
return null;
}
}
/**
* The library files that should be installed.
*/
@PathSensitive(PathSensitivity.RELATIVE)
@InputFiles
public FileCollection getLibs() {
return libs;
}
public void setLibs(FileCollection libs) {
this.libs.setFrom(libs);
}
/**
* Adds a set of library files to be installed. The provided libs object is evaluated as per {@link org.gradle.api.Project#files(Object...)}.
*/
public void lib(Object libs) {
this.libs.from(libs);
}
/**
* Returns the script file that can be used to run the install image.
*
* @since 4.4
*/
@Internal("covered by getInstallDirectory")
public Provider<RegularFile> getRunScriptFile() {
return installDirectory.file(executable.getLocationOnly().map(executableFile -> OperatingSystem.forName(targetPlatform.get().getOperatingSystem().getName()).getScriptName(executableFile.getAsFile().getName())));
}
@Inject
protected FileSystem getFileSystem() {
throw new UnsupportedOperationException();
}
@Inject
protected FileSystemOperations getFileSystemOperations() {
throw new UnsupportedOperationException();
}
@TaskAction
protected void install() {
NativePlatform nativePlatform = targetPlatform.get();
File executable = getExecutableFile().get().getAsFile();
File libDirectory = getLibDirectory().get().getAsFile();
File runScript = getRunScriptFile().get().getAsFile();
Collection<File> libs = getLibs().getFiles();
// TODO: Migrate this to the worker API once the FileSystem and FileOperations services can be injected
workerLeaseService.runAsIsolatedTask(() -> {
installToDir(libDirectory, executable, libs);
if (nativePlatform.getOperatingSystem().isWindows()) {
installWindows(executable, runScript);
} else {
installUnix(executable, runScript);
}
});
}
private Provider<Directory> getLibDirectory() {
return getInstallDirectory().getLocationOnly().map(dir -> dir.dir("lib"));
}
private void installWindows(File executable, File runScript) {
StringBuilder toolChainPath = new StringBuilder();
NativeToolChain toolChain = getToolChain().get();
if (toolChain instanceof Gcc) {
// Gcc on windows requires the path to be set
toolChainPath.append("SET PATH=");
for (File pathEntry : ((Gcc) toolChain).getPath()) {
toolChainPath.append(pathEntry.getAbsolutePath()).append(";");
}
toolChainPath.append("%PATH%");
}
String runScriptText =
"\n@echo off"
+ "\nSETLOCAL"
+ "\n" + toolChainPath
+ "\nCALL \"%~dp0lib\\" + executable.getName() + "\" %*"
+ "\nEXIT /B %ERRORLEVEL%"
+ "\nENDLOCAL"
+ "\n";
GFileUtils.writeFile(runScriptText, runScript);
}
private void installUnix(File executable, File runScript) {
String runScriptText =
"#!/bin/sh"
+ "\nAPP_BASE_NAME=`dirname \"$0\"`"
+ "\nDYLD_LIBRARY_PATH=\"$APP_BASE_NAME/lib\""
+ "\nexport DYLD_LIBRARY_PATH"
+ "\nLD_LIBRARY_PATH=\"$APP_BASE_NAME/lib\""
+ "\nexport LD_LIBRARY_PATH"
+ "\nexec \"$APP_BASE_NAME/lib/" + executable.getName() + "\" \"$@\""
+ "\n";
GFileUtils.writeFile(runScriptText, runScript);
getFileSystem().chmod(runScript, 0755);
}
private void installToDir(final File binaryDir, final File executableFile, final Collection<File> libs) {
getFileSystemOperations().sync(copySpec -> {
copySpec.into(binaryDir);
copySpec.from(executableFile);
copySpec.from(libs);
});
}
}