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