Say bye to CMake and Makefile

用了几年的CMake,最近想试着琢磨如何将C++应用的动态链接全部改成静态链接,发现还需要研究CMake的用法,进入CMake的文档,

http://www.cmake.org/cmake/help/syntax.html

看到这句:

In many ways writing a CMakeLists file is like a writing a program in a simple language. Like most languages CMake provides flow control structures to help you along your way. CMake provides three flow control structures:
再往下看,里面是什么条件语句,循环,定义过程等,一个简单的script语言用来控制CMake生成Makefile。

过去CMake帮助了我,因为我对写Makefile很厌烦,CMake让我能够设计良好的目录结构,不要再自己实现目录树递归的Makefile脚本。

但是时至今天,对于已经熟练掌握newlisp语言的我,还有什么比用newlisp来实现脚本更强大的工具,为什么我还用CMake?我并不需要考虑跨平台编译,我只是在实践Linux服务器上的C++编程。我仅仅使用GCC作为编译器。直接用newlisp来控制GCC,可以让我直接使用GCC,更深层次的理解编译器的行为。

说做就做,本着仍然不改变原有CMake工程的精神,创建了几个newlisp脚本实现递归目录树,编译文件,最后动态或者静态链接。

现在介绍一下使用。这个CppCMS程序目录结构是:

 tree -L 4
.
├── builder
│   ├── build_config.lsp
│   ├── compile.lsp
│   ├── compile.lsp~
│   ├── dlink.lsp
│   ├── dlink.lsp~
│   ├── g++.lsp
│   ├── link.lsp~
│   ├── rebuild.lsp
│   ├── slink.lsp
│   ├── slink.lsp~
│   └── tmpl.lsp
├── codes
│   ├── main
│   │   ├── build
│   │   ├── CMakeLists.txt
│   │   ├── conf.d
│   │   │   ├── ca-certs.crt
│   │   │   ├── site.crt
│   │   │   ├── ssl.key
│   │   │   ├── ssl-pwd.key
│   │   │   ├── site.conf
│   │   │   ├── default.conf_bk
│   │   │   ├── example_ssl.conf
│   │   │   ├── fastcgi.cml
│   │   │   ├── nginx_signing.key
│   │   │   ├── server.crt
│   │   │   ├── server.csr
│   │   │   ├── server.key
│   │   │   └── server.key.org
│   │   ├── config.js
│   │   ├── create_deploy.sh
│   │   ├── include
│   │   │   ├── bean
│   │   │   ├── configuration.h
│   │   │   ├── controller
│   │   │   ├── display.h
│   │   │   ├── displays.h
│   │   │   ├── exception
│   │   │   ├── group.h
│   │   │   ├── groups.h
│   │   │   ├── helper
│   │   │   ├── module
│   │   │   ├── my_application.h
│   │   │   ├── response.h
│   │   │   ├── service
│   │   │   └── web_user.h
│   │   ├── install.sh
│   │   ├── jscheck
│   │   │   ├── pom.xml
│   │   │   └── src
│   │   ├── proto
│   │   │   ├── build.sh
│   │   │   ├── input
│   │   │   └── output
│   │   ├── resources
│   │   │   ├── html
│   │   │   ├── images
│   │   │   ├── plugin
│   │   │   ├── script
│   │   │   └── style
│   │   ├── src
│   │   │   ├── bean
│   │   │   ├── CMakeLists.txt
│   │   │   ├── controller
│   │   │   ├── display.cpp
│   │   │   ├── displays.cpp
│   │   │   ├── group.cpp
│   │   │   ├── groups.cpp
│   │   │   ├── helper
│   │   │   ├── main.cpp
│   │   │   ├── module
│   │   │   ├── my_application.cpp
│   │   │   ├── service
│   │   │   ├── view
│   │   │   └── web_user.cc
│   │   ├── template
│   │   │   ├── accounts.tmpl
│   │   │   ├── default.tmpl
│   │   │   ├── deny.tmpl
│   │   │   ├── gprsConfig.tmpl
│   │   │   ├── gprsManager.tmpl
│   │   │   ├── groupManagement.tmpl
│   │   │   ├── home.tmpl
│   │   │   ├── login.tmpl
│   │   │   ├── log.tmpl
│   │   │   ├── message.tmpl
│   │   │   ├── register_client.tmpl
│   │   │   ├── updatePassword.tmpl
│   │   │   └── userTitle.tmpl
│   │   └── tool.sh
│   └── test
└── README

运行方式就是进入builder目录执行

./rebuild.lsp

首先将CppCMS的tmpl模板文件编译成C++类

然后将所有.cc和.cpp文件编译成.o文件

最后动态链接成可执行程序。

先看rebuild.lsp代码:

#!/usr/bin/newlisp 

;; init
(load "/opt/build_config.lsp")
(set 'armory-folder (env "NEWLISP_ARMORY_HOME"))
(println (append "newlisp armory home folder: " armory-folder))
(load (append armory-folder "/codes/file/file.lsp"))
(file:init)

;; clean files genereated by CMake
(set 'cmake-build-dir "../codes/main/build")
(file:clean-folder cmake-build-dir)

;; clean view/*.cpp files generated from cppcms_tmpl_cc
(set 'view-dir "../codes/main/src/view")
(file:clean-folder view-dir)

(set 'tmpl-dir "../codes/main/template")

;; generate .cc files in view folder
(load "tmpl.lsp")
(tmpl-to-cc tmpl-dir view-dir)

;; compile all c++ files to .o file in ./o folder
(load "g++.lsp")
(set 'include-paths 
     (list "../codes/main/include"
	    "../codes/main/src/../../loki-0.1.7/include"))
(set 'o-dir "./o")
(set 'src-paths 
     (list "../codes/main/src"
	   "../codes/main/src/bean"
	   "../codes/main/src/controller"
	   "../codes/main/src/helper"
	   "../codes/main/src/module"
	   "../codes/main/src/service"
	   "../codes/main/src/view"
	   ))

(compile include-paths src-paths o-dir)

;; link all .o files
(set 'libs 
     (list "pthread"
	   "cppcms"
	   "mongoclient"
	   "booster"
	   "loki"
	   "cryptopp"
	   "boost_system"
	   "boost_thread"
	   "boost_filesystem"
	   ))
(set 'binary-name "sports_lottery")
(set 'bin-dir "bin")
(dynamic-link o-dir bin-dir binary-name libs)


(exit)

该文件用到了newlisp armory模块,参考我的GitHub项目: https://github.com/csfreebird/newlisp_armory

tmpl.lsp文件专门负责处理CppCMS tmpl文件,代码如下:

(define (get-extension name)
  (first (regex "[^.]*$" name))
  )

;; @syntax (remove-extension name)
;; @return file name without extension
;; @note remove extension name e.g a.b .b is extension, it will be removed
(define (remove-extension name)
  ((regex "(.*)\\.(.*)$" name) 3)
  )

;; @syntax (tmpl-to-cc)
;; @note find all tmpl files in tmpl-folder, translate them into *.cc files
(define (tmpl-to-cc tmpl-folder cc-folder)
  (set 'tmpl-files (directory tmpl-folder "\\.tmpl"))
  (set 'cmd-tmpl (append "cppcms_tmpl_cc " tmpl-folder "/%s -o " cc-folder "/%s.cc"))
  (dolist (tmpl-file tmpl-files)
    (set 'cmd (format cmd-tmpl tmpl-file (remove-extension tmpl-file)))
    (println cmd)
    (exec cmd)
    )
)

g++.lsp专门生成g++命令,文件内容如下:

;; @syntax (compile include-dirs src-dirs o-dir)
;; @parameter include-dir a list contains one or more relative or absolute include directories
;; @parameter src-dirs a list contains one or more relative or absolute src dirs
(define (compile include-dirs src-dirs o-dir)
  (if (directory? o-dir)
      (file:clean-folder o-dir)
    (make-dir o-dir))
  (set 'path1 "")
  (dolist (path include-dirs)
    (set 'path1 (append path1 "-I" path " ")))

  (set 'cmd-template (format "/usr/bin/c++ -g %s -Wall -o %s/" path1 o-dir))
  
  (dolist (dir src-dirs)
    (compile-dir dir cmd-template)
    )
  )

;; @syntax (compile-dir dir cmd-template)
;; @parameter dir one folder which contains many .cc or .cpp files
;; @parameter cmd-template the command template that has -g, -Wall and -I args 
(define (compile-dir dir cmd-template)
  (set 'file-list (directory dir "\\.cc|\\.cpp"))
  (println "dir: " dir)
  (dolist (cc-path file-list)
    (set 'str (append cc-path ".o"))
    (set 'cmd (append cmd-template  str " -c "  dir "/" cc-path))
    (println cmd)
    (exec cmd))
  )

;; @syntax (dynamic-link o-dir bin-dir binary-name libs)
;; @parameter o-dir the direcotry includs all .o files
;; @parameter bin-dir the location of linked binary file
;; @libs the list of all dependencies
(define (dynamic-link o-dir bin-dir binary-name libs)
  (if (directory? bin-dir)
      (file:clean-folder bin-dir)
    (make-dir bin-dir))
  (set 'cmd "/usr/bin/c++ -g")
  (set 'o-files (directory o-dir "\\.o"))
  (dolist (o-file o-files)
    (set 'cmd (append cmd " " (real-path o-dir) "/" o-file)))
  (set 'cmd (append cmd " -o " bin-dir "/" binary-name " -rdynamic"))
  (dolist (lib libs)
    (set 'cmd (append cmd " -l" lib)))
  (println cmd)
  (exec cmd)
  )

;; @syntax (static-link o-dir bin-dir binary-name libs)
;; @parameter o-dir the direcotry includs all .o files
;; @parameter bin-dir the location of linked binary file
;; @libs the list of all dependencies
(define (static-link o-dir bin-dir binary-name libs)
  (if (directory? bin-dir)
      (file:clean-folder bin-dir)
    (make-dir bin-dir))
  (set 'cmd "/usr/bin/c++ -g ")
  (set 'o-files (directory o-dir "\\.o"))
  (dolist (o-file o-files)
    (set 'cmd (append cmd " " (real-path o-dir) "/" o-file)))
  (set 'cmd (append cmd " -o " bin-dir "/" binary-name " -static-libgcc -static-libstdc++ -static -L/usr/lib/x86_64-linux-gnu"))
  (dolist (lib libs)
    (set 'cmd (append cmd " -l" lib))
    (println cmd))
    (exec cmd)
  )

为了方便使用,还提供了专门用于编译的文件compile.lsp,可以直接运行。

;; compile all c++ files to .o file in ./o folder
(load "g++.lsp")
(set 'include-paths 
     (list "../codes/main/include"
	    "../codes/main/src/../../loki-0.1.7/include"))
(set 'o-dir "./o")
(set 'src-paths 
     (list "../codes/main/src"
	   "../codes/main/src/bean"
	   "../codes/main/src/controller"
	   "../codes/main/src/helper"
	   "../codes/main/src/module"
	   "../codes/main/src/service"
	   "../codes/main/src/view"
	   ))

(compile include-paths src-paths o-dir)

(exit)

专门动态链接的文件dlink.lsp

#!/usr/bin/newlisp 

;; init
(load "/opt/build_config.lsp")
(set 'armory-folder (env "NEWLISP_ARMORY_HOME"))
(println (append "newlisp armory home folder: " armory-folder))
(load (append armory-folder "/codes/file/file.lsp"))
(file:init)


;; compile all c++ files to .o file in ./o folder
(load "g++.lsp")
(set 'o-dir "./o")
;; link all .o files
(set 'libs 
     (list "pthread"
	   "cppcms"
	   "mongoclient"
	   "booster"
	   "loki"
	   "cryptopp"
	   "boost_system"
	   "boost_thread"
	   "boost_filesystem"
	   ))
(set 'binary-name "sports_lottery_d")
(set 'bin-dir "bin")
(dynamic-link o-dir bin-dir binary-name libs)


(exit)

专门静态链接的文件slink.lsp

#!/usr/bin/newlisp 

;; init
(load "/opt/build_config.lsp")
(set 'armory-folder (env "NEWLISP_ARMORY_HOME"))
(println (append "newlisp armory home folder: " armory-folder))
(load (append armory-folder "/codes/file/file.lsp"))
(file:init)


;; compile all c++ files to .o file in ./o folder
(load "g++.lsp")
(set 'o-dir "./o")
;; link all .o files
(set 'libs 
     (list "mongoclient"
	   "cppcms"
	   "booster"
	   "boost_system"
	   "loki"
	   "cryptopp"
	   "boost_thread"
	   "pthread"
	   "boost_filesystem"
	   "dl"
	   "gcrypt"
	   "z"
	   "pcre"
	   "icuuc"
	   "icui18n"
	   "gpg-error"
	   "icuuc"
	   "icudata"
	   ))
(set 'binary-name "sports_lottery_s")
(set 'bin-dir "bin")
(static-link o-dir bin-dir binary-name libs)


(exit)

注意,slink.lsp中这些静态库的名称和顺序是不断尝试出来的。我总结了一个好方法:

1. A依赖B,A必须出现在B前

2. 一开始只加一个库mongoclient,然后看连接器报错,看少什么库,然后通过ldd来查找正确的库名称,添加在后面。这样就能找全所有的静态库。


newlisp真是强大的脚本工具,不断改进我的生活。将来完善了,放到我自己的Github项目中。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值