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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
|
#!/usr/bin/env -S python3 -B
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
import argparse
import enum
import logging
import os
import shutil
import stat
import subprocess
import sys
from pathlib import Path
logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.INFO)
def find_command(names):
"""Search for command in `names`, and returns the first one that exists."""
for name in names:
path = shutil.which(name)
if path is not None:
return name
return None
def assert_command(env_var, name):
"""Assert that the command is not empty
The command name comes from either environment variable or find_command.
"""
if not name:
logging.error("{} command not found".format(env_var))
sys.exit(1)
def parse_version(topsrc_dir):
"""Parse milestone.txt and return the entire milestone and major version."""
milestone_file = topsrc_dir / "config" / "milestone.txt"
if not milestone_file.is_file():
return ("", "", "")
with milestone_file.open("r") as f:
for line in f:
line = line.strip()
if not line:
continue
if line.startswith("#"):
continue
v = line.split(".")
return tuple((v + ["", ""])[:3])
return ("", "", "")
tmp_dir = Path("/tmp")
tar = os.environ.get("TAR", find_command(["tar"]))
assert_command("TAR", tar)
rsync = os.environ.get("RSYNC", find_command(["rsync"]))
assert_command("RSYNC", rsync)
autoconf = os.environ.get(
"AUTOCONF",
find_command(
[
"autoconf-2.13",
"autoconf2.13",
"autoconf213",
]
),
)
assert_command("AUTOCONF", autoconf)
src_dir = Path(os.environ.get("SRC_DIR", Path(__file__).parent.absolute()))
mozjs_name = os.environ.get("MOZJS_NAME", "mozjs")
staging_dir = Path(os.environ.get("STAGING", tmp_dir / "mozjs-src-pkg"))
dist_dir = Path(os.environ.get("DIST", tmp_dir))
topsrc_dir = src_dir.parent.parent.absolute()
parsed_major_version, parsed_minor_version, parsed_patch_version = parse_version(
topsrc_dir
)
major_version = os.environ.get("MOZJS_MAJOR_VERSION", parsed_major_version)
minor_version = os.environ.get("MOZJS_MINOR_VERSION", parsed_minor_version)
patch_version = os.environ.get("MOZJS_PATCH_VERSION", parsed_patch_version)
alpha = os.environ.get("MOZJS_ALPHA", "")
version = "{}-{}.{}.{}".format(
mozjs_name, major_version, minor_version, patch_version or alpha or "0"
)
target_dir = staging_dir / version
package_name = "{}.tar.xz".format(version)
package_file = dist_dir / package_name
tar_opts = ["-Jcf"]
# Given there might be some external program that reads the following output,
# use raw `print`, instead of logging.
print("Environment:")
print(" TAR = {}".format(tar))
print(" RSYNC = {}".format(rsync))
print(" AUTOCONF = {}".format(autoconf))
print(" STAGING = {}".format(staging_dir))
print(" DIST = {}".format(dist_dir))
print(" SRC_DIR = {}".format(src_dir))
print(" MOZJS_NAME = {}".format(mozjs_name))
print(" MOZJS_MAJOR_VERSION = {}".format(major_version))
print(" MOZJS_MINOR_VERSION = {}".format(minor_version))
print(" MOZJS_PATCH_VERSION = {}".format(patch_version))
print(" MOZJS_ALPHA = {}".format(alpha))
print("")
rsync_filter_list = """
# Top-level config and build files
+ /configure.py
+ /LICENSE
+ /Makefile.in
+ /moz.build
+ /moz.configure
+ /test.mozbuild
+ /.babel-eslint.rc.js
+ .eslintignore
+ .eslintrc.js
+ .flake8
+ .gitignore
+ .hgignore
+ .lldbinit
+ .prettierignore
+ .prettierrc
+ .ycm_extra_conf.py
# Additional libraries (optionally) used by SpiderMonkey
+ /mfbt/**
+ /nsprpub/**
- /intl/icu/source/data
- /intl/icu/source/test
- /intl/icu/source/tools
+ /intl/icu/**
+ /memory/replace/dmd/dmd.py
+ /memory/build/**
+ /memory/moz.build
+ /memory/mozalloc/**
+ /modules/fdlibm/**
+ /modules/zlib/**
+ /mozglue/baseprofiler/**
+ /mozglue/build/**
+ /mozglue/misc/**
+ /mozglue/moz.build
+ /mozglue/static/**
+ /tools/rb/fix_stacks.py
+ /tools/fuzzing/moz.build
+ /tools/fuzzing/interface/**
+ /tools/fuzzing/registry/**
+ /tools/fuzzing/libfuzzer/**
# Build system and dependencies
+ /Cargo.lock
+ /build/**
+ /config/**
+ /python/**
+ /.cargo/config.in
- /third_party/python/gyp
+ /third_party/python/**
+ /third_party/rust/**
+ /layout/tools/reftest/reftest/**
+ /testing/moz.build
+ /testing/mozbase/**
+ /testing/performance/**
+ /testing/web-platform/tests/streams/**
+ /toolkit/crashreporter/tools/symbolstore.py
+ /toolkit/mozapps/installer/package-name.mk
# SpiderMonkey itself
+ /js/src/**
+ /js/app.mozbuild
+ /js/*.configure
+ /js/examples/**
+ /js/public/**
+ /js/rust/**
+ */
- /**
"""
INSTALL_CONTENT = """\
Full build documentation for SpiderMonkey is hosted on MDN:
https://developer.mozilla.org/en-US/docs/SpiderMonkey/Build_Documentation
Note that the libraries produced by the build system include symbols,
causing the binaries to be extremely large. It is highly suggested that `strip`
be run over the binaries before deploying them.
Building with default options may be performed as follows:
cd js/src
mkdir obj
cd obj
../configure
make # or mozmake on Windows
"""
README_CONTENT = """\
This directory contains SpiderMonkey {major_version}.
This release is based on a revision of Mozilla {major_version}:
https://hg.mozilla.org/releases/
The changes in the patches/ directory were applied.
MDN hosts the latest SpiderMonkey {major_version} release notes:
https://developer.mozilla.org/en-US/docs/SpiderMonkey/{major_version}
""".format(
major_version=major_version
)
def is_mozjs_cargo_member(line):
"""Checks if the line in workspace.members is mozjs-related"""
return '"js/' in line
def is_mozjs_crates_io_local_patch(line):
"""Checks if the line in patch.crates-io is mozjs-related"""
return 'path = "js' in line
def clean():
"""Remove temporary directory and package file."""
logging.info("Cleaning {} and {} ...".format(package_file, target_dir))
if package_file.exists():
package_file.unlink()
if target_dir.exists():
shutil.rmtree(str(target_dir))
def assert_clean():
"""Assert that target directory does not contain generated files."""
makefile_file = target_dir / "js" / "src" / "Makefile"
if makefile_file.exists():
logging.error("found js/src/Makefile. Please clean before packaging.")
sys.exit(1)
def create_target_dir():
if target_dir.exists():
logging.warning("dist tree {} already exists!".format(target_dir))
else:
target_dir.mkdir(parents=True)
def sync_files():
# Output of the command should directly go to stdout/stderr.
p = subprocess.Popen(
[
str(rsync),
"--delete-excluded",
"--prune-empty-dirs",
"--quiet",
"--recursive",
"{}/".format(topsrc_dir),
"{}/".format(target_dir),
"--filter=. -",
],
stdin=subprocess.PIPE,
)
p.communicate(rsync_filter_list.encode())
if p.returncode != 0:
sys.exit(p.returncode)
def copy_cargo_toml():
cargo_toml_file = topsrc_dir / "Cargo.toml"
target_cargo_toml_file = target_dir / "Cargo.toml"
with cargo_toml_file.open("r") as f:
class State(enum.Enum):
BEFORE_MEMBER = 1
INSIDE_MEMBER = 2
AFTER_MEMBER = 3
INSIDE_PATCH = 4
AFTER_PATCH = 5
content = ""
state = State.BEFORE_MEMBER
for line in f:
if state == State.BEFORE_MEMBER:
if line.strip() == "members = [":
state = State.INSIDE_MEMBER
elif state == State.INSIDE_MEMBER:
if line.strip() == "]":
state = State.AFTER_MEMBER
elif not is_mozjs_cargo_member(line):
continue
elif state == State.AFTER_MEMBER:
if line.strip() == "[patch.crates-io]":
state = State.INSIDE_PATCH
elif state == State.INSIDE_PATCH:
if line.startswith("["):
state = State.AFTER_PATCH
if "path = " in line:
if not is_mozjs_crates_io_local_patch(line):
continue
content += line
with target_cargo_toml_file.open("w") as f:
f.write(content)
def generate_configure():
"""Generate configure files to avoid build dependency on autoconf-2.13"""
src_configure_in_file = topsrc_dir / "js" / "src" / "configure.in"
src_old_configure_in_file = topsrc_dir / "js" / "src" / "old-configure.in"
dest_configure_file = target_dir / "js" / "src" / "configure"
dest_old_configure_file = target_dir / "js" / "src" / "old-configure"
shutil.copy2(
str(src_configure_in_file), str(dest_configure_file), follow_symlinks=False
)
st = dest_configure_file.stat()
dest_configure_file.chmod(st.st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
js_src_dir = topsrc_dir / "js" / "src"
with dest_old_configure_file.open("w") as f:
subprocess.run(
[
str(autoconf),
"--localdir={}".format(js_src_dir),
str(src_old_configure_in_file),
],
stdout=f,
check=True,
)
def copy_install():
"""Copy or create INSTALL."""
staging_install_file = staging_dir / "INSTALL"
target_install_file = target_dir / "INSTALL"
if staging_install_file.exists():
shutil.copy2(str(staging_install_file), str(target_install_file))
else:
with target_install_file.open("w") as f:
f.write(INSTALL_CONTENT)
def copy_readme():
"""Copy or create README."""
staging_readme_file = staging_dir / "README"
target_readme_file = target_dir / "README"
if staging_readme_file.exists():
shutil.copy2(str(staging_readme_file), str(target_readme_file))
else:
with target_readme_file.open("w") as f:
f.write(README_CONTENT)
def copy_patches():
"""Copy patches dir, if it exists."""
staging_patches_dir = staging_dir / "patches"
top_patches_dir = topsrc_dir / "patches"
target_patches_dir = target_dir / "patches"
if staging_patches_dir.is_dir():
shutil.copytree(str(staging_patches_dir), str(target_patches_dir))
elif top_patches_dir.is_dir():
shutil.copytree(str(top_patches_dir), str(target_patches_dir))
def remove_python_cache():
"""Remove *.pyc and *.pyo files if any."""
for f in target_dir.glob("**/*.pyc"):
f.unlink()
for f in target_dir.glob("**/*.pyo"):
f.unlink()
def stage():
"""Stage source tarball content."""
logging.info("Staging source tarball in {}...".format(target_dir))
create_target_dir()
sync_files()
copy_cargo_toml()
generate_configure()
copy_install()
copy_readme()
copy_patches()
remove_python_cache()
def create_tar():
"""Roll the tarball."""
logging.info("Packaging source tarball at {}...".format(package_file))
subprocess.run(
[str(tar)] + tar_opts + [str(package_file), "-C", str(staging_dir), version],
check=True,
)
def build():
assert_clean()
stage()
create_tar()
parser = argparse.ArgumentParser(description="Make SpiderMonkey source package")
subparsers = parser.add_subparsers(dest="COMMAND")
subparser_update = subparsers.add_parser("clean", help="")
subparser_update = subparsers.add_parser("build", help="")
args = parser.parse_args()
if args.COMMAND == "clean":
clean()
elif not args.COMMAND or args.COMMAND == "build":
build()
|