Debian Codegraph @

thirdparty/folly/build/fbcode_builder/CMake/fb_py_win_main.c

raw
1 // Copyright (c) Facebook, Inc. and its affiliates.
2
3 #define WIN32_LEAN_AND_MEAN
4
5 #include <Windows.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8
9 #define PATH_SIZE 32768
10
11 typedef int (*Py_Main)(int, wchar_t**);
12
13 // Add the given path to Windows's DLL search path.
14 // For Windows DLL search path resolution, see:
15 // https://docs.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-search-order
16 void add_search_path(const wchar_t* path) {
17 wchar_t buffer[PATH_SIZE];
18 wchar_t** lppPart = NULL;
19
20 if (!GetFullPathNameW(path, PATH_SIZE, buffer, lppPart)) {
21 fwprintf(
22 stderr,
23 L"warning: %d unable to expand path %s\n",
24 GetLastError(),
25 path);
26 return;
27 }
28
29 if (!AddDllDirectory(buffer)) {
30 DWORD error = GetLastError();
31 if (error != ERROR_FILE_NOT_FOUND) {
32 fwprintf(
33 stderr,
34 L"warning: %d unable to set DLL search path for %s\n",
35 GetLastError(),
36 path);
37 }
38 }
39 }
40
41 int locate_py_main(int argc, wchar_t** argv) {
42 /*
43 * We have to dynamically locate Python3.dll because we may be loading a
44 * Python native module while running. If that module is built with a
45 * different Python version, we will end up a DLL import error. To resolve
46 * this, we can either ship an embedded version of Python with us or
47 * dynamically look up existing Python distribution installed on user's
48 * machine. This way, we should be able to get a consistent version of
49 * Python3.dll and .pyd modules.
50 */
51 HINSTANCE python_dll;
52 Py_Main pymain;
53
54 // last added directory has highest priority
55 add_search_path(L"C:\\Python36\\");
56 add_search_path(L"C:\\tools\\fb-python\\fb-python36\\");
57 add_search_path(L"C:\\Python37\\");
58 add_search_path(L"C:\\tools\\fb-python\\fb-python37\\");
59 add_search_path(L"C:\\Python38\\");
60 add_search_path(L"C:\\tools\\fb-python\\fb-python38\\");
61 // TODO(T123615656): Re-enable Python 3.9 after the fix
62 // add_search_path(L"C:\\tools\\fb-python\\fb-python39\\");
63
64 python_dll =
65 LoadLibraryExW(L"python3.dll", NULL, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
66
67 int returncode = 0;
68 if (python_dll != NULL) {
69 pymain = (Py_Main)GetProcAddress(python_dll, "Py_Main");
70
71 if (pymain != NULL) {
72 returncode = (pymain)(argc, argv);
73 } else {
74 fprintf(stderr, "error: %d unable to load Py_Main\n", GetLastError());
75 }
76
77 FreeLibrary(python_dll);
78 } else {
79 fprintf(stderr, "error: %d unable to locate python3.dll\n", GetLastError());
80 return 1;
81 }
82 return returncode;
83 }
84
85 int wmain() {
86 /*
87 * This executable will be prepended to the start of a Python ZIP archive.
88 * Python will be able to directly execute the ZIP archive, so we simply
89 * need to tell Py_Main() to run our own file. Duplicate the argument list
90 * and add our file name to the beginning to tell Python what file to invoke.
91 */
92 wchar_t** pyargv = malloc(sizeof(wchar_t*) * (__argc + 1));
93 if (!pyargv) {
94 fprintf(stderr, "error: failed to allocate argument vector\n");
95 return 1;
96 }
97
98 /* Py_Main wants the wide character version of the argv so we pull those
99 * values from the global __wargv array that has been prepared by MSVCRT.
100 *
101 * In order for the zipapp to run we need to insert an extra argument in
102 * the front of the argument vector that points to ourselves.
103 *
104 * An additional complication is that, depending on who prepared the argument
105 * string used to start our process, the computed __wargv[0] can be a simple
106 * shell word like `watchman-wait` which is normally resolved together with
107 * the PATH by the shell.
108 * That unresolved path isn't sufficient to start the zipapp on windows;
109 * we need the fully qualified path.
110 *
111 * Given:
112 * __wargv == {"watchman-wait", "-h"}
113 *
114 * we want to pass the following to Py_Main:
115 *
116 * {
117 * "z:\build\watchman\python\watchman-wait.exe",
118 * "z:\build\watchman\python\watchman-wait.exe",
119 * "-h"
120 * }
121 */
122 wchar_t full_path_to_argv0[PATH_SIZE];
123 DWORD len = GetModuleFileNameW(NULL, full_path_to_argv0, PATH_SIZE);
124 if (len == 0 ||
125 len == PATH_SIZE && GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
126 fprintf(
127 stderr,
128 "error: %d while retrieving full path to this executable\n",
129 GetLastError());
130 return 1;
131 }
132
133 for (int n = 1; n < __argc; ++n) {
134 pyargv[n + 1] = __wargv[n];
135 }
136 pyargv[0] = full_path_to_argv0;
137 pyargv[1] = full_path_to_argv0;
138
139 return locate_py_main(__argc + 1, pyargv);
140 }