MCPcopy Create free account
hub / github.com/dmlc/xgboost / build_libxgboost

Function build_libxgboost

python-package/packager/nativelib.py:30–107  ·  view source on GitHub ↗

Build libxgboost in a temporary directory and obtain the path to built libxgboost.

(
    cpp_src_dir: pathlib.Path,
    build_dir: pathlib.Path,
    build_config: BuildConfiguration,
)

Source from the content-addressed store, hash-verified

28
29
30def build_libxgboost(
31 cpp_src_dir: pathlib.Path,
32 build_dir: pathlib.Path,
33 build_config: BuildConfiguration,
34) -> pathlib.Path:
35 """Build libxgboost in a temporary directory and obtain the path to built
36 libxgboost.
37
38 """
39 logger = logging.getLogger("xgboost.packager.build_libxgboost")
40
41 if not cpp_src_dir.is_dir():
42 raise RuntimeError(f"Expected {cpp_src_dir} to be a directory")
43 logger.info(
44 "Building %s from the C++ source files in %s...", _lib_name(), str(cpp_src_dir)
45 )
46
47 def _build(*, generator: str) -> None:
48 cmake_cmd = [
49 "cmake",
50 str(cpp_src_dir),
51 generator,
52 "-DKEEP_BUILD_ARTIFACTS_IN_BINARY_DIR=ON",
53 ]
54 cmake_cmd.extend(build_config.get_cmake_args())
55
56 logger.info("CMake args: %s", str(cmake_cmd))
57 subprocess.check_call(cmake_cmd, cwd=build_dir)
58
59 if system() == "Windows":
60 subprocess.check_call(
61 ["cmake", "--build", ".", "--config", "Release"], cwd=build_dir
62 )
63 else:
64 nproc = os.cpu_count()
65 assert build_tool is not None
66 subprocess.check_call([build_tool, f"-j{nproc}"], cwd=build_dir)
67
68 if system() == "Windows":
69 supported_generators = (
70 "-GVisual Studio 18 2026",
71 "-GVisual Studio 17 2022",
72 "-GVisual Studio 16 2019",
73 "-GVisual Studio 15 2017",
74 "-GMinGW Makefiles",
75 )
76 for generator in supported_generators:
77 try:
78 _build(generator=generator)
79 logger.info(
80 "Successfully built %s using generator %s", _lib_name(), generator
81 )
82 break
83 except subprocess.CalledProcessError as e:
84 logger.info(
85 "Tried building with generator %s but failed with exception %s",
86 generator,
87 str(e),

Callers 1

Calls 2

_lib_nameFunction · 0.85
_buildFunction · 0.70

Tested by

no test coverage detected