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 public String getSourceFileExtension() { 49 return "aidl"; 50 } 51 52 public void process(String filePath, String sourceFolder, 53 List<String> sourceFolders, Project taskProject) { 54 ExecTask task = new ExecTask(); 55 task.setProject(taskProject); 56 task.setOwningTarget(getOwningTarget()); 57 task.setExecutable(mExecutable); 58 task.setTaskName("aidl"); 59 task.setFailonerror(true); 60 61 task.createArg().setValue("-p" + mFramework); 62 task.createArg().setValue("-o" + mGenFolder); 63 // add all the source folders as import in case an aidl file in a source folder 64 // imports a parcelable from another source folder. 65 for (String importFolder : sourceFolders) { 66 task.createArg().setValue("-I" + importFolder); 67 } 68 69 // set auto dependency file creation 70 task.createArg().setValue("-a"); 71 72 task.createArg().setValue(filePath); 73 74 // execute it. 75 task.execute(); 76 } 77 78 public void displayMessage(DisplayType type, int count) { 79 switch (type) { 80 case FOUND: 81 System.out.println(String.format("Found %1$d AIDL files.", count)); 82 break; 83 case COMPILING: 84 if (count > 0) { 85 System.out.println(String.format("Compiling %1$d AIDL files.", 86 count)); 87 } else { 88 System.out.println("No AIDL files to compile."); 89 } 90 break; 91 case REMOVE_OUTPUT: 92 System.out.println(String.format("Found %1$d obsolete output files to remove.", 93 count)); 94 break; 95 case REMOVE_DEP: 96 System.out.println( 97 String.format("Found %1$d obsolete dependency files to remove.", 98 count)); 99 break; 100 } 101 } 102 103 } 104 105 /** 106 * Sets the value of the "executable" attribute. 107 * @param executable the value. 108 */ 109 public void setExecutable(Path executable) { 110 mExecutable = TaskHelper.checkSinglePath("executable", executable); 111 } 112 113 public void setFramework(Path value) { 114 mFramework = TaskHelper.checkSinglePath("framework", value); 115 } 116 117 public void setGenFolder(Path value) { 118 mGenFolder = TaskHelper.checkSinglePath("genFolder", value); 119 } 120 121 public Path createSource() { 122 Path p = new Path(getProject()); 123 mPaths.add(p); 124 return p; 125 } 126 127 @Override 128 public void execute() throws BuildException { 129 if (mExecutable == null) { 130 throw new BuildException("AidlExecTask's 'executable' is required."); 131 } 132 if (mFramework == null) { 133 throw new BuildException("AidlExecTask's 'framework' is required."); 134 } 135 if (mGenFolder == null) { 136 throw new BuildException("AidlExecTask's 'genFolder' is required."); 137 } 138 139 processFiles(new AidlProcessor(), mPaths, mGenFolder); 140 } 141 142 } 143