WinGammaPlatform.java 24.9 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * 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.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24
 *
 */

25 26 27 28 29 30 31
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
N
neliasso 已提交
32
import java.util.Stack;
33 34
import java.util.TreeSet;
import java.util.Vector;
D
duke 已提交
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

abstract class HsArgHandler extends ArgHandler {
    static final int STRING = 1;
    static final int VECTOR = 2;
    static final int HASH   = 3;

    boolean nextNotKey(ArgIterator it) {
        if (it.next()) {
            String s = it.get();
            return (s.length() == 0) || (s.charAt(0) != '-');
        } else {
            return false;
        }
    }

    void empty(String key, String message) {
        if (key != null) {
            System.err.println("** Error: empty " + key);
        }
        if (message != null) {
            System.err.println(message);
        }
        WinGammaPlatform.usage();
    }

    static String getCfg(String val) {
        int under = val.indexOf('_');
        int len = val.length();
        if (under != -1 && under < len - 1) {
            return val.substring(under+1, len);
        } else {
            return null;
        }
    }
}

class ArgRuleSpecific extends ArgRule {
    ArgRuleSpecific(String arg, ArgHandler handler) {
        super(arg, handler);
    }

    boolean match(String rulePattern, String arg) {
        return rulePattern.startsWith(arg);
    }
}


class SpecificHsArgHandler extends HsArgHandler {

    String message, argKey, valKey;
    int type;

    public void handle(ArgIterator it) {
        String cfg = getCfg(it.get());
        if (nextNotKey(it)) {
            String val = it.get();
            switch (type) {
            case VECTOR:
                BuildConfig.addFieldVector(cfg, valKey, val);
                break;
            case HASH:
                BuildConfig.putFieldHash(cfg, valKey, val, "1");
                break;
            case STRING:
                BuildConfig.putField(cfg, valKey, val);
                break;
            default:
                empty(valKey, "Unknown type: "+type);
            }
            it.next();

        } else {
            empty(argKey, message);
        }
    }

    SpecificHsArgHandler(String argKey, String valKey, String message, int type) {
        this.argKey = argKey;
        this.valKey = valKey;
        this.message = message;
        this.type = type;
    }
}


class HsArgRule extends ArgRuleSpecific {

    HsArgRule(String argKey, String valKey, String message, int type) {
        super(argKey, new SpecificHsArgHandler(argKey, valKey, message, type));
    }

}

128
public abstract class WinGammaPlatform {
D
duke 已提交
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

    public boolean fileNameStringEquality(String s1, String s2) {
        return s1.equalsIgnoreCase(s2);
    }

    static void usage() throws IllegalArgumentException {
        System.err.println("WinGammaPlatform platform-specific options:");
        System.err.println("  -sourceBase <path to directory (workspace) " +
                           "containing source files; no trailing slash>");
        System.err.println("  -projectFileName <full pathname to which project file " +
                           "will be written; all parent directories must " +
                           "already exist>");
        System.err.println("  If any of the above are specified, "+
                           "they must all be.");
        System.err.println("  Additional, optional arguments, which can be " +
                           "specified multiple times:");
        System.err.println("    -absoluteInclude <string containing absolute " +
                           "path to include directory>");
        System.err.println("    -relativeInclude <string containing include " +
                           "directory relative to -sourceBase>");
        System.err.println("    -define <preprocessor flag to be #defined " +
                           "(note: doesn't yet support " +
                           "#define (flag) (value))>");
        System.err.println("    -startAt <subdir of sourceBase>");
        System.err.println("    -additionalFile <file not in database but " +
154
                           "which should show up in project file>");
D
duke 已提交
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 250 251 252 253 254 255 256 257 258 259
        System.err.println("    -additionalGeneratedFile <absolute path to " +
                           "directory containing file; no trailing slash> " +
                           "<name of file generated later in the build process>");
        throw new IllegalArgumentException();
    }


    public void addPerFileLine(Hashtable table,
                               String fileName,
                               String line) {
        Vector v = (Vector) table.get(fileName);
        if (v != null) {
            v.add(line);
        } else {
            v = new Vector();
            v.add(line);
            table.put(fileName, v);
        }
    }

    protected static class PerFileCondData {
        public String releaseString;
        public String debugString;
    }

    protected void addConditionalPerFileLine(Hashtable table,
                                           String fileName,
                                           String releaseLine,
                                           String debugLine) {
        PerFileCondData data = new PerFileCondData();
        data.releaseString = releaseLine;
        data.debugString = debugLine;
        Vector v = (Vector) table.get(fileName);
        if (v != null) {
            v.add(data);
        } else {
            v = new Vector();
            v.add(data);
            table.put(fileName, v);
        }
    }

    protected static class PrelinkCommandData {
      String description;
      String commands;
    }

    protected void addPrelinkCommand(Hashtable table,
                                     String build,
                                     String description,
                                     String commands) {
      PrelinkCommandData data = new PrelinkCommandData();
      data.description = description;
      data.commands = commands;
      table.put(build, data);
    }

    public boolean findString(Vector v, String s) {
        for (Iterator iter = v.iterator(); iter.hasNext(); ) {
            if (((String) iter.next()).equals(s)) {
                return true;
            }
        }

        return false;
    }

     String getProjectName(String fullPath, String extension)
        throws IllegalArgumentException, IOException {
        File file = new File(fullPath).getCanonicalFile();
        fullPath = file.getCanonicalPath();
        String parent = file.getParent();

        if (!fullPath.endsWith(extension)) {
            throw new IllegalArgumentException("project file name \"" +
                                               fullPath +
                                               "\" does not end in "+extension);
        }

        if ((parent != null) &&
            (!fullPath.startsWith(parent))) {
            throw new RuntimeException(
                "Internal error: parent of file name \"" + parent +
                "\" does not match file name \"" + fullPath + "\""
            );
        }

        int len = parent.length();
        if (!parent.endsWith(Util.sep)) {
            len += Util.sep.length();
        }

        int end = fullPath.length() - extension.length();

        if (len == end) {
            throw new RuntimeException(
                "Internal error: file name was empty"
            );
        }

        return fullPath.substring(len, end);
    }

    protected abstract String getProjectExt();

260
    public void createVcproj(String[] args)
D
duke 已提交
261 262 263 264 265 266 267 268 269
        throws IllegalArgumentException, IOException {

        parseArguments(args);

        String projectFileName = BuildConfig.getFieldString(null, "ProjectFileName");
        String ext = getProjectExt();

        String projectName = getProjectName(projectFileName, ext);

270
        writeProjectFile(projectFileName, projectName, createAllConfigs(BuildConfig.getFieldString(null, "PlatformName")));
D
duke 已提交
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
    }

    protected void writePrologue(String[] args) {
        System.err.println("WinGammaPlatform platform-specific arguments:");
        for (int i = 0; i < args.length; i++) {
            System.err.print(args[i] + " ");
        }
        System.err.println();
    }


    void parseArguments(String[] args) {
        new ArgsParser(args,
                       new ArgRule[]
            {
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
                new ArgRule("-sourceBase",
                            new HsArgHandler() {
                                public void handle(ArgIterator it) {
                                   String cfg = getCfg(it.get());
                                   if (nextNotKey(it)) {
                                      String sb = (String) it.get();
                                      if (sb.endsWith(Util.sep)) {
                                         sb = sb.substring(0, sb.length() - 1);
                                      }
                                      BuildConfig.putField(cfg, "SourceBase", sb);
                                      it.next();
                                   } else {
                                      empty("-sourceBase", null);
                                   }
                                }
                            }
                            ),
D
duke 已提交
303 304 305 306 307 308 309

                new HsArgRule("-buildBase",
                              "BuildBase",
                              "   (Did you set the HotSpotBuildSpace environment variable?)",
                              HsArgHandler.STRING
                              ),

N
neliasso 已提交
310 311 312 313 314 315
               new HsArgRule("-buildSpace",
                              "BuildSpace",
                              null,
                              HsArgHandler.STRING
                              ),

316 317 318 319 320 321 322
              new HsArgRule("-platformName",
                              "PlatformName",
                              null,
                              HsArgHandler.STRING
                              ),

              new HsArgRule("-projectFileName",
D
duke 已提交
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
                              "ProjectFileName",
                              null,
                              HsArgHandler.STRING
                              ),

                new HsArgRule("-jdkTargetRoot",
                              "JdkTargetRoot",
                              "   (Did you set the HotSpotJDKDist environment variable?)",
                              HsArgHandler.STRING
                              ),

                new HsArgRule("-compiler",
                              "CompilerVersion",
                              "   (Did you set the VcVersion correctly?)",
                              HsArgHandler.STRING
                              ),

                new HsArgRule("-absoluteInclude",
                              "AbsoluteInclude",
                              null,
                              HsArgHandler.VECTOR
                              ),

                new HsArgRule("-relativeInclude",
                              "RelativeInclude",
                              null,
                              HsArgHandler.VECTOR
                              ),

N
neliasso 已提交
352 353 354 355 356 357 358 359 360 361 362 363
                new HsArgRule("-absoluteSrcInclude",
                              "AbsoluteSrcInclude",
                              null,
                              HsArgHandler.VECTOR
                              ),

                new HsArgRule("-relativeSrcInclude",
                              "RelativeSrcInclude",
                              null,
                              HsArgHandler.VECTOR
                              ),

D
duke 已提交
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
                new HsArgRule("-define",
                              "Define",
                              null,
                              HsArgHandler.VECTOR
                              ),

                new HsArgRule("-useToGeneratePch",
                              "UseToGeneratePch",
                              null,
                              HsArgHandler.STRING
                              ),

                new ArgRuleSpecific("-perFileLine",
                            new HsArgHandler() {
                                public void handle(ArgIterator it) {
                                    String cfg = getCfg(it.get());
                                    if (nextNotKey(it)) {
                                        String fileName = it.get();
                                        if (nextNotKey(it)) {
                                            String line = it.get();
                                            BuildConfig.putFieldHash(cfg, "PerFileLine", fileName, line);
                                            it.next();
                                            return;
                                        }
                                    }
                                    empty(null, "** Error: wrong number of args to -perFileLine");
                                }
                            }
                            ),

                new ArgRuleSpecific("-conditionalPerFileLine",
                            new HsArgHandler() {
                                public void handle(ArgIterator it) {
                                    String cfg = getCfg(it.get());
                                    if (nextNotKey(it)) {
                                        String fileName = it.get();
                                        if (nextNotKey(it)) {
                                            String productLine = it.get();
                                            if (nextNotKey(it)) {
                                                String debugLine = it.get();
                                                BuildConfig.putFieldHash(cfg+"_debug", "CondPerFileLine",
                                                                         fileName, debugLine);
                                                BuildConfig.putFieldHash(cfg+"_product", "CondPerFileLine",
                                                                         fileName, productLine);
                                                it.next();
                                                return;
                                            }
                                        }
                                    }

                                    empty(null, "** Error: wrong number of args to -conditionalPerFileLine");
                                }
                            }
                            ),

                new HsArgRule("-disablePch",
                              "DisablePch",
                              null,
                              HsArgHandler.HASH
                              ),

                new ArgRule("-startAt",
                            new HsArgHandler() {
                                public void handle(ArgIterator it) {
                                    if (BuildConfig.getField(null, "StartAt") != null) {
                                        empty(null, "** Error: multiple -startAt");
                                    }
                                    if (nextNotKey(it)) {
                                        BuildConfig.putField(null, "StartAt", it.get());
                                        it.next();
                                    } else {
                                        empty("-startAt", null);
                                    }
                                }
                            }
                            ),

                new HsArgRule("-ignoreFile",
                                      "IgnoreFile",
                                      null,
                                      HsArgHandler.HASH
                                      ),

447 448 449 450 451 452
                new HsArgRule("-ignorePath",
                              "IgnorePath",
                              null,
                              HsArgHandler.VECTOR
                              ),

N
neliasso 已提交
453 454 455 456 457 458
                new HsArgRule("-hidePath",
                      "HidePath",
                      null,
                      HsArgHandler.VECTOR
                      ),

D
duke 已提交
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
                new HsArgRule("-additionalFile",
                              "AdditionalFile",
                              null,
                              HsArgHandler.VECTOR
                              ),

                new ArgRuleSpecific("-additionalGeneratedFile",
                            new HsArgHandler() {
                                public void handle(ArgIterator it) {
                                    String cfg = getCfg(it.get());
                                    if (nextNotKey(it)) {
                                        String dir = it.get();
                                        if (nextNotKey(it)) {
                                            String fileName = it.get();
                                            BuildConfig.putFieldHash(cfg, "AdditionalGeneratedFile",
                                                                     Util.normalize(dir + Util.sep + fileName),
                                                                     fileName);
                                            it.next();
                                            return;
                                        }
                                    }
                                    empty(null, "** Error: wrong number of args to -additionalGeneratedFile");
                                }
                            }
                            ),

                new ArgRule("-prelink",
                            new HsArgHandler() {
                                public void handle(ArgIterator it) {
                                    if (nextNotKey(it)) {
                                        if (nextNotKey(it)) {
                                            String description = it.get();
                                            if (nextNotKey(it)) {
                                                String command = it.get();
                                                BuildConfig.putField(null, "PrelinkDescription", description);
                                                BuildConfig.putField(null, "PrelinkCommand", command);
                                                it.next();
                                                return;
                                            }
                                        }
                                    }

                                    empty(null,  "** Error: wrong number of args to -prelink");
                                }
                            }
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
                            ),

                new ArgRule("-postbuild",
                            new HsArgHandler() {
                                public void handle(ArgIterator it) {
                                    if (nextNotKey(it)) {
                                        if (nextNotKey(it)) {
                                            String description = it.get();
                                            if (nextNotKey(it)) {
                                                String command = it.get();
                                                BuildConfig.putField(null, "PostbuildDescription", description);
                                                BuildConfig.putField(null, "PostbuildCommand", command);
                                                it.next();
                                                return;
                                            }
                                        }
                                    }

                                    empty(null,  "** Error: wrong number of args to -postbuild");
                                }
                            }
                            ),
D
duke 已提交
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
            },
                                       new ArgHandler() {
                                           public void handle(ArgIterator it) {

                                               throw new RuntimeException("Arg Parser: unrecognized option "+it.get());
                                           }
                                       }
                                       );
        if (BuildConfig.getField(null, "SourceBase") == null      ||
            BuildConfig.getField(null, "BuildBase") == null       ||
            BuildConfig.getField(null, "ProjectFileName") == null ||
            BuildConfig.getField(null, "CompilerVersion") == null) {
            usage();
        }

        if (BuildConfig.getField(null, "UseToGeneratePch") == null) {
            throw new RuntimeException("ERROR: need to specify one file to compute PCH, with -useToGeneratePch flag");
        }

        BuildConfig.putField(null, "PlatformObject", this);
    }

548
    Vector createAllConfigs(String platform) {
D
duke 已提交
549 550 551
        Vector allConfigs = new Vector();

        allConfigs.add(new C1DebugConfig());
552 553
        allConfigs.add(new C1FastDebugConfig());
        allConfigs.add(new C1ProductConfig());
D
duke 已提交
554

555 556 557
        allConfigs.add(new C2DebugConfig());
        allConfigs.add(new C2FastDebugConfig());
        allConfigs.add(new C2ProductConfig());
D
duke 已提交
558

559 560 561
        allConfigs.add(new TieredDebugConfig());
        allConfigs.add(new TieredFastDebugConfig());
        allConfigs.add(new TieredProductConfig());
D
duke 已提交
562

563 564 565
        allConfigs.add(new CoreDebugConfig());
        allConfigs.add(new CoreFastDebugConfig());
        allConfigs.add(new CoreProductConfig());
D
duke 已提交
566

567
        if (platform.equals("Win32")) {
D
duke 已提交
568 569 570 571 572 573 574 575
            allConfigs.add(new KernelDebugConfig());
            allConfigs.add(new KernelFastDebugConfig());
            allConfigs.add(new KernelProductConfig());
        }

        return allConfigs;
    }

N
neliasso 已提交
576
    PrintWriter printWriter;
D
duke 已提交
577

N
neliasso 已提交
578 579 580
    public void writeProjectFile(String projectFileName, String projectName,
                                 Vector<BuildConfig> allConfigs) throws IOException {
        throw new RuntimeException("use compiler version specific version");
D
duke 已提交
581 582
    }

N
neliasso 已提交
583 584
    int indent;
    private Stack<String> tagStack = new Stack<String>();
585

N
neliasso 已提交
586 587
    private void startTagPrim(String name, String[] attrs, boolean close) {
       startTagPrim(name, attrs, close, true);
D
duke 已提交
588 589
    }

N
neliasso 已提交
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
    private void startTagPrim(String name, String[] attrs, boolean close,
          boolean newline) {
       doIndent();
       printWriter.print("<" + name);
       indent++;

       if (attrs != null && attrs.length > 0) {
          for (int i = 0; i < attrs.length; i += 2) {
             printWriter.print(" " + attrs[i] + "=\"" + attrs[i + 1] + "\"");
             if (i < attrs.length - 2) {
             }
          }
       }

       if (close) {
          indent--;
          printWriter.print(" />");
       } else {
          // TODO push tag name, and change endTag to pop and print.
          tagStack.push(name);
          printWriter.print(">");
       }
       if (newline) {
          printWriter.println();
       }
    }
D
duke 已提交
616

N
neliasso 已提交
617 618
    void startTag(String name, String... attrs) {
       startTagPrim(name, attrs, false);
D
duke 已提交
619 620
    }

N
neliasso 已提交
621 622 623 624 625 626 627
    void startTagV(String name, Vector attrs) {
       String s[] = new String[attrs.size()];
       for (int i = 0; i < attrs.size(); i++) {
          s[i] = (String) attrs.elementAt(i);
       }
       startTagPrim(name, s, false);
    }
D
duke 已提交
628

N
neliasso 已提交
629 630 631 632 633 634
    void endTag() {
       String name = tagStack.pop();
       indent--;
       doIndent();
       printWriter.println("</" + name + ">");
    }
D
duke 已提交
635

N
neliasso 已提交
636 637 638 639 640
    private void endTagNoIndent() {
       String name = tagStack.pop();
       indent--;
       printWriter.println("</" + name + ">");
    }
D
duke 已提交
641

N
neliasso 已提交
642 643 644
    void tag(String name, String... attrs) {
       startTagPrim(name, attrs, true);
    }
D
duke 已提交
645

N
neliasso 已提交
646 647 648 649
    void tagData(String name, String data) {
       startTagPrim(name, null, false, false);
       printWriter.print(data);
       endTagNoIndent();
D
duke 已提交
650 651
    }

N
neliasso 已提交
652 653 654 655 656
    void tagData(String name, String data, String... attrs) {
       startTagPrim(name, attrs, false, false);
       printWriter.print(data);
       endTagNoIndent();
    }
D
duke 已提交
657

N
neliasso 已提交
658 659 660 661 662 663 664
    void tagV(String name, Vector attrs) {
       String s[] = new String[attrs.size()];
       for (int i = 0; i < attrs.size(); i++) {
          s[i] = (String) attrs.elementAt(i);
       }
       startTagPrim(name, s, true);
    }
D
duke 已提交
665

N
neliasso 已提交
666 667 668 669
    void doIndent() {
       for (int i = 0; i < indent; i++) {
          printWriter.print("  ");
       }
D
duke 已提交
670 671 672 673
    }


}