View Javadoc

1   /*
2   	Copyright (c) 2009 Olivier Chafik, All Rights Reserved
3   	
4   	This file is part of JNAerator (http://jnaerator.googlecode.com/).
5   	
6   	JNAerator is free software: you can redistribute it and/or modify
7   	it under the terms of the GNU Lesser General Public License as published by
8   	the Free Software Foundation, either version 3 of the License, or
9   	(at your option) any later version.
10  	
11  	JNAerator is distributed in the hope that it will be useful,
12  	but WITHOUT ANY WARRANTY; without even the implied warranty of
13  	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  	GNU Lesser General Public License for more details.
15  	
16  	You should have received a copy of the GNU Lesser General Public License
17  	along with JNAerator.  If not, see <http://www.gnu.org/licenses/>.
18  */
19  package com.ochafik.lang.jnaerator;
20  
21  import java.io.File;
22  import java.io.FileFilter;
23  import java.io.IOException;
24  import java.io.Writer;
25  import java.util.ArrayList;
26  import java.util.Collection;
27  import java.util.EnumSet;
28  import java.util.LinkedHashMap;
29  import java.util.LinkedHashSet;
30  import java.util.List;
31  import java.util.Map;
32  import java.util.Set;
33  
34  import com.ochafik.lang.jnaerator.JNAeratorConfigUtils.FileExtensionFilter;
35  import com.ochafik.lang.jnaerator.cplusplus.CPlusPlusMangler;
36  import com.ochafik.lang.jnaerator.parser.Element;
37  import com.ochafik.lang.jnaerator.parser.Function;
38  import com.ochafik.util.CompoundCollection;
39  import com.ochafik.util.listenable.Adapter;
40  import com.ochafik.util.listenable.Filter;
41  
42  public class JNAeratorConfig {
43  	
44  	public enum Compiler {
45  		GCC4, MSVC9
46  	}
47  	public enum Architecture {
48  		x86_64, i386, PowerPC
49  	}
50  	public enum Platform {
51  		Windows, Linux, MacOSX
52  	}
53  	public enum GenFeatures {
54  		Compile,
55  		FileComments,
56  		UsageComments,
57  		EnumTypeLocationComments,
58  		LibrariesAutoExtraction,
59  		CPlusPlusMangling,
60  		StructConstructors, 
61  		TypedPointersForForwardDeclarations, 
62  		OriginalFunctionSignatures, 
63  		FunctionArgsJavaDoc
64  	}
65  	public static final String DEFAULT_HEADER_EXTENSIONS = "h:hpp:hxx";
66  	public static final String DEFAULT_IMPLEMS_EXTENSIONS = "cpp:c:cxx:m:mm";
67  	
68  	public final EnumSet<GenFeatures> features = EnumSet.allOf(GenFeatures.class);
69  	public final List<CPlusPlusMangler> cPlusPlusManglers = new ArrayList<CPlusPlusMangler>();
70  	
71  	public static class PreprocessorConfig {
72  
73  		public boolean WORKAROUND_PP_BUGS = true;
74  
75  		public final List<String> includes = new ArrayList<String>();
76  		public final Map<String, String> macros = new LinkedHashMap<String, String>();
77  		public final List<String> frameworksPath = new ArrayList<String>();
78  
79  		public List<String> includeStrings = new ArrayList<String>();
80  
81  		public boolean preprocess = true;
82  	}
83  	
84  	public final JNAeratorConfig.PreprocessorConfig preprocessorConfig = new JNAeratorConfig.PreprocessorConfig();
85  	public boolean followIncludes = false;
86  	public File preprocessingOutFile, macrosOutFile;
87  	public File choicesOutFile, choicesInputFile;
88  	public boolean useJNADirectCalls = false;
89  	public boolean limitComments = false, noComments = false;
90  	public boolean putTopStructsInSeparateFiles = true;
91  	public boolean bundleRuntime = true;
92  	public boolean extractLibSymbols = false;
93  	public String entryName;
94  	public int maxConstructedFields = 10;
95  	
96  	public Map<String, String> extraJavaSourceFilesContents = new LinkedHashMap<String, String>();
97  	
98  	public FileFilter fileFilter = new FileExtensionFilter(DEFAULT_HEADER_EXTENSIONS.split("[:;]"));
99  	
100 	public Map<String, List<File>> libraryFilesByArch = new LinkedHashMap<String, List<File>>();
101 	public List<File> libraryFiles = new ArrayList<File>();
102 	
103 	public Map<File, String> libraryByFile = new LinkedHashMap<File, String>();
104 	public void addLibraryFile(File file, String arch) {
105 		
106 		List<File> others = libraryFilesByArch.get(arch);
107 		if (others == null)
108 			libraryFilesByArch.put(arch, others = new ArrayList<File>());
109 		
110 		String fn = file.getName();
111 		int i = fn.lastIndexOf('.');
112 		if (i > 0)
113 			fn = fn.substring(0, i);
114 		
115 		others.add(file);
116 		libraryByFile.put(file, fn);
117 		libraryFiles.add(file);
118 	}
119 	public void addSourceFile(File file, String library, boolean applyFilters) throws IOException {
120 		if (file.isFile()) {
121 			if (fileFilter == null || !applyFilters || fileFilter.accept(file)) {
122 				file = file.getCanonicalFile();
123 				libraryByFile.put(file, library);
124 				sourceFiles.add(file);
125 			}
126 		} else {
127 			File[] fs = file.listFiles();
128 			if (fs != null) {
129 				for (File f : fs) {
130 					addSourceFile(f, library, true);
131 				}
132 			}
133 		}
134 	}
135 
136 	public JNAeratorConfig() {
137 //		if (System.getenv("POINTER_CLASSES") == null)
138 //			features.remove(GenFeatures.TypedPointersForForwardDeclarations);
139 	}
140 	public boolean verbose = false;
141 	public File outputDir;
142 	public List<String> rootDirectoriesPrefixesForSourceComments = new ArrayList<String>();
143 	public Adapter<Function, Boolean> functionsAccepter;
144 	public String packageName = null, rootPackageName = null;
145 	public String defaultLibrary;
146 	public Map<String, File> libraryProjectSources = new LinkedHashMap<String, File>();
147 	public Adapter<File, String> fileToLibrary = new Adapter<File, String>() {
148 		public String adapt(File value) {
149 			String libraryFile = null;
150 			try {
151 				//String canoFile = value.getCanonicalPath();
152 				//libraryFile = libraryByFile.get(canoFile);
153 				libraryFile = libraryByFile.get(value.getCanonicalFile());
154 				//if (value.toString().startsWith("\""))
155 				//	new Exception("Double quotes in file !").printStackTrace();
156 //				if (!canoFile.contains("Program Files")) {
157 //					System.out.println("libraryByFile = " + libraryByFile);
158 //					System.out.println("libraryByFile(" + canoFile + ") = " + libraryFile);
159 //					System.out.println("    value = " + value);
160 //					System.out.println("can value = " + value.getCanonicalFile());
161 //					System.out.println("abs value = " + value.getAbsoluteFile());
162 //				}
163 				
164 			} catch (IOException e) {
165 				e.printStackTrace();
166 			}
167 			return libraryFile == null ? defaultLibrary : libraryFile;
168 		}
169 	};
170 	public void addRootDir(File dir) throws IOException {
171 		if (!dir.exists())
172 			return;
173 		String str = dir.getCanonicalPath();
174 		if (!str.endsWith(File.separator))
175 			str += File.separator;
176 		if (!rootDirectoriesPrefixesForSourceComments.contains(str))
177 			rootDirectoriesPrefixesForSourceComments.add(str);
178 	}
179 	public Filter<Element> symbolsAccepter = new Filter<Element>() {
180 		public boolean accept(Element value) {
181 			String s = Element.getFileOfAscendency(value);
182 			if (s == null)
183 				return false;
184 			
185 			File f = new File(s);
186 			try {
187 				f = f.getCanonicalFile();
188 			} catch (IOException e) {
189 				e.printStackTrace();
190 			}
191 			return libraryByFile.containsKey(f);
192 		}
193 	};
194 	
195 	public String libraryForElementsInNullFile;
196 	public String cPlusPlusNameSpaceSeparator = "_";
197 	public boolean useJNAeratorUnionAndStructClasses = true;
198 	public boolean preferJavac = false;
199 	public Set<File> bridgeSupportFiles = new LinkedHashSet<File>();
200 	public File outputJar;
201 	public File cacheDir;
202 	public boolean autoConf = true;
203 	public boolean gccLong = false, sizeAsLong = false;
204 	public boolean compile = true;
205 	public boolean bundleSources = true;
206 	public boolean noCPlusPlus;
207 	
208 	public String getLibrary(String elementFile) {
209 		if (elementFile == null)
210 			return libraryForElementsInNullFile;
211 		
212 		return fileToLibrary == null ? 
213 				defaultLibrary : 
214 				fileToLibrary.adapt(new File(elementFile));
215 	}
216 
217 	Set<File> sourceFiles = new LinkedHashSet<File>();
218 	public boolean bundleLibraries = true;
219 	public boolean wcharAsShort = false;
220 	public boolean genCPlusPlus = false;
221 	public File extractedSymbolsOut;
222 	public boolean stringifyConstCStringReturnValues = true;
223 	public File bridgesupportOutFile;
224 	public boolean noMangling;
225 	public boolean noPrimitiveArrays;
226 	public File scalaOut;
227 	public Collection<File> getFiles() {
228 		/*return new AdaptedCollection<String, File>(libraryByFile.keySet(), new Adapter<String, File>() {
229 			@Override
230 			public File adapt(String value) {
231 				return new File(value);
232 			}
233 		});*/
234 		return sourceFiles;//libraryByFile.keySet();
235 	}
236 
237 	public String relativizeFileForSourceComments(String path) {
238 		if (path == null)
239 			return null;
240 		
241 		for (String pref : rootDirectoriesPrefixesForSourceComments) {
242 			if (path.startsWith(pref)) {
243 				path = path.substring(pref.length());
244 				break;
245 			}
246 		}
247 		return path;
248 	}
249 	@SuppressWarnings("unchecked")
250 	public Collection<File> getInputFiles() {
251 		return new CompoundCollection<File>(sourceFiles, bridgeSupportFiles, libraryFiles);
252 	}
253 }