Skip to content

Fix linking - #2546

Merged
csukuangfj merged 2 commits into
k2-fsa:masterfrom
csukuangfj:fix-linking
Sep 1, 2025
Merged

csukuangfj merged 2 commits into
k2-fsa:masterfrom
csukuangfj:fix-linking

Conversation

@csukuangfj

@csukuangfj csukuangfj commented Sep 1, 2025 •

Copy link
Copy Markdown
Collaborator

Fixes #2544. The issue is introduced by #2487

Summary by CodeRabbit

  • Bug Fixes

    • Restored missing TTS/phonemization symbols by ensuring the TTS helper library is linked across iOS, macOS, Windows, Pascal, and C API examples.
    • Fixed a crash by making duration data handling conditional on valid recognition results.
  • Chores

    • Made TTS-related dependencies optional to reduce build size/time when TTS is off.
    • Updated pkg-config and build scripts so static linking of TTS components is consistent across platforms.

@coderabbitai

coderabbitai Bot commented Sep 1, 2025 •

Copy link
Copy Markdown

Walkthrough

Conditionally include cppinyin only when SHERPA_ONNX_ENABLE_TTS is ON and add cppinyin_core to link inputs across iOS, macOS Swift, C examples, pkg-config, Windows MFC props, and Pascal API. Move core linking of cppinyin into the TTS block and make durations copying conditional in OfflineRecognizer::GetResult.

Changes

Cohort / File(s) Summary
Top-level CMake conditionalization
CMakeLists.txt
Removed global include(cppinyin) and re-added it inside if(SHERPA_ONNX_ENABLE_TTS) before espeak-ng-for-piper.
Core library linking (sherpa-onnx CMake)
sherpa-onnx/csrc/CMakeLists.txt
Removed unconditional cppinyin_core from sherpa-onnx-core linking; added cppinyin_core alongside piper_phonemize inside the SHERPA_ONNX_ENABLE_TTS branch.
iOS build scripts
build-ios.sh
Include libcppinyin_core.a in the lists and in libtool merges for simulator and os64, ensuring it is merged into sherpa-onnx.a.
macOS Swift build
build-swift-macos.sh
Add ./install/lib/libcppinyin_core.a to libtool inputs when building libsherpa-onnx.a.
C examples & pkg-config
c-api-examples/Makefile, cmake/sherpa-onnx-static.pc.in
Append -lcppinyin_core to LDFLAGS and add -lcppinyin_core to the Libs: line in the static pkg-config template.
Windows MFC examples (props)
mfc-examples/.../sherpa-onnx-deps.props (3 files)
Insert cppinyin_core.lib into the SherpaOnnxLibraries property for the three MFC example props.
Pascal API linking
sherpa-onnx/pascal-api/sherpa_onnx.pas
Add cppinyin_core to the implementation linking section.
API behavior fix (C++)
sherpa-onnx/c-api/cxx-api.cc
Move the durations-copying block inside the if (r) guard so durations are accessed only when r is non-null.

Sequence Diagram(s)

sequenceDiagram
  participant Dev as Developer
  participant CMake as CMake
  participant Core as sherpa-onnx-core
  participant TTS as TTS Modules

  Dev->>CMake: Configure (SHERPA_ONNX_ENABLE_TTS=ON/OFF)
  alt TTS enabled
    CMake->>TTS: include(cppinyin)
    CMake->>Core: link piper_phonemize + cppinyin_core
  else TTS disabled
    CMake--xTTS: skip cppinyin include
    CMake->>Core: do not link cppinyin_core
  end
Loading
sequenceDiagram
  participant Caller as API caller
  participant Offline as OfflineRecognizer::GetResult
  participant Result as InternalResult r

  Caller->>Offline: GetResult()
  Offline->>Result: obtain r (may be null)
  alt r != null
    Offline->>Caller: copy text, tokens, json, lang, emotion, event, durations
  else r == null
    Offline->>Caller: return empty/default fields (no durations access)
  end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Assessment against linked issues

Objective Addressed Explanation
Fix iOS build error: Undefined symbol cppinyin::PinyinEncoder::Load (#2544) ✅

Assessment against linked issues: Out-of-scope changes

Code Change Explanation
Add cppinyin_core.lib to Windows MFC props (mfc-examples/.../sherpa-onnx-deps.props) Linked issue targets iOS undefined symbol; modifying Windows example linker inputs is unrelated to fixing iOS linker errors.
Add -lcppinyin_core to pkg-config template (cmake/sherpa-onnx-static.pc.in) Packaging change is broader than the iOS build fix and not required to resolve the specific iOS undefined symbol.
Add cppinyin_core to Pascal API linking (sherpa-onnx/pascal-api/sherpa_onnx.pas) Updating Pascal linking is unrelated to the iOS build failure described in the linked issue.

Possibly related PRs

  • Add tdt duration to APIs #2514 — Changes to OfflineRecognizer::GetResult and per-token durations handling (strongly related to the durations guard change).

Poem

A twitch of ears, a hop of glee,
Core linked right where TTS can see,
iOS finds pinyin, no more fright,
Durations safe, the code runs light.
I nibble carrots, build takes flight. 🐰✨

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore or @coderabbit ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@dosubot dosubot Bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Sep 1, 2025
@csukuangfj
csukuangfj requested a review from Copilot September 1, 2025 03:46

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes linking issues by ensuring the cppinyin_core library is properly included in builds. The fix addresses issue #2544 by moving the cppinyin dependency from being unconditionally linked to being linked only when TTS is enabled, while adding it to various build configurations that need it.

  • Move cppinyin dependency from global scope to TTS-only scope in CMake configuration
  • Add cppinyin_core library to Pascal API linking
  • Update build scripts and configuration files to include cppinyin_core library

Reviewed Changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated no comments.

Show a summary per file
File Description
CMakeLists.txt Moves cppinyin include from global to TTS-specific scope
sherpa-onnx/csrc/CMakeLists.txt Removes global cppinyin_core link and adds it under TTS condition
sherpa-onnx/pascal-api/sherpa_onnx.pas Adds cppinyin_core library linking
mfc-examples/*.props Adds cppinyin_core.lib to Windows build dependencies
cmake/sherpa-onnx-static.pc.in Adds lcppinyin_core to pkg-config library list
c-api-examples/Makefile Adds lcppinyin_core to linker flags
build-swift-macos.sh Includes libcppinyin_core.a in static library build
build-ios.sh Includes libcppinyin_core.a in iOS framework builds

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (6)
c-api-examples/Makefile (1)

7-7: Optionally link cppinyin only for the TTS example to keep ASR example lean

If you want to avoid over-linking decode-file-c-api, link cppinyin only for offline-tts-c-api.

Apply this diff:

- LDFLAGS += -lsherpa-onnx-c-api -lsherpa-onnx-core -lkaldi-decoder-core -lsherpa-onnx-kaldifst-core -lsherpa-onnx-fstfar -lsherpa-onnx-fst -lkaldi-native-fbank-core -lkissfft-float -lpiper_phonemize -lespeak-ng -lucd -lcargs -lonnxruntime -lcppinyin_core
+ LDFLAGS += -lsherpa-onnx-c-api -lsherpa-onnx-core -lkaldi-decoder-core -lsherpa-onnx-kaldifst-core -lsherpa-onnx-fstfar -lsherpa-onnx-fst -lkaldi-native-fbank-core -lkissfft-float -lpiper_phonemize -lespeak-ng -lucd -lcargs -lonnxruntime
@@
-	$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS)
+	$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) -lcppinyin_core

Also applies to: 20-21

sherpa-onnx/pascal-api/sherpa_onnx.pas (1)

666-666: Static link adds cppinyin_core; verify build when TTS is OFF

This unconditionally adds {$linklib cppinyin_core}. Since cppinyin_core is now built/linked only when SHERPA_ONNX_ENABLE_TTS is ON, static linking may fail on configurations with TTS disabled if libcppinyin_core.a isn’t produced/installed. Consider guarding this with a conditional define aligned with SHERPA_ONNX_ENABLE_TTS, or ensure the build always produces cppinyin_core even when TTS is OFF.

build-ios.sh (1)

130-137: Add existence checks for per-arch archives before lipo

If TTS gets disabled or a sub-build skips cppinyin, these lipo steps will fail hard. Suggest checking file existence to provide a clearer error.

Apply this diff:

 for f in libcppinyin_core.a libkaldi-native-fbank-core.a libkissfft-float.a libsherpa-onnx-c-api.a libsherpa-onnx-core.a \
          libsherpa-onnx-fstfar.a libssentencepiece_core.a \
          libsherpa-onnx-fst.a libsherpa-onnx-kaldifst-core.a libkaldi-decoder-core.a \
          libucd.a libpiper_phonemize.a libespeak-ng.a; do
+  if [[ ! -f build/simulator_arm64/lib/${f} || ! -f build/simulator_x86_64/lib/${f} ]]; then
+    echo "Missing ${f} for one or more simulator arches. Ensure SHERPA_ONNX_ENABLE_TTS=ON and libs are built." >&2
+    exit 1
+  fi
   lipo -create build/simulator_arm64/lib/${f} \
                build/simulator_x86_64/lib/${f} \
        -output build/simulator/lib/${f}
 done
build-swift-macos.sh (1)

29-42: build-swift-macos.sh: enforce TTS artifacts & harden script

  • Append -DSHERPA_ONNX_ENABLE_TTS=ON to your CMake invocation (or wrap the libtool block in if [ -f install/lib/libcppinyin_core.a ]; then … fi) to avoid failures when TTS is disabled.
  • Replace set -ex with set -euxo pipefail and change mkdir -p $dir/cd $dir to quote "$dir" for more robust error handling.
mfc-examples/NonStreamingSpeechRecognition/sherpa-onnx-deps.props (1)

18-24: Added cppinyin_core.lib to Windows deps — ensure TTS-built artifacts exist in install/lib.

These props unconditionally list TTS libs; builds where SHERPA_ONNX_ENABLE_TTS=OFF will link-fail. If that scenario matters, consider a TTS-specific props file or documenting the TTS=ON requirement for these examples.

mfc-examples/StreamingSpeechRecognition/sherpa-onnx-deps.props (1)

18-24: Mirrored addition of cppinyin_core.lib — consistent with other props; same TTS-on caveat applies.

If non-TTS builds of these examples are expected, split or gate the deps; otherwise, document TTS=ON.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 27311b8 and db28b02.

📒 Files selected for processing (10)
  • CMakeLists.txt (1 hunks)
  • build-ios.sh (3 hunks)
  • build-swift-macos.sh (1 hunks)
  • c-api-examples/Makefile (1 hunks)
  • cmake/sherpa-onnx-static.pc.in (1 hunks)
  • mfc-examples/NonStreamingSpeechRecognition/sherpa-onnx-deps.props (1 hunks)
  • mfc-examples/NonStreamingTextToSpeech/sherpa-onnx-deps.props (1 hunks)
  • mfc-examples/StreamingSpeechRecognition/sherpa-onnx-deps.props (1 hunks)
  • sherpa-onnx/csrc/CMakeLists.txt (1 hunks)
  • sherpa-onnx/pascal-api/sherpa_onnx.pas (1 hunks)
🧰 Additional context used
🧠 Learnings (2)
📚 Learning: 2025-08-06T04:23:50.237Z
Learnt from: litongjava
PR: k2-fsa/sherpa-onnx#2440
File: sherpa-onnx/java-api/src/main/java/com/k2fsa/sherpa/onnx/core/Core.java:4-6
Timestamp: 2025-08-06T04:23:50.237Z
Learning: The sherpa-onnx JNI library files are stored in Hugging Face repository at https://huggingface.co/csukuangfj/sherpa-onnx-libs under versioned directories like jni/1.12.7/, and the actual Windows JNI library filename is "sherpa-onnx-jni.dll" as defined in Core.java constants.

Applied to files:

  • mfc-examples/NonStreamingSpeechRecognition/sherpa-onnx-deps.props
  • sherpa-onnx/csrc/CMakeLists.txt
  • mfc-examples/NonStreamingTextToSpeech/sherpa-onnx-deps.props
  • build-ios.sh
  • mfc-examples/StreamingSpeechRecognition/sherpa-onnx-deps.props
📚 Learning: 2025-08-06T04:18:47.981Z
Learnt from: litongjava
PR: k2-fsa/sherpa-onnx#2440
File: sherpa-onnx/java-api/src/main/java/com/k2fsa/sherpa/onnx/core/Core.java:4-6
Timestamp: 2025-08-06T04:18:47.981Z
Learning: In sherpa-onnx Java API, the native library names in Core.java (WIN_NATIVE_LIBRARY_NAME = "sherpa-onnx-jni.dll", UNIX_NATIVE_LIBRARY_NAME = "libsherpa-onnx-jni.so", MACOS_NATIVE_LIBRARY_NAME = "libsherpa-onnx-jni.dylib") are copied directly from the compiled binary filenames and should not be changed to match other libraries' naming conventions.

Applied to files:

  • sherpa-onnx/csrc/CMakeLists.txt
  • build-ios.sh
🔇 Additional comments (6)
c-api-examples/Makefile (1)

7-7: cppinyin_core correctly added; link order is safe

Appending -lcppinyin_core after -lsherpa-onnx-core is correct for resolving Pinyin symbols. No issues seen.

sherpa-onnx/csrc/CMakeLists.txt (1)

313-315: Scoped TTS linking is correct

Linking cppinyin_core (and piper_phonemize) only inside the SHERPA_ONNX_ENABLE_TTS block aligns with the reported undefined symbol and avoids pulling it into non-TTS builds.

build-ios.sh (1)

142-155: Merging libcppinyin_core into the fat archives fixes the iOS undefined symbol

Including libcppinyin_core.a in both simulator and OS64 libtool merges is the right fix for the reported PinyinEncoder::Load() missing symbol.

Also applies to: 156-170

mfc-examples/NonStreamingTextToSpeech/sherpa-onnx-deps.props (1)

18-18: Windows MFC: cppinyin_core.lib correctly added

Appropriate for TTS; link order in the property list is fine.

CMakeLists.txt (1)

399-406: Conditionally including cppinyin under SHERPA_ONNX_ENABLE_TTS is correct.

This aligns the dependency with the feature flag and prevents pulling it into non-TTS builds.

cmake/sherpa-onnx-static.pc.in (1)

25-25: Include -lcppinyin_core in static pkg-config — ordering looks correct for static linkers.

It appears after libraries that may reference it, which is appropriate for --static link lines.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (4)
sherpa-onnx/c-api/cxx-api.cc (4)

163-178: Fix potential null dereference in OnlineRecognizer::GetResult

r is dereferenced without a null check; guard all accesses.

Apply:

   OnlineRecognizerResult ans;
-  ans.text = r->text;
-
-  ans.tokens.resize(r->count);
-  for (int32_t i = 0; i != r->count; ++i) {
-    ans.tokens[i] = r->tokens_arr[i];
-  }
-
-  if (r->timestamps) {
-    ans.timestamps.resize(r->count);
-    std::copy(r->timestamps, r->timestamps + r->count, ans.timestamps.data());
-  }
-
-  ans.json = r->json;
+  if (r) {
+    ans.text = r->text ? r->text : "";
+
+    ans.tokens.resize(r->count);
+    for (int32_t i = 0; i != r->count; ++i) {
+      ans.tokens[i] = r->tokens_arr[i];
+    }
+
+    if (r->timestamps) {
+      ans.timestamps.resize(r->count);
+      std::copy(r->timestamps, r->timestamps + r->count,
+                ans.timestamps.data());
+    }
+
+    ans.json = r->json ? r->json : "";
+  }

571-573: Same loop bug in KeywordSpotter::Decode (multi-stream)

Increment i, not n.

-  for (int32_t i = 0; i != n; ++n) {
+  for (int32_t i = 0; i != n; ++i) {
     streams[i] = ss[i].Get();
   }

303-305: Destroy() ignores argument; use p not p_

This can free the wrong pointer in move scenarios.

-void OfflineRecognizer::Destroy(const SherpaOnnxOfflineRecognizer *p) const {
-  SherpaOnnxDestroyOfflineRecognizer(p_);
-}
+void OfflineRecognizer::Destroy(const SherpaOnnxOfflineRecognizer *p) const {
+  SherpaOnnxDestroyOfflineRecognizer(p);
+}

151-154: Replace ++n with ++i in both loops

  • sherpa-onnx/c-api/cxx-api.cc:152
  • sherpa-onnx/c-api/cxx-api.cc:571
-for (int32_t i = 0; i != n; ++n) {
+for (int32_t i = 0; i != n; ++i) {
🧹 Nitpick comments (2)
sherpa-onnx/c-api/cxx-api.cc (2)

373-392: Add durations to GetResultPtr for parity with GetResult

Populate ans->durations when available.

   if (r) {
     ans->text = r->text;
@@
     ans->event = r->event ? r->event : "";
+    if (r->durations) {
+      ans->durations.resize(r->count);
+      std::copy(r->durations, r->durations + r->count,
+                ans->durations.data());
+    }
   }

561-563: Avoid returning the result of a void call (clarity)

Call then return; reduces confusion in void functions.

-void KeywordSpotter::Decode(const OnlineStream *s) const {
-  return SherpaOnnxDecodeKeywordStream(p_, s->Get());
-}
+void KeywordSpotter::Decode(const OnlineStream *s) const {
+  SherpaOnnxDecodeKeywordStream(p_, s->Get());
+}
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between db28b02 and 3ec0f3f.

📒 Files selected for processing (1)
  • sherpa-onnx/c-api/cxx-api.cc (1 hunks)
🔇 Additional comments (2)
sherpa-onnx/c-api/cxx-api.cc (2)

358-361: Durations copy under r-guard — LGTM

Moving durations handling inside the if (r) block avoids dereferencing a null result. Looks good.


579-596: Consider null-checking r in KeywordSpotter::GetResult

If the C API can return null, current code would dereference it.

If null is possible, apply:

-  KeywordResult ans;
-  ans.keyword = r->keyword;
+  KeywordResult ans;
+  if (!r) {
+    return ans;
+  }
+  ans.keyword = r->keyword ? r->keyword : "";
@@
-  ans.json = r->json;
+  ans.json = r->json ? r->json : "";

@csukuangfj
csukuangfj merged commit f0e68cd into k2-fsa:master Sep 1, 2025
18 of 43 checks passed
@csukuangfj
csukuangfj deleted the fix-linking branch September 1, 2025 03:59
@coderabbitai coderabbitai Bot mentioned this pull request Sep 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cppinyin库找不到

2 participants