将自定义命令添加到CMakeLists.txt文件

时间:2014-08-12 15:42:08

标签: cmake command

最近我将项目编制程序从make切换到cmake。我想在我的CMakeLists.txt文件中添加自定义非编译相关命令。这些命令应该从经典Makefile继承。我想在CMakeLists.txt中重复使用的命令是:

CODE=main
ARCHIVE=$(CODE)_`date +%F`.tar.bz2
BACKUP=$(HOME)/backup/src/$(CODE)

run:
    { time ./$(CODE); } 2> $(CODE)_time

rerun:  fresh run

fresh:
    rm -rvf $(CODE)_time *~ fort.* *.ver
    rm -rvf coef? enc skin_depths log input_params.out

clean:  fresh
    rm -rvf $(CODE) *.o *.a *.mod

backup: *
    tar -cjvf $(ARCHIVE) $?
    tar -tjvf $(ARCHIVE)
    mv  -vf   $(ARCHIVE) $(BACKUP)
    ls  -lrth $(BACKUP)

如何将这些命令转移到CMakeLists.txt,以便它们会自动插入cmake生成的Makefile

的CMakeLists.txt

# Minimum cmake version
cmake_minimum_required(VERSION 2.8)

# Project name
project(main Fortran)

# Toolchain selection, possible arguments: Intel, GNU
SET(Toolchain GNU)

# Compiler selection
include(CMakeForceCompiler) 

# GNU compilers
if(${Toolchain} MATCHES GNU)
  CMAKE_FORCE_Fortran_COMPILER(gfortran "GNU Fortran Compiler")
  set(CMAKE_Fortran_FLAGS "-g -O0 -fimplicit-none -fbounds-check -fbacktrace ${CMAKE_Fortran_FLAGS}")
endif(${Toolchain} MATCHES GNU)

# Source code files
SET(src variables.F90
        init.F90
        io.F90)

# Main executable
ADD_EXECUTABLE(${PROJECT_NAME} ${src})

# EoF: CMakeLists.txt

1 个答案:

答案 0 :(得分:0)

使用ADD_CUSTOM_TARGET和/或ADD_CUSTOM_COMMAND命令,例如:

ADD_CUSTOM_TARGET(myTarget
                  COMMAND firstCommand arg1 arg2
                  COMMAND secondCommand arg1 arg2 arg3)

然后您可以使用make myTarget