1 /* 2 * Copyright (C) 2010 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.ant; 18 19 import org.apache.tools.ant.BuildException; 20 import org.apache.tools.ant.Project; 21 import org.apache.tools.ant.taskdefs.ExecTask; 22 import org.apache.tools.ant.types.Path; 23 24 import java.io.File; 25 import java.util.ArrayList; 26 import java.util.List; 27 28 /** 29 * Task to execute renderscript. 30 * <p> 31 * It expects 5 attributes:<br> 32 * 'executable' ({@link Path} with a single path) for the location of the llvm executable<br> 33 * 'framework' ({@link Path} with 1 or more paths) for the include paths.<br> 34 * 'genFolder' ({@link Path} with a single path) for the location of the gen folder.<br> 35 * 'resFolder' ({@link Path} with a single path) for the location of the res folder.<br> 36 * 'targetApi' for the -target-api value.<br> 37 * <p> 38 * It also expects one or more inner elements called "source" which are identical to {@link Path} 39 * elements for where to find .rs files. 40 */ 41 public class RenderScriptTask extends MultiFilesTask { 42 43 private String mExecutable; 44 private Path mFramework; 45 private String mGenFolder; 46 private String mResFolder; 47 private final List<Path> mPaths = new ArrayList<Path>(); 48 private int mTargetApi = 0; 49 50 private class RenderScriptProcessor implements SourceProcessor { 51 52 private final String mTargetApiStr; 53 54 public RenderScriptProcessor(int targetApi) { 55 // get the target api value. Must be 11+ or llvm-rs-cc complains. 56 mTargetApiStr = Integer.toString(mTargetApi < 11 ? 11 : mTargetApi); 57 } 58 59 public String getSourceFileExtension() { 60 return "rs"; 61 } 62 63 public void process(String filePath, String sourceFolder, List<String> sourceFolders, 64 Project taskProject) { 65 File exe = new File(mExecutable); 66 String execTaskName = exe.getName(); 67 68 ExecTask task = new ExecTask(); 69 task.setTaskName(execTaskName); 70 task.setProject(taskProject); 71 task.setOwningTarget(getOwningTarget()); 72 task.setExecutable(mExecutable); 73 task.setFailonerror(true); 74 75 for (String path : mFramework.list()) { 76 File res = new File(path); 77 if (res.isDirectory()) { 78 task.createArg().setValue("-I"); 79 task.createArg().setValue(path); 80 } else { 81 System.out.println(String.format( 82 "WARNING: RenderScript include directory '%s' does not exist!", 83 res.getAbsolutePath())); 84 } 85 86 } 87 88 task.createArg().setValue("-target-api"); 89 task.createArg().setValue(mTargetApiStr); 90 91 task.createArg().setValue("-d"); 92 task.createArg().setValue(getDependencyFolder(filePath, sourceFolder)); 93 task.createArg().setValue("-MD"); 94 95 task.createArg().setValue("-p"); 96 task.createArg().setValue(mGenFolder); 97 task.createArg().setValue("-o"); 98 task.createArg().setValue(mResFolder); 99 task.createArg().setValue(filePath); 100 101 // execute it. 102 task.execute(); 103 } 104 105 public void displayMessage(DisplayType type, int count) { 106 switch (type) { 107 case FOUND: 108 System.out.println(String.format("Found %1$d RenderScript files.", count)); 109 break; 110 case COMPILING: 111 if (count > 0) { 112 System.out.println(String.format( 113 "Compiling %1$d RenderScript files with -target-api %2$d", 114 count, mTargetApi)); 115 } else { 116 System.out.println("No RenderScript files to compile."); 117 } 118 break; 119 case REMOVE_OUTPUT: 120 System.out.println(String.format("Found %1$d obsolete output files to remove.", 121 count)); 122 break; 123 case REMOVE_DEP: 124 System.out.println( 125 String.format("Found %1$d obsolete dependency files to remove.", 126 count)); 127 break; 128 } 129 } 130 131 private String getDependencyFolder(String filePath, String sourceFolder) { 132 String relative = filePath.substring(sourceFolder.length()); 133 if (relative.charAt(0) == '/') { 134 relative = relative.substring(1); 135 } 136 137 return new File(mGenFolder, relative).getParent(); 138 } 139 } 140 141 142 /** 143 * Sets the value of the "executable" attribute. 144 * @param executable the value. 145 */ 146 public void setExecutable(Path executable) { 147 mExecutable = TaskHelper.checkSinglePath("executable", executable); 148 } 149 150 public void setFramework(Path value) { 151 mFramework = value; 152 } 153 154 public void setGenFolder(Path value) { 155 mGenFolder = TaskHelper.checkSinglePath("genFolder", value); 156 } 157 158 public void setResFolder(Path value) { 159 mResFolder = TaskHelper.checkSinglePath("resFolder", value); 160 } 161 162 public void setTargetApi(String targetApi) { 163 try { 164 mTargetApi = Integer.parseInt(targetApi); 165 if (mTargetApi <= 0) { 166 throw new BuildException("targetApi attribute value must be >= 1"); 167 } 168 } catch (NumberFormatException e) { 169 throw new BuildException("targetApi attribute value must be an integer", e); 170 } 171 } 172 173 public Path createSource() { 174 Path p = new Path(getProject()); 175 mPaths.add(p); 176 return p; 177 } 178 179 @Override 180 public void execute() throws BuildException { 181 if (mExecutable == null) { 182 throw new BuildException("RenderScriptTask's 'executable' is required."); 183 } 184 if (mFramework == null) { 185 throw new BuildException("RenderScriptTask's 'framework' is required."); 186 } 187 if (mGenFolder == null) { 188 throw new BuildException("RenderScriptTask's 'genFolder' is required."); 189 } 190 if (mResFolder == null) { 191 throw new BuildException("RenderScriptTask's 'resFolder' is required."); 192 } 193 if (mTargetApi == 0) { 194 throw new BuildException("RenderScriptTask's 'targetApi' is required."); 195 } 196 197 processFiles(new RenderScriptProcessor(mTargetApi), mPaths, mGenFolder); 198 } 199 } 200