|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Helper for building nvblox docker images |
| 3 | +
|
| 4 | +""" |
| 5 | + |
| 6 | +import os |
| 7 | +import sys |
| 8 | +import subprocess |
| 9 | +from typing import Optional, List |
| 10 | +import argparse |
| 11 | + |
| 12 | +# Realsense lib needs ubuntu22, so that's our base image |
| 13 | +REALSENSE_BASE_IMAGE = 'nvcr.io/nvidia/cuda:12.6.1-devel-ubuntu22.04' |
| 14 | +REALSENSE_IMAGE_NAME_SUFFIX = '_cu12_u22' |
| 15 | + |
| 16 | + |
| 17 | +def build_image(dockerfile_path: str, |
| 18 | + image_name: str, |
| 19 | + base_image: Optional[str] = None, |
| 20 | + extra_build_args: Optional[List[str]] = None) -> None: |
| 21 | + """Build a docker image from a Dockerfile""" |
| 22 | + |
| 23 | + print('=' * 80) |
| 24 | + print(f'Building {image_name} from {dockerfile_path}') |
| 25 | + print(f'with base image {base_image}') |
| 26 | + print('=' * 80) |
| 27 | + |
| 28 | + cmd = ['docker', 'build', '-f', dockerfile_path, '-t', image_name, '--network=host'] |
| 29 | + |
| 30 | + if base_image: |
| 31 | + cmd += ['--build-arg', f'BASE_IMAGE={base_image}'] |
| 32 | + |
| 33 | + if extra_build_args: |
| 34 | + cmd += extra_build_args |
| 35 | + |
| 36 | + cmd += ['.'] |
| 37 | + |
| 38 | + print(cmd) |
| 39 | + subprocess.run(cmd, check=True) |
| 40 | + |
| 41 | + |
| 42 | +def build_deps_image(base_image: Optional[str] = None, image_name_suffix: str = '') -> str: |
| 43 | + """Build nvblox dependencies (deps) image""" |
| 44 | + image_name = 'nvblox_deps' + image_name_suffix |
| 45 | + build_image(dockerfile_path=os.path.join('docker', 'Dockerfile.deps'), |
| 46 | + image_name=image_name, |
| 47 | + base_image=base_image, |
| 48 | + extra_build_args=[]) |
| 49 | + return image_name |
| 50 | + |
| 51 | + |
| 52 | +def build_binaries_image(base_image: Optional[str] = None, |
| 53 | + image_name_suffix: str = '', |
| 54 | + cuda_arch: Optional[str] = None, |
| 55 | + skip_build_binaries_docker: bool = False, |
| 56 | + max_num_build_jobs: Optional[int] = None) -> str: |
| 57 | + """Build nvblox binaries (.build) image""" |
| 58 | + image_name = 'nvblox_build' + image_name_suffix |
| 59 | + |
| 60 | + if cuda_arch is None: |
| 61 | + cuda_arch = get_cuda_arch() |
| 62 | + |
| 63 | + extra_build_args = ['--build-arg', f"CMAKE_ARGS='-DCMAKE_CUDA_ARCHITECTURES={cuda_arch}'"] |
| 64 | + |
| 65 | + if max_num_build_jobs is not None: |
| 66 | + extra_build_args.extend(['--build-arg', f'MAX_NUM_JOBS={max_num_build_jobs}']) |
| 67 | + |
| 68 | + if not skip_build_binaries_docker: |
| 69 | + build_image(dockerfile_path=os.path.join('docker', 'Dockerfile.build'), |
| 70 | + image_name=image_name, |
| 71 | + base_image=base_image, |
| 72 | + extra_build_args=extra_build_args) |
| 73 | + |
| 74 | + return image_name |
| 75 | + |
| 76 | + |
| 77 | +def build_realsense_example_image(base_image: Optional[str] = None, |
| 78 | + image_name_suffix: str = '') -> str: |
| 79 | + """Build nvblox realsense example image""" |
| 80 | + image_name = 'nvblox_realsense_example' + image_name_suffix |
| 81 | + build_image(dockerfile_path=os.path.join('docker', 'Dockerfile.realsense_example'), |
| 82 | + image_name=image_name, |
| 83 | + base_image=base_image, |
| 84 | + extra_build_args=[]) |
| 85 | + |
| 86 | + return image_name |
| 87 | + |
| 88 | + |
| 89 | +def get_cuda_arch() -> str: |
| 90 | + """Get the cuda architecture from nvidia-smi""" |
| 91 | + try: |
| 92 | + command_output = subprocess.check_output( |
| 93 | + ['nvidia-smi', '--query-gpu=compute_cap', '--format=csv']) |
| 94 | + arch = command_output.decode('utf-8').split()[1].replace('.', '') |
| 95 | + return arch |
| 96 | + except FileNotFoundError: |
| 97 | + print('ERROR:nvidia-smi not found. If on aarch64, provide architecture from command line.') |
| 98 | + raise |
| 99 | + |
| 100 | + |
| 101 | +def parse_args() -> argparse.Namespace: |
| 102 | + parser = argparse.ArgumentParser() |
| 103 | + parser.add_argument('--build-deps-image', |
| 104 | + '-d', |
| 105 | + action='store_true', |
| 106 | + help='Whether to build the dependencies (deps) image') |
| 107 | + parser.add_argument('--build-binaries-image', |
| 108 | + '-b', |
| 109 | + action='store_true', |
| 110 | + help='Whether to build the binaries (build) image') |
| 111 | + parser.add_argument('--build-realsense-example-image', |
| 112 | + '-r', |
| 113 | + action='store_true', |
| 114 | + help='Whether to build the realsense (realsense) image') |
| 115 | + parser.add_argument( |
| 116 | + '--skip-build-binaries-docker', |
| 117 | + '-n', |
| 118 | + action='store_true', |
| 119 | + help='Whether to skip building the binary (build) image and rely a previously built image.') |
| 120 | + parser.add_argument('--base-image', |
| 121 | + type=str, |
| 122 | + default=None, |
| 123 | + help='Base image. Will be taken from Dockerfile.deps if not given') |
| 124 | + parser.add_argument('--image_name_suffix', |
| 125 | + type=str, |
| 126 | + default='', |
| 127 | + help='Suffix for created docker image name.') |
| 128 | + parser.add_argument('--cuda-arch', |
| 129 | + type=str, |
| 130 | + help='Optionally input cuda architectures to build for as a ' |
| 131 | + 'semicolon separated list. e.g. "90;89" ') |
| 132 | + parser.add_argument('--max-num-build-jobs', |
| 133 | + type=int, |
| 134 | + help='Max number of build jobs', |
| 135 | + default=None) |
| 136 | + return parser.parse_args() |
| 137 | + |
| 138 | + |
| 139 | +def main() -> int: |
| 140 | + args = parse_args() |
| 141 | + |
| 142 | + if args.build_deps_image: |
| 143 | + build_deps_image(base_image=args.base_image, image_name_suffix=args.image_name_suffix) |
| 144 | + |
| 145 | + if args.build_binaries_image: |
| 146 | + # need deps |
| 147 | + deps_image = build_deps_image(base_image=args.base_image, |
| 148 | + image_name_suffix=args.image_name_suffix) |
| 149 | + build_binaries_image(base_image=deps_image, |
| 150 | + image_name_suffix=args.image_name_suffix, |
| 151 | + cuda_arch=args.cuda_arch, |
| 152 | + skip_build_binaries_docker=args.skip_build_binaries_docker, |
| 153 | + max_num_build_jobs=args.max_num_build_jobs) |
| 154 | + |
| 155 | + if args.build_realsense_example_image: |
| 156 | + # need deps + binaries |
| 157 | + assert args.base_image is None, 'Cannot provide base image for realsense example' |
| 158 | + deps_image = build_deps_image(base_image=REALSENSE_BASE_IMAGE, |
| 159 | + image_name_suffix=REALSENSE_IMAGE_NAME_SUFFIX) |
| 160 | + binaries_image = build_binaries_image( |
| 161 | + base_image=deps_image, |
| 162 | + image_name_suffix=REALSENSE_IMAGE_NAME_SUFFIX, |
| 163 | + cuda_arch=args.cuda_arch, |
| 164 | + skip_build_binaries_docker=args.skip_build_binaries_docker, |
| 165 | + max_num_build_jobs=args.max_num_build_jobs) |
| 166 | + |
| 167 | + build_realsense_example_image(base_image=binaries_image, |
| 168 | + image_name_suffix=REALSENSE_IMAGE_NAME_SUFFIX) |
| 169 | + |
| 170 | + return 0 |
| 171 | + |
| 172 | + |
| 173 | +if __name__ == '__main__': |
| 174 | + sys.exit(main()) |
0 commit comments