xref: /PHP-5.5/win32/build/phpize.js.in (revision a8e0a623)
1/*
2  +----------------------------------------------------------------------+
3  | PHP Version 5                                                        |
4  +----------------------------------------------------------------------+
5  | Copyright (c) 1997-2008 The PHP Group                                |
6  +----------------------------------------------------------------------+
7  | This source file is subject to version 3.01 of the PHP license,      |
8  | that is bundled with this package in the file LICENSE, and is        |
9  | available through the world-wide-web at the following url:           |
10  | http://www.php.net/license/3_01.txt                                  |
11  | If you did not receive a copy of the PHP license and are unable to   |
12  | obtain it through the world-wide-web, please send a note to          |
13  | license@php.net so we can mail you a copy immediately.               |
14  +----------------------------------------------------------------------+
15  | Author: Pierre Joye <pierre1@php.net>                                |
16  +----------------------------------------------------------------------+
17*/
18
19/* $Id$ */
20// This generates a configure script for win32 build
21
22var STDOUT = WScript.StdOut;
23
24var FSO = WScript.CreateObject("Scripting.FileSystemObject");
25var C = FSO.CreateTextFile("configure.js", true);
26var B = FSO.CreateTextFile("configure.bat", true);
27var DSP = false;
28re = /\\script/i;
29var PHP_DIR=FSO.GetParentFolderName(WScript.ScriptFullName).replace(re,"");
30
31var modules = "";
32var MODULES = WScript.CreateObject("Scripting.Dictionary");
33var module_dirs = new Array();
34
35function ERROR(msg)
36{
37	STDERR.WriteLine("ERROR: " + msg);
38	WScript.Quit(3);
39}
40
41function file_get_contents(filename)
42{
43	var t = "";
44	var F = FSO.OpenTextFile(filename, 1);
45
46	if (!F.AtEndOfStream) {
47		t = F.ReadAll();
48		F.Close();
49	}
50	return t;
51}
52
53function Module_Item(module_name, config_path, dir_line, deps, content)
54{
55	this.module_name = module_name;
56	this.config_path = config_path;
57	this.dir_line = dir_line;
58	this.deps = deps;
59	this.content = content;
60}
61
62function get_module_dep(contents)
63{
64	var re_dep_line = new RegExp("ADD_EXTENSION_DEP\\([^,]*\\s*,\\s*['\"]([^'\"]+)['\"].*\\)", "gm");
65	var calls = contents.match(re_dep_line);
66	var deps = new Array();
67	if (calls != null) {
68		for (i = 0; i < calls.length; i++) {
69			// now we need the extension name out of this thing
70			if (calls[i].match(re_dep_line)) {
71				deps[deps.length] = RegExp.$1;
72
73			}
74		}
75	}
76	return deps;
77}
78
79function find_config_w32(dirname)
80{
81	if (!FSO.FolderExists(dirname)) {
82		return;
83	}
84
85	var f = FSO.GetFolder(dirname);
86	var	fc = new Enumerator(f.SubFolders);
87	var c, i, ok, n;
88	var item = null;
89
90	c = dirname + "\\config.w32";
91	if (FSO.FileExists(c)) {
92		var dir_line = "configure_module_dirname = condense_path(FSO.GetParentFolderName('"
93								+ c.replace(new RegExp('(["\\\\])', "g"), '\\$1') + "'));\r\n";
94		var contents = file_get_contents(c);
95
96		deps = get_module_dep(contents);
97
98		item = new Module_Item(n, c, dir_line, deps, contents);
99		MODULES.Add(n, item);
100	}
101
102	for (; !fc.atEnd(); fc.moveNext()) {
103		/* check if we already picked up a module with the same dirname;
104		 * if we have, don't include it here */
105		n = FSO.GetFileName(fc.item());
106		if (n == '.svn' || n == 'tests' || n == '.git') {
107			continue;
108		}
109
110		c = FSO.BuildPath(fc.item(), "config.w32");
111		if (FSO.FileExists(c)) {
112			var dir_line = "configure_module_dirname = condense_path(FSO.GetParentFolderName('"
113							   	+ c.replace(new RegExp('(["\\\\])', "g"), '\\$1') + "'));\r\n";
114			var contents = file_get_contents(c);
115
116			deps = get_module_dep(contents);
117
118			item = new Module_Item(n, c, dir_line, deps, contents);
119			MODULES.Add(n, item);
120		}
121	}
122}
123
124function emit_module(item)
125{
126	return item.dir_line + item.content;
127}
128
129function emit_dep_modules(module_names)
130{
131	var i, mod_name, j;
132	var output = "";
133	var item = null;
134
135	for (i in module_names) {
136		mod_name = module_names[i];
137
138		if (MODULES.Exists(mod_name)) {
139			item = MODULES.Item(mod_name);
140			MODULES.Remove(mod_name);
141			if (item.deps.length) {
142				output += emit_dep_modules(item.deps);
143			}
144			output += emit_module(item);
145		}
146	}
147
148	return output;
149}
150
151function gen_modules()
152{
153	var module_names = (new VBArray(MODULES.Keys())).toArray();
154	var i, mod_name, j;
155	var item;
156	var output = "";
157
158	// first, look for modules with empty deps; emit those first
159	for (i in module_names) {
160		STDOUT.WriteLine("module ... " + module_names);
161		mod_name = module_names[i];
162		item = MODULES.Item(mod_name);
163		if (item.deps.length == 0) {
164			MODULES.Remove(mod_name);
165			output += emit_module(item);
166		}
167	}
168
169	// now we are left with modules that have dependencies on other modules
170	module_names = (new VBArray(MODULES.Keys())).toArray();
171	output += emit_dep_modules(module_names);
172
173	return output;
174}
175
176// Process buildconf arguments
177function buildconf_process_args()
178{
179	args = WScript.Arguments;
180
181	for (i = 0; i < args.length; i++) {
182		arg = args(i);
183		// If it is --foo=bar, split on the equals sign
184		arg = arg.split("=", 2);
185		argname = arg[0];
186		if (arg.length > 1) {
187			argval = arg[1];
188		} else {
189			argval = null;
190		}
191
192		if (argname == '--clean' && argval != null) {
193			STDOUT.WriteLine("Cleaning...");
194			return 0;
195		}
196
197		if (argname == '--help') {
198			STDOUT.WriteLine("Usage: phpize [--clean|--help|--version|-v]");
199			return 0;
200		}
201		return 1;
202	}
203}
204
205if (buildconf_process_args() == 0) {
206	WScript.Quit(3);
207}
208STDOUT.WriteLine("Rebuilding configure.js");
209STDOUT.WriteLine(PHP_DIR);
210
211// Write the head of the configure script
212C.WriteLine("/* This file automatically generated from script/confutils.js */");
213C.WriteLine("var MODE_PHPIZE = true;");
214C.WriteLine("var PHP_DIR = " + '"' + PHP_DIR.replace(new RegExp('(["\\\\])', "g"), '\\$1') + '"');
215C.WriteLine("var PHP_PREFIX = " + '"' + PHP_PREFIX.replace(new RegExp('(["\\\\])', "g"), '\\$1') + '"');
216
217C.Write(file_get_contents(PHP_DIR + "//script//ext_deps.js"));
218if (FSO.FileExists(PHP_DIR + "/script/ext_pickle.js")) {
219	C.Write(file_get_contents(PHP_DIR + "//script//ext_pickle.js"));
220}
221
222C.Write(file_get_contents(PHP_DIR + "/script/confutils.js"));
223C.Write(file_get_contents(PHP_DIR + "/script/config.phpize.js"));
224
225// If project files were requested, pull in the code to generate them
226if (DSP == true) {
227	C.WriteLine('PHP_DSP="yes"');
228	C.WriteBlankLines(1);
229	C.Write(file_get_contents(PHP_DIR + "win32/build/projectgen.js"));
230} else {
231	C.WriteLine('PHP_DSP="no"');
232	C.WriteBlankLines(1);
233}
234
235// Pull in code for the base detection
236modules = file_get_contents(PHP_DIR + "/script/config.w32.phpize.in");
237
238C.WriteLine("ARG_ENABLE('debug', 'Compile with debugging symbols', \"no\");");
239find_config_w32(".");
240
241// Now generate contents of module based on MODULES, chasing dependencies
242// to ensure that dependent modules are emitted first
243modules += gen_modules();
244
245// Look for ARG_ENABLE or ARG_WITH calls
246re = new RegExp("(ARG_(ENABLE|WITH)\([^;]+\);)", "gm");
247calls = modules.match(re);
248for (i = 0; i < calls.length; i++) {
249	item = calls[i];
250	C.WriteLine("try {");
251	C.WriteLine(item);
252	C.WriteLine("} catch (e) {");
253	C.WriteLine('\tSTDOUT.WriteLine("problem: " + e);');
254	C.WriteLine("}");
255}
256
257C.WriteBlankLines(1);
258C.WriteLine("conf_process_args();");
259C.WriteBlankLines(1);
260
261// Comment out the calls from their original positions
262modules = modules.replace(re, "/* $1 */");
263C.Write(modules);
264
265
266C.WriteBlankLines(1);
267C.Write(file_get_contents(PHP_DIR + "\\script\\configure.tail"));
268
269B.WriteLine("@echo off");
270B.WriteLine("cscript /nologo configure.js %*");
271