Debian Codegraph @

vendor/opam/src/core/custom_opamStubs.c

raw
1 #include <caml/mlvalues.h>
2 #include <caml/fail.h>
3
4 #if defined(_WIN32)
5
6 #include <caml/memory.h>
7 #include <caml/osdeps.h>
8 #include <caml/alloc.h>
9 #include <windows.h>
10
11 typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS2) (HANDLE, USHORT *, USHORT *);
12
13 static LPFN_ISWOW64PROCESS2 pIsWow64Process2 = NULL;
14
15 CAMLprim value OPAMW_GetWindowsVersion(value unit)
16 {
17 value result;
18 result = caml_alloc_small(4, 0);
19 Field(result, 0) = Val_int(caml_win32_major);
20 Field(result, 1) = Val_int(caml_win32_minor);
21 Field(result, 2) = Val_int(caml_win32_build);
22 Field(result, 3) = Val_int(caml_win32_revision);
23
24 return result;
25 }
26
27 static inline value get_native_cpu_architecture(void)
28 {
29 SYSTEM_INFO SystemInfo;
30 GetNativeSystemInfo(&SystemInfo);
31 switch (SystemInfo.wProcessorArchitecture) {
32 case PROCESSOR_ARCHITECTURE_AMD64:
33 return Val_int(0);
34 case PROCESSOR_ARCHITECTURE_ARM:
35 return Val_int(1);
36 case PROCESSOR_ARCHITECTURE_ARM64:
37 return Val_int(2);
38 case PROCESSOR_ARCHITECTURE_IA64:
39 return Val_int(3); /* Wow! */
40 case PROCESSOR_ARCHITECTURE_INTEL:
41 return Val_int(4);
42 default: /* PROCESSOR_ARCHITECTURE_UNKNOWN */
43 return Val_int(5);
44 }
45 }
46
47 static inline value get_PE_cpu_architecture(USHORT image)
48 {
49 switch (image) {
50 case IMAGE_FILE_MACHINE_I386:
51 return Val_int(4);
52 case IMAGE_FILE_MACHINE_ARM:
53 case IMAGE_FILE_MACHINE_THUMB:
54 case IMAGE_FILE_MACHINE_ARMNT:
55 return Val_int(1);
56 case IMAGE_FILE_MACHINE_IA64:
57 return Val_int(3); /* Wow! */
58 case IMAGE_FILE_MACHINE_AMD64:
59 return Val_int(0);
60 case IMAGE_FILE_MACHINE_ARM64:
61 return Val_int(2);
62 default:
63 return Val_int(5);
64 }
65 }
66
67 CAMLprim value OPAMW_GetArchitecture(value unit)
68 {
69 USHORT ProcessMachine, NativeMachine;
70
71 if (!pIsWow64Process2)
72 pIsWow64Process2 =
73 (LPFN_ISWOW64PROCESS2)GetProcAddress(GetModuleHandleW(L"kernel32"), "IsWow64Process2");
74
75 if (!pIsWow64Process2 || !pIsWow64Process2(GetCurrentProcess(), &ProcessMachine, &NativeMachine))
76 return get_native_cpu_architecture();
77 else
78 return get_PE_cpu_architecture(NativeMachine);
79 }
80
81 #else
82
83 static char * unavailable_message =
84 "Windows stubs are only allowed to be called on Windows.";
85
86
87 CAMLprim value OPAMW_GetWindowsVersion(value unit)
88 {
89 (void)unit;
90 caml_failwith(unavailable_message);
91 }
92
93 CAMLprim value OPAMW_GetArchitecture(value unit)
94 {
95 (void)unit;
96 caml_failwith(unavailable_message);
97 }
98
99 #endif