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 
  69         put("Id", flavourBuild);
  70         put("OutputDir", outDir);
  71         put("SourceBase", sourceBase);
  72         put("BuildBase", buildBase);
  73         put("BuildSpace", buildSpace);
  74         put("OutputDll", outDir + Util.sep + outDll);
  75 
  76         context = new String [] {flavourBuild, flavour, build, null};
  77     }
  78 
  79     protected void init(Vector<String> includes, Vector<String> defines) {
  80         initDefaultDefines(defines);
  81         initDefaultCompilerFlags(includes);
  82         initDefaultLinkerFlags();
  83         //handleDB();
  84     }
  85 
  86 
  87     protected void initDefaultCompilerFlags(Vector<String> includes) {
  88         Vector compilerFlags = new Vector();
  89 
  90         compilerFlags.addAll(getCI().getBaseCompilerFlags(getV("Define"),
  91                                                           includes,
  92                                                           get("OutputDir")));
  93 
  94         put("CompilerFlags", compilerFlags);
  95     }
  96 
  97     protected void initDefaultLinkerFlags() {
  98         Vector linkerFlags = new Vector();
  99 
 100         linkerFlags.addAll(getCI().getBaseLinkerFlags( get("OutputDir"), get("OutputDll"), get("PlatformName")));
 101 
 102         put("LinkerFlags", linkerFlags);
 103     }
 104 
 105     public boolean matchesIgnoredPath(String path) {
 106         Vector<String> rv = new Vector<String>();
 107         collectRelevantVectors(rv, "IgnorePath");
 108         for (String pathPart : rv) {
 109             if (path.contains(pathPart))  {
 110                 return true;
 111             }
 112         }
 113         return false;
 114     }
 115     
 116     public boolean matchesHidePath(String path) {
 117         Vector<String> rv = new Vector<String>();
 118         collectRelevantVectors(rv, "HidePath");
 119         for (String pathPart : rv) {
 120             if (path.contains(Util.normalize(pathPart)))  {
 121                 return true;
 122             }
 123         }
 124         return false;
 125     }
 126     
 127    public Vector<String> matchesAdditionalGeneratedPath(String fullPath) {
 128             Vector<String> rv = new Vector<String>();
 129         Hashtable<String, String> v = (Hashtable<String, String>)BuildConfig.getField(this.toString(), "AdditionalGeneratedFile");
 130         if (v != null) {
 131             for (Enumeration<String> e=v.keys(); e.hasMoreElements(); ) {
 132                 String key = e.nextElement();
 133                 String val = v.get(key);
 134 
 135                 if (fullPath.endsWith(expandFormat(key))) {
 136                         rv.add(expandFormat(val));
 137                 }                
 138             }
 139         }
 140         return rv;
 141     }
 142 
 143     void addTo(Hashtable ht, String key, String value) {
 144         ht.put(expandFormat(key), expandFormat(value));
 145     }
 146 
 147     void initDefaultDefines(Vector defines) {
 148         Vector sysDefines = new Vector();
 149         sysDefines.add("WIN32");
 150         sysDefines.add("_WINDOWS");
 151         sysDefines.add("HOTSPOT_BUILD_USER=\\\""+System.getProperty("user.name")+"\\\"");
 152         sysDefines.add("HOTSPOT_BUILD_TARGET=\\\""+get("Build")+"\\\"");
 153         sysDefines.add("INCLUDE_TRACE");
 154         sysDefines.add("_JNI_IMPLEMENTATION_");
 155         if (vars.get("PlatformName").equals("Win32")) {
 156             sysDefines.add("HOTSPOT_LIB_ARCH=\\\"i386\\\"");
 157         } else {
 158             sysDefines.add("HOTSPOT_LIB_ARCH=\\\"amd64\\\"");
 159         }
 160 
 161         sysDefines.addAll(defines);
 162 
 163         put("Define", sysDefines);
 164     }
 165 
 166     String get(String key) {
 167         return (String)vars.get(key);
 168     }
 169 
 170     Vector getV(String key) {
 171         return (Vector)vars.get(key);
 172     }
 173 
 174     Object getO(String key) {
 175         return vars.get(key);
 176     }
 177 
 178     Hashtable getH(String key) {
 179         return (Hashtable)vars.get(key);
 180     }
 181 
 182     Object getFieldInContext(String field) {
 183         for (int i=0; i<context.length; i++) {
 184             Object rv = getField(context[i], field);
 185             if (rv != null) {
 186                 return rv;
 187             }
 188         }
 189         return null;
 190     }
 191 
 192     Object lookupHashFieldInContext(String field, String key) {
 193         for (int i=0; i<context.length; i++) {
 194             Hashtable ht = (Hashtable)getField(context[i], field);
 195             if (ht != null) {
 196                 Object rv = ht.get(key);
 197                 if (rv != null) {
 198                     return rv;
 199                 }
 200             }
 201         }
 202         return null;
 203     }
 204 
 205     void put(String key, String value) {
 206         vars.put(key, value);
 207     }
 208 
 209     void put(String key, Vector vvalue) {
 210         vars.put(key, vvalue);
 211     }
 212 
 213     void add(String key, Vector vvalue) {
 214         getV(key).addAll(vvalue);
 215     }
 216 
 217     String flavour() {
 218         return get("Flavour");
 219     }
 220 
 221     String build() {
 222         return get("Build");
 223     }
 224 
 225     Object getSpecificField(String field) {
 226         return getField(get("Id"), field);
 227     }
 228 
 229     void putSpecificField(String field, Object value) {
 230         putField(get("Id"), field, value);
 231     }
 232 
 233     void collectRelevantVectors(Vector rv, String field) {
 234         for (String ctx : context) {
 235             Vector<String> v = getFieldVector(ctx, field);
 236             if (v != null) {
 237                 for (String val : v) {
 238                     rv.add(expandFormat(val).replace('/', '\\'));
 239                 }
 240             }
 241         }
 242     }
 243 
 244     Vector getDefines() {
 245         Vector rv = new Vector();
 246         collectRelevantVectors(rv, "Define");
 247         return rv;
 248     }
 249 
 250     Vector getIncludes() {
 251         Vector rv = new Vector();
 252         collectRelevantVectors(rv, "AbsoluteInclude");
 253         rv.addAll(getSourceIncludes());
 254         return rv;
 255     }
 256 
 257     private Vector getSourceIncludes() {
 258         Vector<String> rv = new Vector<String>();
 259         Vector<String> ri = new Vector<String>();
 260         String sourceBase = getFieldString(null, "SourceBase");
 261         collectRelevantVectors(ri, "RelativeInclude");
 262         for (String f : ri) {
 263             rv.add(sourceBase + Util.sep + f);
 264         }
 265         return rv;
 266     }
 267 
 268     static Hashtable cfgData = new Hashtable();
 269     static Hashtable globalData = new Hashtable();
 270 
 271     static boolean appliesToTieredBuild(String cfg) {
 272         return (cfg != null &&
 273                 (cfg.startsWith("compiler1") ||
 274                  cfg.startsWith("compiler2")));
 275     }
 276 
 277     // Filters out the IgnoreFile and IgnorePaths since they are
 278     // handled specially for tiered builds.
 279     static boolean appliesToTieredBuild(String cfg, String key) {
 280         return (appliesToTieredBuild(cfg))&& (key != null && !key.startsWith("Ignore"));
 281     }
 282 
 283     static String getTieredBuildCfg(String cfg) {
 284         assert appliesToTieredBuild(cfg) : "illegal configuration " + cfg;
 285         return "tiered" + cfg.substring(9);
 286     }
 287 
 288     static Object getField(String cfg, String field) {
 289         if (cfg == null) {
 290             return globalData.get(field);
 291         }
 292 
 293         Hashtable ht =  (Hashtable)cfgData.get(cfg);
 294         return ht == null ? null : ht.get(field);
 295     }
 296 
 297     static String getFieldString(String cfg, String field) {
 298         return (String)getField(cfg, field);
 299     }
 300 
 301     static Vector getFieldVector(String cfg, String field) {
 302         return (Vector)getField(cfg, field);
 303     }
 304 
 305     static void putField(String cfg, String field, Object value) {
 306         putFieldImpl(cfg, field, value);
 307         if (appliesToTieredBuild(cfg, field)) {
 308             putFieldImpl(getTieredBuildCfg(cfg), field, value);
 309         }
 310     }
 311 
 312     private static void putFieldImpl(String cfg, String field, Object value) {
 313         if (cfg == null) {
 314             globalData.put(field, value);
 315             return;
 316         }
 317 
 318         Hashtable ht = (Hashtable)cfgData.get(cfg);
 319         if (ht == null) {
 320             ht = new Hashtable();
 321             cfgData.put(cfg, ht);
 322         }
 323 
 324         ht.put(field, value);
 325     }
 326 
 327     static Object getFieldHash(String cfg, String field, String name) {
 328         Hashtable ht = (Hashtable)getField(cfg, field);
 329 
 330         return ht == null ? null : ht.get(name);
 331     }
 332 
 333     static void putFieldHash(String cfg, String field, String name, Object val) {
 334         putFieldHashImpl(cfg, field, name, val);
 335         if (appliesToTieredBuild(cfg, field)) {
 336             putFieldHashImpl(getTieredBuildCfg(cfg), field, name, val);
 337         }
 338     }
 339 
 340     private static void putFieldHashImpl(String cfg, String field, String name, Object val) {
 341         Hashtable ht = (Hashtable)getField(cfg, field);
 342 
 343         if (ht == null) {
 344             ht = new Hashtable();
 345             putFieldImpl(cfg, field, ht);
 346         }
 347 
 348         ht.put(name, val);
 349     }
 350 
 351     static void addFieldVector(String cfg, String field, String element) {
 352         addFieldVectorImpl(cfg, field, element);
 353         if (appliesToTieredBuild(cfg, field)) {
 354             addFieldVectorImpl(getTieredBuildCfg(cfg), field, element);
 355         }
 356     }
 357 
 358     private static void addFieldVectorImpl(String cfg, String field, String element) {
 359         Vector v = (Vector)getField(cfg, field);
 360 
 361         if (v == null) {
 362             v = new Vector();
 363             putFieldImpl(cfg, field, v);
 364         }
 365 
 366         v.add(element);
 367     }
 368 
 369     String expandFormat(String format) {
 370         if (format == null) {
 371             return null;
 372         }
 373 
 374         if (format.indexOf('%') == -1) {
 375             return format;
 376         }
 377 
 378         StringBuffer sb = new StringBuffer();
 379         int len = format.length();
 380         for (int i=0; i<len; i++) {
 381             char ch = format.charAt(i);
 382             if (ch == '%') {
 383                 char ch1 = format.charAt(i+1);
 384                 switch (ch1) {
 385                 case '%':
 386                     sb.append(ch1);
 387                     break;
 388                 case 'b':
 389                     sb.append(build());
 390                     break;
 391                 case 'f':
 392                     sb.append(flavour());
 393                     break;
 394                 default:
 395                     sb.append(ch);
 396                     sb.append(ch1);
 397                 }
 398                 i++;
 399             } else {
 400                 sb.append(ch);
 401             }
 402         }
 403 
 404         return sb.toString();
 405     }
 406 }
 407 
 408 abstract class GenericDebugConfig extends BuildConfig {
 409     abstract String getOptFlag();
 410 
 411     protected void init(Vector includes, Vector defines) {
 412         defines.add("_DEBUG");
 413         defines.add("ASSERT");
 414 
 415         super.init(includes, defines);
 416 
 417         getV("CompilerFlags").addAll(getCI().getDebugCompilerFlags(getOptFlag()));
 418         getV("LinkerFlags").addAll(getCI().getDebugLinkerFlags());
 419    }
 420 }
 421 
 422 abstract class GenericDebugNonKernelConfig extends GenericDebugConfig {
 423     protected void init(Vector includes, Vector defines) {
 424         super.init(includes, defines);
 425         getCI().getAdditionalNonKernelLinkerFlags(getV("LinkerFlags"));
 426    }
 427 }
 428 
 429 class C1DebugConfig extends GenericDebugNonKernelConfig {
 430     String getOptFlag() {
 431         return getCI().getNoOptFlag();
 432     }
 433 
 434     C1DebugConfig() {
 435         initNames("compiler1", "debug", "jvm.dll");
 436         init(getIncludes(), getDefines());
 437     }
 438 }
 439 
 440 class C1FastDebugConfig extends GenericDebugNonKernelConfig {
 441     String getOptFlag() {
 442         return getCI().getOptFlag();
 443     }
 444 
 445     C1FastDebugConfig() {
 446         initNames("compiler1", "fastdebug", "jvm.dll");
 447         init(getIncludes(), getDefines());
 448     }
 449 }
 450 
 451 class C2DebugConfig extends GenericDebugNonKernelConfig {
 452     String getOptFlag() {
 453         return getCI().getNoOptFlag();
 454     }
 455 
 456     C2DebugConfig() {
 457         initNames("compiler2", "debug", "jvm.dll");
 458         init(getIncludes(), getDefines());
 459     }
 460 }
 461 
 462 class C2FastDebugConfig extends GenericDebugNonKernelConfig {
 463     String getOptFlag() {
 464         return getCI().getOptFlag();
 465     }
 466 
 467     C2FastDebugConfig() {
 468         initNames("compiler2", "fastdebug", "jvm.dll");
 469         init(getIncludes(), getDefines());
 470     }
 471 }
 472 
 473 class TieredDebugConfig extends GenericDebugNonKernelConfig {
 474     String getOptFlag() {
 475         return getCI().getNoOptFlag();
 476     }
 477 
 478     TieredDebugConfig() {
 479         initNames("tiered", "debug", "jvm.dll");
 480         init(getIncludes(), getDefines());
 481     }
 482 }
 483 
 484 class TieredFastDebugConfig extends GenericDebugNonKernelConfig {
 485     String getOptFlag() {
 486         return getCI().getOptFlag();
 487     }
 488 
 489     TieredFastDebugConfig() {
 490         initNames("tiered", "fastdebug", "jvm.dll");
 491         init(getIncludes(), getDefines());
 492     }
 493 }
 494 
 495 abstract class ProductConfig extends BuildConfig {
 496     protected void init(Vector includes, Vector defines) {
 497         defines.add("NDEBUG");
 498         defines.add("PRODUCT");
 499 
 500         super.init(includes, defines);
 501 
 502         getV("CompilerFlags").addAll(getCI().getProductCompilerFlags());
 503         getV("LinkerFlags").addAll(getCI().getProductLinkerFlags());
 504     }
 505 }
 506 
 507 class C1ProductConfig extends ProductConfig {
 508     C1ProductConfig() {
 509         initNames("compiler1", "product", "jvm.dll");
 510         init(getIncludes(), getDefines());
 511     }
 512 }
 513 
 514 class C2ProductConfig extends ProductConfig {
 515     C2ProductConfig() {
 516         initNames("compiler2", "product", "jvm.dll");
 517         init(getIncludes(), getDefines());
 518     }
 519 }
 520 
 521 class TieredProductConfig extends ProductConfig {
 522     TieredProductConfig() {
 523         initNames("tiered", "product", "jvm.dll");
 524         init(getIncludes(), getDefines());
 525     }
 526 }
 527 
 528 /*
 529 class CoreDebugConfig extends GenericDebugNonKernelConfig {
 530     String getOptFlag() {
 531         return getCI().getNoOptFlag();
 532     }
 533 
 534     CoreDebugConfig() {
 535         initNames("core", "debug", "jvm.dll");
 536         init(getIncludes(), getDefines());
 537     }
 538 }
 539 
 540 class CoreFastDebugConfig extends GenericDebugNonKernelConfig {
 541     String getOptFlag() {
 542         return getCI().getOptFlag();
 543     }
 544 
 545     CoreFastDebugConfig() {
 546         initNames("core", "fastdebug", "jvm.dll");
 547         init(getIncludes(), getDefines());
 548     }
 549 }
 550 
 551 class CoreProductConfig extends ProductConfig {
 552     CoreProductConfig() {
 553         initNames("core", "product", "jvm.dll");
 554         init(getIncludes(), getDefines());
 555     }
 556 }*/
 557 
 558 class KernelDebugConfig extends GenericDebugConfig {
 559     String getOptFlag() {
 560         return getCI().getNoOptFlag();
 561     }
 562 
 563     KernelDebugConfig() {
 564         initNames("kernel", "debug", "jvm.dll");
 565         init(getIncludes(), getDefines());
 566     }
 567 }
 568 
 569 
 570 class KernelFastDebugConfig extends GenericDebugConfig {
 571     String getOptFlag() {
 572         return getCI().getOptFlag();
 573     }
 574 
 575     KernelFastDebugConfig() {
 576         initNames("kernel", "fastdebug", "jvm.dll");
 577         init(getIncludes(), getDefines());
 578     }
 579 }
 580 
 581 
 582 class KernelProductConfig extends ProductConfig {
 583     KernelProductConfig() {
 584         initNames("kernel", "product", "jvm.dll");
 585         init(getIncludes(), getDefines());
 586     }
 587 }
 588 
 589 abstract class CompilerInterface {
 590     abstract Vector getBaseCompilerFlags(Vector defines, Vector includes, String outDir);
 591     abstract Vector getBaseLinkerFlags(String outDir, String outDll, String platformName);
 592     abstract Vector getDebugCompilerFlags(String opt);
 593     abstract Vector getDebugLinkerFlags();
 594     abstract void   getAdditionalNonKernelLinkerFlags(Vector rv);
 595     abstract Vector getProductCompilerFlags();
 596     abstract Vector getProductLinkerFlags();
 597     abstract String getOptFlag();
 598     abstract String getNoOptFlag();
 599     abstract String makeCfgName(String flavourBuild, String platformName);
 600 
 601     void addAttr(Vector receiver, String attr, String value) {
 602         receiver.add(attr); receiver.add(value);
 603     }
 604     void extAttr(Vector receiver, String attr, String value) {
 605         int attr_pos=receiver.indexOf(attr) ;
 606         if ( attr_pos == -1) {
 607           // If attr IS NOT present in the Vector - add it
 608           receiver.add(attr); receiver.add(value);
 609         } else {
 610           // If attr IS present in the Vector - append value to it
 611           receiver.set(attr_pos+1,receiver.get(attr_pos+1)+value);
 612         }
 613     }
 614 }