|
| 1 | +import os |
| 2 | +import subprocess |
| 3 | +import sys |
| 4 | + |
| 5 | + |
| 6 | +def run( |
| 7 | + use_oneapi=True, |
| 8 | + c_compiler=None, |
| 9 | + cxx_compiler=None, |
| 10 | + level_zero=True, |
| 11 | + compiler_root=None, |
| 12 | + run_pytest=False, |
| 13 | + bin_llvm=None, |
| 14 | +): |
| 15 | + IS_LIN = False |
| 16 | + |
| 17 | + if "linux" in sys.platform: |
| 18 | + IS_LIN = True |
| 19 | + elif sys.platform in ["win32", "cygwin"]: |
| 20 | + pass |
| 21 | + else: |
| 22 | + assert False, sys.platform + " not supported" |
| 23 | + |
| 24 | + if not IS_LIN: |
| 25 | + raise RuntimeError( |
| 26 | + "This scripts only supports coverage collection on Linux" |
| 27 | + ) |
| 28 | + setup_dir = os.path.dirname(os.path.dirname(__file__)) |
| 29 | + cmake_args = [ |
| 30 | + sys.executable, |
| 31 | + "setup.py", |
| 32 | + "develop", |
| 33 | + "--", |
| 34 | + "-G", |
| 35 | + "Unix Makefiles", |
| 36 | + "-DCMAKE_BUILD_TYPE=Debug", |
| 37 | + "-DCMAKE_C_COMPILER:PATH=" + c_compiler, |
| 38 | + "-DCMAKE_CXX_COMPILER:PATH=" + cxx_compiler, |
| 39 | + "-DDPCTL_ENABLE_LO_PROGRAM_CREATION=" + ("ON" if level_zero else "OFF"), |
| 40 | + "-DDPCTL_GENERATE_COVERAGE=ON", |
| 41 | + "-DDPCTL_BUILD_CAPI_TESTS=ON", |
| 42 | + "-DDPCTL_COVERAGE_REPORT_OUTPUT_DIR=" + setup_dir, |
| 43 | + "-DDPCTL_DPCPP_FROM_ONEAPI:BOOL=" + ("ON" if use_oneapi else "OFF"), |
| 44 | + ] |
| 45 | + if compiler_root: |
| 46 | + cmake_args += [ |
| 47 | + "-DDPCTL_DPCPP_HOME_DIR:PATH=" + compiler_root, |
| 48 | + ] |
| 49 | + env = None |
| 50 | + if bin_llvm: |
| 51 | + env = { |
| 52 | + "PATH": ":".join((os.environ.get("PATH", ""), bin_llvm)), |
| 53 | + } |
| 54 | + env.update({k: v for k, v in os.environ.items() if k != "PATH"}) |
| 55 | + subprocess.check_call(cmake_args, shell=False, cwd=setup_dir, env=env) |
| 56 | + test_dir = ( |
| 57 | + subprocess.check_output( |
| 58 | + ["find", "_skbuild", "-name", "tests"], cwd=setup_dir |
| 59 | + ) |
| 60 | + .decode("utf-8") |
| 61 | + .strip("\n") |
| 62 | + ) |
| 63 | + subprocess.check_call( |
| 64 | + ["make", "-C", test_dir, "lcov-genhtml"], cwd=setup_dir |
| 65 | + ) |
| 66 | + subprocess.check_call( |
| 67 | + [ |
| 68 | + "pytest", |
| 69 | + "-q", |
| 70 | + "-ra", |
| 71 | + "--disable-warnings", |
| 72 | + "--cov-config", |
| 73 | + "pyproject.toml", |
| 74 | + "--cov", |
| 75 | + "dpctl", |
| 76 | + "--cov-report", |
| 77 | + "term-missing", |
| 78 | + "--pyargs", |
| 79 | + "dpctl", |
| 80 | + "-vv", |
| 81 | + ], |
| 82 | + cwd=setup_dir, |
| 83 | + shell=False, |
| 84 | + ) |
| 85 | + |
| 86 | + |
| 87 | +if __name__ == "__main__": |
| 88 | + import argparse |
| 89 | + |
| 90 | + parser = argparse.ArgumentParser( |
| 91 | + description="Driver to build dpctl and generate coverage" |
| 92 | + ) |
| 93 | + driver = parser.add_argument_group(title="Coverage driver arguments") |
| 94 | + driver.add_argument("--c-compiler", help="Name of C compiler", default=None) |
| 95 | + driver.add_argument( |
| 96 | + "--cxx-compiler", help="Name of C++ compiler", default=None |
| 97 | + ) |
| 98 | + driver.add_argument( |
| 99 | + "--not-oneapi", |
| 100 | + help="Is one-API installation", |
| 101 | + dest="oneapi", |
| 102 | + action="store_false", |
| 103 | + ) |
| 104 | + driver.add_argument( |
| 105 | + "--compiler-root", type=str, help="Path to compiler home directory" |
| 106 | + ) |
| 107 | + driver.add_argument( |
| 108 | + "--no-level-zero", |
| 109 | + help="Enable Level Zero support", |
| 110 | + dest="level_zero", |
| 111 | + action="store_false", |
| 112 | + ) |
| 113 | + driver.add_argument( |
| 114 | + "--skip-pytest", |
| 115 | + help="Run pytest and collect coverage", |
| 116 | + dest="run_pytest", |
| 117 | + action="store_false", |
| 118 | + ) |
| 119 | + driver.add_argument( |
| 120 | + "--bin-llvm", help="Path to folder where llvm-cov can be found" |
| 121 | + ) |
| 122 | + args = parser.parse_args() |
| 123 | + |
| 124 | + if args.oneapi: |
| 125 | + args.c_compiler = "icx" |
| 126 | + args.cxx_compiler = "icpx" |
| 127 | + args.compiler_root = None |
| 128 | + icx_path = subprocess.check_output(["which", "icx"]) |
| 129 | + bin_dir = os.path.dirname(os.path.dirname(icx_path)) |
| 130 | + args.bin_llvm = os.path.join(bin_dir.decode("utf-8"), "bin-llvm") |
| 131 | + else: |
| 132 | + args_to_validate = [ |
| 133 | + "c_compiler", |
| 134 | + "cxx_compiler", |
| 135 | + "compiler_root", |
| 136 | + "bin_llvm", |
| 137 | + ] |
| 138 | + for p in args_to_validate: |
| 139 | + arg = getattr(args, p, None) |
| 140 | + if not isinstance(arg, str): |
| 141 | + opt_name = p.replace("_", "-") |
| 142 | + raise RuntimeError( |
| 143 | + f"Option {opt_name} must be provided is " |
| 144 | + "using non-default DPC++ layout" |
| 145 | + ) |
| 146 | + if not os.path.exists(arg): |
| 147 | + raise RuntimeError(f"Path {arg} must exist") |
| 148 | + |
| 149 | + run( |
| 150 | + use_oneapi=args.oneapi, |
| 151 | + c_compiler=args.c_compiler, |
| 152 | + cxx_compiler=args.cxx_compiler, |
| 153 | + level_zero=args.level_zero, |
| 154 | + compiler_root=args.compiler_root, |
| 155 | + run_pytest=args.run_pytest, |
| 156 | + bin_llvm=args.bin_llvm, |
| 157 | + ) |
0 commit comments