Skip to content

Commit c3e6dc9

Browse files
committed
Implementing building with CLang (broken by now because of emitting LLVM bitcode in the case of LTO, llvm/llvm-project#55940) and "improved" building with AVR-gcc - no more hardcoded flags set by CMake itself!
1 parent 04d45f8 commit c3e6dc9

File tree

8 files changed

+680
-64
lines changed

8 files changed

+680
-64
lines changed

Arduino-toolchain.cmake

Lines changed: 161 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,179 @@
44
# A toolchain for the Arduino compatile boards.
55
# Please refer to README.md for the usage.
66

7-
# If the version of CMake used is below 3.7.0, exit with error.
8-
#
9-
# Intended to support CMake version 3.0.0, but there are limitations which
10-
# requires a minimum CMake version of 3.7.0. However, wherever possible, the
11-
# toolchain remains compatible with 3.0.0, looking for some workarounds for
12-
# the limitations in the future. The limitations are captured below.
13-
#
14-
# Version below 3.2.0 has no support for continue() command. Can be fixed.
15-
#
16-
# Version below 3.4.0 has no support for target properties BINARY_DIR,
17-
# SOURCE_DIR etc. These are required in target command generator expressions.
18-
#
19-
# Version below 3.6.0 has issues in identifying try_compile output for
20-
# static library. So there are some errors during the configuration, but
21-
# may still possibly work.
7+
# If the version of CMake used is below 3.9, exit with error.
8+
# Version below 3.9.0 has no proper support for INTERPROCEDURAL_OPTIMIZATION.
229
#
2310
# Version below 3.7.0 has no support for CMAKE_SYSTEM_CUSTOM_CODE, which
2411
# is required when there is some dynamic information, like Board options,
2512
# that needs to be included in the toolchain. Here just including the user
2613
# provided path will not work, because the user variables, cache or root
2714
# binary directory path etc. are not passed to try_compile.
2815

29-
if (CMAKE_VERSION VERSION_LESS 3.7.0)
30-
message(FATAL_ERROR "CMake version below 3.7.0 unsupported!!!")
16+
#[[
17+
CLang building works only with llvm toolchain.
18+
19+
todo: CMAKE_<LANG>_FLAGS_INIT¶
20+
21+
USE_CLANG_AS_COMPILER - ON means CLang, OFF means GCC
22+
23+
GCC_COMPILERS_IN_USR_BIN - ON - GCC compilers are in /usr/bin, OFF - GCC compilers are in the dedicated dir
24+
GCC_PREFIX_DOUBLE_USE - ON - gcc compilers name begins with "target double", OFF - doesn't
25+
GCC_SUFFIX_VERSION_USE - ON means the tools will be called like gcc-11, OFF means tools will not have the postfix
26+
27+
LLVM_TOOLS_IN_USR_BIN - ON - LLVM compilers are in /usr/bin, OFF - LLVM compilers are in the dedicated dir
28+
LLVM_SUFFIX_VERSION_USE - ON means the tools will be called like llvm-readelf-14 and clang-14, OFF means tools will not have the postfix
29+
#]]
30+
31+
cmake_minimum_required(VERSION 3.9 FATAL_ERROR)
32+
33+
set(USE_CLANG_AS_COMPILER ON)
34+
#set(REST_OF_TOOLCHAIN_IS_LLVM ON)
35+
set(GCC_PREFIX_DOUBLE_USE ON)
36+
set(GCC_COMPILERS_IN_USR_BIN OFF)
37+
set(GCC_SUFFIX_VERSION_USE OFF)
38+
39+
40+
if(NOT DEFINED CMAKE_HOST_WIN32)
41+
if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
42+
set(CMAKE_HOST_WIN32 ON)
43+
else()
44+
set(CMAKE_HOST_WIN32 OFF)
45+
endif()
46+
endif()
47+
48+
if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
49+
if(NOT DEFINED llvm_Version)
50+
set(llvm_Version 14)
51+
endif()
52+
53+
if(NOT DEFINED LLVM_SUFFIX_VERSION_USE)
54+
set(LLVM_SUFFIX_VERSION_USE OFF)
55+
endif()
56+
if(NOT DEFINED GCC_PREFIX_DOUBLE_USE)
57+
set(GCC_PREFIX_DOUBLE_USE OFF)
58+
endif()
59+
if(NOT DEFINED GCC_SUFFIX_VERSION_USE)
60+
set(GCC_SUFFIX_VERSION_USE OFF)
61+
endif()
62+
if(NOT DEFINED GCC_SUFFIX_FLAVOUR_USE)
63+
set(GCC_SUFFIX_FLAVOUR_USE OFF)
64+
endif()
65+
66+
get_filename_component(DUMP_DIR "${CMAKE_CURRENT_LIST_DIR}" DIRECTORY) # CACHE PATH "The dir where we have unpacked CLang"
67+
message(STATUS "DUMP_DIR ${DUMP_DIR}")
68+
else()
69+
if(NOT DEFINED GCC_COMPILERS_IN_USR_BIN)
70+
set(GCC_COMPILERS_IN_USR_BIN ON)
71+
endif()
72+
73+
if(NOT DEFINED LLVM_TOOLS_IN_USR_BIN)
74+
set(LLVM_TOOLS_IN_USR_BIN OFF)
75+
endif()
76+
endif()
77+
78+
if(NOT DEFINED USE_CLANG_AS_COMPILER)
79+
message(FATAL_ERROR "Set USE_CLANG_AS_COMPILER into ON if you want to build with CLang(++) and into OFF if you want to build with G(CC|++).")
80+
endif()
81+
82+
if(NOT DEFINED REST_OF_TOOLCHAIN_IS_LLVM)
83+
if(USE_CLANG_AS_COMPILER)
84+
set(REST_OF_TOOLCHAIN_IS_LLVM ON)
85+
else()
86+
set(REST_OF_TOOLCHAIN_IS_LLVM OFF)
87+
endif()
88+
endif()
89+
90+
91+
if(CMAKE_HOST_WIN32)
92+
set(GCC_COMPILERS_IN_USR_BIN OFF)
93+
set(LLVM_TOOLS_IN_USR_BIN OFF)
94+
else()
95+
if(NOT DEFINED GCC_COMPILERS_IN_USR_BIN)
96+
message(FATAL_ERROR "You must specify GCC_COMPILERS_IN_USR_BIN")
97+
endif()
98+
endif()
99+
100+
if(NOT DEFINED LLVM_TOOLS_IN_USR_BIN)
101+
message(FATAL_ERROR "You must specify LLVM_TOOLS_IN_USR_BIN")
102+
endif()
103+
104+
if(GCC_COMPILERS_IN_USR_BIN)
105+
if(NOT DEFINED GCC_PREFIX_DOUBLE_USE)
106+
set(GCC_PREFIX_DOUBLE_USE ON)
107+
endif()
108+
if(NOT DEFINED GCC_SUFFIX_VERSION_USE)
109+
set(GCC_SUFFIX_VERSION_USE OFF)
110+
endif()
111+
endif()
112+
113+
if(NOT DEFINED GCC_PREFIX_DOUBLE_USE)
114+
message(FATAL_ERROR "You must specify GCC_PREFIX_DOUBLE_USE")
31115
endif()
32116

33-
# Save the policy state. We will restore it at the end.
34-
cmake_policy(PUSH)
117+
if(NOT DEFINED GCC_SUFFIX_VERSION_USE)
118+
message(FATAL_ERROR "You must specify GCC_SUFFIX_VERSION_USE")
119+
endif()
35120

36-
# Set policy to above 3.0.0
37-
cmake_policy(VERSION 3.0.0)
121+
if(NOT DEFINED TOOLCHAIN_NAME)
122+
set(TOOLCHAIN_NAME "avr")
123+
endif()
38124

39-
# Interpret if() arguments without quotes as variables/keywords
40-
if (NOT CMAKE_VERSION VERSION_LESS 3.1)
41-
cmake_policy(SET CMP0054 NEW)
125+
if(DEFINED ARDUINO_INSTALL_PATH)
126+
if(NOT DEFINED AVR_GCC_ROOT)
127+
set(AVR_GCC_ROOT "${ARDUINO_INSTALL_PATH}/hardware/tools/${TOOLCHAIN_NAME}")
128+
endif()
42129
endif()
130+
message(STATUS "AVR_GCC_ROOT ${AVR_GCC_ROOT}")
131+
132+
if(REST_OF_TOOLCHAIN_IS_LLVM OR USE_CLANG_AS_COMPILER)
133+
if(NOT DEFINED LLVM_SUFFIX_VERSION_USE)
134+
if(LLVM_TOOLS_IN_USR_BIN)
135+
set(LLVM_SUFFIX_VERSION_USE ON)
136+
else()
137+
set(LLVM_SUFFIX_VERSION_USE OFF)
138+
endif()
139+
endif()
140+
141+
if(NOT DEFINED llvm_Version)
142+
if(CMAKE_HOST_WIN32)
143+
message(FATAL_ERROR "You must specify LLVM version into llvm_Version. It is used to set the right additional flags for clang.")
144+
else()
145+
include("${CMAKE_CURRENT_LIST_DIR}/Arduino/System/DetectInstalledLLVMVersion.cmake")
146+
detect_llvm_version(llvm_Version LLVM_ROOT "/usr/lib")
147+
endif()
148+
endif()
149+
150+
if(CMAKE_HOST_WIN32)
151+
if(NOT DEFINED LLVM_ROOT)
152+
if(DEFINED DUMP_DIR)
153+
set(LLVM_ROOT "${DUMP_DIR}/LLVM-${llvm_Version}.0.0-win32")
154+
else()
155+
message(FATAL_ERROR "You must set DUMP_DIR if you don't specify the full path to CLang base dir in LLVM_ROOT") # CACHE PATH "Path to Clang root"
156+
endif()
157+
endif()
158+
else()
159+
if(NOT DEFINED LLVM_ROOT)
160+
if(LLVM_TOOLS_IN_USR_BIN)
161+
set(LLVM_ROOT "") # CACHE PATH "Path to Clang root"
162+
else()
163+
set(LLVM_ROOT "/usr/lib/llvm-${llvm_Version}") # CACHE PATH "Path to Clang root"
164+
endif()
165+
endif()
166+
endif()
167+
168+
if(NOT DEFINED LLVM_SUFFIX_VERSION_USE)
169+
message(FATAL_ERROR "You must specify LLVM_SUFFIX_VERSION_USE")
170+
endif()
171+
172+
if(NOT DEFINED AVR_GCC_ROOT)
173+
set(AVR_GCC_ROOT "/usr/${double}")
174+
endif()# CACHE PATH "Path to MinGW root"
175+
176+
message(STATUS "CLang root: ${LLVM_ROOT}")
177+
message(STATUS "AVR GCC root: ${AVR_GCC_ROOT}")
178+
endif()
179+
43180

44181
#*****************************************************************************
45182
# Set system name and basic information
@@ -97,5 +234,3 @@ file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/build_opt.h" "")
97234
# Do not try to link during the configure time, due to the dependency on the
98235
# core, which we do not have a target yet.
99236
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
100-
101-
cmake_policy(POP)

Arduino/System/BoardBuildTargets.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,7 @@ function(_link_ard_lib_list target_name lib_list_var link_type
696696

697697
# Finally link the target with all the libraries
698698
# message("target_link_libraries(\"${target_name}\" ${link_type} ${_link_targets})")
699+
message(STATUS "_link_targets ${_link_targets}")
699700
if (_link_targets)
700701
target_link_libraries("${target_name}" ${link_type}
701702
${_link_targets})
@@ -736,6 +737,7 @@ function(_add_internal_arduino_library target lib)
736737
set(lib_sources "${CMAKE_CURRENT_BINARY_DIR}/${target}_dummy.cpp")
737738
endif()
738739

740+
message(STATUS "lib target ${target}" )
739741
add_library("${target}" STATIC ${lib_headers} ${lib_sources})
740742
# message("\"${include_dirs}\"")
741743
target_include_directories(${target} PUBLIC ${include_dirs})
@@ -769,6 +771,7 @@ function(_add_internal_arduino_core target)
769771
# get_headers_parent_directories("${core_headers};${variant_headers}" include_dirs)
770772

771773
# Add the library and set the include directories
774+
message(STATUS "core lib target ${target} ${core_sources}")
772775
add_library("${target}" STATIC ${core_headers} ${core_sources}
773776
${variant_headers} ${variant_sources})
774777
# target_include_directories(${target} PUBLIC ${include_dirs})

0 commit comments

Comments
 (0)