Skip to content

Commit 3d98719

Browse files
tejlmandgalak
authored andcommitted
cmake: Introduce CMake package for Zephyr SDK
This commit introduces CMake package for Zephyr SDK. This allows users to install Zephyr SDK at the following locations: ${HOME}/zephyr-sdk* ${HOME}/.local/zephyr-sdk* ${HOME}/.local/opt/zephyr-sdk* /opt/zephyr-sdk* /usr/zephyr-sdk* /usr/local/zephyr-sdk* and have the Zephyr build system automatically detect the installation. The * indicates that it is allowed to install the Zephyr SDK in a location such as: /opt/zephyr-sdk-0.11.0 and it will still be found by CMake. Using CMake package version check further ensures that multiple SDK versions can be installed, and CMake will select the version closets to the required. For example, requiring Zephyr SDK 0.11.1 in a system where the following versions are installed: - /opt/zephyr-sdk-0.10.0 : 0.10.0 (Not matching) - /opt/zephyr-sdk-0.11.1 : 0.11.1 (First matching - will be used) - /opt/zephyr-sdk-0.12.0 : 0.12.0 (Matching) in this case, both 0.11.1 and 0.12.0 are matching the requirement, but 0.11.1 will be chosen as it is closest to the required version. The files from Zephyr repo: host-tools.cmake, generic.cmake, and target.cmake are also moved to the SDK repository, as it allows the Zephyr build system to include the correct cmake files, even when using a future SDK release with older Zephyr, as long as the Zephyr SDK is backward compatible. Signed-off-by: Torsten Rasmussen <[email protected]>
1 parent 30d82dd commit 3d98719

File tree

10 files changed

+206
-12
lines changed

10 files changed

+206
-12
lines changed

.shippable.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ env:
2121
- TARGET=xtensa_nxp_imx_adsp
2222
- TARGET=xtensa_nxp_imx8m_adsp
2323
- TARGET="tools"
24+
- TARGET="cmake"
2425

2526
build:
2627
cache: true
@@ -51,6 +52,9 @@ build:
5152
if [ "${TARGET}" == 'tools' ]; then
5253
echo "uploading zephyr-sdk-x86_64-hosttools-standalone-0.9.sh to ${S3_PATH}/";
5354
aws s3 cp zephyr-sdk-x86_64-hosttools-standalone-0.9.sh ${S3_PATH}/;
55+
elif [ "${TARGET}" == 'cmake' ]; then
56+
echo "uploading ${TARGET}.tar.bz2 to ${S3_PATH}/";
57+
aws s3 cp ${TARGET}.tar.bz2 ${S3_PATH}/;
5458
else
5559
echo "uploading ${TARGET}.tar.bz2 to ${S3_PATH}/";
5660
pushd build/output;

cmake/Zephyr-sdkConfig.cmake

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
# This file provides Zephyr sdk config version package functionality.
4+
#
5+
6+
# Those are Zephyr variables used.
7+
set(SDK_VERSION ${Zephyr-sdk_VERSION})
8+
set(SDK_MAJOR_MINOR ${Zephyr-sdk_VERSION_MAJOR}.${Zephyr-sdk_VERSION_MINOR})
9+
set(SDK_VERSION ${Zephyr-sdk_VERSION_MAJOR}.${Zephyr-sdk_VERSION_MINOR}.${Zephyr-sdk_VERSION_PATCH})
10+
set(SDK_MAJOR_MINOR_MICRO ${SDK_VERSION})
11+
12+
get_filename_component(ZEPHYR_SDK_INSTALL_DIR ${CMAKE_CURRENT_LIST_DIR}/.. ABSOLUTE)
13+
set(ZEPHYR_SDK_INSTALL_DIR ${ZEPHYR_SDK_INSTALL_DIR})
14+
set(ZEPHYR_TOOLCHAIN_VARIANT zephyr)
15+
16+
# Those are CMake package parameters.
17+
set(Zephyr-sdk_FOUND True)
18+
set(Zephyr-sdk_DIR ${ZEPHYR_SDK_INSTALL_DIR})
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
# This file provides Zephyr sdk config package version information.
4+
#
5+
# The purpose of the version file is to ensure that CMake find_package can
6+
# correctly locate the Zephyr sdk installation for building of applications.
7+
8+
file(READ ${CMAKE_CURRENT_LIST_DIR}/../sdk_version SDK_VERSION)
9+
string(STRIP ${SDK_VERSION} PACKAGE_VERSION)
10+
11+
if(PACKAGE_VERSION VERSION_LESS PACKAGE_FIND_VERSION)
12+
set(PACKAGE_VERSION_COMPATIBLE FALSE)
13+
else()
14+
# Currently, this Zephyr SDK is expected to work with any Zephyr project
15+
# requiring this version or any older version.
16+
#
17+
# In case this version is no longer backward compatible then this is the place
18+
# to test, for example as
19+
# set(ZEPHYR_MINIMUM_COMPATIBLE_VERSION 0.11)
20+
# if(PACKAGE_FIND_VERSION VERSION_LESS ZEPHYR_MINIMUM_COMPATIBLE_VERSION)
21+
# set(PACKAGE_VERSION_COMPATIBLE FALSE)
22+
# return()
23+
# endif()
24+
25+
set(PACKAGE_VERSION_COMPATIBLE TRUE)
26+
if(PACKAGE_FIND_VERSION STREQUAL PACKAGE_VERSION)
27+
set(PACKAGE_VERSION_EXACT TRUE)
28+
endif()
29+
endif()

cmake/zephyr/Kconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Zephyr 0.11 SDK Toolchain
2+
3+
# Copyright (c) 2020 Linaro Limited.
4+
# SPDX-License-Identifier: Apache-2.0
5+
6+
config TOOLCHAIN_ZEPHYR_0_11
7+
def_bool y
8+
select HAS_NEWLIB_LIBC_NANO if (ARC || (ARM && !ARM64) || RISCV)

cmake/zephyr/generic.cmake

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
set(TOOLCHAIN_HOME ${ZEPHYR_SDK_INSTALL_DIR})
4+
5+
set(COMPILER gcc)
6+
set(LINKER ld)
7+
set(BINTOOLS gnu)
8+
9+
# Find some toolchain that is distributed with this particular SDK
10+
11+
file(GLOB toolchain_paths
12+
LIST_DIRECTORIES true
13+
${TOOLCHAIN_HOME}/xtensa/*/*-zephyr-elf
14+
${TOOLCHAIN_HOME}/*-zephyr-elf
15+
${TOOLCHAIN_HOME}/*-zephyr-eabi
16+
)
17+
18+
if(toolchain_paths)
19+
list(GET toolchain_paths 0 some_toolchain_path)
20+
21+
get_filename_component(one_toolchain_root "${some_toolchain_path}" DIRECTORY)
22+
get_filename_component(one_toolchain "${some_toolchain_path}" NAME)
23+
24+
set(CROSS_COMPILE_TARGET ${one_toolchain})
25+
set(SYSROOT_TARGET ${one_toolchain})
26+
endif()
27+
28+
if(NOT CROSS_COMPILE_TARGET)
29+
message(FATAL_ERROR "Unable to find 'x86_64-zephyr-elf' or any other architecture in ${TOOLCHAIN_HOME}")
30+
endif()
31+
32+
set(CROSS_COMPILE ${one_toolchain_root}/${CROSS_COMPILE_TARGET}/bin/${CROSS_COMPILE_TARGET}-)
33+
set(SYSROOT_DIR ${one_toolchain_root}/${SYSROOT_TARGET}/${SYSROOT_TARGET})
34+
set(TOOLCHAIN_HAS_NEWLIB ON CACHE BOOL "True if toolchain supports newlib")

cmake/zephyr/host-tools.cmake

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
set(HOST_TOOLS_HOME ${ZEPHYR_SDK_INSTALL_DIR}/sysroots/x86_64-pokysdk-linux)
4+
5+
# Path used for searching by the find_*() functions, with appropriate
6+
# suffixes added. Ensures that the SDK's host tools will be found when
7+
# we call, e.g. find_program(QEMU qemu-system-x86)
8+
list(APPEND CMAKE_PREFIX_PATH ${HOST_TOOLS_HOME}/usr)
9+
10+
# TODO: Use find_* somehow for these as well?
11+
set_ifndef(QEMU_BIOS ${HOST_TOOLS_HOME}/usr/share/qemu)
12+
set_ifndef(OPENOCD_DEFAULT_PATH ${HOST_TOOLS_HOME}/usr/share/openocd/scripts)

cmake/zephyr/target.cmake

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
if(CONFIG_ARM64)
4+
set(CROSS_COMPILE_TARGET_arm aarch64-zephyr-elf)
5+
else()
6+
set(CROSS_COMPILE_TARGET_arm arm-zephyr-eabi)
7+
endif()
8+
set(CROSS_COMPILE_TARGET_nios2 nios2-zephyr-elf)
9+
set(CROSS_COMPILE_TARGET_riscv riscv64-zephyr-elf)
10+
set(CROSS_COMPILE_TARGET_mips mipsel-zephyr-elf)
11+
set(CROSS_COMPILE_TARGET_xtensa xtensa-zephyr-elf)
12+
set(CROSS_COMPILE_TARGET_arc arc-zephyr-elf)
13+
set(CROSS_COMPILE_TARGET_x86 x86_64-zephyr-elf)
14+
15+
set(CROSS_COMPILE_TARGET ${CROSS_COMPILE_TARGET_${ARCH}})
16+
set(SYSROOT_TARGET ${CROSS_COMPILE_TARGET})
17+
18+
if("${ARCH}" STREQUAL "xtensa")
19+
set(SYSROOT_DIR ${TOOLCHAIN_HOME}/xtensa/${SOC_NAME}/${SYSROOT_TARGET})
20+
set(CROSS_COMPILE ${TOOLCHAIN_HOME}/xtensa/${SOC_NAME}/${CROSS_COMPILE_TARGET}/bin/${CROSS_COMPILE_TARGET}-)
21+
else()
22+
set(SYSROOT_DIR ${TOOLCHAIN_HOME}/${SYSROOT_TARGET}/${SYSROOT_TARGET})
23+
set(CROSS_COMPILE ${TOOLCHAIN_HOME}/${CROSS_COMPILE_TARGET}/bin/${CROSS_COMPILE_TARGET}-)
24+
endif()
25+
26+
if("${ARCH}" STREQUAL "x86")
27+
if(CONFIG_X86_64)
28+
list(APPEND TOOLCHAIN_C_FLAGS -m64)
29+
list(APPEND TOOLCHAIN_LD_FLAGS -m64)
30+
else()
31+
list(APPEND TOOLCHAIN_C_FLAGS -m32)
32+
list(APPEND TOOLCHAIN_LD_FLAGS -m32)
33+
endif()
34+
endif()

go.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ for t in ${TARGETS}; do
4646
./meta-zephyr-sdk/scripts/meta-zephyr-sdk-clone.sh;
4747
./meta-zephyr-sdk/scripts/meta-zephyr-sdk-build.sh tools;
4848
mv ./meta-zephyr-sdk/scripts/toolchains/zephyr-sdk-x86_64-hosttools-standalone-0.9.sh .
49+
elif [ "${t}" = "cmake" ]; then
50+
tar -jcvf ${t}.tar.bz2 -C ${SDK_NG_HOME} cmake;
4951
fi
5052
done
5153

@@ -76,6 +78,9 @@ for t in ${TARGETS}; do
7678
if [ "${t}" = "tools" ]; then
7779
# We handled tools above, so skip it here
7880
continue
81+
elif [ "${t}" = "cmake" ]; then
82+
# We handled cmake above, so skip it here
83+
continue
7984
fi
8085
if [ ! -f ${GITDIR}/configs/${t}.config ]; then
8186
echo "Target configuration does not exist"

scripts/make_zephyr_sdk.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ setup_hdr()
6868
cat template_dir >>$setup
6969
}
7070

71+
setup_cmake()
72+
{
73+
local setup=$1
74+
75+
echo "tar -C \$target_sdk_dir -jxf ./$file_cmake > /dev/null &" >> $setup
76+
echo "spinner \$! \"Installing CMake files...\"" >> $setup
77+
echo "[ \$? -ne 0 ] && echo \"Error(s) encountered during installation.\" && exit 1" >>$setup
78+
echo "echo \"\"" >>$setup
79+
}
80+
7181
setup_arch()
7282
{
7383
local setup=$1
@@ -162,6 +172,8 @@ create_sdk()
162172
setup_arch $setup $arch
163173
done
164174

175+
setup_cmake $setup
176+
165177
setup_host $setup
166178
setup_ftr_sdk $setup
167179

@@ -183,6 +195,7 @@ parse_toolchain_name file_gcc_xtensa_nxp_imx_adsp xtensa_nxp_imx_adsp
183195
parse_toolchain_name file_gcc_xtensa_nxp_imx8m_adsp xtensa_nxp_imx8m_adsp
184196
parse_toolchain_name file_gcc_xtensa_intel_s1000 xtensa_intel_s1000
185197
parse_toolchain_name file_hosttools hosttools
198+
parse_toolchain_name file_cmake cmake
186199

187200
# Host tools are non-optional
188201
if [ -z "$file_hosttools" ]; then

scripts/template_dir

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ do_cleanup()
118118

119119
do_zephyrrc()
120120
{
121+
if [ ! $create_zephyrrc ]; then
122+
return
123+
fi
124+
121125
echo
122126
echo "You need to setup the following environment variables to use the toolchain:"
123127
echo
@@ -145,10 +149,10 @@ do_zephyrrc()
145149
}
146150

147151
# Read the input "y"
152+
# $1 is optional, but allows the caller to provide additional text.
148153
read_confirm () {
149-
echo "The directory $target_sdk_dir/sysroots will be removed! "
150154
if [ "$confirm" != "y" ]; then
151-
echo "Do you want to continue (y/n)? "
155+
echo "Do you want to continue $1(y/n)? "
152156
while read confirm; do
153157
[ "$confirm" = "Y" -o "$confirm" = "y" -o "$confirm" = "n" \
154158
-o "$confirm" = "N" ] && break
@@ -197,6 +201,38 @@ if [ -n "$(echo $target_sdk_dir|grep ' ')" ]; then
197201
exit 1
198202
fi
199203

204+
# Verify that the install forlder is among the default search paths for CMake.
205+
# If not, prompt the user before continue, and create ~/.zephyrrc
206+
eval sdk_dirname=$(dirname "$target_sdk_dir")
207+
eval sdk_basename=$(basename "$target_sdk_dir")
208+
if [ "$sdk_dirname" != "/opt" \
209+
-a "$sdk_dirname" != "/usr" \
210+
-a "$sdk_dirname" != "/usr/local" \
211+
-a "$sdk_dirname" != "$HOME" \
212+
-a "$sdk_dirname" != "$HOME/.local" \
213+
-a "$sdk_dirname" != "$HOME/.local/opt" \
214+
-a "$sdk_dirname" != "$HOME/bin" ] ||\
215+
[[ "${sdk_basename}" != "zephyr-sdk"* ]]; then
216+
echo
217+
echo "It is recommended to install Zephyr SDK at one of the following locations:"
218+
echo " ${HOME}/zephyr-sdk[-${SDK_VERSION}]"
219+
echo " ${HOME}/.local/zephyr-sdk[-${SDK_VERSION}]"
220+
echo " ${HOME}/.local/opt/zephyr-sdk[-${SDK_VERSION}]"
221+
echo " ${HOME}/bin/zephyr-sdk[-${SDK_VERSION}]"
222+
echo " /opt/zephyr-sdk[-${SDK_VERSION}]"
223+
echo " /usr/zephyr-sdk[-${SDK_VERSION}]"
224+
echo " /usr/local/zephyr-sdk[-${SDK_VERSION}]"
225+
echo
226+
227+
read_confirm "installing to ${target_sdk_dir} "
228+
if [ "$confirm" = "n" -o "$confirm" = "N" ]; then
229+
# Abort the installation
230+
echo "SDK installation aborted!"
231+
exit 1
232+
fi
233+
create_zephyrrc=true
234+
fi
235+
200236
echo "Installing SDK to $target_sdk_dir"
201237

202238
if [ -d $target_sdk_dir ]; then
@@ -207,16 +243,17 @@ if [ -d $target_sdk_dir ]; then
207243
else
208244
# wipe the directory first
209245
if [ -d $target_sdk_dir/sysroots ]; then
210-
read_confirm
211-
if [ "$confirm" = "y" -o "$confirm" = "Y" ]; then
212-
rm -rf $target_sdk_dir/sysroots/
213-
rm -rf $target_sdk_dir/info-zephyr-sdk*/
214-
rm -fr $target_sdk_dir/sdk_version
215-
else
216-
# Abort the installation
217-
echo "SDK installation aborted!"
218-
exit 1
219-
fi
246+
echo "The directory $target_sdk_dir/sysroots will be removed! "
247+
read_confirm
248+
if [ "$confirm" = "y" -o "$confirm" = "Y" ]; then
249+
rm -rf $target_sdk_dir/sysroots/
250+
rm -rf $target_sdk_dir/info-zephyr-sdk*/
251+
rm -fr $target_sdk_dir/sdk_version
252+
else
253+
# Abort the installation
254+
echo "SDK installation aborted!"
255+
exit 1
256+
fi
220257
fi
221258
fi
222259
else

0 commit comments

Comments
 (0)