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