cmake常用配置,设置输出文件夹,创建、拷贝

用法

  • cmakelists.txt中使用include包含下面的cmake文件即可

核心cmake脚本代码如下

# ------------------------------------------------------------
# 拷贝文件
macro(CP_FILES )	
	set(options)
	set(oneValueArgs )
	# SRC_FILES - 要拷贝哪些文件
	# TARGET_ROOT - 拷贝到哪里
	set(multiValueArgs SRC_FILES TARGET_ROOT )
	# 解析参数
	cmake_parse_arguments(CF "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )

	# 有这么多文件需要拷贝
    foreach(WHICH_FILE ${CF_SRC_FILES})
		# 将文件拷贝到这么多的文件夹
		foreach(WHICH_ROOT ${CF_TARGET_ROOT})
			# 将文件WHICH_FILE拷贝到WHICH_ROOT目录
			execute_process( COMMAND ${CMAKE_COMMAND} -E copy ${WHICH_FILE} ${WHICH_ROOT})
		endforeach()
    endforeach()
endmacro()


# ------------------------------------------------------------
# 创建文件夹
macro(MKDIR)
	set(options)
	set(oneValueArgs )
	# TARGET_ROOT - 有哪些路径需要创建
	set(multiValueArgs TARGET_ROOT )
	# 解析参数
	cmake_parse_arguments(MK "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )

	# 需要创建这么多的的文件夹
	foreach(WHICH_ROOT ${MK_TARGET_ROOT})
		# 创建文件夹落地
		execute_process( COMMAND ${CMAKE_COMMAND} -E make_directory ${WHICH_ROOT})
	endforeach()
endmacro()


# ------------------------------------------------------------
# 拷贝文件夹
macro(CP_ROOT)
	set(options)
	set(oneValueArgs )
	# SRC_ROOT - 要拷贝哪些文件夹
	# TARGET_ROOT - 拷贝到哪里
	set(multiValueArgs SRC_ROOT TARGET_ROOT )
	# 解析参数
	cmake_parse_arguments(CR "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )

	# 有这么多需要拷贝
    foreach(WHICH_SRC_ROOT ${CR_SRC_ROOT})
		# 将文件拷贝到这么多的文件夹
		foreach(WHICH_TARGET_ROOT ${CR_TARGET_ROOT})
			# 将文件 WHICH_SRC_ROOT 拷贝到 WHICH_TARGET_ROOT 目录
			execute_process( COMMAND ${CMAKE_COMMAND} -E copy_directory ${WHICH_SRC_ROOT} ${WHICH_TARGET_ROOT})
		endforeach()
    endforeach()
endmacro()


# ----------------------------------------------------------------
# 设置输出使用
set(output_prefix ${CMAKE_CURRENT_LIST_DIR}/../publish)

if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
# using Clang
	set(compiler_name clang)
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
# using GCC
	set(compiler_name gcc)
	
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
	set(compiler_name intel)
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
	if(MSVC_VERSION EQUAL 1400)
		set(compiler_name vc80)
	elseif(MSVC_VERSION EQUAL 1500)
		set(compiler_name vc90)
	elseif(MSVC_VERSION EQUAL 1600)
		set(compiler_name vc100)
	elseif(MSVC_VERSION EQUAL 1700)
		set(compiler_name vc110)
	elseif(MSVC_VERSION EQUAL 1800)
		set(compiler_name vc120)
	elseif(MSVC_VERSION EQUAL 1900)
		set(compiler_name vc140)
	elseif(MSVC_VERSION EQUAL 1910)
		set(compiler_name vc141)
	elseif(MSVC_VERSION EQUAL 1920)
		set(compiler_name vc142)
	elseif(MSVC_VERSION EQUAL 1930)
		set(compiler_name vc143)
	else()
		set(compiler_name vcXX)
	endif()
	
endif()

# 64位
if (CMAKE_CL_64)
	set (platform "x64")
# 32
else()
	set (platform "x86")
endif()


# publish/vc100/x64
set(publish_prefix ${output_prefix}/${compiler_name}/${platform})

# 指定include目录
set(pir ${publish_prefix}/include)


# 指定可执行程序输出目录
set(pbd 	${publish_prefix}/bin/Debug)
set(pbr 	${publish_prefix}/bin/Release)
# 指定 库文件输出目录
set(pld 	${publish_prefix}/lib/Debug)
set(plr 	${publish_prefix}/lib/Release)

# 设置 可执行程序输出目录
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${pbd})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${pbr})

# 设置库文件输出目录
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG ${pld})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE ${plr})

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${pld})
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${plr})

# 输出cmake文件夹
set(pc ${publish_prefix}/cmake)


# ----------------------------------------------------------------
# 有这么多文件夹需要创建
set(TO_MK_ROOT 
${pir}
${pld}
${plr}
${pbd}
${pbr}
${pc}
)
# message(${TO_MK_ROOT})
MKDIR(TARGET_ROOT ${TO_MK_ROOT})
posted @ 2023-09-23 07:20  mohist  阅读(584)  评论(0编辑  收藏  举报