1 /*
   2  * Copyright (c) 2020, 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 #include "AppLauncher.h"
  27 #include "FileUtils.h"
  28 #include "UnixSysInfo.h"
  29 #include "JvmLauncher.h"
  30 
  31 
  32 namespace {
  33 
  34 Jvm* jvmLauncher = 0;
  35 
  36 void launchJvm() {
  37     // On Mac JLI_Launch() spawns a new thread that actually starts the JVM.
  38     // This new thread simply re-runs launcher's main() function with
  39     // arguments passed into JLI_Launch() call.
  40     // Jvm::launch() calls JLI_Launch() triggering thread spawning.
  41     jvmLauncher->launch();
  42 }
  43 
  44 void initJvmLauncher() {
  45     const tstring launcherPath = SysInfo::getProcessModulePath();
  46 
  47     // Launcher should be in "Contents/MacOS" subdirectory of app image.
  48     const tstring appImageRoot = FileUtils::dirname(FileUtils::dirname(
  49             FileUtils::dirname(launcherPath)));
  50 
  51     // Create JVM launcher and save in global variable.
  52     jvmLauncher = AppLauncher()
  53         .setImageRoot(appImageRoot)
  54         .addJvmLibName(_T("Contents/Home/lib/libjli.dylib"))
  55         .setAppDir(FileUtils::mkpath() << appImageRoot << _T("Contents/app"))
  56         .setDefaultRuntimePath(FileUtils::mkpath() << appImageRoot
  57                 << _T("Contents/runtime"))
  58         .createJvmLauncher();
  59 
  60     // Kick start JVM launching. The function wouldn't return!
  61     launchJvm();
  62 }
  63 
  64 } // namespace
  65 
  66 
  67 int main(int argc, char *argv[]) {
  68     setlocale(LC_ALL, "en_US.utf8");
  69     if (jvmLauncher) {
  70         // This is the call from the thread spawned by JVM.
  71         // Skip initialization phase as we have done this already in the first
  72         // call of main().
  73         // Besides we should ignore main() arguments because these are the
  74         // arguments passed into JLI_Launch() call and not the arguments with
  75         // which the launcher was started.
  76         return AppLauncher::launch(std::nothrow, launchJvm);
  77     }
  78 
  79     SysInfo::argc = argc;
  80     SysInfo::argv = argv;
  81     return AppLauncher::launch(std::nothrow, initJvmLauncher);
  82 }