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.util.ArrayList; 25 import java.util.List; 26 27 /** 28 * Task to execute aidl. 29 * <p> 30 * It expects 3 attributes:<br> 31 * 'executable' ({@link Path} with a single path) for the location of the aidl executable<br> 32 * 'framework' ({@link Path} with a single path) for the "preprocessed" file containing all the 33 * parcelables exported by the framework<br> 34 * 'genFolder' ({@link Path} with a single path) for the location of the gen folder. 35 * 36 * It also expects one or more inner elements called "source" which are identical to {@link Path} 37 * elements. 38 */ 39 public class AidlExecTask extends MultiFilesTask { 40 41 private String mExecutable; 42 private String mFramework; 43 private String mGenFolder; 44 private final ArrayList<Path> mPaths = new ArrayList<Path>(); 45 46 private class AidlProcessor implements SourceProcessor { 47 48 @Override 49 public String getSourceFileExtension() { 50 return "aidl"; 51 } 52 53 @Override 54 public void process(String filePath, String sourceFolder, 55 List<String> sourceFolders, Project taskProject) { 56 ExecTask task = new ExecTask(); 57 task.setProject(taskProject); 58 task.setOwningTarget(getOwningTarget()); 59 task.setExecutable(mExecutable); 60 task.setTaskName("aidl"); 61 task.setFailonerror(true); 62 63 task.createArg().setValue("-p" + mFramework); 64 task.createArg().setValue("-o" + mGenFolder); 65 // add all the source folders as import in case an aidl file in a source folder 66 // imports a parcelable from another source folder. 67 for (String importFolder : sourceFolders) { 68 task.createArg().setValue("-I" + importFolder); 69 } 70 71 // set auto dependency file creation 72 task.createArg().setValue("-a"); 73 74 task.createArg().setValue(filePath); 75 76 // execute it. 77 task.execute(); 78 } 79 80 @Override 81 public void displayMessage(DisplayType type, int count) { 82 switch (type) { 83 case FOUND: 84 System.out.println(String.format("Found %1$d AIDL files.", count)); 85 break; 86 case COMPILING: 87 if (count > 0) { 88 System.out.println(String.format("Compiling %1$d AIDL files.", 89 count)); 90 } else { 91 System.out.println("No AIDL files to compile."); 92 } 93 break; 94 case REMOVE_OUTPUT: 95 System.out.println(String.format("Found %1$d obsolete output files to remove.", 96 count)); 97 break; 98 case REMOVE_DEP: 99 System.out.println( 100 String.format("Found %1$d obsolete dependency files to remove.", 101 count)); 102 break; 103 } 104 } 105 106 } 107 108 /** 109 * Sets the value of the "executable" attribute. 110 * @param executable the value. 111 */ 112 public void setExecutable(Path executable) { 113 mExecutable = TaskHelper.checkSinglePath("executable", executable); 114 } 115 116 public void setFramework(Path value) { 117 mFramework = TaskHelper.checkSinglePath("framework", value); 118 } 119 120 public void setGenFolder(Path value) { 121 mGenFolder = TaskHelper.checkSinglePath("genFolder", value); 122 } 123 124 public Path createSource() { 125 Path p = new Path(getProject()); 126 mPaths.add(p); 127 return p; 128 } 129 130 @Override 131 public void execute() throws BuildException { 132 if (mExecutable == null) { 133 throw new BuildException("AidlExecTask's 'executable' is required."); 134 } 135 if (mFramework == null) { 136 throw new BuildException("AidlExecTask's 'framework' is required."); 137 } 138 if (mGenFolder == null) { 139 throw new BuildException("AidlExecTask's 'genFolder' is required."); 140 } 141 142 processFiles(new AidlProcessor(), mPaths, mGenFolder); 143 } 144 145 } 146