arobenko / embxx

embxx - Embedded C++ Library
GNU General Public License v3.0
262 stars 35 forks source link

Clang #7

Open ajneu opened 8 years ago

ajneu commented 8 years ago

Placeholder issue for future: Support Clang. ref

Regarding cmake: https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html#cross-compiling-using-clang https://github.com/labapart/polymcu/blob/master/CMakeLists.txt

ajneu commented 8 years ago

Some hints (work in progress... will be improved... as necessary... watch this space):

The are various combinations of compilers and libraries with which this should ultimately work: i) Compilers: gcc and clang ii) Std-libraries: libstdc++ (part of gcc-project) and libc++ (part of clang/llvm-project)

And it should work with the D) Desktop native platform target (native) as well as for E) Embedded targets (cross-compilers)

This gives us 4 combinations: 0) gcc + libstdc++ 1) clang + libc++ 2) gcc + libc++ 3) clang + libstdc++

(times two, for desktop (D) and embedded (E)).

0) and 1) are viable at the moment. 2) and 3) are somewhat harder at the momement.

...0) is already working. ...1) needs some light work to get it working with the desktop-compiler (D). Somewhat more to get it working with a cross-compiler (E). ...2) and 3) would be nice to tackle in the (hopefully not too distant) future.

For each compination of compiler (i) and std-lib (ii), there is also the question (permutation) of which compilation to use: Y) the linux-distibution's compilation versus Z) your own compilation

This is particularly prevalent for the compilation of Boost. For every combination 0), 1), 2), 3) it would be very nice to provide a recipe (script) to a create an own compilation of boost to use.

Compiling Boost

To compile boost yourself, for 0) gcc + libstdc++:

Compile boost into ~/boost_own/boost_*_gcc yourself, as follows:

# cd /tmp                                               ## consider uncommenting this line... ;)
BOOST_VERSION=1.60.0
BOOST_VERSION_=$(echo $BOOST_VERSION | sed "s/\./_/g")  ## with underscore, e.g. 1_60_0                                                                

BOOST_BUILD_DIR=$(pwd)/boost_${BOOST_VERSION_}_build_gcc
BOOST_STAGE_DIR=$(pwd)/boost_${BOOST_VERSION_}_stage_gcc
BOOST_FINAL_DIR=~/boost_own/boost_${BOOST_VERSION_}_gcc                                                                                      

wget http://sourceforge.net/projects/boost/files/boost/${BOOST_VERSION}/boost_${BOOST_VERSION_}.tar.bz2                                              
tar xf boost_${BOOST_VERSION_}.tar.*
cd     boost_${BOOST_VERSION_}

BOOST_DISTRO_SOURCE=$(apt-cache show $(apt-cache search "libboost*.*-all-dev" | grep "libboost[0-9]*.[0-9]*-all-dev" | sort -Vr | head -1 | sed -n "s\
/\([^ ]*\).*/\1/p") | sed -n "s/^Source: \([^ ]*\).*/\1/p")

## get the build dependencies for your distro's boost (it is highly likely that the newest boost needs the same dependencies)                          
sudo apt-get build-dep -y ${BOOST_DISTRO_SOURCE}
##sudo apt-get install python-dev libicu-dev zlib1g-dev libbz2-dev liblzma-dev                                                                         

./bootstrap.sh --with-toolset=gcc
./b2 -j4 toolset=gcc --build-dir="${BOOST_BUILD_DIR}" --stagedir="${BOOST_STAGE_DIR}" stage release
./b2 -j4 toolset=gcc --build-dir="${BOOST_BUILD_DIR}" --stagedir="${BOOST_STAGE_DIR}" --prefix="${BOOST_FINAL_DIR}" install

## See the created directories:
ls -ld $(readlink -f $BOOST_BUILD_DIR)
ls -ld $(readlink -f $BOOST_STAGE_DIR)
ls -ld $(readlink -f $BOOST_FINAL_DIR)

To compile boost yourself, for 1) clang + libc++:

First get the necessary packages:

sudo apt-get install clang libc++-dev libc++abi-dev

Make sure that clang and clang++ are in the PATH; and reference the latest clang. Determine the latest version of your Debian-based (also Ubuntu, etc.) distro's clang as follows:

apt-cache search "clang-*.*v" | grep "^clang-[0-9]*\.[0-9]* " | sort -Vr | head -1 | sed -n "s/\([^ ]*\).*/\1/p"

Determine if clang and clang++ is in you PATH as follows:

clang   --version
clang++ --version

If clang is not in your PATH, or reference older versions (instead of the latest) then do:

CLANG_PKG_LATEST=$(apt-cache search "clang-*.*v" | grep "^clang-[0-9]*\.[0-9]* " | sort -Vr | head -1 | sed -n "s/\([^ ]*\).*/\1/p")
sudo apt-get install $CLANG_PKG_LATEST
CLANG=$CLANG_PKG_LATEST
CLANG_CXX=$(echo $CLANG | sed -n "s/\(clang\)\(.*\)/\1++\2/p")

mkdir -p ~/bin

ln -sfn $(readlink -e $(which $CLANG))     $HOME/bin/clang
ln -sfn $(readlink -e $(which $CLANG_CXX)) $HOME/bin/clang++

## $HOME/bin should be in your path. Loggin out and in should automatically do this: See ~/.profile  ... which is automatically sourced on login. Otherwise do:
if ! ( echo $PATH | grep -F "$HOME/bin:" &>/dev/null ); then
    export             PATH="$HOME/bin:$PATH"
fi

Compile boost into ~/boost_own/boost_*_clang_libcxx yourself, as follows:

# cd /tmp                                               ## consider uncommenting this line... ;)
BOOST_VERSION=1.60.0
BOOST_VERSION_=$(echo $BOOST_VERSION | sed "s/\./_/g")  ## with underscore, e.g. 1_60_0                                                                

BOOST_BUILD_DIR=$(pwd)/boost_${BOOST_VERSION_}_build_clang_libcxx
BOOST_STAGE_DIR=$(pwd)/boost_${BOOST_VERSION_}_stage_clang_libcxx
BOOST_FINAL_DIR=~/boost_own/boost_${BOOST_VERSION_}_clang_libcxx                                                                                      

wget http://sourceforge.net/projects/boost/files/boost/${BOOST_VERSION}/boost_${BOOST_VERSION_}.tar.bz2                                              
tar xf boost_${BOOST_VERSION_}.tar.*
cd     boost_${BOOST_VERSION_}

BOOST_DISTRO_SOURCE=$(apt-cache show $(apt-cache search "libboost*.*-all-dev" | grep "libboost[0-9]*.[0-9]*-all-dev" | sort -Vr | head -1 | sed -n "s\
/\([^ ]*\).*/\1/p") | sed -n "s/^Source: \([^ ]*\).*/\1/p")

## get the build dependencies for your distro's boost (it is highly likely that the newest boost needs the same dependencies)                          
sudo apt-get build-dep -y ${BOOST_DISTRO_SOURCE}
##sudo apt-get install python-dev libicu-dev zlib1g-dev libbz2-dev liblzma-dev                                                                         

./bootstrap.sh --with-toolset=clang
./b2 -j4 toolset=clang cxxflags="-std=c++1y -stdlib=libc++" linkflags="-stdlib=libc++" --build-dir="${BOOST_BUILD_DIR}" --stagedir="${BOOST_STAGE_DIR}" stage release
./b2 -j4 toolset=clang cxxflags="-std=c++1y -stdlib=libc++" linkflags="-stdlib=libc++" --build-dir="${BOOST_BUILD_DIR}" --stagedir="${BOOST_STAGE_DIR}" --prefix="${BOOST_FINAL_DIR}" install

## See the created directories:
ls -ld $(readlink -f $BOOST_BUILD_DIR)
ls -ld $(readlink -f $BOOST_STAGE_DIR)
ls -ld $(readlink -f $BOOST_FINAL_DIR)

Combiling embxx!!

For 0) gcc + libstdc++:

## in embxx directory
mkdir build
cd    build
cmake -DBOOST_ROOT=$(readlink -e ~/boost_own/boost_*_gcc | sort -Vr | head -1) ..
make -j4

For 1) clang + libc++:

## in embxx directory
export CC=clang
export CXX=clang++
mkdir build
cd    build
cmake -DCMAKE_CXX_FLAGS="-stdlib=libc++" -DCMAKE_EXE_LINKER_FLAGS="-stdlib=libc++ -lc++abi" -DBOOST_ROOT=$(readlink -e ~/boost_own/boost_*_clang_libcxx | sort -Vr | head -1) ..
make -j4

For 1) clang + lib++ some changes will be needed to the source... Some initial changes are here, but more is needed... (will update this, as time as found)