In the previous article "say bye to cmake and makefile", I started to replace cmake with my own newlisp script. Today I have improved the previous article.
Improvements:
1. The introduction and initialization of the newlisp armory module can be reused.
2. Separate the directory where the header file is located, the directory where the CPP or CC file is located, and the name of the dependent database. You can configure the files based on different projects.
Now let's look at the changed code:
Rebuild. LSP is the main execution script
#!/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)
The dependent/opt/armory_config.lsp is used to configure the newlisp_armory module:
(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)
Now we have a new build_config.lsp file for Configuration:
(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")
Five items are configured above, excluding the cppcms part. The tmpl file compilation of cppcms has not yet been extracted and is still in the rebuild. LSP script file. However, for other GCC compilation without cppcms, simply delete it.
The G ++. LSP file remains unchanged.
;; @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) )
The content of the tmpl. LSP file remains unchanged:
(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) ))