| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | package com.sun.fortress.repository; |
|---|
| 19 | |
|---|
| 20 | import java.io.File; |
|---|
| 21 | import java.io.FileNotFoundException; |
|---|
| 22 | import java.io.IOException; |
|---|
| 23 | |
|---|
| 24 | import com.sun.fortress.useful.Path; |
|---|
| 25 | import com.sun.fortress.useful.StringMap; |
|---|
| 26 | import com.sun.fortress.useful.Useful; |
|---|
| 27 | |
|---|
| 28 | public class ProjectProperties { |
|---|
| 29 | |
|---|
| 30 | private static String fortressAutoHome() { |
|---|
| 31 | String s = null; |
|---|
| 32 | try { |
|---|
| 33 | s = System.getProperty("fortress.autohome"); |
|---|
| 34 | } catch (Throwable th) { |
|---|
| 35 | |
|---|
| 36 | } |
|---|
| 37 | if (s == null) { |
|---|
| 38 | s = System.getenv("FORTRESS_AUTOHOME"); |
|---|
| 39 | } |
|---|
| 40 | if (s == null) { |
|---|
| 41 | Path p = new Path(System.getProperty("java.class.path")); |
|---|
| 42 | File f = null; |
|---|
| 43 | try { |
|---|
| 44 | f = p.findDir("../ProjectFortress"); |
|---|
| 45 | try { |
|---|
| 46 | s = (new File(f, "..")).getCanonicalPath(); |
|---|
| 47 | } catch (IOException ex) { |
|---|
| 48 | throw new Error("Failure to evaluate relative path .. from " + f); |
|---|
| 49 | } |
|---|
| 50 | } catch (FileNotFoundException ex1) { |
|---|
| 51 | try { |
|---|
| 52 | f = p.findDir("../../ProjectFortress/build"); |
|---|
| 53 | try { |
|---|
| 54 | s = (new File(f, "../..")).getCanonicalPath(); |
|---|
| 55 | } catch (IOException ex) { |
|---|
| 56 | throw new Error("Failure to evaluate relative path ../.. from " + f); |
|---|
| 57 | } |
|---|
| 58 | } catch (FileNotFoundException ex2) { |
|---|
| 59 | throw new Error("Could not find fortress home using fortress.autohome, FORTRESS_AUTOHOME, or probing classpath."); |
|---|
| 60 | } |
|---|
| 61 | } |
|---|
| 62 | } |
|---|
| 63 | return s; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | private static String someImplDir(String property_name, String default_name) { |
|---|
| 77 | String s = null; |
|---|
| 78 | |
|---|
| 79 | try { |
|---|
| 80 | s = System.getProperty(property_name); |
|---|
| 81 | } catch (Throwable th) { |
|---|
| 82 | |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | if (s == null) { |
|---|
| 86 | s = BASEDIR + default_name; |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | s = s + File.separator; |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | s = backslashToSlash(s); |
|---|
| 94 | return s; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | public static String backslashToSlash(String s) { |
|---|
| 110 | |
|---|
| 111 | return s; |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | public static final String FORTRESS_AUTOHOME = fortressAutoHome(); |
|---|
| 115 | |
|---|
| 116 | static final String home = System.getenv("HOME"); |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | static final StringMap searchHead = new StringMap.ComposedMaps( |
|---|
| 123 | new StringMap.FromReflection(ProjectProperties.class, "FORTRESS_"), |
|---|
| 124 | new StringMap.FromSysProps(), |
|---|
| 125 | new StringMap.FromEnv()); |
|---|
| 126 | |
|---|
| 127 | private static final String explicitRepository = searchHead.get("fortress.repository"); |
|---|
| 128 | public static final String REPOSITORY_PATH_STRING = explicitRepository != null ? explicitRepository : |
|---|
| 129 | searchHead.getCompletely(";.fortress;${HOME}/.fortress;${FORTRESS_AUTOHOME}/local_repository;${FORTRESS_AUTOHOME}/default_repository", |
|---|
| 130 | 1000); |
|---|
| 131 | public static final Path REPOSITORY_PATH = new Path(REPOSITORY_PATH_STRING); |
|---|
| 132 | static { |
|---|
| 133 | if (REPOSITORY_PATH.length() == 0) { |
|---|
| 134 | if (explicitRepository != null) |
|---|
| 135 | throw new Error("User-specified repository " + explicitRepository + " is not a readable directory"); |
|---|
| 136 | else |
|---|
| 137 | throw new Error("Could not find any readable directories in the (semicolon-separated) repository list " + REPOSITORY_PATH_STRING); |
|---|
| 138 | } |
|---|
| 139 | } |
|---|
| 140 | public static final String REPOSITORY = REPOSITORY_PATH.findDirName(".", "").toString(); |
|---|
| 141 | |
|---|
| 142 | static { |
|---|
| 143 | if ("".equals(REPOSITORY)) { |
|---|
| 144 | throw new Error("Could not find any readable directories in the (semicolon-separated) repository list " + REPOSITORY_PATH_STRING); |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | File config = new File(REPOSITORY + "/configuration"); |
|---|
| 148 | File otherConfig = null; |
|---|
| 149 | try { |
|---|
| 150 | otherConfig = REPOSITORY_PATH.findFile("configuration"); |
|---|
| 151 | } catch (IOException ex) { |
|---|
| 152 | throw new Error(ex.getMessage()); |
|---|
| 153 | } |
|---|
| 154 | if (otherConfig != null) { |
|---|
| 155 | if (! config.exists()) { |
|---|
| 156 | throw new Error("Repository " + REPOSITORY + " does not contain configuration" + " but " + otherConfig + " is ok "); |
|---|
| 157 | } else if (! config.isFile()) { |
|---|
| 158 | throw new Error("Repository " + REPOSITORY + "/configuration is not a regular file" + " but " + otherConfig + " is ok "); |
|---|
| 159 | } else if (! config.canRead()) { |
|---|
| 160 | throw new Error("Repository " + REPOSITORY + "/configuration is not readable" + " but " + otherConfig + " is ok "); |
|---|
| 161 | } |
|---|
| 162 | } else { |
|---|
| 163 | if (! config.exists()) { |
|---|
| 164 | throw new Error("Repository " + REPOSITORY + " does not contain configuration"); |
|---|
| 165 | } else if (! config.isFile()) { |
|---|
| 166 | throw new Error("Repository " + REPOSITORY + "/configuration is not a regular file"); |
|---|
| 167 | } else if (! config.canRead()) { |
|---|
| 168 | throw new Error("Repository " + REPOSITORY + "/configuration is not readable"); |
|---|
| 169 | } |
|---|
| 170 | } |
|---|
| 171 | } |
|---|
| 172 | static final StringMap searchTail = |
|---|
| 173 | new StringMap.FromFileProps(REPOSITORY+"/configuration"); |
|---|
| 174 | |
|---|
| 175 | static final StringMap allProps = new StringMap.ComposedMaps( |
|---|
| 176 | searchHead, |
|---|
| 177 | searchTail |
|---|
| 178 | ); |
|---|
| 179 | |
|---|
| 180 | static final public String get(String s) { |
|---|
| 181 | String result = Useful.substituteVarsCompletely(s, allProps, 1000); |
|---|
| 182 | return result; |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | static final public String get(String s, String ifMissing) { |
|---|
| 186 | String result = allProps.get(s); |
|---|
| 187 | if (result == null) |
|---|
| 188 | result = ifMissing; |
|---|
| 189 | if (result != null) |
|---|
| 190 | result = Useful.substituteVarsCompletely(result, allProps, 1000); |
|---|
| 191 | if (result == null) { |
|---|
| 192 | throw new Error("Must supply a definition (as property, environment variable, or repository configuration property) for " + s); |
|---|
| 193 | } |
|---|
| 194 | return result; |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | static final public boolean getBoolean(String s, boolean ifMissing) { |
|---|
| 198 | String result = allProps.get(s); |
|---|
| 199 | if (result != null) |
|---|
| 200 | result = Useful.substituteVarsCompletely(result, allProps, 1000); |
|---|
| 201 | if (result == null) return ifMissing; |
|---|
| 202 | if (result.length() == 0) |
|---|
| 203 | return true; |
|---|
| 204 | s = result.toLowerCase(); |
|---|
| 205 | char c = result.charAt(0); |
|---|
| 206 | if (c == 'y' || c == 't' || c == '1') return true; |
|---|
| 207 | if (c == 'n' || c == 'f' || c == '0') return false; |
|---|
| 208 | |
|---|
| 209 | throw new Error("Unexpected definition of prop/env " + s + ", got " + result + ", need t/f/y/n/1/0[...]"); |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | private static String searchDef(String asProp, String asEnv, String defaultValue) { |
|---|
| 224 | String result = null; |
|---|
| 225 | result = System.getProperty(asProp); |
|---|
| 226 | if (result == null) |
|---|
| 227 | result = System.getenv(asEnv); |
|---|
| 228 | if (result == null) |
|---|
| 229 | result = searchTail.get(asProp); |
|---|
| 230 | result = result == null ? defaultValue : result; |
|---|
| 231 | result = Useful.substituteVarsCompletely(result, allProps, 1000); |
|---|
| 232 | return result; |
|---|
| 233 | } |
|---|
| 234 | |
|---|
| 235 | |
|---|
| 236 | |
|---|
| 237 | |
|---|
| 238 | |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | |
|---|
| 242 | public static final String BASEDIR = searchDef("BASEDIR", "BASEDIR", "${FORTRESS_AUTOHOME}/ProjectFortress/"); |
|---|
| 243 | |
|---|
| 244 | public static final String CACHES = get("fortress.caches", "${REPOSITORY}/caches"); |
|---|
| 245 | |
|---|
| 246 | public static final String INTERPRETER_CACHE_DIR = get("fortress.interpreter.cache", "${CACHES}/interpreter_cache"); |
|---|
| 247 | public static final String PRESYNTAX_CACHE_DIR = get("fortress.presyntax.cache", "${CACHES}/presyntax_cache"); |
|---|
| 248 | public static final String ANALYZED_CACHE_DIR = get("fortress.analyzed.cache", "${CACHES}/analyzed_cache"); |
|---|
| 249 | public static final String SYNTAX_CACHE_DIR = get("fortress.syntax.cache", "${CACHES}/syntax_cache"); |
|---|
| 250 | public static final String BYTECODE_CACHE_DIR = get("fortress.bytecode.cache", "${CACHES}/bytecode_cache"); |
|---|
| 251 | public static final String NATIVE_WRAPPER_CACHE_DIR = get("fortress.nativewrapper.cache", "${CACHES}/nativewrapper_cache"); |
|---|
| 252 | |
|---|
| 253 | public static final Path SOURCE_PATH = new Path(searchDef("fortress.source.path", "FORTRESS_SOURCE_PATH", ".")); |
|---|
| 254 | |
|---|
| 255 | |
|---|
| 256 | static { |
|---|
| 257 | ensureDirectoryExists(PRESYNTAX_CACHE_DIR); |
|---|
| 258 | ensureDirectoryExists(INTERPRETER_CACHE_DIR); |
|---|
| 259 | ensureDirectoryExists(ANALYZED_CACHE_DIR); |
|---|
| 260 | ensureDirectoryExists(SYNTAX_CACHE_DIR); |
|---|
| 261 | ensureDirectoryExists(BYTECODE_CACHE_DIR); |
|---|
| 262 | ensureDirectoryExists(NATIVE_WRAPPER_CACHE_DIR); |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | |
|---|
| 266 | public static String ensureDirectoryExists(String s) throws Error { |
|---|
| 267 | File f = new File(s); |
|---|
| 268 | if (f.exists()) { |
|---|
| 269 | if (f.isDirectory()) { |
|---|
| 270 | |
|---|
| 271 | } else { |
|---|
| 272 | throw new Error("Necessary 'directory' " + s + " is not a directory"); |
|---|
| 273 | } |
|---|
| 274 | } else { |
|---|
| 275 | if (f.mkdirs()) { |
|---|
| 276 | |
|---|
| 277 | } else { |
|---|
| 278 | throw new Error("Failed to create directory " + s ); |
|---|
| 279 | } |
|---|
| 280 | } |
|---|
| 281 | return s; |
|---|
| 282 | } |
|---|
| 283 | |
|---|
| 284 | public final static String COMP_SOURCE_SUFFIX = "fss"; |
|---|
| 285 | |
|---|
| 286 | public final static String COMP_TREE_SUFFIX = "tfs"; |
|---|
| 287 | |
|---|
| 288 | public final static String API_SOURCE_SUFFIX = "fsi"; |
|---|
| 289 | |
|---|
| 290 | public final static String API_TREE_SUFFIX = "tfi"; |
|---|
| 291 | |
|---|
| 292 | public final static boolean leakCheck = getBoolean("fortress.test.leaks", false); |
|---|
| 293 | |
|---|
| 294 | |
|---|
| 295 | |
|---|
| 296 | private ProjectProperties() { |
|---|
| 297 | } |
|---|
| 298 | |
|---|
| 299 | |
|---|
| 300 | public static String astSuffixForSource(String s) { |
|---|
| 301 | if (s.endsWith("." + COMP_SOURCE_SUFFIX)) |
|---|
| 302 | return COMP_TREE_SUFFIX; |
|---|
| 303 | |
|---|
| 304 | if (s.endsWith("." + API_SOURCE_SUFFIX)) |
|---|
| 305 | return API_TREE_SUFFIX; |
|---|
| 306 | |
|---|
| 307 | throw new Error("Unexpected suffix on Fortress(?) source file"); |
|---|
| 308 | } |
|---|
| 309 | |
|---|
| 310 | public static String fileName(String dir, String name, String suffix) { |
|---|
| 311 | return dir + "/" + name + "." + suffix; |
|---|
| 312 | } |
|---|
| 313 | |
|---|
| 314 | public static String compFileName(String dir, String name) { |
|---|
| 315 | return fileName(dir, name, ProjectProperties.COMP_TREE_SUFFIX); |
|---|
| 316 | } |
|---|
| 317 | |
|---|
| 318 | public static String apiFileName(String dir, String name) { |
|---|
| 319 | return fileName(dir, name, ProjectProperties.API_TREE_SUFFIX); |
|---|
| 320 | } |
|---|
| 321 | } |
|---|