cross compiling

Discussion of chess software programming and technical issues.

Moderator: Ras

User avatar
Jim Ablett
Posts: 2391
Joined: Fri Jul 14, 2006 7:56 am
Location: London, England
Full name: Jim Ablett

Re: cross compiling

Post by Jim Ablett »

lucasart wrote:I've done a lot of googling around, and found a lot of forum bla bla, but no clear answers. So perhaps people here have more experience in the matter:
1/ How does one go about producing an ARM executable with linux+gcc ? (in the same fashion as mingw dows for windows)
2/ What about MacOSX ?
3/ What about 32-bit compiles ? Everytime I try the "-m32" compiler switch (g++ 4.7) I get insulted by idiotic error messages that seem to suggeest that some libraries are missing. Does anyone know how to fix this problem under Ubuntu based distros ?

I found this for compiling Osx on Linux. It's from 2009 so I don't know if it is still relevant.
Cross-Compiling on Linux for
Mac OS X 10.3 - 10.5 (PPC and Intel)
====================================
Last Updated: 2009-03-29

It often is wroten down as impossible, but it can be done (and our nightlies
are the living proof of that). You can make OSX binaries on a Linux based
system. But, the process is not easy. Even worse, in the years we are
supplying this, we had to change this documentation more often than any other
target. This is mostly because you can't use a mainstream GCC, but you need
the one from Apple. When you figured this out, and where to get the
binutils tools, it in fact is very straight forward.

Requirements:
- Mac OS X SDK Framework 10.4u.
- 7zip 4.61+ to extract DMG files.
- A fast computer, or a lots of time.
- A working 32bit linux compiler (on 64bit, use -m32 ;)).
- Bison installed (else it configures, but compile fails).

You get:
- A i686-apple-darwin9 compiler (gcc and g++)
For Mac OS X 10.4 - 10.5 Intel
- A powerpc-apple-darwin9 compiler (gcc and g++)
For Mac OS X 10.3 - 10.5 PPC
- A x86_64-apple-darwin9 compiler (gcc and g++)
For Mac OS X 10.5 Intel (64bit)

Notes:
- Even on 64bit systems, compile 32bit binaries. It avoids a lot of errors.

Warning:
- I can't guarantee you this will work, nor that it won't break your system.
- Please, don't complain to me or ask me about this documentation. It is
more meant as internal reference for when ever I want to reinstall the
system. If it doesn't work for you, too bad, nothing to see here, find
some other source.

Ps:
- Yes, this also works with Mac OS X 10.5 Intel.
- Yes, you can also make a x86_64 compiler.


The Start
---------

Pick which compiler you want to compile. Either one should do:
- i686-apple-darwin9
- powerpc-apple-darwin9
- x86_64-apple-darwin9

Now put that in a variable:

export TARGET=i686-apple-darwin9

We will use this a lot later on, to simplify this documentation, and show you
it works for any target you pick.


CCTools
-------

To start, you need tools like 'ld', 'ar', ... this you can find in the package
named 'odcctools'. I used various of sources over the years, the last one
which seems to produce valid linux binaries, is located here:

http://iphone-dev.googlecode.com/svn/

A bit good comes of the iPhone development after all ;) To install it, you
need some minor modifications. Also, I used the 'branches' version, which
has 9.2 ld version (well, I thought the name was just more cool!)

The current odcctools (r280) seems to contain a bug in the 'as'. It registers
'word' twice, which gives an error. Sadly enough, both definitions are
different. But, this patch fixes the problem by commenting one out, hoping
the value of the other (older) one is correct. The odcctools also contain a
few other bugs and problems. So load all the patches. After that, it seems to
compile just fine.

# svn checkout http://iphone-dev.googlecode.com/svn/br ... ls-9.2-ld/
# cd odcctools-9.2-ld

# wget http://devs.openttd.org/~truebrain/comp ... s_as.patch
# patch -p0 < odcctools_as.patch

# wget http://devs.openttd.org/~truebrain/comp ... sort.patch
# patch -p0 < odcctools_qsort.patch

# wget http://devs.openttd.org/~truebrain/comp ... lipo.patch
# patch -p0 < odcctools_lipo.patch

# wget http://devs.openttd.org/~truebrain/comp ... ld64.patch
# patch -p0 < odcctools_ld64.patch

# ./configure --prefix=/usr/$TARGET --target=$TARGET --with-sysroot=/usr/$TARGET --enable-ld64

In case you run x86_64-pc-linux-gnu, you might want to consider running this instead:

# LDFLAGS="-m32" CFLAGS="-m32" ./configure --prefix=/usr/$TARGET --target=$TARGET --with-sysroot=/usr/$TARGET --enable-ld64

Next:

# vi Makefile

On the line 'COMPONENTS = ', remove 'otool'. If you want this tool, make sure
you have a objc++ compiler .. I don't, and I don't care about this tool.

# make
# make install

Now you should have a few useful tools in /usr/$TARGET/bin directory. If not,
take a step back, and try again.


Mac OS X SDK
------------

Nowedays 7zip support .dmg files too (4.61+ I believe; I used 4.65). So now you
can extract the files without access to a Mac. First, download xcode 3.1.2
from the Apple website (you need an account!). Feel free to use any other
version. The newer the better I guess.

For most targets, you want to extract the 10.4u SDK. This allows your binary
to run from 10.3 to 10.5. But if you want to go for the x86_64 compiler, you
need to go for the 10.5 SDK. As 10.4 can't run 64bit code anyway, it shouldn't
be a real problem.

After downloading, you can extract it with (a long tree of compressed objects
in other compressed objects):
# mkdir xcode
# cd xcode
# 7z x ../xcode312_2621_developerdvd.dmg
# 7z x 5.hfs
# 7z x Xcode\ Tools/Packages/MacOSX10.4.Universal.pkg
# 7z x Payload
# cpio -i < Payload~
# cp -R SDKs/MacOSX10.4u.sdk/* /usr/$TARGET/
# ln -sf /usr/$TARGET/System/Library/Frameworks /usr/$TARGET/Library/Frameworks

This should give you enough files to continue the compile. You need to use
'cpio' on the last step, as 7z eats symlinks.

GCC
---

Now the most important part: GCC, your compiler.

Download the latest GCC From the Apple website. I used:

http://www.opensource.apple.com/darwins ... 490.tar.gz

Feel free to use any newer. Extract the file somewhere.

# cd /var/tmp
# tar zxvf gcc-5490.tar.gz

gcc works nicely out-of-the-box, but Apple introduced one stupid thing: they
use 'lipo' to check if the host is 32bit or 64bit. Of course it should be
checking against the target. So we need a patch to force either 32bit
(which also happens when you don't patch) or 64bit (which you need if you
want to use x86_64-apple-darwin9). So apply either of those patches:

# cd gcc-5490
# wget http://devs.openttd.org/~truebrain/comp ... 2bit.patch
# wget http://devs.openttd.org/~truebrain/comp ... 4bit.patch
# patch -p0 < gcc-??bit.patch

Also apply this patch, to avoid some weirdness with CFLAGS:

# wget http://devs.openttd.org/~truebrain/comp ... lags.patch
# patch -p0 < gcc-cflags.patch

Apply this patch if you don't want /usr/$TARGET/$TARGET dir (it annoys me ..
it is nothing really ground-breaking or code-changing):

# wget http://devs.openttd.org/~truebrain/comp ... ldir.patch
# patch -p0 < gcc-tooldir.patch
# cd ..

Now you can compile, like:

# mkdir gcc-build
# cd gcc-build
# export PATH=$PATH:/usr/$TARGET/bin
# ../gcc-5490/configure --prefix=/usr/$TARGET --disable-checking --enable-languages=c,objc,c++,obj-c++ --with-as=/usr/$TARGET/bin/$TARGET-as --with-ld=/usr/$TARGET/bin/$TARGET-ld --target=$TARGET --with-sysroot=/usr/$TARGET --enable-static --enable-shared --disable-nls --disable-multilib

If you are on a 64bit system, you can run the following (avoids -lm errors):

# CFLAGS="-m32" LDFLAGS="-m32" ../gcc-5490/configure --prefix=/usr/$TARGET --disable-checking --enable-languages=c,objc,c++,obj-c++ --with-as=/usr/$TARGET/bin/${TARGET}-as --with-ld=/usr/$TARGET/bin/${TARGET}-ld --target=$TARGET --with-sysroot=/usr/$TARGET --enable-static --enable-shared --disable-nls --disable-multilib

If you are compiling x86_64-apple-darwin9, make sure to replace 'ld' with
'ld64' in the above configure. It isn't doing this on its own. Also, it looks
like if you want to use 10.5 SDK for any other target, you need to use 'ld64'
too. It seems 'ld' is slightly too old to understand a certain section in a
library of the 10.5 SDK.

Now just compile:

# make
# make install

On a 32bit system, you most likely run into a problem with libstdc++v3. It
tries to compile something while it is not allowed, or so it tells. I have
yet no idea why this happens, I just know on a 64bit system this problem
does not exist. If you solve it, please email me, and I will add your
addition here.

Now you should have a working gcc and g++ binary in your
/usr/$TARGET/bin directory. As I said, it is very simple if you
know which tools to use and what to use for configure flags.


Other libs
----------

If you need libraries like libpng or libz, please start up your Mac OS X, and
just copy them from there. Make sure that you use the right versions, and if
possible universal build versions (those with multiple versions in them). It
is almost impossible to compile such libraries yourself on your
cross-compiler, and I would advise against it. Copying from a real Mac OS X
is much easier, and shows much better results. But that is just my advise ;)


Conclusion
----------

There is not really much more to it. If this worked for you, and you want to
thank me, please donate some money to OpenTTD. It is always very welcome.
Jim.
User avatar
Jim Ablett
Posts: 2391
Joined: Fri Jul 14, 2006 7:56 am
Location: London, England
Full name: Jim Ablett

Re: cross compiling

Post by Jim Ablett »

It's very easy to compile for Android/Arm if you use the Codesourcery toolchain and cross-compile on Windows or Linux.

You'll need the arm-none-linux-gnueabi (lite) version.

https://sourcery.mentor.com/GNUToolchain/release2323

There are a few things you will need to know though (adjustments to the makefile).

Code: Select all

gcc     = arm-none-linux-gnueabi-gcc

Code: Select all

g++    = arm-none-linux-gnueabi-g++

Code: Select all

make  = cs-make
And for cpu architecture use:

Code: Select all

-mtune=arm7
use

Code: Select all

-static
(may also rarely need)

Code: Select all

-static-libgcc
If code uses pthreads you will need to statically link the whole library with this:

Code: Select all

-Wl,--whole-archive -lpthread -Wl,--no-whole-archive

and use Gcc built-in intrinsics for Lsb() / Msb()

Code: Select all

inline int lsb(Bitboard b)
{
	
return __builtin_ffsll(b) - 1;

}

inline int msb(Bitboard b)
{
	
int64_t ret = sizeof(uint64_t) * CHAR_BIT - 1;
	return b ? ret - __builtin_clzll(b) : ret;
}
Jim.
jdart
Posts: 4420
Joined: Fri Mar 10, 2006 5:23 am
Location: http://www.arasanchess.org

Re: cross compiling

Post by jdart »

Re #3 (errors with -m32):

sudo apt-get install ia32-libs

--Jon
Michel
Posts: 2292
Joined: Mon Sep 29, 2008 1:50 am

Re: cross compiling

Post by Michel »

Please, windows in windows.
Why???

Cross compiling for Windows on Linux is completely trivial.
User avatar
Don
Posts: 5106
Joined: Tue Apr 29, 2008 4:27 pm

Re: cross compiling

Post by Don »

michiguel wrote:
velmarin wrote:Dev_cpp portable tablets occupies only 30 megas.

I repeat, windows in Windows, the rest, beating around the bush.

99 percent of users use "Windows2 progamas chess.
There is absolutely no problem compiling windows program from linux with mingw64. In fact, the binaries are very fast. I use those that all the time.

Miguel
Absolutely. That is how I produce the Komodo binaries for official distribution. Don't even have to get my fingers dirty with Windows.
Capital punishment would be more effective as a preventive measure if it were administered prior to the crime.
User avatar
velmarin
Posts: 1600
Joined: Mon Feb 21, 2011 9:48 am

Re: cross compiling

Post by velmarin »

Don wrote:
michiguel wrote:
velmarin wrote:Dev_cpp portable tablets occupies only 30 megas.

I repeat, windows in Windows, the rest, beating around the bush.

99 percent of users use "Windows2 progamas chess.
There is absolutely no problem compiling windows program from linux with mingw64. In fact, the binaries are very fast. I use those that all the time.

Miguel
Absolutely. That is how I produce the Komodo binaries for official distribution. Don't even have to get my fingers dirty with Windows.


If you sell your products but users "windows".
But you can not care dirty, champion.
User avatar
Don
Posts: 5106
Joined: Tue Apr 29, 2008 4:27 pm

Re: cross compiling

Post by Don »

velmarin wrote:
Don wrote:
michiguel wrote:
velmarin wrote:Dev_cpp portable tablets occupies only 30 megas.

I repeat, windows in Windows, the rest, beating around the bush.

99 percent of users use "Windows2 progamas chess.
There is absolutely no problem compiling windows program from linux with mingw64. In fact, the binaries are very fast. I use those that all the time.

Miguel
Absolutely. That is how I produce the Komodo binaries for official distribution. Don't even have to get my fingers dirty with Windows.


If you sell your products but users "windows".
But you can not care crap, champion.
Of course I care because the majority of customers are windows users and it's in my best interest (and a matter or pride too) to have the best possible product.

But that doesn't mean I think Windows is great. It sucks and I absolutely hate it. Because of Komodo I have dual boot on most of my machines. A necessary evil for testing Komodo on Windows.
Capital punishment would be more effective as a preventive measure if it were administered prior to the crime.
User avatar
velmarin
Posts: 1600
Joined: Mon Feb 21, 2011 9:48 am

Re: cross compiling

Post by velmarin »

To prove

and sell it, do not forget.

Do not despise your customers, but you do not use "queer".
Windows is the standard, the rest, Imitations.
Komodo would win much to do in "windows", sure.
User avatar
Don
Posts: 5106
Joined: Tue Apr 29, 2008 4:27 pm

Re: cross compiling

Post by Don »

velmarin wrote:To prove

and sell it, do not forget.

Do not despise your customers, but you do not use "queer".
Windows is the standard, the rest, Imitations.
Komodo would win much to do in "windows", sure.
I do not despise my customers - I value all of them. I don't have to worship the Microsoft gods in order to appreciate my customers.

You should study your history more. Windows is the imitator and Unix was there long before Windows. The Internet came very late for Windows due to Bill Gates lack of insight (a mistake he repeated many times.) And the graphical user interface came along on both Unix and Apple long before Microsoft picked it up. And Microsoft was not wise to multi-tasking until almost everyone else was already doing it. In fact other companies made superior DOS clones in order to provide real multi-tasking which DOS did not have. The first Windows did not even have proper multitasking, it was a shell over DOS that had cooperative multi-tasking only.

You have really bought into the Microsoft propaganda in a big way to believe that everyone copied windows. Study your history.
Capital punishment would be more effective as a preventive measure if it were administered prior to the crime.
User avatar
velmarin
Posts: 1600
Joined: Mon Feb 21, 2011 9:48 am

Re: cross compiling

Post by velmarin »

My first personal computer was a 8086, after the commodore, I have my years, my friend.

The principles were the principles, do not refuse.
we are in the XXI century, and I am a happy grandfather, but Windows is the most, are past.
Each distro of Linux is an imitation of Windows.
Actually glad to see Komodo play Fritz in Windows.
Using Visual Studio, has nowhere to compare "is a pleasure, if a little gay, but a pleasure," Intel Compiler, hehe.

Linux is a dinosaur.