Package: cmwalk
1. IntroductioncmWalkis a python script to walk the directory tree of a C/C++
project of embedded system to generate CMakeLists.txt files for building
the executable.2. Usageusage: cmwalk.py [-h] input_dirA python script to generate CMakeLists.txt of a C/C++ project - 0.0.7.positional arguments: input_dir The base directory of C/C++ project.optional arguments: -h, –help show this help message and exit3. Configuration FileYou can create a json file for each directory of project directory tree
to configure howcmwalkto generate CMakeLists.txt. The
configuration filename ofcmwalkiscmwalk.json.If there is nocmwalk.jsonin a directory of project directory tree, then all the files and
subdirectories of that directory will be parsed for CMakeLists.txt generation.3.1 Supported properties ofcmwalk.jsoncmakeToolchainFileSpecifying the toolchain file of the used compiler for current
project. You can also set the used toolchain file by invokingcmakewith the command line parameter-DCMAKE_TOOLCHAIN_FILE=path/to/file.Refer cmake documentationcmake-toolchains(7)for the details.Example:{"cmakeToolchainFile":"gcc_arm_none_eabi_toolchain.cmake"}For above example,cmwalkwill generate following command beforeprojectcommand to set the used toolchain.# set the toolchain file.
# toolchain file should be set before "project" command.
# the toolchain file can also be set via "cmake -DCMAKE_TOOLCHAIN_FILE=path/to/file".set(CMAKE_TOOLCHAIN_FILEgcc_arm_none_eabi_toolchain.cmake)An example of toolchain file:# refer https://cmake.org/Wiki/CMake_Cross_Compiling
#include(CMakeForceCompiler)    # cmake_force_c_compiler and cmake_force_cxx_compiler are deprecated.set(CMAKE_SYSTEM_NAMEGeneric)set(CMAKE_SYSTEM_VERSION1)set(CMAKE_SYSTEM_PROCESSOR"armv7-m")# refer https://cmake.org/pipermail/cmake-developers/2016-February/027871.html
# about how to solve this problem: "arm-none-eabi-gcc.exe" is not able to compile a simple test program.set(CMAKE_TRY_COMPILE_TARGET_TYPESTATIC_LIBRARY)# find the cross compiler and associated tools that we need:find_program(ARM_NONE_EABI_GCCarm-none-eabi-gcc)find_program(ARM_NONE_EABI_GPParm-none-eabi-g++)find_program(ARM_NONE_EABI_OBJCOPYarm-none-eabi-objcopy)macro(gcc_program_notfoundprogname)message("**************************************************************************\n")message(" ERROR: the arm gcc program ${progname} could not be found\n")if(CMAKE_HOST_SYSTEM_NAMESTREQUAL"Windows"ORCMAKE_HOST_SYSTEM_NAMESTREQUAL"Linux")message(" you can install the ARM GCC embedded compiler tools from:")message(" https://developer.arm.com/open-source/gnu-toolchain/gnu-rm/downloads")elseif(CMAKE_HOST_SYSTEM_NAMESTREQUAL"Darwin")message(" it is included in the arm-none-eabi-gcc package that you can install")message(" with homebrew:\n")message("   brew tap ARMmbed/homebrew-formulae")message("   brew install arm-none-eabi-gcc")endif()message("\n**************************************************************************")message(FATAL_ERROR"missing program prevents build")return()endmacro(gcc_program_notfound)if(NOTARM_NONE_EABI_GCC)gcc_program_notfound("arm-none-eabi-gcc")endif()if(NOTARM_NONE_EABI_GPP)gcc_program_notfound("arm-none-eabi-g++")endif()if(NOTARM_NONE_EABI_OBJCOPY)gcc_program_notfound("arm-none-eabi-objcopy")endif()set(CMAKE_C_COMPILERarm-none-eabi-gcc)set(CMAKE_CXX_COMPILERarm-none-eabi-g++)set(C_FAMILY_FLAGS_INIT"-ffunction-sections -fdata-sections -g -fno-common -fmessage-length=0 --specs=nosys.specs --specs=nano.specs")set(CMAKE_C_FLAGS_INIT"${C_FAMILY_FLAGS_INIT} -std=c99")set(CMAKE_CXX_FLAGS_INIT"${C_FAMILY_FLAGS_INI} -std=c++11")set(CMAKE_ASM_FLAGS_INIT"-fno-exceptions -fno-unwind-tables -x assembler-with-cpp")set(CMAKE_EXE_LINKER_FLAGS_INIT"-Wl,-gc-sections,-print-memory-usage")cmakeCompilerOptionsFileSpecifying a file that contains the additional compiler settings
to be included in the top-level CMakeLists.txt file.Example:{"cmakeCompilerOptionsFile":"gcc_arm_none_eabi_opts.cmake"}For above example,cmwalkwill generate following command afterprojectcommand to set the compiler options.# load and run the CMake code from the given file to specify project specific options.include(gcc_arm_none_eabi_opts.cmake)An example of compiler option files forGNU Arm Embedded
Toolchain:set(EXTRA_COMMON_FLAGS"-mcpu=cortex-m4 -mthumb-interwork -mfloat-abi=hard -mfpu=fpv4-sp-d16 -DUSE_HAL_DRIVER -DSTM32F429xx")set(CMAKE_C_FLAGS"${CMAKE_C_FLAGS} ${EXTRA_COMMON_FLAGS}")set(CMAKE_CXX_FLAGS"${CMAKE_CXX_FLAGS} ${EXTRA_COMMON_FLAGS}")set(CMAKE_ASM_FLAGS"${CMAKE_ASM_FLAGS} -mcpu=cortex-m4 -mthumb-interwork -mfloat-abi=hard -mfpu=fpv4-sp-d16")set(LINKER_SCRIPT${CMAKE_SOURCE_DIR}/app/STM32F429ZITx_FLASH.ld)set(CMAKE_EXE_LINKER_FLAGS"${CMAKE_EXE_LINKER_FLAGS} -Wl,-Map=${CMAKE_BINARY_DIR}/${PROJECT_NAME}.map -T${LINKER_SCRIPT}")addToCompilerIncludeDirectoriesA flag to control if current directory should be added to the include directories of compiler.The default option is true whenaddToCompilerIncludeDirectoriesdoes not exist.An example of not adding current directory to include directories of compiler.{"addToCompilerIncludeDirectories":false}sourceDirectoriesA list of source directories.IfsourceDirectoriesis specified incmwalk.json, then only
the specified directories will be included for parsing, other
directories will be excluded. IfsourceDirectoriesdoes not exist
butignoredDirectoriesexist, then all the directories except
those specified byignoredDirectorieswill be excluded.An example of specifying the source subdirectories for searching the
source files:{"sourceDirectories":["app","libs"]}ignoredDirectories- A list of ignored directories.sourceDirectoriesproperty has higher priority thanignoredDirectoriesproperty. If both ofsourceDirectoriesandignoredDirectoriesproperties
are specified incmwalk.json,ignoredDirectoriesproperty has no
effect.An example of excluding subdirectories for searching the source
files:{"ignoredDirectories":["docs"]}ignoredFiles- A list of ignored files.An example of excluding a file fromcmakebuild system:{"ignoredFiles":["cfg.h.template"]}4. Example of generated CMakeLists.txtThis is an example of generated top-levelCMakeLists.txt:cmake_minimum_required(VERSION3.9)# set the toolchain file.
# toolchain file should be set before "project" command.
# the toolchain file can also be set via "cmake -DCMAKE_TOOLCHAIN_FILE=path/to/file".set(CMAKE_TOOLCHAIN_FILEgcc_arm_none_eabi_toolchain.cmake)project(nucleo_f429zi_freertos_lwip)enable_language(CCXXASM)# load and run the CMake code from the given file to specify project specific options.include(gcc_arm_none_eabi_opts.cmake)# export the executable target through a variable to CMakeLists.txt files in subdirectories.
# update the dependent sources.add_executable(nucleo_f429zi_freertos_lwip.elf"")if(${LINK_SCRIPT})set_target_properties(nucleo_f429zi_freertos_lwip.elfPROPERTIESLINK_DEPENDS${LINKER_SCRIPT})endif()# export the name of executable target via a variable to CMakeLists.txt files in subdirectories.set(CURRENT_EXE_NAME${PROJECT_NAME}.elf)# load and run the CMake code from subdirectories for current target.include(app/CMakeLists.txt)include(libs/CMakeLists.txt)# if compiler is GNU gcc/g++, then generate *.bin & *.hex files.if("${CMAKE_C_COMPILER_ID}"STREQUAL"GNU")# generate the hex file from the built target.set(HEX_FILE${PROJECT_NAME}.hex)add_custom_command(TARGET${PROJECT_NAME}.elfPOST_BUILDCOMMAND${CMAKE_OBJCOPY}-Oihex$<TARGET_FILE:${PROJECT_NAME}.elf>${HEX_FILE}COMMENT"Building ${HEX_FILE}...")# generate the bin file from the built target.set(BIN_FILE${PROJECT_NAME}.bin)add_custom_command(TARGET${PROJECT_NAME}.elfPOST_BUILDCOMMAND${CMAKE_OBJCOPY}-Obinary$<TARGET_FILE:${PROJECT_NAME}.elf>${BIN_FILE}COMMENT"Building ${BIN_FILE}...")endif()5. ReferencesEnhanced source file handling with target_sources() –
CrascitCLion for embedded development | CLion
Blog