cake icon indicating copy to clipboard operation
cake copied to clipboard

Preprocessing SDL sources with cake

Open madebr opened this issue 1 month ago • 78 comments

Hello!

Over at SDL, we're currently debating on how to support older platforms whose compilers do not support c99 and newer. Think about WatCom, Windows 95 , OS/2, ...

SDL's source code is currently c99, with a public c89 api. The main issue with c89 is our use of declarations after statements (-Wdeclaration-after-statement gcc warning).

I was thinking about using cake as a source preprocessor to transform SDL sources on the fly into c89 code. SDL uses CMake as build system, so I think -DCMAKE_C_COMPILER_LAUNCHER= might do the trick.

Using a wrapper, configuring SDL succeeds but building fails due to failing static_assert's. Is there something we can do about this?

`cake-wrapper.py`
#!/usr/bin/env python

# Cake wrapper for compiler

import os
from pathlib import Path
import shlex
import subprocess
import sys
import tempfile

DEBUG_WRAPPER = os.getenv("DEBUG_CAKE_WRAPPER") is not None
CAKE = os.getenv("CAKE") or "cake"

def main() -> int:
    compiler_cmd = []
    cake_args = []
    for arg in sys.argv[1:]:
        if arg.startswith("-I"):
            cake_args.append(arg)
        elif arg.startswith("-D"):
            cake_args.append(arg)
        elif arg.startswith("-U"):
            cake_args.append(arg)
    for arg in sys.argv[1:]:
        if arg.endswith(".c"):
            c = Path(arg)
            cake_c = Path(tempfile.gettempdir()) / c.with_suffix(".cake.c").name
            cake_cmd = [CAKE] + cake_args + [f"-I{c.parent}", str(c), "-o", str(cake_c)]
            compiler_cmd.append(f"-I{c.parent}")
            if DEBUG_WRAPPER:
                print(f"Running cake: '{shlex.join(cake_cmd)}'", file=sys.stderr)
                sys.stderr.flush()
            result = subprocess.call(cake_cmd)
            if result != 0:
                print(f"Cake failed: {shlex.join(cake_cmd)}", file=sys.stderr)
                sys.stderr.flush()
                return result
            arg = str(cake_c)
        compiler_cmd.append(arg)

    if DEBUG_WRAPPER:
        print(f"Running compiler: '{shlex.join(compiler_cmd)}'", file=sys.stderr)
        sys.stderr.flush()
    return subprocess.call(compiler_cmd)


if __name__ == "__main__":
    raise SystemExit(main())

Reproducer:

git clone https://github.com/libsdl-org/SDL /tmp/SDL --depth 1
cmake -S /tmp/SDL -B /tmp/SDL-build -DCMAKE_COMPILER=/usr/bin/gcc "-DCMAKE_C_COMPILERR_LAUNCHER=python;/path/to/cake-wrapper.py"
cmake --build /tmp/SDL-build

Is there something we can do about the errors?

cake errors of a single SDL soruce
Cake 0.12.35 (GCC)
/home/maarten/projects/SDL/src/libm/e_atan2.c
/home/maarten/projects/SDL/include/SDL3/SDL_stdinc.h:1175:1: error C1060: static_assert failed "sizeof(bool) == 1"

 1175 |SDL_COMPILE_TIME_ASSERT(bool_size, sizeof(bool) == 1)static_assert(sizeof(_Bool)==1,"sizeof(bool) == 1");
      |                                                     ~~~~~~~~~~~~~                                        
/home/maarten/projects/SDL/include/SDL3/SDL_stdinc.h:1176:1: error C1060: static_assert failed "sizeof(Uint8) == 1"

 1176 |SDL_COMPILE_TIME_ASSERT(uint8_size, sizeof(Uint8) == 1)static_assert(sizeof(Uint8)==1,"sizeof(Uint8) == 1");
      |                                                       ~~~~~~~~~~~~~                                         
/home/maarten/projects/SDL/include/SDL3/SDL_stdinc.h:1177:1: error C1060: static_assert failed "sizeof(Sint8) == 1"

 1177 |SDL_COMPILE_TIME_ASSERT(sint8_size, sizeof(Sint8) == 1)static_assert(sizeof(Sint8)==1,"sizeof(Sint8) == 1");
      |                                                       ~~~~~~~~~~~~~                                         
/home/maarten/projects/SDL/include/SDL3/SDL_stdinc.h:1178:1: error C1060: static_assert failed "sizeof(Uint16) == 2"

 1178 |SDL_COMPILE_TIME_ASSERT(uint16_size, sizeof(Uint16) == 2)static_assert(sizeof(Uint16)==2,"sizeof(Uint16) == 2");
      |                                                         ~~~~~~~~~~~~~                                           
/home/maarten/projects/SDL/include/SDL3/SDL_stdinc.h:1179:1: error C1060: static_assert failed "sizeof(Sint16) == 2"

 1179 |SDL_COMPILE_TIME_ASSERT(sint16_size, sizeof(Sint16) == 2)static_assert(sizeof(Sint16)==2,"sizeof(Sint16) == 2");
      |                                                         ~~~~~~~~~~~~~                                           
/home/maarten/projects/SDL/include/SDL3/SDL_stdinc.h:1180:1: error C1060: static_assert failed "sizeof(Uint32) == 4"

 1180 |SDL_COMPILE_TIME_ASSERT(uint32_size, sizeof(Uint32) == 4)static_assert(sizeof(Uint32)==4,"sizeof(Uint32) == 4");
      |                                                         ~~~~~~~~~~~~~                                           
/home/maarten/projects/SDL/include/SDL3/SDL_stdinc.h:1181:1: error C1060: static_assert failed "sizeof(Sint32) == 4"

 1181 |SDL_COMPILE_TIME_ASSERT(sint32_size, sizeof(Sint32) == 4)static_assert(sizeof(Sint32)==4,"sizeof(Sint32) == 4");
      |                                                         ~~~~~~~~~~~~~                                           
/home/maarten/projects/SDL/include/SDL3/SDL_stdinc.h:1182:1: error C1060: static_assert failed "sizeof(Uint64) == 8"

 1182 |SDL_COMPILE_TIME_ASSERT(uint64_size, sizeof(Uint64) == 8)static_assert(sizeof(Uint64)==8,"sizeof(Uint64) == 8");
      |                                                         ~~~~~~~~~~~~~                                           
/home/maarten/projects/SDL/include/SDL3/SDL_stdinc.h:1183:1: error C1060: static_assert failed "sizeof(Sint64) == 8"

 1183 |SDL_COMPILE_TIME_ASSERT(sint64_size, sizeof(Sint64) == 8)static_assert(sizeof(Sint64)==8,"sizeof(Sint64) == 8");
      |                                                         ~~~~~~~~~~~~~                                           
/home/maarten/projects/SDL/include/SDL3/SDL_stdinc.h:1185:1: error C1060: static_assert failed "sizeof(Uint64) <= sizeof(unsigned long long)"

 1185 |SDL_COMPILE_TIME_ASSERT(uint64_longlong, sizeof(Uint64) <= sizeof(unsigned long long))static_assert(sizeof(Uint64)<=sizeof(unsignedlonglong),"sizeof(Uint64) <= sizeof(unsigned long long)");
      |                                                                                      ~~~~~~~~~~~~~                                                                                           
/home/maarten/projects/SDL/include/SDL3/SDL_stdinc.h:1186:1: error C1060: static_assert failed "sizeof(size_t) <= sizeof(unsigned long long)"

 1186 |SDL_COMPILE_TIME_ASSERT(size_t_longlong, sizeof(size_t) <= sizeof(unsigned long long))static_assert(sizeof(size_t)<=sizeof(unsignedlonglong),"sizeof(size_t) <= sizeof(unsigned long long)");
      |                                                                                      ~~~~~~~~~~~~~                                                                                           
/home/maarten/projects/SDL/include/SDL3/SDL_stdinc.h:1193:1: error C1060: static_assert failed "sizeof(SDL_alignment_test) == (2 * sizeof(void *))"

 1193 |SDL_COMPILE_TIME_ASSERT(struct_alignment, sizeof(SDL_alignment_test) == (2 * sizeof(void *)))static_assert(sizeof(SDL_alignment_test)==(2*sizeof(void*)),"sizeof(SDL_alignment_test) == (2 * sizeof(void *))");
      |                                                                                             ~~~~~~~~~~~~~                                                                                                      
/home/maarten/projects/SDL/include/SDL3/SDL_stdinc.h:1213:1: error C1060: static_assert failed "sizeof(SDL_DUMMY_ENUM) == sizeof(int)"

 1213 |SDL_COMPILE_TIME_ASSERT(enum, sizeof(SDL_DUMMY_ENUM) == sizeof(int))static_assert(sizeof(SDL_DUMMY_ENUM)==sizeof(int),"sizeof(SDL_DUMMY_ENUM) == sizeof(int)");
      |                                                                    ~~~~~~~~~~~~~                                                                               
/usr/include/signal.h:369:57: error C0970: expected token '{', got ';' 
 369 |    ("Use the signal and sigprocmask functions instead");
     |                                                        ~ 

14 errors 0 warnings 0 notes 
1 files in 0.65 seconds

madebr avatar Nov 03 '25 03:11 madebr