bash自动生成Cmake工程

sh代码如下(gen_cmake.sh)

#!/bin/bash 
PWD=$(pwd)
cd $PWD
 # $0为执行的文件名 这里是./gen_cmake.sh

if [[ "$1" = "" ]]; then
echo "Usage: ./gen_cmake.sh project_name"
echo -ne "Please input the Project Name like [test] !"
read Answer
if [[ "$Answer" = "" ]]; then
echo -e "test";
PRO_NAME=test;
else
	PRO_NAME=$Answer;
fi # 第二个if
else
	PRO_NAME=$1;
fi # 第一个if
 
if [[ "$2" = "" ]]; then
echo "Usage: ./gen_cmake.sh project_name exe_name"
echo -ne "Please input the exe name like [test] !"
read Exename
if [[ "$Exename" = "" ]]; then
echo -e "test";
EXE_NAME=test;
else
	EXE_NAME=$Exename;
fi
else
	EXE_NAME=$2;
fi
 
#创建目录
echo "-------------Init Directory ...-------------"
mkdir -p $PRO_NAME/build
mkdir -p $PRO_NAME/include
mkdir -p $PRO_NAME/lib
#mkdir -p $PRO_NAME/${EXE_NAME}
 
#创建CMakeLists.txt
echo "-------------Create CMakeLists.txt-------------"
cd $PRO_NAME
 
echo "cmake_minimum_required (VERSION 2.8)
PROJECT($PRO_NAME)
#ADD_SUBDIRECTORY($EXE_NAME obj)
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG(\"-std=c++11\" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG(\"-std=c++0x\" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX11)
        SET(CMAKE_CXX_FLAGS \"\${CMAKE_CXX_FLAGS} -std=c++11\")
elseif(COMPILER_SUPPORTS_CXX0X)
        SET(CMAKE_CXX_FLAGS \"\${CMAKE_CXX_FLAGS} -std=c++0x\")
else()
     message(STATUS \"The compiler \${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.\")
endif()
# The version number.
set (VERSION_MAJOR 0)
set (VERSION_MINOR 0)
set (VERSION_PATCH 0)
set (VERSION_BUILD 1)
# configure a header file to pass some of the CMake settings to the source code
configure_file (
    \"\${PROJECT_SOURCE_DIR}/version.h.in\"
    \"\${PROJECT_BINARY_DIR}/version.h\"
)
SET(CMAKE_INSTALL_PREFIX \${PROJECT_BINARY_DIR})
install(FILES \${PROJECT_SOURCE_DIR}/config.json \${PROJECT_SOURCE_DIR}/${EXE_NAME}d DESTINATION \${PROJECT_BINARY_DIR}/bin)
#添加${EXE_NAME}的项目构建信息
#添加当前目录CPP文件
AUX_SOURCE_DIRECTORY(\${PROJECT_SOURCE_DIR} EXE_SRC_LIST)
#添加 include 搜索路径
INCLUDE_DIRECTORIES(\${PROJECT_SOURCE_DIR}
					\${PROJECT_SOURCE_DIR}/include)
#添加version的搜索路径
INCLUDE_DIRECTORIES(\${PROJECT_BINARY_DIR})
#添加 lib 搜索路径
LINK_DIRECTORIES(\${PROJECT_SOURCE_DIR}/lib
					/usr/local/lib)
#设置目标文件生成目录
set(EXECUTABLE_OUTPUT_PATH \${PROJECT_BINARY_DIR}/bin)
#目标文件名
ADD_EXECUTABLE(${EXE_NAME} \${EXE_SRC_LIST})
#链接lib库
#TARGET_LINK_LIBRARIES(play \${PROJECT_SOURCE_DIR}/lib/srs_librtmp.a)  # 显示指定链接静态库
#TARGET_LINK_LIBRARIES(${EXE_NAME})
" > CMakeLists.txt
 
#创建version.h.in
echo "-------------Create version.h.in-------------"
 
echo "#define VERSION_MAJOR @VERSION_MAJOR@
#define VERSION_MINOR @VERSION_MINOR@
#define VERSION_PATCH @VERSION_PATCH@
#define VERSION_BUILD @VERSION_BUILD@
" > version.h.in
 
#创建 config.json
echo "-------------Create config.json-------------"
 
echo "{
    \"name\":\"${EXE_NAME}\"
}" > config.json
 
echo "Create ${EXE_NAME}d"
 
echo "#!/bin/bash
# @Author:  ifree2017
# @Date:    `date "+%Y-%m-%d %H:%M:%S"`
# @Last Modified by:    ifree2017
# @Last Modified time:  `date "+%Y-%m-%d %H:%M:%S"`
# @processname: /etc/init.d/${EXE_NAME}d
# Source function library.
. /etc/init.d/functions
if [ \$2 == \"-d\" ] ; then
    EXEC_PATH=\`pwd\`
else
    EXEC_PATH=/usr/local/${EXE_NAME}
fi
EXEC=${EXE_NAME}
PID_FILE=/var/run/\${EXEC}.pid
DAEMON=\${EXEC_PATH}/\${EXEC}
if ! [ -x \${EXEC_PATH}/\${EXEC} ] ; then
    echo \"ERROR: \${EXEC_PATH}/\${EXEC} not found\"
    exit 1
fi
if ! [ -x \${EXEC_PATH}/log ] ; then
    echo \"mkdir log\"
    mkdir -p \${EXEC_PATH}/log
fi
logfilename=\"\${EXEC_PATH}/log/\${EXEC}_info_\`date +%Y%m%d_%H%M%S\`.log\"
stop()
{
        echo \"Stoping \${EXEC} ...\"
        killall \${DAEMON} >/dev/null
        usleep 100
        echo \"Shutting down \${EXEC}: [ OK ]\"
}
start()
{
        echo \"Starting \${EXEC} ...\"
        nohup \${DAEMON} 2>&1 >> \${logfilename} 2>&1 /dev/null &
        echo \"Starting \${EXEC}: [ OK ]\"
        usleep 100
}
restart()
{
        stop
        start
}
case \$1 in
        start)
                start
        ;;
        stop)
                stop
        ;;
        restart)
                start
        ;;
        status)
                ps aux|grep ${EXEC}
        ;;
        *)
                echo \"Usage: service \${EXEC} {start|stop|restart|status}\"
                exit 1
        ;;
esac
exit \$?" > ${EXE_NAME}d
 
chmod a+x ${EXE_NAME}d
 
#cd $EXE_NAME
 
#创建main.cc
echo "Create main.cc"
 
echo  "#include \"version.h\"
#include <stdio.h>
int main(int argc, char const *argv[])
{
	/* code */
	printf(\"Hello ${EXE_NAME}!/n\");
	printf(\"version:%d.%d.%d.%d!/n\", VERSION_MAJOR, VERSION_MINOR,
         VERSION_PATCH, VERSION_BUILD);
	return 0;
}
" >  main.cc
 
echo "-------------All Done!-------------"

运行示例

$ gen_cmake.sh test test
-------------Init Directory ...-------------
-------------Create CMakeLists.txt-------------
-------------Create version.h.in-------------
-------------Create config.json-------------
Create testd
Create main.cc
-------------All Done!-------------
$ cd test/
$ ll
drwxrwxrwx 1 zhujun zhujun  440 7月   7 16:08 ./
drwxrwxrwx 1 zhujun zhujun 4.0K 7月   7 16:08 ../
drwxrwxrwx 1 zhujun zhujun    0 7月   7 16:08 build/
-rwxrwxrwx 1 zhujun zhujun 1.7K 7月   7 16:08 CMakeLists.txt*
-rwxrwxrwx 1 zhujun zhujun   22 7月   7 16:08 config.json*
drwxrwxrwx 1 zhujun zhujun    0 7月   7 16:08 include/
drwxrwxrwx 1 zhujun zhujun    0 7月   7 16:08 lib/
-rwxrwxrwx 1 zhujun zhujun  237 7月   7 16:08 main.cc*
-rwxrwxrwx 1 zhujun zhujun 1.4K 7月   7 16:08 testd*
-rwxrwxrwx 1 zhujun zhujun  153 7月   7 16:08 version.h.in*

说明

  • bash 和sh有些差别,bash更强,支持[[]]
  • 原作者 : Shunli Guo
  • Usage : ./gen_cmake.sh PRO_NAME EXE_NAME
  • Description : 创建一个cmake的工程
  • 输入参数 :PRO_NAME EXE_NAME 即工程名和可执行文件名
  • 默认情况下运行本程序,会生成如下目录和文件
 test
 ├── build
 ├── CMakeLists.txt
 ├── config.json
 ├── include
 ├── lib
 ├── test
 │   └── main.cc
 └── version.h.in
  • 其中:
    1. CMakeLists.txt是CMake的描述文件
    2. build是编译目录
    3. config.json是配置文件
    4. include文件夹是头文件搜索路径
    5. lib文件夹是库文件搜索路径
    6. main.cc是主程序

编译操作过程:

 cd build 
 cmake ..
 make
上一篇:docker run VS docker exec 的区别


下一篇:SqlServer存储器创建