Skip to content

Commit

Permalink
Try to build rviz2 for M1 Mac but could not (failed building ogre and…
Browse files Browse the repository at this point in the history
… this is a well-known issue c.f. osrf/homebrew-simulation#1628)
  • Loading branch information
light-tech committed Oct 1, 2022
1 parent 7387d9d commit 67f3de9
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:
- name: Download and extract prebuilt dependencies
id: download_dependencies
run: curl -OL https://github.com/light-tech/ROS2-On-iOS/releases/download/humble-macos/ros2deps_${{ matrix.platformArch }}.tar.xz && tar xzf ros2deps_${{ matrix.platformArch }}.tar.xz
if: ${{ false && steps.build_dependencies.outcome != 'success' && startsWith(matrix.platformArch, 'macOS') }}
if: ${{ true && steps.build_dependencies.outcome != 'success' && startsWith(matrix.platformArch, 'macOS') }}

# On macOS, only build ROS2 if either building or downloading dependencies succeeds
- name: Build ROS2
Expand Down
22 changes: 16 additions & 6 deletions build_deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function getSource() {
-o boost.tar.gz https://boostorg.jfrog.io/artifactory/main/release/1.80.0/source/boost_1_80_0.tar.gz

# Dependencies for cartographer
if [ $targetPlatform != "macOS" ]; then
if [ $targetPlatform != "macOS" ] && [ $targetPlatform != "macOS_M1" ]; then
curl -s -L -o abseil-cpp.tar.gz https://github.com/abseil/abseil-cpp/archive/refs/tags/20220623.0.tar.gz \
-o gflags.tar.gz https://github.com/gflags/gflags/archive/refs/tags/v2.2.2.tar.gz \
-o cairo.tar.xz https://www.cairographics.org/releases/cairo-1.16.0.tar.xz \
Expand Down Expand Up @@ -81,18 +81,28 @@ function setupPlatform() {
case $targetPlatform in
"iOS")
targetArch=arm64
boostArch=arm
targetSysroot=`xcodebuild -version -sdk iphoneos Path`
platformBasicConfigureArgs+=(--host=aarch64-apple-darwin)
platformBasicConfigureArgsPixmanCairo+=(--host=arm-apple-darwin) # For pixman and cairo we must use arm-apple thank to https://gist.github.com/jvcleave/9d78de9bb27434bde2b0c3a1af355d9c
platformExtraCMakeArgs+=(-DCMAKE_TOOLCHAIN_FILE=$REPO_ROOT/cmake/$targetPlatform.cmake);;
"iOS_Simulator")
target=x86_64
boostArch=ia64
targetSysroot=`xcodebuild -version -sdk iphonesimulator Path`
platformExtraCMakeArgs+=(-DCMAKE_TOOLCHAIN_FILE=$REPO_ROOT/cmake/$targetPlatform.cmake);;
"macOS")
targetArch=x86_64
boostArch=ia64
targetSysroot=`xcodebuild -version -sdk macosx Path`
platformExtraCMakeArgs+=(-DCMAKE_OSX_ARCHITECTURES=$targetArch);;
"macOS_M1")
targetArch=arm64
boostArch=arm
targetSysroot=`xcodebuild -version -sdk macosx Path`
platformBasicConfigureArgs+=(--host=aarch64-apple-darwin)
platformBasicConfigureArgsPixmanCairo+=(--host=arm-apple-darwin)
platformExtraCMakeArgs+=(-DCMAKE_OSX_ARCHITECTURES=$targetArch);;
esac
}

Expand Down Expand Up @@ -248,7 +258,7 @@ function buildBoost() {
echo "Build Boost"
cd $ros2DependenciesSourceExtractionPath/boost_1_80_0
./bootstrap.sh --prefix=$ros2SystemDependenciesPath --without-libraries=python
./b2 install
./b2 install architecture=$boostArch
}

function buildQt5Host() {
Expand All @@ -265,7 +275,7 @@ function buildQt5() {
# Patch the source https://codereview.qt-project.org/c/qt/qtbase/+/378706
sed -i.bak "s,QT_BEGIN_NAMESPACE,#include <CoreGraphics/CGColorSpace.h>\n#include <IOSurface/IOSurface.h>\nQT_BEGIN_NAMESPACE," src/plugins/platforms/cocoa/qiosurfacegraphicsbuffer.h

./configure -prefix $ros2SystemDependenciesPath -opensource -confirm-license -nomake examples -nomake tests -no-framework
./configure -prefix $ros2SystemDependenciesPath -opensource -confirm-license -nomake examples -nomake tests -no-framework -device-option QMAKE_APPLE_DEVICE_ARCHS=$targetArch
make && make install
}

Expand Down Expand Up @@ -363,16 +373,16 @@ setupPlatform
buildHostTools

case $targetPlatform in
"macOS") # Build dependencies for RVIZ and OpenCV
"macOS"|"macOS_M1") # Build dependencies for RVIZ and OpenCV
buildZlib
buildLibPng
buildFreeType2
buildEigen3
buildTinyXML2
buildBullet3
buildQt5
buildBoost
buildOpenCV;;
buildBoost;;
#buildOpenCV;;

*) # Build dependencies for ROS2 cartographer package
echo "We currently do not build any dependencies for iOS";;
Expand Down
78 changes: 48 additions & 30 deletions build_ros2.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ ros2PythonEnvPath=$REPO_ROOT/ros2PythonEnv
ros2InstallPath=$REPO_ROOT/ros2_$targetPlatform
ros2SystemDependenciesPath=$ros2InstallPath/deps

# --packages-up-to libcurl_vendor
# --executor sequential --event-handlers console_direct+
# --packages-up-to libcurl_vendor rviz_ogre_vendor
colconArgs=(--install-base $ros2InstallPath \
--merge-install --cmake-force-configure \
--executor sequential --event-handlers console_direct+ \
--cmake-args -DBUILD_TESTING=NO \
-DTHIRDPARTY=FORCE \
-DCOMPILE_TOOLS=NO \
-DFORCE_BUILD_VENDOR_PKG=ON \
-DBUILD_MEMORY_TOOLS=OFF \
-DRCL_LOGGING_IMPLEMENTATION=rcl_logging_noop)
colconVerbose=0
colconVerbose=1

prepareVirtualEnv() {
python3 -m venv $ros2PythonEnvPath
Expand All @@ -41,6 +41,27 @@ printPython() {
# find /Users/runner/hostedtoolcache/Python/3.10.7/x64/
}

patchRviz() {
# Pass the path to dependencies to rviz_ogre_vendor and orocos_kdl_vendor
sed -i.bak "s,CMAKE_ARGS,CMAKE_ARGS\n -DCMAKE_PREFIX_PATH=$ros2SystemDependenciesPath,g" \
src/ros2/rviz/rviz_ogre_vendor/CMakeLists.txt \
src/ros2/orocos_kdl_vendor/orocos_kdl_vendor/CMakeLists.txt

# Disable most 3rd party dependencies when building libcurl_vendor
sed -i.bak "s,--with-ssl,--without-ssl --without-librtmp --without-nghttp2 --without-ngtcp2 --without-nghttp3 --without-quiche --without-zlib --without-zstd --without-brotli --disable-ldap,g" src/ros/resource_retriever/libcurl_vendor/CMakeLists.txt

touch src/ros2/orocos_kdl_vendor/python_orocos_kdl_vendor/AMENT_IGNORE \
src/ros2/rviz/rviz_visual_testing_framework/AMENT_IGNORE
}

patchRvizM1() {
# rviz_ogre_vendor forces CMAKE_OSX_ARCHITECTURES=x86_64
sed -i.bak "s,x86_64,arm64,g" src/ros2/rviz/rviz_ogre_vendor/CMakeLists.txt

# sed -i.bak "s,CMAKE_ARGS,CMAKE_ARGS\n -DCMAKE_PREFIX_PATH=$ros2SystemDependenciesPath,g" \
src/ros2/tinyxml_vendor/CMakeLists.txt
}

buildRos2Base() {
echo "Build ros2 base ( assuming dependencies are available at $ros2SystemDependenciesPath )"

Expand All @@ -56,30 +77,21 @@ buildRos2Base() {

if [ $targetPlatform == "macOS" ] || [ $targetPlatform == "macOS_M1" ]; then

# For now disable extra packages for M1 Mac
if [ $targetPlatform == "macOS" ]; then
# For macOS desktop, we add the CLI tools (ros2 launch) and rclpy as well
vcs import src < $REPO_ROOT/ros2_cli.repos
# For macOS desktop (Intel), we add the CLI tools (ros2 launch) and rclpy as well
# We probably won't be able to build these on GitHub Action as there is no way to link with the ARM version of libpython*.dylib there
vcs import src < $REPO_ROOT/ros2_cli.repos
fi

# And also build RVIZ2
vcs import src < $REPO_ROOT/rviz2.repos

# Pass the path to dependencies to rviz_ogre_vendor and orocos_kdl_vendor
sed -i.bak "s,CMAKE_ARGS,CMAKE_ARGS\n -DCMAKE_PREFIX_PATH=$ros2SystemDependenciesPath,g" \
src/ros2/rviz/rviz_ogre_vendor/CMakeLists.txt \
src/ros2/orocos_kdl_vendor/orocos_kdl_vendor/CMakeLists.txt

# Disable most 3rd party dependencies when building libcurl_vendor
sed -i.bak "s,--with-ssl,--without-ssl --without-librtmp --without-nghttp2 --without-ngtcp2 --without-nghttp3 --without-quiche --without-zlib --without-zstd --without-brotli --disable-ldap,g" src/ros/resource_retriever/libcurl_vendor/CMakeLists.txt

touch src/ros2/orocos_kdl_vendor/python_orocos_kdl_vendor/AMENT_IGNORE \
src/ros2/rviz/rviz_visual_testing_framework/AMENT_IGNORE
fi
# vcs import src < $REPO_ROOT/rviz2.repos
# patchRviz

colconArgs+=(-DCMAKE_PREFIX_PATH=$ros2SystemDependenciesPath)

if [ $targetPlatform == "macOS_M1" ]; then
colconArgs+=(-DCMAKE_TOOLCHAIN_FILE=$REPO_ROOT/cmake/$targetPlatform.cmake) #(-DCMAKE_OSX_ARCHITECTURES=arm64)
patchRvizM1
colconArgs+=(-DCMAKE_TOOLCHAIN_FILE=$REPO_ROOT/cmake/$targetPlatform.cmake)
fi

else
Expand All @@ -97,17 +109,15 @@ setupROS2base() {
echo "Prepare ROS2 base and dependencies"

# Extract previously built dependencies and ROS2 base to save time while we try to build rviz2
curl -L -o ros2deps_macOS.tar.xz https://github.com/light-tech/ROS2-On-iOS/releases/download/humble-macos/ros2deps_macOS.tar.xz \
-o ros2_macOS.tar.xz https://github.com/light-tech/ROS2-On-iOS/releases/download/humble-macos/ros2_macOS.tar.xz
tar xzf ros2deps_macOS.tar.xz
tar xzf ros2_macOS.tar.xz
curl -L -o ros2_$targetPlatform.tar.xz https://github.com/light-tech/ROS2-On-iOS/releases/download/humble-macos/ros2_$targetPlatform.tar.xz
tar xzf ros2_$targetPlatform.tar.xz

# Source the prebuilt ROS2 base
# IMPORTANT: GitHub Action uses bash shell!
source $REPO_ROOT/ros2_macOS/setup.sh
source $REPO_ROOT/ros2_$targetPlatform/setup.sh

# Rebuild dependencies
# ./build_deps.sh macOS
# ./build_deps.sh $targetPlatform
}

buildWorkspace() {
Expand All @@ -118,14 +128,22 @@ buildWorkspace() {
cd ros2_ws
vcs import src < $REPO_ROOT/rviz2.repos

patchRviz

colconArgs+=(-DCMAKE_PREFIX_PATH=$ros2SystemDependenciesPath)

VERBOSE=1 colcon build --executor sequential --event-handlers console_direct+ "${colconArgs[@]}"
if [ $targetPlatform == "macOS_M1" ]; then
echo "Additional fixes for M1"
patchRvizM1
colconArgs+=(-DCMAKE_TOOLCHAIN_FILE=$REPO_ROOT/cmake/$targetPlatform.cmake)
fi

VERBOSE=1 colcon build --packages-up-to rviz_ogre_vendor "${colconArgs[@]}"
}

test -d $ros2PythonEnvPath || prepareVirtualEnv
source $ros2PythonEnvPath/bin/activate
printPython
buildRos2Base
# setupROS2base
# buildWorkspace
# buildRos2Base
setupROS2base
buildWorkspace
2 changes: 2 additions & 0 deletions cmake/macOS_M1.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ execute_process(COMMAND xcodebuild -version -sdk macosx Path
OUTPUT_STRIP_TRAILING_WHITESPACE)

SET(CMAKE_OSX_ARCHITECTURES "arm64")
SET(CMAKE_C_FLAGS "-arch arm64")
SET(CMAKE_CXX_FLAGS "-arch arm64")
SET(CMAKE_CROSSCOMPILING True)
SET(CMAKE_SYSTEM_PROCESSOR "arm64")

0 comments on commit 67f3de9

Please sign in to comment.