Hello Christos!

I agree with Randy, this is a good start :)

Le 13/01/2025 à 21:44, Christos Gavros via lists.openembedded.org a écrit :
> Some users have reported issues which were caused because they were missing 
> the right libstdc++-version-dev in their system.
> A new function with the name 'check_cpp_toolchain'  was added in 
> sanity.bbclass in order to verify that the host system can compile and link 
> successfully a 'hello world' c++ code.
> In case the test fails , the build will abort
> Fixes [YOCTO #bug-15712]

Patch messages are usually wrapped at 75 characters
Like in
https://www.kernel.org/doc/html/latest/process/submitting-patches.html#the-canonical-patch-format
but we did not document this rule...

(This is loosely enforced by patchtest that starts complaining at 200
character lines)

> Signed-off-by: Christos Gavros <gavr...@yahoo.com>
> ---
>  meta/classes-global/sanity.bbclass | 40 ++++++++++++++++++++++++++++++
>  1 file changed, 40 insertions(+)
> 
> diff --git a/meta/classes-global/sanity.bbclass 
> b/meta/classes-global/sanity.bbclass
> index 7b8a497d5a..ca7f8117d6 100644
> --- a/meta/classes-global/sanity.bbclass
> +++ b/meta/classes-global/sanity.bbclass
> @@ -780,6 +780,44 @@ def sanity_check_locale(d):
>      except locale.Error:
>          raise_sanity_error("Your system needs to support the en_US.UTF-8 
> locale.", d)
>  
> +def check_cpp_toolchain():
> +    # it checks if the c++ compiling and linking works properly in the 
> native system
> +    import os
> +    import subprocess
> +    from tempfile import NamedTemporaryFile
> +
> +    try:
> +        with NamedTemporaryFile(delete=False, suffix=".cpp") as cpp_file:
> +            cpp_code = """
> +            #include <iostream>
> +            int main() {
> +                std::cout << "Hello, World!" << std::endl;
> +                return 0;
> +            }
> +            """
> +            cpp_file.write(cpp_code.encode('utf-8'))
> +            cpp_file_name = cpp_file.name
> +
> +        output_binary = cpp_file_name + ".out"
> +        compile_command = ['g++', '-std=c++17', cpp_file_name, '-o', 
> output_binary]
> +        result = subprocess.run(compile_command, stdout=subprocess.PIPE, 
> stderr=subprocess.PIPE)
> +
> +        if result.returncode == 0:
> +            #os.remove(cpp_file_name)  
> +            #os.remove(output_binary)
> +            return False
> +        else:
> +            bb.fatal(f"C++ toolchain check failed during compilation or 
> linking. Error:\n{result.stderr.decode()}")

If I understood correctly bb.fatal() here will prevent other sanity
checks to run. The idea is to do as much of those checks as possible.

You can reuse the pattern seen in other checks:
the function either return an error message as a string or None. And
then, calling the function like this:
  status.addresult(check_cpp_toolchain())

> +            return True
> +    except Exception as e:
> +        bb.fatal(f"An unexpected issue occured during the C++ toolchain 
> check {str(e)}")

s/occured/occurred/

> +        return True
> +    finally:
> +        if cpp_file_name and os.path.exists(cpp_file_name):
> +            os.remove(cpp_file_name)
> +        if output_binary and os.path.exists(output_binary):
> +            os.remove(output_binary)
> +
>  def check_sanity_everybuild(status, d):
>      import os, stat
>      # Sanity tests which test the users environment so need to run at each 
> build (or are so cheap
> @@ -975,6 +1013,8 @@ def check_sanity_everybuild(status, d):
>          # forms, such as /bin/dash, bin/bash, /bin/bash.bash ...
>          if '/dash' not in real_sh and '/bash' not in real_sh:
>              status.addresult("Error, /bin/sh links to %s, must be dash or 
> bash\n" % real_sh)
> +    
> +    check_cpp_toolchain()

See above comment about calling status.addresult()

>  
>  def check_sanity(sanity_data):
>      class SanityStatus(object):

If you need help implementing the recommended changes, don't hesitate to
ask here or on IRC :)

Regards,
-- 
Yoann Congal
Smile ECS - Tech Expert

-=-=-=-=-=-=-=-=-=-=-=-
Links: You receive all messages sent to this group.
View/Reply Online (#209766): 
https://lists.openembedded.org/g/openembedded-core/message/209766
Mute This Topic: https://lists.openembedded.org/mt/110595436/21656
Group Owner: openembedded-core+ow...@lists.openembedded.org
Unsubscribe: https://lists.openembedded.org/g/openembedded-core/unsub 
[arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-

Reply via email to