📜 ⬆️ ⬇️

Cross-compiling on OS X under Linux using crosstool-ng



In this article we will talk about a great crosstool-ng cross-toolbox build automation tool, an almost indispensable tool for any embedded developer who respects himself. If you had to seriously collect software from x86-linux under arm-linux , then you probably heard about it.

This tutorial does not deal with the cross-compilation of the architecture, but rather the cross-compilation of the system — the Linux build in Darwin .

Disclaimer

On the Internet there are several articles on building crosstool-ng under OS X, for example on benmont.com and in the official manual . However, in some articles there are many errors and outdated information, while in others only common features are described. Here will be described my path, by which I successfully assembled the toolchain in July 2013.
')

Training

This part depends on which package manager you are using in OS X — MacPorts or Homebrew . I have chosen ports-way for myself for a long time, so I will write based on this.

1. Register-dependent file system
Everything is simple, in OS X there is Disk Utility utility , we will use it to create a new partition. 5+ GB required.



2. Tools
It is assumed that you have installed MacPorts. Install the following packages:

ncurses lzma libtool binutils gsed gawk grep gcc48 

Check which versions of gcc are in the system: sudo port select --list gcc . We need mp-gcc48 - choose the default command sudo port select --set gcc mp-gcc48 .

3. Installing crosstool-ng
It is quite simple to assemble the toolkit itself; we will use the official instructions:

 hg clone http://crosstool-ng.org/hg/crosstool-ng cd crosstool-ng ./bootstrap 

Forced to abort the official instructions: the kconfig / zconf.hash.c file lacks a line
 #include <stddef.h> 

 ./configure \ --with-objcopy=/opt/local/bin/gobjcopy \ --with-objdump=/opt/local/bin/gobjdump \ --with-readelf=/opt/local/bin/greadelf \ --with-sed=/opt/local/bin/gsed \ --with-libtool=/opt/local/bin/glibtool \ --with-grep=/opt/local/bin/grep \ --with-install=/opt/local/bin/ginstall make sudo make install 

This will install ct-ng in /usr/local/bin . Home directory ct-ng: /usr/local/lib/ct-ng.hg+default-2685dfa9de14 depending on the revision. In this directory, edit the scripts / functions file, replacing the line for Darwin with a line from Linux:
 --- scripts/functions.orig 2013-07-09 22:15:12.000000000 +0400 +++ scripts/functions 2013-07-06 04:28:41.000000000 +0400 @@ -460,7 +460,7 @@ mode="$(stat -c '%a' "$(dirname "${dir}")")" ;; Darwin|*BSD) - mode="$(stat -f '%Lp' "$(dirname "${dir}")")" + mode="$(stat -c '%a' "$(dirname "${dir}")")" ;; *) CT_Abort "Unhandled host OS $CT_SYS_OS" 

This is because ct-ng accidentally chooses the version of gstat from the GNU set, instead of the original stat from OS X. Admire the beauty and elegance of the local code and close the file.

You can also copy the ct-ng.comp script for bash-completion , it works well.
Now you need to select a temporary folder in which our wonderful cross-toolchain and its sysroot will be forged. I have this /Volumes/Unixen/ct-config , go to your folder and start the configuration.

Crosstool-ng setup

Before starting the configuration, we will inherit the configuration from the template. We are interested in x86_64-unknown-linux-gnu :

 cd /Volumes/Unixen/ct-config ct-ng x86_64-unknown-linux-gnu ct-ng menuconfig 

After that you see the menu in which we will customize our toolkit.

1. Paths and misc options
Here it is important to specify the Local tarballs directory ( /Volumes/Unixen/src ) and Prefix directory ( /Volumes/Unixen/${CT_TARGET} ) /Volumes/Unixen/${CT_TARGET} , do not forget to create /Volumes/Unixen/src .

2. C compiler
Here I have disabled Java and Fortran support, because I don’t know how GCC will behave when building with enabled features. Be sure to disable [] Link libstdc ++ statically into the gcc binary , otherwise there will be an error
 collect2: error: ld returned 1 exit status ld: library not found for -lcrt0.o 

3. Debug facilities
Here you have to disable support for dmalloc and ltrace , as otherwise there will be unsolvable problems. In the gdb section, you should disable [] Native gdb and, if not necessary, disable [*] Enable python scripting (problem with python, but the solution will be lower). I am using the gdb version (7.3.1) version .

4. Companion libraries
Here strictly following versions of libraries, otherwise there will be compilation errors and autotools, these versions were selected by typing, often the choice was more recent.
  * GMP version (5.0.2) * MPFR version (3.1.2) * PPL version (0.11.2) * CLooG version (0.15.11) * MPC version (1.0.1) 

Assembly toolchain

Almost everything is ready. During the build process, the following error may occur (the kernel version is mine):
 [ERROR] /Volumes/Unixen/ct-config/.build/src/linux-3.8.11/arch/x86/tools/relocs.c:8:17: fatal error: elf.h: No such file or directory 

Therefore, we will take care of this in advance by taking elf.h from a trusted source. If not at hand, take my elf.h. You need to put in /usr/include .

Still need to correct the limit of open files ( RE: Libc iconvdata compilation problem ):
 ulimit -n 1024 

If you get an error in gdb during the build, if you have not disabled [*] Enable python scripting before:
 configure: error: python is missing or unusable 

then edit the .build / gdb - * / gdb / python / python-config.py file by commenting out the lines
 # if getvar('LINKFORSHARED') is not None: # libs.extend(getvar('LINKFORSHARED').split()) 

It seems everything is ready.
 ct-ng build 

On the Macbook Air with i5, the build takes about 69 minutes , and you will most likely have some errors in the middle. So go far from the computer will not work.

Conclusion




/ Volumes / Unixen $ du -csh ct-config /
 4,0G ct-config/ 4,0G  

/ Volumes / Unixen $ x86_64-unknown-linux-gnu / bin / x86_64-unknown-linux-gnu-gcc -v
 Using built-in specs. COLLECT_GCC=x86_64-unknown-linux-gnu/bin/x86_64-unknown-linux-gnu-gcc COLLECT_LTO_WRAPPER=/Volumes/Unixen/x86_64-unknown-linux-gnu/libexec/gcc/x86_64-unknown-linux-gnu/4.7.3/lto-wrapper Configured with: /Volumes/Unixen/ct-config/.build/src/gcc-4.7.3/configure --build=x86_64-build_apple-darwin12.4.0 --host=x86_64-build_apple-darwin12.4.0 --target=x86_64-unknown-linux-gnu --prefix=/Volumes/Unixen/x86_64-unknown-linux-gnu --with-sysroot=/Volumes/Unixen/x86_64-unknown-linux-gnu/x86_64-unknown-linux-gnu/sysroot --enable-languages=c,c++ ..... Target: x86_64-unknown-linux-gnu Thread model: posix gcc version 4.7.3 (crosstool-NG hg+default-2685dfa9de14) 

You can try to collect something:
 cd linux-source-3.9 export PATH=$PATH:/Volumes/Unixen/x86_64-unknown-linux-gnu/bin export C_INCLUDE_PATH=/usr/include:/opt/local/include:/Volumes/Unixen/x86_64-unknown-linux-gnu/x86_64-unknown-linux-gnu/sysroot/usr/include make ARCH=x86_64 CROSS_COMPILE=x86_64-unknown-linux-gnu- all -j4 

Successes,
Xlab was there.

Source: https://habr.com/ru/post/186124/


All Articles