c3lang / c3c

Compiler for the C3 language
https://c3-lang.org
GNU Lesser General Public License v3.0
2.83k stars 172 forks source link

wasm and webGL #1504

Open panjea opened 3 weeks ago

panjea commented 3 weeks ago

I can locally run C3 with openGL and GLFW with no problems. I am stuck on how to compile for wasm32 target. I saw the SDL2 bindings: https://github.com/c3lang/vendor/blob/main/libraries/sdl2.c3l/sdl2.c3i I do not mind switching from GLFW to SDL. But I also have a simple C example that I compile with Emscripten and use -s GLFW=3 and that works, is there some way i can call wasm-ld directly so i can link with GLFW? Below is my build test script:

#!/usr/bin/python3
# install ubuntu and debian: sudo apt-get install libglfw3-dev
import os, sys, subprocess

if not os.path.isfile('c3-ubuntu-20.tar.gz'):
    cmd = 'wget -c https://github.com/c3lang/c3c/releases/download/latest/c3-ubuntu-20.tar.gz'
    print(cmd)
    subprocess.check_call(cmd.split())

if not os.path.isdir('c3'):
    cmd = 'tar -xvf c3-ubuntu-20.tar.gz'
    print(cmd)
    subprocess.check_call(cmd.split())

C3 = os.path.abspath('./c3/c3c')
assert os.path.isfile(C3)

#if not os.path.isdir('opengl-examples'):
#   cmd = 'git clone --depth 1 https://github.com/tonis2/opengl-examples.git'
#   print(cmd)
#   subprocess.check_call(cmd.split())
if not os.path.isdir('c3-opengl-examples'):
    cmd = 'git clone --depth 1 https://github.com/brentharts/c3-opengl-examples.git'
    print(cmd)
    subprocess.check_call(cmd.split())

## https://github.com/tonis2/opengl-c3.git
if not os.path.isdir('opengl-c3'):
    cmd = 'git clone --depth 1 https://github.com/tonis2/opengl-c3.git'
    print(cmd)
    subprocess.check_call(cmd.split())

def test_triangle():
    cmd = [C3, 'run', 'triangle']
    print(cmd)
    subprocess.check_call(cmd, cwd='c3-opengl-examples')

def test_c3(output='test-c3-glfw.bin', opt='--opt' in sys.argv, wasm='--wasm' in sys.argv):
    if opt:
        output = output.replace('.bin', '.opt.bin')
    if wasm:
        output = output.replace('.bin', '.wasm.bin')

    cmd = [C3]
    if wasm:
        cmd += [
            '--target', 'wasm32',
            '--linker=custom', './emsdk/upstream/bin/wasm-ld'
        ]
    else:
        cmd += ['--target', 'linux-x64']
    mode = 'compile'

    cmd += [
        '--output-dir', '/tmp',
        '--obj-out', '/tmp',
        '--build-dir', '/tmp',
        '--print-output',
        '-o', output,
    ]
    if wasm:
        cmd += [#'--link-libc=no', '--use-stdlib=no', 
            '--no-entry', '--reloc=none']
        pass
    else:
        cmd += ['-l', 'glfw']

    if opt:
        cmd.append('-Oz')

    cmd += [
        mode, 
        './c3-opengl-examples/examples/tri.c3',
        './opengl-c3/build/gl.c3',
        './c3-opengl-examples/dependencies/glfw.c3',
        './c3-opengl-examples/dependencies/helpers.c3',

    ]
    print(cmd)
    res = subprocess.check_output(cmd).decode('utf-8')
    ofiles = []
    for ln in res.splitlines():
        if ln.endswith('.o'):
            ofiles.append(ln.strip())
    print(ofiles)
    os.system('ls -lh /tmp/*.bin')
    os.system('ls -lh /tmp/*.o')

    if '--run' in sys.argv:
        subprocess.check_call(['/tmp/'+output])
    if wasm:
        cmd = ['./emsdk/upstream/bin/llvm-objdump', '--syms', '/tmp/'+output+'.wasm']
        print(cmd)
        subprocess.check_call(cmd)

if __name__=='__main__':
    if '--simple' in sys.argv:
        test_triangle()
    else:
        test_c3()
lerno commented 3 weeks ago

Well, first of all you should be able to pass in arguments to wasm-ld using "-z ". Did you try this already?