xref: /third_party/node/src/node_main.cc (revision 1cb0ef41)
1// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22#include "node.h"
23#include <cstdio>
24
25#ifdef _WIN32
26#include <windows.h>
27#include <VersionHelpers.h>
28#include <WinError.h>
29
30#define SKIP_CHECK_VAR "NODE_SKIP_PLATFORM_CHECK"
31#define SKIP_CHECK_VALUE "1"
32#define SKIP_CHECK_STRLEN (sizeof(SKIP_CHECK_VALUE) - 1)
33
34int wmain(int argc, wchar_t* wargv[]) {
35  // Windows Server 2012 (not R2) is supported until 10/10/2023, so we allow it
36  // to run in the experimental support tier.
37  char buf[SKIP_CHECK_STRLEN + 1];
38  if (!IsWindows8Point1OrGreater() &&
39      !(IsWindowsServer() && IsWindows8OrGreater()) &&
40      (GetEnvironmentVariableA(SKIP_CHECK_VAR, buf, sizeof(buf)) !=
41           SKIP_CHECK_STRLEN ||
42       strncmp(buf, SKIP_CHECK_VALUE, SKIP_CHECK_STRLEN) != 0)) {
43    fprintf(stderr, "Node.js is only supported on Windows 8.1, Windows "
44                    "Server 2012 R2, or higher.\n"
45                    "Setting the " SKIP_CHECK_VAR " environment variable "
46                    "to 1 skips this\ncheck, but Node.js might not execute "
47                    "correctly. Any issues encountered on\nunsupported "
48                    "platforms will not be fixed.");
49    exit(ERROR_EXE_MACHINE_TYPE_MISMATCH);
50  }
51
52  // Convert argv to UTF8
53  char** argv = new char*[argc + 1];
54  for (int i = 0; i < argc; i++) {
55    // Compute the size of the required buffer
56    DWORD size = WideCharToMultiByte(CP_UTF8,
57                                     0,
58                                     wargv[i],
59                                     -1,
60                                     nullptr,
61                                     0,
62                                     nullptr,
63                                     nullptr);
64    if (size == 0) {
65      // This should never happen.
66      fprintf(stderr, "Could not convert arguments to utf8.");
67      exit(1);
68    }
69    // Do the actual conversion
70    argv[i] = new char[size];
71    DWORD result = WideCharToMultiByte(CP_UTF8,
72                                       0,
73                                       wargv[i],
74                                       -1,
75                                       argv[i],
76                                       size,
77                                       nullptr,
78                                       nullptr);
79    if (result == 0) {
80      // This should never happen.
81      fprintf(stderr, "Could not convert arguments to utf8.");
82      exit(1);
83    }
84  }
85  argv[argc] = nullptr;
86  // Now that conversion is done, we can finally start.
87  return node::Start(argc, argv);
88}
89#else
90// UNIX
91
92int main(int argc, char* argv[]) {
93  return node::Start(argc, argv);
94}
95#endif
96