-
Notifications
You must be signed in to change notification settings - Fork 29
/
measure_size.php
executable file
·203 lines (172 loc) · 6.69 KB
/
measure_size.php
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
#!php
<?php
/**
* Copyright (c) 2022 Yun Dou <[email protected]>
*
* lwmbs is licensed under Mulan PSL v2. You can use this
* software according to the terms and conditions of the
* Mulan PSL v2. You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS,
* WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
*
* See the Mulan PSL v2 for more details.
*/
require __DIR__ . '/autoload.php';
function cliPath(Config $testConfig):string
{
return match (PHP_OS_FAMILY) {
'Windows', 'WINNT' => "src\\php-src\\{$testConfig->arch}\\Release_TS\\php.exe",
default => 'src/php-src/sapi/cli/php',
};
}
function microPath(Config $testConfig):string
{
return match (PHP_OS_FAMILY) {
'Windows', 'WINNT' => "src\\php-src\\{$testConfig->arch}\\Release_TS\\micro.sfx",
default => 'src/php-src/sapi/micro/micro.sfx',
};
}
function buildCli(Config $testConfig, bool $fresh)
{
$cliBuild = new CliBuild($testConfig);
if (!$fresh) {
unlink(cliPath($testConfig));
}
$cliBuild->build(
fresh: $fresh,
bloat: true,
);
clearstatcache(clear_realpath_cache: true, filename: cliPath($testConfig));
$stat = stat(cliPath($testConfig));
return $stat['size'];
}
function buildMicro(Config $testConfig, bool $fresh)
{
$microBuild = new MicroBuild($testConfig);
$microBuild->build(
fresh: $fresh,
bloat: true,
);
clearstatcache(clear_realpath_cache: true, filename: microPath($testConfig));
$stat = stat(microPath($testConfig));
return $stat['size'];
}
function measure_size($argv): int
{
Util::setErrorHandler();
$namedKeys = match (PHP_OS_FAMILY) {
'Windows', 'WINNT', 'Cygwin' => [
'phpBinarySDKDir' => ['path to sdk', true, null, 'path to binary sdk'],
'vsVer' => ['vs version', true, null, 'vs version, e.g. "17" for Visual Studio 2022'],
'arch' => ['arch', false, 'x64', 'architecture, "x64" or "arm64:'], // TODO: use real host arch
],
'Darwin' => [
'cc' => ['compiler', false, null, 'C compiler'],
'cxx' => ['compiler', false, null, 'C++ compiler'],
'arch' => ['arch', false, php_uname('m'), 'architecture'],
],
'Linux' => [
'cc' => ['compiler', false, null, 'C compiler'],
'cxx' => ['compiler', false, null, 'C++ compiler'],
'arch' => ['arch', false, php_uname('m'), 'architecture'],
'noSystem' => ['BOOL', false, false, 'donot use system static libraries'],
'allStatic' => ['BOOL', false, false, 'use -all-static in php build'],
]
};
$cmdArgs = Util::parseArgs(
argv: $argv,
positionalNames: [
'libraries' => ['LIBRARIES', true, null, 'select libraries, comma separated'],
'extensions' => ['EXTENSIONS', true, null, 'select extensions, comma separated'],
'srcFile' => ['SRCFILE', true, __DIR__ . DIRECTORY_SEPARATOR . 'src.json', 'src.json path'],
],
namedKeys: $namedKeys,
);
$config = Config::fromCmdArgs($cmdArgs);
$srcJson = json_decode(file_get_contents($cmdArgs['positional']['srcFile']), true);
$phpVerH = file_get_contents('src/php-src/main/php_version.h');
preg_match('/#\s*define\s+PHP_MAJOR_VERSION\s+(\d+)/', $phpVerH, $matchesMajor);
preg_match('/#\s*define\s+PHP_MINOR_VERSION\s+(\d+)/', $phpVerH, $matchesMinor);
if (!$matchesMajor || !$matchesMinor) {
throw new Exception('failed to find php version');
}
$phpVer = "{$matchesMajor[1]}.{$matchesMinor[1]}";
$base = match (PHP_OS_FAMILY) {
'Linux' =>
"$phpVer-linux-{$config->gnuArch}-" . $config->libc->literalName() . ($cmdArgs['named']['allStatic'] ? '_static' : '_shared'),
'Darwin' =>
"$phpVer-macos-{$config->gnuArch}",
'Windows' =>
"$phpVer-windows-{$config->gnuArch}-vs{$config->vsVer}",
};
$libNames = array_filter(array_map('trim', explode(',', $cmdArgs['positional']['libraries'])));
$extNames = array_filter(array_map('trim', explode(',', $cmdArgs['positional']['extensions'])));
if ((bool)($cmdArgs['named']['allStatic'] ?? false)) {
if (false !== array_search('libffi', $libNames, true)) {
unset($libNames[array_search('libffi', $libNames, true)]);
}
if (false !== array_search('ffi', $extNames, true)) {
unset($extNames[array_search('ffi', $extNames, true)]);
}
}
// add all libs and exts to make order
foreach ($libNames as $name) {
$config->addLib(new ("Lib$name")($config));
}
foreach ($extNames as $name) {
$config->addExt(new Extension(name: $name, config: $config));
}
$allLibs = $config->makeLibArray();
$allExts = $config->makeExtArray();
// make clean args
array_splice($argv, 1, 3, ['', '', '--fresh']);
// make size struct
$srcJson['size'] = $srcJson['size'] ?? [];
$srcJson['size'][$base]['libs'] = $srcJson['size'][$base]['libs'] ?? [];
$srcJson['size'][$base]['exts'] = $srcJson['size'][$base]['exts'] ?? [];
// make test config
[$_, $testConfig] = Util::makeConfig($argv);
// try build base micro binary
$microSize = buildMicro($testConfig, true);
$srcJson['size'][$base]['micro'] = $microSize;
Log::i("size of base micro is {$microSize}");
// try build base cli binary
$lastSize = buildCli($testConfig, true);
$srcJson['size'][$base]['cli'] = $lastSize;
Log::i("size of base cli is {$lastSize}");
// try to add once a lib
foreach ($allLibs as $name => $lib) {
$testConfig->addLib($lib);
$lib->prove(
forceBuild: $cmdArgs['named']['noSystem'] ?? false,
fresh: true,
);
$size = buildCli($testConfig, false);
$delta = $size - $lastSize;
$lastSize = $size;
Log::i("size of library {$name} is {$delta}");
$srcJson['size'][$base]['libs'][$name] = $delta;
}
//$allExtDesc = CommonExtension::getAllExtensionDescs();
// try to add once an ext
$fresh = true;
foreach ($allExts as $name => $ext) {
$testConfig->addExt($ext);
$size = buildCli($testConfig, true);
$delta = $size - $lastSize;
$lastSize = $size;
Log::i("size of extension {$name} is {$delta}");
$srcJson['size'][$base]['exts'][$name] = $delta;
}
file_put_contents($cmdArgs['positional']['srcFile'], json_encode(value: $srcJson, flags: JSON_PRETTY_PRINT));
return 0;
}
if (!isset($asLib)) {
$asLib = true;
exit(measure_size($argv));
}