I think you should look at prior art - other recipes where this works
correctly. For example rpm, a fairly simple component, is using
python.h and linking with libpython. How is it working there?

Alex

On Thu, 4 May 2023 at 14:00, Peter Bergin <[email protected]> wrote:
>
>
> On 2023-05-04 11:00, Alexander Kanavin wrote:
> > On Thu, 4 May 2023 at 10:41, Peter Bergin <[email protected]> wrote:
> >
> >> At
> >> https://github.com/pyca/cryptography/blob/f816b457494e010b655cd7fdcd30e3446f86a703/src/rust/build.rs#L46
> >> the path to python includes is defined by calling 'python3 -c "import
> >> sysconfig; print(sysconfig.get_path('include'))"' (but from inside
> >> rust). With print-debugging this seems to return the bad string
> >> '/usr/include/python3.11' which is then passed to cc::Build
> >>
> >> I executed this in recipe context:
> >>
> >> do_compile:prepend() {
> >>       which python3
> >>       python3 -c "import sysconfig; print(sysconfig.get_path('include'))"
> >> }
> >>
> >> and then I got the correct return value:
> >>
> >> |
> >> /work/yocto/poky/build/tmp/work/core2-64-poky-linux/python3-cryptography/40.0.2-r0/recipe-sysroot-native/usr/bin/python3-native/python3
> >> |
> >> /work/yocto/poky/build/tmp/work/core2-64-poky-linux/python3-cryptography/40.0.2-r0/recipe-sysroot-native/usr/include/python3.11
> > This is pointing to the native sysroot in a target build, so it is
> > most likely not actually correct. I believe 'inherit
> > python3targetconfig' may help, as it substitutes most places where
> > native python gets queried by components with target-specific values.
> >
> > Alex
> >
> The bad thing is that the build.rs script is running python script to
> figure out the path to the headers. I'm not sure we can use target
> python exe in the build steps without running qemu instance or something
> which seems overkill in this case. We have the information about python
> include path for the target in the variable PYTHON_INCLUDE_DIR.
>
> With this patch the compilation will find Python.h:
>
> $ git diff src/rust/build.rs
> diff --git a/src/rust/build.rs b/src/rust/build.rs
> index 01177ac..7dc13fe 100644
> --- a/src/rust/build.rs
> +++ b/src/rust/build.rs
> @@ -43,11 +43,8 @@ fn main() {
>       )
>       .unwrap();
>       println!("cargo:rustc-cfg=python_implementation=\"{}\"", python_impl);
> -    let python_include = run_python_script(
> -        &python,
> -        "import sysconfig; print(sysconfig.get_path('include'), end='')",
> -    )
> -    .unwrap();
> +    let python_include = env::var("PYTHON_INCLUDE_DIR").unwrap();
> +
>       let openssl_include =
>           std::env::var_os("DEP_OPENSSL_INCLUDE").expect("unable to find
> openssl include path");
>       let openssl_c = Path::new(&out_dir).join("_openssl.c");
>
>
> But it fails later in the linking step.
>
> .....linux/python3-cryptography/40.0.2-r0/recipe-sysroot/usr/lib/rustlib/x86_64-poky-linux-gnu/lib"
> "-L"
> "/work/yocto/poky/build/tmp/work/core2-64-poky-linux/python3-cryptography/40.0.2-r0/python3-cryptography-40.0.2/target/x86_64-poky-linux-gnu/release/build/cryptography-rust-aa00e39a952c07ee/out"
> "-L"
> "/work/yocto/poky/build/tmp/work/core2-64-poky-linux/python3-cryptography/40.0.2-r0/recipe-sysroot/usr/lib"
> "-L"
> "/work/yocto/poky/build/tmp/work/core2-64-poky-linux/python3-cryptography/40.0.2-r0/recipe-sysroot-native/usr/lib/rustlib/x86_64-poky-linux-gnu/lib"
> "-Wl,-Bstatic" "-l_openssl.a"
> "/work/yocto/poky/build/tmp/work/core2-64-poky-linux/python3-cryptography/40.0.2-r0/recipe-sysroot/usr/lib/rustlib/x86_64-poky-linux-gnu/lib/libcompiler_builtins-45df5d471f0d20d0.rlib"
> "-Wl,-Bdynamic" "-lssl" "-lcrypto" "-lgcc_s" "-lutil" "-lrt" "-lpthread"
> "-lm" "-ldl" "-lc" "-Wl,--eh-frame-hdr" "-Wl,-znoexecstack" "-L"
> "/work/yocto/poky/build/tmp/work/core2-64-poky-linux/python3-cryptography/40.0.2-r0/recipe-sysroot-native/usr/lib/rustlib/x86_64-poky-linux-gnu/lib"
> "-o"
> "/work/yocto/poky/build/tmp/work/core2-64-poky-linux/python3-cryptography/40.0.2-r0/python3-cryptography-40.0.2/target/x86_64-poky-linux-gnu/release/deps/libcryptography_rust.so"
> "-Wl,--gc-sections" "-shared" "-Wl,-O1" "-nodefaultlibs"
>    = note:
> /work/yocto/poky/build/tmp/work/core2-64-poky-linux/python3-cryptography/40.0.2-r0/recipe-sysroot-native/usr/bin/x86_64-poky-linux/../../libexec/x86_64-poky-linux/gcc/x86_64-poky-linux/12.2.0/ld:
> /work/yocto/poky/build/tmp/work/core2-64-poky-linux/python3-cryptography/40.0.2-r0/python3-cryptography-40.0.2/target/x86_64-poky-linux-gnu/release/build/cryptography-rust-aa00e39a952c07ee/out/lib_openssl.a.a(9bdf47a443b7a07f-_openssl.o):
> warning: relocation against `_Py_NoneStruct' in read-only section `.text'
> /work/yocto/poky/build/tmp/work/core2-64-poky-linux/python3-cryptography/40.0.2-r0/recipe-sysroot-native/usr/bin/x86_64-poky-linux/../../libexec/x86_64-poky-linux/gcc/x86_64-poky-linux/12.2.0/ld:
> /work/yocto/poky/build/tmp/work/core2-64-poky-linux/python3-cryptography/40.0.2-r0/python3-cryptography-40.0.2/target/x86_64-poky-linux-gnu/release/build/cryptography-rust-aa00e39a952c07ee/out/lib_openssl.a.a(9bdf47a443b7a07f-_openssl.o):
> relocation R_X86_64_PC32 against undefined symbol `_Py_NoneStruct' can
> not be used when making a shared object; recompile with -fPIC
> /work/yocto/poky/build/tmp/work/core2-64-poky-linux/python3-cryptography/40.0.2-r0/recipe-sysroot-native/usr/bin/x86_64-poky-linux/../../libexec/x86_64-poky-linux/gcc/x86_64-poky-linux/12.2.0/ld:
> final link failed: bad value
>            collect2: error: ld returned 1 exit status
>
>
> There are some '-L .../recipe-sysroot-native/usr/lib/rustlib/...' that
> can probably causing this. Don't know if it helps us forward?
>
> /Peter
>
-=-=-=-=-=-=-=-=-=-=-=-
Links: You receive all messages sent to this group.
View/Reply Online (#180871): 
https://lists.openembedded.org/g/openembedded-core/message/180871
Mute This Topic: https://lists.openembedded.org/mt/98667212/21656
Group Owner: [email protected]
Unsubscribe: https://lists.openembedded.org/g/openembedded-core/unsub 
[[email protected]]
-=-=-=-=-=-=-=-=-=-=-=-

Reply via email to