DepperDan/common/cmake.py

67 lines
2.3 KiB
Python

# (c) 2025 by Stephan Menzel
# Licensed under the Apache License, Version 2.0.
# See attached file LICENSE for full details
import os
from enum import Enum
from pathlib import Path
from build_functions.build_utils import file_and_console_log, run_in_shell
from common.directory_helpers import pushd
import common.settings
from package.package_info import PackageInfo
class CMakeBuildType(Enum):
RELEASE = (1, "Release")
RELWITHDEBINFO = (2, "RelWithDebInfo")
DEBUG = (3, "Debug")
def __init__(self, num, label):
self._num = num
self.label = label
def cmake_build_install(local_directory, package_info: PackageInfo, cmake_args: list[tuple[str, str]] = [],
build_type: CMakeBuildType = CMakeBuildType.RELEASE) -> Path:
argstr = ""
# Create flags string out of args tuples
for key, value in common.settings.cmake_global_flags:
slash_value = value.replace("\\", "/")
argstr += f"-D{key}={slash_value} "
for key, value in cmake_args:
slash_value = value.replace("\\", "/")
argstr += f"-D{key}={slash_value} "
argstr += f"-DCMAKE_CXX_STANDARD:STRING={common.settings.cpp_standard} "
build_folder = '_build'
with pushd(local_directory):
install_prefix = package_info.install_location()
if not os.path.isdir(build_folder):
os.makedirs(build_folder)
with pushd(build_folder):
if not common.settings.rebuild and Path("built_and_installed.txt").exists():
file_and_console_log("already built, exiting")
return install_prefix
run_in_shell(f"cmake .. {common.settings.cmake_toolset} -DCMAKE_CONFIGURATION_TYPES:STRING=Release "
f"{argstr} -DCMAKE_INSTALL_PREFIX={install_prefix}")
run_in_shell(f'cmake --build . --config {build_type.label} --target INSTALL --parallel {common.settings.num_cores}')
with open("built_and_installed.txt", "w") as lockfile:
lockfile.write(f"built release")
# This stopped working somehow...
# if run_in_buildpipeline:
# shutil.rmtree(local_directory)
return install_prefix
def assemble_prefix_path(packages: list[PackageInfo]) -> str:
paths = [str(pkg.install_location().as_posix()) for pkg in packages]
return ";".join(paths)