1 /*
   2  * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 import java.util.Enumeration;
  26 import java.util.Hashtable;
  27 import java.util.Vector;
  28 
  29 class BuildConfig {
  30     @SuppressWarnings("rawtypes")
  31     Hashtable vars;
  32     Vector<String> basicNames, basicPaths;
  33     String[] context;
  34 
  35     static CompilerInterface ci;
  36     static CompilerInterface getCI() {
  37         if (ci == null) {
  38             String comp = (String)getField(null, "CompilerVersion");
  39             try {
  40                 ci = (CompilerInterface)Class.forName("CompilerInterface" + comp).newInstance();
  41             } catch (Exception cnfe) {
  42                 System.err.println("Cannot find support for compiler " + comp);
  43                 throw new RuntimeException(cnfe.toString());
  44             }
  45         }
  46         return ci;
  47     }
  48 
  49     @SuppressWarnings("rawtypes")
  50     protected void initNames(String flavour, String build, String outDll) {
  51         if (vars == null) vars = new Hashtable();
  52 
  53         String flavourBuild =  flavour + "_" + build;
  54         String platformName = getFieldString(null, "PlatformName");
  55         System.out.println();
  56         System.out.println(flavourBuild);
  57 
  58         put("Name", getCI().makeCfgName(flavourBuild, platformName));
  59         put("Flavour", flavour);
  60         put("Build", build);
  61         put("PlatformName", platformName);
  62 
  63         // ones mentioned above were needed to expand format
  64         String buildBase = expandFormat(getFieldString(null, "BuildBase"));
  65         String sourceBase = getFieldString(null, "SourceBase");
  66         String buildSpace = getFieldString(null, "BuildSpace");
  67         String outDir = buildBase;
  68         String jdkTargetRoot = getFieldString(null, "JdkTargetRoot");
  69 
  70         put("Id", flavourBuild);
  71         put("OutputDir", outDir);
  72         put("SourceBase", sourceBase);
  73         put("BuildBase", buildBase);
  74         put("BuildSpace", buildSpace);
  75         put("OutputDll", outDir + Util.sep + outDll);
  76         put("JdkTargetRoot", jdkTargetRoot);
  77 
  78         context = new String [] {flavourBuild, flavour, build, null};
  79     }
  80 
  81     protected void init(Vector<String> includes, Vector<String> defines) {
  82         initDefaultDefines(defines);
  83         initDefaultCompilerFlags(includes);
  84         initDefaultLinkerFlags();
  85         //handleDB();
  86     }
  87 
  88 
  89     protected void initDefaultCompilerFlags(Vector<String> includes) {
  90         Vector compilerFlags = new Vector();
  91 
  92         compilerFlags.addAll(getCI().getBaseCompilerFlags(getV("Define"),
  93                                                           includes,
  94                                                           get("OutputDir")));
  95 
  96         put("CompilerFlags", compilerFlags);
  97     }
  98 
  99     protected void initDefaultLinkerFlags() {
 100         Vector linkerFlags = new Vector();
 101 
 102         linkerFlags.addAll(getCI().getBaseLinkerFlags( get("OutputDir"), get("OutputDll"), get("PlatformName")));
 103 
 104         put("LinkerFlags", linkerFlags);
 105     }
 106 
 107     public boolean matchesIgnoredPath(String path) {
 108         Vector<String> rv = new Vector<String>();
 109         collectRelevantVectors(rv, "IgnorePath");
 110         for (String pathPart : rv) {
 111             if (path.contains(pathPart))  {
 112                 return true;
 113             }
 114         }
 115         return false;
 116     }
 117 
 118     public boolean matchesHidePath(String path) {
 119         Vector<String> rv = new Vector<String>();
 120         collectRelevantVectors(rv, "HidePath");
 121         for (String pathPart : rv) {
 122             if (path.contains(Util.normalize(pathPart)))  {
 123                 return true;
 124             }
 125         }
 126         return false;
 127     }
 128 
 129    public Vector<String> matchesAdditionalGeneratedPath(String fullPath) {
 130         Vector<String> rv = new Vector<String>();
 131         Hashtable<String, String> v = (Hashtable<String, String>)BuildConfig.getField(this.toString(), "AdditionalGeneratedFile");
 132         if (v != null) {
 133             for (Enumeration<String> e=v.keys(); e.hasMoreElements(); ) {
 134                 String key = e.nextElement();
 135                 String val = v.get(key);
 136 
 137                 if (fullPath.endsWith(expandFormat(key))) {
 138                     rv.add(expandFormat(val));
 139                 }
 140             }
 141         }
 142         return rv;
 143     }
 144 
 145     void addTo(Hashtable ht, String key, String value) {
 146         ht.put(expandFormat(key), expandFormat(value));
 147     }
 148 
 149     void initDefaultDefines(Vector defines) {
 150         Vector sysDefines = new Vector();
 151         sysDefines.add("WIN32");
 152         sysDefines.add("_WINDOWS");
 153         sysDefines.add("HOTSPOT_BUILD_USER=\\\""+System.getProperty("user.name")+"\\\"");
 154         sysDefines.add("HOTSPOT_BUILD_TARGET=\\\""+get("Build")+"\\\"");
 155         sysDefines.add("INCLUDE_TRACE");
 156         sysDefines.add("_JNI_IMPLEMENTATION_");
 157         if (vars.get("PlatformName").equals("Win32")) {
 158             sysDefines.add("HOTSPOT_LIB_ARCH=\\\"i386\\\"");
 159         } else {
 160             sysDefines.add("HOTSPOT_LIB_ARCH=\\\"amd64\\\"");
 161         }
 162 
 163         sysDefines.addAll(defines);
 164 
 165         put("Define", sysDefines);
 166     }
 167 
 168     String get(String key) {
 169         return (String)vars.get(key);
 170     }
 171 
 172     Vector getV(String key) {
 173         return (Vector)vars.get(key);
 174     }
 175 
 176     Object getO(String key) {
 177         return vars.get(key);
 178     }
 179 
 180     Hashtable getH(String key) {
 181         return (Hashtable)vars.get(key);
 182     }
 183 
 184     Object getFieldInContext(String field) {
 185         for (int i=0; i<context.length; i++) {
 186             Object rv = getField(context[i], field);
 187             if (rv != null) {
 188                 return rv;
 189             }
 190         }
 191         return null;
 192     }
 193 
 194     Object lookupHashFieldInContext(String field, String key) {
 195         for (int i=0; i<context.length; i++) {
 196             Hashtable ht = (Hashtable)getField(context[i], field);
 197             if (ht != null) {
 198                 Object rv = ht.get(key);
 199                 if (rv != null) {
 200                     return rv;
 201                 }
 202             }
 203         }
 204         return null;
 205     }
 206 
 207     void put(String key, String value) {
 208         vars.put(key, value);
 209     }
 210 
 211     void put(String key, Vector vvalue) {
 212         vars.put(key, vvalue);
 213     }
 214 
 215     void add(String key, Vector vvalue) {
 216         getV(key).addAll(vvalue);
 217     }
 218 
 219     String flavour() {
 220         return get("Flavour");
 221     }
 222 
 223     String build() {
 224         return get("Build");
 225     }
 226 
 227     Object getSpecificField(String field) {
 228         return getField(get("Id"), field);
 229     }
 230 
 231     void putSpecificField(String field, Object value) {
 232         putField(get("Id"), field, value);
 233     }
 234 
 235     void collectRelevantVectors(Vector rv, String field) {
 236         for (String ctx : context) {
 237             Vector<String> v = getFieldVector(ctx, field);
 238             if (v != null) {
 239                 for (String val : v) {
 240                     rv.add(expandFormat(val).replace('/', '\\'));
 241                 }
 242             }
 243         }
 244     }
 245 
 246     void collectRelevantHashes(Hashtable rv, String field) {
 247         for (String ctx : context) {
 248             Hashtable v = (Hashtable)getField(ctx, field);
 249             if (v != null) {
 250                 for (Enumeration e=v.keys(); e.hasMoreElements(); ) {
 251                     String key = (String)e.nextElement();
 252                     String val =  (String)v.get(key);
 253                     addTo(rv, key, val);
 254                 }
 255             }
 256         }
 257     }
 258 
 259 
 260     Vector getDefines() {
 261         Vector rv = new Vector();
 262         collectRelevantVectors(rv, "Define");
 263         return rv;
 264     }
 265 
 266     Vector getIncludes() {
 267         Vector rv = new Vector();
 268         collectRelevantVectors(rv, "AbsoluteInclude");
 269         rv.addAll(getSourceIncludes());
 270         return rv;
 271     }
 272 
 273     private Vector getSourceIncludes() {
 274         Vector<String> rv = new Vector<String>();
 275         Vector<String> ri = new Vector<String>();
 276         String sourceBase = getFieldString(null, "SourceBase");
 277         collectRelevantVectors(ri, "RelativeInclude");
 278         for (String f : ri) {
 279             rv.add(sourceBase + Util.sep + f);
 280         }
 281         return rv;
 282     }
 283 
 284     static Hashtable cfgData = new Hashtable();
 285     static Hashtable globalData = new Hashtable();
 286 
 287     static boolean appliesToTieredBuild(String cfg) {
 288         return (cfg != null &&
 289                 (cfg.startsWith("compiler1") ||
 290                  cfg.startsWith("compiler2")));
 291     }
 292 
 293     // Filters out the IgnoreFile and IgnorePaths since they are
 294     // handled specially for tiered builds.
 295     static boolean appliesToTieredBuild(String cfg, String key) {
 296         return (appliesToTieredBuild(cfg))&& (key != null && !key.startsWith("Ignore"));
 297     }
 298 
 299     static String getTieredBuildCfg(String cfg) {
 300         assert appliesToTieredBuild(cfg) : "illegal configuration " + cfg;
 301         return "tiered" + cfg.substring(9);
 302     }
 303 
 304     static Object getField(String cfg, String field) {
 305         if (cfg == null) {
 306             return globalData.get(field);
 307         }
 308 
 309         Hashtable ht =  (Hashtable)cfgData.get(cfg);
 310         return ht == null ? null : ht.get(field);
 311     }
 312 
 313     static String getFieldString(String cfg, String field) {
 314         return (String)getField(cfg, field);
 315     }
 316 
 317     static Vector getFieldVector(String cfg, String field) {
 318         return (Vector)getField(cfg, field);
 319     }
 320 
 321     static void putField(String cfg, String field, Object value) {
 322         putFieldImpl(cfg, field, value);
 323         if (appliesToTieredBuild(cfg, field)) {
 324             putFieldImpl(getTieredBuildCfg(cfg), field, value);
 325         }
 326     }
 327 
 328     private static void putFieldImpl(String cfg, String field, Object value) {
 329         if (cfg == null) {
 330             globalData.put(field, value);
 331             return;
 332         }
 333 
 334         Hashtable ht = (Hashtable)cfgData.get(cfg);
 335         if (ht == null) {
 336             ht = new Hashtable();
 337             cfgData.put(cfg, ht);
 338         }
 339 
 340         ht.put(field, value);
 341     }
 342 
 343     static Object getFieldHash(String cfg, String field, String name) {
 344         Hashtable ht = (Hashtable)getField(cfg, field);
 345 
 346         return ht == null ? null : ht.get(name);
 347     }
 348 
 349     static void putFieldHash(String cfg, String field, String name, Object val) {
 350         putFieldHashImpl(cfg, field, name, val);
 351         if (appliesToTieredBuild(cfg, field)) {
 352             putFieldHashImpl(getTieredBuildCfg(cfg), field, name, val);
 353         }
 354     }
 355 
 356     private static void putFieldHashImpl(String cfg, String field, String name, Object val) {
 357         Hashtable ht = (Hashtable)getField(cfg, field);
 358 
 359         if (ht == null) {
 360             ht = new Hashtable();
 361             putFieldImpl(cfg, field, ht);
 362         }
 363 
 364         ht.put(name, val);
 365     }
 366 
 367     static void addFieldVector(String cfg, String field, String element) {
 368         addFieldVectorImpl(cfg, field, element);
 369         if (appliesToTieredBuild(cfg, field)) {
 370             addFieldVectorImpl(getTieredBuildCfg(cfg), field, element);
 371         }
 372     }
 373 
 374     private static void addFieldVectorImpl(String cfg, String field, String element) {
 375         Vector v = (Vector)getField(cfg, field);
 376 
 377         if (v == null) {
 378             v = new Vector();
 379             putFieldImpl(cfg, field, v);
 380         }
 381 
 382         v.add(element);
 383     }
 384 
 385     String expandFormat(String format) {
 386         if (format == null) {
 387             return null;
 388         }
 389 
 390         if (format.indexOf('%') == -1) {
 391             return format;
 392         }
 393 
 394         StringBuffer sb = new StringBuffer();
 395         int len = format.length();
 396         for (int i=0; i<len; i++) {
 397             char ch = format.charAt(i);
 398             if (ch == '%') {
 399                 char ch1 = format.charAt(i+1);
 400                 switch (ch1) {
 401                 case '%':
 402                     sb.append(ch1);
 403                     break;
 404                 case 'b':
 405                     sb.append(build());
 406                     break;
 407                 case 'f':
 408                     sb.append(flavour());
 409                     break;
 410                 default:
 411                     sb.append(ch);
 412                     sb.append(ch1);
 413                 }
 414                 i++;
 415             } else {
 416                 sb.append(ch);
 417             }
 418         }
 419 
 420         return sb.toString();
 421     }
 422 }
 423 
 424 abstract class GenericDebugConfig extends BuildConfig {
 425     abstract String getOptFlag();
 426 
 427     protected void init(Vector includes, Vector defines) {
 428         defines.add("_DEBUG");
 429         defines.add("ASSERT");
 430 
 431         super.init(includes, defines);
 432 
 433         getV("CompilerFlags").addAll(getCI().getDebugCompilerFlags(getOptFlag()));
 434         getV("LinkerFlags").addAll(getCI().getDebugLinkerFlags());
 435    }
 436 }
 437 
 438 abstract class GenericDebugNonKernelConfig extends GenericDebugConfig {
 439     protected void init(Vector includes, Vector defines) {
 440         super.init(includes, defines);
 441         getCI().getAdditionalNonKernelLinkerFlags(getV("LinkerFlags"));
 442    }
 443 }
 444 
 445 class C1DebugConfig extends GenericDebugNonKernelConfig {
 446     String getOptFlag() {
 447         return getCI().getNoOptFlag();
 448     }
 449 
 450     C1DebugConfig() {
 451         initNames("compiler1", "debug", "jvm.dll");
 452         init(getIncludes(), getDefines());
 453     }
 454 }
 455 
 456 class C1FastDebugConfig extends GenericDebugNonKernelConfig {
 457     String getOptFlag() {
 458         return getCI().getOptFlag();
 459     }
 460 
 461     C1FastDebugConfig() {
 462         initNames("compiler1", "fastdebug", "jvm.dll");
 463         init(getIncludes(), getDefines());
 464     }
 465 }
 466 
 467 class C2DebugConfig extends GenericDebugNonKernelConfig {
 468     String getOptFlag() {
 469         return getCI().getNoOptFlag();
 470     }
 471 
 472     C2DebugConfig() {
 473         initNames("compiler2", "debug", "jvm.dll");
 474         init(getIncludes(), getDefines());
 475     }
 476 }
 477 
 478 class C2FastDebugConfig extends GenericDebugNonKernelConfig {
 479     String getOptFlag() {
 480         return getCI().getOptFlag();
 481     }
 482 
 483     C2FastDebugConfig() {
 484         initNames("compiler2", "fastdebug", "jvm.dll");
 485         init(getIncludes(), getDefines());
 486     }
 487 }
 488 
 489 class TieredDebugConfig extends GenericDebugNonKernelConfig {
 490     String getOptFlag() {
 491         return getCI().getNoOptFlag();
 492     }
 493 
 494     TieredDebugConfig() {
 495         initNames("tiered", "debug", "jvm.dll");
 496         init(getIncludes(), getDefines());
 497     }
 498 }
 499 
 500 class TieredFastDebugConfig extends GenericDebugNonKernelConfig {
 501     String getOptFlag() {
 502         return getCI().getOptFlag();
 503     }
 504 
 505     TieredFastDebugConfig() {
 506         initNames("tiered", "fastdebug", "jvm.dll");
 507         init(getIncludes(), getDefines());
 508     }
 509 }
 510 
 511 abstract class ProductConfig extends BuildConfig {
 512     protected void init(Vector includes, Vector defines) {
 513         defines.add("NDEBUG");
 514         defines.add("PRODUCT");
 515 
 516         super.init(includes, defines);
 517 
 518         getV("CompilerFlags").addAll(getCI().getProductCompilerFlags());
 519         getV("LinkerFlags").addAll(getCI().getProductLinkerFlags());
 520     }
 521 }
 522 
 523 class C1ProductConfig extends ProductConfig {
 524     C1ProductConfig() {
 525         initNames("compiler1", "product", "jvm.dll");
 526         init(getIncludes(), getDefines());
 527     }
 528 }
 529 
 530 class C2ProductConfig extends ProductConfig {
 531     C2ProductConfig() {
 532         initNames("compiler2", "product", "jvm.dll");
 533         init(getIncludes(), getDefines());
 534     }
 535 }
 536 
 537 class TieredProductConfig extends ProductConfig {
 538     TieredProductConfig() {
 539         initNames("tiered", "product", "jvm.dll");
 540         init(getIncludes(), getDefines());
 541     }
 542 }
 543 
 544 class CoreDebugConfig extends GenericDebugNonKernelConfig {
 545     String getOptFlag() {
 546         return getCI().getNoOptFlag();
 547     }
 548 
 549     CoreDebugConfig() {
 550         initNames("core", "debug", "jvm.dll");
 551         init(getIncludes(), getDefines());
 552     }
 553 }
 554 
 555 class CoreFastDebugConfig extends GenericDebugNonKernelConfig {
 556     String getOptFlag() {
 557         return getCI().getOptFlag();
 558     }
 559 
 560     CoreFastDebugConfig() {
 561         initNames("core", "fastdebug", "jvm.dll");
 562         init(getIncludes(), getDefines());
 563     }
 564 }
 565 
 566 class CoreProductConfig extends ProductConfig {
 567     CoreProductConfig() {
 568         initNames("core", "product", "jvm.dll");
 569         init(getIncludes(), getDefines());
 570     }
 571 }
 572 
 573 
 574 abstract class CompilerInterface {
 575     abstract Vector getBaseCompilerFlags(Vector defines, Vector includes, String outDir);
 576     abstract Vector getBaseLinkerFlags(String outDir, String outDll, String platformName);
 577     abstract Vector getDebugCompilerFlags(String opt);
 578     abstract Vector getDebugLinkerFlags();
 579     abstract void   getAdditionalNonKernelLinkerFlags(Vector rv);
 580     abstract Vector getProductCompilerFlags();
 581     abstract Vector getProductLinkerFlags();
 582     abstract String getOptFlag();
 583     abstract String getNoOptFlag();
 584     abstract String makeCfgName(String flavourBuild, String platformName);
 585 
 586     void addAttr(Vector receiver, String attr, String value) {
 587         receiver.add(attr); receiver.add(value);
 588     }
 589     void extAttr(Vector receiver, String attr, String value) {
 590         int attr_pos=receiver.indexOf(attr) ;
 591         if ( attr_pos == -1) {
 592           // If attr IS NOT present in the Vector - add it
 593           receiver.add(attr); receiver.add(value);
 594         } else {
 595           // If attr IS present in the Vector - append value to it
 596           receiver.set(attr_pos+1,receiver.get(attr_pos+1)+value);
 597         }
 598     }
 599 }