@@ -529,32 +529,6 @@ def get_default_variation(cls, **kwargs):
529
529
return super ().get_default_variation (is_debug = settings .ASSERTIONS , ** kwargs )
530
530
531
531
532
- class OptimizedAggressivelyForSizeLibrary (Library ):
533
- def __init__ (self , ** kwargs ):
534
- self .is_optz = kwargs .pop ('is_optz' )
535
- super ().__init__ (** kwargs )
536
-
537
- def get_base_name (self ):
538
- name = super ().get_base_name ()
539
- if self .is_optz :
540
- name += '-optz'
541
- return name
542
-
543
- def get_cflags (self ):
544
- cflags = super ().get_cflags ()
545
- if self .is_optz :
546
- cflags += ['-DEMSCRIPTEN_OPTIMIZE_FOR_OZ' ]
547
- return cflags
548
-
549
- @classmethod
550
- def vary_on (cls ):
551
- return super ().vary_on () + ['is_optz' ]
552
-
553
- @classmethod
554
- def get_default_variation (cls , ** kwargs ):
555
- return super ().get_default_variation (is_optz = settings .SHRINK_LEVEL >= 2 , ** kwargs )
556
-
557
-
558
532
class Exceptions (IntEnum ):
559
533
"""
560
534
This represents exception handling mode of Emscripten. Currently there are
@@ -727,7 +701,6 @@ class libnoexit(Library):
727
701
728
702
class libc (MuslInternalLibrary ,
729
703
DebugLibrary ,
730
- OptimizedAggressivelyForSizeLibrary ,
731
704
AsanInstrumentedLibrary ,
732
705
MTLibrary ):
733
706
name = 'libc'
@@ -782,7 +755,6 @@ def get_libcall_files(self):
782
755
'__math_oflow.c' , '__math_oflowf.c' ,
783
756
'__math_uflow.c' , '__math_uflowf.c' ,
784
757
'__math_invalid.c' , '__math_invalidf.c' , '__math_invalidl.c' ,
785
- 'pow_small.c' , 'log_small.c' , 'log2_small.c' ,
786
758
'pow.c' , 'pow_data.c' , 'log.c' , 'log_data.c' , 'log2.c' , 'log2_data.c'
787
759
]
788
760
math_files = files_in_path (path = 'system/lib/libc/musl/src/math' , filenames = math_files )
@@ -908,10 +880,10 @@ def get_files(self):
908
880
'proxying_stub.c' ,
909
881
])
910
882
911
- if self . is_optz :
912
- ignore += ['pow.c' , 'pow_data.c' , 'log.c' , 'log_data.c' , 'log2.c' , 'log2_data.c' ]
913
- else :
914
- ignore += [ 'pow_small.c' , 'log_small.c' , 'log2_small.c' ]
883
+ # These files are in libc directories, but only built in libc_optz.
884
+ ignore += [
885
+ 'pow_small.c' , 'log_small.c' , 'log2_small.c'
886
+ ]
915
887
916
888
ignore = set (ignore )
917
889
for dirpath , dirnames , filenames in os .walk (musl_srcdir ):
@@ -1041,6 +1013,56 @@ def customize_build_cmd(self, cmd, filename):
1041
1013
return cmd
1042
1014
1043
1015
1016
+ # Contains the files from libc that are optimized differently in -Oz mode, where
1017
+ # we want to aggressively optimize them for size. This is linked in before libc
1018
+ # so we can override those specific files, when in -Oz.
1019
+ class libc_optz (libc ):
1020
+ name = 'libc_optz'
1021
+
1022
+ cflags = ['-Os' , '-fno-builtin' , '-DEMSCRIPTEN_OPTIMIZE_FOR_OZ' ]
1023
+
1024
+ def __init__ (self , ** kwargs ):
1025
+ super ().__init__ (** kwargs )
1026
+ self .non_lto_files = self .get_libcall_files ()
1027
+
1028
+ def get_libcall_files (self ):
1029
+ # see comments in libc.customize_build_cmd
1030
+
1031
+ # some files also appear in libc, and a #define affects them
1032
+ mem_files = files_in_path (
1033
+ path = 'system/lib/libc' ,
1034
+ filenames = ['emscripten_memcpy.c' , 'emscripten_memset.c' ,
1035
+ 'emscripten_memmove.c' ])
1036
+
1037
+ # some functions have separate files
1038
+ math_files = files_in_path (
1039
+ path = 'system/lib/libc/musl/src/math' ,
1040
+ filenames = ['pow_small.c' , 'log_small.c' , 'log2_small.c' ])
1041
+
1042
+ return mem_files + math_files
1043
+
1044
+ def get_files (self ):
1045
+ libcall_files = self .get_libcall_files ()
1046
+
1047
+ # some files also appear in libc, and a #define affects them
1048
+ mem_files = files_in_path (
1049
+ path = 'system/lib/libc/musl/src/string' ,
1050
+ filenames = ['memcmp.c' ])
1051
+
1052
+ return libcall_files + mem_files
1053
+
1054
+ def customize_build_cmd (self , cmd , filename ):
1055
+ if filename in self .non_lto_files :
1056
+ # see comments in libc.customize_build_cmd
1057
+ cmd = [a for a in cmd if not a .startswith ('-flto' )]
1058
+ cmd = [a for a in cmd if not a .startswith ('-O' )]
1059
+ cmd += ['-O2' ]
1060
+ return cmd
1061
+
1062
+ def can_use (self ):
1063
+ return super (libc_optz , self ).can_use () and settings .SHRINK_LEVEL >= 2
1064
+
1065
+
1044
1066
class libprintf_long_double (libc ):
1045
1067
name = 'libprintf_long_double'
1046
1068
cflags = ['-DEMSCRIPTEN_PRINTF_LONG_DOUBLE' ]
@@ -1835,9 +1857,16 @@ def add_library(libname):
1835
1857
if settings .JS_MATH :
1836
1858
add_library ('libjsmath' )
1837
1859
1838
- # to override the normal libc printf, we must come before it
1860
+ # C libraries that override libc must come before it
1839
1861
if settings .PRINTF_LONG_DOUBLE :
1840
1862
add_library ('libprintf_long_double' )
1863
+ # libc_optz is a size optimization, and therefore not really important when
1864
+ # MAIN_MODULE=1 (which links in all system libraries, leading to a large
1865
+ # size far bigger than any savings from libc_optz). Also, libc_optz overrides
1866
+ # parts of libc, which will not link due to --whole-archive in MAIN_MODULE=1
1867
+ # currently.
1868
+ if settings .SHRINK_LEVEL >= 2 and settings .MAIN_MODULE != 1 :
1869
+ add_library ('libc_optz' )
1841
1870
1842
1871
if settings .STANDALONE_WASM :
1843
1872
add_library ('libstandalonewasm' )
0 commit comments