From 813fdfc98551ac700a1b05d58c9d43896883b969 Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Fri, 7 Jul 2017 11:28:56 +0800 Subject: [PATCH] Update C4droid makefile generator. Remove linux makefile generator.(Recommand Codeblocks on Linux) --- makefile_c4gen.cpp | 170 ++++++++++++++++++++++++++++++------------ makefile_linuxgen.cpp | 74 ------------------ 2 files changed, 124 insertions(+), 120 deletions(-) delete mode 100644 makefile_linuxgen.cpp diff --git a/makefile_c4gen.cpp b/makefile_c4gen.cpp index 8daca92..14b2219 100644 --- a/makefile_c4gen.cpp +++ b/makefile_c4gen.cpp @@ -1,70 +1,70 @@ /// makefile_c4gen.cpp /// Under MIT License. Part of MiniEngine Project. -/// You can run this code to generate a makefile for c4droid. +/// You can run this code to generate a makefile for C4droid. + +#include +#include +/// Declaration +void FindFileRev(const std::string& dirname,const std::function& func); #include #include #include #include #include +#include using namespace std; char buff[1024]; int main() { - printf("#Detecting source files...#\n"); - // Set up environment - sprintf(buff,"PATH=%s:/data/data/com.n0n3m4.droidc/bzbox/",getenv("PATH")); - putenv(buff); - fflush(stdout); - system("find . -name \"*.cpp\" > cpplist.txt "); - system("find . -name \"*.c\" > clist.txt "); - vector dvec; - FILE* fp=fopen("cpplist.txt","r"); - memset(buff,0,1024); - while(fgets(buff,1024,fp)!=nullptr) + printf("Generator: Detecting source files...\n"); + /// Find files + vector objvec; + const string toFindCpp=".cpp"; + const string toFindC=".c"; + const string toAppendObj=".o"; + auto replaceEnd=[](const string& source,const string& replaceFrom,const string& replaceTo)->string { - int len=strlen(buff); - if(buff[len-1]=='\n') buff[len-1]=0; - char* p=strstr(buff,".cpp"); - sprintf(p,".o"); - string d(buff); - dvec.push_back(d); - memset(buff,0,1024); - } - fclose(fp); - vector ccd; - fp=fopen("clist.txt","r"); - memset(buff,0,1024); - while(fgets(buff,1024,fp)!=nullptr) + return source.substr(0,source.size()-replaceFrom.size()).append(replaceTo); + }; + auto endWith=[](const string& text,const string& testEndWith)->bool { - int len=strlen(buff); - if(buff[len-1]=='\n') buff[len-1]=0; - char* p=strstr(buff,".c"); - sprintf(p,".o"); - string d(buff); - dvec.push_back(d); - memset(buff,0,1024); - } - fclose(fp); - - printf("#Excluding files...#\n"); - auto iter=find_if(dvec.begin(),dvec.end(),[](const string& str){ return (str.find("makefile_c4gen")!=string::npos);}); - if(iter!=dvec.end()) dvec.erase(iter); - - printf("#Generating makefile...#\n"); - fp=fopen("makefile","w"); + return (text.substr(text.size()-testEndWith.size())==testEndWith); + }; + FindFileRev(".",[&](const std::string& name) + { + if(endWith(name,toFindCpp)) + { + objvec.push_back(replaceEnd(name,toFindCpp,toAppendObj)); + } + else if(endWith(name,toFindC)) + { + objvec.push_back(replaceEnd(name,toFindC,toAppendObj)); + } + }); + printf("Generator: Excluding files...\n"); + objvec.erase(remove_if(objvec.begin(),objvec.end(),[](const string& objname)->bool + { + return ( /// Beginning of excluding list + (objname.find("makefile_")!=string::npos && objname.find("gen.")!=string::npos) || + (objname.find("_Windows.")!=string::npos) + ); + }),objvec.end()); + printf("Generator: Generating makefile...\n"); + FILE* fp=fopen("makefile","w"); fprintf(fp, -"CXXFLAGS = -std=c++14 -Wall -O2 -D__C4DROID__ -Iinclude\n" +"CFLAGS = -Wall -s -O2 -D__LINUX__ -Iinclude -fPIC\n" +"CXXFLAGS = -std=c++14 -Wall -s -O2 -D__C4DROID__ -Iinclude\n" "LDFLAGS =\n" "LDLIBS = -lSDL2_image -lSDL2_net -ltiff -ljpeg -lpng -lz -lSDL2_ttf -lfreetype -lSDL2_mixer " "-lSDL2_test -lsmpeg2 -lvorbisfile -lvorbis -logg -lstdc++ -lSDL2 -lEGL -lGLESv1_CM -lGLESv2 " "-landroid -Wl,--no-undefined -shared\n" "PROG = program_name\n" "OBJS = "); - for(auto& d:dvec) + for(auto& obj:objvec) { - fprintf(fp,"%s ",d.c_str()); + fprintf(fp,"%s ",obj.c_str()); } fprintf(fp,"\n"); fprintf(fp, @@ -74,9 +74,87 @@ int main() "\t$(CXX) $(CXXFLAGS) $(LDFLAGS) $(LDLIBS) -o $@ $(OBJS) `sdl2-config --cflags --libs`\n" "\n" "clean:\n" -"\trm -f $(PROG) $(OBJS))\n"); +"\trm -f $(PROG) $(OBJS)\n"); fclose(fp); - printf("#End of Generation#\n"); + printf("Generator: Generation Finished.\n"); return 0; } + +/// Implement +#if defined(_MSC_VER) || defined(_WIN32) /// VS or Windows +#include +#include +#include +#include +void FindFileRev(const std::string& dirname,const std::function& func) +{ + std::string patternString; + if(dirname[dirname.size()-1]!='\\') + { + patternString=dirname+"\\*"; + } + else + { + patternString=dirname+"*"; + } + + WIN32_FIND_DATA fnd; + HANDLE hand=FindFirstFile(patternString.c_str(),&fnd); + if(hand!=INVALID_HANDLE_VALUE) + { + do + { + std::string fullname=dirname+fnd.cFileName; + if(fnd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) + { + fullname.append("\\"); + FindFileRev(fullname,func); + } + else + { + func(fullname); + } + } + while(FindNextFile(hand,&fnd)); + FindClose(hand); + } +} +#else /// Linux-like +#include +#include +#include +#include + +void FindFileRev(const std::string& dirname,const std::function& func) +{ + DIR* Dir = NULL; + struct dirent* file = NULL; + std::string curDir; + if (dirname[dirname.size()-1] != '/') + { + curDir=dirname+"/"; + } + else + { + curDir=dirname; + } + + if ((Dir = opendir(curDir.c_str())) == NULL) + { + return ; + } + while ((file = readdir(Dir)) != nullptr) + { + if (file->d_type == DT_REG) + { + func(curDir + file->d_name); + } + else if (file->d_type == DT_DIR && strcmp(file->d_name, ".") != 0 && strcmp(file->d_name, "..") != 0) + { + FindFileRev(curDir + file->d_name,func); + } + } + closedir(Dir); +} +#endif diff --git a/makefile_linuxgen.cpp b/makefile_linuxgen.cpp deleted file mode 100644 index 55e6447..0000000 --- a/makefile_linuxgen.cpp +++ /dev/null @@ -1,74 +0,0 @@ -/// makefile_linuxgen.cpp -/// Under MIT License. Part of MiniEngine Project. -/// You can run this code to generate a makefile for linux. - -#include -#include -#include -#include -#include -using namespace std; - -char buff[1024]; -int main() -{ - printf("#Detecting source files...#\n"); - /// Find files - system("find . -name \"*.cpp\" > cpplist.txt "); - system("find . -name \"*.c\" > clist.txt "); - vector dvec; - FILE* fp=fopen("cpplist.txt","r"); - memset(buff,0,1024); - while(fgets(buff,1024,fp)!=nullptr) - { - int len=strlen(buff); - if(buff[len-1]=='\n') buff[len-1]=0; - char* p=strstr(buff,".cpp"); - sprintf(p,".o"); - string d(buff); - dvec.push_back(d); - memset(buff,0,1024); - } - fclose(fp); - vector ccd; - fp=fopen("clist.txt","r"); - memset(buff,0,1024); - while(fgets(buff,1024,fp)!=nullptr) - { - int len=strlen(buff); - if(buff[len-1]=='\n') buff[len-1]=0; - char* p=strstr(buff,".c"); - sprintf(p,".o"); - string d(buff); - dvec.push_back(d); - memset(buff,0,1024); - } - fclose(fp); - - printf("#Generating makefile...#\n"); - fp=fopen("makefile","w"); - fprintf(fp, -"CXXFLAGS = -std=c++14 -Wall -O2 -D__LINUX__ -Iinclude\n" -"LDFLAGS = \n" -"LDLIBS = -lstdc++ -lSDL2_image -lSDL2_net -lSDL2_ttf -lSDL2_mixer -lSDL2_test -lSDL2 -shared\n" -"\n" -"PROG = your_project\n" -"OBJS = "); - for(auto& d:dvec) - { - fprintf(fp,"%s ",d.c_str()); - } - fprintf(fp,"\n"); - fprintf(fp, -"all: $(PROG)\n" -"\n" -"$(PROG): $(OBJS)\n" -"\t$(CXX) $(CXXFLAGS) $(LDFLAGS) $(LDLIBS) -o $@ $(OBJS) `sdl2-config --cflags --libs`\n" -"\n" -"clean:\n" -"\trm -f $(PROG) $(OBJS))\n"); - fclose(fp); - - printf("#End of Generation#\n"); - return 0; -}