thradams / cake

Cake a C23 front end and transpiler written in C
http://thradams.com/cake/index.html
GNU General Public License v3.0
529 stars 19 forks source link

Adding include directories does not work (on Linux) #192

Open madebr opened 1 month ago

madebr commented 1 month ago

While experimenting with cake, I tried to transpile a few sources from SDL2. But adding -I <include-folder> appears to not work.

$ git clone https://github.com/libsdl-org/SDL /tmp/SDL -b SDL2 --depth 1
$ cake /tmp/SDL/test/testsprite2.c -I /tmp/SDL/include -o /tmp/newsrc.c
Cake 0.9.10
/tmp/SDL/test/testsprite2.c
/tmp/SDL/test/testsprite2.c:22:22: error: file SDL_test.h not found
 22 |#include "SDL_test.h"
    |                     ~
/tmp/SDL/include
file not found '/tmp/SDL/include'
/tmp/newsrc.c
file not found '/tmp/newsrc.c'

 3 files in 0.04 seconds
 2 errors 0 warnings 0 notes 

Copying all SDL headers to the test folder allows cake to start (but fail later):

$ cp /tmp/SDL/include/*.h /tmp/SDL/test/
$ cake /tmp/SDL/test/testsprite2.c -I /tmp/SDL/include -o /tmp/newsrc.c
Cake 0.9.10
/tmp/SDL/test/testsprite2.c
/tmp/SDL/test/SDL_stdinc.h:414:1: error: _Static_assert failed "sizeof(Uint64) == 8"
...
thradams commented 1 month ago

I think the problem is the extra space after -I . For test purposes you can add at cakeconfig.h using pragma dir. Was your test on windows or linux? cake -autoconfig generate the cakeconfig.h with the include directories of gcc on linux.

thradams commented 1 month ago

I tested on Windows. No space after -I. cakeconfig already configured.

C:\Users\thiag\source\repos\SDL\test>cake -IC:\Users\thiag\source\repos\SDL\include testsprite.c
Cake 0.9.10
C:/Users/thiag/source/repos/SDL/test/testsprite.c

 1 files in 1.41 seconds
 0 errors 0 warnings 0 notes

It worked.

madebr commented 1 month ago

I'm running the tests on Linux. And indeed, removing the space (-I/tmp/SDL/include) does not require me to copy the SDL headers anymore.

After running cake -autoconfig, recompiling src/build.c and rebuilding cake.

I get the following behavior:

$ cake /tmp/SDL/test/testsprite2.c -I/tmp/SDL/include -o /tmp/newsrc.c
Cake 0.9.10
/tmp/SDL/test/testsprite2.c
/tmp/SDL/include/SDL_stdinc.h:414:1: error: _Static_assert failed "sizeof(Uint64) == 8"
 [E106]
 414 |SDL_COMPILE_TIME_ASSERT(uint64, sizeof(Uint64) == 8)_Static_assert(sizeof(Uint64)==8,"sizeof(Uint64) == 8");
     |                                                    ~~~~~~~~~~~~~~                                           
/tmp/SDL/include/SDL_stdinc.h:415:1: error: _Static_assert failed "sizeof(Sint64) == 8"
 [E106]
 415 |SDL_COMPILE_TIME_ASSERT(sint64, sizeof(Sint64) == 8)_Static_assert(sizeof(Sint64)==8,"sizeof(Sint64) == 8");
     |                                                    ~~~~~~~~~~~~~~                                           
/tmp/SDL/include/SDL_pixels.h:305:9: error: not found 'SDL_static_cast' [E68]
 305 |        SDL_DEFINE_PIXELFOURCC('Y', 'V', '1', '2')((((Uint32)(SDL_static_cast(Uint8,('Y'))))<<0)|(((Uint32)(SDL_static_cast(Uint8,('V'))))<<8)|(((Uint32)(SDL_static_cast(Uint8,('1'))))<<16)|(((Uint32)(SDL_static_cast(Uint8,('2'))))<<24)),
     |                                                              ~~~~~~~~~~~~~~~                                                                                                                                                                  
/tmp/SDL/include/SDL_pixels.h:305:9: error: expected TK_RIGHT_CURLY_BRACKET [E97]
 305 |        SDL_DEFINE_PIXELFOURCC('Y', 'V', '1', '2')((((Uint32)(SDL_static_cast(Uint8,('Y'))))<<0)|(((Uint32)(SDL_static_cast(Uint8,('V'))))<<8)|(((Uint32)(SDL_static_cast(Uint8,('1'))))<<16)|(((Uint32)(SDL_static_cast(Uint8,('2'))))<<24)),
     |                                                              ~~~~~~~~~~~~~~~                                                                                                                                                                  
/tmp/SDL/include/SDL_pixels.h:305:9: error: empty declarator name?? unexpected [E65]
 305 |        SDL_DEFINE_PIXELFOURCC('Y', 'V', '1', '2')((((Uint32)(SDL_static_cast(Uint8,('Y'))))<<0)|(((Uint32)(SDL_static_cast(Uint8,('V'))))<<8)|(((Uint32)(SDL_static_cast(Uint8,('1'))))<<16)|(((Uint32)(SDL_static_cast(Uint8,('2'))))<<24)),
     |                                                                                         ~                                                                                                                                                     
/tmp/SDL/include/SDL_pixels.h:305:9: error: expected TK_SEMICOLON [E97]
 305 |        SDL_DEFINE_PIXELFOURCC('Y', 'V', '1', '2')((((Uint32)(SDL_static_cast(Uint8,('Y'))))<<0)|(((Uint32)(SDL_static_cast(Uint8,('V'))))<<8)|(((Uint32)(SDL_static_cast(Uint8,('1'))))<<16)|(((Uint32)(SDL_static_cast(Uint8,('2'))))<<24)),
     |                                                                                         ~                                                                                                                                                     
/tmp/newsrc.c
file not found '/tmp/newsrc.c'

 2 files in 0.24 seconds
 7 errors 0 warnings 0 notes 
thradams commented 1 month ago

I need more time to check . The problems seams to be on

Uint64, Uint8 etc..definition.

It may be that the code that declares Uint64 is checking for some compiler specific stuff. Then we need to adapt or "clone" something from gcc.

thradams commented 1 month ago

(I never checked SDL code before, but it can be included for tests)

thradams commented 1 month ago

-E also may be useful because it can shows how Uint64 was defined

cl  -IC:\Users\thiag\source\repos\SDL\include testsprite.c -E
madebr commented 1 month ago

On gcc, SDL uses the libc types from stdint.h to define its Uint32, Sint32, ... types. Does cake autoconvert these? Reading cppreference, they are c99.

I attached the output of /tmp/cake/src/cake -E /tmp/SDL/test/testsprite2.c -I/tmp/SDL/include -o /tmp/newsrc.c > /tmp/testsprite2_preproc.c: testsprite2_preproc.txt

thradams commented 1 month ago

Found the problem

typedef long unsigned int uint64_t;
_Static_assert(sizeof(uint64_t) == 8, "");
int main(){}

The sizeof "long unsigned int" is different from gcc. In gcc it compiles and in cake it fail.s I will fix that. On windows it does not have this problem.

thradams commented 1 month ago

This problem has been fixed.

typedef long unsigned int uint64_t;
_Static_assert(sizeof(uint64_t) == 8, "");
int main(){}
madebr commented 1 month ago

Indeed! Very nice!

I still see the SDL_DEFINE_PIXELFOURCC error. Should I create a new issue for that?

madebr commented 1 month ago

(I never checked SDL code before, but it can be included for tests)

One of the reasons I'm looking into cake is for this. It would be useful to transpile SDL to c89 for deprecated platforms with no new toolchains.

thradams commented 1 month ago

It took me a while. (added cake to linux path was new to me etc) I am on linux WSL . It is compiling in my machine.

tra@Thiago:~/SDL/test$ cake testsprite.c -I../include
Cake 0.9.10
/home/tra/SDL/test/testsprite.c

 1 files in 0.29 seconds
 0 errors 0 warnings 0 notes
thradams commented 1 month ago

linuxsdl.txt

thradams commented 1 month ago

(I never checked SDL code before, but it can be included for tests)

One of the reasons I'm looking into cake is for this. It would be useful to transpile SDL to c89 for deprecated platforms with no new toolchains.

The main motivation for target < C99 was to be compatible with C++ 98. But if necessary I can add more C89 translations. (for loop is missing, designed initializers..)

more info http://thradams.com/cake/manual.html#toc_12

madebr commented 1 month ago

The main motivation for target < C99 was to be compatible with C++ 98. But if necessary I can add more C89 translations. (for loop is missing, designed initializers..)

I suppose c++98 would be fine as well, as long as the public interface is a C interface. The problem with c++ however is that is more pedantic: you can't assign a void pointer to a typed pointer without a warning/error.

linuxsdl.txt

Nice! I see an issue with FLT_MAX__FLT_MAX__ in the log. The -Wenum-conversion warnings might need to be fixed by a cast in a helper macro. The -Wdiv-by-zero warning is a thing the tests do to generate a nan float.

I don't know why, but I still see the SDL_DEFINE_PIXELFOURCC errors from https://github.com/thradams/cake/issues/192#issuecomment-2227547969 when caking testsprite2.c.

madebr commented 1 month ago

Building with -DTEST=ON makes the errors of https://github.com/thradams/cake/issues/192#issuecomment-2227547969 go away. See https://github.com/thradams/cake/issues/194

thradams commented 1 month ago

The

-DTEST

create a new main function that call test functions that are under #ifdef TEST groups.