#include "stdutil.h" #include #include bool opt_trace; //-z BMK0: trace dependancies string opt_outfile; //-o BMK0: output exe bool opt_quiet; //-q bool opt_verbose; //-v bool opt_makeall; //-a bool opt_debug; //-d bool opt_release; //-r bool opt_threaded; string opt_arch; //-g x86/ppc string opt_apptype; //-t apptype string opt_module; //-m 'modname' string opt_framework; //-f 'modname' or "*" string opt_infile; set env_config; string env_blitzpath,env_platform,env_binpath,env_libpath,config_mung,global_mung; #if _WIN32 static void init_env(){ char path[1024]={0}; GetModuleFileName( GetModuleHandle(0),path,1024 ); string t=path; int n=t.rfind( "\\bin\\" ); if( n==string::npos ) n=t.rfind( "\\BIN\\" ); if( n==string::npos ) abort(); env_blitzpath=t.substr(0,n); env_platform="win32"; } #elif __APPLE__ static void init_env(){ CFURLRef url; char path[1024],*p; url=CFBundleCopyExecutableURL( CFBundleGetMainBundle() ); CFURLGetFileSystemRepresentation( url,true,(UInt8*)path,1024 ); string t=path; int n=t.rfind( "/bin/" ); if( n==string::npos ) abort(); env_blitzpath=t.substr(0,n); env_platform="macos"; } #elif __linux static void init_env(){ char linkname[256]; // /proc//exe char path[1024]={0}; pid_t pid; int ret; pid=getpid(); sprintf(linkname, "/proc/%i/exe", pid); ret=readlink(linkname, path,1024); if (ret<1 || ret>1022) abort(); path[ret]=0; string t=path; int n=t.rfind( "/bin/" ); if( n==string::npos ) abort(); env_blitzpath=t.substr(0,n); env_platform="linux"; } #else #error "Unsuppported build platform" #endif void stdutil_init( int argc,char *argv[] ){ init_env(); fixpath( env_blitzpath ); env_binpath=env_blitzpath+"/bin"; env_libpath=env_blitzpath+"/lib"; #if __APPLE__ #if __ppc__ if( is_pid_native(0) ) opt_arch="ppc"; else opt_arch="x86"; #elif __i386__ if( is_pid_native(0) ) opt_arch="x86"; else opt_arch="ppc"; #endif #else opt_arch="x86"; #endif opt_debug=true; opt_release=false; for( int k=1;k ids; splitModule( opt_module,ids ); int k; for( k=0;k &ids ){ if( !mod.size() ) return; int i; while( (i=mod.find('.'))!=string::npos ){ ids.push_back(mod.substr(0,i)); mod=mod.substr(i+1); } ids.push_back(mod); } string moduleIdent( string mod ){ int i=mod.rfind('.'); return i==string::npos ? mod : mod.substr(i+1); } string moduleInterface( string mod ){ string path=modulePath( mod,false )+"/"+moduleIdent(mod)+config_mung+".i"; if( ftime( path ) ) return path; fail( "Can't find interface for module '%s'",mod.c_str() ); return ""; /* string path=modulePath( mod,false ); string d_ext=".debug."+env_platform+"."+opt_arch+".i"; string r_ext=".release."+env_platform+"."+opt_arch+".i"; string f1=path+"/"+moduleIdent(mod)+r_ext; string f2=path+"/"+moduleIdent(mod)+d_ext; if( opt_debug ) std::swap(f1,f2); if( ftime(f1) ) return f1; if( ftime(f2) ) return f2; fail( "Can't find interface for module '%s'",mod.c_str() ); return ""; */ } void enumModules( string mod,vector &mods ){ string path=modulePath( mod,false ); DIR *d=opendir( path.c_str() ); if( !d ) return; string d_ext=".debug."+env_platform+"."+opt_arch+".i"; string r_ext=".release."+env_platform+"."+opt_arch+".i"; while( dirent *e=readdir( d ) ){ string f=e->d_name; if( getext(f)!="mod" ) continue; string id=stripall(f); string tmod=mod.size() ? mod+"."+id : id; enumModules( tmod,mods ); string path=modulePath( tmod,false )+"/"+id; if( ftime(path+d_ext) || ftime(path+r_ext) ){ mods.push_back( tmod ); } } closedir(d); } time_t ftime( string path ){ struct stat st; fixpath(path); if( !stat( path.c_str(),&st ) ) return st.st_mtime; return 0; } string getcwd(){ char buf[256]; if( !getcwd( buf,255 ) ) fail( "getcwd failed" ); string path=string(buf); fixpath(path); return buf; } void setcwd( string path ){ fixpath(path); chdir( path.c_str() ); } string getdir( string path ){ fixpath(path); int n=path.rfind( '/' ); if( n==string::npos ) return ""; return path.substr(0,n); } string getext( string path ){ fixpath(path); int n=path.rfind( '.' ); if( n==string::npos ) return ""; if( path.find( '/',n+1 )!=string::npos ) return ""; return path.substr(n+1); } string stripdir( string path ){ fixpath(path); int n=path.rfind( '/' ); if( n==string::npos ) n=path.rfind( '\\' ); if( n==string::npos ) return path; return path.substr(n+1); } string stripext( string path ){ fixpath(path); int n=path.rfind( '.' ); if( n==string::npos ) return path; if( path.find( '/',n+1 )!=string::npos ) return path; if( path.find( '\\',n+1 )!=string::npos ) return path; return path.substr(0,n); } string stripall( string path ){ return stripext(stripdir(path)); } string realpath( string path ){ fixpath(path); string dir=getdir(path); string file=stripdir(path); static char buf[MAX_PATH+8]; if( dir.size() ){ // printf( "Calling realpath( %s ), MAX_PATH=%i PATH_MAX=%i\n",dir.c_str(),MAX_PATH,PATH_MAX );fflush( stdout ); if( !_realpath( dir.c_str(),buf ) ){ fail( "realpath failed for %s",path.c_str() ); } dir=buf; }else{ dir=getcwd(); } fixpath( dir ); return dir+"/"+file; } string tolower( string t ){ for( int k=0;k='A' ) c-=('A'-'0'-10); n=n*16+(c-'0'); } }else{ for( ;i0 ? n : -n; } double tofloat( string t ){ double zero=0.0; string q=tolower(t); if( q=="nan#" ){ return 0.0/zero; }else if( q=="inf#" || q=="+inf#" ){ return 1.0/zero; }else if( q=="-inf#" ){ return -1.0/zero; } return atof(t.c_str()); } string fromint( int64 n ){ // Can't use %lld 'coz it doesn't work on mingw! // char buf[64]; // sprintf( buf,"%lld",n ); // return buf; char buf[64],*p=buf+64; int neg=n<0; if( neg ){ n=-n; if( n<0 ) return "-9223372036854775808"; } *--p=0; do{ *--p=n%10+'0'; }while(n/=10); if( neg ) *--p='-'; return p; } string fromfloat( float n ){ char buf[64]; if( isnan(n) ){ sprintf( buf,"nan" ); }else if( isinf(n) ){ if( n>0 ) sprintf( buf,"inf" ); else sprintf( buf,"-inf" ); }else{ sprintf( buf,"%#.9g",n ); } return buf; } string fromdouble( double n ){ char buf[64]; if( isnan(n) ){ sprintf( buf,"nan" ); }else if( isinf(n) ){ if( n>0 ) sprintf( buf,"inf" ); else sprintf( buf,"-inf" ); }else{ sprintf( buf,"%#.17lg",n ); } return buf; } string tostring( bstring w ){ string t; t.resize(w.size()); for( int k=0;k static int sysctlbyname_with_pid (const char *name, pid_t pid, void *oldp, size_t *oldlenp, void *newp, size_t newlen) { if (pid == 0) { if (sysctlbyname(name, oldp, oldlenp, newp, newlen) == -1) { return -1; } }else{ int mib[CTL_MAXNAME]; size_t len = CTL_MAXNAME; if (sysctlnametomib(name, mib, &len) == -1) { return -1; } mib[len] = pid; len++; if (sysctl(mib, len, oldp, oldlenp, newp, newlen) == -1) { return -1; } } return 0; } int is_pid_native (pid_t pid) { int ret = 0; size_t sz = sizeof(ret); if (sysctlbyname_with_pid("sysctl.proc_native", pid, &ret, &sz, NULL, 0) == -1) { if (errno == ENOENT) { // sysctl doesn't exist, which means that this version of Mac OS // pre-dates Rosetta, so the application must be native. return 1; } return -1; } return ret; } #endif