Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tools/ports/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def build_port(src_dir, output_path, port_name, includes=[], flags=[], cxxflags=
os.makedirs(build_dir, exist_ok=True)
ninja_file = os.path.join(build_dir, 'build.ninja')
system_libs.ensure_sysroot()
system_libs.create_ninja_file(srcs, ninja_file, output_path, cflags=cflags)
system_libs.create_ninja_file(srcs, ninja_file, output_path, cflags=cflags, cxxflags=cflags + cxxflags)
if not os.getenv('EMBUILDER_PORT_BUILD_DEFERRED'):
system_libs.run_ninja(build_dir)
else:
Expand Down
82 changes: 47 additions & 35 deletions tools/system_libs.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def escape_ninja_path(path):
return re.sub(r'([ :$])', r'$\1', path)


def create_ninja_file(input_files, filename, libname, cflags, asflags=None, customize_build_flags=None):
def create_ninja_file(input_files, filename, libname, cflags, cxxflags, asflags=None, customize_build_flags=None):
if asflags is None:
asflags = []

Expand All @@ -193,6 +193,7 @@ def create_ninja_file(input_files, filename, libname, cflags, asflags=None, cust

ASFLAGS = {join_cmd(asflags)}
CFLAGS = {join_cmd(cflags)}
CXXFLAGS = {join_cmd(cxxflags)}
EMCC = {shared.EMCC}
EMXX = {shared.EMXX}
EMAR = {shared.EMAR}
Expand All @@ -204,7 +205,7 @@ def create_ninja_file(input_files, filename, libname, cflags, asflags=None, cust

rule cxx
depfile = $out.d
command = $EMXX -MD -MF $out.d $CFLAGS -c $in -o $out
command = $EMXX -MD -MF $out.d $CXXFLAGS -c $in -o $out
description = CXX $out

rule asm
Expand Down Expand Up @@ -266,12 +267,15 @@ def create_ninja_file(input_files, filename, libname, cflags, asflags=None, cust
flags = cflags
case _:
cmd = 'cxx'
flags = cflags
flags = cxxflags
out += f'build {escape_ninja_path(o)}: {cmd} {escape_ninja_path(src)}\n'
if customize_build_flags:
custom_flags = customize_build_flags(flags, src)
if custom_flags != flags:
out += f' CFLAGS = {join_cmd(custom_flags)}'
if flags == cflags:
out += f' CFLAGS = {join_cmd(custom_flags)}'
else:
out += f' CXXFLAGS = {join_cmd(custom_flags)}'
out += '\n'

objects = sorted(objects, key=objectfile_sort_key)
Expand Down Expand Up @@ -474,11 +478,14 @@ def generate_ninja(self, build_dir, libname):
utils.safe_ensure_dirs(build_dir)
self.build_dir = build_dir

cflags = self.get_cflags()
# Use random file names with .c and .cpp extensions
cflags = self.get_cflags('test.c')
cxxflags = self.get_cflags('test.cpp')

asflags = get_base_cflags(self.build_dir, preprocess=False)
input_files = self.get_files()
ninja_file = os.path.join(build_dir, 'build.ninja')
create_ninja_file(input_files, ninja_file, libname, cflags, asflags=asflags, customize_build_flags=self.customize_build_cmd)
create_ninja_file(input_files, ninja_file, libname, cflags, cxxflags, asflags=asflags, customize_build_flags=self.customize_build_cmd)

def build_objects(self, build_dir):
"""
Expand All @@ -492,7 +499,6 @@ def build_objects(self, build_dir):
batches = {}
commands = []
objects = set()
cflags = self.get_cflags()
for src in self.get_files():
ext = utils.suffix(src)
if ext in {'.s', '.S', '.c'}:
Expand All @@ -506,7 +512,7 @@ def build_objects(self, build_dir):
# `-sMEMORY64`.
cmd += get_base_cflags(self.build_dir, preprocess=False)
else:
cmd += cflags
cmd += self.get_cflags(src)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we already have the extension calculated here perhaps we can add self.cxxflags here when we have a cxx extension?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if I understand... We don't have self.cflags either. Do you want to add both? But then why would we have both self.cflags and self.get_cflags() then?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do have self.cflags and the default self.get_cflags() will read from this field.

The idea is that for simple libraries where the cflags don't vary you can just do cflags = ['x', 'y', 'z'] at the class level.

See libunwind for example.

In this case we could just add support for cxxflags = ['x', 'y', 'z']?

cmd = self.customize_build_cmd(cmd, src)

object_basename = utils.unsuffixed_basename(src).lower()
Expand Down Expand Up @@ -586,7 +592,7 @@ def _inherit_list(cls, attr):
result += item.__dict__.get(attr, [])
return result

def get_cflags(self):
def get_cflags(self, _filename):
"""
Returns the list of flags to pass to emcc when building this variation
of the library.
Expand Down Expand Up @@ -710,8 +716,8 @@ def __init__(self, **kwargs):
self.is_ww = kwargs.pop('is_ww') and not self.is_mt
super().__init__(**kwargs)

def get_cflags(self):
cflags = super().get_cflags()
def get_cflags(self, filename):
cflags = super().get_cflags(filename)
if self.is_mt:
cflags += ['-pthread', '-sWASM_WORKERS']
if self.is_ww:
Expand Down Expand Up @@ -751,8 +757,8 @@ def __init__(self, **kwargs):
self.is_debug = kwargs.pop('is_debug')
super().__init__(**kwargs)

def get_cflags(self):
cflags = super().get_cflags()
def get_cflags(self, filename):
cflags = super().get_cflags(filename)
if not self.is_debug:
cflags += ['-DNDEBUG']
return cflags
Expand Down Expand Up @@ -795,8 +801,8 @@ def __init__(self, **kwargs):
self.eh_mode = kwargs.pop('eh_mode')
super().__init__(**kwargs)

def get_cflags(self):
cflags = super().get_cflags()
def get_cflags(self, filename):
cflags = super().get_cflags(filename)
match self.eh_mode:
case Exceptions.NONE:
cflags += ['-fno-exceptions']
Expand Down Expand Up @@ -850,8 +856,8 @@ def __init__(self, **kwargs):
self.eh_mode = kwargs.pop('eh_mode')
super().__init__(**kwargs)

def get_cflags(self):
cflags = super().get_cflags()
def get_cflags(self, filename):
cflags = super().get_cflags(filename)
match self.eh_mode:
case Exceptions.EMSCRIPTEN:
cflags += ['-sSUPPORT_LONGJMP=emscripten',
Expand Down Expand Up @@ -919,8 +925,8 @@ def __init__(self, **kwargs):
self.is_asan = kwargs.pop('is_asan', False)
super().__init__(**kwargs)

def get_cflags(self):
cflags = super().get_cflags()
def get_cflags(self, filename):
cflags = super().get_cflags(filename)
if self.is_asan:
cflags += ['-fsanitize=address']
return cflags
Expand Down Expand Up @@ -1478,8 +1484,8 @@ def __init__(self, **kwargs):
self.is_stub = kwargs.pop('stub')
super().__init__(**kwargs)

def get_cflags(self):
cflags = super().get_cflags()
def get_cflags(self, filename):
cflags = super().get_cflags(filename)
if self.is_debug:
cflags += ['-D_DEBUG']
# library_wasm_worker.c contains an assert that a nonnull parameter
Expand Down Expand Up @@ -1635,8 +1641,8 @@ def __init__(self, **kwargs):
# the non-debug version of libc++abi.
self.is_debug |= settings.EXCEPTION_STACK_TRACES

def get_cflags(self):
cflags = super().get_cflags()
def get_cflags(self, filename):
cflags = super().get_cflags(filename)
if not self.is_mt and not self.is_ww:
cflags.append('-D_LIBCXXABI_HAS_NO_THREADS')
match self.eh_mode:
Expand Down Expand Up @@ -1747,16 +1753,22 @@ def __init__(self, **kwargs):
def can_use(self):
return super().can_use() and self.eh_mode in (Exceptions.WASM_LEGACY, Exceptions.WASM)

def get_cflags(self):
cflags = super().get_cflags()
def get_cflags(self, filename):
cflags = super().get_cflags(filename)
cflags.append('-DNDEBUG')
if not self.is_mt and not self.is_ww:
cflags.append('-D_LIBUNWIND_HAS_NO_THREADS')
if filename.endswith('.c'):
cflags.append('-std=c23')
else:
cflags.append('-std=c++23')

match self.eh_mode:
case Exceptions.NONE:
cflags.append('-D_LIBUNWIND_HAS_NO_EXCEPTIONS')
case Exceptions.EMSCRIPTEN:
cflags.append('-D__EMSCRIPTEN_EXCEPTIONS__')

return cflags


Expand Down Expand Up @@ -1789,8 +1801,8 @@ def get_files(self):
sbrk = utils.path_from_root('system/lib/libc/sbrk.c')
return [malloc, sbrk]

def get_cflags(self):
cflags = super().get_cflags()
def get_cflags(self, filename):
cflags = super().get_cflags(filename)
if self.memvalidate:
cflags += ['-DEMMALLOC_MEMVALIDATE']
if self.verbose:
Expand Down Expand Up @@ -1926,8 +1938,8 @@ def get_base_name(self):
name += '-getprocaddr'
return name

def get_cflags(self):
cflags = super().get_cflags()
def get_cflags(self, filename):
cflags = super().get_cflags(filename)
if self.is_legacy:
cflags += ['-DLEGACY_GL_EMULATION=1']
cflags += [f'-DMAX_WEBGL_VERSION={2 if self.is_webgl2 else 1}']
Expand Down Expand Up @@ -1963,8 +1975,8 @@ def __init__(self, **kwargs):
self.with_rtti = kwargs.pop('with_rtti', False)
super().__init__(**kwargs)

def get_cflags(self):
cflags = super().get_cflags()
def get_cflags(self, filename):
cflags = super().get_cflags(filename)
cflags.append('-std=c++20')
if not self.with_rtti:
cflags += ['-fno-rtti', '-DEMSCRIPTEN_HAS_UNBOUND_TYPE_NAMES=0']
Expand Down Expand Up @@ -2017,8 +2029,8 @@ def __init__(self, **kwargs):
self.ignore_case = kwargs.pop('ignore_case')
super().__init__(**kwargs)

def get_cflags(self):
cflags = super().get_cflags()
def get_cflags(self, filename):
cflags = super().get_cflags(filename)
if self.ignore_case:
cflags += ['-DWASMFS_CASE_INSENSITIVE']
return cflags
Expand Down Expand Up @@ -2204,8 +2216,8 @@ def get_base_name(self):
name += '-pure'
return name

def get_cflags(self):
cflags = super().get_cflags()
def get_cflags(self, filename):
cflags = super().get_cflags(filename)
cflags += ['-DNDEBUG', '-DEMSCRIPTEN_STANDALONE_WASM']
if self.is_mem_grow:
cflags += ['-DEMSCRIPTEN_MEMORY_GROWTH']
Expand Down