2020-12-11 00:30:53 +01:00
|
|
|
"""Generate and run C code.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Copyright The Mbed TLS Contributors
|
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
# not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
import os
|
|
|
|
import platform
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import tempfile
|
|
|
|
|
|
|
|
def remove_file_if_exists(filename):
|
|
|
|
"""Remove the specified file, ignoring errors."""
|
|
|
|
if not filename:
|
|
|
|
return
|
|
|
|
try:
|
|
|
|
os.remove(filename)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def create_c_file(file_label):
|
|
|
|
"""Create a temporary C file.
|
|
|
|
|
|
|
|
* ``file_label``: a string that will be included in the file name.
|
|
|
|
|
|
|
|
Return ```(c_file, c_name, exe_name)``` where ``c_file`` is a Python
|
|
|
|
stream open for writing to the file, ``c_name`` is the name of the file
|
|
|
|
and ``exe_name`` is the name of the executable that will be produced
|
|
|
|
by compiling the file.
|
|
|
|
"""
|
|
|
|
c_fd, c_name = tempfile.mkstemp(prefix='tmp-{}-'.format(file_label),
|
|
|
|
suffix='.c')
|
|
|
|
exe_suffix = '.exe' if platform.system() == 'Windows' else ''
|
|
|
|
exe_name = c_name[:-2] + exe_suffix
|
|
|
|
remove_file_if_exists(exe_name)
|
|
|
|
c_file = os.fdopen(c_fd, 'w', encoding='ascii')
|
2021-05-12 10:11:53 +02:00
|
|
|
return c_file, c_name, exe_name
|
2020-12-11 00:30:53 +01:00
|
|
|
|
|
|
|
def generate_c_printf_expressions(c_file, cast_to, printf_format, expressions):
|
|
|
|
"""Generate C instructions to print the value of ``expressions``.
|
|
|
|
|
|
|
|
Write the code with ``c_file``'s ``write`` method.
|
|
|
|
|
|
|
|
Each expression is cast to the type ``cast_to`` and printed with the
|
|
|
|
printf format ``printf_format``.
|
|
|
|
"""
|
|
|
|
for expr in expressions:
|
|
|
|
c_file.write(' printf("{}\\n", ({}) {});\n'
|
|
|
|
.format(printf_format, cast_to, expr))
|
|
|
|
|
|
|
|
def generate_c_file(c_file,
|
|
|
|
caller, header,
|
|
|
|
main_generator):
|
|
|
|
"""Generate a temporary C source file.
|
|
|
|
|
|
|
|
* ``c_file`` is an open stream on the C source file.
|
|
|
|
* ``caller``: an informational string written in a comment at the top
|
|
|
|
of the file.
|
|
|
|
* ``header``: extra code to insert before any function in the generated
|
|
|
|
C file.
|
|
|
|
* ``main_generator``: a function called with ``c_file`` as its sole argument
|
|
|
|
to generate the body of the ``main()`` function.
|
|
|
|
"""
|
|
|
|
c_file.write('/* Generated by {} */'
|
|
|
|
.format(caller))
|
|
|
|
c_file.write('''
|
|
|
|
#include <stdio.h>
|
|
|
|
''')
|
|
|
|
c_file.write(header)
|
|
|
|
c_file.write('''
|
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
''')
|
|
|
|
main_generator(c_file)
|
|
|
|
c_file.write(''' return 0;
|
|
|
|
}
|
|
|
|
''')
|
|
|
|
|
|
|
|
def get_c_expression_values(
|
|
|
|
cast_to, printf_format,
|
|
|
|
expressions,
|
|
|
|
caller=__name__, file_label='',
|
|
|
|
header='', include_path=None,
|
|
|
|
keep_c=False,
|
2021-05-06 15:14:16 +02:00
|
|
|
): # pylint: disable=too-many-arguments, too-many-locals
|
2020-12-11 00:30:53 +01:00
|
|
|
"""Generate and run a program to print out numerical values for expressions.
|
|
|
|
|
|
|
|
* ``cast_to``: a C type.
|
|
|
|
* ``printf_format``: a printf format suitable for the type ``cast_to``.
|
|
|
|
* ``header``: extra code to insert before any function in the generated
|
|
|
|
C file.
|
|
|
|
* ``expressions``: a list of C language expressions that have the type
|
2021-01-19 21:19:02 +01:00
|
|
|
``cast_to``.
|
2020-12-11 00:30:53 +01:00
|
|
|
* ``include_path``: a list of directories containing header files.
|
|
|
|
* ``keep_c``: if true, keep the temporary C file (presumably for debugging
|
|
|
|
purposes).
|
|
|
|
|
2021-04-23 22:07:25 +02:00
|
|
|
Use the C compiler specified by the ``CC`` environment variable, defaulting
|
|
|
|
to ``cc``. If ``CC`` looks like MSVC, use its command line syntax,
|
|
|
|
otherwise assume the compiler supports Unix traditional ``-I`` and ``-o``.
|
|
|
|
|
2020-12-11 00:30:53 +01:00
|
|
|
Return the list of values of the ``expressions``.
|
|
|
|
"""
|
|
|
|
if include_path is None:
|
|
|
|
include_path = []
|
|
|
|
c_name = None
|
|
|
|
exe_name = None
|
2021-05-12 10:11:53 +02:00
|
|
|
obj_name = None
|
2020-12-11 00:30:53 +01:00
|
|
|
try:
|
2021-05-12 10:11:53 +02:00
|
|
|
c_file, c_name, exe_name = create_c_file(file_label)
|
2020-12-11 00:30:53 +01:00
|
|
|
generate_c_file(
|
|
|
|
c_file, caller, header,
|
|
|
|
lambda c_file: generate_c_printf_expressions(c_file,
|
|
|
|
cast_to, printf_format,
|
|
|
|
expressions)
|
|
|
|
)
|
|
|
|
c_file.close()
|
|
|
|
cc = os.getenv('CC', 'cc')
|
2021-04-23 22:07:25 +02:00
|
|
|
cmd = [cc]
|
2021-05-10 20:50:07 +02:00
|
|
|
|
2021-05-12 10:28:30 +02:00
|
|
|
proc = subprocess.Popen(cmd,
|
|
|
|
stdout=subprocess.DEVNULL,
|
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
universal_newlines=True)
|
|
|
|
cc_is_msvc = 'Microsoft (R) C/C++ Optimizing Compiler' in \
|
|
|
|
proc.communicate()[1]
|
2021-05-10 20:50:07 +02:00
|
|
|
|
2021-04-23 22:07:25 +02:00
|
|
|
cmd += ['-I' + dir for dir in include_path]
|
2021-05-12 10:28:30 +02:00
|
|
|
if cc_is_msvc:
|
2021-05-10 23:56:29 +02:00
|
|
|
# MSVC has deprecated using -o to specify the output file,
|
|
|
|
# and produces an object file in the working directory by default.
|
2021-05-12 10:11:53 +02:00
|
|
|
obj_name = exe_name[:-4] + '.obj'
|
2021-05-10 23:56:29 +02:00
|
|
|
cmd += ['-Fe' + exe_name, '-Fo' + obj_name]
|
|
|
|
else:
|
|
|
|
cmd += ['-o' + exe_name]
|
2021-04-23 22:07:25 +02:00
|
|
|
subprocess.check_call(cmd + [c_name])
|
2020-12-11 00:30:53 +01:00
|
|
|
if keep_c:
|
|
|
|
sys.stderr.write('List of {} tests kept at {}\n'
|
|
|
|
.format(caller, c_name))
|
|
|
|
else:
|
|
|
|
os.remove(c_name)
|
|
|
|
output = subprocess.check_output([exe_name])
|
|
|
|
return output.decode('ascii').strip().split('\n')
|
|
|
|
finally:
|
|
|
|
remove_file_if_exists(exe_name)
|
2021-05-12 10:21:39 +02:00
|
|
|
remove_file_if_exists(obj_name)
|