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