|
| 1 | +# |
| 2 | +# SPDX-License-Identifier: BSD-3-Clause |
| 3 | +# |
| 4 | +# Copyright © 2024 Keith Packard |
| 5 | +# |
| 6 | +# Redistribution and use in source and binary forms, with or without |
| 7 | +# modification, are permitted provided that the following conditions |
| 8 | +# are met: |
| 9 | +# |
| 10 | +# 1. Redistributions of source code must retain the above copyright |
| 11 | +# notice, this list of conditions and the following disclaimer. |
| 12 | +# |
| 13 | +# 2. Redistributions in binary form must reproduce the above |
| 14 | +# copyright notice, this list of conditions and the following |
| 15 | +# disclaimer in the documentation and/or other materials provided |
| 16 | +# with the distribution. |
| 17 | +# |
| 18 | +# 3. Neither the name of the copyright holder nor the names of its |
| 19 | +# contributors may be used to endorse or promote products derived |
| 20 | +# from this software without specific prior written permission. |
| 21 | +# |
| 22 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 | +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 24 | +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 25 | +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 26 | +# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 27 | +# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 28 | +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 29 | +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 30 | +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 31 | +# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 32 | +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 33 | +# OF THE POSSIBILITY OF SUCH DAMAGE. |
| 34 | +# |
| 35 | + |
| 36 | +if(NOT CONFIG_GLIBCXX_LIBCPP_USE_MODULE) |
| 37 | + return() |
| 38 | +endif() |
| 39 | + |
| 40 | +cmake_minimum_required(VERSION 3.14.0) |
| 41 | +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) |
| 42 | +project(libstdc++ CXX) |
| 43 | + |
| 44 | +# |
| 45 | +# Generate a subset of the compile options, removing Zephyr-specific settings |
| 46 | +# which aren't appropriate for building libstdc++. This replicates the |
| 47 | +# functionality of zephyr_get_compile_options_for_lang_as_string with the |
| 48 | +# addition of the filter step. |
| 49 | +# |
| 50 | +function(libstdcxx_get_compile_options_for_lang_as_string lang i) |
| 51 | + get_property(all_options TARGET zephyr_interface PROPERTY INTERFACE_COMPILE_OPTIONS) |
| 52 | + process_flags(${lang} all_options all_option_list) |
| 53 | + set(option_list "") |
| 54 | + # |
| 55 | + # Filter out imacros, warning and rtti settings |
| 56 | + # |
| 57 | + set(skip_next 0) |
| 58 | + foreach(option ${all_option_list}) |
| 59 | + if (skip_next) |
| 60 | + set(skip_next 0) |
| 61 | + elseif(option MATCHES "compiler,imacros" OR option MATCHES "compiler,include") |
| 62 | + set(skip_next 1) |
| 63 | + elseif(option MATCHES "compiler-cpp,no_rtti" OR |
| 64 | + option MATCHES "compiler-cpp,dialect" OR |
| 65 | + option MATCHES "compiler,warning" OR |
| 66 | + option MATCHES "compiler-cpp,warning") |
| 67 | + else() |
| 68 | + list(APPEND option_list ${option}) |
| 69 | + endif() |
| 70 | + endforeach() |
| 71 | + |
| 72 | + # |
| 73 | + # The picolibc module doesn't get configured until after this one, |
| 74 | + # so we don't get its include directory in cxx_system_includes above. |
| 75 | + # Stick it in cxx_options as a convenient place |
| 76 | + # |
| 77 | + if(CONFIG_PICOLIBC_USE_MODULE) |
| 78 | + list(APPEND option_list "-isystem" "$<TARGET_PROPERTY:c,INTERFACE_SYSTEM_INCLUDE_DIRECTORIES>") |
| 79 | + endif() |
| 80 | + |
| 81 | + list(APPEND option_list "-D_DEFAULT_SOURCE") |
| 82 | + |
| 83 | + string(REPLACE ";" "$<SEMICOLON>" genexp_options "${option_list}") |
| 84 | + |
| 85 | + set(args_DELIMITER " ") |
| 86 | + |
| 87 | + set(gen_option_list "$<JOIN:${genexp_options},${args_DELIMITER}>") |
| 88 | + |
| 89 | + convert_list_of_flags_to_string_of_flags(gen_option_list options) |
| 90 | + set(${i} ${options} PARENT_SCOPE) |
| 91 | +endfunction() |
| 92 | + |
| 93 | +zephyr_get_system_include_directories_for_lang_as_string(CXX cxx_system_includes) |
| 94 | +zephyr_get_compile_definitions_for_lang_as_string( CXX cxx_definitions) |
| 95 | +libstdcxx_get_compile_options_for_lang_as_string( CXX cxx_options) |
| 96 | + |
| 97 | +zephyr_get_system_include_directories_for_lang_as_string(C c_system_includes) |
| 98 | +zephyr_get_compile_definitions_for_lang_as_string( C c_definitions) |
| 99 | +libstdcxx_get_compile_options_for_lang_as_string( C c_options) |
| 100 | + |
| 101 | +set(LIBSTDCXX_URL "${CONFIG_LIBSTDCXX_MODULE_DIR}/${CONFIG_LIBSTDCXX_MODULE_BASENAME}.tar.gz") |
| 102 | + |
| 103 | +include(ExternalProject) |
| 104 | + |
| 105 | +set(BUILD_TOOLS |
| 106 | + "CFLAGS_FOR_TARGET=${c_definitions} ${c_system_includes} ${c_options} ${CMAKE_REQUIRED_FLAGS}" |
| 107 | + "CXXFLAGS_FOR_TARGET=${cxx_definitions} ${cxx_system_includes} ${cxx_options} ${CMAKE_REQUIRED_FLAGS}" |
| 108 | + XGCC_FLAGS_FOR_TARGET= |
| 109 | + CC_FOR_TARGET=${CMAKE_C_COMPILER} |
| 110 | + GCC_FOR_TARGET=${CMAKE_C_COMPILER} |
| 111 | + CXX_FOR_TARGET=${CMAKE_CXX_COMPILER} |
| 112 | + RAW_CXX_FOR_TARGET=${CMAKE_CXX_COMPILER} |
| 113 | + AR_FOR_TARGET=${CMAKE_AR} |
| 114 | + AS_FOR_TARGET=${CMAKE_AS} |
| 115 | + LD_FOR_TARGET=${CMAKE_LINKER} |
| 116 | + NM_FOR_TARGET=${CMAKE_NM} |
| 117 | + OBJDUMP_FOR_TARGET=${CMAKE_OBJDUMP} |
| 118 | + RANLIB_FOR_TARGET=${CMAKE_RANLIB} |
| 119 | + READELF_FOR_TARGET=${CMAKE_READELF} |
| 120 | + STRIP_FOR_TARGET=${CMAKE_STRIP}) |
| 121 | + |
| 122 | +set(CONFIGURE_FLAGS |
| 123 | + --enable-bootstrap |
| 124 | + --disable-multilib |
| 125 | + --disable-decimal-float |
| 126 | + --disable-libffi |
| 127 | + --disable-libgomp |
| 128 | + --disable-libmudflap |
| 129 | + --disable-libquadmath |
| 130 | + --disable-libssp |
| 131 | + --disable-libstdcxx-pch |
| 132 | + --disable-nls |
| 133 | + --disable-shared |
| 134 | + --disable-threads |
| 135 | + --enable-tls |
| 136 | + --enable-cstdio=stdio_pure |
| 137 | + --disable-plugin |
| 138 | + --disable-libstdcxx-verbose |
| 139 | + --mandir=/usr/share/man |
| 140 | + --with-system-zlib |
| 141 | + --with-gnu-as |
| 142 | + --with-gnu-ld |
| 143 | + --with-newlib |
| 144 | + --with-headers=yes |
| 145 | + --without-included-gettext |
| 146 | + "--with-mpc-include=${CMAKE_CURRENT_SOURCE_DIR}/include" |
| 147 | + "--with-mpc-lib=${CMAKE_CURRENT_BINARY_DIR}/lib" |
| 148 | + "--with-mpfr-include=${CMAKE_CURRENT_SOURCE_DIR}/include" |
| 149 | + "--with-mpfr-lib=${CMAKE_CURRENT_BINARY_DIR}/lib" |
| 150 | + "--with-gmp-include=${CMAKE_CURRENT_SOURCE_DIR}/include" |
| 151 | + "--with-gmp-lib=${CMAKE_CURRENT_BINARY_DIR}/lib" |
| 152 | + "--with-gxx-include-dir=${PROJECT_BINARY_DIR}/libstdc++/${CROSS_COMPILE_TARGET}/include" |
| 153 | + "--with-host-libstdcxx=-static-libgcc -Wl,-Bstatic,-lstdc++,-Bdynamic -lm" |
| 154 | + --enable-languages=c,c++ |
| 155 | + --disable-option-checking |
| 156 | + --target=${CROSS_COMPILE_TARGET}) |
| 157 | + |
| 158 | +add_subdirectory(lib) |
| 159 | + |
| 160 | +ExternalProject_Add(libstdcxx_project |
| 161 | + URL "${LIBSTDCXX_URL}" |
| 162 | + PREFIX libstdc++ |
| 163 | + SOURCE_DIR gcc |
| 164 | + BINARY_DIR build |
| 165 | + DEPENDS gmp mpfr mpc |
| 166 | + CONFIGURE_COMMAND ../gcc/configure --prefix=<INSTALL_DIR> ${CONFIGURE_FLAGS} ${BUILD_TOOLS} |
| 167 | + BUILD_COMMAND make all-target-libstdc++-v3 ${BUILD_TOOLS} |
| 168 | + INSTALL_COMMAND make -C ${CROSS_COMPILE_TARGET}/libstdc++-v3 install) |
| 169 | + |
| 170 | +zephyr_include_directories(${PROJECT_BINARY_DIR}/libstdc++/${CROSS_COMPILE_TARGET}/include) |
| 171 | +zephyr_include_directories(${PROJECT_BINARY_DIR}/libstdc++/${CROSS_COMPILE_TARGET}/include/${CROSS_COMPILE_TARGET}) |
| 172 | +set_property(TARGET linker PROPERTY c++_library ${PROJECT_BINARY_DIR}/libstdc++/${CROSS_COMPILE_TARGET}/lib/libstdc++.a) |
| 173 | +add_dependencies(zephyr_interface libstdcxx_project) |
0 commit comments