Skip to content

Commit

Permalink
build: support universal macOS binary [skip appveyor]
Browse files Browse the repository at this point in the history
  • Loading branch information
lotem committed Jan 9, 2021
2 parents 2297718 + 9bee96e commit 1fad0b8
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 43 deletions.
29 changes: 21 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,42 @@ matrix:
sudo: required
install: "./travis-install.sh"
script: make test
- &osx_build
- if: tag IS NOT present
os: osx
compiler: clang
install: "./travis-install.sh"
script: make xcode/test
before_deploy:
- make xcode/dist
- if: tag IS present
env: >
BOOST_ROOT="${TRAVIS_BUILD_DIR}/thirdparty/src/boost_1_75_0"
BUILD_UNIVERSAL=1
RIME_PLUGINS="${RIME_BUNDLED_PLUGINS}"
os: osx
osx_image: xcode12u
compiler: clang
install: "./travis-install.sh"
script:
- make xcode/test xcode/dist RIME_PLUGINS=
- |
zip -r rime-${TRAVIS_TAG}-${TRAVIS_OS_NAME}.zip \
dist thirdparty/bin thirdparty/share \
-x '*/curl.exe'
- make xcode/test xcode/dist
- |
zip -r ${RIME_VARIANT:-rime}-${TRAVIS_TAG}-${TRAVIS_OS_NAME}.zip \
zip -r rime-with-plugins-${TRAVIS_TAG}-${TRAVIS_OS_NAME}.zip \
dist thirdparty/bin thirdparty/share \
-x '*/curl.exe'
deploy:
provider: releases
api_key:
secure: Le2PfdrOqVmk5c1Eu/q9fvLGG1WZz5SuIXw0LVTqWn8cPCJvvsh9/RsjKx59uI0rxs/sJnbsCnPSaiveY5i51Jy7apkqtysUfglr+2CjgyXY5s7qBWiZ60/j2V5CDkk+Q/PSB/GXcVxHQWPh6X1M75GWa9b+4COKJX3Kh/FO0rM=
file: "${RIME_VARIANT:-rime}-${TRAVIS_TAG}-${TRAVIS_OS_NAME}.zip"
file:
- "rime-${TRAVIS_TAG}-${TRAVIS_OS_NAME}.zip"
- "rime-with-plugins-${TRAVIS_TAG}-${TRAVIS_OS_NAME}.zip"
skip_cleanup: true
name: librime ${TRAVIS_TAG}
body: 'chore(release): ${TRAVIS_TAG} :tada:'
draft: true
on:
repo: rime/librime
tags: true
- <<: *osx_build
if: tag IS present
env: RIME_PLUGINS="${RIME_BUNDLED_PLUGINS}" RIME_VARIANT="rime-with-plugins"
84 changes: 58 additions & 26 deletions README-mac.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,83 @@ Install other build tools:
brew install cmake git
```

Install Boost C++ libraries:
## Get the code

``` sh
brew install boost
git clone --recursive https://github.com/rime/librime.git
```
or [download from GitHub](https://github.com/rime/librime), then get code for
third party dependencies separately.

> **Note:**
>
> Starting from version 1.68, `boost::locale` library from Homebrew depends on
> `icu4c`, which is not provided by macOS.
>
> The make target `xcode/release-with-icu` tells cmake to link to ICU libraries
> installed locally with Homebrew. This is only required if building with the
> `librime-charcode` plugin.
>
> To make a portable build with the plugin, install an earlier version of
> `boost` via homebrew:
## Install Boost C++ libraries

Boost is a third-party library which librime code heavily depend on.
These dependencies include a few compiled (non-header-only) Boost libraries.

**Option 1 (recommended):** Download and build Boost from source.

``` sh
brew install boost@1.60
brew link --force boost@1.60
cd librime
make xcode/thirdparty/boost
```

If you manually download and build Boost libraries from source code, set shell
variable `BOOST_ROOT` to its top level directory prior to building librime.
The make script will download Boost source tarball, extract it to
`librime/thirdparty/src/boost_<version>` and create needed static libraries
for building macOS uinversal binary.

## Get the code
Set shell variable `BOOST_ROOT` to the path to `boost_<version>` directory prior
to building librime.

``` sh
git clone --recursive https://github.com/rime/librime.git
export BOOST_ROOT="$(pwd)/thirdparty/src/boost_1_75_0"
```

**Option 2:** Install Boost libraries from Homebrew.

``` sh
brew install boost
```

This is a time-saving option if you are building and installing Rime only for your
own Mac computer.

Built with Homebrewed version of Boost, the `librime` binary will not be
portable to machines without certain Homebrew formulae installed.

**Option 3:** Install an older version of Boost libraries from Homebrew.

Starting from version 1.68, `boost::locale` library from Homebrew depends on
`icu4c`, which is not provided by macOS.

Make target `xcode/release-with-icu` tells cmake to link to ICU libraries
installed locally with Homebrew. This is only required if building with the
[`librime-charcode`](https://github.com/rime/librime-charcode) plugin.

To make a portable build with this plugin, install an earlier version of
`boost` that wasn't dependent on `icu4c`:

``` sh
brew install boost@1.60
brew link --force boost@1.60
```
or [download from GitHub](https://github.com/rime/librime), then get code for
third party dependencies separately.

## Build third-party libraries

Required third-party libraries other than Boost are included as git submodules:

``` sh
cd librime
# cd librime

# if you haven't checked out the submodules with git clone --recursive ..., do:
# git submodule update --init

make xcode/thirdparty
```

This builds dependent libraries in `thirdparty/src/*`, and copies artifacts to
`thirdparty/lib` and `thirdparty/bin`.
This builds libraries located at `thirdparty/src/*`, and installs the build
artifacts to `thirdparty/include`, `thirdparty/lib` and `thirdparty/bin`.

You can build an individual library, eg. opencc, with:
You can also build an individual library, eg. `opencc`, with:

``` sh
make xcode/thirdparty/opencc
Expand All @@ -65,7 +97,7 @@ make xcode/thirdparty/opencc
``` sh
make xcode
```
This creates `build/lib/Release/librime.dylib` and command line tools
This creates `build/lib/Release/librime*.dylib` and command line tools
`build/bin/Release/rime_*`.

Or, create a debug build:
Expand Down
40 changes: 40 additions & 0 deletions install-boost.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/bin/bash
set -ev

RIME_ROOT="$(cd "$(dirname "$0")"; pwd)"

boost_version="${boost_version=1.75.0}"
boost_x_y_z="${boost_version//./_}"

BOOST_ROOT="${BOOST_ROOT=${RIME_ROOT}/thirdparty/src/boost_${boost_x_y_z}}"

boost_tarball="boost_${boost_x_y_z}.tar.bz2"
download_url="https://dl.bintray.com/boostorg/release/${boost_version}/source/${boost_tarball}"
boost_tarball_sha256sum_1_75_0='953db31e016db7bb207f11432bef7df100516eeb746843fa0486a222e3fd49cb boost_1_75_0.tar.bz2'
boost_tarball_sha256sum="${boost_tarball_sha256sum=${boost_tarball_sha256sum_1_75_0}}"

download_boost_source() {
cd "${RIME_ROOT}/thirdparty/src"
if ! [[ -f "${boost_tarball}" ]]; then
curl -LO "${download_url}"
fi
echo "${boost_tarball_sha256sum}" | shasum -a 256 -c
tar --bzip2 -xf "${boost_tarball}"
[[ -f "${BOOST_ROOT}/bootstrap.sh" ]]
}

boost_libs="${boost_libs=filesystem,regex,system}"
boost_cxxflags='-arch arm64 -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk'

build_boost_macos() {
cd "${BOOST_ROOT}"
./bootstrap.sh --with-libraries="${boost_libs}"
./b2 -q -a toolset=darwin link=static cxxflags="${boost_cxxflags}" stage
}

if [[ "$OSTYPE" =~ 'darwin' ]]; then
if ! [[ -f "${BOOST_ROOT}/bootstrap.sh" ]]; then
download_boost_source
fi
build_boost_macos
fi
7 changes: 7 additions & 0 deletions thirdparty.mk
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ capnproto:
-DBUILD_TESTING:BOOL=OFF \
-DCMAKE_BUILD_TYPE:STRING="Release" \
-DCMAKE_INSTALL_PREFIX:PATH="$(THIRD_PARTY_DIR)" \
$(CMAKE_OSX_ARCHS) \
&& cmake --build $(build) --target install

glog:
Expand All @@ -42,6 +43,7 @@ glog:
-DWITH_GFLAGS:BOOL=OFF \
-DCMAKE_BUILD_TYPE:STRING="Release" \
-DCMAKE_INSTALL_PREFIX:PATH="$(THIRD_PARTY_DIR)" \
$(CMAKE_OSX_ARCHS) \
&& cmake --build $(build) --target install

gtest:
Expand All @@ -50,6 +52,7 @@ gtest:
-DBUILD_GMOCK:BOOL=OFF \
-DCMAKE_BUILD_TYPE:STRING="Release" \
-DCMAKE_INSTALL_PREFIX:PATH="$(THIRD_PARTY_DIR)" \
$(CMAKE_OSX_ARCHS) \
&& cmake --build $(build) --target install

leveldb:
Expand All @@ -59,13 +62,15 @@ leveldb:
-DLEVELDB_BUILD_TESTS:BOOL=OFF \
-DCMAKE_BUILD_TYPE:STRING="Release" \
-DCMAKE_INSTALL_PREFIX:PATH="$(THIRD_PARTY_DIR)" \
$(CMAKE_OSX_ARCHS) \
&& cmake --build $(build) --target install

marisa:
cd $(SRC_DIR)/marisa-trie; \
cmake $(SRC_DIR) -B$(build) \
-DCMAKE_BUILD_TYPE:STRING="Release" \
-DCMAKE_INSTALL_PREFIX:PATH="$(THIRD_PARTY_DIR)" \
$(CMAKE_OSX_ARCHS) \
&& cmake --build $(build) --target install

opencc:
Expand All @@ -74,6 +79,7 @@ opencc:
-DBUILD_SHARED_LIBS:BOOL=OFF \
-DCMAKE_BUILD_TYPE:STRING="Release" \
-DCMAKE_INSTALL_PREFIX:PATH="$(THIRD_PARTY_DIR)" \
$(CMAKE_OSX_ARCHS) \
&& cmake --build $(build) --target install

yaml-cpp:
Expand All @@ -84,4 +90,5 @@ yaml-cpp:
-DYAML_CPP_BUILD_TOOLS:BOOL=OFF \
-DCMAKE_BUILD_TYPE:STRING="Release" \
-DCMAKE_INSTALL_PREFIX:PATH="$(THIRD_PARTY_DIR)" \
$(CMAKE_OSX_ARCHS) \
&& cmake --build $(build) --target install
3 changes: 3 additions & 0 deletions travis-install-osx.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#!/bin/bash

if [[ -n "$BOOST_ROOT" ]]; then
make xcode/thirdparty/boost
fi
make xcode/thirdparty
33 changes: 24 additions & 9 deletions xcode.mk
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ RIME_ROOT = $(CURDIR)

RIME_DIST_DIR = $(RIME_ROOT)/dist

RIME_COMPILER_OPTIONS = CC=clang CXX=clang++ \
CXXFLAGS="-stdlib=libc++" LDFLAGS="-stdlib=libc++"
ifdef BOOST_ROOT
CMAKE_BOOST_OPTIONS = -DBoost_NO_BOOST_CMAKE=TRUE \
-DBOOST_ROOT="$(BOOST_ROOT)"
endif

ifdef BUILD_UNIVERSAL
export CMAKE_OSX_ARCHS = -DCMAKE_OSX_ARCHITECTURES="arm64;x86_64"
endif

ICU_PREFIX = $(shell brew --prefix)/opt/icu4c

Expand All @@ -19,30 +25,36 @@ release:
cmake . -B$(build) -GXcode \
-DBUILD_STATIC=ON \
-DCMAKE_BUILD_WITH_INSTALL_RPATH=ON \
-DCMAKE_INSTALL_PREFIX="$(RIME_DIST_DIR)"
-DCMAKE_INSTALL_PREFIX="$(RIME_DIST_DIR)" \
$(CMAKE_BOOST_OPTIONS) \
$(CMAKE_OSX_ARCHS)
cmake --build $(build) --config Release

release-with-icu:
cmake . -B$(build) -GXcode \
-DBUILD_STATIC=ON \
-DBUILD_WITH_ICU=ON \
-DCMAKE_BUILD_WITH_INSTALL_RPATH=ON \
-DCMAKE_INSTALL_PREFIX="$(RIME_DIST_DIR)" \
-DBUILD_WITH_ICU=ON \
-DCMAKE_PREFIX_PATH=$(ICU_PREFIX)
-DCMAKE_PREFIX_PATH="$(ICU_PREFIX)" \
$(CMAKE_BOOST_OPTIONS) \
$(CMAKE_OSX_ARCHS)
cmake --build $(build) --config Release

debug:
cmake . -B$(build) -GXcode \
-DBUILD_STATIC=ON \
-DBUILD_SEPARATE_LIBS=ON
-DBUILD_SEPARATE_LIBS=ON \
$(CMAKE_BOOST_OPTIONS)
cmake --build $(build) --config Debug

debug-with-icu:
cmake . -B$(build) -GXcode \
-DBUILD_STATIC=ON \
-DBUILD_SEPARATE_LIBS=ON \
-DBUILD_WITH_ICU=ON \
-DCMAKE_PREFIX_PATH=$(ICU_PREFIX)
-DCMAKE_PREFIX_PATH="$(ICU_PREFIX)" \
$(CMAKE_BOOST_OPTIONS)
cmake --build $(build) --config Debug

clean:
Expand All @@ -68,7 +80,10 @@ test-debug: debug
(cd $(build)/test; Debug/rime_test)

thirdparty:
$(RIME_COMPILER_OPTIONS) make -f thirdparty.mk
make -f thirdparty.mk

thirdparty/boost:
./install-boost.sh

thirdparty/%:
$(RIME_COMPILER_OPTIONS) make -f thirdparty.mk $(@:thirdparty/%=%)
make -f thirdparty.mk $(@:thirdparty/%=%)

0 comments on commit 1fad0b8

Please sign in to comment.