]> granicus.if.org Git - graphviz/commitdiff
helper test function for compiling C code
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Sat, 27 Feb 2021 03:04:07 +0000 (19:04 -0800)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Sat, 6 Mar 2021 01:30:19 +0000 (17:30 -0800)
rtest/test_regression.py

index 5f211d7a3bd9dc9f70e7e948a1bf2af91f3ee1b5..a18888f2ebd77d6eb7a511c638de6a0278eefb63 100644 (file)
@@ -8,6 +8,7 @@ import os
 import re
 import stat
 import tempfile
+from typing import List, Optional
 
 # The terminology used in rtest.py is a little inconsistent. At the
 # end it reports the total number of tests, the number of "failures"
@@ -38,6 +39,41 @@ def test_regression_failure():
 # FIXME: re-enable when all tests pass on all platforms
 #    assert result.returncode == 0
 
+def compile_c(src: Path, link: List[str] = [], dst: Optional[Path] = None) \
+  -> Path:
+    '''
+    compile a C program
+    '''
+
+    # if the user did not give us destination, use a temporary path
+    if dst is None:
+        _, dst = tempfile.mkstemp('.exe')
+
+    if platform.system() == 'Windows':
+        # determine which runtime library option we need
+        rtflag = '-MDd' if os.environ.get('configuration') == 'Debug' else '-MD'
+
+        # construct an invocation of MSVC
+        args = ['cl', src, '-Fe:', dst, '-nologo', rtflag]
+        if len(link) > 0:
+            args += ['-link'] + [f'{l}.lib' for l in link]
+
+    else:
+        # construct an invocation of the default C compiler
+        cc = os.environ.get('CC', 'cc')
+        args = [cc, '-std=c99', src, '-o', dst]
+        if len(link) > 0:
+            args += [f'-l{l}' for l in link]
+
+    # compile the program
+    try:
+        subprocess.check_call(args)
+    except subprocess.CalledProcessError:
+        dst.unlink(missing_ok=True)
+        raise
+
+    return dst
+
 def test_165():
     '''
     dot should be able to produce properly escaped xdot output