diff --git a/.github/workflows/upload_buck2.yml b/.github/workflows/upload_buck2.yml index 6ee9c9399b6c9..8cccfd70553ad 100644 --- a/.github/workflows/upload_buck2.yml +++ b/.github/workflows/upload_buck2.yml @@ -144,7 +144,7 @@ jobs: cd examples/with_prelude # TODO: rust examples don't work on windows + arm64 if [[ "${{ matrix.target.triple }}" == "aarch64-pc-windows-msvc" ]]; then - TARGETS="cpp/... python/..." + TARGETS="cpp/... python/hello_world/..." else TARGETS="rust/... cpp/... python/..." fi diff --git a/examples/with_prelude/cpp/python_cextension/BUCK b/examples/with_prelude/cpp/python_cextension/BUCK deleted file mode 100644 index 98f0a08396dcc..0000000000000 --- a/examples/with_prelude/cpp/python_cextension/BUCK +++ /dev/null @@ -1,27 +0,0 @@ -# @lint-ignore BUCKLINT missing-oncall-call-severe This file is unowned - - -# DO NOT EDIT THIS FILE WITHOUT ADDING AN ONCALL - YOUR CHANGES - -# WILL BE DELETED. UNLESS THERE IS AN ONCALL ADDED THIS FILE WILL BE - -# DELETED WITHOUT NOTICE - DO NOT DEPEND ON IT. -# cxx_python_extension( -# name = "cpprint", -# srcs = ["print.cpp"], -# base_module = "", -# deps = [":python-3.10"], -# visibility = ["PUBLIC"], -# ) - -# buildifier: disable=no-effect -cxx_library( - name = "python-3.10", - exported_linker_flags = [ - "-L{}".format("/usr/lib64"), - "-lpython{}".format("3.10"), - ], - exported_preprocessor_flags = [ - "-isystem{}".format("/usr/include/python3.10"), - ], - visibility = ["PUBLIC"], -) if not host_info().os.is_windows else None diff --git a/examples/with_prelude/python/use_cextension/BUCK b/examples/with_prelude/python/use_cextension/BUCK index 3e2e5ad19a0b2..544fc26542ba9 100644 --- a/examples/with_prelude/python/use_cextension/BUCK +++ b/examples/with_prelude/python/use_cextension/BUCK @@ -5,9 +5,16 @@ # WILL BE DELETED. UNLESS THERE IS AN ONCALL ADDED THIS FILE WILL BE # DELETED WITHOUT NOTICE - DO NOT DEPEND ON IT. -# FIXME(marwhal): Get python_cextension example working again -# python_binary( -# name = "cext", -# deps = ["//cpp/python_cextension:cpprint"], -# main = "cext/cext.py" -# ) + +cxx_python_extension( + name = "cpprint", + srcs = ["print.cpp"], + base_module = "", + visibility = ["PUBLIC"], +) + +python_binary( + name = "cext", + deps = [":cpprint"], + main = "cext.py" +) diff --git a/examples/with_prelude/python/use_cextension/cext/cext.py b/examples/with_prelude/python/use_cextension/cext.py similarity index 100% rename from examples/with_prelude/python/use_cextension/cext/cext.py rename to examples/with_prelude/python/use_cextension/cext.py diff --git a/examples/with_prelude/cpp/python_cextension/print.cpp b/examples/with_prelude/python/use_cextension/print.cpp similarity index 60% rename from examples/with_prelude/cpp/python_cextension/print.cpp rename to examples/with_prelude/python/use_cextension/print.cpp index 3f4c49a5924ce..0edb96db454a7 100644 --- a/examples/with_prelude/cpp/python_cextension/print.cpp +++ b/examples/with_prelude/python/use_cextension/print.cpp @@ -30,26 +30,3 @@ static struct PyModuleDef extension = { PyMODINIT_FUNC PyInit_cpprint(void) { return PyModule_Create(&extension); } - -int main(int argc, char* argv[]) { - wchar_t* program = Py_DecodeLocale(argv[0], NULL); - if (program == NULL) { - fprintf(stderr, "Fatal error: cannot decode argv[0]\n"); - exit(1); - } - - /* Add a built-in module, before Py_Initialize */ - if (PyImport_AppendInittab("cppring", PyInit_cpprint) == -1) { - fprintf(stderr, "Error: could not extend in-built modules table\n"); - exit(1); - } - - /* Pass argv[0] to the Python interpreter */ - Py_SetProgramName(program); - - /* Initialize the Python interpreter. Required. - If this step fails, it will be a fatal error. */ - Py_Initialize(); - - return 0; -} diff --git a/prelude/python/cxx_python_extension.bzl b/prelude/python/cxx_python_extension.bzl index eb99877431d6d..cfb735b5e3db3 100644 --- a/prelude/python/cxx_python_extension.bzl +++ b/prelude/python/cxx_python_extension.bzl @@ -130,7 +130,7 @@ def cxx_python_extension_impl(ctx: AnalysisContext) -> list[Provider]: platform_compiler_flags = ctx.attrs.platform_compiler_flags, extra_link_flags = python_toolchain.extension_linker_flags, lang_platform_compiler_flags = ctx.attrs.lang_platform_compiler_flags, - preprocessor_flags = ctx.attrs.preprocessor_flags, + preprocessor_flags = python_toolchain.extension_preprocessor_flags + ctx.attrs.preprocessor_flags, lang_preprocessor_flags = ctx.attrs.lang_preprocessor_flags, platform_preprocessor_flags = ctx.attrs.platform_preprocessor_flags, lang_platform_preprocessor_flags = ctx.attrs.lang_platform_preprocessor_flags, diff --git a/prelude/python/toolchain.bzl b/prelude/python/toolchain.bzl index 17012e25b580e..92f9c3bbbd09f 100644 --- a/prelude/python/toolchain.bzl +++ b/prelude/python/toolchain.bzl @@ -59,6 +59,7 @@ PythonToolchainInfo = provider( "binary_linker_flags": provider_field(ArgLike, default = []), "extension_linker_flags": provider_field(ArgLike, default = []), "wheel_linker_flags": provider_field(ArgLike, default = []), + "extension_preprocessor_flags": provider_field(list[ArgLike], default = []), # site-packages-relative rpaths to emebed into libs/bins in the wheel "wheel_rpaths": provider_field(ArgLike, default = []), "gen_lpar_bootstrap": provider_field(Dependency | None, default = None), diff --git a/prelude/toolchains/python.bzl b/prelude/toolchains/python.bzl index 1bacf2d13cd5f..0cfd1c280fe1e 100644 --- a/prelude/toolchains/python.bzl +++ b/prelude/toolchains/python.bzl @@ -104,6 +104,7 @@ def python_toolchain_impl(ctx) -> list[Provider]: linker_flags = [], binary_linker_flags = [], extension_linker_flags = ctx.attrs.extension_linker_flags, + extension_preprocessor_flags = ctx.attrs.extension_preprocessor_flags, ), PythonPlatformInfo(name = "x86_64"), ] @@ -113,6 +114,7 @@ python_toolchain = rule( attrs = { "compile": attrs.default_only(attrs.dep(default = "prelude//python/tools:compile.py")), "extension_linker_flags": attrs.list(attrs.arg()), + "extension_preprocessor_flags": attrs.list(attrs.arg()), "interpreter": attrs.dep(providers = [RunInfo]), }, is_toolchain_rule = True, @@ -204,5 +206,9 @@ def remote_python_toolchain( "DEFAULT": ["-L$(location :cpython_archive[lib])", "@$(location :libpython_symbols)"], "prelude//os:windows": ["/LIBPATH:$(location :cpython_archive[lib])"], }), + extension_preprocessor_flags = select({ + "DEFAULT": ["-I$(location :cpython_archive[include])"], + "prelude//os:windows": ["/I$(location :cpython_archive[include])"], + }), **kwargs )