已经大半个月没有更新文章了,大家还记得之前作者写的一篇关于RT-Thread的微内核移植的文章吗?如果不记得没关系。看如下是文章链接。
《如何移植RTT微内核到树莓派3B》
目前RTT微内核是RTT提供的体验版本。它采用了scons构建。作者也是刚接触scons,不是很了解,在这不过多的说明。那么来谈谈为什么不使用scons,采用cmake编译。不是因为scons不好,而是之前作者在写关于cmake的文章中,熊大(RTT的创始人)看到,然后在交流中,熊大说可以采用cmake进行编译。所以我也是冒着尝试的想法,开始了使用cmake去构建微内核。下面几篇文章是关于CMake理论的文章。
《CMake,大型项目采用的构建工具》
《CMake 静态库与动态库构建》
《CMake 教你如何链接共享库》
在构建过程中,由于不是很熟悉整个构建流程,在熊大亲自指导下,让我清晰很多。非常感谢熊大的支持。
其实整个过程最麻烦的编译选项的问题,特别是最后链接部分,这也是我花费最长去研究的。接下来,说一说微内核初步版本(只单独编译一个application)。
整个CMakeLists.txt文件如下,下面对每一行进行说明:
cmake_minimum_required(VERSION 3.10)
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(CONFIG_PREFIX "/opt/gcc-arm-none-eabi/bin/arm-none-eabi-")
set(CMAKE_C_COMPILER "${CONFIG_PREFIX}gcc")
set(CMAKE_CXX_COMPILER "${CONFIG_PREFIX}g++")
set(CMAKE_ASM_COMPILER "${CONFIG_PREFIX}gcc")
set(CMAKE_OBJCOPY "${CONFIG_PREFIX}objcopy")
set(CMAKE_C_AR "${CONFIG_PREFIX}ar")
set(CMAKE_SIZE "${CONFIG_PREFIX}size")
#Disable compiler checks
set(CMAKE_C_COMPILER_FORCED TRUE)
set(CMAKE_CXX_COMPILER_FORCED TRUE)
set(microkernel_sdk_dir "${CMAKE_SOURCE_DIR}/sdk")
set(microkernel_apps_dir "${CMAKE_SOURCE_DIR}/apps")
project(rtt_microkernel)
enable_language(C ASM)
add_definitions(
"-D__ASSEMBLY__"
"-DRT_USING_NEWLIB"
"-DHAVE_CCONFIG_H"
"-D__STDC_ISO_10646__=201206L"
"-D_STDC_PREDEF_H"
)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=armv7-a -marm -msoft-float -Werror -Wall -O0 -g -gdwarf-2 -n -pie -fpie -fPIC -fpic --static -nostdinc")
set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -march=armv7-a -marm -msoft-float -x assembler-with-cpp -O0 -g -fPIC")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=armv7-a -marm -msoft-float -Werror -Wall -Woverloaded-virtual -fno-exceptions -fno-rtti -O0 -g -gdwarf-2 -n -pie -fpie -fPIC -fpic --static -nostdinc")
set(CMAKE_EXE_LINKER_FLAGS "-march=armv7-a -marm -msoft-float -nostartfiles -nostdlib -n -pie -fpie -fPIC -fpic -Wl,-pie")
set(microkernel_sdk_inc
"${CMAKE_SOURCE_DIR}"
"${microkernel_sdk_dir}/crt/rt-thread/components/dfs"
"${microkernel_sdk_dir}/crt/rt-thread/components/drivers"
"${microkernel_sdk_dir}/crt/rt-thread/components/net"
"${microkernel_sdk_dir}/crt/rt-thread/components/net/netdev"
"${microkernel_sdk_dir}/crt/rt-thread/components/lwip-2.1.0"
"${microkernel_sdk_dir}/crt/rt-thread/include"
"${microkernel_sdk_dir}/crt/rt-thread/include/libc"
"${microkernel_sdk_dir}/crt/gcc"
"${microkernel_sdk_dir}/libc/include"
)
set(microkernel_apps_inc
"${microkernel_apps_dir}/hello"
)
set(microkernel_sdk_src
"${microkernel_sdk_dir}/crt/cplusplus/cxx_crt.cpp"
"${microkernel_sdk_dir}/crt/gcc/arch/arm/syscall.S"
"${microkernel_sdk_dir}/crt/rt-thread/src/rt_channel.c"
"${microkernel_sdk_dir}/crt/rt-thread/src/rt_completion.c"
"${microkernel_sdk_dir}/crt/rt-thread/src/rt_device.c"
"${microkernel_sdk_dir}/crt/rt-thread/src/rt_ipc.c"
"${microkernel_sdk_dir}/crt/rt-thread/src/rt_net.c"
"${microkernel_sdk_dir}/crt/rt-thread/src/rt_syscall.c"
"${microkernel_sdk_dir}/crt/rt-thread/src/rt_thread.c"
"${microkernel_sdk_dir}/crt/rt-thread/src/rt_timer.c"
"${microkernel_sdk_dir}/crt/rt-thread/src/stub.c"
)
set(microkernel_apps_src
"${microkernel_apps_dir}/hello/main.c"
)
set(CMAKE_EXECUTABLE_SUFFIX ".elf")
add_executable(${PROJECT_NAME} ${microkernel_apps_src} ${microkernel_sdk_src})
target_include_directories(${PROJECT_NAME} PRIVATE ${microkernel_sdk_inc} ${microkernel_apps_inc})
target_link_libraries(${PROJECT_NAME}
PRIVATE
-Wl,--start-group
"${CMAKE_SOURCE_DIR}/sdk/libc/lib/libc.a"
"${CMAKE_SOURCE_DIR}/sdk/libc/lib/libm.a"
"${CMAKE_SOURCE_DIR}/sdk/libc/lib/libcrt.a"
"libgcc.a"
-Wl,--end-group
-T ${microkernel_sdk_dir}/crt/gcc/arch/arm/gcc_arm.ld
)
1.检查cmake的版本
cmake_minimum_required(VERSION 3.10)
2.其中
CMAKE_SYSTEM_NAME:这个变量被设置,cmake才认为采用交叉编译,CMAKE_SYSTEM_NAME即目标机target所在的操作系统名称,比如ARM或者Linux你就需要写”Linux”,如果Android平台你就写”Android”,如果你的嵌入式平台没有相关OS你即需要写成”Generic”。
CMAKE_SYSTEM_PROCESSOR:代表目标系统的硬件或者CPU的型号,例如ARM、X86。
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR arm)
3.关闭编译检测
set(CMAKE_C_COMPILER_FORCED TRUE)
set(CMAKE_CXX_COMPILER_FORCED TRUE)
4.定义sdk和apps两个目录变量。
set(microkernel_sdk_dir "${CMAKE_SOURCE_DIR}/sdk")
set(microkernel_apps_dir "${CMAKE_SOURCE_DIR}/apps")
5.定义项目的名字。
project(rtt_microkernel)
6.使能可以支持的语言,这里使能C语言和汇编语言。如果不是能,则相关文件不会进行编译。
enable_language(C ASM)
7.添加宏,即打开源文件的某项宏定义
add_definitions(
"-D__ASSEMBLY__"
"-DRT_USING_NEWLIB"
"-DHAVE_CCONFIG_H"
"-D__STDC_ISO_10646__=201206L"
"-D_STDC_PREDEF_H"
)
8.添加编译选项,由于不同源文件添加的编译选项的不同,所以需要分别添加(分别是:C,汇编,C++, 链接),分别如下:
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=armv7-a -marm -msoft-float -Werror -Wall -O0 -g -gdwarf-2 -n -pie -fpie -fPIC -fpic --static -nostdinc")
set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -march=armv7-a -marm -msoft-float -x assembler-with-cpp -O0 -g -fPIC")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=armv7-a -marm -msoft-float -Werror -Wall -Woverloaded-virtual -fno-exceptions -fno-rtti -O0 -g -gdwarf-2 -n -pie -fpie -fPIC -fpic --static -nostdinc")
set(CMAKE_EXE_LINKER_FLAGS "-march=armv7-a -marm -msoft-float -nostartfiles -nostdlib -n -pie -fpie -fPIC -fpic -Wl,-pie")
如何看我们添加的编译选项是否正确。我们编译采用外部编译,在build目录执行(cmake ..),然后查看flags.make文件。
flags.make文件的内容如下(包含相关语言的编译选项,宏定义,相关头文件路径):我们通过这个文件查看是否正确:
9.定义两个头文件变量,分别是sdk头文件目录和apps头文件目录:
set(microkernel_sdk_inc
"${CMAKE_SOURCE_DIR}"
"${microkernel_sdk_dir}/crt/rt-thread/components/dfs"
"${microkernel_sdk_dir}/crt/rt-thread/components/drivers"
"${microkernel_sdk_dir}/crt/rt-thread/components/net"
"${microkernel_sdk_dir}/crt/rt-thread/components/net/netdev"
"${microkernel_sdk_dir}/crt/rt-thread/components/lwip-2.1.0"
"${microkernel_sdk_dir}/crt/rt-thread/include"
"${microkernel_sdk_dir}/crt/rt-thread/include/libc"
"${microkernel_sdk_dir}/crt/gcc"
"${microkernel_sdk_dir}/libc/include"
)
set(microkernel_apps_inc
"${microkernel_apps_dir}/hello"
)
10.定义两个源文件变量,分别是sdk源文件和apps源文件:
set(microkernel_sdk_src
"${microkernel_sdk_dir}/crt/cplusplus/cxx_crt.cpp"
"${microkernel_sdk_dir}/crt/gcc/arch/arm/syscall.S"
"${microkernel_sdk_dir}/crt/rt-thread/src/rt_channel.c"
"${microkernel_sdk_dir}/crt/rt-thread/src/rt_completion.c"
"${microkernel_sdk_dir}/crt/rt-thread/src/rt_device.c"
"${microkernel_sdk_dir}/crt/rt-thread/src/rt_ipc.c"
"${microkernel_sdk_dir}/crt/rt-thread/src/rt_net.c"
"${microkernel_sdk_dir}/crt/rt-thread/src/rt_syscall.c"
"${microkernel_sdk_dir}/crt/rt-thread/src/rt_thread.c"
"${microkernel_sdk_dir}/crt/rt-thread/src/rt_timer.c"
"${microkernel_sdk_dir}/crt/rt-thread/src/stub.c"
)
set(microkernel_apps_src
"${microkernel_apps_dir}/hello/main.c"
)
11.定义可执行文件的后缀。
set(CMAKE_EXECUTABLE_SUFFIX ".elf")
12.根据源文件,添加一个可执行文件。
add_executable(${PROJECT_NAME} ${microkernel_apps_src} ${microkernel_sdk_src})
13.添加目标所依赖的头文件目录
target_include_directories(${PROJECT_NAME} PRIVATE ${microkernel_sdk_inc} ${microkernel_apps_inc})
14.添加目标依赖的链接库。以及链接脚本。
target_link_libraries(${PROJECT_NAME}
PRIVATE
-Wl,--start-group
"${CMAKE_SOURCE_DIR}/sdk/libc/lib/libc.a"
"${CMAKE_SOURCE_DIR}/sdk/libc/lib/libm.a"
"${CMAKE_SOURCE_DIR}/sdk/libc/lib/libcrt.a"
"libgcc.a"
-Wl,--end-group
-T ${microkernel_sdk_dir}/crt/gcc/arch/arm/gcc_arm.ld
)
15.最后链接生成可执行文件的命令是否正确,我们可以查看link.txt文件。
link.txt文件内容如下:
编译测试:
1.测试应用如下:
2.生成可执行文件:rtt_microkernel.elf.
3.运行验证(烧录到树莓派上进行验证):