Skip to content

Commit 3dd63e7

Browse files
committed
python_toolchain: set and use preprocessor flags for extensions
1 parent 30fe8cb commit 3dd63e7

File tree

7 files changed

+21
-57
lines changed

7 files changed

+21
-57
lines changed

examples/with_prelude/cpp/python_cextension/BUCK

Lines changed: 0 additions & 27 deletions
This file was deleted.

examples/with_prelude/python/use_cextension/BUCK

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,16 @@
55
# WILL BE DELETED. UNLESS THERE IS AN ONCALL ADDED THIS FILE WILL BE
66

77
# DELETED WITHOUT NOTICE - DO NOT DEPEND ON IT.
8-
# FIXME(marwhal): Get python_cextension example working again
9-
# python_binary(
10-
# name = "cext",
11-
# deps = ["//cpp/python_cextension:cpprint"],
12-
# main = "cext/cext.py"
13-
# )
8+
9+
cxx_python_extension(
10+
name = "cpprint",
11+
srcs = ["print.cpp"],
12+
base_module = "",
13+
visibility = ["PUBLIC"],
14+
)
15+
16+
python_binary(
17+
name = "cext",
18+
deps = [":cpprint"],
19+
main = "cext.py"
20+
)

examples/with_prelude/cpp/python_cextension/print.cpp renamed to examples/with_prelude/python/use_cextension/print.cpp

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,3 @@ static struct PyModuleDef extension = {
3030
PyMODINIT_FUNC PyInit_cpprint(void) {
3131
return PyModule_Create(&extension);
3232
}
33-
34-
int main(int argc, char* argv[]) {
35-
wchar_t* program = Py_DecodeLocale(argv[0], NULL);
36-
if (program == NULL) {
37-
fprintf(stderr, "Fatal error: cannot decode argv[0]\n");
38-
exit(1);
39-
}
40-
41-
/* Add a built-in module, before Py_Initialize */
42-
if (PyImport_AppendInittab("cppring", PyInit_cpprint) == -1) {
43-
fprintf(stderr, "Error: could not extend in-built modules table\n");
44-
exit(1);
45-
}
46-
47-
/* Pass argv[0] to the Python interpreter */
48-
Py_SetProgramName(program);
49-
50-
/* Initialize the Python interpreter. Required.
51-
If this step fails, it will be a fatal error. */
52-
Py_Initialize();
53-
54-
return 0;
55-
}

prelude/python/cxx_python_extension.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def cxx_python_extension_impl(ctx: AnalysisContext) -> list[Provider]:
130130
platform_compiler_flags = ctx.attrs.platform_compiler_flags,
131131
extra_link_flags = python_toolchain.extension_linker_flags,
132132
lang_platform_compiler_flags = ctx.attrs.lang_platform_compiler_flags,
133-
preprocessor_flags = ctx.attrs.preprocessor_flags,
133+
preprocessor_flags = python_toolchain.extension_preprocessor_flags + ctx.attrs.preprocessor_flags,
134134
lang_preprocessor_flags = ctx.attrs.lang_preprocessor_flags,
135135
platform_preprocessor_flags = ctx.attrs.platform_preprocessor_flags,
136136
lang_platform_preprocessor_flags = ctx.attrs.lang_platform_preprocessor_flags,

prelude/python/toolchain.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ PythonToolchainInfo = provider(
5959
"binary_linker_flags": provider_field(ArgLike, default = []),
6060
"extension_linker_flags": provider_field(ArgLike, default = []),
6161
"wheel_linker_flags": provider_field(ArgLike, default = []),
62+
"extension_preprocessor_flags": provider_field(list[ArgLike], default = []),
6263
# site-packages-relative rpaths to emebed into libs/bins in the wheel
6364
"wheel_rpaths": provider_field(ArgLike, default = []),
6465
"gen_lpar_bootstrap": provider_field(Dependency | None, default = None),

prelude/toolchains/python.bzl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ def python_toolchain_impl(ctx) -> list[Provider]:
104104
linker_flags = [],
105105
binary_linker_flags = [],
106106
extension_linker_flags = ctx.attrs.extension_linker_flags,
107+
extension_preprocessor_flags = ctx.attrs.extension_preprocessor_flags,
107108
),
108109
PythonPlatformInfo(name = "x86_64"),
109110
]
@@ -113,6 +114,7 @@ python_toolchain = rule(
113114
attrs = {
114115
"compile": attrs.default_only(attrs.dep(default = "prelude//python/tools:compile.py")),
115116
"extension_linker_flags": attrs.list(attrs.arg()),
117+
"extension_preprocessor_flags": attrs.list(attrs.arg()),
116118
"interpreter": attrs.dep(providers = [RunInfo]),
117119
},
118120
is_toolchain_rule = True,
@@ -204,5 +206,9 @@ def remote_python_toolchain(
204206
"DEFAULT": ["-L$(location :cpython_archive[lib])", "@$(location :libpython_symbols)"],
205207
"prelude//os:windows": ["/LIBPATH:$(location :cpython_archive[lib])"],
206208
}),
209+
extension_preprocessor_flags = select({
210+
"DEFAULT": ["-I$(location :cpython_archive[include])"],
211+
"prelude//os:windows": ["/I$(location :cpython_archive[include])"],
212+
}),
207213
**kwargs
208214
)

0 commit comments

Comments
 (0)