From 31e84adece53e92373b108f125ec32508eacd763 Mon Sep 17 00:00:00 2001 From: dragonmux Date: Wed, 29 Nov 2023 07:22:17 +0000 Subject: [PATCH] platforms: Switched over to the new more formal locm3 Meson build system --- .github/workflows/build-pr.yml | 5 - deps/libopencm3 | 2 +- deps/packagefiles/libopencm3/configure | 195 ----------------------- deps/packagefiles/libopencm3/meson.build | 60 ------- src/platforms/meson.build | 49 +----- 5 files changed, 8 insertions(+), 303 deletions(-) delete mode 100755 deps/packagefiles/libopencm3/configure delete mode 100644 deps/packagefiles/libopencm3/meson.build diff --git a/.github/workflows/build-pr.yml b/.github/workflows/build-pr.yml index e4f8f8a72dc..0b9f070e527 100644 --- a/.github/workflows/build-pr.yml +++ b/.github/workflows/build-pr.yml @@ -334,11 +334,6 @@ jobs: with: submodules: true - # Prepare the submodule as they conflict with the Meson build by default - # This is mesons job, but I can't figure out how to make it work :( - - name: Prepare submodules - run: cp -r deps/packagefiles/libopencm3/* deps/libopencm3/ - # Build the firmware for all platform variants (currently available) - name: Build run: | diff --git a/deps/libopencm3 b/deps/libopencm3 index 834a4184a3c..446fa8187a0 160000 --- a/deps/libopencm3 +++ b/deps/libopencm3 @@ -1 +1 @@ -Subproject commit 834a4184a3c8fe8495f8013f0b9649e93f57ef33 +Subproject commit 446fa8187a0a108aa5c5322bfe4b347d2e863d7c diff --git a/deps/packagefiles/libopencm3/configure b/deps/packagefiles/libopencm3/configure deleted file mode 100755 index 06e1df1fd36..00000000000 --- a/deps/packagefiles/libopencm3/configure +++ /dev/null @@ -1,195 +0,0 @@ -#!/usr/bin/python -# This file is part of the Black Magic Debug project. -# -# Copyright (C) 2023 1BitSquared -# Written by Rafael Silva -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions are met: -# -# 1. Redistributions of source code must retain the above copyright notice, this -# list of conditions and the following disclaimer. -# -# 2. Redistributions in binary form must reproduce the above copyright notice, -# this list of conditions and the following disclaimer in the documentation -# and/or other materials provided with the distribution. -# -# 3. Neither the name of the copyright holder nor the names of its -# contributors may be used to endorse or promote products derived from -# this software without specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE -# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER -# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, -# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -# This script is used to wrap the libopencm3 build system to allow out-of-tree builds. -# This is required because libopencm3's build system does not provide a way to build out-of-tree, and does not provide an install target. -# Both are required for the meson external project module to work, so we wrap the build system to hack it in. -# Note that with this wrapper, the build log reports build failures when in fact the build succeeded. - -import argparse -import os.path -import shutil - -SOURCE_DIR = os.path.dirname(os.path.realpath(__file__)) - -# Original file, alias in build directory -TO_COPY = { - "Makefile": "libopencm3.mk", # Copy original Makefile to build directory as libopencm3.mk - "scripts/irq2nvic_h": None, # Copy script scripts/irq2nvic_h - "lib": None, # Copy directory lib - "include": None, # Copy directory include -} - -# Wrap Makefile -WRAP_MAKEFILE_NAME = "Makefile" -WRAP_MAKEFILE = """# This file is part of the Black Magic Debug project. -# Copyright (C) 2023 1BitSquared -# -# This is a generated file. Do not edit. -# generated by {configure} - -# Verbosity -# Variable Q is provided by original makefile to control cmd echo -V = {verbose} - -# Toolchain prefix -PREFIX = {toolchain} - -# Include the original makefile -include {makefile} - -# Installation prefix -INSTALL_PREFIX = $(DESTDIR){prefix} -LIBDIR = $(INSTALL_PREFIX)/{libdir} -INCDIR = $(INSTALL_PREFIX)/{incdir} - -install: install-headers install-lib - -install-headers: -\t@printf " INSTALL $(INCDIR)\\n"; -\t$(Q)mkdir -p $(INCDIR) -\t$(Q)cp -r include/* $(INCDIR) -\t$(Q)mkdir -p $(INCDIR)/usb -\t$(Q)cp -r lib/usb/*.h $(INCDIR)/usb - -install-lib: -\t@printf " INSTALL $(LIBDIR)\\n"; -\t$(Q)mkdir -p $(LIBDIR) -\t$(Q)cp -r lib/*.a $(LIBDIR) -\t$(Q)cp -r lib/*.ld $(LIBDIR) - -.PHONY: install - -""" - - -def clean(outdir: str) -> None: - if not os.path.exists(outdir): - return - - print(f"Cleaning build directory `{outdir}`") - - # Remove files from build directory - for file in TO_COPY: - alias = TO_COPY[file] - dst = os.path.join(outdir, alias or file) - if os.path.exists(dst): - if os.path.isdir(dst): - shutil.rmtree(dst) - else: - os.remove(dst) - - # Remove wrap Makefile - makefile = os.path.join(outdir, WRAP_MAKEFILE_NAME) - if os.path.exists(makefile): - os.remove(makefile) - - -def configure(outdir: str, toolchain: str, prefix: str, libdir: str, incdir: str, verbose: bool) -> None: - if not os.path.exists(outdir): - os.makedirs(os.path.dirname(dst), exist_ok=True) - - print(f"Configuring build directory `{outdir}`") - - # Copy files from source directory - for file in TO_COPY: - alias = TO_COPY[file] - src = os.path.join(SOURCE_DIR, file) - dst = os.path.join(outdir, alias or file) - if os.path.isdir(src): - shutil.copytree(src, dst, dirs_exist_ok=True) - else: - os.makedirs(os.path.dirname(dst), exist_ok=True) - shutil.copy2(src, dst) - - # Create wrap Makefile - makefile = os.path.join(outdir, WRAP_MAKEFILE_NAME) - with open(makefile, "w") as f: - f.write( - WRAP_MAKEFILE.format( - configure=__file__, - verbose="1" if verbose else "0", - toolchain=toolchain, - makefile=TO_COPY["Makefile"], - prefix=prefix, - libdir=libdir, - incdir=incdir, - ), - ) - - -def main(): - parser = argparse.ArgumentParser(description="Configure libopencm3 for out-of-tree build") - parser.add_argument( - "--outdir", - help="Build directory", - default=os.getcwd(), - ) - parser.add_argument( - "--toolchain", - default="arm-none-eabi-", - ) - parser.add_argument( - "--prefix", - help="Installation prefix", - default="/usr/local", - ) - parser.add_argument( - "--libdir", - help="Installation library directory", - default="lib", - ) - parser.add_argument( - "--incdir", - help="Installation include directory", - default="include", - ) - parser.add_argument( - "--host", - help="Not used, provided by meson for compatibility", - ) - parser.add_argument( - "--verbose", - action="store_true", - help="Verbose output", - ) - args = parser.parse_args() - - assert args.outdir != SOURCE_DIR, "Cannot configure in-tree, use a different directory from source" - - print("Configuring libopencm3 for out-of-tree build") - - clean(args.outdir) - configure(args.outdir, args.toolchain, args.prefix, args.libdir, args.incdir, args.verbose) - - -if __name__ == "__main__": - main() diff --git a/deps/packagefiles/libopencm3/meson.build b/deps/packagefiles/libopencm3/meson.build deleted file mode 100644 index 9e2554846d9..00000000000 --- a/deps/packagefiles/libopencm3/meson.build +++ /dev/null @@ -1,60 +0,0 @@ -project( - 'libopencm3', - 'c', - version: 'v0.8.0', - license: 'GPL-3.0-or-later OR BSD-3-Clause OR MIT', -) - -# Ideally this would provide a complete meson build system, but libopencm3 includes some complexities (code generation) that make it not trivial to do so. -# -# To save myself from madness I opted to integrate it with the external project meson module instead (unstable) and hack it a bit to make it work. -# Why 'hack'? The libopencm3 build system is not very flexible, it does not provide a way to build out-of-tree, and does not provide an install target, -# both are required for the meson external project module to work, the hack is a script copying the required sources and a wrapper Makefile. -# (see lib/packagefiles/libopencm3/configure) -# -# If you are reading this and have the drive to port a proper meson build system for libopencm3, please go ahead (and thank you!). - -# Ensure we are cross-compiling and not building for the build host -assert(meson.is_cross_build(), 'libopencm3 must be cross-compiled') - -# Ensure we are using a GCC compiler -compiler = meson.get_compiler('c') -assert(compiler.get_id() == 'gcc', 'libopencm3 must be compiled with GCC') - -# Import the external project meson module -external_project = import('unstable-external_project') - -# Parse the compiler prefix -compiler_prefix = compiler.cmd_array()[0].substring(0, -3) # Remove 'gcc' suffix -summary('GCC prefix', compiler_prefix) - -# Add the libopencm3 project as an external project -libopencm3_project = external_project.add_project( - 'configure', - configure_options: [ - '--prefix=@PREFIX@', - '--libdir=@LIBDIR@', - '--incdir=@INCLUDEDIR@', - f'--toolchain=@compiler_prefix@', - '--verbose', - ], -) - -# Get all targets/libraries supported by libopencm3 -make = find_program('make') -libopencm3_targets = run_command( - make, - '--no-print-directory', - '-C', - meson.current_source_dir(), - 'list-targets', - check: true, -).stdout().split(' ') - -summary('Targets', libopencm3_targets, list_sep: ', ') - -# Declare a libopencm3 dependency for each library -foreach target : libopencm3_targets - library = ''.join(['opencm3_', target.replace('/', '')]) - meson.override_dependency(library, libopencm3_project.dependency(library)) -endforeach diff --git a/src/platforms/meson.build b/src/platforms/meson.build index 0e2b6ff2f2c..a077fdef417 100644 --- a/src/platforms/meson.build +++ b/src/platforms/meson.build @@ -28,48 +28,13 @@ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -# include libopencm3 subproject, providing dependencies for some platforms -libopencm3 = subproject('libopencm3', required: false) -if not libopencm3.found() - # Because for now we are sharing the directory for the libopencm3 subproject for meson and - # the libopencm3 submodule, and the upstream submodule does not contain the meson build - # we need to let meson fetch it itself as that is when the meson build overlay is applied, - # otherwise, meson detects that the subproject exists, but does not know what to do with it - # This is a workaround until the upstream buildsystem is updated to include the meson build - # - # This checks that the directory exists, and if it does, it checks if it contains the meson - # build overlay, if it does not, it means that the user cloned the submodule - # - # This just code is not required per-se, it is just here to provide the user with a better - # error message so they know what to do, as it is not obvious what and why the problem is - libopencm3_dir = meson.global_source_root() / 'deps' / 'libopencm3' - libopencm3_no_overlay = fs.is_dir(libopencm3_dir) and not fs.is_file(libopencm3_dir / 'meson.build') - error( - 'libopencm3 subproject configuration failed', - libopencm3_no_overlay ? ''' - -The subproject directory exists but does not contain the meson build overlay, this is likely -because you cloned it as a submodule, either manually, or by running the `make` buildsystem - -Please remove the submodule and try again, you can do this by running the following commands on -the root of the repository: - - `git submodule deinit --force deps/libopencm3` - - `meson subprojects purge --confirm libopencm3` - -Note that this will remove all files in `deps/libopencm3`, any changes you made will be lost. -''' : ''' - -An unknown problem ocurred, the subproject was not found but should have been fetched automatically - -Try the following: - - Make sure you have a working internet connection and try again - - Create a fresh build directory and configure without any extra options - - Make a fresh clone of the repository, do not clone the submodules, meson will take care of it - -If the problem persists, please report the issue to https://github.com/blackmagic-debug/blackmagic/issues -''', - ) -endif +# Pull in the libopencm3 subproject for various platform dependencies +libopencm3 = subproject( + 'libopencm3', + default_options: [ + 'c_std=c11', + ], +) subdir('common') subdir('common/stm32')