Generate Protocol Buffers files for iOS

This guide shows you how to generate the Protocol Buffers source files and library for iOS from scratch.

Generate C++ source files from the proto file

  1. Install Protocol Buffers v3.10.1 on your local machine. Please pay attention to the section titled "Note for Mac users".

  2. Check your installation:

    protoc --version

    You should have libprotoc 3.10.1.

  3. In the Cardboard repository, navigate to the proto folder.

  4. Generate the Protocol Buffers source files in C++:

    protoc --proto_path=. --cpp_out=. cardboard_device.proto

    You should now have cardboard_device.pb.cc and cardboard_device.pb.h.

Cross-compile the Protocol Buffers library

You will need a cross-compiled Protocol Buffers library to build the Cardboard SDK.

  1. Clone the Protocol Buffers repository into your local machine:

    git clone https://github.com/protocolbuffers/protobuf.git
  2. Navigate into the into Protocol Buffers repository folder. Run the following commands:

    git checkout v3.10.1
    git submodule update --init --recursive
    ./autogen.sh
  3. Create environment variables:

    build_dir=`pwd`/../libprotobuf/ios
    darwin=darwin`uname -r`
    protoc=`which protoc`
    isysroot=`xcrun --sdk iphoneos --show-sdk-path`
    cflags="-Wno-unused-local-typedef -Wno-unused-function -DNDEBUG -g -O0 -pipe -fPIC -fcxx-exceptions"
    cxxflags="$cflags -std=c++11 -stdlib=libc++"
    
  4. Create the output directories:

    mkdir -p $build_dir/arch
    mkdir -p $build_dir/lib
    
  5. Configure the 64-bit build:

    ./configure \
    --build=x86_64-apple-$darwin \
    --host=arm \
    --with-protoc=$protoc \
    --disable-shared \
    --prefix=$build_dir \
    --exec-prefix=$build_dir/arch/arm64 \
    "CC=clang" \
    "CFLAGS=$cflags -miphoneos-version-min=8.0 -arch arm64 -isysroot $isysroot" \
    "CXX=clang" \
    "CXXFLAGS=$cxxflags -miphoneos-version-min=8.0 -arch arm64 -isysroot $isysroot" \
    LDFLAGS="-arch arm64 -miphoneos-version-min=8.0 -stdlib=libc++" \
    "LIBS=-lc++ -lc++abi"
    
  1. Build the 64-bit library:

    make -j8
    make install
    
  2. Clean the Protocol Buffers directory:

    make distclean
    
  3. Configure the 32-bit library build:

    ./configure \
    --build=x86_64-apple-$darwin \
    --host=armv7-apple-$darwin \
    --with-protoc=$protoc \
    --disable-shared \
    --prefix=$build_dir \
    --exec-prefix=$build_dir/arch/armv7 \
    "CC=clang" \
    "CFLAGS=$cflags -miphoneos-version-min=8.0 -arch armv7 -isysroot $isysroot" \
    "CXX=clang" \
    "CXXFLAGS=$cxxflags -miphoneos-version-min=8.0 -arch armv7 -isysroot $isysroot" \
    LDFLAGS="-arch armv7 -miphoneos-version-min=8.0 -stdlib=libc++" \
    "LIBS=-lc++ -lc++abi"
    
  4. Build the 32-bit library:

    make -j8
    make install
    
  5. Build the fat library:

    lipo \
    $build_dir/arch/arm64/lib/libprotobuf-lite.a \
    $build_dir/arch/armv7/lib/libprotobuf-lite.a \
    -create \
    -output $build_dir/lib/libprotobuf-lite.a
    
  6. Remove unused libraries:

    rm -rf $build_dir/arch
    
  7. Navigate out of the protobuf repository. You should now have the cross-compiled library (libprotobuf-lite.a) in the libprotobuf/ios/lib directory, and all the include files in the include (liprotobuf/ios/include) directory.

  8. Follow the schema below to copy the libprotobuf directory into the proto directory in the Cardboard repository:

    proto
    ├── cardboard_device.pb.cc
    ├── cardboard_device.pb.h
    └── libprotobuf
        └── ios
            ├── include
            │   └── google/*
            └── lib
                └── libprotobuf-lite.a