改进的newlisp编译脚本,只需要配置

前面有一篇Say bye to CMake and Makefile我开始用自己编写的newlisp脚本替代CMake,今天对前面的进行改进。

改进部分是:

1. newlisp armory模块的引入和初始化 部分可以重用

2. 将头文件所在目录,cpp或者cc文件所在目录以及依赖库名称等 作为配置单独抽出来,可以根据不同的项目进行配置


现在看改过后的代码:

rebuild.lsp是主要执行脚本

#!/usr/bin/newlisp 

;; init
(load "/opt/armory_config.lsp")
(load "build_config.lsp")

;; clean view/*.cpp files generated from cppcms_tmpl_cc
(set 'view-dir "../codes/main/src/view")
(if (directory? view-dir)
    (file:clean-folder view-dir)
  (make-dir 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")
(compile include-paths src-paths o-dir)

;; link all .o files
(dynamic-link o-dir bin-dir binary-name libs)


(exit)

依赖的/opt/armory_config.lsp用来配置newlisp_armory模块:

(env "NEWLISP_ARMORY_HOME" "/home/dean/github/newlisp_armory")
(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)

现在有了一个新的build_config.lsp文件,用来专门做配置:

(set 'include-paths 
     (list "../codes/main/include"
	    "../codes/main/src/../../loki-0.1.7/include"))

(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"
	   ))
(set 'libs 
     (list "pthread"
	   "cppcms"
	   "mongoclient"
	   "booster"
	   "loki"
	   "cryptopp"
	   "boost_system"
	   "boost_thread"
	   "boost_filesystem"
	   ))

(set 'o-dir "./o")
(set 'bin-dir "bin")
(set 'binary-name "sports_lottery_d")

上面配置了5项,不包含cppcms的部分,cppcms的tmpl文件编译还没有抽出来,仍然在rebuild.lsp脚本文件中。不过对于其他不用CppCMS的GCC编译来讲,直接删除即可。


g++.lsp文件不变。

;; @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)
  )


tmpl.lsp文件内容也不变:

(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)
    )
)



你可能感兴趣的:(改进的newlisp编译脚本,只需要配置)