Skip to content

Commit

Permalink
build: Add C++ support
Browse files Browse the repository at this point in the history
Adds C++ support to the build system.

Change-Id: Ice1e57a13598e7a48b0bf3298fc318f4ce012ee6
Signed-off-by: Dmitriy Korovkin <[email protected]>
Signed-off-by: Peter Mitsis <[email protected]>
  • Loading branch information
wrsPeterMitsis authored and nashif committed Feb 6, 2016
1 parent eaed145 commit 8e35cc8
Show file tree
Hide file tree
Showing 22 changed files with 448 additions and 3 deletions.
23 changes: 21 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,10 @@ AS = $(CROSS_COMPILE)as
LD = $(CROSS_COMPILE)ld
ifeq ($(USE_CCACHE),1)
CC = $(CCACHE) $(CROSS_COMPILE)gcc
CXX = $(CCACHE) $(CROSS_COMPILE)g++
else
CC = $(CROSS_COMPILE)gcc
CXX = $(CROSS_COMPILE)g++
endif
CPP = $(CC) -E
AR = $(CROSS_COMPILE)ar
Expand Down Expand Up @@ -366,6 +368,18 @@ KBUILD_CFLAGS := -c -g -std=c99 \
-Wno-format-zero-length \
-Wno-main -ffreestanding

KBUILD_CXXFLAGS := -c -g -std=c++11 \
-fno-reorder-functions \
-fno-asynchronous-unwind-tables \
-fno-omit-frame-pointer \
-fcheck-new \
-fno-defer-pop -Wall \
-Wno-unused-but-set-variable \
-Wno-format-zero-length \
-Wno-main -ffreestanding \
-ffunction-sections -fdata-sections \
-fno-rtti -fno-exceptions

KBUILD_AFLAGS := -c -g -xassembler-with-cpp

LDFLAGS += $(call ld-option,-nostartfiles)
Expand All @@ -377,13 +391,13 @@ KERNELVERSION = $(VERSION_MAJOR)$(if $(VERSION_MINOR),.$(VERSION_MINOR)$(if $(PA

export VERSION_MAJOR VERSION_MINOR PATCHLEVEL VERSION_RESERVED EXTRAVERSION
export KERNELRELEASE KERNELVERSION
export ARCH CONFIG_SHELL HOSTCC HOSTCFLAGS CROSS_COMPILE AS LD CC
export ARCH CONFIG_SHELL HOSTCC HOSTCFLAGS CROSS_COMPILE AS LD CC CXX
export CPP AR NM STRIP OBJCOPY OBJDUMP
export MAKE AWK INSTALLKERNEL PERL PYTHON GENIDT GENOFFSET_H
export HOSTCXX HOSTCXXFLAGS CHECK CHECKFLAGS

export KBUILD_CPPFLAGS NOSTDINC_FLAGS ZEPHYRINCLUDE OBJCOPYFLAGS LDFLAGS
export KBUILD_CFLAGS CFLAGS_GCOV KBUILD_AFLAGS AFLAGS_KERNEL
export KBUILD_CFLAGS KBUILD_CXXFLAGS CFLAGS_GCOV KBUILD_AFLAGS AFLAGS_KERNEL
export KBUILD_ARFLAGS


Expand Down Expand Up @@ -599,6 +613,7 @@ export LDFLAG_LINKERCMD
include arch/$(ARCH)/Makefile

KBUILD_CFLAGS += $(CFLAGS)
KBUILD_CXXFLAGS += $(CXXFLAGS)
KBUILD_AFLAGS += $(CFLAGS)


Expand Down Expand Up @@ -1103,6 +1118,10 @@ qemu: zephyr
$(Q)$(MAKE) $(build)=$(build-dir) $(target-dir)$(notdir $@)
%.o: %.c prepare scripts FORCE
$(Q)$(MAKE) $(build)=$(build-dir) $(target-dir)$(notdir $@)
%.o: %.cpp prepare scripts FORCE
$(Q)$(MAKE) $(build)=$(build-dir) $(target-dir)$(notdir $@)
%.o: %.cxx prepare scripts FORCE
$(Q)$(MAKE) $(build)=$(build-dir) $(target-dir)$(notdir $@)
%.lst: %.c prepare scripts FORCE
$(Q)$(MAKE) $(build)=$(build-dir) $(target-dir)$(notdir $@)
%.s: %.S prepare scripts FORCE
Expand Down
1 change: 1 addition & 0 deletions arch/arc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ arch-$(CONFIG_CPU_ARCEM4) = $(call cc-option,-mARCv2EM) \
$(call cc-option,-mav2em,) $(call cc-option,-mno-sdata)

KBUILD_CFLAGS += $(arch-y) $(cflags-y)
KBUILD_CXXFLAGS += $(arch-y) $(cflags-y)
KBUILD_AFLAGS += $(arch-y)
1 change: 1 addition & 0 deletions arch/arm/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ arch-$(CONFIG_CPU_CORTEX_M4) += $(call cc-option,-mthumb -mcpu=cortex-m4) \
cflags-$(CONFIG_LTO) = $(call cc-option,-flto,)

KBUILD_CFLAGS += $(cflags-y) $(arch-y)
KBUILD_CXXFLAGS += $(cflags-y) $(arch-y)
KBUILD_AFLAGS += $(arch-y)

QEMU_CPU_TYPE_arm = cortex-m3
Expand Down
1 change: 1 addition & 0 deletions arch/arm/core/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ obj-y = atomic.o exc_exit.o irq_init.o \
fault_s.o gdb_stub.o isr_wrapper.o \
fatal.o sys_fatal_error_handler.o

obj-$(CONFIG_CPLUSPLUS) += __aeabi_atexit.o
obj-$(CONFIG_MICROKERNEL) += task_abort.o
obj-$(CONFIG_IRQ_OFFLOAD) += irq_offload.o
obj-$(CONFIG_CPU_CORTEX_M) += cortex_m/
38 changes: 38 additions & 0 deletions arch/arm/core/__aeabi_atexit.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2016 Wind River Systems, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/*
* @file
* @brief Basic C++ destructor module for globals for ARM
*/

#include <toolchain.h>

EXTERN_C int __cxa_atexit(void (*destructor)(void *), void *objptr, void *dso);

/**
* @brief Register destructor for a global object
*
* @param objptr global object pointer
* @param destructor the global object destructor function
* @param dso Dynamic Shared Object handle for shared libraries
*
* Wrapper for __cxa_atexit()
*/
int __aeabi_atexit(void *objptr, void (*destructor)(void *), void *dso)
{
return __cxa_atexit(destructor, objptr, dso);
}
1 change: 1 addition & 0 deletions arch/x86/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ arch-$(CONFIG_CPU_ATOM) += $(call cc-option,-march=atom) \
arch-$(CONFIG_CPU_MINUTEIA) += $(call cc-option,-march=pentium)

KBUILD_CFLAGS += $(cflags-y) $(arch-y)
KBUILD_CXXFLAGS += $(cflags-y) $(arch-y)
KBUILD_AFLAGS += $(arch-y)

QEMU_BIOS ?= /usr/share/qemu
Expand Down
12 changes: 12 additions & 0 deletions include/arch/arc/v2/linker.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ SECTIONS {

*(.text)
*(".text.*")
*(.gnu.linkonce.t.*)

_image_text_end = .;
} GROUP_LINK_IN(ROMABLE_REGION)
Expand All @@ -98,6 +99,7 @@ SECTIONS {
__devconfig_end = .;
} GROUP_LINK_IN(ROMABLE_REGION)

#ifdef CONFIG_CPLUSPLUS
SECTION_PROLOGUE(_CTOR_SECTION_NAME,,) {
/*
* The compiler fills the constructor pointers table below, hence
Expand All @@ -114,9 +116,19 @@ SECTIONS {
__CTOR_END__ = .;
} GROUP_LINK_IN(ROMABLE_REGION)

SECTION_PROLOGUE(init_array, (OPTIONAL),)
{
. = ALIGN(4);
__init_array_start = .;
KEEP(*(SORT_BY_NAME(".init_array*")))
__init_array_end = .;
} GROUP_LINK_IN(ROMABLE_REGION)
#endif

SECTION_PROLOGUE(_RODATA_SECTION_NAME,,) {
*(.rodata)
*(".rodata.*")
*(.gnu.linkonce.r.*)
} GROUP_LINK_IN(ROMABLE_REGION)

_image_rom_end = .;
Expand Down
29 changes: 29 additions & 0 deletions include/arch/arm/cortex_m/scripts/linker.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ SECTIONS
_image_text_start = .;
*(.text)
*(".text.*")
*(.gnu.linkonce.t.*)
} GROUP_LINK_IN(ROMABLE_REGION)

_image_text_end = .;
Expand Down Expand Up @@ -128,10 +129,38 @@ SECTIONS
__exidx_end = .;
} GROUP_LINK_IN(ROMABLE_REGION)

#ifdef CONFIG_CPLUSPLUS
SECTION_PROLOGUE(_CTOR_SECTION_NAME,,)
{
/*
* The compiler fills the constructor pointers table below, hence symbol
* __CTOR_LIST__ must be aligned on 4 byte boundary.
* To align with the C++ standard, the first elment of the array
* contains the number of actual constructors. The last element is
* NULL.
*/
. = ALIGN(4);
__CTOR_LIST__ = .;
LONG((__CTOR_END__ - __CTOR_LIST__) / 4 - 2)
KEEP(*(SORT_BY_NAME(".ctors*")))
LONG(0)
__CTOR_END__ = .;
} GROUP_LINK_IN(ROMABLE_REGION)

SECTION_PROLOGUE(init_array, (OPTIONAL),)
{
. = ALIGN(4);
__init_array_start = .;
KEEP(*(SORT_BY_NAME(".init_array*")))
__init_array_end = .;
} GROUP_LINK_IN(ROMABLE_REGION)
#endif

SECTION_PROLOGUE(_RODATA_SECTION_NAME,,)
{
*(.rodata)
*(".rodata.*")
*(.gnu.linkonce.r.*)
} GROUP_LINK_IN(ROMABLE_REGION)

_image_rom_end = .;
Expand Down
29 changes: 29 additions & 0 deletions include/arch/x86/linker-common-sections.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ SECTIONS
*(".text_start.*")
*(.text)
*(".text.*")
*(.gnu.linkonce.t.*)
*(.eh_frame)
*(.init)
*(.fini)
Expand All @@ -83,6 +84,33 @@ SECTIONS

_image_text_end = .;

#ifdef CONFIG_CPLUSPLUS
SECTION_PROLOGUE(_CTOR_SECTION_NAME, ,)
{
/*
* The compiler fills the constructor pointers table below, hence symbol
* __CTOR_LIST__ must be aligned on 4 byte boundary.
* To align with the C++ standard, the first elment of the array
* contains the number of actual constructors. The last element is
* NULL.
*/
. = ALIGN(4);
__CTOR_LIST__ = .;
LONG((__CTOR_END__ - __CTOR_LIST__) / 4 - 2)
KEEP(*(SORT_BY_NAME(".ctors*")))
LONG(0)
__CTOR_END__ = .;
} GROUP_LINK_IN(ROMABLE_REGION)

SECTION_PROLOGUE(init_array, (OPTIONAL),)
{
. = ALIGN(4);
__init_array_start = .;
KEEP(*(SORT_BY_NAME(".init_array*")))
__init_array_end = .;
} GROUP_LINK_IN(ROMABLE_REGION)
#endif

SECTION_PROLOGUE(devconfig, (OPTIONAL),)
{
__devconfig_start = .;
Expand All @@ -95,6 +123,7 @@ SECTIONS
{
*(.rodata)
*(".rodata.*")
*(.gnu.linkonce.r.*)
#if ALL_DYN_STUBS == 0
IDT_MEMORY
#endif
Expand Down
29 changes: 29 additions & 0 deletions include/nanokernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -1363,6 +1363,35 @@ struct nano_timer *_track_list_nano_timer;
}
#endif

#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
/*
* Define new and delete operators.
* At this moment they do nothing since objects are supposed
* to be statically allocated
*/
inline void operator delete(void *ptr)
{
(void)ptr;
}

inline void operator delete[](void *ptr)
{
(void)ptr;
}

inline void *operator new(size_t size)
{
(void)size;
return NULL;
}

inline void *operator new[](size_t size)
{
(void)size;
return NULL;
}
#endif

/* architecture-specific nanokernel public APIs */

#include <arch/cpu.h>
Expand Down
8 changes: 8 additions & 0 deletions kernel/microkernel/k_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ void _main(void)
_sys_device_do_config_level(_SYS_INIT_LEVEL_MICROKERNEL);
_sys_device_do_config_level(_SYS_INIT_LEVEL_APPLICATION);

#ifdef CONFIG_CPLUSPLUS
/* Process the .ctors and .init_array sections */
extern void __do_global_ctors_aux(void);
extern void __do_init_array_aux(void);
__do_global_ctors_aux();
__do_init_array_aux();
#endif


#ifdef CONFIG_WORKLOAD_MONITOR
_k_workload_monitor_calibrate();
Expand Down
8 changes: 8 additions & 0 deletions kernel/nanokernel/nano_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ static void _main(void)
_sys_device_do_config_level(_SYS_INIT_LEVEL_NANOKERNEL);
_sys_device_do_config_level(_SYS_INIT_LEVEL_APPLICATION);

#ifdef CONFIG_CPLUSPLUS
/* Process the .ctors and .init_array sections */
extern void __do_global_ctors_aux(void);
extern void __do_init_array_aux(void);
__do_global_ctors_aux();
__do_init_array_aux();
#endif

extern void main(void);
main();
}
Expand Down
6 changes: 6 additions & 0 deletions misc/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ config TOOLCHAIN_VARIANT
For optimized compilers with reduced features, specify the name
of the variant.

config CPLUSPLUS
bool "Enable C++ support for the application"
default n
help
This option enables the use of applications built with C++.

choice
prompt "C Library"
default MINIMAL_LIBC
Expand Down
2 changes: 2 additions & 0 deletions misc/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
obj-y =
obj-$(CONFIG_CPLUSPLUS) += cpp_virtual.o cpp_vtable.o \
cpp_init_array.o cpp_ctors.o cpp_dtors.o
obj-$(CONFIG_PRINTK) += printk.o
obj-$(CONFIG_REBOOT) += reboot.o
obj-y += generated/
Expand Down
Loading

0 comments on commit 8e35cc8

Please sign in to comment.