Zen
A cross-platform functional programming language

/home/renji/Data/projects/zenlang/sources/zbl/Build.hpp

Go to the documentation of this file.
00001 
00007 #pragma once
00008 
00011 namespace Build {
00014     struct Unit {
00015     public:
00017         inline Unit() : _moc(false) {}
00018 
00019     private:
00021         z::string _fileName;
00022 
00024         bool _moc;
00025 
00027         z::string _copyTo;
00028 
00030         z::stringlist _depList;
00031 
00032     public:
00037         inline Unit& setFileName(const z::string& val) {_fileName = val; return ref(this);}
00038 
00042         inline const z::string& getFileName() const {return _fileName;}
00043 
00048         inline Unit& setMoc(const bool& val) {_moc = val; return ref(this);}
00049 
00053         inline const bool& getMoc() const {return _moc;}
00054 
00059         inline Unit& setCopyTo(const z::string& val) {_copyTo = val; return ref(this);}
00060 
00064         inline const z::string& getCopyTo() const {return _copyTo;}
00065 
00066     public:
00071         inline Unit& addDep(const z::string& val) {_depList.append(val); return ref(this);}
00072 
00076         inline const z::stringlist& getDepList() const {return _depList;}
00077     };
00078 
00080 
00082     struct RunMode {
00084         enum T{
00085             Help,     
00086             Info,     
00087             Make,     
00088             Build,    
00089             Compile,  
00090         };
00091     };
00092 
00094 
00096     struct OutputType {
00098         enum T {
00099             None,          
00100             Executable,    
00101             SharedLib,     
00102             StaticLib      
00103         };
00104     };
00105 
00108     struct Config {
00109     public:
00111         inline Config()
00112             : _outputType(Build::OutputType::None)
00113             , _pchName("_pch")
00114             , _noPch(false)
00115             , _genMain(false)
00116             , _isGui(false), _mkdir(true), _verbose(1), _debug(false)
00117         {}
00118 
00119     private:
00121         z::string _name;
00122 
00124         OutputType::T _outputType;
00125 
00127         z::string _qtl;
00128 
00130         z::string _suffix;
00131 
00133         z::string _outFile;
00134 
00136         z::string _apiDir;
00137 
00139         z::string _pchName;
00140 
00142         bool _noPch;
00143 
00145         bool _genMain;
00146 
00148         bool _isGui;
00149 
00151         bool _mkdir;
00152 
00154         int _verbose;
00155 
00157         bool _debug;
00158 
00160         z::stringlist _includePathList;
00161 
00163         z::stringlist _libraryList;
00164 
00165     public:
00170         inline Config& setName(const z::string& val) {_name = val; return ref(this);}
00171 
00175         inline const z::string& getName() const {return _name;}
00176 
00181         inline Config& setOutputType(const OutputType::T& val) {_outputType = val; return ref(this);}
00182 
00186         inline const OutputType::T& getOutputType() const {return _outputType;}
00187 
00192         inline Config& setQtl(const z::string& val) {_qtl = val; return ref(this);}
00193 
00197         inline const z::string& getQtl() const {return _qtl;}
00198 
00203         inline Config& setSuffix(const z::string& val) {_suffix = val; return ref(this);}
00204 
00208         inline const z::string& getSuffix() const {return _suffix;}
00209 
00214         inline Config& setApiDir(const z::string& val) {_apiDir = val; return ref(this);}
00215 
00219         inline const z::string& getApiDir() const {return _apiDir;}
00220 
00225         inline Config& setPchName(const z::string& val) {_pchName = val; return ref(this);}
00226 
00230         inline const z::string& getPchName() const {return _pchName;}
00231 
00232     public:
00237         inline Config& setNoPch(const bool& val) {_noPch = val; return ref(this);}
00238 
00242         inline const bool& getNoPch() const {return _noPch;}
00243 
00248         inline Config& setGenMain(const bool& val) {_genMain = val; return ref(this);}
00249 
00253         inline const bool& getGenMain() const {return _genMain;}
00254 
00259         inline Config& setIsGui(const bool& val) {_isGui = val; return ref(this);}
00260 
00264         inline const bool& getIsGui() const {return _isGui;}
00265 
00270         inline Config& setMkdir(const bool& val) {_mkdir = val; return ref(this);}
00271 
00275         inline const bool& getMkdir() const {return _mkdir;}
00276 
00281         inline Config& setVerbose(const int& val) {_verbose = val; return ref(this);}
00282 
00286         inline const int& getVerbose() const {return _verbose;}
00287 
00292         inline Config& setDebug(const bool& val) {_debug = val; return ref(this);}
00293 
00297         inline const bool& getDebug() const {return _debug;}
00298 
00299     public:
00304         inline Config& addIncludePath(const z::string& val) {_includePathList.append(val); return ref(this);}
00305 
00309         inline const z::stringlist& getIncludePathList() const {return _includePathList;}
00310 
00311     public:
00316         inline Config& addLibrary(const z::string& val) {_libraryList.append(val); return ref(this);}
00317 
00321         inline const z::stringlist& getLibraryList() const {return _libraryList;}
00322     };
00323 
00325 
00329     struct Project {
00330     public:
00332         inline Project()
00333             : _runMode(RunMode::Compile), _rmBldDir(false), _noBuild(false), _noTest(false), _main(false)
00334             , _zppExtensionList("zpp;"), _cppExtensionList("cpp;c;cc;cxx;"), _hppExtensionList("h;hpp;hxx;inl;")
00335         {
00336             _cmdCfgList.append(Config());
00337         }
00338 
00339     private:
00341         RunMode::T _runMode;
00342 
00344         z::string _name;
00345 
00347         z::string _project;
00348 
00350         z::string _src;
00351 
00353         z::string _out;
00354 
00356         z::string _bldDir;
00357 
00359         bool _rmBldDir;
00360 
00362         bool _noBuild;
00363 
00365         bool _noTest;
00366 
00368         bool _main;
00369 
00371         z::list<Unit> _fileList;
00372 
00374         z::list<Config> _cfgList;
00375 
00377         z::list<z::string> _genCfgList;
00378 
00379     private:
00381         z::string _projectFile;
00382 
00383     private:
00385         z::string _root;
00386 
00388         z::string _config;
00389 
00391         z::list<Config> _cmdCfgList;
00392 
00393     private:
00395         z::string _zppExtensionList;
00396 
00398         z::string _cppExtensionList;
00399 
00401         z::string _hppExtensionList;
00402 
00403     public:
00408         inline Project& setRunMode(const RunMode::T& val) {_runMode = val; return ref(this);}
00409 
00413         inline const RunMode::T& getRunMode() const {return _runMode;}
00414 
00419         inline Project& setName(const z::string& val) {_name = val; return ref(this);}
00420 
00424         inline const z::string& getName() const {return _name;}
00425 
00430         inline Project& setProjectType(const z::string& val) {_project = val; return ref(this);}
00431 
00435         inline const z::string& getProjectType() const {return _project;}
00436 
00441         inline Project& setSrc(const z::string& val) {_src = val; return ref(this);}
00442 
00446         inline const z::string& getSrc() const {return _src;}
00447 
00452         inline Project& setOut(const z::string& val) {_out = val; return ref(this);}
00453 
00457         inline const z::string& getOut() const {return _out;}
00458 
00463         inline Project& setBldDir(const z::string& val) {_bldDir = val; return ref(this);}
00464 
00468         inline const z::string& getBldDir() const {return _bldDir;}
00469 
00474         inline Project& setRmBldDir(const bool& val) {_rmBldDir = val; return ref(this);}
00475 
00479         inline const bool& getRmBldDir() const {return _rmBldDir;}
00480 
00485         inline Project& setNoBuild(const bool& val) {_noBuild = val; return ref(this);}
00486 
00490         inline const bool& getNoBuild() const {return _noBuild;}
00491 
00496         inline Project& setNoTest(const bool& val) {_noTest = val; return ref(this);}
00497 
00501         inline const bool& getNoTest() const {return _noTest;}
00502 
00507         inline Project& setMain(const bool& val) {_main = val; return ref(this);}
00508 
00512         inline const bool& getMain() const {return _main;}
00513 
00514     public:
00519         inline Project& addFile(const Unit& val) {_fileList.append(val); return ref(this);}
00520 
00524         inline const z::list<Unit>& getFileList() const {return _fileList;}
00525 
00530         inline Project& addConfig(const Config& val) {_cfgList.append(val); return ref(this);}
00531 
00535         inline const z::list<Config>& getConfigList() const {return _cfgList;}
00536 
00541         inline Project& addGenCfg(const z::string& val) {_genCfgList.append(val); return ref(this);}
00542 
00546         inline const z::list<z::string>& getGenCfgList() const {return _genCfgList;}
00547 
00548     public:
00554         inline const z::list<Config>& getCConfigList() const {
00555             if(_cfgList.size() > 0) {
00556                 return _cfgList;
00557             }
00558             return _cmdCfgList;
00559         }
00560 
00561     public:
00568         inline Project& addFileName(const z::string& val) {_fileList.append(Unit().setFileName(val)); return ref(this);}
00569 
00570     public:
00575         inline bool inGenCfgList(const Config& cfg) const {
00576             if((_runMode == RunMode::Make) && (_genCfgList.size() > 0)) {
00577                 for(z::list<z::string>::iterator it(_genCfgList); !it.end(); ++it) {
00578                     const z::string& name = *it;
00579                     if(cfg.getName() == name)
00580                         return true;
00581                 }
00582                 return false;
00583             }
00584             return true;
00585         }
00586 
00587     public:
00592         inline Project& setProjectFile(const z::string& val) {_projectFile = val; return ref(this);}
00593 
00597         inline const z::string& getProjectFile() const {return _projectFile;}
00598 
00599     public:
00604         inline Project& setRoot(const z::string& val) {_root = val; return ref(this);}
00605 
00609         inline const z::string& getRoot() const {return _root;}
00610 
00615         inline Project& setConfig(const z::string& val) {_config = val; return ref(this);}
00616 
00620         inline const z::string& getConfig() const {return _config;}
00621 
00622     public:
00626         inline Config& getCmdConfig() {return _cmdCfgList[0];}
00627 
00631         inline const Config& getCmdConfig() const {return _cmdCfgList.at(0);}
00632 
00633     public:
00638         inline bool isZppExt(const z::string& ext) const { return (_zppExtensionList.find(ext) >= 0); }
00639 
00644         inline bool isCppExt(const z::string& ext) const { return (_cppExtensionList.find(ext) >= 0); }
00645 
00650         inline bool isHppExt(const z::string& ext) const { return (_hppExtensionList.find(ext) >= 0); }
00651 
00652     public:
00654 
00658         inline z::string getAbsoluteDirPath(const z::string& filename) const {
00659             z::string apath = filename;
00660             apath = z::fileinfo::getNativeSeparator(apath);
00661             apath = z::fileinfo::getCanonicalDirPath(apath);
00662             apath = z::fileinfo::getAbsoluteFilePath(apath);
00663             return apath;
00664         }
00665 
00667 
00672         inline z::string getDirPath(const z::string& dir, const z::string& filename) const {
00673             z::string apath = filename;
00674             if(!z::fileinfo::isAbsolute(apath)) {
00675                 // if not already absolute make it relative to path of project file
00676                 apath = dir + "/" + filename;
00677             }
00678             return getAbsoluteDirPath(apath);
00679         }
00680 
00681     public:
00683 
00687         inline z::string getAbsoluteFilePath(const z::string& filename) const {
00688             z::string apath = filename;
00689             apath = z::fileinfo::getNativeSeparator(apath);
00690             apath = z::fileinfo::getCanonicalFilePath(apath);
00691             apath = z::fileinfo::getAbsoluteFilePath(apath);
00692             return apath;
00693         }
00694 
00696 
00701         inline z::string getFilePath(const z::string& dir, const z::string& filename) const {
00702             z::string apath = filename;
00703             if(!z::fileinfo::isAbsolute(apath)) {
00704                 // if not already absolute make it relative to path of project file
00705                 apath = dir + "/" + filename;
00706             }
00707             return getAbsoluteFilePath(apath);
00708         }
00709 
00711 
00717         inline z::string getRelativeFilePath(const z::string& srcdir, const z::string& dstdir, const z::string& filename) const {
00718             z::string apath = getFilePath(dstdir, filename);
00719             apath = z::fileinfo::getRelativeFilePath(srcdir, apath);
00720             apath = z::fileinfo::getNativeSeparator(apath); // getRelativeFilePath() converts "\" back to "/" on Win32.
00721             return apath;
00722         }
00723 
00725 
00730         inline void verifyDir(const z::string& name, const z::string& dir, const Config& cfg) const {
00731             if(z::fileinfo::exists(dir)) {
00732                 return;
00733             }
00734 
00735             if(!cfg.getMkdir()) {
00736                 throw z::exception(z::string::creator("%{name} directory does not exist: %{dir}").arg(z::any("name"), z::any(name)).arg(z::any("dir"), z::any(dir)).value());
00737             }
00738 
00739             if(cfg.getVerbose()) {
00740                 zbl::mlog() << "Creating " << name << " folder: " << dir << z::end();
00741             }
00742 
00743             if(!z::fileinfo::mkdir(dir)) {
00744                 throw z::exception(z::string::creator("Error creating %{name} directory: %{dir}").arg(z::any("name"), z::any(name)).arg(z::any("dir"), z::any(dir)).value());
00745             }
00746         }
00747 
00749 
00753         inline z::string getExt(const Build::Config& cfg) const {
00754             z::string ext;
00755         #ifdef WIN32
00756             if(cfg.getOutputType() == Build::OutputType::Executable)
00757                 ext = ".exe";
00758             else if(cfg.getOutputType() == Build::OutputType::SharedLib)
00759                 ext = ".dll";
00760             else if(cfg.getOutputType() == Build::OutputType::StaticLib)
00761                 ext = ".lib";
00762         #else
00763             if(cfg.getOutputType() == Build::OutputType::Executable)
00764                 ext = "";
00765             else if(cfg.getOutputType() == Build::OutputType::SharedLib)
00766                 ext = ".so";
00767             else if(cfg.getOutputType() == Build::OutputType::StaticLib)
00768                 ext = ".a";
00769         #endif
00770             return ext;
00771         }
00772 
00774 
00779         inline z::string getConfigValue(const Build::Config& cfg, const z::string& val) const {
00780             z::string vstr = z::string::creator(val)
00781                 .arg(z::any("out"), z::any(getAbsoluteDirPath(getOut())))
00782                 .arg(z::any("api"), z::any(getAbsoluteDirPath(cfg.getApiDir())))
00783                 .arg(z::any("bld"), z::any(getAbsoluteDirPath(getBldDir())))
00784                 .arg(z::any("config"), z::any(cfg.getName()))
00785                 .arg(z::any("name"), z::any(getName()))
00786                 .arg(z::any("ext"), z::any(getExt(cfg)))
00787                 .value();
00788             return vstr;
00789         }
00790 
00792 
00796         inline z::string getConfigValue(const z::string& val) const {
00797             return getConfigValue(getCmdConfig(), val);
00798         }
00799     };
00800 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines