Skip to content

Build system changes to allow compiler toolchain override #454

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 25 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ project (Jerry CXX C ASM)

# Require g++ of version >= 4.7.0
if(NOT CMAKE_COMPILER_IS_GNUCXX)
message(FATAL_ERROR "g++ compiler is required")
message(WARNING "g++ compiler is required")
else()
execute_process(COMMAND ${CMAKE_CXX_COMPILER} -dumpversion
OUTPUT_VARIABLE GNU_CXX_VERSION
Expand All @@ -32,10 +32,14 @@ project (Jerry CXX C ASM)
get_filename_component(PATH_TO_GCC ${CMAKE_C_COMPILER} REALPATH)
get_filename_component(DIRECTORY_GCC ${PATH_TO_GCC} DIRECTORY)
get_filename_component(FILE_NAME_GCC ${PATH_TO_GCC} NAME)
string(REPLACE "gcc" "gcc-ar" CMAKE_AR ${FILE_NAME_GCC})
string(REPLACE "gcc" "gcc-ranlib" CMAKE_RANLIB ${FILE_NAME_GCC})
set(CMAKE_AR ${DIRECTORY_GCC}/${CMAKE_AR})
set(CMAKE_RANLIB ${DIRECTORY_GCC}/${CMAKE_RANLIB})
if(NOT DEFINED CMAKE_AR)
string(REPLACE "gcc" "gcc-ar" CMAKE_AR ${FILE_NAME_GCC})
set(CMAKE_AR ${DIRECTORY_GCC}/${CMAKE_AR})
endif()
if(NOT DEFINED CMAKE_RANLIB)
string(REPLACE "gcc" "gcc-ranlib" CMAKE_RANLIB ${FILE_NAME_GCC})
set(CMAKE_RANLIB ${DIRECTORY_GCC}/${CMAKE_RANLIB})
endif()

# Imported and third-party targets prefix
set(PREFIX_IMPORTED_LIB imported_)
Expand Down Expand Up @@ -202,8 +206,12 @@ project (Jerry CXX C ASM)

# LTO
if("${ENABLE_LTO}" STREQUAL "ON")
set(COMPILE_FLAGS_JERRY "${COMPILE_FLAGS_JERRY} -flto -fno-fat-lto-objects")
set(LINKER_FLAGS_COMMON "${LINKER_FLAGS_COMMON} -flto")
if(CMAKE_COMPILER_IS_GNUCC AND CMAKE_COMPILER_IS_GNUCXX)
set(COMPILE_FLAGS_JERRY "${COMPILE_FLAGS_JERRY} -flto -fno-fat-lto-objects")
set(LINKER_FLAGS_COMMON "${LINKER_FLAGS_COMMON} -flto")
else()
message(FATAL_ERROR "LTO is only supported for g++ (consider using LTO=OFF)")
endif()
endif()

# Turn off stack protector
Expand All @@ -213,11 +221,17 @@ project (Jerry CXX C ASM)
set(COMPILE_FLAGS_JERRY "${COMPILE_FLAGS_JERRY} -g -gdwarf-4")

# Warnings
set(COMPILE_FLAGS_JERRY "${COMPILE_FLAGS_JERRY} -Wall -Wextra -pedantic -Wlogical-op")
set(COMPILE_FLAGS_JERRY "${COMPILE_FLAGS_JERRY} -Wall -Wextra -pedantic")
set(COMPILE_FLAGS_JERRY "${COMPILE_FLAGS_JERRY} -Wformat-nonliteral -Winit-self -Wno-stack-protector")
set(COMPILE_FLAGS_JERRY "${COMPILE_FLAGS_JERRY} -Wconversion -Wsign-conversion -Wformat-security")
set(COMPILE_FLAGS_JERRY "${COMPILE_FLAGS_JERRY} -Wmissing-declarations -Wno-attributes")
set(COMPILE_FLAGS_JERRY "${COMPILE_FLAGS_JERRY} -Werror -Wfatal-errors")
if(CMAKE_COMPILER_IS_GNUCC AND CMAKE_COMPILER_IS_GNUCXX)
set(COMPILE_FLAGS_JERRY "${COMPILE_FLAGS_JERRY} -Wlogical-op")
endif()

# Extra flags
set(COMPILE_FLAGS_JERRY "${COMPILE_FLAGS_JERRY} ${EXTRA_COMPILE_FLAGS}")

# Static build
set(LINKER_FLAGS_STATIC "-static")
Expand All @@ -226,7 +240,9 @@ project (Jerry CXX C ASM)
set(CXX_FLAGS_JERRY "-std=c++11 -fno-exceptions -fno-rtti")

# Turn off implicit template instantiation
set(CXX_FLAGS_JERRY "${CXX_FLAGS_JERRY} -fno-implicit-templates -fno-implicit-inline-templates")
if(CMAKE_COMPILER_IS_GNUCXX)
set(CXX_FLAGS_JERRY "${CXX_FLAGS_JERRY} -fno-implicit-templates -fno-implicit-inline-templates")
endif()

# C
set(C_FLAGS_JERRY "-std=c99")
Expand Down
21 changes: 20 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,24 @@
QLOG := >/dev/null
endif

# Toolchain overrride
CMAKE_TOOLCHAIN_OPTIONS :=
ifdef CC
CMAKE_TOOLCHAIN_OPTIONS += -DCMAKE_C_COMPILER=$(CC)
endif
ifdef CXX
CMAKE_TOOLCHAIN_OPTIONS += -DCMAKE_CXX_COMPILER=$(CXX)
endif
ifdef EXTRA_CFLAGS
CMAKE_TOOLCHAIN_OPTIONS += -DEXTRA_COMPILE_FLAGS="$(EXTRA_CFLAGS)"
endif
ifdef AR
CMAKE_TOOLCHAIN_OPTIONS += -DCMAKE_AR=$(AR)
endif
ifdef RANLIB
CMAKE_TOOLCHAIN_OPTIONS += -DCMAKE_RANLIB=$(RANLIB)
endif

# External build configuration
# Flag, indicating whether to use compiler's default libc (YES / NO)
USE_COMPILER_DEFAULT_LIBC ?= NO
Expand All @@ -91,6 +109,7 @@
# Compiler to use for external build
EXTERNAL_C_COMPILER ?= arm-none-eabi-gcc
EXTERNAL_CXX_COMPILER ?= arm-none-eabi-g++
CMAKE_TOOLCHAIN_OPTIONS += -DUSE_COMPILER_DEFAULT_LIBC=$(USE_COMPILER_DEFAULT_LIBC)

export TARGET_DEBUG_MODES = debug
export TARGET_RELEASE_MODES = release
Expand Down Expand Up @@ -176,7 +195,7 @@ $(BUILD_DIRS_NATIVE): prerequisites
mkdir -p $@; \
echo "$$TOOLCHAIN" > $@/toolchain.config
$(Q) cd $@ && \
(cmake -DENABLE_VALGRIND=$(VALGRIND) -DENABLE_LOG=$(LOG) -DENABLE_LTO=$(LTO) -DCMAKE_TOOLCHAIN_FILE=`cat toolchain.config` ../../.. 2>&1 | tee cmake.log $(QLOG) ; ( exit $${PIPESTATUS[0]} ) ) || \
(cmake $(CMAKE_TOOLCHAIN_OPTIONS) -DENABLE_VALGRIND=$(VALGRIND) -DENABLE_LOG=$(LOG) -DENABLE_LTO=$(LTO) -DCMAKE_TOOLCHAIN_FILE=`cat toolchain.config` ../../.. 2>&1 | tee cmake.log $(QLOG) ; ( exit $${PIPESTATUS[0]} ) ) || \
(echo "CMake run failed. See "`pwd`"/cmake.log for details."; exit 1;); \

.PHONY: $(BUILD_DIRS_STM32F3)
Expand Down
12 changes: 9 additions & 3 deletions build/configs/toolchain_linux_x86_64.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR x86_64)

find_program(CMAKE_C_COMPILER NAMES x86_64-linux-gnu-gcc x86_64-unknown-linux-gnu-gcc)
find_program(CMAKE_CXX_COMPILER NAMES x86_64-linux-gnu-g++ x86_64-unknown-linux-gnu-g++)
if(NOT DEFINED CMAKE_C_COMPILER)
find_program(CMAKE_C_COMPILER NAMES x86_64-linux-gnu-gcc x86_64-unknown-linux-gnu-gcc)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@akiss77, maybe, we should introduce some set of toolchain_*.cmake files for different compilers. The files would set compiler executable names and also apply compiler-specific values of build options (for example FLAGS_LTO=-flto for gcc / g++) / warnings configuration etc.
Also, maybe we should extract the architecture-specific part to sub-configuration files for each supported compiler-architecture pair and put compiler-architecture specific flags / options to the files.

In the case, we could remove gcc / g++ specific if blocks from CMakeLists.txt, and also would support clang build for arhitectures other than x86_64.

What do you think about this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ruben-ayrapetyan, having "worked-for-us" defaults in the build system is a good thing (actually, that's what we already have in the cmake listfile) but not letting developers deviate from the defaults (i.e., "set compiler executable names") is not, IMHO. There will always be platforms and/or tool chains what we don't have access to (or setups which slightly differ from our development environment, see #489 ). I'd definitely vote for a solution where developers have the maximum freedom to choose tools and settings for the build if they want to adapt to their environment or simply experiment.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@akiss77, I agree to you that we should provide maximum freedom to choose build tools and settings.

I think, we should make the choice as simple, as possible.
To make choice simple, we should provide a simple way of switching to another tool (from predefined set or some new / unknown).

In current version of CMakeLists.txt there are some dependencies on gcc / g++.
I think that the dependencies should be moved to configuration files, external to CMakeLists.txt and also add another necessary configurations (currently, for clang).
In the way, we could save logic and structure of CMakeLists.txt.

In the case, any developer would have simple way to add support of another compiler, without changing logic of CMakeLists.txt.

If there would be some features that would not be provided up to the moment, the CMakeLists.txt could be generalized for the cases.

What do you think about this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would the above-described approach allow overriding the defaults from command line as usually expected to work with make, e.g., make CC=xxx CFLAGS=xxx or only by defining configuration files?

endif()
if(NOT DEFINED CMAKE_CXX_COMPILER)
find_program(CMAKE_CXX_COMPILER NAMES x86_64-linux-gnu-g++ x86_64-unknown-linux-gnu-g++)
endif()
# FIXME: This could break cross compilation, when the strip is not for the target architecture
find_program(CMAKE_STRIP NAMES x86_64-linux-gnu-strip x86_64-unknown-linux-gnu-strip strip)

set(FLAGS_COMMON_ARCH -ffixed-rbp)
if(CMAKE_COMPILER_IS_GNUCC AND CMAKE_COMPILER_IS_GNUCXX)
set(FLAGS_COMMON_ARCH -ffixed-rbp)
endif()