forked from shiguredo-webrtc-build/webrtc-build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
1104 lines (952 loc) · 40.3 KB
/
run.py
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import subprocess
import json
import logging
import os
import urllib.parse
import zipfile
import tarfile
import shutil
import platform
import argparse
import collections
import re
from typing import Optional, Dict, List
logging.basicConfig(level=logging.INFO)
class ChangeDirectory(object):
def __init__(self, cwd):
self._cwd = cwd
def __enter__(self):
self._old_cwd = os.getcwd()
logging.debug(f'pushd {self._old_cwd} --> {self._cwd}')
os.chdir(self._cwd)
def __exit__(self, exctype, excvalue, trace):
logging.debug(f'popd {self._old_cwd} <-- {self._cwd}')
os.chdir(self._old_cwd)
return False
def cd(cwd):
return ChangeDirectory(cwd)
def cmd(args, **kwargs):
logging.debug(f'+{args} {kwargs}')
if 'check' not in kwargs:
kwargs['check'] = True
if 'resolve' in kwargs:
resolve = kwargs['resolve']
del kwargs['resolve']
else:
resolve = True
if resolve:
args = [shutil.which(args[0]), *args[1:]]
return subprocess.run(args, **kwargs)
# 標準出力をキャプチャするコマンド実行。シェルの `cmd ...` や $(cmd ...) と同じ
def cmdcap(args, **kwargs):
# 3.7 でしか使えない
# kwargs['capture_output'] = True
kwargs['stdout'] = subprocess.PIPE
kwargs['stderr'] = subprocess.PIPE
kwargs['encoding'] = 'utf-8'
return cmd(args, **kwargs).stdout.strip()
def rm_rf(path: str):
if not os.path.exists(path):
logging.debug(f'rm -rf {path} => path not found')
return
if os.path.isfile(path) or os.path.islink(path):
os.remove(path)
logging.debug(f'rm -rf {path} => file removed')
if os.path.isdir(path):
shutil.rmtree(path)
logging.debug(f'rm -rf {path} => directory removed')
def mkdir_p(path: str):
if os.path.exists(path):
logging.debug(f'mkdir -p {path} => already exists')
return
os.makedirs(path, exist_ok=True)
logging.debug(f'mkdir -p {path} => directory created')
if platform.system() == 'Windows':
PATH_SEPARATOR = ';'
else:
PATH_SEPARATOR = ':'
def add_path(path: str, is_after=False):
logging.debug(f'add_path: {path}')
if 'PATH' not in os.environ:
os.environ['PATH'] = path
return
if is_after:
os.environ['PATH'] = os.environ['PATH'] + PATH_SEPARATOR + path
else:
os.environ['PATH'] = path + PATH_SEPARATOR + os.environ['PATH']
def download(url: str, output_dir: Optional[str] = None, filename: Optional[str] = None) -> str:
if filename is None:
output_path = urllib.parse.urlparse(url).path.split('/')[-1]
else:
output_path = filename
if output_dir is not None:
output_path = os.path.join(output_dir, output_path)
if os.path.exists(output_path):
return output_path
try:
if shutil.which('curl') is not None:
cmd(["curl", "-fLo", output_path, url])
else:
cmd(["wget", "-cO", output_path, url])
except Exception:
# ゴミを残さないようにする
if os.path.exists(output_path):
os.remove(output_path)
raise
return output_path
def read_version_file(path: str) -> Dict[str, str]:
versions = {}
lines = open(path).readlines()
for line in lines:
line = line.strip()
# コメント行
if line[:1] == '#':
continue
# 空行
if len(line) == 0:
continue
[a, b] = map(lambda x: x.strip(), line.split('=', 2))
versions[a] = b.strip('"')
return versions
# dir 以下にある全てのファイルパスを、dir2 からの相対パスで返す
def enum_all_files(dir, dir2):
for root, _, files in os.walk(dir):
for file in files:
yield os.path.relpath(os.path.join(root, file), dir2)
def get_depot_tools(source_dir, fetch=False):
dir = os.path.join(source_dir, 'depot_tools')
if os.path.exists(dir):
if fetch:
cmd(['git', 'fetch'])
cmd(['git', 'checkout', '-f', 'origin/HEAD'])
else:
cmd(['git', 'clone', 'https://chromium.googlesource.com/chromium/tools/depot_tools.git', dir])
return dir
PATCH_INFO = {
'4k.patch': (2, []),
'macos_h264_encoder.patch': (2, []),
'macos_screen_capture.patch': (2, []),
}
PATCHES = {
'windows_x86_64': [
'4k.patch',
'add_license_dav1d.patch',
'windows_add_deps.patch',
'windows_silence_warnings.patch',
'ssl_verify_callback_with_native_handle.patch',
],
'windows_arm64': [
'4k.patch',
'add_license_dav1d.patch',
'windows_add_deps.patch',
'windows_silence_warnings.patch',
'ssl_verify_callback_with_native_handle.patch',
],
'macos_x86_64': [
'add_dep_zlib.patch',
'4k.patch',
'add_license_dav1d.patch',
'macos_h264_encoder.patch',
'macos_screen_capture.patch',
'macos_simulcast.patch',
'ios_simulcast.patch',
'ssl_verify_callback_with_native_handle.patch',
],
'macos_arm64': [
'add_dep_zlib.patch',
'4k.patch',
'add_license_dav1d.patch',
'macos_h264_encoder.patch',
'macos_screen_capture.patch',
'macos_simulcast.patch',
'ios_simulcast.patch',
'ssl_verify_callback_with_native_handle.patch',
],
'ios': [
'add_dep_zlib.patch',
'4k.patch',
'add_license_dav1d.patch',
'macos_h264_encoder.patch',
'macos_screen_capture.patch',
'macos_simulcast.patch',
'ios_manual_audio_input.patch',
'ios_simulcast.patch',
'ssl_verify_callback_with_native_handle.patch',
'ios_bitcode.patch',
],
'android': [
'add_dep_zlib.patch',
'4k.patch',
'add_license_dav1d.patch',
'ssl_verify_callback_with_native_handle.patch',
'android_webrtc_version.patch',
'android_fixsegv.patch',
'android_simulcast.patch',
'android_hardware_video_encoder.patch',
],
'raspberry-pi-os_armv6': [
'nacl_armv6_2.patch',
'add_dep_zlib.patch',
'4k.patch',
'add_license_dav1d.patch',
'ssl_verify_callback_with_native_handle.patch',
],
'raspberry-pi-os_armv7': [
'add_dep_zlib.patch',
'4k.patch',
'add_license_dav1d.patch',
'ssl_verify_callback_with_native_handle.patch',
],
'raspberry-pi-os_armv8': [
'add_dep_zlib.patch',
'4k.patch',
'add_license_dav1d.patch',
'ssl_verify_callback_with_native_handle.patch',
],
'ubuntu-18.04_armv8': [
'add_dep_zlib.patch',
'4k.patch',
'add_license_dav1d.patch',
'ssl_verify_callback_with_native_handle.patch',
],
'ubuntu-20.04_armv8': [
'add_dep_zlib.patch',
'4k.patch',
'add_license_dav1d.patch',
'ssl_verify_callback_with_native_handle.patch',
],
'ubuntu-18.04_x86_64': [
'4k.patch',
'add_license_dav1d.patch',
'ssl_verify_callback_with_native_handle.patch',
],
'ubuntu-20.04_x86_64': [
'4k.patch',
'add_license_dav1d.patch',
'ssl_verify_callback_with_native_handle.patch',
]
}
def apply_patch(patch, dir, depth):
with cd(dir):
if platform.system() == 'Windows':
cmd(['git', 'apply', f'-p{depth}',
'--ignore-space-change', '--ignore-whitespace', '--whitespace=nowarn',
patch])
else:
with open(patch) as stdin:
cmd(['patch', f'-p{depth}'], stdin=stdin)
def get_webrtc(source_dir, patch_dir, version, target,
webrtc_source_dir=None, force=False, fetch=False):
if webrtc_source_dir is None:
webrtc_source_dir = os.path.join(source_dir, 'webrtc')
if force:
rm_rf(webrtc_source_dir)
mkdir_p(webrtc_source_dir)
if not os.path.exists(os.path.join(webrtc_source_dir, 'src')):
with cd(webrtc_source_dir):
cmd(['gclient'])
cmd(['fetch', 'webrtc'])
if target == 'android':
with open('.gclient', 'a') as f:
f.write("target_os = [ 'android' ]\n")
if target == 'ios':
with open('.gclient', 'a') as f:
f.write("target_os = [ 'ios' ]\n")
fetch = True
src_dir = os.path.join(webrtc_source_dir, 'src')
if fetch:
with cd(src_dir):
cmd(['git', 'fetch'])
if version == 'HEAD':
cmd(['git', 'checkout', '-f', 'origin/HEAD'])
else:
cmd(['git', 'checkout', '-f', version])
cmd(['git', 'clean', '-df'])
cmd(['gclient', 'sync', '-D', '--force', '--reset', '--with_branch_heads'])
for patch in PATCHES[target]:
depth, dirs = PATCH_INFO.get(patch, (1, ['.']))
dir = os.path.join(src_dir, *dirs)
apply_patch(os.path.join(patch_dir, patch), dir, depth)
def git_get_url_and_revision(dir):
with cd(dir):
rev = cmdcap(['git', 'rev-parse', 'HEAD'])
url = cmdcap(['git', 'remote', 'get-url', 'origin'])
return url, rev
VersionInfo = collections.namedtuple('VersionInfo', [
'webrtc_version',
'webrtc_commit',
'webrtc_build_version',
])
def archive_objects(ar, dir, output):
with cd(dir):
files = cmdcap(['find', '.', '-name', '*.o']).splitlines()
rm_rf(output)
cmd([ar, '-rc', output, *files])
MultistrapConfig = collections.namedtuple('MultistrapConfig', [
'config_file',
'arch',
'triplet'
])
MULTISTRAP_CONFIGS = {
'raspberry-pi-os_armv6': MultistrapConfig(
config_file=['raspberry-pi-os_armv6', 'rpi-raspbian.conf'],
arch='armhf',
triplet='arm-linux-gnueabihf'
),
'raspberry-pi-os_armv7': MultistrapConfig(
config_file=['raspberry-pi-os_armv7', 'rpi-raspbian.conf'],
arch='armhf',
triplet='arm-linux-gnueabihf'
),
'raspberry-pi-os_armv8': MultistrapConfig(
config_file=['raspberry-pi-os_armv8', 'rpi-raspbian.conf'],
arch='arm64',
triplet='aarch64-linux-gnu'
),
'ubuntu-18.04_armv8': MultistrapConfig(
config_file=['ubuntu-18.04_armv8', 'arm64.conf'],
arch='arm64',
triplet='aarch64-linux-gnu'
),
'ubuntu-20.04_armv8': MultistrapConfig(
config_file=['ubuntu-20.04_armv8', 'arm64.conf'],
arch='arm64',
triplet='aarch64-linux-gnu'
),
}
def init_rootfs(sysroot: str, config: MultistrapConfig, force=False):
if force:
rm_rf(sysroot)
if os.path.exists(sysroot):
return
cmd(['multistrap', '--no-auth', '-a', config.arch, '-d', sysroot, '-f', os.path.join(*config.config_file)])
lines = cmdcap(['find', f'{sysroot}/usr/lib/{config.triplet}', '-lname', '/*', '-printf', '%p %l\n']).splitlines()
for line in lines:
[link, target] = line.split()
cmd(['ln', '-snfv', f'{sysroot}{target}', link])
lines = cmdcap(['find', f'{sysroot}/usr/lib/gcc/{config.triplet}',
'-lname', '/*', '-printf', '%p %l\n']).splitlines()
for line in lines:
[link, target] = line.split()
cmd(['ln', '-snfv', f'{sysroot}{target}', link])
lines = cmdcap(['find', f'{sysroot}/usr/lib/{config.triplet}/pkgconfig', '-printf', '%f\n']).splitlines()
for line in lines:
target = line.strip()
cmd(['ln', '-snfv', f'../../lib/{config.triplet}/pkgconfig/{target}',
f'{sysroot}/usr/share/pkgconfig/{target}'])
COMMON_GN_ARGS = [
"rtc_include_tests=false",
"rtc_use_h264=false",
"is_component_build=false",
'rtc_build_examples=false',
"use_rtti=true",
'rtc_build_tools=false',
]
WEBRTC_BUILD_TARGETS_MACOS_COMMON = [
'api/audio_codecs:builtin_audio_decoder_factory',
'api/task_queue:default_task_queue_factory',
'sdk:native_api',
'sdk:default_codec_factory_objc',
'pc:peerconnection',
'sdk:videocapture_objc',
]
WEBRTC_BUILD_TARGETS = {
'macos_x86_64': [*WEBRTC_BUILD_TARGETS_MACOS_COMMON, 'sdk:mac_framework_objc'],
'macos_arm64': [*WEBRTC_BUILD_TARGETS_MACOS_COMMON, 'sdk:mac_framework_objc'],
'ios': [*WEBRTC_BUILD_TARGETS_MACOS_COMMON, 'sdk:framework_objc'],
'android': ['sdk/android:libwebrtc', 'sdk/android:libjingle_peerconnection_so', 'sdk/android:native_api'],
}
def get_build_targets(target):
ts = [':default']
if target not in ('windows_x86_64', 'windows_arm64', 'ios', 'macos_x86_64', 'macos_arm64'):
ts += ['buildtools/third_party/libc++']
ts += WEBRTC_BUILD_TARGETS.get(target, [])
return ts
IOS_ARCHS = ['simulator:x64', 'device:arm64']
IOS_FRAMEWORK_ARCHS = ['simulator:x64', 'simulator:arm64', 'device:arm64']
def to_gn_args(gn_args: List[str], extra_gn_args: str) -> str:
s = ' '.join(gn_args)
if len(extra_gn_args) == 0:
return s
return s + ' ' + extra_gn_args
def gn_gen(webrtc_src_dir: str, webrtc_build_dir: str, gn_args: List[str], extra_gn_args: str):
with cd(webrtc_src_dir):
args = ['gn', 'gen', webrtc_build_dir, '--args=' + to_gn_args(gn_args, extra_gn_args)]
logging.info(' '.join(args))
return cmd(args)
def get_webrtc_version_info(version_info: VersionInfo):
xs = version_info.webrtc_version.split('.')
ys = version_info.webrtc_build_version.split('.')
if len(xs) >= 3 and len(ys) >= 4:
branch = 'M' + version_info.webrtc_version.split('.')[0]
commit = version_info.webrtc_version.split('.')[2]
revision = version_info.webrtc_commit
maint = version_info.webrtc_build_version.split('.')[3]
else:
# HEAD ビルドだと正しくバージョンが取れないので、その場合は適当に空文字を入れておく
branch = ''
commit = ''
revision = ''
maint = ''
return [branch, commit, revision, maint]
def build_webrtc_ios(
source_dir, build_dir, version_info: VersionInfo, extra_gn_args,
webrtc_source_dir=None, webrtc_build_dir=None,
debug=False,
gen=False, gen_force=False,
nobuild=False, nobuild_framework=False,
overlap_build_dir=False):
if webrtc_source_dir is None:
webrtc_source_dir = os.path.join(source_dir, 'webrtc')
if webrtc_build_dir is None:
webrtc_build_dir = os.path.join(build_dir, 'webrtc')
webrtc_src_dir = os.path.join(webrtc_source_dir, 'src')
mkdir_p(webrtc_build_dir)
mkdir_p(os.path.join(webrtc_build_dir, 'framework'))
# - M92-M93 あたりで clang++: error: -gdwarf-aranges is not supported with -fembed-bitcode
# がでていたので use_xcode_clang=false をすることで修正
# - M94 で use_xcode_clang=true かつ --bitcode を有効にしてビルドが通り bitcode が有効になってることを確認
# - M95 で再度 clang++: error: -gdwarf-aranges is not supported with -fembed-bitcode エラーがでるようになった
# - https://webrtc-review.googlesource.com/c/src/+/232600 が影響している可能性があるため use_lld=false を追加
gn_args_base = [
'rtc_libvpx_build_vp9=true',
'enable_dsyms=true',
'use_lld=false',
'rtc_enable_objc_symbol_export=true',
*COMMON_GN_ARGS,
]
# WebRTC.xcframework のビルド
if not nobuild_framework:
gn_args = [
*gn_args_base,
]
cmd([
os.path.join(webrtc_src_dir, 'tools_webrtc', 'ios', 'build_ios_libs.sh'),
'-o', os.path.join(webrtc_build_dir, 'framework'),
'--build_config', 'debug' if debug else 'release',
'--arch', *IOS_FRAMEWORK_ARCHS,
'--bitcode',
'--extra-gn-args', to_gn_args(gn_args, extra_gn_args)
])
info = {}
branch, commit, revision, maint = get_webrtc_version_info(version_info)
info['branch'] = branch
info['commit'] = commit
info['revision'] = revision
info['maint'] = maint
with open(os.path.join(webrtc_build_dir, 'framework', 'WebRTC.xcframework', 'build_info.json'), 'w') as f:
f.write(json.dumps(info, indent=4))
libs = []
for device_arch in IOS_ARCHS:
[device, arch] = device_arch.split(':')
if overlap_build_dir:
work_dir = os.path.join(webrtc_build_dir, 'framework', device, f'{arch}_libs')
else:
work_dir = os.path.join(webrtc_build_dir, device, arch)
if gen_force:
rm_rf(work_dir)
with cd(os.path.join(webrtc_src_dir, 'tools_webrtc', 'ios')):
ios_deployment_target = cmdcap(
['python3', '-c',
f'from build_ios_libs import IOS_DEPLOYMENT_TARGET; print(IOS_DEPLOYMENT_TARGET["{device}"])'])
if not os.path.exists(os.path.join(work_dir, 'args.gn')) or gen or overlap_build_dir:
gn_args = [
f"is_debug={'true' if debug else 'false'}",
'target_os="ios"',
f'target_cpu="{arch}"',
f'target_environment="{device}"',
"ios_enable_code_signing=false",
f'ios_deployment_target="{ios_deployment_target}"',
'enable_ios_bitcode=true',
f"enable_stripping={'false' if debug else 'true'}",
*gn_args_base,
]
gn_gen(webrtc_src_dir, work_dir, gn_args, extra_gn_args)
if not nobuild:
cmd(['ninja', '-C', work_dir, *get_build_targets('ios')])
ar = '/usr/bin/ar'
archive_objects(ar, os.path.join(work_dir, 'obj'), os.path.join(work_dir, 'libwebrtc.a'))
libs.append(os.path.join(work_dir, 'libwebrtc.a'))
cmd(['lipo', *libs, '-create', '-output', os.path.join(webrtc_build_dir, 'libwebrtc.a')])
ANDROID_ARCHS = ['armeabi-v7a', 'arm64-v8a']
ANDROID_TARGET_CPU = {
'armeabi-v7a': 'arm',
'arm64-v8a': 'arm64',
}
def build_webrtc_android(
source_dir, build_dir, version_info: VersionInfo, extra_gn_args,
webrtc_source_dir=None, webrtc_build_dir=None,
debug=False,
gen=False, gen_force=False,
nobuild=False, nobuild_aar=False):
if webrtc_source_dir is None:
webrtc_source_dir = os.path.join(source_dir, 'webrtc')
if webrtc_build_dir is None:
webrtc_build_dir = os.path.join(build_dir, 'webrtc')
webrtc_src_dir = os.path.join(webrtc_source_dir, 'src')
mkdir_p(webrtc_build_dir)
# Java ファイル作成
branch, commit, revision, maint = get_webrtc_version_info(version_info)
name = 'WebrtcBuildVersion'
lines = []
lines.append('package org.webrtc;')
lines.append(f'public interface {name} {{')
lines.append(f' public static final String webrtc_branch = "{branch}";')
lines.append(f' public static final String webrtc_commit = "{commit}";')
lines.append(f' public static final String webrtc_revision = "{revision}";')
lines.append(f' public static final String maint_version = "{maint}";')
lines.append('}')
with open(os.path.join(webrtc_src_dir, 'sdk', 'android', 'api', 'org', 'webrtc', f'{name}.java'), 'wb') as f:
f.writelines(map(lambda x: (x + '\n').encode('utf-8'), lines))
gn_args_base = [
f"is_debug={'true' if debug else 'false'}",
f"is_java_debug={'true' if debug else 'false'}",
*COMMON_GN_ARGS
]
# aar 生成
if not nobuild_aar:
work_dir = os.path.join(webrtc_build_dir, 'aar')
mkdir_p(work_dir)
gn_args = [*gn_args_base]
with cd(webrtc_src_dir):
cmd(['python3', os.path.join(webrtc_src_dir, 'tools_webrtc', 'android', 'build_aar.py'),
'--build-dir', work_dir,
'--output', os.path.join(work_dir, 'libwebrtc.aar'),
'--arch', *ANDROID_ARCHS,
'--extra-gn-args', to_gn_args(gn_args, extra_gn_args)])
for arch in ANDROID_ARCHS:
work_dir = os.path.join(webrtc_build_dir, arch)
if gen_force:
rm_rf(work_dir)
if not os.path.exists(os.path.join(work_dir, 'args.gn')) or gen:
gn_args = [
*gn_args_base,
'target_os="android"',
f'target_cpu="{ANDROID_TARGET_CPU[arch]}"',
]
gn_gen(webrtc_src_dir, work_dir, gn_args, extra_gn_args)
if not nobuild:
cmd(['ninja', '-C', work_dir, *get_build_targets('android')])
ar = os.path.join(webrtc_src_dir, 'third_party/llvm-build/Release+Asserts/bin/llvm-ar')
archive_objects(ar, os.path.join(work_dir, 'obj'), os.path.join(work_dir, 'libwebrtc.a'))
def build_webrtc(
source_dir, build_dir, target: str, version_info: VersionInfo, extra_gn_args,
webrtc_source_dir=None, webrtc_build_dir=None,
debug=False,
gen=False, gen_force=False,
nobuild=False, nobuild_macos_framework=False):
if webrtc_source_dir is None:
webrtc_source_dir = os.path.join(source_dir, 'webrtc')
if webrtc_build_dir is None:
webrtc_build_dir = os.path.join(build_dir, 'webrtc')
webrtc_src_dir = os.path.join(webrtc_source_dir, 'src')
mkdir_p(webrtc_build_dir)
# ビルド
if gen_force:
rm_rf(webrtc_build_dir)
if not os.path.exists(os.path.join(webrtc_build_dir, 'args.gn')) or gen:
gn_args = [
f"is_debug={'true' if debug else 'false'}",
*COMMON_GN_ARGS,
]
if target in ['windows_x86_64', 'windows_arm64']:
gn_args += [
'target_os="win"',
f'target_cpu="{"x64" if target == "windows_x86_64" else "arm64"}"',
"use_custom_libcxx=false",
]
elif target in ('macos_x86_64', 'macos_arm64'):
gn_args += [
'target_os="mac"',
f'target_cpu="{"x64" if target == "macos_x86_64" else "arm64"}"',
'mac_deployment_target="10.11"',
'enable_stripping=true',
'enable_dsyms=true',
'rtc_libvpx_build_vp9=true',
'rtc_enable_symbol_export=true',
'rtc_enable_objc_symbol_export=false',
]
elif target in ('raspberry-pi-os_armv6',
'raspberry-pi-os_armv7',
'raspberry-pi-os_armv8',
'ubuntu-18.04_armv8',
'ubuntu-20.04_armv8'):
sysroot = os.path.join(source_dir, 'rootfs')
gn_args += [
'target_os="linux"',
f'target_cpu="{"arm64" if target in ("raspberry-pi-os_armv8", "ubuntu-18.04_armv8", "ubuntu-20.04_armv8") else "arm"}"',
f'target_sysroot="{sysroot}"',
'rtc_use_pipewire=false',
]
if target == 'raspberry-pi-os_armv6':
gn_args += [
'arm_version=6',
'arm_arch="armv6"',
'arm_tune="arm1176jzf-s"',
'arm_fpu="vfpv2"',
'arm_float_abi="hard"',
'arm_use_neon=false',
'enable_libaom=false',
]
elif target in ('ubuntu-18.04_x86_64', 'ubuntu-20.04_x86_64'):
gn_args += [
'target_os="linux"',
'rtc_use_pipewire=false',
]
else:
raise Exception(f'Target {target} is not supported')
gn_gen(webrtc_src_dir, webrtc_build_dir, gn_args, extra_gn_args)
if nobuild:
return
cmd(['ninja', '-C', webrtc_build_dir, *get_build_targets(target)])
if target in ['windows_x86_64', 'windows_arm64']:
pass
elif target in ('macos_x86_64', 'macos_arm64'):
ar = '/usr/bin/ar'
else:
ar = os.path.join(webrtc_src_dir, 'third_party/llvm-build/Release+Asserts/bin/llvm-ar')
# ar で libwebrtc.a を生成する
if target not in ['windows_x86_64', 'windows_arm64']:
archive_objects(ar, os.path.join(webrtc_build_dir, 'obj'), os.path.join(webrtc_build_dir, 'libwebrtc.a'))
# macOS の場合は WebRTC.framework に追加情報を入れる
if (target in ('macos_x86_64', 'macos_arm64')) and not nobuild_macos_framework:
branch, commit, revision, maint = get_webrtc_version_info(version_info)
info = {}
info['branch'] = branch
info['commit'] = commit
info['revision'] = revision
info['maint'] = maint
with open(os.path.join(webrtc_build_dir, 'WebRTC.framework', 'Resources', 'build_info.json'), 'w') as f:
f.write(json.dumps(info, indent=4))
# Info.plistの編集(tools_wertc/ios/build_ios_libs.py内の処理を踏襲)
info_plist_path = os.path.join(webrtc_build_dir, 'WebRTC.framework', 'Resources', 'Info.plist')
ver = cmdcap(['/usr/libexec/PlistBuddy', '-c', 'Print :CFBundleShortVersionString', info_plist_path],
resolve=False)
cmd(['/usr/libexec/PlistBuddy', '-c',
f'Set :CFBundleVersion {ver}.0', info_plist_path], resolve=False, encoding='utf-8')
cmd(['plutil', '-convert', 'binary1', info_plist_path])
# xcframeworkの作成
rm_rf(os.path.join(webrtc_build_dir, 'WebRTC.xcframework'))
cmd(['xcodebuild', '-create-xcframework',
'-framework', os.path.join(webrtc_build_dir, 'WebRTC.framework'),
'-debug-symbols', os.path.join(webrtc_build_dir, 'WebRTC.dSYM'),
'-output', os.path.join(webrtc_build_dir, 'WebRTC.xcframework')])
def copy_headers(webrtc_src_dir, webrtc_package_dir, target):
if target in ['windows_x86_64', 'windows_arm64']:
# robocopy の戻り値は特殊なので、check=False にしてうまくエラーハンドリングする
# https://docs.microsoft.com/ja-jp/troubleshoot/windows-server/backup-and-storage/return-codes-used-robocopy-utility
r = cmd(['robocopy', webrtc_src_dir, os.path.join(webrtc_package_dir, 'include'),
'*.h', '*.hpp', '/S', '/NP', '/NFL', '/NDL'], check=False)
if r.returncode >= 4:
raise Exception('robocopy failed')
else:
mkdir_p(os.path.join(webrtc_package_dir, 'include'))
cmd(['rsync', '-amv', '--include=*/', '--include=*.h', '--include=*.hpp', '--exclude=*',
os.path.join(webrtc_src_dir, '.'), os.path.join(webrtc_package_dir, 'include', '.')])
def generate_version_info(webrtc_src_dir, webrtc_package_dir):
lines = []
GIT_INFOS = [
(['.'], ''),
(['build'], 'BUILD'),
(['buildtools'], 'BUILDTOOLS'),
(['buildtools', 'third_party', 'libc++', 'trunk'], 'BUILDTOOLS_THIRD_PARTY_LIBCXX_TRUNK'),
(['buildtools', 'third_party', 'libc++abi', 'trunk'], 'BUILDTOOLS_THIRD_PARTY_LIBCXXABI_TRUNK'),
(['buildtools', 'third_party', 'libunwind', 'trunk'], 'BUILDTOOLS_THIRD_PARTY_LIBUNWIND_TRUNK'),
(['third_party'], 'THIRD_PARTY'),
(['tools'], 'TOOLS'),
]
for dirs, name in GIT_INFOS:
url, rev = git_get_url_and_revision(os.path.join(webrtc_src_dir, *dirs))
prefix = 'WEBRTC_SRC_' + (f'{name}_' if len(name) != 0 else '')
lines += [
f'{prefix}URL={url}',
f'{prefix}COMMIT={rev}',
]
shutil.copyfile('VERSION', os.path.join(webrtc_package_dir, 'VERSIONS'))
with open(os.path.join(webrtc_package_dir, 'VERSIONS'), 'ab') as f:
f.writelines(map(lambda x: (x + '\n').encode('utf-8'), lines))
def package_webrtc(source_dir, build_dir, package_dir, target,
webrtc_source_dir=None, webrtc_build_dir=None, webrtc_package_dir=None,
overlap_ios_build_dir=False):
if webrtc_source_dir is None:
webrtc_source_dir = os.path.join(source_dir, 'webrtc')
if webrtc_build_dir is None:
webrtc_build_dir = os.path.join(build_dir, 'webrtc')
if webrtc_package_dir is None:
webrtc_package_dir = os.path.join(package_dir, 'webrtc')
webrtc_src_dir = os.path.join(webrtc_source_dir, 'src')
rm_rf(webrtc_package_dir)
mkdir_p(webrtc_package_dir)
# ライセンス生成
if target == 'android':
dirs = []
for arch in ANDROID_ARCHS:
dirs += [
os.path.join(webrtc_build_dir, arch),
os.path.join(webrtc_build_dir, 'aar', arch)
]
elif target == 'ios':
dirs = []
for device_arch in IOS_FRAMEWORK_ARCHS:
[device, arch] = device_arch.split(':')
dirs.append(os.path.join(webrtc_build_dir,
'framework', device, f'{arch}_libs'))
if not overlap_ios_build_dir:
for device_arch in IOS_ARCHS:
[device, arch] = device_arch.split(':')
dirs.append(os.path.join(webrtc_build_dir, device, arch))
else:
dirs = [webrtc_build_dir]
ts = []
for t in get_build_targets(target):
ts += ['--target', t]
cmd(['python3', os.path.join(webrtc_src_dir, 'tools_webrtc', 'libs', 'generate_licenses.py'),
*ts, webrtc_package_dir, *dirs])
os.rename(os.path.join(webrtc_package_dir, 'LICENSE.md'), os.path.join(webrtc_package_dir, 'NOTICE'))
# ヘッダーファイルをコピー
copy_headers(webrtc_src_dir, webrtc_package_dir, target)
# バージョン情報
generate_version_info(webrtc_src_dir, webrtc_package_dir)
# ライブラリ
if target in ['windows_x86_64', 'windows_arm64']:
files = [
(['obj', 'webrtc.lib'], ['lib', 'webrtc.lib']),
]
elif target in ('macos_x86_64', 'macos_arm64'):
files = [
(['libwebrtc.a'], ['lib', 'libwebrtc.a']),
(['WebRTC.xcframework'], ['Frameworks', 'WebRTC.xcframework']),
]
elif target == 'ios':
files = [
(['libwebrtc.a'], ['lib', 'libwebrtc.a']),
(['framework', 'WebRTC.xcframework'], ['Frameworks', 'WebRTC.xcframework']),
]
elif target == 'android':
# aar を展開して classes.jar を取り出す
tmp = os.path.join(webrtc_build_dir, 'tmp')
rm_rf(tmp)
mkdir_p(tmp)
with cd(tmp):
cmd(['unzip', os.path.join(webrtc_build_dir, 'aar', 'libwebrtc.aar')])
dstpath = os.path.join(webrtc_build_dir, 'aar', 'webrtc.jar')
rm_rf(dstpath)
os.rename('classes.jar', dstpath)
rm_rf(tmp)
files = [
(['aar', 'libwebrtc.aar'], ['aar', 'libwebrtc.aar']),
(['aar', 'webrtc.jar'], ['jar', 'webrtc.jar']),
]
for arch in ANDROID_ARCHS:
files.append(([arch, 'libwebrtc.a'], ['lib', arch, 'libwebrtc.a']))
else:
files = [
(['libwebrtc.a'], ['lib', 'libwebrtc.a']),
]
for src, dst in files:
dstpath = os.path.join(webrtc_package_dir, *dst)
mkdir_p(os.path.dirname(dstpath))
srcpath = os.path.join(webrtc_build_dir, *src)
if os.path.isdir(srcpath):
shutil.copytree(srcpath, dstpath)
else:
shutil.copy2(os.path.join(webrtc_build_dir, *src), dstpath)
# 圧縮
with cd(package_dir):
if target in ['windows_x86_64', 'windows_arm64']:
with zipfile.ZipFile('webrtc.zip', 'w') as f:
for file in enum_all_files('webrtc', '.'):
f.write(filename=file, arcname=file)
else:
with tarfile.open('webrtc.tar.gz', 'w:gz') as f:
for file in enum_all_files('webrtc', '.'):
f.add(name=file, arcname=file)
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
TARGETS = [
'windows_x86_64',
'windows_arm64',
'macos_x86_64',
'macos_arm64',
'ubuntu-18.04_x86_64',
'ubuntu-20.04_x86_64',
'ubuntu-18.04_armv8',
'ubuntu-20.04_armv8',
'raspberry-pi-os_armv6',
'raspberry-pi-os_armv7',
'raspberry-pi-os_armv8',
'android',
'ios',
]
def check_target(target):
logging.debug(f'uname: {platform.uname()}')
if platform.system() == 'Windows':
logging.info(f'OS: {platform.system()}')
return target in ['windows_x86_64', 'windows_arm64']
elif platform.system() == 'Darwin':
logging.info(f'OS: {platform.system()}')
return target in ('macos_x86_64', 'macos_arm64', 'ios')
elif platform.system() == 'Linux':
release = read_version_file('/etc/os-release')
os = release['NAME']
logging.info(f'OS: {os}')
if os != 'Ubuntu':
return False
# x86_64 環境以外ではビルド不可
arch = platform.machine()
logging.info(f'Arch: {arch}')
if arch not in ('AMD64', 'x86_64'):
return False
# クロスコンパイルなので Ubuntu だったら任意のバージョンでビルド可能(なはず)
if target in ('ubuntu-18.04_armv8',
'ubuntu-20.04_armv8',
'raspberry-pi-os_armv6',
'raspberry-pi-os_armv7',
'raspberry-pi-os_armv8',
'android'):
return True
# x86_64 用ビルドはバージョンが合っている必要がある
osver = release['VERSION_ID']
logging.info(f'OS Version: {osver}')
if target == 'ubuntu-18.04_x86_64' and osver == '18.04':
return True
if target == 'ubuntu-20.04_x86_64' and osver == '20.04':
return True
return False
else:
return False
def main():
"""
メモ
ビルド方針:
- 引数無しで実行した場合、ビルドのみ行う
- もし必要とするファイルが存在しなければ取得や生成を行うが、新しい更新があるかどうかは確認しない。
- 各種引数を渡すと、更新や生成を行う。
- fetch 系: 各種ソースを更新する
- fetch-force 系: 一旦全て削除してから取得し直す
- gen 系: 既存のビルドディレクトリの上に gn gen を行う
- gen-force 系: 既存のビルドディレクトリは完全に削除してから gn gen をやり直す
- nobuild 系: ビルドを行わない
"""
parser = argparse.ArgumentParser()
sp = parser.add_subparsers()
bp = sp.add_parser('build')
bp.set_defaults(op='build')
bp.add_argument("target", choices=TARGETS)
bp.add_argument("--debug", action='store_true')
bp.add_argument("--source-dir")
bp.add_argument("--build-dir")
bp.add_argument("--rootfs-fetch-force", action='store_true')
bp.add_argument('--depottools-fetch', action='store_true')
bp.add_argument("--webrtc-fetch", action='store_true')
bp.add_argument("--webrtc-fetch-force", action='store_true')
bp.add_argument("--webrtc-gen", action='store_true')
bp.add_argument("--webrtc-gen-force", action='store_true')
bp.add_argument("--webrtc-extra-gn-args", default='')
bp.add_argument("--webrtc-nobuild", action='store_true')
bp.add_argument("--webrtc-nobuild-ios-framework", action='store_true')
bp.add_argument("--webrtc-nobuild-android-aar", action='store_true')
bp.add_argument("--webrtc-overlap-ios-build-dir", action='store_true')
bp.add_argument("--webrtc-build-dir")
bp.add_argument("--webrtc-source-dir")
# 現在 build と package を分ける意味は無いのだけど、
# 今後複数のビルドを纏めてパッケージングする時に備えて別コマンドにしておく
pp = sp.add_parser('package')
pp.set_defaults(op='package')
pp.add_argument("target", choices=TARGETS)
pp.add_argument("--debug", action='store_true')
pp.add_argument("--source-dir")
pp.add_argument("--build-dir")
pp.add_argument("--package-dir")
pp.add_argument("--webrtc-build-dir")
pp.add_argument("--webrtc-source-dir")
pp.add_argument("--webrtc-package-dir")
pp.add_argument("--webrtc-overlap-ios-build-dir", action='store_true')
args = parser.parse_args()
if not hasattr(args, 'op'):