Skip to content

Commit 9ac70a1

Browse files
committed
Create a wrapper around the SDK build for libstdc++
This filters out some compiler flags which aren't compatible with libstdc++ sources including runtime type information (rtti), the imacros files used for Zephyr code and the language dialect and warning settings. The URL directory and basename can be configured with defaults set to align with Zephyr SDK 0.17.0. Fake headers and libraries for gmp, mpc and mpfr are included to get past the gcc configure script without needing those libraries installed. They aren't actually used by libstdc++. Signed-off-by: Keith Packard <[email protected]>
0 parents  commit 9ac70a1

File tree

10 files changed

+277
-0
lines changed

10 files changed

+277
-0
lines changed

CMakeLists.txt

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
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)

Kconfig

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright © 2024 Amazon.com, Inc. or its affiliates.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
config LIBSTDCXX_MODULE_DIR
5+
string "libstdc++ URL"
6+
default "https://github.com/zephyrproject-rtos/gcc/archive"
7+
help
8+
Specify the website path base for the libstdc++ .tar.gz files.
9+
10+
config LIBSTDCXX_MODULE_BASENAME
11+
string "libstdc++ basename"
12+
default "428d8d7b0542338244ca41ac06a5f3fa4f29bb6d"
13+
help
14+
Specify the basename portion of the libstdc++ module URL.
15+
The full URL is constructed by concatenating
16+
LIBSTDCXX_MODULE_DIR, LIBSTDCXX_MODULE_BASE and .tar.gz.
17+
For github URLs, this portion will usually be the git
18+
revision hash value and should match the value used in
19+
sdk-ng for a suitable release.

LICENSE

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
SPDX-License-Identifier: BSD-3-Clause
2+
3+
Copyright © 2024 Keith Packard
4+
5+
Redistribution and use in source and binary forms, with or without
6+
modification, are permitted provided that the following conditions
7+
are met:
8+
9+
1. Redistributions of source code must retain the above copyright
10+
notice, this list of conditions and the following disclaimer.
11+
12+
2. Redistributions in binary form must reproduce the above
13+
copyright notice, this list of conditions and the following
14+
disclaimer in the documentation and/or other materials provided
15+
with the distribution.
16+
17+
3. Neither the name of the copyright holder nor the names of its
18+
contributors may be used to endorse or promote products derived
19+
from this software without specific prior written permission.
20+
21+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24+
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25+
COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
26+
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29+
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30+
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31+
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
32+
OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Zephyr libstdc++ module
2+
3+
This module builds libstdc++ using Zephyr SDK source code. Note that
4+
the LICENSE file in this directory covers only these packaging files
5+
and does not relate to the license of the libstdc++ source code.

include/gmp.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#define __GNU_MP_VERSION 6
2+
#define __GNU_MP_VERSION_MINOR 3
3+
#define __GNU_MP_VERSION_PATCHLEVEL 0
4+
#define __GNU_MP_RELEASE (__GNU_MP_VERSION * 10000 + __GNU_MP_VERSION_MINOR * 100 + __GNU_MP_VERSION_PATCHLEVEL)
5+

include/mpc.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <mpfr.h>
2+
#define MPC_VERSION_MAJOR 1
3+
#define MPC_VERSION_MINOR 3
4+
#define MPC_VERSION_PATCHLEVEL 1
5+
#define MPC_VERSION_STRING "1.3.1"
6+
#define MPC_VERSION_NUM(a,b,c) (((a) << 16L) | ((b) << 8) | (c))
7+
#define MPC_VERSION \
8+
MPC_VERSION_NUM(MPC_VERSION_MAJOR,MPC_VERSION_MINOR,MPC_VERSION_PATCHLEVEL)
9+
10+
typedef int mpc_t;
11+
#define MPC_RNDNN 0
12+
#define mpc_init2(a,b)
13+
#define mpc_set_ui_ui(a,b,c,d)
14+
#define mpc_cosh(a,b,c)
15+
#define mpc_pow(a,b,c,d)
16+
#define mpc_acosh(a,b,c)
17+
#define mpc_clear(a)

include/mpfr.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#define MPFR_VERSION_MAJOR 4
2+
#define MPFR_VERSION_MINOR 2
3+
#define MPFR_VERSION_PATCHLEVEL 1
4+
#define MPFR_VERSION_STRING "4.2.1"
5+
/* Macros dealing with MPFR VERSION */
6+
#define MPFR_VERSION_NUM(a,b,c) (((a) << 16L) | ((b) << 8) | (c))
7+
#define MPFR_VERSION \
8+
MPFR_VERSION_NUM(MPFR_VERSION_MAJOR,MPFR_VERSION_MINOR,MPFR_VERSION_PATCHLEVEL)
9+
10+
typedef int mpfr_t;
11+
#define MPFR_RNDN 0
12+
#define mpfr_init(a)
13+
#define mpfr_atan2(a,b,c,d)
14+
#define mpfr_erfc(a,b,c)
15+
#define mpfr_subnormalize(a,b,c)
16+
#define mpfr_clear(a)

lib/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
add_custom_target(gmp ${CMAKE_CURRENT_SOURCE_DIR}/fake-library libgmp.a)
2+
add_custom_target(mpfr ${CMAKE_CURRENT_SOURCE_DIR}/fake-library libmpfr.a)
3+
add_custom_target(mpc ${CMAKE_CURRENT_SOURCE_DIR}/fake-library libmpc.a)

lib/fake-library

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
rm -f "$1"
3+
ln -s `cc -print-file-name=libc.so` "$1"

zephyr/module.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
name: libstdcxx
2+
build:
3+
cmake: .
4+
kconfig: ./Kconfig

0 commit comments

Comments
 (0)