Build libLiteRtLmC #4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Build libLiteRtLmC | |
| # Builds the LiteRT-LM C engine as a shared library from source. | |
| # Strategy: build the project normally (all static archives), then post-link | |
| # them into a single shared library. This avoids patching CMake targets. | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| litert_lm_tag: | |
| description: "LiteRT-LM upstream tag to build from" | |
| default: "v0.10.2" | |
| type: string | |
| push: | |
| tags: ["lm-build-*"] | |
| env: | |
| LITERT_LM_TAG: ${{ inputs.litert_lm_tag || 'v0.10.2' }} | |
| jobs: | |
| build-linux-x86_64: | |
| name: linux x86_64 | |
| runs-on: ubuntu-latest | |
| timeout-minutes: 90 | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: install build deps | |
| run: | | |
| sudo apt-get update && sudo apt-get install -y \ | |
| cmake ninja-build g++ git | |
| - name: clone litert-lm | |
| run: | | |
| git clone --depth=1 --branch=${{ env.LITERT_LM_TAG }} \ | |
| https://github.com/google-ai-edge/litert-lm.git /tmp/litert-lm | |
| # Use the OUTER CMakeLists.txt — it orchestrates ExternalProject deps | |
| # (protobuf, abseil, flatbuffers, TFLite, etc.) in the right order. | |
| # PIC flags are critical: every .a must be position-independent to | |
| # link into a .so later. | |
| - name: cmake configure (outer) | |
| run: | | |
| cmake -S /tmp/litert-lm -B /tmp/litert-lm/build \ | |
| -DCMAKE_BUILD_TYPE=Release \ | |
| -DCMAKE_C_FLAGS="-fPIC" \ | |
| -DCMAKE_CXX_FLAGS="-fPIC" \ | |
| -DCMAKE_POSITION_INDEPENDENT_CODE=ON | |
| - name: cmake build (full project) | |
| run: cmake --build /tmp/litert-lm/build -j$(nproc) | |
| - name: create version script | |
| run: | | |
| cat > /tmp/litert_lm_exports.map <<'EOF' | |
| VERS_1.0 { | |
| global: | |
| litert_lm_*; | |
| local: | |
| *; | |
| }; | |
| EOF | |
| - name: post-link shared library | |
| run: | | |
| BUILD=/tmp/litert-lm/build | |
| # The C engine static lib (our API surface) | |
| ENGINE_A=$(find $BUILD -name 'libc_engine.a' -type f | head -1) | |
| echo "engine lib: $ENGINE_A" | |
| # All other static archives (deps) | |
| ALL_A=$(find $BUILD -name '*.a' -type f ! -name 'libc_engine.a' | sort) | |
| echo "total .a files: $(echo "$ALL_A" | wc -l)" | |
| # Link: whole-archive the engine (so all litert_lm_* symbols survive), | |
| # regular-link the deps (only pull what's referenced). | |
| g++ -shared -o /tmp/libLiteRtLmC.so \ | |
| -Wl,--version-script=/tmp/litert_lm_exports.map \ | |
| -Wl,--whole-archive $ENGINE_A -Wl,--no-whole-archive \ | |
| $ALL_A \ | |
| -Wl,--allow-multiple-definition \ | |
| -lstdc++ -lpthread -ldl -lm | |
| echo "=== output ===" | |
| ls -la /tmp/libLiteRtLmC.so | |
| echo "litert_lm_* symbols:" | |
| nm -D --defined-only /tmp/libLiteRtLmC.so | grep -c 'litert_lm_' | |
| - uses: actions/upload-artifact@v4 | |
| with: | |
| name: libLiteRtLmC-linux-x86_64 | |
| path: /tmp/libLiteRtLmC.so | |
| if-no-files-found: error | |
| build-macos-arm64: | |
| name: macos arm64 | |
| runs-on: macos-latest | |
| timeout-minutes: 90 | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: install build deps | |
| run: brew install cmake ninja | |
| - name: clone litert-lm | |
| run: | | |
| git clone --depth=1 --branch=${{ env.LITERT_LM_TAG }} \ | |
| https://github.com/google-ai-edge/litert-lm.git /tmp/litert-lm | |
| - name: cmake configure (outer) | |
| run: | | |
| cmake -S /tmp/litert-lm -B /tmp/litert-lm/build \ | |
| -DCMAKE_BUILD_TYPE=Release \ | |
| -DCMAKE_POSITION_INDEPENDENT_CODE=ON | |
| - name: cmake build (full project) | |
| run: cmake --build /tmp/litert-lm/build -j$(sysctl -n hw.ncpu) | |
| - name: create export list | |
| run: | | |
| grep -E 'LITERT_LM_C_API_EXPORT' /tmp/litert-lm/c/engine.h -A1 | \ | |
| grep 'litert_lm_' | sed 's/.*\(litert_lm_[a-z_]*\).*/\1/' | \ | |
| sed 's/^/_/' | sort -u > /tmp/litert_lm_exports.txt | |
| echo "export count: $(wc -l < /tmp/litert_lm_exports.txt)" | |
| cat /tmp/litert_lm_exports.txt | |
| - name: post-link shared library | |
| run: | | |
| BUILD=/tmp/litert-lm/build | |
| ENGINE_A=$(find $BUILD -name 'libc_engine.a' -type f | head -1) | |
| echo "engine lib: $ENGINE_A" | |
| ALL_A=$(find $BUILD -name '*.a' -type f ! -name 'libc_engine.a' | sort) | |
| echo "total .a files: $(echo "$ALL_A" | wc -l)" | |
| clang++ -dynamiclib -o /tmp/libLiteRtLmC.dylib \ | |
| -Wl,-exported_symbols_list,/tmp/litert_lm_exports.txt \ | |
| -Wl,-all_load $ENGINE_A \ | |
| $ALL_A \ | |
| -framework Foundation -framework Metal -framework CoreFoundation \ | |
| -lstdc++ -lpthread -ldl -lm | |
| echo "=== output ===" | |
| ls -la /tmp/libLiteRtLmC.dylib | |
| echo "litert_lm_* symbols:" | |
| nm -gU /tmp/libLiteRtLmC.dylib | grep -c 'litert_lm_' | |
| - uses: actions/upload-artifact@v4 | |
| with: | |
| name: libLiteRtLmC-macos-arm64 | |
| path: /tmp/libLiteRtLmC.dylib | |
| if-no-files-found: error |