Nuitka
The Python compiler
Loading...
Searching...
No Matches
blobs.h
1// Copyright 2026, Kay Hayen, mailto:kay.hayen@gmail.com find license text at end of file
2
3#ifndef __NUITKA_BLOBS_H__
4#define __NUITKA_BLOBS_H__
5
17#if defined(_NUITKA_CONSTANTS_FROM_INCBIN) || defined(_NUITKA_CONSTANTS_FROM_C23_EMBED)
18
19#ifdef __cplusplus
20#define NUITKA_CONSTANTS_EXTERN_C_START extern "C" {
21#define NUITKA_CONSTANTS_EXTERN_C_END }
22#else
23#define NUITKA_CONSTANTS_EXTERN_C_START
24#define NUITKA_CONSTANTS_EXTERN_C_END
25#endif
26
27#define NUITKA_DECLARE_CONSTANT_BLOB(blob_name, blob_camel_name, modifier, res_id) \
28 NUITKA_CONSTANTS_EXTERN_C_START \
29 extern unsigned modifier char *get##blob_camel_name##Data(void); \
30 NUITKA_CONSTANTS_EXTERN_C_END
31
32#elif defined(_NUITKA_CONSTANTS_FROM_RESOURCE)
33
34#include <windows.h> // Ensure FindResource and loaded objects are valid
35
36#ifdef __cplusplus
37#define NUITKA_CONSTANTS_EXTERN_C_START extern "C" {
38#define NUITKA_CONSTANTS_EXTERN_C_END }
39#else
40#define NUITKA_CONSTANTS_EXTERN_C_START
41#define NUITKA_CONSTANTS_EXTERN_C_END
42#endif
43
44#if _NUITKA_EXE_MODE
45#define _NUITKA_GET_RESOURCE_HANDLE() NULL
46#else
47extern HMODULE getDllModuleHandle(void);
48#define _NUITKA_GET_RESOURCE_HANDLE() getDllModuleHandle()
49#endif
50
51#define NUITKA_DECLARE_CONSTANT_BLOB(blob_name, blob_camel_name, modifier, res_id) \
52 NUITKA_CONSTANTS_EXTERN_C_START \
53 static inline unsigned modifier char *get##blob_camel_name##Data(void) { \
54 HMODULE handle = _NUITKA_GET_RESOURCE_HANDLE(); \
55 HRSRC hRes = FindResource(handle, MAKEINTRESOURCE(res_id), RT_RCDATA); \
56 if (unlikely(hRes == NULL)) { \
57 abort(); \
58 } \
59 HGLOBAL hData = LoadResource(handle, hRes); \
60 if (unlikely(hData == NULL)) { \
61 abort(); \
62 } \
63 return (unsigned modifier char *)LockResource(hData); \
64 } \
65 NUITKA_CONSTANTS_EXTERN_C_END
66
67#elif defined(_NUITKA_CONSTANTS_FROM_MACOS_SECTION)
68
69#include <assert.h>
70#include <dlfcn.h>
71#include <mach-o/dyld.h>
72#include <mach-o/getsect.h>
73#include <mach-o/ldsyms.h>
74#include <string.h>
75
76#ifdef __LP64__
77#define mach_header_arch mach_header_64
78#else
79#define mach_header_arch mach_header
80#endif
81
82#ifdef __cplusplus
83#define NUITKA_CONSTANTS_EXTERN_C_START extern "C" {
84#define NUITKA_CONSTANTS_EXTERN_C_END }
85#else
86#define NUITKA_CONSTANTS_EXTERN_C_START
87#define NUITKA_CONSTANTS_EXTERN_C_END
88#endif
89
90// Helper to find the mach header of the current module or executable
91static inline const struct mach_header_arch *_getNuitkaMachHeader(void) {
92#if _NUITKA_EXE_MODE
93 return &_mh_execute_header;
94#else
95 Dl_info where;
96 int res = dladdr((void *)_getNuitkaMachHeader, &where);
97 assert(res != 0);
98
99 char const *dll_filename = where.dli_fname;
100 unsigned long image_count = _dyld_image_count();
101
102 for (unsigned long i = 0; i < image_count; i++) {
103 struct mach_header const *header = _dyld_get_image_header(i);
104 if (header == NULL) {
105 continue;
106 }
107 if (strcmp(dll_filename, _dyld_get_image_name(i)) == 0) {
108 return (const struct mach_header_arch *)header;
109 }
110 }
111 return NULL;
112#endif
113}
114
115#define NUITKA_DECLARE_CONSTANT_BLOB(blob_name, blob_camel_name, modifier, res_id) \
116 NUITKA_CONSTANTS_EXTERN_C_START \
117 static inline unsigned modifier char *get##blob_camel_name##Data(void) { \
118 const struct mach_header_arch *header = _getNuitkaMachHeader(); \
119 unsigned long size; \
120 return (unsigned modifier char *)getsectiondata(header, #blob_name, #blob_name, &size); \
121 } \
122 NUITKA_CONSTANTS_EXTERN_C_END
123
124#else
125
126#ifdef __cplusplus
127#define NUITKA_CONSTANTS_EXTERN_C_START extern "C" {
128#define NUITKA_CONSTANTS_EXTERN_C_END }
129#else
130#define NUITKA_CONSTANTS_EXTERN_C_START
131#define NUITKA_CONSTANTS_EXTERN_C_END
132#endif
133
134#define NUITKA_DECLARE_CONSTANT_BLOB(blob_name, blob_camel_name, modifier, res_id) \
135 NUITKA_CONSTANTS_EXTERN_C_START \
136 extern modifier unsigned char blob_name##_data[]; \
137 NUITKA_CONSTANTS_EXTERN_C_END \
138 static inline unsigned modifier char *get##blob_camel_name##Data(void) { \
139 return (unsigned modifier char *)(blob_name##_data); \
140 }
141
142#endif // INCBIN
143
144#endif
145
146// Part of "Nuitka", an optimizing Python compiler that is compatible and
147// integrates with CPython, but also works on its own.
148//
149// Licensed under the GNU Affero General Public License, Version 3 (the "License");
150// you may not use this file except in compliance with the License.
151// You may obtain a copy of the License at
152//
153// http://www.gnu.org/licenses/agpl.txt
154//
155// Unless required by applicable law or agreed to in writing, software
156// distributed under the License is distributed on an "AS IS" BASIS,
157// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
158// See the License for the specific language governing permissions and
159// limitations under the License.