0960b72002-09-09Marcus Comstedt // -*- Pike -*-
0d03312003-08-24Martin Nilsson #pike __REAL_VERSION__
7aaaf12008-06-20Stephen R. van den Berg constant version = sprintf("%d.%d.%d",(int)__REAL_VERSION__,__REAL_MINOR__,__REAL_BUILD__);
6148162002-12-14Martin Nilsson constant description = "Pike module installer.";
0960b72002-09-09Marcus Comstedt  // Source directory string srcdir;
a1a48e2003-03-20Dan Nelson string make=getenv("MAKE")||"make";
0960b72002-09-09Marcus Comstedt string make_flags=""; string include_path=master()->include_prefix;
ad1d632003-02-22Marcus Comstedt string config_args="";
a98cf82002-09-13Marcus Comstedt #ifdef NOT_INSTALLED string src_path=combine_path(__FILE__,"../../../../../src"); string bin_path=combine_path(src_path,"../bin"); #else string src_path=include_path; string bin_path=include_path;
2654632003-11-06Martin Nilsson #endif
0418252003-10-30H. William Welliver III 
c2b0632003-10-30H. William Welliver III // this is not the ideal location for all systems, but it's a start.
3560352008-07-08Henrik Grubbström (Grubba) string local_module_path=combine_path(getenv("HOME")||"","lib/pike/modules");
1be98f2008-06-29Per Hedbor bool old_style_module = false;
0418252003-10-30H. William Welliver III // we prefer the last element, because if there are more than one // master() puts the lib/modules path last. string system_module_path=master()->system_module_path[-1];
0df63c2003-11-04H. William Welliver III  // where do we install the documentation?
9443dd2006-04-22Henrik Grubbström (Grubba) string system_doc_path = master()->doc_prefix;
0df63c2003-11-04H. William Welliver III 
a98cf82002-09-13Marcus Comstedt string run_pike;
0960b72002-09-09Marcus Comstedt  #define NOT 0 #define AUTO 1 #define ALWAYS 2 mapping(string:int) run= ([ "automake":AUTO, "autoheader":AUTO, "autoconf":AUTO, "configure":AUTO, "depend":AUTO, "make":AUTO,
9443dd2006-04-22Henrik Grubbström (Grubba) ]);
0960b72002-09-09Marcus Comstedt  mapping(string:string) specs = ([]); void load_specs(string fn) { string name, val; foreach(Stdio.read_file(fn)/"\n", string line) if(2==sscanf(line, "%s=%s", name, val) && !specs[name]) specs[name] = val;
9443dd2006-04-22Henrik Grubbström (Grubba)  // We should try to find the core autodoc file if (!system_doc_path) { if(file_stat(combine_path(system_module_path, "../../doc/src/core_autodoc.xml"))) system_doc_path=combine_path(system_module_path, "../../doc"); else if(file_stat(combine_path(system_module_path, "../../../doc/pike/src/core_autodoc.xml"))) system_doc_path=combine_path(system_module_path, "../../../doc/pike"); else if(file_stat(combine_path(system_module_path, "../../doc"))) system_doc_path=combine_path(system_module_path, "../../doc"); else if(file_stat(combine_path(system_module_path, "../../../doc/pike"))) system_doc_path=combine_path(system_module_path, "../../../doc/pike"); else { // No autodoc file or directory, but we set this path as doc path anyway. system_doc_path = combine_path(system_module_path, "../../doc"); } }
0960b72002-09-09Marcus Comstedt } array(string) do_split_quoted_string(string s) { array(string) res = Process.split_quoted_string(s); if(sizeof(res) == 1 && res[0] == "" && search(s, "'")<0 && search(s, "\"")<0) return ({}); else return res; } string fix(string path) {
7c056d2003-06-06Johan Sundström  if(!has_value(path, "$src")) return path;
0960b72002-09-09Marcus Comstedt  if(!srcdir) { string s=Stdio.read_file("Makefile"); if(s) { sscanf(s,"%*s\nSRCDIR=%s\n",srcdir); }
1be98f2008-06-29Per Hedbor  if(!srcdir)
0960b72002-09-09Marcus Comstedt  { srcdir="."; }
3524712015-05-26Martin Nilsson 
0960b72002-09-09Marcus Comstedt  if(!srcdir) { werror("You must provide --source=<source dir>\n"); exit(1); } } return replace(path,"$src",srcdir); } void do_zero() { foreach(indices(run), string x) if(run[x]==AUTO) m_delete(run,x); } int max_time_of_files(string ... a) { int t=0;
7c056d2003-06-06Johan Sundström  foreach(a, string file) { Stdio.Stat s = file_stat(fix(file)); if(!s) return 0; t = max(t, s->mtime); }
0960b72002-09-09Marcus Comstedt  return t; } int just_run(mapping options, string ... cmd) { werror(" %s\n",cmd*" ");
f6883f2003-02-14Marcus Comstedt  return Process.create_process(cmd,(["env":getenv()])|options)->wait();
0960b72002-09-09Marcus Comstedt } void run_or_fail(mapping options,string ... cmd) { werror(" %s\n",cmd*" ");
f6883f2003-02-14Marcus Comstedt  int ret=Process.create_process(cmd,(["env":getenv()])|options)->wait();
0960b72002-09-09Marcus Comstedt  if(ret) exit(ret); } void do_make(array(string) cmd) { int tmp1;
c2b0632003-10-30H. William Welliver III  string lmp;
e9bce72003-11-07H. William Welliver III  string full_srcdir;
c2b0632003-10-30H. William Welliver III  if(search(cmd, "testsuite")!=-1) lmp="./plib/modules";
2654632003-11-06Martin Nilsson  else lmp = local_module_path;
3524712015-05-26Martin Nilsson 
e9bce72003-11-07H. William Welliver III  if(srcdir !=".") full_srcdir=srcdir + "/"; else full_srcdir=getcwd() + "/";
c2b0632003-10-30H. William Welliver III 
1be98f2008-06-29Per Hedbor  array extra_args = ({});
3524712015-05-26Martin Nilsson 
1be98f2008-06-29Per Hedbor  if( old_style_module ) { extra_args = ({"PIKE_INCLUDES=-I"+include_path, "PIKE_SRC_DIR="+src_path, "BUILD_BASE="+include_path,
e0cb602008-06-29Marcus Agehall  "MODULE_BASE="+combine_path(include_path, "modules"),
1be98f2008-06-29Per Hedbor  "TMP_BINDIR="+bin_path, "SRCDIR="+fix("$src"), "FULL_SRCDIR=" + full_srcdir, "TMP_MODULE_BASE=.", "PIKE_EXTERNAL_MODULE=pike_external_module", "CORE_AUTODOC_PATH=" + combine_path(system_doc_path, "src/core_autodoc.xml"), "SYSTEM_DOC_PATH=" + system_doc_path + "/", "SYSTEM_MODULE_PATH=" + system_module_path, "LOCAL_MODULE_PATH=" + lmp, "RUNPIKE="+run_pike,
51a3302012-08-06Bill Welliver  "FINAL_PIKE="+run_pike,
1be98f2008-06-29Per Hedbor  }); } else {
3647eb2008-06-29Per Hedbor  extra_args = ({ "PIKE="+run_pike, "SRCDIR="+fix("$src"), "MODULE_INSTALL_DIR="+combine_path(__FILE__,"../../.."), "LOCAL_MODULE_PATH=" + lmp, });
1be98f2008-06-29Per Hedbor  }
3524712015-05-26Martin Nilsson 
1be98f2008-06-29Per Hedbor  array(string) makecmd=({make})+do_split_quoted_string(make_flags)+extra_args+cmd;
0960b72002-09-09Marcus Comstedt  if(tmp1=max_time_of_files("Makefile")) { rm("remake"); tmp1=just_run(([]),@makecmd); if(tmp1) { if(max_time_of_files("remake")) { run_or_fail(([]),@makecmd); }else{ exit(tmp1); } } }else{ werror("No Makefile.\n"); } } int main(int argc, array(string) argv) {
e0cb602008-06-29Marcus Agehall  string specspath=combine_path(include_path, "specs");
d0c6212008-03-28Bertrand Lupart  if(!Stdio.is_file(specspath)) { werror("Missing specs file at %s\n",specspath); return 1; }
a98cf82002-09-13Marcus Comstedt  run_pike = master()->_pike_file_name; #ifdef NOT_INSTALLED run_pike += " -DNOT_INSTALLED"; #endif #ifdef PRECOMPILED_SEARCH_MORE run_pike += " -DPRECOMPILED_SEARCH_MORE"; #endif if(master()->_master_file_name) run_pike += " -m"+master()->_master_file_name;
e46bb62002-09-14Marcus Comstedt  putenv("RUNPIKE", run_pike);
a98cf82002-09-13Marcus Comstedt 
d0c6212008-03-28Bertrand Lupart  load_specs(specspath);
3524712015-05-26Martin Nilsson 
0960b72002-09-09Marcus Comstedt  foreach(Getopt.find_all_options(argv,aggregate( ({"autoconf",Getopt.NO_ARG,({"--autoconf"}) }), ({"configure",Getopt.NO_ARG,({"--configure"}) }), ({"autoheader",Getopt.NO_ARG,({"--autoheader"}) }), ({"automake",Getopt.NO_ARG,({"--automake"}) }), ({"depend",Getopt.NO_ARG,({"--depend"}) }), ({"all",Getopt.NO_ARG,({"--all"}) }), ({"make",Getopt.NO_ARG,({"--make"}) }), ({"auto",Getopt.NO_ARG,({"--auto"}) }), ({"source",Getopt.HAS_ARG,({"--source"}) }), ({"query",Getopt.HAS_ARG,({"--query"}) }),
ec9d4c2003-04-07Martin Nilsson  ({"config_args",Getopt.HAS_ARG,({"--configure-args"}) }), ({"help",Getopt.NO_ARG,({"--help"}) }),
0960b72002-09-09Marcus Comstedt  )),array opt) { switch(opt[0]) { case "query":
d22f9c2003-04-04Martin Nilsson  if(opt[1]=="specs") write("%O\n", specs); else if(stringp(this[opt[1]])) write("%s\n", this[opt[1]]); else write("Unknown variable %s.\n", opt[1]);
0960b72002-09-09Marcus Comstedt  exit(0);
ec9d4c2003-04-07Martin Nilsson  case "help":
4ceddc2003-04-07Martin Nilsson  write(help, version);
ec9d4c2003-04-07Martin Nilsson  exit(0);
0960b72002-09-09Marcus Comstedt 
ad1d632003-02-22Marcus Comstedt  case "config_args": config_args=opt[1]; break;
0960b72002-09-09Marcus Comstedt  case "source": srcdir=opt[1]; break; case "automake": run->automake=ALWAYS; do_zero(); break; case "autoheader": run->autoheader=ALWAYS; do_zero(); break; case "autoconf": run->autoconf=ALWAYS; do_zero(); break; case "configure": run->configure=ALWAYS; do_zero(); break; case "make": run->make=ALWAYS; do_zero(); break; case "depend": run->depend=ALWAYS; do_zero(); break;
3524712015-05-26Martin Nilsson  case "all":
0960b72002-09-09Marcus Comstedt  run->depend=run->autoheader=run->autoconf=run->configure=run->make=ALWAYS; break;
3524712015-05-26Martin Nilsson 
0960b72002-09-09Marcus Comstedt  case "auto": run->depend=run->autoheader=run->autoconf=run->configure=run->make=AUTO; break; } } argv=Getopt.get_args(argv);
1be98f2008-06-29Per Hedbor  string configure_args="";
3524712015-05-26Martin Nilsson 
1be98f2008-06-29Per Hedbor  foreach( argv, string arg ) if( sscanf( arg, "CONFIGUREARGS=%s", configure_args ) ) argv-=({arg});
0960b72002-09-09Marcus Comstedt 
1be98f2008-06-29Per Hedbor  string configure_in = "$src/configure.in"; if( max_time_of_files( configure_in ) < max_time_of_files( "$src/configure.ac" ) ) configure_in = "$src/configure.ac" ;
0960b72002-09-09Marcus Comstedt  int tmp1;
1be98f2008-06-29Per Hedbor 
0960b72002-09-09Marcus Comstedt  if(run->automake) {
1be98f2008-06-29Per Hedbor  if(tmp1=max_time_of_files("$src/Makefile.am",configure_in))
0960b72002-09-09Marcus Comstedt  {
3524712015-05-26Martin Nilsson  write("** Running automake\n");
0960b72002-09-09Marcus Comstedt  if(run->automake == ALWAYS || max_time_of_files("$src/Makefile.in") < tmp1) {
594b9e2009-10-24H. William Welliver III  run_or_fail((["cwd":srcdir]),"aclocal"); run_or_fail((["cwd":srcdir]),"automake");
0960b72002-09-09Marcus Comstedt  rm(fix("$src/stamp-h.in")); } } }
3524712015-05-26Martin Nilsson 
1be98f2008-06-29Per Hedbor  if( max_time_of_files( configure_in ) < max_time_of_files( "$src/configure.ac" ) ) configure_in = "$src/configure.ac" ; string configure_content = Stdio.read_file(fix(configure_in))||""; old_style_module = has_value(configure_content, "AC_MODULE_INIT" ); if( old_style_module ) { write("** Old style module\n"); }
0960b72002-09-09Marcus Comstedt 
1be98f2008-06-29Per Hedbor  string stamp_file = "$src/stamp-h.in"; if( sscanf( configure_content, "%*sAC_CONFIG_HEADER(%[^)])", stamp_file ) == 2 ) stamp_file="$src/"+stamp_file+".in";
3524712015-05-26Martin Nilsson 
0960b72002-09-09Marcus Comstedt  if(run->autoheader) {
1be98f2008-06-29Per Hedbor  if(tmp1=max_time_of_files("$src/acconfig.h",configure_in))
0960b72002-09-09Marcus Comstedt  {
1be98f2008-06-29Per Hedbor  if(run->autoheader==ALWAYS || max_time_of_files(stamp_file) <= tmp1)
0960b72002-09-09Marcus Comstedt  {
594b9e2009-10-24H. William Welliver III  run_or_fail((["cwd":srcdir]),"autoheader");
1be98f2008-06-29Per Hedbor  if( stamp_file == "$src/stamp-h.in" ) Stdio.write_file(fix(stamp_file),"foo\n");
0960b72002-09-09Marcus Comstedt  } } }
b0b5522011-11-19Marcus Comstedt  if(run->configure && run->autoconf != ALWAYS && old_style_module) { string cfscript = Stdio.read_file(fix("$src/configure")); if(cfscript && !has_value(cfscript, "propagated_variables")) { write("** WARNING: Incompatible configure script detected, deleting it\n"); rm(fix("$src/configure")); } }
0960b72002-09-09Marcus Comstedt  if(run->autoconf) {
1be98f2008-06-29Per Hedbor  if(tmp1=max_time_of_files(configure_in))
0960b72002-09-09Marcus Comstedt  {
1be98f2008-06-29Per Hedbor  if(run->autoconf==ALWAYS || max_time_of_files("$src/configure") <= tmp1)
0960b72002-09-09Marcus Comstedt  {
1be98f2008-06-29Per Hedbor  if( old_style_module ) {
b0b5522011-11-19Marcus Comstedt  if(file_stat(fix("$src/aclocal.m4"))) { write("** WARNING: aclocal.m4 detected in src tree, deleting it\n"); rm(fix("$src/aclocal.m4")); }
1be98f2008-06-29Per Hedbor  write("** Running autoconf (with extra compat macros)\n"); string data = Process.popen("autoconf --version"); data = (data/"\n")[0]; float v; sscanf(data, "%*s%f", v); // If we fail to determine the autoconf version we assume // yet another incompatble autoconf change. if(!v || v>2.52)
594b9e2009-10-24H. William Welliver III  run_or_fail((["cwd":srcdir]),"autoconf","--include="+src_path);
1be98f2008-06-29Per Hedbor  else
594b9e2009-10-24H. William Welliver III  run_or_fail((["cwd":srcdir]),"autoconf","--localdir="+src_path);
1be98f2008-06-29Per Hedbor  }
ec9d4c2003-04-07Martin Nilsson  else
1be98f2008-06-29Per Hedbor  { write("** Running autoconf\n");
594b9e2009-10-24H. William Welliver III  run_or_fail( (["cwd":srcdir]), "autoconf" );
1be98f2008-06-29Per Hedbor  }
0960b72002-09-09Marcus Comstedt  } } } if(run->configure) {
1be98f2008-06-29Per Hedbor  if( tmp1=max_time_of_files("$src/configure") )
0960b72002-09-09Marcus Comstedt  {
1be98f2008-06-29Per Hedbor  if(run->configure == ALWAYS || (max_time_of_files("Makefile") <= tmp1))
0960b72002-09-09Marcus Comstedt  {
1be98f2008-06-29Per Hedbor  if( old_style_module ) {
f543d22008-06-29Marcus Comstedt  if(!max_time_of_files("propagated_variables")) { write("** Copying propagated_variables\n"); Stdio.write_file("propagated_variables", Stdio.read_file(combine_path(include_path, "propagated_variables"))); }
1be98f2008-06-29Per Hedbor  write("** Running configure (with extra compat args)\n"); array(string) cfa = do_split_quoted_string(specs->configure_args||""); mapping(string:string) cfa_env = ([]); foreach(cfa; int i; string arg)
f6883f2003-02-14Marcus Comstedt  {
1be98f2008-06-29Per Hedbor  string key, val; if(2==sscanf(arg, "%[a-zA-Z0-9_]=%s", key, val)) { cfa_env[key] = val; cfa[i] = 0; }
3e97e22003-02-20Marcus Comstedt  }
1be98f2008-06-29Per Hedbor  run_or_fail((["env":getenv()|cfa_env| (specs & ({"CC","CFLAGS","CPPFLAGS","CPP", "LDFLAGS","LDSHARED"}))| (["PIKE_SRC_DIR":src_path, "BUILD_BASE":include_path])]), fix("$src/configure"), "--cache-file=./config.cache", @(cfa-({0})), @do_split_quoted_string(config_args)); } else { write("** Running configure\n");
3647eb2008-06-29Per Hedbor  run_or_fail( ([ "env":getenv()| ([ "PIKE":run_pike, "MODULE_INSTALL_DIR":combine_path(__FILE__,"../../.."), "LOCAL_MODULE_PATH":local_module_path, ]) ]), fix("$src/configure"), @do_split_quoted_string(configure_args));
3e97e22003-02-20Marcus Comstedt  }
0960b72002-09-09Marcus Comstedt  } } }
1be98f2008-06-29Per Hedbor  if( old_style_module && run->depend )
0960b72002-09-09Marcus Comstedt  {
bbf9a32002-09-12Marcus Comstedt  if(!max_time_of_files("$src/dependencies") || run->depend == ALWAYS)
0960b72002-09-09Marcus Comstedt  {
1be98f2008-06-29Per Hedbor  write("** Running make depend\n");
0960b72002-09-09Marcus Comstedt  // Create an empty file first.. Stdio.write_file(srcdir+"/dependencies",""); do_make( ({"depend"}) ); do_make( ({"Makefile"}) ); } } if(run->make) {
1be98f2008-06-29Per Hedbor  write("** Running "+argv[1..]*" ");
0960b72002-09-09Marcus Comstedt  do_make(argv[1..]); } }
ec9d4c2003-04-07Martin Nilsson 
4ceddc2003-04-07Martin Nilsson constant help=#"Pike module installer %s.
ec9d4c2003-04-07Martin Nilsson module <options> <arguments passed to make> Information options: --help Prints this text --query=X Shows the value of setting X. Possible settings are include_path, configure_command, src_path, bin_path,
0418252003-10-30H. William Welliver III  system_module_path, make and specs.
ec9d4c2003-04-07Martin Nilsson  Stage selection: If none of these stages are selected, all of them will be run, given that the timestamps of the dependncies indicates that it is needed. If one or more stage is selected, these stages will always run, but none of the unselected ones. --automake Runs aclocal and automake. --autoheader Runs autoheader. --autoconf Runs autoconf. --configure Runs configure. --depend Runs make depend and make Makefile. --make Runs make. --all Runs all of the above. --auto Sets all stages to auto (default). Environment: --source=X Path to the source directory. --configure-args Additional arguments to configure. ";