194aa3
commit 841afa116e32b3c7195475769c26bf46fd870d32
194aa3
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
194aa3
Date:   Wed Aug 10 16:24:06 2022 -0300
194aa3
194aa3
    glibcextract.py: Add compile_c_snippet
194aa3
    
194aa3
    It might be used on tests to check if a snippet build with the provided
194aa3
    compiler and flags.
194aa3
    
194aa3
    Reviewed-by: Florian Weimer <fweimer@redhat.com>
194aa3
194aa3
diff --git a/scripts/glibcextract.py b/scripts/glibcextract.py
194aa3
index 8f2246aae6a9dfb7..0fb50dc8f9c4f7f9 100644
194aa3
--- a/scripts/glibcextract.py
194aa3
+++ b/scripts/glibcextract.py
194aa3
@@ -17,6 +17,7 @@
194aa3
 # License along with the GNU C Library; if not, see
194aa3
 # <http://www.gnu.org/licenses/>.
194aa3
 
194aa3
+import collections
194aa3
 import os.path
194aa3
 import re
194aa3
 import subprocess
194aa3
@@ -173,3 +174,21 @@ def compare_macro_consts(source_1, source_2, cc, macro_re, exclude_re=None,
194aa3
             if not allow_extra_2:
194aa3
                 ret = 1
194aa3
     return ret
194aa3
+
194aa3
+CompileResult = collections.namedtuple("CompileResult", "returncode output")
194aa3
+
194aa3
+def compile_c_snippet(snippet, cc, extra_cc_args=''):
194aa3
+    """Compile and return whether the SNIPPET can be build with CC along
194aa3
+       EXTRA_CC_ARGS compiler flags.  Return a CompileResult with RETURNCODE
194aa3
+       being 0 for success, or the failure value and the compiler output.
194aa3
+    """
194aa3
+    with tempfile.TemporaryDirectory() as temp_dir:
194aa3
+        c_file_name = os.path.join(temp_dir, 'test.c')
194aa3
+        obj_file_name = os.path.join(temp_dir, 'test.o')
194aa3
+        with open(c_file_name, 'w') as c_file:
194aa3
+            c_file.write(snippet + '\n')
194aa3
+        cmd = cc.split() + extra_cc_args.split() + ['-c', '-o', obj_file_name,
194aa3
+                c_file_name]
194aa3
+        r = subprocess.run(cmd, check=False, stdout=subprocess.PIPE,
194aa3
+                stderr=subprocess.STDOUT)
194aa3
+        return CompileResult(r.returncode, r.stdout)