start arm stuff, better keyboard folder path management

This commit is contained in:
Jack Humbert 2023-04-02 16:38:37 -04:00
parent 2031d063b5
commit 4aea69ba21
11 changed files with 351 additions and 32 deletions

View File

@ -1,20 +1,25 @@
cmake_minimum_required(VERSION 3.3)
cmake_minimum_required(VERSION 3.20)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
include(UpdateSubmodule)
# early declaration of macro defined in toolchain
macro(add_qmk_executable _)
endmacro(add_qmk_executable)
if(NOT DEFINED QMK_KEYBOARD)
# configure step - populate targets
project(qmk_firmware)
add_subdirectory(keyboards)
else()
# build step
set(QMK_KEYBOARD_CURRENT_FOLDER ${QMK_KEYBOARD_FOLDER})
set(QMK_KEYBOARD_FOLDERS)
set(QMK_KEYBOARD_FOLDER_PARTS)
@ -72,7 +77,7 @@ endif()
# list(GET QMK_KEYBOARD_FOLDERS 0 QMK_KEYBOARD_BASE_FOLDER)
list(GET QMK_KEYBOARD_KEYMAPS 0 QMK_KEYBOARD_KEYMAP)
list(GET QMK_KEYBOARD_HEADERS 0 QMK_KEYBOARD_HEADER)
list(GET QMK_KEYBOARD_HEADERS -1 QMK_KEYBOARD_HEADER)
project(${QMK_KEYBOARD}
LANGUAGES C CXX ASM

View File

@ -1,12 +1,27 @@
macro(add_keyboard KEYBOARD_CMAKE)
get_filename_component(KEYBOARD_FULL_DIRECTORY "${KEYBOARD_CMAKE}" DIRECTORY)
macro(add_keyboard KEYBOARD_FOLDER)
# not entirely sure why this is necessary
set(TEMP_PATH ${KEYBOARD_FOLDER})
cmake_path(IS_RELATIVE TEMP_PATH IS_FOLDER_RELATIVE)
if(${IS_FOLDER_RELATIVE})
set(KEYBOARD_FOLDER_ABS ${CMAKE_SOURCE_DIR}/keyboards/${KEYBOARD_FOLDER})
if(NOT EXISTS ${KEYBOARD_FOLDER_ABS})
message(FATAL_ERROR "Keyboard does not exist in QMK - try using an absolute path to the keyboard folder")
endif()
else()
set(KEYBOARD_FOLDER_ABS ${KEYBOARD_FOLDER})
if(NOT EXISTS ${KEYBOARD_FOLDER_ABS})
message(FATAL_ERROR "Absolute path to keyboard does not exist")
endif()
endif()
# find the right toolchain
file(READ ${KEYBOARD_FULL_DIRECTORY}/info.json JSON_STRING)
message(STATUS "Reading config from ${KEYBOARD_FOLDER_ABS}/info.json")
file(READ ${KEYBOARD_FOLDER_ABS}/info.json JSON_STRING)
string(JSON PROCESSOR GET ${JSON_STRING} processor)
if(${PROCESSOR} MATCHES "^at.*")
set(PLATFORM "avr")
# find_package(avr-gcc)
include(FindAVRToolchain)
find_avr_toolchain()
elseif(
${PROCESSOR} MATCHES "^STM.*" OR
${PROCESSOR} MATCHES "^WB32.*" OR
@ -14,16 +29,20 @@ macro(add_keyboard KEYBOARD_CMAKE)
${PROCESSOR} MATCHES "RP2040" OR
${PROCESSOR} MATCHES "^GD32.*")
set(PLATFORM "chibios")
include(FindARMToolchain)
find_arm_toolchain()
endif()
if(NOT DEFINED PLATFORM)
message(FATAL_ERROR "Could not find platform for ${KEYBOARD_FULL_DIRECTORY}")
message(FATAL_ERROR "Could not find platform for ${KEYBOARD_FOLDER}")
endif()
include(FindToolChain)
find_toolchain()
if(${IS_FOLDER_RELATIVE})
string(REPLACE "/" "_" KEYBOARD_NAME ${KEYBOARD_FOLDER})
else()
string(JSON KEYBOARD_NAME GET ${JSON_STRING} keyboard_name)
string(REPLACE " " "_" KEYBOARD_NAME ${KEYBOARD_NAME})
endif()
file(RELATIVE_PATH KEYBOARD_FOLDER "${CMAKE_SOURCE_DIR}/keyboards" "${KEYBOARD_FULL_DIRECTORY}")
string(REPLACE "/" "." KEYBOARD_NAME ${KEYBOARD_FOLDER})
ExternalProject_Add(${KEYBOARD_NAME}
SOURCE_DIR ${CMAKE_SOURCE_DIR}
PREFIX ${CMAKE_SOURCE_DIR}/build/keyboards/${KEYBOARD_FOLDER}

View File

@ -0,0 +1,63 @@
macro(find_gcc TRIPLE)
if(UNIX)
set(OS_SUFFIX "")
find_path(ARM_TOOLCHAIN_ROOT
NAMES
${TRIPLE}-gcc${OS_SUFFIX}
PATHS
"${CMAKE_SOURCE_DIR}/toolchains/avr-gcc/avr-gcc-12.1.0-x64-linux/bin/"
/usr/bin/
/usr/local/bin
/bin/
$ENV{AVR_ROOT}
)
find_path(MAKE_ROOT
NAMES
make${OS_SUFFIX}
PATHS
"${CMAKE_SOURCE_DIR}/toolchains/avr-gcc-12.1.0-x64-linux/bin/"
/usr/bin/
/usr/local/bin
/bin/
$ENV{AVR_ROOT}
)
elseif(WIN32)
set(OS_SUFFIX ".exe")
find_path(ARM_TOOLCHAIN_ROOT
NAMES
${TRIPLE}-gcc${OS_SUFFIX}
PATHS
"${CMAKE_SOURCE_DIR}/toolchains/avr-gcc-12.1.0-x64-windows/bin"
"${CMAKE_SOURCE_DIR}/toolchains/gcc-arm-none-eabi-10.3-2021.10/bin/"
"C:/Users/Jack/Downloads/avr-gcc-12.1.0-x64-windows/bin/"
$ENV{AVR_ROOT}
)
find_path(MAKE_ROOT
NAMES
make${OS_SUFFIX}
PATHS
"${CMAKE_SOURCE_DIR}/toolchains/avr-gcc-12.1.0-x64-windows/bin/"
"C:/Users/Jack/Downloads/avr-gcc-12.1.0-x64-windows/bin/"
$ENV{AVR_ROOT}
)
else(UNIX)
message(FATAL_ERROR "toolchain not supported on this OS")
endif(UNIX)
endmacro(find_gcc)
macro(find_arm_toolchain)
find_gcc(arm-none-eabi)
if(NOT ARM_TOOLCHAIN_ROOT)
include(GetARMToolchain)
find_gcc(arm-none-eabi)
endif()
if(NOT ARM_TOOLCHAIN_ROOT)
message(FATAL_ERROR "ARM Toolchain could not be found")
endif()
set(TOOLCHAIN_ROOT ${ARM_TOOLCHAIN_ROOT})
message("ARM toolchain found: ${ARM_TOOLCHAIN_ROOT}")
message("Found make: ${MAKE_ROOT}")
endmacro()

View File

@ -0,0 +1,62 @@
macro(find_gcc TRIPLE)
if(UNIX)
set(OS_SUFFIX "")
find_path(AVR_TOOLCHAIN_ROOT
NAMES
${TRIPLE}-gcc${OS_SUFFIX}
PATHS
"${CMAKE_SOURCE_DIR}/toolchains/avr-gcc/avr-gcc-12.1.0-x64-linux/bin/"
/usr/bin/
/usr/local/bin
/bin/
$ENV{AVR_ROOT}
)
find_path(MAKE_ROOT
NAMES
make${OS_SUFFIX}
PATHS
"${CMAKE_SOURCE_DIR}/toolchains/avr-gcc-12.1.0-x64-linux/bin/"
/usr/bin/
/usr/local/bin
/bin/
$ENV{AVR_ROOT}
)
elseif(WIN32)
set(OS_SUFFIX ".exe")
find_path(AVR_TOOLCHAIN_ROOT
NAMES
${TRIPLE}-gcc${OS_SUFFIX}
PATHS
"${CMAKE_SOURCE_DIR}/toolchains/avr-gcc-12.1.0-x64-windows/bin"
"C:/Users/Jack/Downloads/avr-gcc-12.1.0-x64-windows/bin/"
$ENV{AVR_ROOT}
)
find_path(MAKE_ROOT
NAMES
make${OS_SUFFIX}
PATHS
"${CMAKE_SOURCE_DIR}/toolchains/avr-gcc-12.1.0-x64-windows/bin/"
"C:/Users/Jack/Downloads/avr-gcc-12.1.0-x64-windows/bin/"
$ENV{AVR_ROOT}
)
else(UNIX)
message(FATAL_ERROR "toolchain not supported on this OS")
endif(UNIX)
endmacro(find_gcc)
macro(find_avr_toolchain)
find_gcc(avr)
if(NOT AVR_TOOLCHAIN_ROOT)
include(GetAVRToolchain)
find_gcc(avr)
endif()
if(NOT AVR_TOOLCHAIN_ROOT)
message(FATAL_ERROR "AVR Toolchain could not be found")
endif()
set(TOOLCHAIN_ROOT ${AVR_TOOLCHAIN_ROOT})
message("AVR toolchain found: ${AVR_TOOLCHAIN_ROOT}")
message("Found make: ${MAKE_ROOT}")
endmacro()

View File

@ -5,7 +5,7 @@ if(UNIX)
NAMES
${TRIPLE}-gcc${OS_SUFFIX}
PATHS
"${CMAKE_SOURCE_DIR}/toolchains/avr-gcc/avr-gcc-12.1.0-x64-linux/bin/"
"${CMAKE_SOURCE_DIR}/toolchains/avr-gcc/avr-gcc-12.1.0-x64-linux/bin/"
/usr/bin/
/usr/local/bin
/bin/
@ -15,7 +15,7 @@ if(UNIX)
NAMES
make${OS_SUFFIX}
PATHS
"${CMAKE_SOURCE_DIR}/toolchains/avr-gcc/avr-gcc-12.1.0-x64-linux/bin/"
"${CMAKE_SOURCE_DIR}/toolchains/avr-gcc-12.1.0-x64-linux/bin/"
/usr/bin/
/usr/local/bin
/bin/
@ -27,7 +27,8 @@ elseif(WIN32)
NAMES
${TRIPLE}-gcc${OS_SUFFIX}
PATHS
"${CMAKE_SOURCE_DIR}/toolchains/avr-gcc/avr-gcc-12.1.0-x64-windows/bin/"
"${CMAKE_SOURCE_DIR}/toolchains/avr-gcc-12.1.0-x64-windows/bin"
"${CMAKE_SOURCE_DIR}/toolchains/gcc-arm-none-eabi-10.3-2021.10/bin/"
"C:/Users/Jack/Downloads/avr-gcc-12.1.0-x64-windows/bin/"
$ENV{AVR_ROOT}
)
@ -35,7 +36,7 @@ elseif(WIN32)
NAMES
make${OS_SUFFIX}
PATHS
"${CMAKE_SOURCE_DIR}/toolchains/avr-gcc/avr-gcc-12.1.0-x64-windows/bin/"
"${CMAKE_SOURCE_DIR}/toolchains/avr-gcc-12.1.0-x64-windows/bin/"
"C:/Users/Jack/Downloads/avr-gcc-12.1.0-x64-windows/bin/"
$ENV{AVR_ROOT}
)
@ -44,19 +45,22 @@ else(UNIX)
endif(UNIX)
endmacro(find_gcc)
macro(find_toolchain)
find_gcc(avr)
macro(find_toolchain TRIPLE)
find_gcc(${TRIPLE})
if(NOT TOOLCHAIN_ROOT)
include(GetAVR-GCC)
find_gcc(avr)
endif(NOT TOOLCHAIN_ROOT)
if(NOT TOOLCHAIN_ROOT)
if("${TRIPLE}" STREQUAL "avr")
include(GetAVRToolchain)
else()
include(GetARMToolchain)
endif()
find_gcc(${TRIPLE})
endif()
if(NOT TOOLCHAIN_ROOT)
message(FATAL_ERROR "Toolchain root could not be found!!!")
endif(NOT TOOLCHAIN_ROOT)
message("Found toolchain: ${TOOLCHAIN_ROOT}")
message("Found make: ${MAKE_ROOT}")
if(NOT TOOLCHAIN_ROOT)
message(FATAL_ERROR "Toolchain could not be found")
endif()
message("Found toolchain for ${TRIPLE}: ${TOOLCHAIN_ROOT}")
message("Found make: ${MAKE_ROOT}")
endmacro()

View File

@ -0,0 +1,24 @@
message("Downloading gcc-arm-none-eabi")
if(WIN32)
file(DOWNLOAD
https://developer.arm.com/-/media/Files/downloads/gnu-rm/10.3-2021.10/gcc-arm-none-eabi-10.3-2021.10-win32.zip?rev=8f4a92e2ec2040f89912f372a55d8cf3&hash=8A9EAF77EF1957B779C59EADDBF2DAC118170BBF
${CMAKE_SOURCE_DIR}/toolchains/downloads/gcc-arm-none-eabi-win32.zip
EXPECTED_HASH MD5=2bc8f0c4c4659f8259c8176223eeafc1
SHOW_PROGRESS
)
file(ARCHIVE_EXTRACT
INPUT ${CMAKE_SOURCE_DIR}/toolchains/downloads/gcc-arm-none-eabi-win32.zip
DESTINATION ${CMAKE_SOURCE_DIR}/toolchains/
)
elseif(UNIX)
file(DOWNLOAD
https://developer.arm.com/-/media/Files/downloads/gnu-rm/10.3-2021.10/gcc-arm-none-eabi-10.3-2021.10-x86_64-linux.tar.bz2?rev=78196d3461ba4c9089a67b5f33edf82a&hash=D484B37FF37D6FC3597EBE2877FB666A41D5253B
${CMAKE_SOURCE_DIR}/toolchains/downloads/gcc-arm-none-eabi-x86_64-linux.tar.bz2
EXPECTED_HASH MD5=3fe3d8bb693bd0a6e4615b6569443d0d
SHOW_PROGRESS
)
file(ARCHIVE_EXTRACT
INPUT ${CMAKE_SOURCE_DIR}/toolchains/downloads/gcc-arm-none-eabi-x86_64-linux.tar.bz2
DESTINATION ${CMAKE_SOURCE_DIR}/toolchains/
)
endif()

View File

@ -4,7 +4,7 @@ file(DOWNLOAD
https://github.com/ZakKemble/avr-gcc-build/releases/download/v12.1.0-1/avr-gcc-12.1.0-x64-windows.zip
${CMAKE_SOURCE_DIR}/toolchains/downloads/avr-gcc-x64-windows.zip
EXPECTED_HASH SHA256=e921a964fdeaedbe963352d0f26c6520a0a3eb8effc6ff232f3824b06c4ea0e2
SHOW_PROGRESS TRUE
SHOW_PROGRESS
)
file(ARCHIVE_EXTRACT
INPUT ${CMAKE_SOURCE_DIR}/toolchains/downloads/avr-gcc-x64-windows.zip
@ -15,7 +15,7 @@ file(DOWNLOAD
https://github.com/ZakKemble/avr-gcc-build/releases/download/v12.1.0-1/avr-gcc-12.1.0-x64-linux.tar.bz2
${CMAKE_SOURCE_DIR}/toolchains/downloads/avr-gcc-x64-linux.tar.bz2
EXPECTED_HASH SHA256=feb034f4b85237032da8bac1f03765af5ebc4a8939b69bed57ff31bc482ca1a6
SHOW_PROGRESS TRUE
SHOW_PROGRESS
)
file(ARCHIVE_EXTRACT
INPUT ${CMAKE_SOURCE_DIR}/toolchains/downloads/avr-gcc-x64-linux.tar.bz2

View File

@ -2,11 +2,13 @@ include(ExternalProject)
include(AddKeyboard)
if(DEFINED QMK_KEYBOARD_FOLDER)
add_keyboard(${CMAKE_SOURCE_DIR}/keyboards/${QMK_KEYBOARD_FOLDER}/CMakeFiles.txt)
add_keyboard(${QMK_KEYBOARD_FOLDER})
endif()
file(GLOB_RECURSE KEYBOARDS **/CMakeLists.txt)
foreach(KEYBOARD_CMAKE ${KEYBOARDS})
add_keyboard(${KEYBOARD_CMAKE})
get_filename_component(KEYBOARD_FOLDER_ABS "${KEYBOARD_CMAKE}" DIRECTORY)
file(RELATIVE_PATH KEYBOARD_FOLDER "${CMAKE_SOURCE_DIR}/keyboards" "${KEYBOARD_FOLDER_ABS}")
add_keyboard(${KEYBOARD_FOLDER})
endforeach()

View File

@ -0,0 +1 @@
add_qmk_executable(planck.rev6 rev6.c planck.c)

View File

@ -11,7 +11,8 @@ if(UNIX)
elseif(WIN32)
set(OS_SUFFIX ".exe")
endif()
# include(FindAVRToolchain)
# find_avr_toolchain()
# setup the AVR compiler variables

View File

@ -0,0 +1,138 @@
set(TRIPLE "arm-none-eabi")
if(UNIX)
set(OS_SUFFIX "")
elseif(WIN32)
set(OS_SUFFIX ".exe")
endif()
# include(FindARMToolchain)
# find_arm_toolchain()
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR avr)
set(CMAKE_CROSS_COMPILING 1)
set(CMAKE_MAKE_PROGRAM "${MAKE_ROOT}/make${OS_SUFFIX}" CACHE PATH "make" FORCE)
set(CMAKE_C_COMPILER "${TOOLCHAIN_ROOT}/${TRIPLE}-gcc${OS_SUFFIX}" CACHE PATH "gcc" FORCE)
set(CMAKE_CXX_COMPILER "${TOOLCHAIN_ROOT}/${TRIPLE}-g++${OS_SUFFIX}" CACHE PATH "g++" FORCE)
set(CMAKE_AR "${TOOLCHAIN_ROOT}/${TRIPLE}-ar${OS_SUFFIX}" CACHE PATH "ar" FORCE)
set(CMAKE_AS "${TOOLCHAIN_ROOT}/${TRIPLE}-as${OS_SUFFIX}" CACHE PATH "as" FORCE)
set(CMAKE_LINKER "${TOOLCHAIN_ROOT}/${TRIPLE}-ld${OS_SUFFIX}" CACHE PATH "linker" FORCE)
set(CMAKE_NM "${TOOLCHAIN_ROOT}/${TRIPLE}-nm${OS_SUFFIX}" CACHE PATH "nm" FORCE)
set(CMAKE_OBJCOPY "${TOOLCHAIN_ROOT}/${TRIPLE}-objcopy${OS_SUFFIX}" CACHE PATH "objcopy" FORCE)
set(CMAKE_OBJDUMP "${TOOLCHAIN_ROOT}/${TRIPLE}-objdump${OS_SUFFIX}" CACHE PATH "objdump" FORCE)
set(CMAKE_STRIP "${TOOLCHAIN_ROOT}/${TRIPLE}-strip${OS_SUFFIX}" CACHE PATH "strip" FORCE)
set(CMAKE_RANLIB "${TOOLCHAIN_ROOT}/${TRIPLE}-ranlib${OS_SUFFIX}" CACHE PATH "ranlib" FORCE)
set(AVR_SIZE "${TOOLCHAIN_ROOT}/${TRIPLE}-size${OS_SUFFIX}" CACHE PATH "size" FORCE)
add_compile_options(
$<$<COMPILE_LANGUAGE:C>:-std=gnu11>
$<$<COMPILE_LANGUAGE:CXX>:-std=gnu++14>
# -flto
-Os
-Wall
-Wstrict-prototypes
-fcommon
# -g
-funsigned-char
-funsigned-bitfields
-ffunction-sections
-fdata-sections
-fpack-struct
-fshort-enums
-fno-builtin-printf
$<$<COMPILE_LANGUAGE:C>:-fno-inline-small-functions>
$<$<COMPILE_LANGUAGE:C>:-fno-strict-aliasing>
$<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions>
)
add_compile_definitions(
F_CPU=16000000
F_USB=16000000UL
__AVR_ATmega32U4__
# LTO_ENABLE
)
# include_directories("C:/Users/Jack/Downloads/avr-gcc-12.1.0-x64-windows/avr/include")
macro(add_qmk_executable target_name)
set(elf_file ${target_name}-${QMK_MCU}.elf)
set(map_file ${target_name}-${QMK_MCU}.map)
set(hex_file ${target_name}-${QMK_MCU}.hex)
set(lst_file ${target_name}-${QMK_MCU}.lst)
add_link_options(-Wl,--gc-sections,-Map=${map_file})
# create elf file
add_executable(${elf_file}
${ARGN}
)
target_link_libraries(${elf_file}
PUBLIC
quantum
tmk_core_protocol
platforms
)
# set_target_properties(
# ${elf_file}
# PROPERTIES
# COMPILE_FLAGS "-mmcu=${QMK_MCU} -g -w -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics"
# LINK_FLAGS "-mmcu=${QMK_MCU} -Wl,-Map,${map_file}"
# )
# generate the lst file
add_custom_command(
OUTPUT ${lst_file}
COMMAND
${CMAKE_OBJDUMP} -h -S ${elf_file} > ${lst_file}
DEPENDS ${elf_file}
)
# create hex file
add_custom_command(
OUTPUT ${hex_file}
COMMAND
${CMAKE_OBJCOPY} -j .text -j .data -O ihex ${elf_file} ${hex_file}
DEPENDS ${elf_file}
)
add_custom_command(
OUTPUT "print-size-${elf_file}"
COMMAND
${AVR_SIZE} ${elf_file}
DEPENDS ${elf_file}
)
add_custom_command(
OUTPUT "print-size-${hex_file}"
COMMAND
${AVR_SIZE} ${hex_file}
DEPENDS ${hex_file}
)
# build the intel hex file for the device
add_custom_target(
${target_name}
ALL
DEPENDS ${hex_file} ${lst_file} "print-size-${elf_file}" "print-size-${hex_file}"
)
set_target_properties(
${target_name}
PROPERTIES
OUTPUT_NAME ${elf_file}
)
endmacro(add_qmk_executable)