2a0970d
/* JavaConfig - tool for getting paths for current java environment.
20fedc5
 * © 2011, 2012, 2014, 2017  Petr Písař <ppisar@redhat.com>
2a0970d
 *
2a0970d
 * This program is free software: you can redistribute it and/or modify
2a0970d
 * it under the terms of the GNU General Public License as published by
2a0970d
 * the Free Software Foundation, either version 3 of the License, or
2a0970d
 * (at your option) any later version.
2a0970d
 * 
2a0970d
 * This program is distributed in the hope that it will be useful,
2a0970d
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
2a0970d
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2a0970d
 * GNU General Public License for more details.
2a0970d
 * 
2a0970d
 * You should have received a copy of the GNU General Public License
2a0970d
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2a0970d
 */
2a0970d
2a0970d
class JavaConfig {
2a0970d
  private static String output = "";
2a0970d
2a0970d
2a0970d
  /*
2a0970d
   * Append text to output with proper padding or terminates if text is null.
2a0970d
   * @param text String to append.
2a0970d
   */
2a0970d
  private static void concatenate(String text) {
2a0970d
    if (text == null) {
2a0970d
      System.exit(2);
2a0970d
    }
2a0970d
    output = (output.equals("") ? ""  : output + " " ) + text;
2a0970d
  }
2a0970d
2a0970d
2a0970d
  /*
2a0970d
   * Show usage message and terminates program with given @exit_code.
2a0970d
   * @param exit_code code to return
2a0970d
   * */
2a0970d
  private static void usage(int exitCode) {
2a0970d
    System.out.print(
2a0970d
        "JavaConfig [OPTIONS]\n" +
2a0970d
        "  --home         Output path to Java home\n" +
ad2d4f2
        "  --libs-only-L  Output -L linker flags\n" +
ad2d4f2
        "  --version      Output Java version on first separate line\n"
2a0970d
    );
2a0970d
    System.exit(exitCode);
2a0970d
  }
2a0970d
2a0970d
2a0970d
  /*
2a0970d
   * @Return path to Java home or null in case of error.
2a0970d
   */
2a0970d
  public static String home() {
2a0970d
    return System.getProperty("java.home");
2a0970d
  }
2a0970d
2a0970d
2a0970d
  /*
2a0970d
   * @Return formated libary search path as -L compiler flag,
2a0970d
   * null if error occured.
2a0970d
   * */
2a0970d
  public static String libsOnlyL() {
2a0970d
    String value;
ad2d4f2
    String architecture = System.getProperty("os.arch");
ad2d4f2
    String home = home();
ad2d4f2
    String filesep = System.getProperty("file.separator");
2a0970d
    String paths[];
2a0970d
   
ad2d4f2
    /* The java.library.path works up to JDK 1.6. */
2a0970d
    if (null == (value = System.getProperty("java.library.path"))) {
2a0970d
      return null;
2a0970d
    }
2a0970d
ad2d4f2
    /* The java.library.path does not work since JDK 1.7. See
5156526
     * <https://bugzilla.redhat.com/show_bug.cgi?id=740762>.
5156526
     * The "client" subdirectory is need on AArch64 since JDK 1.8. See
5156526
     * <https://bugzilla.redhat.com/show_bug.cgi?id=1112012>. */
ad2d4f2
    if (null == architecture || null == home || null == filesep) {
ad2d4f2
      return null;
ad2d4f2
    }
20fedc5
20fedc5
    /* Experimental 32-bit ARM openjdk variant uses lib/aarch32. See
20fedc5
     * <https://bugzilla.redhat.com/show_bug.cgi?id=1412953>. */
20fedc5
    if (architecture.contentEquals("arm") && home.contains("aarch32")) {
20fedc5
      architecture = "aarch32";
20fedc5
    }
20fedc5
ad2d4f2
    value = value + ":" +
ad2d4f2
      home + filesep + "lib" + filesep + architecture + ":" +
5156526
      home + filesep + "lib" + filesep + architecture + filesep + "server" +
5156526
        ":" +
Yaakov Selkowitz 625a7c8
      home + filesep + "lib" + filesep + architecture + filesep + "client";
ad2d4f2
ad2d4f2
c176a2a
    /* Convert the colon-delimited paths to LDFLAGS format */
2a0970d
    paths = value.split(":");
2a0970d
2a0970d
    for (int i = 0; i < paths.length; i++) {
2a0970d
      if (paths[i].equals("")) {
2a0970d
        continue;
2a0970d
      }
2a0970d
      
2a0970d
      if (i == 0) {
2a0970d
        value = "-L" + paths[i];
2a0970d
      } else {
2a0970d
        value = value + " -L" + paths[i];
2a0970d
      }
2a0970d
    }
c176a2a
    value = value + " -Wl,-rpath," + home + filesep + "lib" + architecture +
c176a2a
	    filesep + "server";
2a0970d
2a0970d
    return value;
2a0970d
  }
2a0970d
2a0970d
2a0970d
  /*
ad2d4f2
   * @Return version of Java home or null in case of error.
ad2d4f2
   */
ad2d4f2
  public static String version() {
ad2d4f2
    return System.getProperty("java.version");
ad2d4f2
  }
ad2d4f2
ad2d4f2
ad2d4f2
  /*
2a0970d
   * Entry point to this class.
2a0970d
   */
2a0970d
  public static void main(String argv[]) {
2a0970d
    if (argv.length < 1) {
2a0970d
      usage(1);
2a0970d
    }
2a0970d
2a0970d
    for (int i = 0; i < argv.length; i++) {
2a0970d
      if (argv[i].equals("--home")) {
2a0970d
          concatenate(home());
2a0970d
      } else if (argv[i].equals("--libs-only-L")) {
2a0970d
          concatenate(libsOnlyL());
ad2d4f2
      } else if (argv[i].equals("--version")) {
ad2d4f2
          /* pkg-config prints version on first line */
ad2d4f2
          String version = version();
ad2d4f2
          if (null == version) {
ad2d4f2
            System.exit(2);
ad2d4f2
          }
ad2d4f2
          System.out.println(version);
2a0970d
      } else {
2a0970d
          usage(1);
2a0970d
      }
2a0970d
    }
2a0970d
ad2d4f2
    if (!output.equals("")) {
ad2d4f2
      System.out.println(output);
ad2d4f2
    }
2a0970d
    System.exit(0);
2a0970d
  }
2a0970d
}