1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
  
33
  
34
  
35
  
36
  
37
  
38
  
39
  
40
  
41
  
42
  
43
  
44
  
45
  
46
  
47
  
48
  
49
  
50
  
51
  
52
  
53
  
54
  
55
  
56
  
57
  
58
  
59
  
60
  
61
  
62
  
63
  
64
  
65
  
66
  
67
  
68
  
69
  
70
  
71
  
72
  
73
  
74
  
75
  
76
  
77
  
78
  
79
  
80
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
119
  
120
  
121
  
122
  
123
  
124
  
125
  
126
  
127
  
128
  
129
  
130
  
131
  
132
  
133
  
134
  
135
  
136
  
137
  
138
  
139
  
140
  
141
  
142
  
143
  
144
  
145
  
146
  
147
  
148
  
149
  
150
  
151
  
152
  
153
  
154
  
155
  
156
  
157
  
158
  
159
  
160
  
161
  
162
  
163
  
164
  
165
  
166
  
167
  
168
  
169
  
170
  
171
  
172
  
173
  
174
  
175
  
176
  
177
  
178
  
179
  
180
  
181
  
182
  
183
  
184
  
185
  
186
  
187
  
188
  
189
  
190
  
191
  
192
  
193
  
194
  
195
  
196
  
197
  
198
  
199
  
200
  
201
  
202
  
203
  
204
  
205
  
206
  
207
  
208
  
209
  
210
  
211
  
212
  
213
  
214
  
215
  
216
  
217
  
218
  
219
  
220
  
221
  
222
  
223
  
224
  
225
  
226
  
227
  
228
  
229
  
230
  
231
  
232
  
233
  
234
  
235
  
236
  
237
  
238
  
239
  
240
  
241
  
242
  
243
  
244
  
245
  
246
  
247
  
248
  
249
  
250
  
251
  
252
  
253
  
254
  
255
  
256
  
257
  
258
  
259
  
260
  
261
  
262
  
263
  
264
  
265
  
266
  
267
  
268
  
269
  
270
  
271
  
272
  
273
  
274
  
275
  
276
  
277
  
278
  
279
  
280
  
281
  
282
  
283
  
284
  
285
  
286
  
287
  
288
  
289
  
290
  
291
  
292
  
293
  
294
  
295
  
296
  
297
  
298
  
299
  
300
  
301
  
302
  
303
  
304
  
305
  
306
  
307
  
308
  
309
  
310
  
311
  
312
  
313
  
314
  
315
  
316
  
317
  
318
  
319
  
320
  
321
  
322
  
323
  
324
  
325
  
326
  
327
  
328
  
329
  
330
  
331
  
332
  
333
  
334
  
335
  
336
  
337
  
338
  
339
  
340
  
341
  
342
  
343
  
344
  
345
  
346
  
347
  
348
  
349
  
350
  
351
  
352
  
353
  
354
  
355
  
356
  
357
  
358
  
359
  
360
  
361
  
362
  
363
  
364
  
365
  
366
  
367
  
368
  
369
  
370
  
371
  
372
  
373
  
374
  
375
  
376
  
377
  
378
  
379
  
380
  
381
  
382
  
383
  
384
  
385
  
386
  
387
  
388
  
389
  
390
  
391
  
392
  
393
  
394
  
395
  
396
  
397
  
398
  
399
  
400
  
401
  
402
  
403
  
404
  
405
  
406
  
407
  
408
  
409
  
410
  
411
  
412
  
413
  
414
  
415
  
416
  
417
  
418
  
419
  
420
  
421
  
422
  
423
  
424
  
425
  
426
  
427
  
428
  
429
  
430
  
431
  
432
  
433
  
434
  
435
  
436
  
437
  
438
  
439
  
440
  
441
  
442
  
443
  
444
  
445
  
446
  
447
  
448
  
449
  
450
  
451
  
452
  
453
  
454
  
455
  
456
  
457
  
458
  
459
  
460
  
461
  
462
  
463
  
464
  
465
  
466
  
467
  
468
  
469
  
470
  
471
  
472
  
473
  
474
  
475
  
476
  
477
  
478
  
479
  
480
  
481
  
482
  
483
  
// -*- Pike -*- 
 
#pike __REAL_VERSION__ 
 
constant version = 
 sprintf("%d.%d.%d",(int)__REAL_VERSION__,__REAL_MINOR__,__REAL_BUILD__); 
constant description = "Pike module installer."; 
 
// Source directory 
string srcdir; 
string make=getenv("MAKE")||"make"; 
string make_flags=""; 
string include_path=master()->include_prefix; 
string config_args=""; 
#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; 
#endif 
 
// this is not the ideal location for all systems, but it's a start. 
string local_module_path=combine_path(getenv("HOME")||"","lib/pike/modules"); 
bool old_style_module = false; 
// 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]; 
 
// where do we install the documentation? 
string system_doc_path = master()->doc_prefix; 
 
string run_pike; 
 
#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, 
]); 
 
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; 
 
  // 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"); 
    } 
  } 
} 
 
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) 
{ 
  if(!has_value(path, "$src")) return path; 
  if(!srcdir) 
  { 
    string s=Stdio.read_file("Makefile"); 
    if(s) 
    { 
      sscanf(s,"%*s\nSRCDIR=%s\n",srcdir); 
    } 
    if(!srcdir) 
    { 
      srcdir="."; 
    } 
 
    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; 
  foreach(a, string file) 
  { 
    Stdio.Stat s = file_stat(fix(file)); 
    if(!s) return 0; 
    t = max(t, s->mtime); 
  } 
  return t; 
} 
 
 
int just_run(mapping options, string ... cmd) 
{ 
  werror(" %s\n",cmd*" "); 
  return Process.create_process(cmd,(["env":getenv()])|options)->wait(); 
} 
 
void run_or_fail(mapping options,string ... cmd) 
{ 
  werror(" %s\n",cmd*" "); 
  int ret=Process.create_process(cmd,(["env":getenv()])|options)->wait(); 
  if(ret) 
    exit(ret); 
} 
 
 
void do_make(array(string) cmd) 
{ 
  int tmp1; 
  string lmp; 
  string full_srcdir; 
 
  if(search(cmd, "testsuite")!=-1) 
    lmp="./plib/modules"; 
  else 
    lmp = local_module_path; 
 
  if(srcdir !=".") full_srcdir=srcdir + "/"; 
  else full_srcdir=getcwd() + "/"; 
 
  array extra_args = ({}); 
 
  if( old_style_module ) 
  { 
    extra_args = 
      ({"PIKE_INCLUDES=-I"+include_path, 
        "PIKE_SRC_DIR="+src_path, 
        "BUILD_BASE="+include_path, 
        "MODULE_BASE="+combine_path(include_path, "modules"), 
        "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, 
        "FINAL_PIKE="+run_pike, 
      }); 
  } 
  else 
  { 
    extra_args = ({ 
      "PIKE="+run_pike, 
      "SRCDIR="+fix("$src"), 
      "MODULE_INSTALL_DIR="+combine_path(__FILE__,"../../.."), 
      "LOCAL_MODULE_PATH=" + lmp, 
    }); 
  } 
 
  array(string) makecmd=({make})+do_split_quoted_string(make_flags)+extra_args+cmd; 
 
  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) 
{ 
  string specspath=combine_path(include_path, "specs"); 
 
  if(!Stdio.is_file(specspath)) 
  { 
    werror("Missing specs file at %s\n",specspath); 
    return 1; 
  } 
 
  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; 
  putenv("RUNPIKE", run_pike); 
 
  load_specs(specspath); 
 
  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"}) }), 
    ({"config_args",Getopt.HAS_ARG,({"--configure-args"}) }), 
    ({"help",Getopt.NO_ARG,({"--help"}) }), 
    )),array opt) 
    { 
      switch(opt[0]) 
      { 
        case "query": 
          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]); 
          exit(0); 
        case "help": 
          write(help, version); 
          exit(0); 
 
        case "config_args": config_args=opt[1]; break; 
        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; 
 
        case "all": 
          run->depend=run->autoheader=run->autoconf=run->configure=run->make=ALWAYS; 
          break; 
 
        case "auto": 
          run->depend=run->autoheader=run->autoconf=run->configure=run->make=AUTO; 
          break; 
      } 
    } 
 
  argv=Getopt.get_args(argv); 
  string configure_args=""; 
 
  foreach( argv, string arg ) 
    if( sscanf( arg, "CONFIGUREARGS=%s", configure_args ) ) 
      argv-=({arg}); 
 
  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" ; 
  int tmp1; 
 
  if(run->automake) 
  { 
    if(tmp1=max_time_of_files("$src/Makefile.am",configure_in)) 
    { 
      write("** Running automake\n"); 
      if(run->automake == ALWAYS || 
         max_time_of_files("$src/Makefile.in") < tmp1) 
      { 
        run_or_fail((["cwd":srcdir]),"aclocal"); 
        run_or_fail((["cwd":srcdir]),"automake"); 
        rm(fix("$src/stamp-h.in")); 
      } 
    } 
  } 
 
 
  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"); 
  } 
 
  string stamp_file = "$src/stamp-h.in"; 
 
  if( sscanf( configure_content, "%*sAC_CONFIG_HEADER(%[^)])", stamp_file ) == 2 ) 
    stamp_file="$src/"+stamp_file+".in"; 
 
  if(run->autoheader) 
  { 
    if(tmp1=max_time_of_files("$src/acconfig.h",configure_in)) 
    { 
      if(run->autoheader==ALWAYS || max_time_of_files(stamp_file) <= tmp1) 
      { 
        run_or_fail((["cwd":srcdir]),"autoheader"); 
        if( stamp_file == "$src/stamp-h.in" ) 
          Stdio.write_file(fix(stamp_file),"foo\n"); 
      } 
    } 
  } 
 
  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")); 
    } 
  } 
 
  if(run->autoconf) 
  { 
    if(tmp1=max_time_of_files(configure_in)) 
    { 
      if(run->autoconf==ALWAYS || max_time_of_files("$src/configure") <= tmp1) 
      { 
        if( old_style_module ) 
        { 
          if(file_stat(fix("$src/aclocal.m4"))) 
          { 
            write("** WARNING: aclocal.m4 detected in src tree, deleting it\n"); 
            rm(fix("$src/aclocal.m4")); 
          } 
          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) 
            run_or_fail((["cwd":srcdir]),"autoconf","--include="+src_path); 
          else 
            run_or_fail((["cwd":srcdir]),"autoconf","--localdir="+src_path); 
        } 
        else 
        { 
          write("** Running autoconf\n"); 
          run_or_fail( (["cwd":srcdir]), "autoconf" ); 
        } 
      } 
    } 
  } 
 
  if(run->configure) 
  { 
    if( tmp1=max_time_of_files("$src/configure") ) 
    { 
      if(run->configure == ALWAYS || (max_time_of_files("Makefile") <= tmp1)) 
      { 
        if( old_style_module ) 
        { 
          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"))); 
          } 
 
          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) 
          { 
            string key, val; 
            if(2==sscanf(arg, "%[a-zA-Z0-9_]=%s", key, val)) 
            { 
              cfa_env[key] = val; 
              cfa[i] = 0; 
            } 
          } 
          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"); 
          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)); 
        } 
      } 
    } 
  } 
 
  if( old_style_module && run->depend ) 
  { 
    if(!max_time_of_files("$src/dependencies") || run->depend == ALWAYS) 
    { 
      write("** Running make depend\n"); 
      // Create an empty file first.. 
      Stdio.write_file(srcdir+"/dependencies",""); 
      do_make( ({"depend"}) ); 
      do_make( ({"Makefile"}) ); 
    } 
  } 
 
  if(run->make) 
  { 
    write("** Running "+argv[1..]*" "); 
    do_make(argv[1..]); 
  } 
} 
 
constant help=#"Pike module installer %s. 
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, 
                system_module_path, make and specs. 
 
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. 
";