Skip to content

Commit 4e8d447

Browse files
committed
Added option -C/--config to pixie cli: Takes input a json configuration file which specifies translation units and export configurations
1 parent 41043b5 commit 4e8d447

2 files changed

Lines changed: 111 additions & 8 deletions

File tree

pixie/cli.py

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import argparse
22
import pathlib
33
import os
4+
import json
45

56
from pixie import (
67
PIXIECompiler,
@@ -20,6 +21,7 @@ def _generate_parser(descr):
2021
help="optimization level", default=3,)
2122
parser.add_argument("-o", metavar="<lib>", help="output library")
2223
parser.add_argument("files", help="input source files", nargs="+")
24+
parser.add_argument("-C", "--config", help="Configuration JSON file")
2325
return parser
2426

2527

@@ -78,22 +80,62 @@ def pixie_cc():
7880
compiler.compile()
7981

8082

83+
def load_config(config_file):
84+
with open(config_file, 'r') as f:
85+
config = json.load(f)
86+
return config
87+
88+
8189
def pixie_cythonize():
8290
parser = _generate_parser("pixie-cythonize")
8391
args = vars(parser.parse_args())
8492

8593
opt_flags, clang_flags, library_name = \
8694
_translate_common_options(parser, args)
8795

88-
# cythonize the source
89-
inps = args["files"]
90-
tus = []
91-
for inp in inps:
92-
path = pathlib.Path(inp)
93-
tus.append(TranslationUnit.from_cython_source(str(path),
94-
extra_clang_flags=clang_flags))
96+
if args["config"] is not None:
97+
config = load_config(args.config)
98+
assert 'export_config' in config
99+
assert 'translation_unit' in config
100+
101+
export_config = ExportConfiguration()
102+
assert 'symbols' in config['export_config']
103+
for symbol in config['export_config']['symbols']:
104+
assert 'python_name' in symbol
105+
assert 'symbol_name' in symbol
106+
assert 'signature' in symbol
107+
export_config.add_symbol(**symbol)
108+
109+
tus = []
110+
for tu in config['translation_unit']:
111+
assert 'name' in tu
112+
if 'source' in tu:
113+
tus.append(
114+
TranslationUnit(**tu)
115+
)
116+
elif 'path' in tu:
117+
file_path = tu.pop('path')
118+
file_type = file_path.split('.')[-1]
119+
if file_type == 'c':
120+
tus.append(
121+
TranslationUnit.from_c_source(file_path, **tu)
122+
)
123+
elif file_type == 'pyx':
124+
tus.append(
125+
TranslationUnit.from_cython_source(file_path, **tu)
126+
)
127+
else:
128+
raise ValueError("Invalid file type provided in path")
129+
else:
130+
# cythonize the source
131+
inps = args["files"]
132+
tus = []
133+
for inp in inps:
134+
path = pathlib.Path(inp)
135+
tus.append(TranslationUnit.from_cython_source(str(path),
136+
extra_clang_flags=clang_flags))
137+
export_config = ExportConfiguration()
95138

96-
export_config = ExportConfiguration()
97139
target_description = targets.get_default_configuration()
98140
compiler = PIXIECompiler(
99141
library_name=library_name,

pixie/tests/test_cli.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,32 @@
55

66
from pixie.tests.support import PixieTestCase
77

8+
example_config = """
9+
{
10+
"translation_unit": [
11+
{
12+
"name": "llvm_foo_double_double",
13+
"source": "int _Z3fooPdS_(double* a, double* b, double* c) {
14+
*c = *a + *b;
15+
}"
16+
},
17+
{
18+
"name": "llvm_foo_float_float",
19+
"path": "llvm_foo_float_float.c",
20+
"extra_flags": []
21+
},
22+
],
23+
"export_config": {
24+
"symbols": [
25+
{
26+
"python_name": "foo",
27+
"symbol_name": "_Z3fooPdS_",
28+
"signature": "void(double*, double*, double*)"
29+
}
30+
]
31+
}
32+
}"""
33+
834

935
class TestCLI(PixieTestCase):
1036

@@ -28,6 +54,41 @@ def test_pixie_cc_basic(self):
2854
self.assertEqual(files[0], cfile_name)
2955
self.assertTrue(files[1].startswith(testlib_name))
3056

57+
def test_pixie_cc_config(self):
58+
cfile_name = "test.c"
59+
cfile_source = textwrap.dedent(
60+
"""
61+
int f(x) {
62+
return x + 1;
63+
}
64+
""")
65+
json_file_name = "config.json"
66+
json_file_source = example_config
67+
tu_cfile_name = "llvm_foo_float_float.c"
68+
tu_cfile_source = textwrap.dedent(
69+
"""
70+
int foo(float* a, float* b, float* c) { *c = *a + *b; }
71+
""")
72+
with TemporaryDirectory(prefix=self.tmpdir.name) as tmpdir:
73+
cfile_path = os.path.join(tmpdir, cfile_name)
74+
with open(cfile_path, "wt") as f:
75+
f.write(cfile_source)
76+
json_file_path = os.path.join(tmpdir, json_file_name)
77+
with open(json_file_path, "wt") as f:
78+
f.write(json_file_source)
79+
with open(os.path.join(tmpdir, tu_cfile_name), "wt") as f:
80+
f.write(tu_cfile_source)
81+
testlib_name = "testclib"
82+
command = ["pixie-cc", cfile_name, "-g", "-O0", "-o",
83+
testlib_name, "-C", json_file_name]
84+
subprocess.run(command, check=True, cwd=tmpdir)
85+
files = sorted(os.listdir(tmpdir))
86+
self.assertEqual(4, len(files))
87+
self.assertEqual(files[0], json_file_name)
88+
self.assertEqual(files[1], tu_cfile_name)
89+
self.assertEqual(files[2], cfile_name)
90+
self.assertTrue(files[3].startswith(testlib_name))
91+
3192
def test_pixie_cc_two_files(self):
3293
cfile1_name = "test1.c"
3394
cfile1_source = textwrap.dedent(

0 commit comments

Comments
 (0)