129 lines
3.9 KiB
Python
129 lines
3.9 KiB
Python
"""
|
|
CMake abstraction
|
|
"""
|
|
import os
|
|
import sys
|
|
import subprocess
|
|
import re
|
|
|
|
__all__ = [
|
|
'cmake_generate_template',
|
|
'cmake_configure',
|
|
'cmake_build',
|
|
'cmake_install',
|
|
'cmake_uninstall',
|
|
]
|
|
|
|
#---
|
|
# Internals
|
|
#---
|
|
|
|
_CMAKE_TEMPLATE = """
|
|
cmake_minimum_required(VERSION 3.15)
|
|
|
|
#---
|
|
# project-specific information
|
|
#---
|
|
|
|
project({VXDEV_PROJ_NAME} VERSION {VXDEV_PROJ_VERSION} LANGUAGES C ASM)
|
|
|
|
include_directories({VXDEV_BUILD_INCLUDES})
|
|
add_compile_options({VXDEV_BUILD_CFLAGS})
|
|
add_link_options({VXDEV_BUILD_LDFLAGS})
|
|
set(
|
|
CMAKE_EXE_LINKER_FLAGS
|
|
"${CMAKE_EXE_LINKER_FLAGS} -T {VXDEV_BUILD_LINKER}"
|
|
)
|
|
|
|
#---
|
|
# source files listing
|
|
#---
|
|
|
|
set(VXDEV_PROJ_SOURCES
|
|
{VXDEV_SOURCE_FILES}
|
|
)
|
|
|
|
#---
|
|
# commit projet
|
|
#---
|
|
|
|
add_executable({VXDEV_PROJ_NAME} ${VXDEV_PROJ_SOURCES})
|
|
""".strip()
|
|
|
|
#---
|
|
# Pulbic
|
|
#---
|
|
|
|
def cmake_generate_template(prefix_build, proj):
|
|
""" Generate a common CMake file """
|
|
if os.path.exists(f"{prefix_build}/CMakeLists.txt"):
|
|
return
|
|
cmakefile = _CMAKE_TEMPLATE
|
|
cmakefile = re.sub('{VXDEV_PROJ_NAME}', proj['name'], cmakefile)
|
|
cmakefile = re.sub('{VXDEV_PROJ_VERSION}', proj['version'], cmakefile)
|
|
cmakefile = re.sub('{VXDEV_BUILD_CFLAGS}', proj['cflags'], cmakefile)
|
|
cmakefile = re.sub('{VXDEV_BUILD_LDFLAGS}', proj['ldflags'], cmakefile)
|
|
cmakefile = re.sub('{VXDEV_BUILD_LINKER}', proj['linker'], cmakefile)
|
|
cmakefile = re.sub('{VXDEV_SOURCE_FILES}', proj['src'], cmakefile)
|
|
cmakefile = re.sub('{VXDEV_BUILD_INCLUDES}', proj['include'], cmakefile)
|
|
if not os.path.exists(prefix_build):
|
|
os.makedirs(prefix_build)
|
|
with open(f"{prefix_build}/CMakeLists.txt", 'x', encoding='ascii') as cmk:
|
|
cmk.write(cmakefile)
|
|
|
|
def cmake_configure(prefix_build):
|
|
""" Abstract cmake configuration """
|
|
toolchain_flag = ''
|
|
if toolchain_path := os.environ.get('VXSDK_HOOK_CMAKE_TOOLCHAIN'):
|
|
toolchain_flag = f"-DCMAKE_TOOLCHAIN_FILE={toolchain_path}"
|
|
shell_cmd = f"cmake {toolchain_flag} -B {prefix_build} -S {prefix_build}"
|
|
return subprocess.run(shell_cmd.split(), check=False).returncode
|
|
|
|
#def cmake_configure(prefix_build, prefix_src):
|
|
# """ Abstract cmake configuration """
|
|
# toolchain_flag = ''
|
|
# if toolchain_path := os.environ.get('VXSDK_HOOK_CMAKE_TOOLCHAIN'):
|
|
# toolchain_flag = f"-DCMAKE_TOOLCHAIN_FILE={toolchain_path}"
|
|
# shell_cmd = f"cmake {toolchain_flag} -B {prefix_build} -S {prefix_src}"
|
|
# return subprocess.run(shell_cmd.split(), check=False).returncode
|
|
|
|
def cmake_build(prefix_build, verbose):
|
|
""" Abstract cmake configuration """
|
|
shell_cmd = f"cmake --build {prefix_build}"
|
|
if verbose:
|
|
shell_cmd += ' --verbose'
|
|
return subprocess.run(shell_cmd.split(), check=False).returncode
|
|
|
|
def cmake_install(prefix_build, verbose):
|
|
""" Abstract cmake installation """
|
|
shell_cmd = f"cmake --install {prefix_build}"
|
|
if verbose:
|
|
shell_cmd += ' --verbose'
|
|
return subprocess.run(shell_cmd.split(), check=False).returncode
|
|
|
|
def cmake_uninstall(prefix_build, verbose):
|
|
""" Abstract cmake uninstall
|
|
|
|
Note that CMake does not offert a easy way to uninstall project, but it
|
|
generate a file which contains all installed pathname
|
|
"""
|
|
manifile = f"{prefix_build}/install_manifest.txt"
|
|
if not os.path.exists(manifile):
|
|
print('project not installed')
|
|
return -1
|
|
|
|
retcode = 0
|
|
with open(manifile, 'r', encoding='utf8') as manifest:
|
|
for pathname in manifest.readlines():
|
|
pathname = pathname.strip()
|
|
if not os.path.exists(pathname):
|
|
continue
|
|
if verbose:
|
|
print("-- Removing {pathname}")
|
|
ret = subprocess.run(f"rm {pathname}".split(), check=False)
|
|
if ret.returncode == 0:
|
|
continue
|
|
print("warning : error during removing file", file=sys.stderr)
|
|
retcode -= 1
|
|
return retcode
|