[v5] glibcextract.py: Add compile_c_snippet

Message ID 20220811165909.1854953-1-adhemerval.zanella@linaro.org
State Committed
Commit 841afa116e32b3c7195475769c26bf46fd870d32
Headers
Series [v5] glibcextract.py: Add compile_c_snippet |

Checks

Context Check Description
dj/TryBot-apply_patch success Patch applied to master at the time it was sent
dj/TryBot-32bit success Build for i686

Commit Message

Adhemerval Zanella Aug. 11, 2022, 4:59 p.m. UTC
  It might be used on tests to check if a snippet build with the provided
compiler and flags.
---
 scripts/glibcextract.py | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
  

Comments

Florian Weimer Aug. 11, 2022, 9:52 p.m. UTC | #1
* Adhemerval Zanella:

> It might be used on tests to check if a snippet build with the provided
> compiler and flags.
> ---
>  scripts/glibcextract.py | 19 +++++++++++++++++++
>  1 file changed, 19 insertions(+)
>
> diff --git a/scripts/glibcextract.py b/scripts/glibcextract.py
> index 43ab58ffe2..36d204c9b0 100644
> --- a/scripts/glibcextract.py
> +++ b/scripts/glibcextract.py
> @@ -17,6 +17,7 @@
>  # License along with the GNU C Library; if not, see
>  # <https://www.gnu.org/licenses/>.
>  
> +import collections
>  import os.path
>  import re
>  import subprocess
> @@ -173,3 +174,21 @@ def compare_macro_consts(source_1, source_2, cc, macro_re, exclude_re=None,
>              if not allow_extra_2:
>                  ret = 1
>      return ret
> +
> +CompileResult = collections.namedtuple("CompileResult", "returncode output")
> +
> +def compile_c_snippet(snippet, cc, extra_cc_args=''):
> +    """Compile and return whether the SNIPPET can be build with CC along
> +       EXTRA_CC_ARGS compiler flags.  Return a CompileResult with RETURNCODE
> +       being 0 for success, or the failure value and the compiler output.
> +    """
> +    with tempfile.TemporaryDirectory() as temp_dir:
> +        c_file_name = os.path.join(temp_dir, 'test.c')
> +        obj_file_name = os.path.join(temp_dir, 'test.o')
> +        with open(c_file_name, 'w') as c_file:
> +            c_file.write(snippet + '\n')
> +        cmd = cc.split() + extra_cc_args.split() + ['-c', '-o', obj_file_name,
> +                c_file_name]
> +        r = subprocess.run(cmd, check=False, stdout=subprocess.PIPE,
> +                stderr=subprocess.STDOUT)
> +        return CompileResult(r.returncode, r.stdout)

This version looks good and works with Python 3.6.

Reviewed-by: Florian Weimer <fweimer@redhat.com>

Thanks,
Florian
  

Patch

diff --git a/scripts/glibcextract.py b/scripts/glibcextract.py
index 43ab58ffe2..36d204c9b0 100644
--- a/scripts/glibcextract.py
+++ b/scripts/glibcextract.py
@@ -17,6 +17,7 @@ 
 # License along with the GNU C Library; if not, see
 # <https://www.gnu.org/licenses/>.
 
+import collections
 import os.path
 import re
 import subprocess
@@ -173,3 +174,21 @@  def compare_macro_consts(source_1, source_2, cc, macro_re, exclude_re=None,
             if not allow_extra_2:
                 ret = 1
     return ret
+
+CompileResult = collections.namedtuple("CompileResult", "returncode output")
+
+def compile_c_snippet(snippet, cc, extra_cc_args=''):
+    """Compile and return whether the SNIPPET can be build with CC along
+       EXTRA_CC_ARGS compiler flags.  Return a CompileResult with RETURNCODE
+       being 0 for success, or the failure value and the compiler output.
+    """
+    with tempfile.TemporaryDirectory() as temp_dir:
+        c_file_name = os.path.join(temp_dir, 'test.c')
+        obj_file_name = os.path.join(temp_dir, 'test.o')
+        with open(c_file_name, 'w') as c_file:
+            c_file.write(snippet + '\n')
+        cmd = cc.split() + extra_cc_args.split() + ['-c', '-o', obj_file_name,
+                c_file_name]
+        r = subprocess.run(cmd, check=False, stdout=subprocess.PIPE,
+                stderr=subprocess.STDOUT)
+        return CompileResult(r.returncode, r.stdout)