|
| 1 | +import os |
| 2 | +import platform |
| 3 | +import subprocess |
| 4 | + |
| 5 | +from .env import check_env_flag |
| 6 | + |
| 7 | +IS_WINDOWS = (platform.system() == 'Windows') |
| 8 | + |
| 9 | +WITH_DISTRIBUTED = not check_env_flag('NO_DISTRIBUTED') and not IS_WINDOWS |
| 10 | +WITH_DISTRIBUTED_MW = WITH_DISTRIBUTED and check_env_flag('WITH_DISTRIBUTED_MW') |
| 11 | +WITH_GLOO_IBVERBS = False |
| 12 | + |
| 13 | +IB_DEVINFO_CMD = "ibv_devinfo" |
| 14 | + |
| 15 | + |
| 16 | +def get_command_path(command): |
| 17 | + """ |
| 18 | + Helper function that checks if the command exists in the path and gets the |
| 19 | + full path of a given linux command if it exists. |
| 20 | + """ |
| 21 | + def excutable(command_path): |
| 22 | + return os.path.isfile(command_path) and os.access(command_path, os.X_OK) |
| 23 | + |
| 24 | + for path in os.environ["PATH"].split(os.pathsep): |
| 25 | + command_path = os.path.join(path, command) |
| 26 | + if excutable(command_path): |
| 27 | + return command_path |
| 28 | + |
| 29 | + return None |
| 30 | + |
| 31 | + |
| 32 | +def should_build_ib(): |
| 33 | + """ |
| 34 | + Helper function that detects the system's IB support and returns if we |
| 35 | + should build with IB support. |
| 36 | + """ |
| 37 | + try: |
| 38 | + # If the command doesn't exist, we can directly return instead of |
| 39 | + # making a subprocess call |
| 40 | + full_cmd_path = get_command_path(IB_DEVINFO_CMD) |
| 41 | + if not full_cmd_path: |
| 42 | + return False |
| 43 | + subprocess.check_output([full_cmd_path, "--list"]) |
| 44 | + # Here we just would like to simply run the command to test if IB |
| 45 | + # related tools / lib are installed without parsing the output. We |
| 46 | + # will enable IB build as long as the command runs successfully. |
| 47 | + # |
| 48 | + # The output should look like either: |
| 49 | + # |
| 50 | + # > ibv_devinfo --list |
| 51 | + # 0 HCAs founds: |
| 52 | + # |
| 53 | + # or |
| 54 | + # |
| 55 | + # > ibv_devinfo --list |
| 56 | + # 4 HCAs found: |
| 57 | + # mlx5_3 |
| 58 | + # mlx5_2 |
| 59 | + # mlx5_1 |
| 60 | + # mlx5_0 |
| 61 | + return True |
| 62 | + except Exception: |
| 63 | + # We just take all the exceptions here without affecting the build |
| 64 | + return False |
| 65 | + |
| 66 | + |
| 67 | +WITH_GLOO_IBVERBS = WITH_DISTRIBUTED and (should_build_ib() or |
| 68 | + check_env_flag("WITH_GLOO_IBVERBS")) |
0 commit comments