Exemplo n.º 1
0
int main(int argc,char* argv[])
{	
	int res=0;
	CCfg::init();

	//help(argc,argv);
	CRingArgs ringArgs;
	ringArgs.check(argc,argv);

	CTrace::add("new request");
	CTrace::add(argc,argv);

	CEnv env;
	env.init(argc,argv);

	CArgs args;
	args.parse(argc,argv);

	CCollect collect;
	collect.init(argc,argv);

	bool needCollect=args.getNeedCollect();
	
	//是编译操作
	if(needCollect)
	{
		//获取obj文件名称
		std::string oldFile=args.getObjFileName();
		std::string newFile;

		//生成收集信息目录
		res=collect.prepareObjFile(oldFile,newFile);
		cond_check_r(0==res,"createObjFilePath failed",-4);
		char** newArgs=NULL;

		//拿到新的gcc命令,进行临时编译,获取obj文件对应的所有头文件,源文件信息
		res=args.createNewCmd(newFile,newArgs);
		cond_check_r(0==res,"getOriCmd failed",-1);
		cond_check_r(NULL!=newArgs,"oriArgs is NULL",-2);
		cond_check_r(NULL!=newArgs[0],"args[0] is NULL",-3);

		//执行新的gcc命令
		int pid=fork();
		cond_check_r(0<=pid,"fork failed",-4);
		if(0==pid)
		{
			execvp(newArgs[0],newArgs);
			printf("child process execvp failed\n");
			exit(-1);
		}
		int status;
		waitpid(pid,&status,0);
		int childRetCode=WEXITSTATUS(status);
		if(0!=childRetCode)
			return childRetCode;

		//复制obj文件依赖的所有文件
		res=collect.copyFiles(newFile);
		cond_check_r(0==res,"collect copy files failed",-4);

		utils::rm(newFile.c_str());
		
		std::map<std::string,std::string> macros;
		res=args.getMacros(macros);
		cond_check_r(0==res,"getMacros failed",-5);
		res=collect.addMacros(macros);
		cond_check_r(0==res,"collect addMacros failed",-6);
	}

	//执行原来的编译操作
	{
		char** oriArgs=NULL;
		res=args.getOriCmd(oriArgs);
		cond_check_r(0==res,"getOriCmd failed",-1);
		cond_check_r(NULL!=oriArgs,"oriArgs is NULL",-2);
		cond_check_r(NULL!=oriArgs[0],"args[0] is NULL",-3);
		execvp(oriArgs[0],oriArgs);
		printf("execvp failed\n");
		exit(-1);
	}
	return 0;
}