eive-obsw/cmake/scripts/cmake-build-cfg.py

174 lines
5.2 KiB
Python
Raw Normal View History

2020-12-29 01:09:59 +01:00
#!/usr/bin/env python3
"""
@brief CMake configuration helper
@details
This script was written to have a portable way to perform the CMake configuration with various parameters on
different OSes. It was first written for the FSFW Example, but could be adapted to be more generic
in the future.
Run cmake_build_config.py --help to get more information.
"""
import os
import sys
import argparse
import shutil
2022-03-14 18:43:16 +01:00
import stat
2020-12-29 01:09:59 +01:00
def main():
print("-- Python CMake build configurator utility --")
print("Parsing command line arguments..")
2021-05-05 22:43:17 +02:00
parser = argparse.ArgumentParser(
description="Processing arguments for CMake build configuration."
)
parser.add_argument(
2022-11-28 11:21:12 +01:00
"-o",
"--osal",
type=str,
choices=["freertos", "linux", "rtems", "host"],
help="FSFW OSAL. Valid arguments: host, linux, rtems, freertos",
)
parser.add_argument(
"-b",
"--buildtype",
type=str,
choices=["debug", "release", "size", "reldeb"],
2021-05-05 22:43:17 +02:00
help="CMake build type. Valid arguments: debug, release, size, reldeb (Release with Debug "
2022-11-28 11:21:12 +01:00
"Information)",
default="debug",
2021-05-05 22:43:17 +02:00
)
2020-12-29 01:09:59 +01:00
parser.add_argument("-l", "--builddir", type=str, help="Specify build directory.")
2021-10-18 18:15:39 +02:00
parser.add_argument(
2022-11-28 11:21:12 +01:00
"-g", "--generator", type=str, help="CMake Generator", choices=["make", "ninja"]
2021-10-18 18:15:39 +02:00
)
2021-05-05 22:43:17 +02:00
parser.add_argument(
2022-11-28 11:21:12 +01:00
"-d",
"--defines",
2021-05-05 22:43:17 +02:00
help="Additional custom defines passed to CMake (supply without -D prefix!)",
2022-11-28 11:21:12 +01:00
nargs="*",
type=str,
2021-05-05 22:43:17 +02:00
)
parser.add_argument(
2022-11-28 11:21:12 +01:00
"-t",
"--target-bsp",
type=str,
help="Target BSP, combination of architecture and machine",
2021-05-05 22:43:17 +02:00
)
2020-12-29 01:09:59 +01:00
args = parser.parse_args()
print("Determining source location..")
source_location = determine_source_location()
print(f"Determined source location: {source_location}")
print("Building cmake configuration command..")
if args.osal is None:
print("No FSFW OSAL specified, setting to default (host)..")
osal = "host"
else:
osal = args.osal
if args.generator is None:
generator_cmake_arg = ""
else:
2022-11-28 11:21:12 +01:00
if args.generator == "make":
if os.name == "nt":
2021-10-18 18:15:39 +02:00
generator_cmake_arg = '-G "MinGW Makefiles"'
else:
generator_cmake_arg = '-G "Unix Makefiles"'
2022-11-28 11:21:12 +01:00
elif args.generator == "ninja":
generator_cmake_arg = "-G Ninja"
2021-10-18 18:15:39 +02:00
else:
generator_cmake_arg = args.generator
2020-12-29 01:09:59 +01:00
if args.buildtype == "debug":
cmake_build_type = "Debug"
elif args.buildtype == "release":
cmake_build_type = "Release"
elif args.buildtype == "size":
cmake_build_type = "MinSizeRel"
else:
cmake_build_type = "RelWithDebInfo"
if args.target_bsp is not None:
2022-11-28 11:21:12 +01:00
cmake_target_cfg_cmd = f'-DTGT_BSP="{args.target_bsp}"'
2020-12-29 01:09:59 +01:00
else:
cmake_target_cfg_cmd = ""
2021-05-05 22:43:17 +02:00
define_string = ""
if args.defines is not None:
define_list = args.defines[0].split()
for define in define_list:
define_string += f"-D{define} "
2020-12-29 01:09:59 +01:00
build_folder = cmake_build_type
2021-02-22 12:36:28 +01:00
if args.builddir is not None:
build_folder = args.builddir
2022-11-28 11:21:12 +01:00
2020-12-29 01:09:59 +01:00
build_path = source_location + os.path.sep + build_folder
if os.path.isdir(build_path):
2022-11-28 11:21:12 +01:00
remove_old_dir = input(
f"{build_folder} folder already exists. Remove old directory? [y/n]: "
)
2020-12-29 01:09:59 +01:00
if str(remove_old_dir).lower() in ["yes", "y", 1]:
remove_old_dir = True
else:
build_folder = determine_new_folder()
build_path = source_location + os.path.sep + build_folder
remove_old_dir = False
if remove_old_dir:
2022-03-14 18:43:16 +01:00
rm_build_dir(build_path)
2020-12-29 01:09:59 +01:00
os.chdir(source_location)
os.mkdir(build_folder)
print(f"Navigating into build directory: {build_path}")
os.chdir(build_folder)
2022-11-28 11:21:12 +01:00
cmake_command = (
f'cmake {generator_cmake_arg} -DFSFW_OSAL="{osal}" '
f'-DCMAKE_BUILD_TYPE="{cmake_build_type}" {cmake_target_cfg_cmd} '
f"{define_string} {source_location}"
)
2020-12-29 01:09:59 +01:00
# Remove redundant spaces
2022-11-28 11:21:12 +01:00
cmake_command = " ".join(cmake_command.split())
2020-12-29 01:09:59 +01:00
print("Running CMake command: ")
2022-11-28 11:21:12 +01:00
print(f'" {cmake_command} "')
2020-12-29 01:09:59 +01:00
os.system(cmake_command)
print("-- CMake configuration done. --")
2022-11-28 11:21:12 +01:00
2022-03-14 18:43:16 +01:00
def rm_build_dir(path: str):
# On windows the permissions of the build directory may have been set to read-only. If this
# is the case the permissions are changed before trying to delete the directory.
if not os.access(path, os.W_OK):
os.chmod(path, stat.S_IWUSR)
shutil.rmtree(path)
2020-12-29 01:09:59 +01:00
def determine_source_location() -> str:
index = 0
while not os.path.isdir("fsfw"):
index += 1
os.chdir("..")
if index >= 5:
2022-11-28 11:21:12 +01:00
print(
"Error: Could not find source directory (determined by looking for fsfw folder!)"
)
2020-12-29 01:09:59 +01:00
sys.exit(1)
return os.getcwd()
def determine_new_folder() -> str:
new_folder = input(f"Use different folder name? [y/n]: ")
if str(new_folder).lower() in ["yes", "y", 1]:
new_folder_name = input("New folder name: ")
return new_folder_name
else:
print("Aborting configuration.")
sys.exit(0)
if __name__ == "__main__":
main()