code: def locate_or_build_libxgboost(
      toplevel_dir: pathlib.Path,
      build_dir: pathlib.Path,
      build_config: BuildConfiguration,
  ) -> pathlib.Path:
      """Locate libxgboost; if not exist, build it"""
      logger = logging.getLogger(
          "xgboost.packager.locate_or_build_libxgboost"
      )
  
      if build_config.use_system_libxgboost:
          # Find libxgboost from system prefix
          sys_prefix = pathlib.Path(sys.base_prefix)
          sys_prefix_candidates = [
              sys_prefix / "lib",
              # Paths possibly used on Windows
              sys_prefix / "bin",
              sys_prefix / "Library",
              sys_prefix / "Library" / "bin",
              sys_prefix / "Library" / "lib",
          ]
          sys_prefix_candidates = [
              p.expanduser().resolve() for p in sys_prefix_candidates
          ]
          for candidate_dir in sys_prefix_candidates:
              libtreelite_sys = candidate_dir / _lib_name()
              if libtreelite_sys.exists():
                  logger.info(
                      "Using system XGBoost: %s", str(libtreelite_sys)
                  )
                  return libtreelite_sys
          raise RuntimeError(
              f"use_system_libxgboost was specified but {_lib_name()} is "
              f"not found. Paths searched (in order): \n"
              + "\n".join(
                  [f"* {str(p)}" for p in sys_prefix_candidates]
              )
          )
  
      libxgboost = locate_local_libxgboost(toplevel_dir, logger=logger)
      if libxgboost is not None:
          return libxgboost
  
      if toplevel_dir.joinpath("cpp_src").exists():
          # Source distribution; all C++ source files to be found in cpp_src/
          cpp_src_dir = toplevel_dir.joinpath("cpp_src")
      else:
          # Probably running "pip install ." from python-package/
          cpp_src_dir = toplevel_dir.parent
          if not cpp_src_dir.joinpath("CMakeLists.txt").exists():
              raise RuntimeError(
                  f"Did not find CMakeLists.txt from {cpp_src_dir}"
              )
      return build_libxgboost(
          cpp_src_dir, build_dir=build_dir, build_config=build_config
      )
  
file_path: xgboost/python-package/packager/nativelib.py
imports: import logging
  import os
  import pathlib
  import shutil
  import subprocess
  import sys
  from platform import system
  from typing import Optional
  from .build_config import BuildConfiguration
label_desc: Use image node_id 4 for calling a global function with example usage: locate_or_build_libxgboost(toplevel_dir, build_dir, build_config) and returns: build_libxgboost, libxgboost, libtreelite_sys