Skip to content

Commit

Permalink
Added option -C/--config to pixie cli: Takes input a json configurati…
Browse files Browse the repository at this point in the history
…on file which specifies translation units and export configurations
  • Loading branch information
kc611 committed Jul 2, 2024
1 parent 432c067 commit e222420
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 3 deletions.
46 changes: 43 additions & 3 deletions pixie/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import subprocess
import sysconfig
import tempfile

import json
from llvmlite import binding as llvm

from pixie.compiler import (
Expand Down Expand Up @@ -93,13 +93,53 @@ def pixie_cc():
"-O", metavar="<n>", type=int, nargs=1, help="optimization level"
)
parser.add_argument("c-source", help="input source file")
parser.add_argument("-C","--config", help="Configuration JSON file")
args = parser.parse_args()
# TODO: verbose
print(args)

def load_config(config_file):
with open(config_file, 'r') as f:
config = json.load(f)
return config

with tempfile.TemporaryDirectory(prefix="__pxbld__") as build_directory:
tranlastion_units = [tu_from_c_source(vars(args)["c-source"], build_directory)]
export_config = ExportConfiguration()
if args.config:
config = load_config(args.config)
assert 'export_config' in config
assert 'translation_unit' in config

export_config = ExportConfiguration()
assert 'symbols' in config['export_config']
for symbol in config['export_config']['symbols']:
assert 'python_name' in symbol
assert 'symbol_name' in symbol
assert 'signature' in symbol
export_config.add_symbol(**symbol)

tranlastion_units = []
for tu in config['translation_unit']:
assert 'name' in tu
if 'source' in tu:
tranlastion_units.append(
TranslationUnit(**tu)
)
elif 'path' in tu:
file_type = tu.pop('path').split('.')[-1]
if file_type == 'c':
tranlastion_units.append(
TranslationUnit.from_c_source(tu['path'], **tu)
)
elif file_type == 'pyx':
tranlastion_units.append(
TranslationUnit.from_cython_source(tu['path'], **tu)
)
else:
raise ValueError("Invalid file type provided in path")
else:
tranlastion_units = [tu_from_c_source(vars(args)["c-source"], build_directory)]
export_config = ExportConfiguration()

target_description = default_test_config()
compiler = PIXIECompiler(
library_name=args.o,
Expand Down
41 changes: 41 additions & 0 deletions pixie/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import tempfile
import os
import sys
import subprocess
import sysconfig

from llvmlite import ir
from llvmlite.ir.values import GlobalValue
Expand Down Expand Up @@ -122,6 +124,45 @@ def __init__(self, name, source):
f"got {type(source).__name__}.")
raise TypeError(msg)

@classmethod
def from_c_source(cls, path_to_c_file, name="", extra_flags=()):
# Takes a C-language source file at path `path_to_c_file` and produces a
# translation unit from it via a call to clang.
with tempfile.NamedTemporaryFile(suffix=".ll") as ntf:
# NOTE: This needs to be -O1 or great, -O0 adds `optnone` to
# function attributes which then prevents optimisation by the PIXIE
# toolchain.
cmd = ('clang', '-x', 'c', '-O1',
'-I', sysconfig.get_path("include"),
'-fPIC', '-mcmodel=small',
*extra_flags,
'-emit-llvm', path_to_c_file, '-o', ntf.name, '-S')
try:
subprocess.check_output(cmd, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as err:
# print here, in case another exception interrupts the handling
# of this one and the output is masked.
print(err.stdout.decode())
raise err

ntf.flush()
with open(ntf.name, 'rt') as f:
data = f.read()
_name = name or path_to_c_file
return TranslationUnit(_name, data)

@classmethod
def from_cython_source(cls, path_to_cython_file, name="",
extra_clang_flags=(), extra_cython_flags=()):
# convert cython to C, then pass that to `from_c_source`.
with tempfile.NamedTemporaryFile(suffix='.c') as ntf:
cmd = ('cython', '-3', *extra_cython_flags, path_to_cython_file,
'-o', ntf.name)
subprocess.check_output(cmd)
_name = name or path_to_cython_file
return TranslationUnit.from_c_source(ntf.name, _name,
extra_flags=extra_clang_flags)


class ExportConfiguration():

Expand Down

0 comments on commit e222420

Please sign in to comment.