The International Simutrans Forum

 

Author Topic: Compiling Simutrans with clang  (Read 4050 times)

0 Members and 1 Guest are viewing this topic.

Offline Vonjo

  • *
  • Posts: 273
Compiling Simutrans with clang
« on: September 11, 2012, 06:55:08 PM »
When I tried to compile simutrans with clang to create LLVM build, I got this error:
Code: [Select]
===> 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:
Code: [Select]
#       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.

Offline VS

  • Senior Plumber (Devotee)
  • Devotees (Inactive)
  • *
  • Posts: 4856
  • Vladimír Slávik
    • VS's Simutrans site
  • Languages: CS,EN
Re: Compiling Simutrans with clang
« Reply #1 on: September 11, 2012, 07:16:09 PM »
Not sure if this is as relevant as I would think, but... Can you estimate how many times this happens?

Offline Vonjo

  • *
  • Posts: 273
Re: Compiling Simutrans with clang
« Reply #2 on: September 11, 2012, 07:52:40 PM »
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.

Offline Ters

  • Coder/patcher
  • Devotee
  • *
  • Posts: 5675
  • Languages: EN, NO
Re: Compiling Simutrans with clang
« Reply #3 on: September 11, 2012, 07:57:25 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.

Offline Dwachs

  • DevTeam, Coder/patcher
  • Administrator
  • *
  • Posts: 4777
  • Languages: EN, DE, AT
Re: Compiling Simutrans with clang
« Reply #4 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.

Offline Vonjo

  • *
  • Posts: 273
Re: Compiling Simutrans with clang
« Reply #5 on: September 12, 2012, 05:57:40 AM »
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:
Code: [Select]
# 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. :)

Offline Dwachs

  • DevTeam, Coder/patcher
  • Administrator
  • *
  • Posts: 4777
  • Languages: EN, DE, AT
Re: Compiling Simutrans with clang
« Reply #6 on: September 12, 2012, 06:54:08 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 ;)

Offline Vonjo

  • *
  • Posts: 273
Re: Compiling Simutrans with clang
« Reply #7 on: September 12, 2012, 01:42:19 PM »
If you could provide a patch ... or at least report how you manage to compile with clang ;)
Like this? :)
Code: [Select]
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.
« Last Edit: September 12, 2012, 01:53:00 PM by Vonjo »