/* * Neko Tools * Copyright (c)2005 Motion-Twin * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License or the LICENSE file for more details. */ // CONFIG osx_universal = $loader.args[0] == "-osx-universal"; // PRIMITIVES system = $loader.loadprim("std@sys_string",0)(); chdir = $loader.loadprim("std@set_cwd",1); readdir = $loader.loadprim("std@sys_read_dir",1); stdin = $loader.loadprim("std@file_stdin",0)(); read_char = $loader.loadprim("std@file_read_char",1); ftype = $loader.loadprim("std@sys_file_type",1); command = $loader.loadprim("std@sys_command",1); exists = $loader.loadprim("std@sys_exists",1); split = $loader.loadprim("std@string_split",2); mkdir = $loader.loadprim("std@sys_create_dir",2); delete = $loader.loadprim("std@file_delete",1); getenv = $loader.loadprim("std@get_env",1); buffer_new = $loader.loadprim("std@buffer_new",0); buffer_add_char = $loader.loadprim("std@buffer_add_char",2); buffer_string = $loader.loadprim("std@buffer_string",1); // LIBS DATAS libs = { mod_neko => { src => $array("mod_neko","cgi","../../vm/stats"), inc => "httpd.h", incname => "Apache 1.3.x" }, mod_neko2 => { src => $array("mod_neko","cgi","../../vm/stats"), inc => $array("httpd.h","apr.h"), incname => "Apache 2.2.x", cflags => "-D_LARGEFILE64_SOURCE", realdir => "mod_neko", apache2 => true, }, mysql => { src => $array("mysql"), inc => "mysql.h", incname => "MySQL 4.+" lib => "libmysqlclient_r.a", lparams => "-lz -lssl" }, regexp => { src => $array("regexp"), inc => "pcre.h", incname => "PCRE", lparams => "-lpcre", }, zlib => { src => $array("zlib"), inc => "zlib.h", incname => "LibZ", lparams => "-lz", }, sqlite => { src => $array("sqlite"), inc => "sqlite3.h", incname => "Sqlite 3", lparams => "-lsqlite3", }, ui => { src => $array("ui"), inc => switch system { "Mac" => "Carbon.h" default => "gtk/gtk.h" }, incname => switch system { "Mac" => "Carbon" default => "GTK+2.0" }, cflags => switch system { "Mac" => "" default => "`pkg-config --cflags gtk+-2.0`" }, lparams => switch system { "Mac" => "-framework Carbon" default => "`pkg-config --libs gtk+-2.0` -lgthread-2.0" }, }, mod_tora => { src => $array("mod_tora","protocol"), inc => "httpd.h", incname => "Apache 1.3.x" }, mod_tora2 => { src => $array("mod_tora","protocol"), inc => $array("httpd.h","apr.h"), incname => "Apache 2.2.x", cflags => "-D_LARGEFILE64_SOURCE", realdir => "mod_tora", apache2 => true, }, } // PLATFORM cflags = "-O3 -fPIC -pthread"; cc = getenv("CC"); if( cc == null ) cc = "gcc"; linkcmd = switch system { "BSD" => "ld" default => cc }; linkneko = "-lneko"; linkoptions = switch system { "Mac" => "-bundle -undefined dynamic_lookup -L../../bin -pthread" default => "-shared -L../../bin -pthread" }; nekovm = switch system { "Windows" => "..\\bin\\neko" default => "../bin/neko" }; if( osx_universal ) { cflags += " -arch ppc -arch i386"; linkoptions += " -arch ppc -arch i386"; libs.regexp.lparams = null; libs.regexp.lib = "osx_universal/libpcre.a"; libs.zlib.lparams = null; libs.zlib.lib = "osx_universal/libz.a"; libs.mysql.lparams = null; libs.mysql.lib = "osx_universal/libmysqlclient.a"; } // COMMANDS includes = $array( "../../vm", ); search_includes = function(isap2) { var inc1 = $array( "/usr/include", "/usr/local/include/mysql", "/usr/include/mysql", "/usr/local/include", "/usr/include/gtk-2.0", "/opt/local/include", "/opt/local/include/mysql", "/Developer/Headers/FlatCarbon", ); var inc2; if( isap2 ) inc2 = $array( "/usr/local/apache2/include", "/usr/include/apache2", "/opt/local/apache2/include", "/usr/include/apr-1", "/usr/include/apr-1.0", "/usr/include/apr-0", "/usr/local/include/apr-1", "/usr/local/include/apr-1.0", "/usr/local/include/apr-0", "/opt/local/include/apr-1", "/opt/local/include/apr-1.0", "/opt/local/include/apr-0" ); else inc2 = $array( "/usr/apache/include", "/usr/include/apache-1.3", "/usr/include/httpd", "/opt/local/include/apache", "/usr/local/apache/include" ); return $aconcat($array(inc1,inc2)); } libraries = $array( "../../libs/include", "/usr/lib", "/usr/lib/mysql", "/usr/local/lib", "/usr/local/lib/mysql", "/opt/local/lib", "/opt/local/lib/mysql", ); exec = function(cmd) { $print(cmd,"\n"); var ecode = command(cmd); if( ecode != 0 ) $throw("Error "+ecode+" : aborted"); } make = function(sep,arr) { var i = 0; var s = ""; while( i < $asize(arr) ) { s = s + sep+arr[i]+" "; i = i + 1; } return s; } append = function(arr,s) { var n = $asize(arr); var arr2 = $amake(n+1); $ablit(arr2,0,arr,0,n); arr2[n] = s; return arr2; } readline = function() { $print("> "); var b = buffer_new(); do { var c = read_char(stdin); if( c == 10 ) break; buffer_add_char(b,c); } while(true); return buffer_string(b); } request_include = function(incl) { var s = readline(); if( s == "s" ) return null; return append(incl,s); } request_lib = function() { var s = readline(); if( s == "s" ) return false; libraries = append(libraries,s); return true; } find_file = function(f,arr) { var i = 0; while( i < $asize(arr) ) { if( exists(arr[i]+"/"+f) ) return arr[i]; i = i + 1; } return null; } map = function(a,f) { var i = 0; var l = $asize(a); var a2 = $amake(l); while( i < l ) { a2[i] = f(a[i]); i = i + 1; } return a2; } copy = function(file,path) { switch system { "Windows" => { slashes = function(f) { var c = split(f,"/"); var b = ""; while( c != null ) { b = b + c[0]; if( c[1] != null ) b = b + "\\"; c = c[1]; } return b; } exec("copy "+slashes(file)+" "+slashes(path)+" >NUL"); } default => exec("cp "+file+" "+path) }; } compile = function(file,eflags,incl) { exec(cc+" "+cflags+" "+eflags+" -c "+make("-I",includes)+make("-I",incl)+file+".c"); } nekoboot = function(file) { exec(nekovm+" tools/nekoboot "+file); delete(file); } link = function(files,target,params1,params2) { files = make("",map(files,function(f){ f + ".o" })); var opt = linkoptions; if( target != "mod_tora" && target != "mod_tora2" ) opt += " "+linkneko; var cmd = linkcmd+" "+opt+" "+make("-L",libraries); var out = " -o ../../bin/"+target+".ndll "; exec(cmd+out+params1+files+params2); } // COMPILATION loop_include = function(data,i,incl) { var dir; while( (dir = find_file(data.inc[i],incl)) == null ) { $print("The file "+data.inc[i]+" provided when installing "+data.incname+" was not found\n"); $print("Please enter a valid include path to look for it\n"); $print("Or 's' to skip this library\n"); incl = request_include(incl); if( incl == null ) return null; } return dir; } compile_lib = function(name,data) { $print("Compiling ",name,"...\n"); var incl = search_includes(data.apache2); var dirs = $array(); if( $typeof(data.inc) == $tstring ) data.inc = $array(data.inc); var i = 0; while( i < $asize(data.inc) ) { var dir = loop_include(data,i,incl); if( dir == null ) return; dirs = append(dirs,dir); i += 1; } var linklib = data.lib; var dir = ""; if( linklib == null ) linklib = ""; else { while( (dir = find_file(linklib,libraries)) == null ) { $print("The file "+linklib+" provided when installing "+data.incname+" was not found\n"); $print("Please enter a valid include path to look for it\n"); $print("Or 's' to skip this library\n"); if( $not(request_lib()) ) return; } linklib = dir + "/" + linklib; } var i = 0; var dir = data.realdir; if( dir == null ) dir = name; chdir(dir); while( i < $asize(data.src) ) { compile(data.src[i],if( data.cflags != null ) data.cflags else "", dirs); i = i + 1; } link(data.src,name,if( data.lparams != null ) { data.lparams+" " } else ""," "+linklib); chdir(".."); data.built = true; } // MAIN LOOP rights = 493; // octal 755 base = "../bin"; try { mkdir(base,rights); } catch e { }; copy("../boot/nekoc.n",base); copy("../boot/nekoml.n",base); // compile some neko sources exec(nekovm+" nekoc tools/test.neko"); copy("tools/test.n",base); exec(nekovm+" nekoc tools/nekoboot.neko"); exec(nekovm+" nekoml -nostd -p tools Tools.nml"); exec(nekovm+" nekoc -link tools/nekotools.n Tools"); copy("tools/nekotools.n",base); nekoboot(base+"/nekoc.n"); nekoboot(base+"/nekoml.n"); nekoboot(base+"/nekotools.n"); // compile libs chdir("../libs"); var liblist = $objfields(libs); var i = 0; while( i < $asize(liblist) ) { var l = liblist[i]; compile_lib($field(l),$objget(libs,l)); i += 1; } // rebuild nekoml.std (needs zlib) chdir("../src"); if( libs.zlib.built ) exec(nekovm+" nekoml -nostd neko/Main.nml nekoml/Main.nml core/*.nml -pack ../bin/nekoml.std"); // END $print("Install done, all files are available in /bin directory\n");