From ba1000abc4b2c4e516d7ee07072d5841f99cc8d4 Mon Sep 17 00:00:00 2001 From: Saturneic Date: Mon, 28 Jan 2019 00:06:41 +0800 Subject: [PATCH] Added and Fixed. --- Net.xcodeproj/project.pbxproj | 18 +-- include/cmap.h | 258 ++++++++++++++++++++++++++++------ include/memory.h | 3 + include/type.h | 3 + src/main.cpp | 7 +- 5 files changed, 232 insertions(+), 57 deletions(-) diff --git a/Net.xcodeproj/project.pbxproj b/Net.xcodeproj/project.pbxproj index c4edade..c194da2 100644 --- a/Net.xcodeproj/project.pbxproj +++ b/Net.xcodeproj/project.pbxproj @@ -7,18 +7,13 @@ objects = { /* Begin PBXBuildFile section */ - 9277A15E21FD725F009C5F11 /* addr.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9277A15221FD725F009C5F11 /* addr.cpp */; }; - 9277A15F21FD725F009C5F11 /* socket.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9277A15321FD725F009C5F11 /* socket.cpp */; }; 9277A16021FD725F009C5F11 /* md5.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9277A15421FD725F009C5F11 /* md5.cpp */; }; 9277A16121FD725F009C5F11 /* cproj.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9277A15521FD725F009C5F11 /* cproj.cpp */; }; 9277A16221FD725F009C5F11 /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9277A15621FD725F009C5F11 /* main.cpp */; }; - 9277A16321FD725F009C5F11 /* cthread.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9277A15721FD725F009C5F11 /* cthread.cpp */; }; - 9277A16421FD725F009C5F11 /* server.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9277A15821FD725F009C5F11 /* server.cpp */; }; 9277A16521FD725F009C5F11 /* cmap.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9277A15921FD725F009C5F11 /* cmap.cpp */; }; 9277A16621FD725F009C5F11 /* cpart.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9277A15A21FD725F009C5F11 /* cpart.cpp */; }; 9277A16721FD725F009C5F11 /* memory.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9277A15B21FD725F009C5F11 /* memory.cpp */; }; 9277A16821FD725F009C5F11 /* clock.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9277A15C21FD725F009C5F11 /* clock.cpp */; }; - 9277A16921FD725F009C5F11 /* client.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9277A15D21FD725F009C5F11 /* client.cpp */; }; /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ @@ -59,7 +54,8 @@ 9277A15A21FD725F009C5F11 /* cpart.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = cpart.cpp; path = src/cpart.cpp; sourceTree = ""; }; 9277A15B21FD725F009C5F11 /* memory.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = memory.cpp; path = src/memory.cpp; sourceTree = ""; }; 9277A15C21FD725F009C5F11 /* clock.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = clock.cpp; path = src/clock.cpp; sourceTree = ""; }; - 9277A15D21FD725F009C5F11 /* client.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = client.cpp; path = src/client.cpp; sourceTree = ""; }; + 9277A16A21FE034F009C5F11 /* a.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = a.cpp; path = build/Debug/PCS/srcs/a.cpp; sourceTree = ""; }; + 9277A16C21FE0357009C5F11 /* da.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = da.cpp; path = build/Debug/PCS/srcs/da.cpp; sourceTree = ""; }; 92A1F2A421F21DC700340EFA /* a.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = a.cpp; path = build/Debug/PCS/a.cpp; sourceTree = ""; }; /* End PBXFileReference section */ @@ -93,6 +89,8 @@ 9221DA0E21EB5FAD007310A7 /* Net */ = { isa = PBXGroup; children = ( + 9277A16C21FE0357009C5F11 /* da.cpp */, + 9277A16A21FE034F009C5F11 /* a.cpp */, 9277A15121FD724C009C5F11 /* src */, 92A1F2A421F21DC700340EFA /* a.cpp */, 925A13AA21EC989500CBD427 /* include */, @@ -124,7 +122,6 @@ isa = PBXGroup; children = ( 9277A15221FD725F009C5F11 /* addr.cpp */, - 9277A15D21FD725F009C5F11 /* client.cpp */, 9277A15C21FD725F009C5F11 /* clock.cpp */, 9277A15921FD725F009C5F11 /* cmap.cpp */, 9277A15A21FD725F009C5F11 /* cpart.cpp */, @@ -197,16 +194,11 @@ files = ( 9277A16721FD725F009C5F11 /* memory.cpp in Sources */, 9277A16821FD725F009C5F11 /* clock.cpp in Sources */, - 9277A16921FD725F009C5F11 /* client.cpp in Sources */, 9277A16621FD725F009C5F11 /* cpart.cpp in Sources */, 9277A16121FD725F009C5F11 /* cproj.cpp in Sources */, 9277A16021FD725F009C5F11 /* md5.cpp in Sources */, 9277A16521FD725F009C5F11 /* cmap.cpp in Sources */, - 9277A15F21FD725F009C5F11 /* socket.cpp in Sources */, - 9277A16321FD725F009C5F11 /* cthread.cpp in Sources */, 9277A16221FD725F009C5F11 /* main.cpp in Sources */, - 9277A16421FD725F009C5F11 /* server.cpp in Sources */, - 9277A15E21FD725F009C5F11 /* addr.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -328,6 +320,7 @@ isa = XCBuildConfiguration; buildSettings = { CODE_SIGN_STYLE = Automatic; + HEADER_SEARCH_PATHS = "\"$(SRCROOT)/include\""; PRODUCT_NAME = "$(TARGET_NAME)"; }; name = Debug; @@ -336,6 +329,7 @@ isa = XCBuildConfiguration; buildSettings = { CODE_SIGN_STYLE = Automatic; + HEADER_SEARCH_PATHS = "\"$(SRCROOT)/include\""; PRODUCT_NAME = "$(TARGET_NAME)"; }; name = Release; diff --git a/include/cmap.h b/include/cmap.h index 266a653..b8034c6 100644 --- a/include/cmap.h +++ b/include/cmap.h @@ -61,10 +61,9 @@ struct cpt_func_args{ class Cpt:public setting_file{ friend Proj; - vector cparts; vector src_files; -// 源文件内含有的入口函数 - map> funcs; +// 入口函数对应的源文件 + map funcs_src; // 入口函数的输入与输出参数格式 map> fargs_in,fargs_out; string path; @@ -73,6 +72,9 @@ class Cpt:public setting_file{ public: Cpt(string path, string proj_name){ +#ifdef DEBUG + printf("Reading Cpt File %s\n[*]Require Project Name %s\n",path.data(),proj_name.data()); +#endif ifscpt.open(path); string line; if(search_key(ifscpt, "cparts")){ @@ -89,6 +91,9 @@ public: if(if_illegal(c)); else throw "project's name has illegal char"; } +#ifdef DEBUG + printf("Read Project Name %s\n",t_name.data()); +#endif // 检查工程名 if(t_name == proj_name) name = t_name; else throw "project's name confilct"; @@ -107,9 +112,11 @@ public: if(qb_idx == string::npos) tsrc_name = line; else tsrc_name = line.substr(0,qb_idx); tsrc_name = tsrc_name.substr(1,tsrc_name.size()-2); +#ifdef DEBUG + printf("Read Source File Name %s\n",tsrc_name.data()); +#endif // 记录源文件名 src_files.push_back(tsrc_name); - funcs.insert({tsrc_name,{}}); // 寻找左大括号 if(qb_idx == string::npos){ ifscpt>>line; @@ -123,11 +130,17 @@ public: } if(real_line.empty()) continue; if(real_line != "};"){ +#ifdef DEBUG + printf("Read Function Description %s\n",real_line.data()); +#endif // 分离输出参数列表 string::size_type dq_l = real_line.find("{"); string::size_type dq_r = real_line.find("}"); if(dq_l == string::npos || dq_r == string::npos) throw "syntax error"; string str_argout = real_line.substr(dq_l+1,dq_r-dq_l-1); +#ifdef DEBUG + printf("Read Args (OUT) Description %s\n",str_argout.data()); +#endif vector cfgo = deal_args(str_argout); // 分离输入参数列表 string::size_type yq_l = real_line.find("("); @@ -135,7 +148,9 @@ public: if(yq_l == string::npos || yq_r == string::npos) throw "syntax error"; string str_argin = real_line.substr(yq_l+1,yq_r-yq_l-1); vector cfgi = deal_args(str_argin); - +#ifdef DEBUG + printf("Read Args (IN) Description %s\n",str_argin.data()); +#endif // 分离入口函数名 string func = real_line.substr(dq_r+1,yq_l-dq_r-1); string real_func; @@ -145,6 +160,11 @@ public: if(!if_illegal(c)) throw "func name has illegal char"; } } +#ifdef DEBUG + printf("Read Function Name %s\n",real_func.data()); +#endif +// 记录入口函数名 + funcs_src.insert({real_func,tsrc_name}); // 添加相关参数 fargs_out.insert({real_func,cfgo}); fargs_in.insert({real_func,cfgi}); @@ -215,9 +235,10 @@ class Map: public Cpt{ }; -class Proj{ -// 计算工程描述文件所在的地址 +class Proj:public setting_file{ +// 计算工程所在的目录 string proj_path; +// 计算工程描述文件名 string proj_file; // 工程名 string name; @@ -225,9 +246,11 @@ class Proj{ ifstream ifsproj; // 源文件所在的目录 vector src_paths; -// 源文件 +// 源文件搜索目录下的所有源文件 map src_files; -// 源文件的MD5 +// 计算工程所涉及到的源文件 + map used_srcfiles; +// 计算工程所涉及到的源文件的MD5 map src_md5; // 关系描述文件所在的目录 vector map_paths; @@ -239,6 +262,11 @@ class Proj{ vector maps; // 动态链接库存放的目录 string lib_path; +// 模块入口函数索引 + map func_index; +// 动态链接库对应的源文件索引 + map lib_index; + // 判断参数是否为字符串 bool if_string(string &arg){ if(arg[0] == '\"' && arg[arg.size()-1] == '\"') return true; @@ -270,35 +298,49 @@ class Proj{ else throw "syntax error"; } protected: -// 检查路径或文件 - void check_paths(string main_path, vector paths){ - for(auto path : paths){ - if(!~access((main_path+path).data(),F_OK)) throw path+" is abnormal"; - } - } -// 检查字符是否合法 - bool if_illegal(char c){ - if(isalnum(c) || c == '_') return true; - else return false; - } -// 寻找保留字 - bool search_key(ifstream &ifsfile,string key){ - string line; - do{ - ifsfile>>line; - }while(line.find(key) == string::npos && ifsfile.eof() == false); - if(ifsfile.eof()) return false; - return true; - } + +// 读取所有涉及的Cpt文件 void build_cpts(void){ for(auto cptp : cpt_paths){ Cpt *ncpt = new Cpt(proj_path + cptp, name); cpts.push_back(ncpt); } } + +// 检查Cpt文件中描述的源文件是否存在对应实体 void check_cpt(void){ - + for(auto cpt:cpts){ +#ifdef DEBUG + printf("Checking Cpt File %p.\n",cpt); +#endif + for(auto file:cpt->src_files){ + auto src_file = src_files.find(file); +// 如果在索引中没有找到对应的源文件 + if(src_file == src_files.end()){ +#ifdef DEBUG + printf("Fail To Find Soruce File Related %s\n",file.data()); +#endif + throw "source file not exist"; + } + else{ +#ifdef DEBUG + printf("Succeed In Finding Source File %s\n",file.data()); +#endif + used_srcfiles.insert({src_file->first,src_file->second}); +// 计算源文件的MD5 + string md5; + string tsrc_path = src_paths[src_file->second]; + string t_path = proj_path+tsrc_path+"/"+file; + ComputeFile(t_path, md5); + src_md5.insert({file,md5}); +#ifdef DEBUG + printf("Computed Source File's MD5 %s\n[*]Source File Path %s\n",md5.data(),t_path.data()); +#endif + } + } + } } + // 搜寻源文件目录 void search_src(int idx,string path){ DIR *pdir = opendir(path.data()); @@ -311,16 +353,49 @@ protected: // 含有.cpp的文件 if(file.find(".cpp") != string::npos){ src_files.insert({file,idx}); -// 计算源文件的MD5 - string md5; - ComputeFile(path+"/"+file, md5); - src_md5.insert({file,md5}); } } } } else throw "path is abnormal"; } + +// 将现有信息储存到一个新的数据库中 + void update_db(void); + +// 检查涉及到的源文件的MD5与数据库中的是否一致 + void check_src_md5(string db_path); + +// 编译目标源文件生成动态链接库 + void build_src(string lib_name,string srcfile_path, string libs_path){ + string build_command = "g++ -fPIC -shared -std=c++11 -o "+libs_path+"/"+lib_name+" "+srcfile_path+" >gcc_build.log 2>&1"; +#ifdef DEBUG + printf("Build Command %s\n",build_command.data()); +#endif + int rtn = system(build_command.data()); +// 检测命令执行情况 + if(rtn != -1 && rtn != 127){ + struct stat statbuff; + stat("gcc_build.log", &statbuff); + unsigned long file_size = statbuff.st_size; +// 检测命令重定向输出文件的大小,判断编译是否出现了错误 + if(file_size == 0){ +#ifdef DEBUG + printf("Succeed In Compiling File %s\n",srcfile_path.data()); +#endif + } + else{ +#ifdef DEBUG + printf("Caught Errors or Warrnings In Compiling File %s\n",srcfile_path.data()); +#endif + throw "compile error"; + } + } + else throw "fail to build lib file"; + } + + + public: Proj(string t_projpath, string t_projfile){ // 检查工程描述文件是否可读 @@ -392,7 +467,8 @@ public: check_paths(proj_path,cpt_paths); } - void SearchPathInfo(void){ +// 搜寻源文件搜索目录并读取Cpt文件 + void SearchInfo(void){ int idx = 0; // 遍历源文件储存目录 for(auto path : src_paths) search_src(idx++, proj_path+path); @@ -400,20 +476,116 @@ public: build_cpts(); } +// 检查Cpt文件内所描述的源文件是否有对应的实体 + void CheckCptInfo(void){ + check_cpt(); + } - void CheckInfo(void){ - for(auto cpt:cpts){ - for(auto file:cpt->src_files){ - if(src_files.find(file) == src_files.end()) throw "source file not exist"; +// 建立计算模块入口函数索引 + void BuildFuncIndex(void){ +// 对应cpt文件 + for(auto pcpt : cpts){ +// 对应源文件 + for(auto func: pcpt->funcs_src){ +// 对应的计算模块入口函数 + func_index.insert({func.first,pcpt}); +#ifdef DEBUG + printf("Building Func Index %s (%p).\n",func.first.data(),pcpt); +#endif } } } +// 编译涉及到的源文件 + void CompileUsedSrcFiles(void){ + for(auto src : used_srcfiles){ + string tsrc_path = src_paths[src.second]; + string tlib_name = "lib_"+src.first+".so"; + string t_path = proj_path+tsrc_path+"/"+src.first; + string tlib_path = proj_path+lib_path; +#ifdef DEBUG + printf("Compling Used Source File %s\n[*]Libname %s\n[*]Source File Path %s\n[*]Lib Path %s\n",src.first.data(),tlib_name.data(),t_path.data(),tlib_path.data()); +#endif + build_src(tlib_name,t_path,tlib_path); + lib_index.insert({src.first,tlib_name}); + } + } + +// 检查入口函数是否在对应的动态链接库中可被按要求正确解析 + void CheckFuncInfo(void){ + for(auto func : func_index){ + string src_file = func.second->funcs_src.find(func.first)->second; + string tlib_name = lib_index.find(src_file)->second; + string tlib_path = proj_path+lib_path+"/"+tlib_name; +#ifdef DEBUG + printf("Checking Func %s\n[*]Lib File %s\n[*]Source File %s\n",func.first.data(),tlib_path.data(),src_file.data()); + +#endif +// 动态链接库操作柄 + void *lib_handle; +// 入口函数操作柄 + PCSFUNC func_handle; +// 传入传出参数列表操作柄 + vector *args_handle; + // 获得动态链接库的操作柄 + lib_handle = dlopen(tlib_path.data(), RTLD_NOW | RTLD_LOCAL); + if(lib_handle == nullptr){ +#ifdef DEBUG + printf("Fail To Get Lib File Handle\n"); + +#endif + throw "can not open library"; + } +#ifdef DEBUG + printf("Succeed In Getting Lib File Handle %p\n",lib_handle); + +#endif + // 获得该模块的入口 + func_handle = (PCSFUNC) dlsym(lib_handle, func.first.data()); + if(func_handle == nullptr){ +#ifdef DEBUG + printf("Fail To Get Func Handle\n"); + +#endif + throw "can not get func "+func.first; + } +#ifdef DEBUG + printf("Succeed In Getting Func Handle %p\n",func_handle); + +#endif + // 获得向该模块传入参数的操作柄 + args_handle = (vector *) dlsym(lib_handle, ("__"+func.first+"_args_in").data()); + if(args_handle == nullptr){ +#ifdef DEBUG + printf("Fail To Get Args (IN) Handle\n"); + +#endif + throw "can not get the HANDLE to PUSH args"; + } +#ifdef DEBUG + printf("Succeed In Getting Args (IN) Handle %p\n",args_handle); + +#endif + // 获得获取该模块传出参数的操作柄 + args_handle = (vector *) dlsym(lib_handle, ("__"+func.first+"_args_out").data()); + if(args_handle == nullptr){ +#ifdef DEBUG + printf("Fail To Get Args (OUT) Handle\n"); + +#endif + throw "can not get the HANDLE to GET args"; + } +#ifdef DEBUG + printf("Succeed In Getting Args (OUT) Handle %p\n",args_handle); + +#endif + dlclose(lib_handle); + } + } + }; - - //计算任务图类 class CMap{ public: @@ -421,8 +593,8 @@ public: map cparts; // 记录计算模块依赖关系图 map depends; -// 构造函数传入图包所在的目录 - CMap(string proj_path); +// 构造函数传入计算工程管理类 + CMap(class Proj &); // 根据图的表述文件构造计算模块列表 void BuildCPart(ifstream &map); // 根据图表述文件中的描述信息,处理并转化为形式输入或输出参数列表 diff --git a/include/memory.h b/include/memory.h index e93adb4..2f40c8c 100644 --- a/include/memory.h +++ b/include/memory.h @@ -12,6 +12,9 @@ #include "type.h" #include "memory_type.h" +using std::map; +using std::pair; + class BlocksPool{ // 内存块表 map blocks_list; diff --git a/include/type.h b/include/type.h index 1a8ff4c..6993670 100644 --- a/include/type.h +++ b/include/type.h @@ -9,6 +9,8 @@ #ifndef type_h #define type_h +#define DEBUG + #include #include #include @@ -23,6 +25,7 @@ #include #include +#include #include #include #include diff --git a/src/main.cpp b/src/main.cpp index b4c78b6..a05165b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,8 +18,11 @@ int main(int argc, char *argv[]){ try{ Proj nproj("./PCS","pcs.proj"); - nproj.SearchPathInfo(); - nproj.CheckInfo(); + nproj.SearchInfo(); + nproj.BuildFuncIndex(); + nproj.CheckCptInfo(); + nproj.CompileUsedSrcFiles(); + nproj.CheckFuncInfo(); //Cpt ncpt("./PCS/pcs.cpt","CPTest"); }