2018-10-16 13:31:00 +00:00
|
|
|
from collections import namedtuple
|
|
|
|
from pathlib import Path
|
|
|
|
import sys
|
|
|
|
|
|
|
|
sys.path.append(str(Path(__file__).parents[2]))
|
|
|
|
|
|
|
|
from pyodide_build.pywasmcross import handle_command # noqa: E402
|
2018-10-26 15:22:10 +00:00
|
|
|
from pyodide_build.pywasmcross import f2c # noqa: E402
|
2018-10-16 13:31:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _args_wrapper(func):
|
|
|
|
"""Convert function to take as input / return a string instead of a
|
|
|
|
list of arguments
|
|
|
|
|
2018-10-23 10:11:25 +00:00
|
|
|
Also sets dryrun=True
|
2018-10-16 13:31:00 +00:00
|
|
|
"""
|
2020-06-28 18:24:40 +00:00
|
|
|
|
2018-10-16 13:31:00 +00:00
|
|
|
def _inner(line, *pargs):
|
|
|
|
args = line.split()
|
2018-10-23 10:11:25 +00:00
|
|
|
res = func(args, *pargs, dryrun=True)
|
2020-06-28 18:24:40 +00:00
|
|
|
if hasattr(res, "__len__"):
|
|
|
|
return " ".join(res)
|
2018-10-16 13:31:00 +00:00
|
|
|
else:
|
|
|
|
return res
|
2020-06-28 18:24:40 +00:00
|
|
|
|
2018-10-16 13:31:00 +00:00
|
|
|
return _inner
|
|
|
|
|
|
|
|
|
|
|
|
handle_command_wrap = _args_wrapper(handle_command)
|
2018-10-26 15:22:10 +00:00
|
|
|
f2c_wrap = _args_wrapper(f2c)
|
2018-10-16 13:31:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_handle_command():
|
2021-01-03 00:17:08 +00:00
|
|
|
Args = namedtuple("args", ["cflags", "cxxflags", "ldflags", "host"])
|
|
|
|
args = Args(cflags="", cxxflags="", ldflags="", host="")
|
2020-06-28 18:24:40 +00:00
|
|
|
assert handle_command_wrap("gcc -print-multiarch", args) is None
|
|
|
|
assert handle_command_wrap("gcc test.c", args) == "emcc test.c"
|
|
|
|
assert (
|
|
|
|
handle_command_wrap("gcc -shared -c test.o -o test.so", args)
|
2021-01-03 14:14:56 +00:00
|
|
|
== "emcc -shared -c test.o -o test.wasm"
|
2020-06-28 18:24:40 +00:00
|
|
|
)
|
2018-10-16 13:31:00 +00:00
|
|
|
|
2021-01-03 00:17:08 +00:00
|
|
|
# check cxxflags injection and cpp detection
|
|
|
|
args = Args(cflags="-I./lib2", cxxflags="-std=c++11", ldflags="-lm", host="")
|
|
|
|
assert (
|
|
|
|
handle_command_wrap("gcc -I./lib1 test.cpp -o test.o", args)
|
2021-01-03 14:14:56 +00:00
|
|
|
== "em++ -I./lib2 -std=c++11 -I./lib1 test.cpp -o test.o"
|
2021-01-03 00:17:08 +00:00
|
|
|
)
|
|
|
|
|
2018-10-16 13:31:00 +00:00
|
|
|
# check ldflags injection
|
2021-01-03 00:17:08 +00:00
|
|
|
args = Args(cflags="", cxxflags="", ldflags="-lm", host="")
|
2020-06-28 18:24:40 +00:00
|
|
|
assert (
|
|
|
|
handle_command_wrap("gcc -shared -c test.o -o test.so", args)
|
2021-01-03 14:14:56 +00:00
|
|
|
== "emcc -lm -shared -c test.o -o test.wasm"
|
2020-06-28 18:24:40 +00:00
|
|
|
)
|
2018-10-16 13:31:00 +00:00
|
|
|
|
|
|
|
# compilation checks in numpy
|
2020-06-28 18:24:40 +00:00
|
|
|
assert handle_command_wrap("gcc /usr/file.c", args) is None
|
2018-10-26 15:22:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_f2c():
|
2020-06-28 18:24:40 +00:00
|
|
|
assert f2c_wrap("gfortran test.f") == "gfortran test.c"
|
|
|
|
assert f2c_wrap("gcc test.c") is None
|
|
|
|
assert f2c_wrap("gfortran --version") is None
|
|
|
|
assert (
|
|
|
|
f2c_wrap("gfortran --shared -c test.o -o test.so")
|
|
|
|
== "gfortran --shared -c test.o -o test.so"
|
|
|
|
)
|
2020-12-08 08:00:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_conda_compiler_compat():
|
2021-01-03 00:17:08 +00:00
|
|
|
Args = namedtuple("args", ["cflags", "cxxflags", "ldflags", "host"])
|
|
|
|
args = Args(cflags="", cxxflags="", ldflags="", host="")
|
2020-12-08 08:00:53 +00:00
|
|
|
assert handle_command_wrap(
|
|
|
|
"gcc -shared -c test.o -B /compiler_compat -o test.so", args
|
2021-01-03 14:14:56 +00:00
|
|
|
) == ("emcc -shared -c test.o -o test.wasm")
|