1 /*
   2  * Copyright (c) 2015, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * This file defines build profiles for the JIB tool and others.
  28  *
  29  * A build profile defines a set of configuration options and external
  30  * dependencies that we for some reason or other care about specifically.
  31  * Typically, build profiles are defined for the build configurations we
  32  * build regularly.
  33  *
  34  * Contract against this file from the tools that use it, is to provide
  35  * a function on the form:
  36  *
  37  * getJibProfiles(input)
  38  *
  39  * which returns an object graph describing the profiles and their
  40  * dependencies. The name of the function is based on the name of this
  41  * file, minus the extension and the '-', camel cased and prefixed with
  42  * 'get'.
  43  *
  44  *
  45  * The parameter 'input' is an object that optionally contains  some data.
  46  * Optionally because a tool may read the configuration for different purposes.
  47  * To initially get a list of available profiles, the active profile may not
  48  * yet be known for instance.
  49  *
  50  * Data that may be set on the input object:
  51  *
  52  * input.profile = <name of active profile>
  53  *
  54  * If the active profile is set, the following data from it must also
  55  * be provided:
  56  *
  57  * input.profile
  58  * input.target_os
  59  * input.target_cpu
  60  * input.build_os
  61  * input.build_cpu
  62  * input.target_platform
  63  * input.build_platform
  64  * // The build_osenv_* variables describe the unix layer on Windows systems,
  65  * // i.e. Cygwin, which may also be 32 or 64 bit.
  66  * input.build_osenv
  67  * input.build_osenv_cpu
  68  * input.build_osenv_platform
  69  *
  70  * For more complex nested attributes, there is a method "get":
  71  *
  72  * input.get("<dependency>", "<attribute>")
  73  *
  74  * Valid attributes are:
  75  * install_path
  76  * download_path
  77  * download_dir
  78  *
  79  *
  80  * The output data generated by this configuration file has the following
  81  * format:
  82  *
  83  * data: {
  84  *   // Identifies the version of this format to the tool reading it
  85  *   format_version: "1.0",
  86  *
  87  *   // Name of base outputdir. JIB assumes the actual output dir is formed
  88  *   // by adding the configuration name: <output_basedir>/<config-name>
  89  *   output_basedir: "build",
  90  *   // Configure argument to use to specify configuration name
  91  *   configuration_configure_arg:
  92  *   // Make argument to use to specify configuration name
  93  *   configuration_make_arg:
  94  *
  95  *   profiles: {
  96  *     <profile-name>: {
  97  *       // Name of os the profile is built to run on
  98  *       target_os; <string>
  99  *       // Name of cpu the profile is built to run on
 100  *       target_cpu; <string>
 101  *       // Combination of target_os and target_cpu for convenience
 102  *       target_platform; <string>
 103  *       // Name of os the profile is built on
 104  *       build_os; <string>
 105  *       // Name of cpu the profile is built on
 106  *       build_cpu; <string>
 107  *       // Combination of build_os and build_cpu for convenience
 108  *       build_platform; <string>
 109  *
 110  *       // List of dependencies needed to build this profile
 111  *       dependencies: <Array of strings>
 112  *
 113  *       // List of configure args to use for this profile
 114  *       configure_args: <Array of strings>
 115  *
 116  *       // List of free form labels describing aspects of this profile
 117  *       labels: <Array of strings>
 118  *     }
 119  *   }
 120  *
 121  *   // Dependencies use a Maven like deployment structure
 122  *   dependencies: {
 123  *     <dependency-name>: {
 124  *       // Organization part of path defining this dependency
 125  *       organization: <string>
 126  *       // File extension for this dependency
 127  *       ext: <string>
 128  *       // Module part of path for defining this dependency,
 129  *       // defaults to <dependency-name>
 130  *       module: <string>
 131  *       // Revision part of path for defining this dependency
 132  *       revision: <string>
 133  *
 134  *       // List of configure args to add when using this dependency,
 135  *       // defaults to
 136  *       // "--with-<dependency-name>=input.get("<dependency-name", "install_path")"
 137  *       configure_args: <array of strings>
 138  *
 139  *       // Name of environment variable to set when using this dependency
 140  *       // when running make
 141  *       environment_name: <string>
 142  *       // Value of environment variable to set when using this dependency
 143  *       // when running make
 144  *       environment_value: <string>
 145  *
 146  *       // Value to add to the PATH variable when using this dependency,
 147  *       // applies to both make and configure
 148  *       environment_path: <string>
 149  *     }
 150  *
 151  *     <dependency-name>: {
 152  *       // For certain dependencies where a legacy distribution mechanism is
 153  *       // already in place, the "javare" server layout is also supported
 154  *       // Indicate that an alternate server source and layout should be used
 155  *       server: "javare"
 156  *
 157  *       // For "javare", a combination of module, revision,
 158  *       // build number (optional), files and checksum file is possible for
 159  *       // artifacts following the standard layout.
 160  *       module: <string>
 161  *       revision: <string>
 162  *       build_number: <string>
 163  *       checksum_file: <string>
 164  *       file: <string>
 165  *
 166  *       // For other files, use checksum path and path instead
 167  *       checksum_path: <string>
 168  *       path: <string>
 169  *     }
 170  *   }
 171  * }
 172  */
 173 
 174 /**
 175  * Main entry to generate the profile configuration
 176  *
 177  * @param input External data to use for generating the configuration
 178  * @returns {{}} Profile configuration
 179  */
 180 var getJibProfiles = function (input) {
 181 
 182     var data = {};
 183 
 184     // Identifies the version of this format to the tool reading it
 185     data.format_version = "1.0";
 186 
 187     // Organization is used when uploading/publishing build results
 188     data.organization = "com.oracle.jpg.jdk";
 189 
 190     // The base directory for the build output. JIB will assume that the
 191     // actual build directory will be <output_basedir>/<configuration>
 192     data.output_basedir = "build";
 193     // The configure argument to use to specify the name of the configuration
 194     data.configuration_configure_arg = "--with-conf-name=";
 195     // The make argument to use to specify the name of the configuration
 196     data.configuration_make_arg = "CONF_NAME=";
 197 
 198     // Define some common values
 199     var common = getJibProfilesCommon(input);
 200     // Generate the profiles part of the configuration
 201     data.profiles = getJibProfilesProfiles(input, common);
 202     // Generate the dependencies part of the configuration
 203     data.dependencies = getJibProfilesDependencies(input, common);
 204 
 205     return data;
 206 };
 207 
 208 /**
 209  * Generates some common values
 210  *
 211  * @param input External data to use for generating the configuration
 212  * @returns Common values
 213  */
 214 var getJibProfilesCommon = function (input) {
 215     var common = {};
 216 
 217     common.dependencies = ["boot_jdk", "gnumake", "jtreg"],
 218     common.default_make_targets = ["product-bundles", "test-bundles"],
 219     common.default_make_targets_debug = common.default_make_targets;
 220     common.default_make_targets_slowdebug = common.default_make_targets;
 221     common.configure_args = ["--enable-jtreg-failure-handler"],
 222     common.configure_args_32bit = ["--with-target-bits=32"],
 223     common.configure_args_debug = ["--enable-debug"],
 224     common.configure_args_slowdebug = ["--with-debug-level=slowdebug"],
 225     common.organization = "jpg.infra.builddeps"
 226 
 227     return common;
 228 };
 229 
 230 /**
 231  * Generates the profiles part of the configuration.
 232  *
 233  * @param input External data to use for generating the configuration
 234  * @param common The common values
 235  * @returns {{}} Profiles part of the configuration
 236  */
 237 var getJibProfilesProfiles = function (input, common) {
 238     var profiles = {};
 239 
 240     // Main SE profiles
 241     var mainProfiles = {
 242 
 243         "linux-x64": {
 244             target_os: "linux",
 245             target_cpu: "x64",
 246             dependencies: concat(common.dependencies, "devkit"),
 247             configure_args: concat(common.configure_args, "--with-zlib=system"),
 248             default_make_targets: concat(common.default_make_targets, "docs-bundles")
 249         },
 250 
 251         "linux-x86": {
 252             target_os: "linux",
 253             target_cpu: "x86",
 254             build_cpu: "x64",
 255             dependencies: concat(common.dependencies, "devkit"),
 256             configure_args: concat(common.configure_args, common.configure_args_32bit,
 257                 "--with-jvm-variants=minimal,server", "--with-zlib=system"),
 258             default_make_targets: common.default_make_targets
 259         },
 260 
 261         "macosx-x64": {
 262             target_os: "macosx",
 263             target_cpu: "x64",
 264             dependencies: concat(common.dependencies, "devkit"),
 265             configure_args: concat(common.configure_args, "--with-zlib=system"),
 266             default_make_targets: common.default_make_targets
 267         },
 268 
 269         "solaris-x64": {
 270             target_os: "solaris",
 271             target_cpu: "x64",
 272             dependencies: concat(common.dependencies, "devkit", "cups"),
 273             configure_args: concat(common.configure_args, "--with-zlib=system"),
 274             default_make_targets: common.default_make_targets
 275         },
 276 
 277         "solaris-sparcv9": {
 278             target_os: "solaris",
 279             target_cpu: "sparcv9",
 280             dependencies: concat(common.dependencies, "devkit", "cups"),
 281             configure_args: concat(common.configure_args, "--with-zlib=system"),
 282             default_make_targets: common.default_make_targets
 283         },
 284 
 285         "windows-x64": {
 286             target_os: "windows",
 287             target_cpu: "x64",
 288             dependencies: concat(common.dependencies, "devkit", "freetype"),
 289             configure_args: concat(common.configure_args),
 290             default_make_targets: common.default_make_targets
 291         },
 292 
 293         "windows-x86": {
 294             target_os: "windows",
 295             target_cpu: "x86",
 296             build_cpu: "x64",
 297             dependencies: concat(common.dependencies, "devkit", "freetype"),
 298             configure_args: concat(common.configure_args, common.configure_args_32bit),
 299             default_make_targets: common.default_make_targets
 300         }
 301     };
 302     profiles = concatObjects(profiles, mainProfiles);
 303     // Generate debug versions of all the main profiles
 304     profiles = concatObjects(profiles, generateDebugProfiles(common, mainProfiles));
 305     // Generate slowdebug versions of all the main profiles
 306     profiles = concatObjects(profiles, generateSlowdebugProfiles(common, mainProfiles));
 307 
 308     // Generate open only profiles for all the main profiles for JPRT and reference
 309     // implementation builds.
 310     var openOnlyProfiles = generateOpenOnlyProfiles(common, mainProfiles);
 311     // The open only profiles on linux are used for reference builds and should
 312     // produce the compact profile images by default. This adds "profiles" as an
 313     // extra default target.
 314     var openOnlyProfilesExtra = {
 315         "linux-x64-open": {
 316             default_make_targets: "profiles"
 317         },
 318 
 319         "linux-x86-open": {
 320             default_make_targets: "profiles",
 321             configure_args: "--with-jvm-variants=client,server"
 322         }
 323     };
 324     var openOnlyProfiles = concatObjects(openOnlyProfiles, openOnlyProfilesExtra);
 325 
 326     profiles = concatObjects(profiles, openOnlyProfiles);
 327     // Generate debug profiles for the open jprt profiles
 328     profiles = concatObjects(profiles, generateDebugProfiles(common, openOnlyProfiles));
 329 
 330     // Profiles used to run tests. Used in JPRT.
 331     var testOnlyProfiles = {
 332 
 333         "run-test": {
 334             target_os: input.build_os,
 335             target_cpu: input.build_cpu,
 336             dependencies: [ "jtreg", "gnumake" ],
 337             labels: "test"
 338         }
 339     };
 340     profiles = concatObjects(profiles, testOnlyProfiles);
 341 
 342     // Generate the missing platform attributes
 343     profiles = generatePlatformAttributes(profiles);
 344     profiles = generateDefaultMakeTargetsConfigureArg(common, profiles);
 345     return profiles;
 346 };
 347 
 348 /**
 349  * Generate the dependencies part of the configuration
 350  *
 351  * @param input External data to use for generating the configuration
 352  * @param common The common values
 353  * @returns {{}} Dependencies part of configuration
 354  */
 355 var getJibProfilesDependencies = function (input, common) {
 356 
 357     var boot_jdk_platform = input.build_os + "-"
 358         + (input.build_cpu == "x86" ? "i586" : input.build_cpu);
 359 
 360     var boot_jdk_revision = "8";
 361     var boot_jdk_subdirpart = "1.8.0";
 362     // JDK 8 does not work on sparc M7 cpus, need a newer update when building
 363     // on such hardware.
 364     if (input.build_cpu == "sparcv9") {
 365        var cpu_brand = $EXEC("bash -c \"kstat -m cpu_info | grep brand | head -n1 | awk '{ print \$2 }'\"");
 366        if (cpu_brand.trim() == 'SPARC-M7') {
 367            boot_jdk_revision = "8u20";
 368            boot_jdk_subdirpart = "1.8.0_20";
 369        }
 370     }
 371 
 372     var devkit_platform_revisions = {
 373         linux_x64: "gcc4.9.2-OEL6.4+1.0",
 374         macosx_x64: "Xcode6.3-MacOSX10.9+1.0",
 375         solaris_x64: "SS12u4-Solaris11u1+1.0",
 376         solaris_sparcv9: "SS12u4-Solaris11u1+1.0",
 377         windows_x64: "VS2013SP4+1.0"
 378     };
 379 
 380     var devkit_platform = (input.target_cpu == "x86"
 381         ? input.target_os + "_x64"
 382         : input.target_platform);
 383 
 384     var dependencies = {
 385 
 386         boot_jdk: {
 387             server: "javare",
 388             module: "jdk",
 389             revision: boot_jdk_revision,
 390             checksum_file: boot_jdk_platform + "/MD5_VALUES",
 391             file: boot_jdk_platform + "/jdk-" + boot_jdk_revision + "-" + boot_jdk_platform + ".tar.gz",
 392             configure_args: (input.build_os == "macosx"
 393                 ? "--with-boot-jdk=" + input.get("boot_jdk", "install_path") + "/jdk" + boot_jdk_subdirpart + ".jdk/Contents/Home"
 394                 : "--with-boot-jdk=" + input.get("boot_jdk", "install_path") + "/jdk" + boot_jdk_subdirpart)
 395         },
 396 
 397         devkit: {
 398             organization: common.organization,
 399             ext: "tar.gz",
 400             module: "devkit-" + devkit_platform,
 401             revision: devkit_platform_revisions[devkit_platform]
 402         },
 403 
 404         build_devkit: {
 405             organization: common.organization,
 406             ext: "tar.gz",
 407             module: "devkit-" + input.build_platform,
 408             revision: devkit_platform_revisions[input.build_platform]
 409         },
 410 
 411         cups: {
 412             organization: common.organization,
 413             ext: "tar.gz",
 414             revision: "1.0118+1.0"
 415         },
 416 
 417         jtreg: {
 418             server: "javare",
 419             revision: "4.2",
 420             build_number: "b03",
 421             checksum_file: "MD5_VALUES",
 422             file: "jtreg_bin-4.2.zip",
 423             environment_name: "JT_HOME"
 424         },
 425 
 426         gnumake: {
 427             organization: common.organization,
 428             ext: "tar.gz",
 429             revision: "4.0+1.0",
 430 
 431             module: (input.build_os == "windows"
 432                 ? "gnumake-" + input.build_osenv_platform
 433                 : "gnumake-" + input.build_platform),
 434 
 435             configure_args: (input.build_os == "windows"
 436                 ? "MAKE=" + input.get("gnumake", "install_path") + "/cygwin/bin/make"
 437                 : "MAKE=" + input.get("gnumake", "install_path") + "/bin/make"),
 438 
 439             environment_path: (input.build_os == "windows"
 440                 ? input.get("gnumake", "install_path") + "/cygwin/bin"
 441                 : input.get("gnumake", "install_path") + "/bin")
 442         },
 443 
 444         freetype: {
 445             organization: common.organization,
 446             ext: "tar.gz",
 447             revision: "2.3.4+1.0",
 448             module: "freetype-" + input.target_platform
 449         }
 450     };
 451 
 452     return dependencies;
 453 };
 454 
 455 /**
 456  * Generate the missing platform attributes for profiles
 457  *
 458  * @param profiles Profiles map to generate attributes on
 459  * @returns {{}} New profiles map with platform attributes fully filled in
 460  */
 461 var generatePlatformAttributes = function (profiles) {
 462     var ret = concatObjects(profiles, {});
 463     for (var profile in profiles) {
 464         if (ret[profile].build_os == null) {
 465             ret[profile].build_os = ret[profile].target_os;
 466         }
 467         if (ret[profile].build_cpu == null) {
 468             ret[profile].build_cpu = ret[profile].target_cpu;
 469         }
 470         ret[profile].target_platform = ret[profile].target_os + "_" + ret[profile].target_cpu;
 471         ret[profile].build_platform = ret[profile].build_os + "_" + ret[profile].build_cpu;
 472     }
 473     return ret;
 474 };
 475 
 476 /**
 477  * Generates debug versions of profiles. Clones the given profiles and adds
 478  * debug metadata.
 479  *
 480  * @param common Common values
 481  * @param profiles Profiles map to generate debug profiles for
 482  * @returns {{}} New map of profiles containing debug profiles
 483  */
 484 var generateDebugProfiles = function (common, profiles) {
 485     var newProfiles = {};
 486     for (var profile in profiles) {
 487         var debugProfile = profile + "-debug";
 488         newProfiles[debugProfile] = clone(profiles[profile]);
 489         newProfiles[debugProfile].debug_level = "fastdebug";
 490         newProfiles[debugProfile].default_make_targets
 491             = common.default_make_targets_debug;
 492         newProfiles[debugProfile].labels
 493             = concat(newProfiles[debugProfile].labels || [], "debug"),
 494             newProfiles[debugProfile].configure_args
 495                 = concat(newProfiles[debugProfile].configure_args,
 496                 common.configure_args_debug);
 497     }
 498     return newProfiles;
 499 };
 500 
 501 /**
 502  * Generates slowdebug versions of profiles. Clones the given profiles and adds
 503  * debug metadata.
 504  *
 505  * @param common Common values
 506  * @param profiles Profiles map to generate debug profiles for
 507  * @returns {{}} New map of profiles containing debug profiles
 508  */
 509 var generateSlowdebugProfiles = function (common, profiles) {
 510     var newProfiles = {};
 511     for (var profile in profiles) {
 512         var debugProfile = profile + "-slowdebug";
 513         newProfiles[debugProfile] = clone(profiles[profile]);
 514         newProfiles[debugProfile].debug_level = "slowdebug";
 515         newProfiles[debugProfile].default_make_targets
 516             = common.default_make_targets_slowdebug;
 517         newProfiles[debugProfile].labels
 518             = concat(newProfiles[debugProfile].labels || [], "slowdebug"),
 519             newProfiles[debugProfile].configure_args
 520                 = concat(newProfiles[debugProfile].configure_args,
 521                 common.configure_args_slowdebug);
 522     }
 523     return newProfiles;
 524 };
 525 
 526 /**
 527  * Generates open only versions of profiles. Clones the given profiles and adds
 528  * open metadata.
 529  *
 530  * @param common Common values
 531  * @param profiles Profiles map to generate open only profiles for
 532  * @returns {{}} New map of profiles containing open only profiles
 533  */
 534 var generateOpenOnlyProfiles = function (common, profiles) {
 535     var newProfiles = {};
 536     for (var profile in profiles) {
 537         var openProfile = profile + "-open";
 538         newProfiles[openProfile] = clone(profiles[profile]);
 539         newProfiles[openProfile].labels
 540             = concat(newProfiles[openProfile].labels || [], "open"),
 541             newProfiles[openProfile].configure_args
 542                 = concat(newProfiles[openProfile].configure_args,
 543                 "--enable-openjdk-only");
 544     }
 545     return newProfiles;
 546 };
 547 
 548 /**
 549  * The default_make_targets attribute on a profile is not a real Jib attribute.
 550  * This function rewrites that attribute into the corresponding configure arg.
 551  * Calling this function multiple times on the same profiles object is safe.
 552  *
 553  * @param common Common values
 554  * @param profiles Profiles map to rewrite profiles for
 555  * @returns {{}} New map of profiles with the make targets converted
 556  */
 557 var generateDefaultMakeTargetsConfigureArg = function (common, profiles) {
 558     var ret = concatObjects(profiles, {});
 559     for (var profile in ret) {
 560         if (ret[profile]["default_make_targets"] != null) {
 561             var targetsString = concat(ret[profile].default_make_targets).join(" ");
 562             // Iterate over all configure args and see if --with-default-make-target
 563             // is already there and change it, otherwise add it.
 564             var found = false;
 565             for (var arg in ret[profile].configure_args) {
 566                 if (arg.startsWith("--with-default-make-target")) {
 567                     found = true;
 568                     arg.replace(/=.*/, "=" + targetsString);
 569                 }
 570             }
 571             if (!found) {
 572                 ret[profile].configure_args = concat(
 573                     ret[profile].configure_args,
 574                     "--with-default-make-target=" + targetsString);
 575             }
 576         }
 577     }
 578     return ret;
 579 }
 580 
 581 /**
 582  * Deep clones an object tree.
 583  *
 584  * @param o Object to clone
 585  * @returns {{}} Clone of o
 586  */
 587 var clone = function (o) {
 588     return JSON.parse(JSON.stringify(o));
 589 };
 590 
 591 /**
 592  * Concatenates all arguments into a new array
 593  *
 594  * @returns {Array.<T>} New array containing all arguments
 595  */
 596 var concat = function () {
 597     return Array.prototype.concat.apply([], arguments);
 598 };
 599 
 600 /**
 601  * Copies all elements in an array into a new array but replacing all
 602  * occurrences of original with replacement.
 603  *
 604  * @param original Element to look for
 605  * @param replacement Element to replace with
 606  * @param a Array to copy
 607  * @returns {Array} New array with all occurrences of original replaced
 608  *                  with replacement
 609  */
 610 var replace = function (original, replacement, a) {
 611     var newA = [];
 612     for (var i in a) {
 613         if (original == a[i]) {
 614             newA.push(replacement);
 615         } else {
 616             newA.push(a[i]);
 617         }
 618     }
 619     return newA;
 620 };
 621 
 622 /**
 623  * Deep concatenation of two objects. For each node encountered, merge
 624  * the contents with the corresponding node in the other object tree,
 625  * treating all strings as array elements.
 626  *
 627  * @param o1 Object to concatenate
 628  * @param o2 Object to concatenate
 629  * @returns {{}} New object tree containing the concatenation of o1 and o2
 630  */
 631 var concatObjects = function (o1, o2) {
 632     var ret = {};
 633     for (var a in o1) {
 634         if (o2[a] == null) {
 635             ret[a] = o1[a];
 636         }
 637     }
 638     for (var a in o2) {
 639         if (o1[a] == null) {
 640             ret[a] = o2[a];
 641         } else {
 642             if (typeof o1[a] == 'string') {
 643                 ret[a] = [o1[a]].concat(o2[a]);
 644             } else if (Array.isArray(o1[a])) {
 645                 ret[a] = o1[a].concat(o2[a]);
 646             } else if (typeof o1[a] == 'object') {
 647                 ret[a] = concatObjects(o1[a], o2[a]);
 648             }
 649         }
 650     }
 651     return ret;
 652 };