Skip to content

Commit 7c5ecc5

Browse files
committed
build: rework gyp files for zlib
Restructure the zlib.gyp file based on the upstream gn file, breaking out the files with optimizations that need additional compiler flags. Use a copy of the GN-scraper.py script to reduce the amount of hand editing when the zlib dependency is updated.
1 parent 9b68d84 commit 7c5ecc5

File tree

3 files changed

+185
-65
lines changed

3 files changed

+185
-65
lines changed

deps/zlib/GN-scraper.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Copyright (c) 2019 Refael Ackeramnn<[email protected]>. All rights reserved.
2+
# Use of this source code is governed by an MIT-style license.
3+
import re
4+
import os
5+
import sys
6+
7+
PLAIN_SOURCE_RE = re.compile('\s*"([^/$].+)"\s*')
8+
def DoMain(args):
9+
gn_filename, pattern = args
10+
src_root = os.path.dirname(gn_filename)
11+
with open(gn_filename, 'rb') as gn_file:
12+
gn_content = gn_file.read().decode('utf-8')
13+
14+
scraper_re = re.compile(pattern + r'\[([^\]]+)', re.DOTALL)
15+
matches = scraper_re.search(gn_content)
16+
match = matches.group(1)
17+
files = []
18+
for l in match.splitlines():
19+
m2 = PLAIN_SOURCE_RE.match(l)
20+
if not m2:
21+
continue
22+
files.append(m2.group(1))
23+
# always use `/` since GYP will process paths further downstream
24+
rel_files = ['"%s/%s"' % (src_root, f) for f in files]
25+
return ' '.join(rel_files)
26+
27+
if __name__ == '__main__':
28+
print(DoMain(sys.argv[1:]))

deps/zlib/zlib.gyp

+155-63
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
{
66
'variables': {
7+
'ZLIB_ROOT': '.',
78
'use_system_zlib%': 0,
89
'arm_fpu%': '',
910
'llvm_version%': '0.0',
@@ -12,44 +13,148 @@
1213
['use_system_zlib==0', {
1314
'targets': [
1415
{
15-
'target_name': 'zlib',
16+
'target_name': 'zlib_adler32_simd',
17+
'type': 'static_library',
18+
'conditions': [
19+
['(target_arch in "ia32 x64 x32" and OS!="ios") or arm_fpu=="neon"', {
20+
'defines': [ 'ADLER32_SIMD_SSSE3' ],
21+
'conditions': [
22+
['OS=="win"', {
23+
'defines': [ 'X86_WINDOWS' ],
24+
},{
25+
'defines': [ 'X86_NOT_WINDOWS' ],
26+
}],
27+
['OS!="win" or llvm_version!="0.0"', {
28+
'cflags': [ '-mssse3' ],
29+
}],
30+
],
31+
}],
32+
['arm_fpu=="neon"', {
33+
'defines': [ 'ADLER32_SIMD_NEON' ],
34+
}],
35+
],
36+
'include_dirs': [ '<(ZLIB_ROOT)' ],
37+
'defines': [ 'ZLIB_IMPLEMENTATION' ],
38+
'direct_dependent_settings': {
39+
'include_dirs': [ '<(ZLIB_ROOT)' ],
40+
},
41+
'sources': [
42+
'<!@pymod_do_main(GN-scraper "<(ZLIB_ROOT)/BUILD.gn" "\\"zlib_adler32_simd\\".*?sources = ")',
43+
],
44+
}, # zlib_adler32_simd
45+
{
46+
'target_name': 'zlib_arm_crc32',
47+
'type': 'static_library',
48+
'conditions': [
49+
['OS!="ios"', {
50+
'conditions': [
51+
['OS!="win" and llvm_version!="0.0"', {
52+
'cflags': [ '-march=armv8-a+crc' ],
53+
}],
54+
['OS=="android"', {
55+
'defines': [ 'ARMV8_OS_ANDROID' ],
56+
}],
57+
['OS=="linux"', {
58+
'defines': [ 'ARMV8_OS_LINUX' ],
59+
}],
60+
['OS=="win"', {
61+
'defines': [ 'ARMV8_OS_WINDOWS' ],
62+
}],
63+
],
64+
'defines': [ 'CRC32_ARMV8_CRC32' ],
65+
'include_dirs': [ '<(ZLIB_ROOT)' ],
66+
'direct_dependent_settings': {
67+
'include_dirs': [ '<(ZLIB_ROOT)' ],
68+
},
69+
'sources': [
70+
'<!@pymod_do_main(GN-scraper "<(ZLIB_ROOT)/BUILD.gn" "\\"zlib_arm_crc32\\".*?sources = ")',
71+
],
72+
}],
73+
],
74+
'defines': [ 'ZLIB_IMPLEMENTATION' ],
75+
}, # zlib_arm_crc32
76+
{
77+
'target_name': 'zlib_inflate_chunk_simd',
78+
'type': 'static_library',
79+
'conditions': [
80+
['(target_arch in "ia32 x64 x32" and OS!="ios")', {
81+
'defines': [ 'INFLATE_CHUNK_SIMD_SSE2' ],
82+
'conditions': [
83+
['target_arch=="x64"', {
84+
'defines': [ 'INFLATE_CHUNK_READ_64LE' ],
85+
}],
86+
],
87+
}],
88+
['arm_fpu=="neon"', {
89+
'defines': [ 'INFLATE_CHUNK_SIMD_NEON' ],
90+
'conditions': [
91+
['target_arch=="arm64"', {
92+
'defines': [ 'INFLATE_CHUNK_READ_64LE' ],
93+
}],
94+
],
95+
}]
96+
],
97+
'include_dirs': [ '<(ZLIB_ROOT)' ],
98+
'defines': [ 'ZLIB_IMPLEMENTATION' ],
99+
'direct_dependent_settings': {
100+
'include_dirs': [ '<(ZLIB_ROOT)' ],
101+
},
102+
'sources': [
103+
'<!@pymod_do_main(GN-scraper "<(ZLIB_ROOT)/BUILD.gn" "\\"zlib_inflate_chunk_simd\\".*?sources = ")',
104+
],
105+
}, # zlib_inflate_chunk_simd
106+
{
107+
'target_name': 'zlib_crc32_simd',
16108
'type': 'static_library',
109+
'conditions': [
110+
['OS!="win" or llvm_version!="0.0"', {
111+
'cflags': [
112+
'-msse4.2',
113+
'-mpclmul',
114+
],
115+
}]
116+
],
117+
'defines': [
118+
'CRC32_SIMD_SSE42_PCLMUL',
119+
'ZLIB_IMPLEMENTATION',
120+
],
121+
'include_dirs': [ '<(ZLIB_ROOT)' ],
122+
'direct_dependent_settings': {
123+
'include_dirs': [ '<(ZLIB_ROOT)' ],
124+
},
17125
'sources': [
18-
'adler32.c',
19-
'compress.c',
20-
'contrib/optimizations/insert_string.h',
21-
'crc32.c',
22-
'crc32.h',
23-
'deflate.c',
24-
'deflate.h',
25-
'gzclose.c',
26-
'gzguts.h',
27-
'gzlib.c',
28-
'gzread.c',
29-
'gzwrite.c',
30-
'infback.c',
31-
'inffast.c',
32-
'inffast.h',
33-
'inffixed.h',
34-
'inflate.h',
35-
'inftrees.c',
36-
'inftrees.h',
37-
'trees.c',
38-
'trees.h',
39-
'uncompr.c',
40-
'x86.h',
41-
'zconf.h',
42-
'zlib.h',
43-
'zutil.c',
44-
'zutil.h',
126+
'<!@pymod_do_main(GN-scraper "<(ZLIB_ROOT)/BUILD.gn" "\\"zlib_crc32_simd\\".*?sources = ")',
45127
],
46-
'include_dirs': [
47-
'.',
128+
}, # zlib_crc32_simd
129+
{
130+
'target_name': 'zlib_x86_simd',
131+
'type': 'static_library',
132+
'conditions': [
133+
['OS!="win" or llvm_version!="0.0"', {
134+
'cflags': [
135+
'-msse4.2',
136+
'-mpclmul',
137+
],
138+
}]
48139
],
140+
'include_dirs': [ '<(ZLIB_ROOT)' ],
141+
'defines': [ 'ZLIB_IMPLEMENTATION' ],
49142
'direct_dependent_settings': {
50-
'include_dirs': [
51-
'.',
52-
],
143+
'include_dirs': [ '<(ZLIB_ROOT)' ],
144+
},
145+
'sources': [
146+
'<!@pymod_do_main(GN-scraper "<(ZLIB_ROOT)/BUILD.gn" "\\"zlib_x86_simd\\".*?sources = ")',
147+
],
148+
}, # zlib_x86_simd
149+
{
150+
'target_name': 'zlib',
151+
'type': 'static_library',
152+
'sources': [
153+
'<!@pymod_do_main(GN-scraper "<(ZLIB_ROOT)/BUILD.gn" "\\"zlib\\".*?sources = ")',
154+
],
155+
'include_dirs': [ '<(ZLIB_ROOT)' ],
156+
'direct_dependent_settings': {
157+
'include_dirs': [ '<(ZLIB_ROOT)' ],
53158
},
54159
'conditions': [
55160
['OS!="win"', {
@@ -64,45 +169,31 @@
64169
'USE_FILE32API'
65170
],
66171
}],
67-
['(target_arch in "ia32 x64 x32" and OS!="ios") or arm_fpu=="neon"', {
68-
'sources': [
69-
'adler32_simd.c',
70-
'adler32_simd.h',
71-
'contrib/optimizations/chunkcopy.h',
72-
'contrib/optimizations/inffast_chunk.c',
73-
'contrib/optimizations/inffast_chunk.h',
74-
'contrib/optimizations/inflate.c',
172+
# Incorporate optimizations where possible.
173+
# Optimizations not enabled for x86 as problems were reported,
174+
# https://github.com/nodejs/node/issues/33019.
175+
['(target_arch in "x64 x32" and OS!="ios") or arm_fpu=="neon"', {
176+
'dependencies': [
177+
'zlib_adler32_simd',
178+
'zlib_inflate_chunk_simd',
75179
],
76180
}, {
77-
'sources': [ 'inflate.c', ],
181+
'defines': [ 'CPU_NO_SIMD' ],
182+
'sources': [ '<(ZLIB_ROOT)/inflate.c', ],
78183
}],
79-
# Incorporate optimizations where possible
80184
['target_arch in "ia32 x64 x32" and OS!="ios"', {
81-
'defines': [
82-
'ADLER32_SIMD_SSSE3',
83-
'INFLATE_CHUNK_SIMD_SSE2',
84-
'CRC32_SIMD_SSE42_PCLMUL',
185+
'dependencies': [
186+
'zlib_crc32_simd',
187+
'zlib_x86_simd',
85188
],
86189
'sources': [
87-
'crc32_simd.c',
88-
'crc32_simd.h',
89-
'crc_folding.c',
90-
'fill_window_sse.c',
91-
'x86.c',
92-
],
93-
'conditions': [
94-
['target_arch=="x64"', {
95-
'defines': [ 'INFLATE_CHUNK_READ_64LE' ],
96-
}],
190+
'<(ZLIB_ROOT)/x86.c',
97191
],
98-
}, {
99-
'sources': [ 'simd_stub.c', ],
192+
},{
193+
'sources': [ '<(ZLIB_ROOT)/simd_stub.c' ],
100194
}],
101195
['arm_fpu=="neon"', {
102-
'defines': [
103-
'ADLER32_SIMD_NEON',
104-
'INFLATE_CHUNK_SIMD_NEON',
105-
],
196+
'dependencies': [ 'zlib_arm_crc32' ],
106197
'sources': [
107198
'contrib/optimizations/slide_hash_neon.h',
108199
],
@@ -138,6 +229,7 @@
138229
],
139230
}],
140231
],
232+
'defines': [ 'ZLIB_IMPLEMENTATION' ],
141233
},
142234
],
143235
}, {

doc/guides/maintaining-zlib.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ performance improvements not currently available in standard zlib.
88
Update zlib:
99
```bash
1010
git clone https://chromium.googlesource.com/chromium/src/third_party/zlib
11-
cp deps/zlib/zlib.gyp deps/zlib/win32/zlib.def deps
11+
cp deps/zlib/zlib.gyp deps/zlib/GN-scraper.py deps/zlib/win32/zlib.def deps
1212
rm -rf deps/zlib zlib/.git
1313
mv zlib deps/
14-
mv deps/zlib.gyp deps/zlib/
14+
mv deps/zlib.gyp deps/GN-scraper.py deps/zlib/
1515
mkdir deps/zlib/win32
1616
mv deps/zlib.def deps/zlib/win32
1717
sed -i -- 's_^#include "chromeconf.h"_//#include "chromeconf.h"_' deps/zlib/zconf.h

0 commit comments

Comments
 (0)