BuildConfig.java 21.7 KB
Newer Older
D
duke 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
/*
 * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 *
 */

import java.util.*;
import java.io.File;

class BuildConfig {
    Hashtable vars;
    Vector basicNames, basicPaths;
    String[] context;

    static CompilerInterface ci;
    static CompilerInterface getCI() {
        if (ci == null) {
            String comp = (String)getField(null, "CompilerVersion");
            try {
                ci = (CompilerInterface)Class.forName("CompilerInterface" + comp).newInstance();
            } catch (Exception cnfe) {
                System.err.println("Cannot find support for compiler " + comp);
                throw new RuntimeException(cnfe.toString());
            }
        }
        return ci;
    }

    protected void initNames(String flavour, String build, String outDll) {
        if (vars == null) vars = new Hashtable();

        String flavourBuild =  flavour + "_" + build;
        put("Name", getCI().makeCfgName(flavourBuild));
        put("Flavour", flavour);
        put("Build", build);

        // ones mentioned above were needed to expand format
        String buildBase = expandFormat(getFieldString(null, "BuildBase"));
        String jdkDir =  getFieldString(null, "JdkTargetRoot");
        String sourceBase = getFieldString(null, "SourceBase");
        String outDir = buildBase;

        put("Id", flavourBuild);
        put("OutputDir", outDir);
        put("SourceBase", sourceBase);
        put("BuildBase", buildBase);
        put("OutputDll", jdkDir + Util.sep + outDll);

        context = new String [] {flavourBuild, flavour, build, null};
    }

    protected void init(Vector includes, Vector defines) {
        initDefaultDefines(defines);
        initDefaultCompilerFlags(includes);
        initDefaultLinkerFlags();
        handleDB((String)getFieldInContext("IncludeDB"));
    }


    protected void initDefaultCompilerFlags(Vector includes) {
        Vector compilerFlags = new Vector();

        compilerFlags.addAll(getCI().getBaseCompilerFlags(getV("Define"),
                                                          includes,
                                                          get("OutputDir")));

        put("CompilerFlags", compilerFlags);
    }

    protected void initDefaultLinkerFlags() {
        Vector linkerFlags = new Vector();

        linkerFlags.addAll(getCI().getBaseLinkerFlags( get("OutputDir"), get("OutputDll")));

        put("LinkerFlags", linkerFlags);
    }

    DirectoryTree getSourceTree(String sourceBase, String startAt) {
        DirectoryTree tree = new DirectoryTree();

        tree.addSubdirToIgnore("Codemgr_wsdata");
        tree.addSubdirToIgnore("deleted_files");
        tree.addSubdirToIgnore("SCCS");
        tree.setVerbose(true);
        if (startAt != null) {
            tree.readDirectory(sourceBase + File.separator + startAt);
        } else {
            tree.readDirectory(sourceBase);
        }

        return tree;
    }


    Vector getPreferredPaths(Database currentDB) {
        Vector preferredPaths = new Vector();
        // In the case of multiple files with the same name in
        // different subdirectories, prefer the versions specified in
        // the platform file as the "os_family" and "arch" macros.
        for (Iterator iter = currentDB.getMacros(); iter.hasNext(); ) {
            Macro macro = (Macro) iter.next();
            if (macro.name.equals("os_family") ||
                macro.name.equals("arch")) {
                preferredPaths.add(macro.contents);
            }
        }
        // Also prefer "opto" over "adlc" for adlcVMDeps.hpp
        preferredPaths.add("opto");

        return preferredPaths;
    }


    void handleDB(String dbFile) {
        WinGammaPlatform platform = (WinGammaPlatform)getField(null, "PlatformObject");
        Database db = new Database(platform, platform.defaultGrandIncludeThreshold());

        try {
            File incls = new File(get("OutputDir")+Util.sep+"incls");
            FileName oldInclTempl = platform.getInclFileTemplate();
            FileName oldGITempl = platform.getGIFileTemplate();
            FileName oldGDTempl = platform.getGDFileTemplate();

            platform.setInclFileTemplate(new FileName(platform, incls.getPath()+Util.sep,
                                                      "_", "", ".incl", "", ""));
            platform.setGIFileTemplate(new FileName(platform, incls.getPath()+Util.sep,
                                                    "",  "_precompiled", ".incl", "", ""));

            incls.mkdirs();

            db.get(getFieldString(null, "Platform"), dbFile);
            db.compute();

            db.put();

            //platform.setInclFileTemplate(oldInclTempl);
            //platform.setGIFileTemplate(oldInclTempl);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("cannot do db: "+e);
        }

        putSpecificField("AllFilesHash", computeAllFiles(platform, db));
    }


    void addAll(Iterator i, Hashtable hash,
                WinGammaPlatform platform, DirectoryTree tree,
                Vector preferredPaths, Vector filesNotFound, Vector filesDuplicate) {
        for (; i.hasNext(); ) {
            String fileName = (String) i.next();
            if (lookupHashFieldInContext("IgnoreFile", fileName) == null) {
                String prefixedName = platform.envVarPrefixedFileName(fileName,
                                                                      0, /* ignored */
                                                                      tree,
                                                                      preferredPaths,
                                                                      filesNotFound,
                                                                      filesDuplicate);
                if (prefixedName != null) {
                    addTo(hash, Util.normalize(prefixedName), fileName);
                }
            }
        }
    }

    void addTo(Hashtable ht, String key, String value) {
        ht.put(expandFormat(key), expandFormat(value));
    }

    Hashtable computeAllFiles(WinGammaPlatform platform, Database db) {
        Hashtable rv = new Hashtable();
        DirectoryTree tree = getSourceTree(get("SourceBase"), getFieldString(null, "StartAt"));
        Vector preferredPaths = getPreferredPaths(db);

        // Hold errors until end
        Vector filesNotFound = new Vector();
        Vector filesDuplicate = new Vector();


        // find all files
        Vector dbFiles = new Vector();
        for (Iterator i=db.getAllFiles().iterator(); i.hasNext(); ) {
            FileList fl = (FileList) i.next();
            dbFiles.add(fl.getName());
        }
        addAll(dbFiles.iterator(), rv,
               platform, tree,
               preferredPaths, filesNotFound, filesDuplicate);

        Vector addFiles = new Vector();
        collectRelevantVectors(addFiles, "AdditionalFile");
        addAll(addFiles.iterator(), rv,
               platform, tree,
               preferredPaths, filesNotFound, filesDuplicate);

        collectRelevantHashes(rv, "AdditionalGeneratedFile");

        if ((filesNotFound.size() != 0) ||
            (filesDuplicate.size() != 0)) {
            System.err.println("Error: some files were not found or " +
                               "appeared in multiple subdirectories of " +
                               "directory " + get("SourceBase") + " and could not " +
                               "be resolved with the os_family and arch " +
                               "macros in the platform file.");
            if (filesNotFound.size() != 0) {
                System.err.println("Files not found:");
                for (Iterator iter = filesNotFound.iterator();
                     iter.hasNext(); ) {
                    System.err.println("  " + (String) iter.next());
                }
            }
            if (filesDuplicate.size() != 0) {
                System.err.println("Duplicate files:");
                for (Iterator iter = filesDuplicate.iterator();
                     iter.hasNext(); ) {
                    System.err.println("  " + (String) iter.next());
                }
            }
            throw new RuntimeException();
        }

        return rv;
    }

    void initDefaultDefines(Vector defines) {
        Vector sysDefines = new Vector();
        sysDefines.add("WIN32");
        sysDefines.add("_WINDOWS");
        sysDefines.add("HOTSPOT_BUILD_USER="+System.getProperty("user.name"));
        sysDefines.add("HOTSPOT_BUILD_TARGET=\\\""+get("Build")+"\\\"");
        sysDefines.add("_JNI_IMPLEMENTATION_");
250
        sysDefines.add("HOTSPOT_LIB_ARCH=\\\"i386\\\"");
D
duke 已提交
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707

        sysDefines.addAll(defines);

        put("Define", sysDefines);
    }

    String get(String key) {
        return (String)vars.get(key);
    }

    Vector getV(String key) {
        return (Vector)vars.get(key);
    }

    Object getO(String key) {
        return vars.get(key);
    }

    Hashtable getH(String key) {
        return (Hashtable)vars.get(key);
    }

    Object getFieldInContext(String field) {
        for (int i=0; i<context.length; i++) {
            Object rv = getField(context[i], field);
            if (rv != null) {
                return rv;
            }
        }
        return null;
    }

    Object lookupHashFieldInContext(String field, String key) {
        for (int i=0; i<context.length; i++) {
            Hashtable ht = (Hashtable)getField(context[i], field);
            if (ht != null) {
                Object rv = ht.get(key);
                if (rv != null) {
                    return rv;
                }
            }
        }
        return null;
    }

    void put(String key, String value) {
        vars.put(key, value);
    }

    void put(String key, Vector vvalue) {
        vars.put(key, vvalue);
    }

    void add(String key, Vector vvalue) {
        getV(key).addAll(vvalue);
    }

    String flavour() {
        return get("Flavour");
    }

    String build() {
        return get("Build");
    }

    Object getSpecificField(String field) {
        return getField(get("Id"), field);
    }

    void putSpecificField(String field, Object value) {
        putField(get("Id"), field, value);
    }

    void collectRelevantVectors(Vector rv, String field) {
        for (int i = 0; i < context.length; i++) {
            Vector v = getFieldVector(context[i], field);
            if (v != null) {
                for (Iterator j=v.iterator(); j.hasNext(); ) {
                    String val = (String)j.next();
                    rv.add(expandFormat(val));
                }
            }
        }
    }

    void collectRelevantHashes(Hashtable rv, String field) {
        for (int i = 0; i < context.length; i++) {
            Hashtable v = (Hashtable)getField(context[i], field);
            if (v != null) {
                for (Enumeration e=v.keys(); e.hasMoreElements(); ) {
                    String key = (String)e.nextElement();
                    String val =  (String)v.get(key);
                    addTo(rv, key, val);
                }
            }
        }
    }


    Vector getDefines() {
        Vector rv = new Vector();
        collectRelevantVectors(rv, "Define");
        return rv;
    }

    Vector getIncludes() {
        Vector rv = new Vector();

        // for generated includes
        rv.add(get("OutputDir"));

        collectRelevantVectors(rv, "AbsoluteInclude");

        Vector ri = new Vector();
        String sourceBase = getFieldString(null, "SourceBase");
        collectRelevantVectors(ri, "RelativeInclude");
        for (Iterator i = ri.iterator(); i.hasNext(); ) {
            String f = (String)i.next();
            rv.add(sourceBase + Util.sep + f);
        }

        return rv;
    }

    static Hashtable cfgData = new Hashtable();
    static Hashtable globalData = new Hashtable();

    static boolean appliesToTieredBuild(String cfg) {
        return (cfg != null &&
                (cfg.startsWith("compiler1") ||
                 cfg.startsWith("compiler2")));
    }

    // Filters out the IncludeDB statement, which is the only command-
    // line argument we explicitly specialize for the tiered build
    static boolean appliesToTieredBuild(String cfg, String key) {
        return (appliesToTieredBuild(cfg) &&
                (key != null &&
                 !key.equals("IncludeDB")));
    }

    static String getTieredBuildCfg(String cfg) {
        assert appliesToTieredBuild(cfg) : "illegal configuration " + cfg;
        return "tiered" + cfg.substring(9);
    }

    static Object getField(String cfg, String field) {
        if (cfg == null) {
            return globalData.get(field);
        }

        Hashtable ht =  (Hashtable)cfgData.get(cfg);
        return ht == null ? null : ht.get(field);
    }

    static String getFieldString(String cfg, String field) {
        return (String)getField(cfg, field);
    }

    static Vector getFieldVector(String cfg, String field) {
        return (Vector)getField(cfg, field);
    }

    static void putField(String cfg, String field, Object value) {
        putFieldImpl(cfg, field, value);
        if (appliesToTieredBuild(cfg, field)) {
            putFieldImpl(getTieredBuildCfg(cfg), field, value);
        }
    }

    private static void putFieldImpl(String cfg, String field, Object value) {
        if (cfg == null) {
            globalData.put(field, value);
            return;
        }

        Hashtable ht = (Hashtable)cfgData.get(cfg);
        if (ht == null) {
            ht = new Hashtable();
            cfgData.put(cfg, ht);
        }

        ht.put(field, value);
    }

    static Object getFieldHash(String cfg, String field, String name) {
        Hashtable ht = (Hashtable)getField(cfg, field);

        return ht == null ? null : ht.get(name);
    }

    static void putFieldHash(String cfg, String field, String name, Object val) {
        putFieldHashImpl(cfg, field, name, val);
        if (appliesToTieredBuild(cfg)) {
            putFieldHashImpl(getTieredBuildCfg(cfg), field, name, val);
        }
    }

    private static void putFieldHashImpl(String cfg, String field, String name, Object val) {
        Hashtable ht = (Hashtable)getField(cfg, field);

        if (ht == null) {
            ht = new Hashtable();
            putFieldImpl(cfg, field, ht);
        }

        ht.put(name, val);
    }

    static void addFieldVector(String cfg, String field, String element) {
        addFieldVectorImpl(cfg, field, element);
        if (appliesToTieredBuild(cfg)) {
            addFieldVectorImpl(getTieredBuildCfg(cfg), field, element);
        }
    }

    private static void addFieldVectorImpl(String cfg, String field, String element) {
        Vector v = (Vector)getField(cfg, field);

        if (v == null) {
            v = new Vector();
            putFieldImpl(cfg, field, v);
        }

        v.add(element);
    }

    String expandFormat(String format) {
        if (format == null) {
            return null;
        }

        if (format.indexOf('%') == -1) {
            return format;
        }

        StringBuffer sb = new StringBuffer();
        int len = format.length();
        for (int i=0; i<len; i++) {
            char ch = format.charAt(i);
            if (ch == '%') {
                char ch1 = format.charAt(i+1);
                switch (ch1) {
                case '%':
                    sb.append(ch1);
                    break;
                case 'b':
                    sb.append(build());
                    break;
                case 'f':
                    sb.append(flavour());
                    break;
                default:
                    sb.append(ch);
                    sb.append(ch1);
                }
                i++;
            } else {
                sb.append(ch);
            }
        }

        return sb.toString();
    }
}

abstract class GenericDebugConfig extends BuildConfig {
    abstract String getOptFlag();

    protected void init(Vector includes, Vector defines) {
        defines.add("_DEBUG");
        defines.add("ASSERT");

        super.init(includes, defines);

        getV("CompilerFlags").addAll(getCI().getDebugCompilerFlags(getOptFlag()));
        getV("LinkerFlags").addAll(getCI().getDebugLinkerFlags());
   }
}

class C1DebugConfig extends GenericDebugConfig {
    String getOptFlag() {
        return getCI().getNoOptFlag();
    }

    C1DebugConfig() {
        initNames("compiler1", "debug", "fastdebug\\jre\\bin\\client\\jvm.dll");
        init(getIncludes(), getDefines());
    }
}

class C1FastDebugConfig extends GenericDebugConfig {
    String getOptFlag() {
        return getCI().getOptFlag();
    }

    C1FastDebugConfig() {
        initNames("compiler1", "fastdebug", "fastdebug\\jre\\bin\\client\\jvm.dll");
        init(getIncludes(), getDefines());
    }
}

class C2DebugConfig extends GenericDebugConfig {
    String getOptFlag() {
        return getCI().getNoOptFlag();
    }

    C2DebugConfig() {
        initNames("compiler2", "debug", "fastdebug\\jre\\bin\\server\\jvm.dll");
        init(getIncludes(), getDefines());
    }
}

class C2FastDebugConfig extends GenericDebugConfig {
    String getOptFlag() {
        return getCI().getOptFlag();
    }

    C2FastDebugConfig() {
        initNames("compiler2", "fastdebug", "fastdebug\\jre\\bin\\server\\jvm.dll");
        init(getIncludes(), getDefines());
    }
}

class TieredDebugConfig extends GenericDebugConfig {
    String getOptFlag() {
        return getCI().getNoOptFlag();
    }

    TieredDebugConfig() {
        initNames("tiered", "debug", "fastdebug\\jre\\bin\\server\\jvm.dll");
        init(getIncludes(), getDefines());
    }
}

class TieredFastDebugConfig extends GenericDebugConfig {
    String getOptFlag() {
        return getCI().getOptFlag();
    }

    TieredFastDebugConfig() {
        initNames("tiered", "fastdebug", "fastdebug\\jre\\bin\\server\\jvm.dll");
        init(getIncludes(), getDefines());
    }
}


abstract class ProductConfig extends BuildConfig {
    protected void init(Vector includes, Vector defines) {
        defines.add("NDEBUG");
        defines.add("PRODUCT");

        super.init(includes, defines);

        getV("CompilerFlags").addAll(getCI().getProductCompilerFlags());
        getV("LinkerFlags").addAll(getCI().getProductLinkerFlags());
    }
}

class C1ProductConfig extends ProductConfig {
    C1ProductConfig() {
        initNames("compiler1", "product", "jre\\bin\\client\\jvm.dll");
        init(getIncludes(), getDefines());
    }
}

class C2ProductConfig extends ProductConfig {
    C2ProductConfig() {
        initNames("compiler2", "product", "jre\\bin\\server\\jvm.dll");
        init(getIncludes(), getDefines());
    }
}

class TieredProductConfig extends ProductConfig {
    TieredProductConfig() {
        initNames("tiered", "product", "jre\\bin\\server\\jvm.dll");
        init(getIncludes(), getDefines());
    }
}


class CoreDebugConfig extends GenericDebugConfig {
    String getOptFlag() {
        return getCI().getNoOptFlag();
    }

    CoreDebugConfig() {
        initNames("core", "debug", "fastdebug\\jre\\bin\\core\\jvm.dll");
        init(getIncludes(), getDefines());
    }
}


class CoreFastDebugConfig extends GenericDebugConfig {
    String getOptFlag() {
        return getCI().getOptFlag();
    }

    CoreFastDebugConfig() {
        initNames("core", "fastdebug", "fastdebug\\jre\\bin\\core\\jvm.dll");
        init(getIncludes(), getDefines());
    }
}


class CoreProductConfig extends ProductConfig {
    CoreProductConfig() {
        initNames("core", "product", "jre\\bin\\core\\jvm.dll");
        init(getIncludes(), getDefines());
    }
}

class KernelDebugConfig extends GenericDebugConfig {
    String getOptFlag() {
        return getCI().getNoOptFlag();
    }

    KernelDebugConfig() {
        initNames("kernel", "debug", "fastdebug\\jre\\bin\\kernel\\jvm.dll");
        init(getIncludes(), getDefines());
    }
}


class KernelFastDebugConfig extends GenericDebugConfig {
    String getOptFlag() {
        return getCI().getOptFlag();
    }

    KernelFastDebugConfig() {
        initNames("kernel", "fastdebug", "fastdebug\\jre\\bin\\kernel\\jvm.dll");
        init(getIncludes(), getDefines());
    }
}


class KernelProductConfig extends ProductConfig {
    KernelProductConfig() {
        initNames("kernel", "product", "jre\\bin\\kernel\\jvm.dll");
        init(getIncludes(), getDefines());
    }
}
abstract class CompilerInterface {
    abstract Vector getBaseCompilerFlags(Vector defines, Vector includes, String outDir);
    abstract Vector getBaseLinkerFlags(String outDir, String outDll);
    abstract Vector getDebugCompilerFlags(String opt);
    abstract Vector getDebugLinkerFlags();
    abstract Vector getProductCompilerFlags();
    abstract Vector getProductLinkerFlags();
    abstract String getOptFlag();
    abstract String getNoOptFlag();
    abstract String makeCfgName(String flavourBuild);

    void addAttr(Vector receiver, String attr, String value) {
        receiver.add(attr); receiver.add(value);
    }
}