Skip to contentSkip to conversation

Sanity test for cpp toolchain

PATCH Superseded Christos Gavros 5 participants 5 messages Jan 13, 2025 – Jan 15, 2025 1 message not in the archive

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH 1/1 Sanity test for cpp toolchain

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]

---
 meta/classes-global/sanity.bbclass | 40 ++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)
meta/classes-global/sanity.bbclass +40 -0
index 7b8a497d5a..ca7f8117d6 100644
@@ -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()}")
+            return True
+    except Exception as e:
+        bb.fatal(f"An unexpected issue occured during the C++ toolchain check {str(e)}")
+        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()
 
 def check_sanity(sanity_data):
     class SanityStatus(object):
Signature · 1 line
2.34.1
#2 lore raw

Patchtest results for [meta-oe][PATCH] Sanity test for cpp toolchain

Thank you for your submission. Patchtest identified one or more issues with the patch. Please see the log below for more information:

---
Testing patch /home/patchtest/share/mboxes/meta-oe-Sanity-test-for-cpp-toolchain.patch

FAIL: test bugzilla entry format: Bugzilla issue ID is not correctly formatted - specify it with format: "[YOCTO #<bugzilla ID>]" (test_mbox.TestMbox.test_bugzilla_entry_format)
FAIL: test shortlog format: Commit shortlog (first line of commit message) should follow the format "<target>: <summary>" (test_mbox.TestMbox.test_shortlog_format)
FAIL: test target mailing list: Series sent to the wrong mailing list or some patches from the series correspond to different mailing lists (test_mbox.TestMbox.test_target_mailing_list)

PASS: test Signed-off-by presence (test_mbox.TestMbox.test_signed_off_by_presence)
PASS: test author valid (test_mbox.TestMbox.test_author_valid)
PASS: test commit message presence (test_mbox.TestMbox.test_commit_message_presence)
PASS: test commit message user tags (test_mbox.TestMbox.test_commit_message_user_tags)
PASS: test max line length (test_metadata.TestMetadata.test_max_line_length)
PASS: test mbox format (test_mbox.TestMbox.test_mbox_format)
PASS: test non-AUH upgrade (test_mbox.TestMbox.test_non_auh_upgrade)
PASS: test shortlog length (test_mbox.TestMbox.test_shortlog_length)

SKIP: pretest pylint: No python related patches, skipping test (test_python_pylint.PyLint.pretest_pylint)
SKIP: pretest src uri left files: No modified recipes, skipping pretest (test_metadata.TestMetadata.pretest_src_uri_left_files)
SKIP: test CVE check ignore: No modified recipes or older target branch, skipping test (test_metadata.TestMetadata.test_cve_check_ignore)
SKIP: test CVE tag format: No new CVE patches introduced (test_patch.TestPatch.test_cve_tag_format)
SKIP: test Signed-off-by presence: No new CVE patches introduced (test_patch.TestPatch.test_signed_off_by_presence)
SKIP: test Upstream-Status presence: No new CVE patches introduced (test_patch.TestPatch.test_upstream_status_presence_format)
SKIP: test lic files chksum modified not mentioned: No modified recipes, skipping test (test_metadata.TestMetadata.test_lic_files_chksum_modified_not_mentioned)
SKIP: test lic files chksum presence: No added recipes, skipping test (test_metadata.TestMetadata.test_lic_files_chksum_presence)
SKIP: test license presence: No added recipes, skipping test (test_metadata.TestMetadata.test_license_presence)
SKIP: test pylint: No python related patches, skipping test (test_python_pylint.PyLint.test_pylint)
SKIP: test series merge on head: Merge test is disabled for now (test_mbox.TestMbox.test_series_merge_on_head)
SKIP: test src uri left files: No modified recipes, skipping pretest (test_metadata.TestMetadata.test_src_uri_left_files)
SKIP: test summary presence: No added recipes, skipping test (test_metadata.TestMetadata.test_summary_presence)

---

Please address the issues identified and
submit a new revision of the patch, or alternatively, reply to this email with an explanation of why the patch should be accepted. If you believe these results are due to an error in patchtest, please submit a bug at https://bugzilla.yoctoproject.org/ (use the 'Patchtest' category under 'Yocto Project Subprojects'). For more information on specific failures, see: https://wiki.yoctoproject.org/wiki/Patchtest. Thank you!

#3 lore raw
In reply to Christos Gavros
Quoted 4 lines · Christos Gavros via lists.openembedded.org

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]

Thanks for the patch. It's a good start!

Repeating what I said on IRC about the patchtest errors:

Maybe shortlog: sanity: test for cpp toolchain

Drop the meta-oe prefix, this is oe-core and your email will automagically get an oe-core prefix as you can see above.

YP BZ # is optional but patchtest documented the expected format.

Quoted 11 lines
---
  meta/classes-global/sanity.bbclass | 40 ++++++++++++++++++++++++++++++
  1 file changed, 40 insertions(+)
meta/classes-global/sanity.bbclass +0 -0
index 7b8a497d5a..ca7f8117d6 100644

meta/classes-global/sanity.bbclass def sanity_check_locale(d): · line 784

Quoted 5 lines
meta/classes-global/sanity.bbclass +2 -0
@@ -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

The bug talks about testing, "linking to libstdc++" so best to mention that rather than have it implied.

Quoted 8 lines
meta/classes-global/sanity.bbclass +8 -0
+    import os
+    import subprocess
+    from tempfile import NamedTemporaryFile
+
+    try:
+        with NamedTemporaryFile(delete=False, suffix=".cpp") as cpp_file:
+            cpp_code = """
+            #include <iostream>

meta/classes-global/sanity.bbclass def sanity_check_locale(d): · line 802

Quoted 10 lines
meta/classes-global/sanity.bbclass +10 -0
+            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]

Maybe use BUILD_CC here?

Oddly, that variable isn't in the docs but BUILD_CC_ARCH is:
https://docs.yoctoproject.org/ref-manual/variables.html#term-BUILD_CC_ARCH

CC-ing Antonin to see if that's a bug.

Yocto/OE supports  or would like support more than just GCC mostly for target but it would be nice to use the compiler that BUILD_CC points to so that there isn't too much gcc hard-coded.

meta/classes-global/sanity.bbclass def sanity_check_locale(d): · line 807

Quoted 5 lines
meta/classes-global/sanity.bbclass +5 -0
+        result = subprocess.run(compile_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+
+        if result.returncode == 0:
+            #os.remove(cpp_file_name)
+            #os.remove(output_binary)

Did you mean to leave these two lines commented out? If so why? Add a comment explaining why if needed.

../Randy

Quoted 34 lines
meta/classes-global/sanity.bbclass +15 -1
+            return False
+        else:
+            bb.fatal(f"C++ toolchain check failed during compilation or linking. Error:\n{result.stderr.decode()}")
+            return True
+    except Exception as e:
+        bb.fatal(f"An unexpected issue occured during the C++ toolchain check {str(e)}")
+        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()
 
  def check_sanity(sanity_data):
      class SanityStatus(object):
 
-=-=-=-=-=-=-=-=-=-=-=-

Links: You receive all messages sent to this group.
View/Reply Online (#209743):https://lists.openembedded.org/g/openembedded-core/message/209743 Mute This Topic:https://lists.openembedded.org/mt/110595436/3616765
Group Owner:[email protected] Unsubscribe:https://lists.openembedded.org/g/openembedded-core/unsub [[email protected]]
-=-=-=-=-=-=-=-=-=-=-=-

Signature · 2 lines
# Randy MacLeod
# Wind River Linux
#4 lore raw

Hi Randy,

On Mon Jan 13, 2025 at 11:02 PM CET, Randy MacLeod wrote:
[...]

Quoted 10 lines

meta/classes-global/sanity.bbclass def sanity_check_locale(d): · line 802

Quoted 3 lines
meta/classes-global/sanity.bbclass +3 -0
+
+        output_binary = cpp_file_name + ".out"
+        compile_command = ['g++', '-std=c++17', cpp_file_name, '-o', output_binary]

Maybe use BUILD_CC here?

Oddly, that variable isn't in the docs but BUILD_CC_ARCH is:
https://docs.yoctoproject.org/ref-manual/variables.html#term-BUILD_CC_ARCH

CC-ing Antonin to see if that's a bug.

Looks like a bug to me :) I've created one:
https://bugzilla.yoctoproject.org/show_bug.cgi?id=15719

Thanks!
Antonin

Signature · 3 lines
Antonin Godard, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#5 lore raw
In reply to Christos Gavros

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 :

Quoted 4 lines

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)

Quoted 31 lines
---
 meta/classes-global/sanity.bbclass | 40 ++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)
meta/classes-global/sanity.bbclass +18 -0
index 7b8a497d5a..ca7f8117d6 100644
@@ -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
+

meta/classes-global/sanity.bbclass def sanity_check_locale(d): · line 810

Quoted 10 lines
meta/classes-global/sanity.bbclass +10 -0
+        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())

meta/classes-global/sanity.bbclass def sanity_check_locale(d): · line 813

Quoted 3 lines
meta/classes-global/sanity.bbclass +3 -0
+            return True
+    except Exception as e:
+        bb.fatal(f"An unexpected issue occured during the C++ toolchain check {str(e)}")

s/occured/occurred/

Quoted 11 lines
meta/classes-global/sanity.bbclass +7 -0
+        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

meta/classes-global/sanity.bbclass def check_sanity_everybuild(status, d): · line 1017

Quoted 5 lines
meta/classes-global/sanity.bbclass +2 -0
@@ -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()

meta/classes-global/sanity.bbclass def check_sanity_everybuild(status, d): · line 1020

Quoted 3 lines
meta/classes-global/sanity.bbclass +0 -0
 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,

Signature · 2 lines
Yoann Congal
Smile ECS - Tech Expert