News:

Use the "Forum Search"
It may help you to find anything in the forum ;).

Compiling Simutrans with clang

Started by Vonjo, September 11, 2012, 06:55:08 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Vonjo

When I tried to compile simutrans with clang to create LLVM build, I got this error:

===> CXX freight_list_sorter.cc
freight_list_sorter.cc:114:2: error: variable length array of non-POD element type 'ware_t'
        ALLOCA(ware_t, wlist, warray.get_count());
        ^
./simtypes.h:21:45: note: expanded from:
#       define ALLOCA(type, name, count) type name[count]
                                                  ^
1 error generated.
make: *** [build/clang/freight_list_sorter.o] Error 1

It seems the problem is related to this 'Variable-length arrays' problem:
http://clang.llvm.org/compatibility.html#vla

If I change count to any number, Simutrans can be compiled and run. For example in simtypes.h:
#       define ALLOCA(type, name, count) type name[65530]

Not sure what is the best way to solve this. I still don't have a lot of experience with c++ code.

VS

Not sure if this is as relevant as I would think, but... Can you estimate how many times this happens?

My projects... Tools for messing with Simutrans graphics. Graphic archive - templates and some other stuff for painters. Development logs for most recent information on what is going on. And of course pak128!

Vonjo

I have tested it since a few months ago, but I have just posted it today. The problem always and still happens until today. The compilation process stops there with that error.
The workaround is by changing the array length with fixed number. Some also say to change the array to something like vector, but I'm not sure.

Ters

Quote from: Vonjo on September 11, 2012, 07:52:40 PM
The workaround is by changing the array length with fixed number. Some also say to change the array to something like vector, but I'm not sure.

The latter is for certain far better than the former. A fixed number isn't really a solution at all, except where fixed numbers are passed to ALLOCA in the first place, so that doesn't say much.

Dwachs

You could replace line number 21 by line number 17 in simtypes.h, which replaces this incompatible macro.
Parsley, sage, rosemary, and maggikraut.

Vonjo

Quote from: Dwachs on September 11, 2012, 08:00:16 PM
You could replace line number 21 by line number 17 in simtypes.h, which replaces this incompatible macro.
Yes, It works. Thanks. :)
In simtypes.h, line 21:

# include <alloca.h>
# define ALLOCA(type, name, count) type* name = static_cast<type*>(alloca(sizeof(type) * (count)))


Maybe we can add configuration for clang too, to create clang LLVM build. :)

Dwachs

Quote from: Vonjo on September 12, 2012, 05:57:40 AM
Maybe we can add configuration for clang too, to create clang LLVM build. :)
If you could provide a patch ... or at least report how you manage to compile with clang ;)
Parsley, sage, rosemary, and maggikraut.

Vonjo

#7
Quote from: Dwachs on September 12, 2012, 06:54:08 AM
If you could provide a patch ... or at least report how you manage to compile with clang ;)
Like this? :)

diff -r simutrans-original/config.template simutrans-patched/config.template
21a22
> #OSTYPE = linux_clang
diff -r simutrans-original/Makefile simutrans-patched/Makefile
7c7
< OSTYPES       = amiga beos cygwin freebsd haiku linux mingw mac
---
> OSTYPES       = amiga beos cygwin freebsd haiku linux linux_clang mingw mac
49a50,54
> ifeq ($(OSTYPE),linux_clang)
>   CC = clang
>   CXX = clang
>   LIBS += -lz -lbz2 -lstdc++
> endif
diff -r simutrans-original/simtypes.h simutrans-patched/simtypes.h
19a20,22
> #elif defined __clang__
> #   include <alloca.h>
> #   define ALLOCA(type, name, count) type* name = static_cast<type*>(alloca(sizeof(type) * (count)))


Patch file on attachments.