| 1 |
/* |
| 2 |
* Copyright (C) 2015 The Android Open Source Project |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package com.android.build.gradle.model; |
| 18 |
|
| 19 |
import static com.android.build.gradle.model.ModelConstants.EXTERNAL_BUILD_CONFIG; |
| 20 |
import static com.android.build.gradle.model.ModelConstants.NATIVE_BUILD_SYSTEMS; |
| 21 |
import static com.google.common.base.Preconditions.checkNotNull; |
| 22 |
|
| 23 |
import com.android.annotations.NonNull; |
| 24 |
import com.android.annotations.Nullable; |
| 25 |
import com.android.build.gradle.internal.model.NativeAndroidProjectImpl; |
| 26 |
import com.android.build.gradle.internal.model.NativeArtifactImpl; |
| 27 |
import com.android.build.gradle.internal.model.NativeFileImpl; |
| 28 |
import com.android.build.gradle.internal.model.NativeFolderImpl; |
| 29 |
import com.android.build.gradle.internal.model.NativeSettingsImpl; |
| 30 |
import com.android.build.gradle.internal.model.NativeToolchainImpl; |
| 31 |
import com.android.build.gradle.managed.NativeBuildConfig; |
| 32 |
import com.android.build.gradle.managed.NativeLibrary; |
| 33 |
import com.android.build.gradle.ndk.internal.NativeCompilerArgsUtil; |
| 34 |
import com.android.builder.Version; |
| 35 |
import com.android.builder.model.NativeAndroidProject; |
| 36 |
import com.android.builder.model.NativeArtifact; |
| 37 |
import com.android.builder.model.NativeFile; |
| 38 |
import com.android.builder.model.NativeFolder; |
| 39 |
import com.android.builder.model.NativeSettings; |
| 40 |
import com.android.builder.model.NativeToolchain; |
| 41 |
import com.android.utils.StringHelper; |
| 42 |
import com.google.common.base.Strings; |
| 43 |
import com.google.common.collect.ImmutableList; |
| 44 |
import com.google.common.collect.ImmutableMap; |
| 45 |
import com.google.common.collect.Lists; |
| 46 |
import com.google.common.collect.Maps; |
| 47 |
|
| 48 |
import org.gradle.api.Project; |
| 49 |
import org.gradle.model.internal.core.ModelPath; |
| 50 |
import org.gradle.model.internal.registry.ModelRegistry; |
| 51 |
import org.gradle.model.internal.type.ModelType; |
| 52 |
import org.gradle.model.internal.type.ModelTypes; |
| 53 |
import org.gradle.tooling.provider.model.ToolingModelBuilder; |
| 54 |
|
| 55 |
import java.util.Collection; |
| 56 |
import java.util.List; |
| 57 |
import java.util.Map; |
| 58 |
import java.util.stream.Collectors; |
| 59 |
|
| 60 |
/** |
| 61 |
* Builder for the {@link NativeAndroidProject} model. |
| 62 |
*/ |
| 63 |
public class NativeComponentModelBuilder implements ToolingModelBuilder { |
| 64 |
|
| 65 |
@NonNull |
| 66 |
private final ModelRegistry registry; |
| 67 |
@NonNull |
| 68 |
private final Map<List<String>, NativeSettings> settingsMap = Maps.newHashMap(); |
| 69 |
private int settingIndex = 0; |
| 70 |
private NativeBuildConfig config; |
| 71 |
private List<String> buildSystems; |
| 72 |
|
| 73 |
public NativeComponentModelBuilder(@NonNull ModelRegistry registry) { |
| 74 |
this.registry = registry; |
| 75 |
} |
| 76 |
|
| 77 |
@Override |
| 78 |
public boolean canBuild(String modelName) { |
| 79 |
return modelName.equals(NativeAndroidProject.class.getName()); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Initialize private members. |
| 84 |
*/ |
| 85 |
private void initialize() { |
| 86 |
config = registry.realize( |
| 87 |
new ModelPath(EXTERNAL_BUILD_CONFIG), |
| 88 |
ModelType.of(NativeBuildConfig.class)); |
| 89 |
buildSystems = registry.realize( |
| 90 |
new ModelPath(NATIVE_BUILD_SYSTEMS), |
| 91 |
ModelTypes.list(ModelType.of(String.class))); |
| 92 |
settingIndex = 0; |
| 93 |
} |
| 94 |
|
| 95 |
@Override |
| 96 |
public Object buildAll(String modelName, Project project) { |
| 97 |
initialize(); |
| 98 |
|
| 99 |
List<NativeArtifact> artifacts = createNativeArtifacts(); |
| 100 |
List<NativeToolchain> toolchains = createNativeToolchains(); |
| 101 |
Collection<NativeSettings> settings = ImmutableList.copyOf(settingsMap.values()); |
| 102 |
Map<String, String> extensions = createFileExtensionMap(); |
| 103 |
return new NativeAndroidProjectImpl( |
| 104 |
Version.ANDROID_GRADLE_PLUGIN_VERSION, |
| 105 |
project.getName(), |
| 106 |
ImmutableList.copyOf(config.getBuildFiles()), |
| 107 |
artifacts, |
| 108 |
toolchains, |
| 109 |
settings, |
| 110 |
extensions, |
| 111 |
buildSystems, |
| 112 |
Version.BUILDER_NATIVE_MODEL_API_VERSION); |
| 113 |
|
| 114 |
} |
| 115 |
|
| 116 |
private List<NativeArtifact> createNativeArtifacts() { |
| 117 |
List<NativeArtifact> artifacts = Lists.newArrayList(); |
| 118 |
|
| 119 |
for (NativeLibrary lib : config.getLibraries()) { |
| 120 |
List<NativeFolder> folders = lib.getFolders().stream() |
| 121 |
.map(src -> { |
| 122 |
checkNotNull(src.getSrc()); |
| 123 |
return new NativeFolderImpl( |
| 124 |
src.getSrc(), |
| 125 |
ImmutableMap.of( |
| 126 |
"c", getSettingsName(convertFlagFormat(src.getcFlags())), |
| 127 |
"c++", getSettingsName(convertFlagFormat(src.getCppFlags()))), |
| 128 |
src.getWorkingDirectory());}) |
| 129 |
.collect(Collectors.toList()); |
| 130 |
List<NativeFile> files = lib.getFiles().stream() |
| 131 |
.map(src -> { |
| 132 |
checkNotNull(src.getSrc()); |
| 133 |
return new NativeFileImpl( |
| 134 |
src.getSrc(), |
| 135 |
getSettingsName(convertFlagFormat(src.getFlags())), |
| 136 |
src.getWorkingDirectory());}) |
| 137 |
.collect(Collectors.toList()); |
| 138 |
checkNotNull(lib.getToolchain()); |
| 139 |
checkNotNull(lib.getAssembleTaskName()); |
| 140 |
checkNotNull(lib.getOutput()); |
| 141 |
checkNotNull(lib.getAbi()); |
| 142 |
checkNotNull(lib.getArtifactName()); |
| 143 |
NativeArtifact artifact = new NativeArtifactImpl( |
| 144 |
lib.getName(), |
| 145 |
lib.getToolchain(), |
| 146 |
Strings.nullToEmpty(lib.getGroupName()), |
| 147 |
lib.getAssembleTaskName(), |
| 148 |
folders, |
| 149 |
files, |
| 150 |
ImmutableList.copyOf(lib.getExportedHeaders()), |
| 151 |
lib.getOutput(), |
| 152 |
lib.getAbi(), |
| 153 |
lib.getArtifactName()); |
| 154 |
artifacts.add(artifact); |
| 155 |
} |
| 156 |
return artifacts; |
| 157 |
} |
| 158 |
|
| 159 |
@NonNull |
| 160 |
private static List<String> convertFlagFormat(@Nullable String flags) { |
| 161 |
if (flags == null) { |
| 162 |
return Lists.newArrayList(); |
| 163 |
} |
| 164 |
return NativeCompilerArgsUtil.transform(StringHelper.tokenizeString(flags)); |
| 165 |
} |
| 166 |
|
| 167 |
private String getSettingsName(@NonNull List<String> flags) { |
| 168 |
// Copy flags to ensure it is serializable. |
| 169 |
List<String> flagsCopy = ImmutableList.copyOf(flags); |
| 170 |
NativeSettings setting = settingsMap.get(flags); |
| 171 |
if (setting == null) { |
| 172 |
setting = new NativeSettingsImpl("setting" + settingIndex, flagsCopy); |
| 173 |
settingsMap.put(flagsCopy, setting); |
| 174 |
settingIndex++; |
| 175 |
} |
| 176 |
return setting.getName(); |
| 177 |
} |
| 178 |
|
| 179 |
private List<NativeToolchain> createNativeToolchains() { |
| 180 |
return config.getToolchains().values().stream() |
| 181 |
.map(toolchain -> new NativeToolchainImpl( |
| 182 |
toolchain.getName(), |
| 183 |
toolchain.getCCompilerExecutable(), |
| 184 |
toolchain.getCppCompilerExecutable())).collect(Collectors.toList()); |
| 185 |
} |
| 186 |
|
| 187 |
private Map<String, String> createFileExtensionMap() { |
| 188 |
Map<String, String> extensions = Maps.newHashMap(); |
| 189 |
for (String ext : config.getcFileExtensions()) { |
| 190 |
extensions.put(ext, "c"); |
| 191 |
} |
| 192 |
for (String ext : config.getCppFileExtensions()) { |
| 193 |
extensions.put(ext, "c++"); |
| 194 |
} |
| 195 |
return extensions; |
| 196 |
} |
| 197 |
} |