ʹÓÃCMake¹¹½¨LinuxÄں˵ÄÉèÖÃÖ¸ÄÏ
ʹÓÃcmake¹¹½¨linuxÄں˵ÄÉèÖÃÖ¸ÄÏ
¸ÅÊö
ÔÚLinux¿ª·¢ÖУ¬¹¹½¨ºÍÉèÖÃÄÚºËÊÇÒ»¸öÖ÷ÒªµÄ»·½Ú¡£¹ØÓÚ´ó´ó¶¼ÈËÀ´Ëµ£¬Ê¹ÓÃKconfigºÍMakefileÊÇ×î³£¼ûµÄÉèÖ÷½·¨¡£È»¶ø£¬Ê¹ÓÃCMakeÀ´¹¹½¨ºÍÉèÖÃLinuxÄÚºËÒ²ÊÇÒ»¸öÎÞаÇÒÇ¿Ê¢µÄÑ¡Ôñ¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃCMakeÀ´¹¹½¨ºÍÉèÖÃLinuxÄںˣ¬²¢¸½ÉÏһЩ´úÂëʾÀý¡£
×°ÖÃCMake
Ê×ÏÈ£¬ÎÒÃÇÐèҪװÖÃCMake¡£ÔÚUbuntuÉÏ£¬¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁî¾ÙÐÐ×°Öãº
$ sudo apt-get install cmake
µÇ¼ºó¸´ÖÆ
¹ØÓÚÆäËûLinux¿¯Ðа棬¿ÉÒÔͨ¹ýÏìÓ¦µÄ°ü¹ÜÀíÆ÷À´×°Öá£
½¨ÉèCMakeLists.txtÎļþ
ÔÚLinuxÄÚºËÔ´ÂëĿ¼Ï½¨ÉèÒ»¸öCMakeLists.txtÎļþ¡£Õâ¸öÎļþ½«°üÀ¨¹¹½¨ºÍÉèÖÃÄÚºËËùÐèµÄCMakeÖ¸Áî¡£ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄÀý×Ó£º
# CMake ×îµÍ°æ±¾ÒªÇó cmake_minimum_required(VERSION 3.0) # ÏîÄ¿Ãû³Æ project(LinuxKernel) # Ö¸¶¨ÄÚºËÔ´Âë·¾¶ set(KERNEL_SOURCE_DIRECTORY "/path/to/kernel/source") # Ö¸¶¨Êä³öĿ¼ set(OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/output") # ÉèÖñàÒëÑ¡Ïî set(CMAKE_C_FLAGS "-O2 -g") # ÉèÖÃÄÚºË configure_file(${KERNEL_SOURCE_DIRECTORY}/.config ${KERNEL_SOURCE_DIRECTORY}/arch/x86/configs/myconfig) # ¹¹½¨ÄÚºË add_custom_target(build_kernel ALL COMMAND make -C ${KERNEL_SOURCE_DIRECTORY} O=${OUTPUT_DIRECTORY} bzImage WORKING_DIRECTORY ${CMAKE_BINARY_DIR} )
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÖУ¬ÎÒÃÇÖ¸¶¨ÁËÄÚºËÔ´Âë·¾¶£¬Êä³öĿ¼ºÍ±àÒëÑ¡Ïî¡£ÎÒÃÇ»¹Ê¹ÓÃconfigure_fileÏÂÁԤÏÈ×¼±¸ºÃµÄÉèÖÃÎļþ¸´ÖƵ½ÄÚºËÔ´ÂëÖС£×îºó£¬ÎÒÃÇʹÓÃadd_custom_targetÏÂÁîÀ´¹¹½¨Äںˡ£
¹¹½¨ºÍÉèÖÃÄÚºË
Òª¹¹½¨ºÍÉèÖÃÄںˣ¬ÐèÒªÖ´ÐÐÒÔÏÂÏÂÁ
$ cmake . $ cmake --build .
µÇ¼ºó¸´ÖÆ
Ö´ÐÐÉÏÊöÏÂÁîºó£¬CMake½«×îÏȹ¹½¨Äں˲¢Æ¾Ö¤ÉèÖÃÎļþ¾ÙÐÐÉèÖ᣹¹½¨ºóµÄÄں˾µÏñÎļþ½«ÉúÑÄÔÚÖ¸¶¨µÄÊä³öĿ¼ÖС£
¶¨ÖÆÉèÖÃ
ÈôÊÇÐèÒª¶ÔÄں˾ÙÐиü¶àµÄ¶¨ÖÆÉèÖ㬿ÉÒÔÐÞ¸ÄÉèÖÃÎļþ(.config)²¢ÖØÐÂÔËÐÐCMakeÏÂÁî¡£ÕâÑù£¬CMake½«Ê¹ÓÃеÄÉèÖÃÎļþ¾ÙÐй¹½¨ºÍÉèÖá£
´úÂëʾÀý
ÒÔÏÂÊÇÒ»¸öCMakeLists.txtÎļþµÄÍêÕûʾÀý£¬ÆäÖаüÀ¨Á˸ü¶àµÄÉèÖÃÑ¡Ï
cmake_minimum_required(VERSION 3.0) project(LinuxKernel) set(KERNEL_SOURCE_DIRECTORY "/path/to/kernel/source") set(OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/output") set(CMAKE_C_FLAGS "-O2 -g") configure_file(${KERNEL_SOURCE_DIRECTORY}/.config ${KERNEL_SOURCE_DIRECTORY}/arch/x86/configs/myconfig) add_custom_target(build_kernel ALL COMMAND make -C ${KERNEL_SOURCE_DIRECTORY} O=${OUTPUT_DIRECTORY} bzImage WORKING_DIRECTORY ${CMAKE_BINARY_DIR} ) # Ìí¼ÓÒ»¸öÃûΪ"install_headers"µÄ×Ô½ç˵ĿµÄ£¬½«Í·Îļþ¸´ÖƵ½Ö¸¶¨Ä¿Â¼ add_custom_target(install_headers ALL COMMAND make -C ${KERNEL_SOURCE_DIRECTORY} O=${OUTPUT_DIRECTORY} INSTALL_HDR_PATH=${OUTPUT_DIRECTORY}/usr headers_install WORKING_DIRECTORY ${CMAKE_BINARY_DIR} ) # Ìí¼ÓÒ»¸öÃûΪ"install_modules"µÄ×Ô½ç˵ĿµÄ£¬½«Ä£¿éÎļþ¸´ÖƵ½Ö¸¶¨Ä¿Â¼ add_custom_target(install_modules ALL COMMAND make -C ${KERNEL_SOURCE_DIRECTORY} O=${OUTPUT_DIRECTORY} INSTALL_MOD_PATH=${OUTPUT_DIRECTORY}/rootfs modules_install WORKING_DIRECTORY ${CMAKE_BINARY_DIR} ) # ʹÓÃ"make clean"ÕûÀíÄÚºËÔ´ÂëĿ¼ add_custom_target(clean_kernel COMMAND make -C ${KERNEL_SOURCE_DIRECTORY} clean ) # Ìí¼ÓÒ»¸öÃûΪ"clean_all"µÄ×Ô½ç˵ĿµÄ£¬ÓÃÓÚÕûÀíËùÓÐÌìÉúµÄÎļþ add_custom_target(clean_all COMMAND rm -rf ${OUTPUT_DIRECTORY} COMMAND rm -rf ${CMAKE_BINARY_DIR}/CMakeCache.txt COMMAND rm -rf ${CMAKE_BINARY_DIR}/CMakeFiles )
µÇ¼ºó¸´ÖÆ
ÉÏÊöʾÀýÖеĴúÂëÑÝʾÁËÔõÑùÌí¼Ó×Ô½ç˵ĿµÄÀ´Ö´Ðиü¶àµÄ²Ù×÷£¬ºÃ±È×°ÖÃÍ·ÎļþºÍÄ£¿éÎļþ£¬ÒÔ¼°ÕûÀíÌìÉúµÄÎļþ¡£
×ܽá
±¾ÎÄÏÈÈÝÁËÔõÑùʹÓÃCMakeÀ´¹¹½¨ºÍÉèÖÃLinuxÄںˣ¬²¢ÌṩÁËһЩ´úÂëʾÀý¡£CMake×÷Ϊһ¸öÎÞа¶øÇ¿Ê¢µÄ¹¹½¨¹¤¾ß£¬ÎªÄں˿ª·¢ÕßÌṩÁ˸ü¶àµÄ¶¨ÖƺÍÉèÖÃÑ¡ÏʹµÃÄں˿ª·¢Ô½·¢±ã½ÝºÍ¸ßЧ¡£Ï£Íû±¾ÎĶÔÄúÔÚ¹¹½¨ºÍÉèÖÃLinuxÄÚºËʱÓÐËù×ÊÖú¡£
ÒÔÉϾÍÊÇʹÓÃCMake¹¹½¨LinuxÄں˵ÄÉèÖÃÖ¸ÄϵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡