Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable exception catching; Separate build targets for node and worker environments. #11

Merged
merged 21 commits into from
Nov 1, 2024

Conversation

junhaoliao
Copy link
Collaborator

@junhaoliao junhaoliao commented Oct 1, 2024

Description

  1. Enable exception catching with flag -fwasm-exceptions for all build types (i.e., "Release", "Debug") for easier debugging.
  2. Instead of compiling for environment "node" in the "Debug" build type and environment "worker" in the "Release" type, separate those into different targets, which allows optimization flags to be enabled with "Release" build type even when building for environment "node".
  3. Update target names in Taskfile.yml .
  4. Update package.json exports to export the release binaries for both environments.

Validation performed

  1. Built the js assets with task and ran below sample code:
    import ModuleInit from "./cmake-build-release/ClpFfiJs-node.js"
    import fs from "node:fs"
    
    const main = async () => {
        const file = fs.openSync("./empty-file", "w")
    
        console.time("perf")
        const Module = await ModuleInit()
        try {
            const decoder = new Module.ClpIrStreamReader(new Uint8Array(file))
            const numEvents = decoder.deserializeRange(0, 0)
            const results = decoder.decodeRange(0, numEvents)
            console.log(results)
        } catch (e) {
            console.trace("Exception caught:", e)
        }
        console.timeEnd("perf")
    }
    
    void main()
    
  2. Observed below in the console:
    /path/to/.nvm/versions/node/v22.1.0/bin/node /path/to/clp-ffi-js/test.mjs
    [2024-10-06 03:35:43.545] [info] [StreamReader.cpp:37] StreamReader::create: got buffer of length=14
    [2024-10-06 03:35:43.553] [error] [Decompressor.cpp:100] streaming_compression::zstd::Decompressor: ZSTD_decompressStream() error: Unknown frame descriptor
    [2024-10-06 03:35:43.553] [critical] [StreamReader.cpp:55] Failed to decode encoding type, err=4
    Trace: Exception caught: Exception [WebAssembly.Exception] {}
        at main (file:///path/to/clp-ffi-js/test.mjs:15:17)
    perf: 19.961ms
    
    Process finished with exit code 0
    

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Introduced support for multiple executable targets for different environments (node and worker).
    • Added new module structure with distinct entry points for each environment.
  • Chores

    • Updated build configuration for improved flexibility and modularity.
    • Adjusted package configuration to reflect new module type and export paths.

Copy link

coderabbitai bot commented Oct 1, 2024

Walkthrough

The changes in the pull request focus on updating the CMakeLists.txt, Taskfile.yml, and package.json files for the clp-ffi-js project. The CMakeLists.txt has been modified to support multiple executable targets for different environments, consolidating linker flags into a single variable. The Taskfile.yml now accommodates multiple target names and updates tasks accordingly. Additionally, package.json has been updated to declare the module type, modify export paths, and introduce new entry points for different environments.

Changes

Files Change Summary
CMakeLists.txt Introduced a loop for multiple executable targets, consolidated linker flags into CLP_FFI_JS_COMMON_LINK_OPTIONS, updated binary naming to be environment-specific, and set compile options uniformly.
Taskfile.yml Changed G_CLP_FFI_JS_TARGET_NAME to G_CLP_FFI_JS_ENV_NAMES for multiple environments, introduced G_CLP_FFI_JS_TARGET_PREFIX for dynamic target names, and updated tasks to iterate over these targets.
package.json Added "type": "module", updated files path, modified existing exports for the root path, and added new exports for "./node" and "./worker".

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant CMake
    participant Taskfile
    participant Package

    User->>CMake: Initiate Build
    CMake->>CMake: Define Executables for Node and Worker
    CMake-->>User: Build Complete
    User->>Taskfile: Execute Build Task
    Taskfile->>Taskfile: Iterate Over Targets
    Taskfile-->>User: Build Tasks Complete
    User->>Package: Access Module
    Package-->>User: Provide Exports for Node and Worker
Loading

Possibly related PRs

Suggested reviewers

  • kirkrodrigues

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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.
    • Generate unit testing code for this file.
    • 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. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • 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 src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai 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

Documentation and Community

  • 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.

CMakeLists.txt Outdated Show resolved Hide resolved
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🧹 Outside diff range and nitpick comments (6)
Taskfile.yml (2)

10-10: LGTM! Consider using a YAML sequence for better readability.

The change to support multiple target names aligns well with the PR objectives. It allows for building targets for different environments (node and worker).

For improved readability, consider using a YAML sequence instead of a space-separated string:

G_CLP_FFI_JS_TARGET_NAMES:
  - ClpFfiJs-node
  - ClpFfiJs-worker

This format would make it easier to add or remove targets in the future.


117-123: LGTM! The package task now handles multiple targets effectively.

The new for loop correctly iterates over the space-separated target names and copies the relevant files for each target. This change aligns well with the multi-target approach introduced in this PR.

Consider adding a comment explaining the purpose of this loop for better code documentation. For example:

# Iterate over all targets and copy their build artifacts to the output directory
- for:
    var: "G_CLP_FFI_JS_TARGET_NAMES"
    split: " "
  cmd: >-
    rsync -a
    "{{.G_CLP_FFI_JS_BUILD_DIR}}/{{.ITEM}}."{d.ts,js,wasm}
    "{{.OUTPUT_DIR}}/"
CMakeLists.txt (4)

109-113: Good addition of environment support.

The introduction of CLP_FFI_JS_ENVIRONMENTS allows for flexible builds targeting different JavaScript environments. This is a good practice for cross-environment compatibility.

Consider adding a brief comment explaining the purpose of these environments and how they differ in the build process.


115-118: Excellent implementation of multi-environment build.

The foreach loop effectively creates separate executables for each environment, which is a robust approach to handle different JavaScript contexts.

Consider adding a brief comment at the start of the loop to explain its purpose and the significance of creating separate executables for each environment.


120-128: Excellent consolidation of linker flags and addition of exception support.

The consolidation of linker flags into CLP_FFI_JS_LINKER_FLAGS improves maintainability. The addition of -fwasm-exceptions successfully addresses the PR objective of enabling exception catching.

Regarding the past comment from kirkrodrigues about -fwasm-exceptions, this flag has indeed been implemented as suggested, which is the correct approach for enabling Emscripten's JavaScript-based exception support.

Consider adding a brief comment explaining the purpose of each linker flag, especially for less common ones like -sWASM_BIGINT.


139-168: Well-structured target configuration and source inclusion.

The target configuration, including linking embind, setting include directories, and adding source files, is well-implemented within the environment loop. This ensures that each target is properly set up for its specific environment.

The use of embind is appropriate for binding C++ to JavaScript, which aligns well with the project's objectives.

Consider grouping related configuration steps (e.g., include directories, compile definitions, and source files) into separate functions to improve readability and maintainability of the CMakeLists.txt file.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 333dd2c and 9000016.

📒 Files selected for processing (3)
  • CMakeLists.txt (1 hunks)
  • Taskfile.yml (4 hunks)
  • package.json (1 hunks)
🔇 Additional comments (5)
package.json (2)

Line range hint 1-35: Ensure consistency across the project

The changes to package.json represent a significant shift in the project structure, supporting multiple environments. To maintain consistency and prevent potential issues:

  1. Update the project documentation to reflect these changes, especially regarding the new export paths and module type.

  2. Review and update any build scripts or CI/CD pipelines to ensure they're compatible with the new structure.

  3. Consider updating the version number (currently 0.1.0) to reflect these significant changes, following semantic versioning principles.

Please run the following script to check for potential inconsistencies:

#!/bin/bash
# Description: Check for potential inconsistencies in project files

# Test: Check if README.md mentions the new export paths
echo "Checking README.md for new export paths:"
rg --type md 'ClpFfiJs-worker.js|ClpFfiJs-node.js'

# Test: Check if build scripts in Taskfile.yml have been updated
echo "Checking Taskfile.yml for updated build tasks:"
rg --type yaml 'ClpFfiJs-worker|ClpFfiJs-node'

# Test: Check if .gitignore includes the new dist structure
echo "Checking .gitignore for new dist structure:"
rg --type gitignore '/dist/ClpFfiJs-(worker|node)'

Would you like assistance in updating the documentation or build scripts to reflect these changes?


18-18: Verify compatibility with ES modules

The addition of "type": "module" indicates that this package is now using ECMAScript modules. This is a significant change that affects how the package is imported and used.

Please ensure that:

  1. All code in the package is compatible with ES modules.
  2. Any CommonJS-specific syntax (e.g., require(), module.exports) has been replaced with ES module syntax (import/export).
  3. File extensions are explicitly specified in import statements where necessary.

Run the following script to check for potential incompatibilities:

✅ Verification successful

Retry ES module compatibility checks with corrected scripts

[/run_scripts]


Retry ES module compatibility checks with corrected scripts

[/run_scripts]


Retry ES module compatibility checks with corrected scripts

[/run_scripts]


Retry ES module compatibility checks with corrected scripts

[/run_scripts]


ES module compatibility verified successfully

No instances of CommonJS syntax (require(), module.exports) or import statements without file extensions were found.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for potential ES module incompatibilities

# Test: Search for CommonJS syntax
echo "Checking for CommonJS syntax:"
rg --type js 'require\(|module\.exports'

# Test: Check for missing file extensions in import statements
echo "Checking for import statements without file extensions:"
rg --type js 'import .+ from .+[^.]["\']'

Length of output: 476


Script:

#!/bin/bash
# Description: Check for potential ES module incompatibilities

# Test: Search for CommonJS syntax
echo "Checking for CommonJS syntax:"
fd --extension js --exec rg 'require\(|module\.exports' {}

# Test: Check for import statements without file extensions
echo "Checking for import statements without file extensions:"
fd --extension js --exec rg 'import\s+.*\s+from\s+["\'][^."\'\s]+["\']' {}

Length of output: 347


Script:

#!/bin/bash
# Description: Check for potential ES module incompatibilities

# Test: Search for CommonJS syntax
echo "Checking for CommonJS syntax:"
fd --extensions js,mjs --exec rg 'require\(|module\.exports' {}

# Test: Check for import statements without file extensions
echo "Checking for import statements without file extensions:"
fd --extensions js,mjs --exec rg "import\s+.*\s+from\s+['\"][^.'\"\\s]+['\"]" {}

Length of output: 759


Script:

#!/bin/bash
# Description: Check for potential ES module incompatibilities

# Test: Search for CommonJS syntax
echo "Checking for CommonJS syntax:"
fd --extension js --extension mjs --exec rg 'require\(|module\.exports' {}

# Test: Check for import statements without file extensions
echo "Checking for import statements without file extensions:"
fd --extension js --extension mjs --exec rg 'import\s+.*\s+from\s+["\'][^."\'\s]+["\']' {}

Length of output: 363


Script:

#!/bin/bash
# Description: Check for potential ES module incompatibilities

# Test: Search for CommonJS syntax
echo "Checking for CommonJS syntax:"
fd --extension js --extension mjs --exec rg 'require\(|module\.exports' {}

# Test: Check for import statements without file extensions
echo "Checking for import statements without file extensions:"
fd --extension js --extension mjs --exec rg "import\s+.*\s+from\s+['\"][^.'\"\s]+['\"]" {}

Length of output: 369

Taskfile.yml (1)

53-53: LGTM! The build command now supports multiple targets.

This change correctly utilizes the new G_CLP_FFI_JS_TARGET_NAMES variable, ensuring that all defined targets are built. It's consistent with the multi-target approach introduced in this PR.

CMakeLists.txt (2)

129-138: Good implementation of build information output and linker options.

The addition of the message output provides valuable information during the build process, enhancing transparency. The application of linker flags to the target is correctly implemented.

The clear output of build configuration details will be helpful for debugging and ensuring the correct build settings are applied.


109-168: Overall excellent improvements to the build system.

The changes to the CMakeLists.txt file significantly enhance the project's build system:

  1. Multi-environment support allows for flexible builds targeting different JavaScript contexts.
  2. Exception handling is now enabled, addressing the main PR objective.
  3. Consolidation of linker flags improves maintainability.
  4. The build process is more transparent with added information output.

These modifications result in a more robust, flexible, and maintainable build system that better supports the project's needs across different JavaScript environments.

Great work on implementing these improvements! The changes align well with the PR objectives and enhance the overall quality of the build system.

package.json Outdated
"files": [
"./dist"
"/dist"
Copy link

@coderabbitai coderabbitai bot Oct 5, 2024

Choose a reason for hiding this comment

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

⚠️ Potential issue

Correct the "files" field path

The change from "./dist" to "/dist" in the "files" field is problematic. The leading slash in "/dist" typically refers to the root of the file system, which is likely not the intended behaviour.

Please change this back to "./dist" or simply "dist" to correctly reference the distribution directory relative to the package root. For example:

"files": [
  "dist"
]

This ensures that the correct directory is included when the package is published.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I believe the / refers to the project root. Correct me if I'm wrong.

Copy link

Choose a reason for hiding this comment

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

@junhaoliao, in the package.json files field, the leading slash / refers to the root of the filesystem, not the project root. This means that specifying "/dist" may not include the intended directory when publishing your package. Paths in the files field should be relative to the package root, so it's recommended to use "./dist" or simply "dist" to correctly reference the dist directory.

(_/)
(•_•)
( > 🧐

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The slash was needed to mitigate an issue when I was simulated the package publish / installation locally with Yalc on a Mac computer (previously I don't recall observing the issue on a Linux computer). With the "./", the "dist" folder does not seem to be correctly included in the installations.
Still, it seems with no "./" or "/" added the installation and the config works fine locally with Yalc. Will stick with simply "dist" then.

Copy link

Choose a reason for hiding this comment

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

@junhaoliao, glad to hear that using "dist" works correctly with Yalc on Mac as well. Let me know if you have any other questions.

(_/)
(•ᴗ•)
( ♥


✏️ Learnings added
Learnt from: junhaoliao
PR: y-scope/clp-ffi-js#11
File: package.json:0-0
Timestamp: 2024-10-05T19:26:30.186Z
Learning: Using `"dist"` without any leading `"./"` or `"/"` in the `"files"` field of `package.json` ensures proper inclusion of the `dist` folder during package installation, especially when using tools like Yalc on Mac computers.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.


If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!

Comment on lines +24 to +33
"import": "./dist/ClpFfiJs-worker.js",
"types": "./dist/ClpFfiJs-worker.d.ts"
},
"./node": {
"import": "./dist/ClpFfiJs-node.js",
"types": "./dist/ClpFfiJs-node.d.ts"
},
"./worker": {
"import": "./dist/ClpFfiJs-worker.js",
"types": "./dist/ClpFfiJs-worker.d.ts"
Copy link

@coderabbitai coderabbitai bot Oct 5, 2024

Choose a reason for hiding this comment

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

💡 Codebase verification

🛠️ Refactor suggestion

Exported files are missing in the dist directory

The verification script indicates that the following exported files do not exist:

  • dist/ClpFfiJs-worker.js
  • dist/ClpFfiJs-worker.d.ts
  • dist/ClpFfiJs-node.js
  • dist/ClpFfiJs-node.d.ts

This suggests that the "exports" field in package.json references files that are missing, which may lead to runtime errors or broken imports.

Actions Needed:

  • Ensure that the build process generates the necessary files in the dist directory.
  • Verify the build configuration to include exports for both Node.js and Web Worker environments.
  • Update the "exports" field in package.json to reference existing files or adjust the build to produce the required files.
🔗 Analysis chain

Review the changes to export configurations

The modifications to the "exports" field indicate a shift towards supporting multiple environments (Node.js and Web Workers). However, there are some considerations:

  1. The main export (.) now points to the worker version by default. This might not be suitable for all use cases and could potentially break existing implementations.

  2. The addition of specific exports for node and worker environments provides more flexibility, which is good.

Consider the following suggestions:

  1. Make the main export more generic or provide a default that works in most environments. For example:
".": {
  "import": "./dist/ClpFfiJs.js",
  "types": "./dist/ClpFfiJs.d.ts"
}
  1. Ensure that the ClpFfiJs.js file can detect the environment and use the appropriate version (node or worker).

  2. Update the documentation to clearly explain the different export options and when to use each one.

Please run the following script to verify the existence of all referenced files:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the existence of exported files

files=(
  "dist/ClpFfiJs-worker.js"
  "dist/ClpFfiJs-worker.d.ts"
  "dist/ClpFfiJs-node.js"
  "dist/ClpFfiJs-node.d.ts"
)

for file in "${files[@]}"; do
  if [ ! -f "$file" ]; then
    echo "Warning: $file does not exist"
  else
    echo "$file exists"
  fi
done

Length of output: 553


Script:

#!/bin/bash
# Description: Verify the existence of exported files using fd

files=(
  "dist/ClpFfiJs-worker.js"
  "dist/ClpFfiJs-worker.d.ts"
  "dist/ClpFfiJs-node.js"
  "dist/ClpFfiJs-node.d.ts"
)

for file in "${files[@]}"; do
  if fd --type f --max-depth 2 "$(basename "$file")" | grep -q "$(basename "$file")"; then
    echo "$file exists"
  else
    echo "Warning: $file does not exist"
  fi
done

Length of output: 1237

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The files are generated rather than directly checked in, so it's expected you don't see the files under /dist without any compilations.

Copy link

Choose a reason for hiding this comment

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

@junhaoliao, thank you for the clarification. I understand that the files under /dist are generated during the build process and are not checked into source control. I'll make a note of this for future reviews.

(_/)
(•_•)
(🧠) Curious rabbit learns


✏️ Learnings added
Learnt from: junhaoliao
PR: y-scope/clp-ffi-js#11
File: package.json:24-33
Timestamp: 2024-10-05T19:27:36.977Z
Learning: In the `clp-ffi-js` project, the files in the `/dist` directory are generated during the build process and are not checked into source control, so their absence before compilation is expected.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

Taskfile.yml Outdated
@@ -41,7 +41,7 @@ tasks:
- "src/**/*"
generates: ["{{.CHECKSUM_FILE}}"]
deps:
- "emsdk"
# - "emsdk"
Copy link

@coderabbitai coderabbitai bot Oct 5, 2024

Choose a reason for hiding this comment

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

💡 Codebase verification

emsdk Dependency Remains in Use

The "emsdk" dependency is still referenced in both CMakeLists.txt and Taskfile.yml. Commenting out the dependency in Taskfile.yml does not fully remove its usage and may impact the build process. Please ensure that all references to "emsdk" are appropriately handled.

🔗 Analysis chain

Please clarify the reason for commenting out the emsdk dependency.

The emsdk dependency has been commented out, which could potentially affect the build process. This change wasn't mentioned in the PR objectives or summary. Could you please explain the rationale behind this modification?

To ensure this change doesn't introduce issues, please run the following script:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check if emsdk is still required in the build process

# Test: Search for emsdk usage in CMakeLists.txt and other relevant files
rg --type cmake 'emscripten|emsdk' CMakeLists.txt
rg --type yaml 'emscripten|emsdk' Taskfile.yml

Length of output: 1314

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Right, that was a typo. Reverted.

Copy link

Choose a reason for hiding this comment

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

@junhaoliao, thanks for correcting the typo!

(^_^)/


If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!

CMakeLists.txt Outdated Show resolved Hide resolved
@junhaoliao junhaoliao changed the title Enable exception catching with CMake "Debug" build type. Enable exception catching; Separate build targets for node and worker environments. Oct 5, 2024
CMakeLists.txt Outdated Show resolved Hide resolved
CMakeLists.txt Outdated Show resolved Hide resolved
CMakeLists.txt Outdated Show resolved Hide resolved
CMakeLists.txt Show resolved Hide resolved
CMakeLists.txt Outdated Show resolved Hide resolved
Taskfile.yml Outdated Show resolved Hide resolved
CMakeLists.txt Outdated Show resolved Hide resolved
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (6)
Taskfile.yml (3)

57-57: Efficient parallel build for multiple targets

The updated cmake build command now supports building multiple targets in parallel, which is more efficient and aligns well with the new multi-environment support. Well done!

For improved clarity, consider adding a comment explaining the parallel build process:

# Build all environment-specific targets in parallel
- "cmake --build '{{.OUTPUT_DIR}}' --parallel --target {{.G_CLP_FFI_JS_TARGET_NAMES}}"

122-128: Excellent update to support multiple targets in packaging

The package task has been effectively updated to handle multiple targets using a loop, which aligns perfectly with the PR objectives. This change makes the task more flexible and maintainable.

To improve readability, consider adding a comment and slightly restructuring the loop:

# Package all environment-specific targets
- for: { var: "G_CLP_FFI_JS_TARGET_NAMES", split: " " }
  cmd: >
    rsync -a
    "{{.G_CLP_FFI_JS_BUILD_DIR}}/{{.ITEM}}."{d.ts,js,wasm}
    "{{.OUTPUT_DIR}}/"

148-148: Good addition of CMake definition for supported environments

The new CMake definition CLP_FFI_JS_SUPPORTED_ENVIRONMENTS effectively passes the supported environments to the CMake configuration, aligning with the PR objectives of separating build targets for different environments.

For consistency with other multi-line CMake arguments, consider formatting it as follows:

-DCLP_FFI_JS_SUPPORTED_ENVIRONMENTS="{{.G_CLP_FFI_JS_ENV_NAMES}}" \
CMakeLists.txt (3)

63-78: LGTM! Consider adding a comment for -fwasm-exceptions.

The consolidation of compile and link options improves maintainability. The addition of -fwasm-exceptions aligns with the PR objectives for enabling exception catching in WebAssembly builds. The use of -flto for Release builds can improve performance.

Consider adding a brief comment explaining the purpose of -fwasm-exceptions for future reference:

set(CLP_FFI_JS_COMMON_COMPILE_OPTIONS
+    # Enable WebAssembly exception handling
    -fwasm-exceptions
)
set(CLP_FFI_JS_COMMON_LINK_OPTIONS
+    # Enable WebAssembly exception handling
    -fwasm-exceptions
    -sALLOW_MEMORY_GROWTH
    -sEXPORT_ES6

111-115: LGTM! Consider using a regular variable instead of CACHE INTERNAL.

The introduction of CLP_FFI_JS_SUPPORTED_ENVIRONMENTS supports the PR objective of separating build targets for different environments. However, using a CACHE INTERNAL variable might be unnecessary unless you plan to modify it from external scripts.

Consider using a regular variable instead:

-set(CLP_FFI_JS_SUPPORTED_ENVIRONMENTS
-    "node;worker"
-    CACHE INTERNAL
-    "List of supported environments."
-)
+set(CLP_FFI_JS_SUPPORTED_ENVIRONMENTS "node;worker")

This change would still allow you to override the variable from the command line if needed, while simplifying the CMake script.


126-143: LGTM! Comprehensive link options setup with good debugging info.

The link options setup is thorough and includes all necessary options, including environment-specific ones. The addition of -sENVIRONMENT=${env} addresses a previous comment about specifying the environment. The debug message provides useful information for troubleshooting.

Consider using string(JOIN " " CLP_FFI_JS_LINK_OPTIONS_STR "${CLP_FFI_JS_LINK_OPTIONS}") to create a space-separated string of link options for the debug message. This would improve readability in the CMake output:

 message(
     "CLP_FFI_JS_BIN_NAME=\"${CLP_FFI_JS_BIN_NAME}\". \
 CMAKE_BUILD_TYPE=\"${CMAKE_BUILD_TYPE}\". \
 Compile options: ${CLP_FFI_JS_COMMON_COMPILE_OPTIONS}. \
-Link options: ${CLP_FFI_JS_LINK_OPTIONS}"
+Link options: ${CLP_FFI_JS_LINK_OPTIONS_STR}"
 )
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 9000016 and c0f4d68.

📒 Files selected for processing (3)
  • CMakeLists.txt (2 hunks)
  • Taskfile.yml (4 hunks)
  • package.json (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • package.json
🧰 Additional context used
🔇 Additional comments (6)
Taskfile.yml (2)

11-15: Excellent addition of environment-specific variables!

The introduction of G_CLP_FFI_JS_ENV_NAMES and the dynamic generation of G_CLP_FFI_JS_TARGET_NAMES effectively support multiple environments (node and worker) as outlined in the PR objectives. This approach is both efficient and maintainable, allowing for easy addition of new environments in the future.


Line range hint 1-159: Overall excellent implementation of multi-environment support

The changes in this file effectively implement the PR objectives of enabling exception catching and separating build targets for "node" and "worker" environments. Key improvements include:

  1. Introduction of environment-specific variables
  2. Updated build commands to support multiple targets
  3. Flexible packaging process for different environments
  4. CMake configuration to pass supported environments

These changes enhance the build process's flexibility and maintainability while aligning with the project's goals. Great work on this implementation!

CMakeLists.txt (4)

117-119: LGTM! Excellent implementation of environment-specific targets.

The introduction of the foreach loop and dynamic executable naming effectively implements the PR objective of creating separate build targets for "node" and "worker" environments. This approach ensures unique executables for each environment and improves the flexibility of the build process.


121-124: LGTM! Consistent setup of compile options.

The setup of compile features, definitions, and options for each executable is well-structured and consistent with CMake best practices. The use of CLP_FFI_JS_COMMON_COMPILE_OPTIONS ensures that all environments receive the same base compile options, promoting consistency and maintainability.


145-158: LGTM! Well-structured include directories setup.

The include directories setup is well-structured and comprehensive. Marking the directories as SYSTEM is a good practice to suppress warnings from third-party headers, which helps maintain a clean build output. The included directories cover all necessary components for the project, including Boost, CLP core, fmt, spdlog, and zstd.


160-170: LGTM! Proper setup of source files and target sources.

The setup of source files and target sources is well-structured and comprehensive. The use of previously defined variables for source files improves maintainability. The additional include directory for the src/ folder ensures proper inclusion of project-specific headers. The endforeach() properly closes the environment loop, ensuring all settings are applied to each environment.

Taskfile.yml Outdated Show resolved Hide resolved
CMakeLists.txt Outdated Show resolved Hide resolved
CMakeLists.txt Outdated Show resolved Hide resolved
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (4)
Taskfile.yml (1)

54-58: Consider improving command readability

While the command is functionally correct, its readability could be improved by using a more structured format.

Consider this alternative format:

-      - >-
-        cmake
-        --build '{{.OUTPUT_DIR}}'
-        --parallel
-        --target {{range .G_CLP_FFI_JS_ENV_NAMES}}"{{$.G_CLP_FFI_JS_TARGET_PREFIX}}{{.}}" {{end}}
+      - |
+        cmake --build '{{.OUTPUT_DIR}}' \
+          --parallel \
+          --target {{range .G_CLP_FFI_JS_ENV_NAMES}}"{{$.G_CLP_FFI_JS_TARGET_PREFIX}}{{.}}" {{end}}
CMakeLists.txt (3)

63-82: LGTM! Consider adding performance impact documentation.

The consolidation of compile and link options is well-structured. The use of -fwasm-exceptions is a good choice based on the performance comparison shared in previous discussions (10.504s vs 17.087s for processing 380k log events).

Consider adding a comment documenting the performance impact of -fwasm-exceptions for future reference:

 # Set up common compile and link options to be merged with other options as necessary.
+# Note: Using -fwasm-exceptions adds minimal overhead (~0.2%) compared to -fexceptions (~63%)
 set(CLP_FFI_JS_COMMON_COMPILE_OPTIONS

111-116: LGTM! Consider improving readability.

The CLP_FFI_JS_SUPPORTED_ENVIRONMENTS cache variable is well-designed for external configuration through Taskfile.

Consider improving readability by placing each environment on a separate line:

 set(CLP_FFI_JS_SUPPORTED_ENVIRONMENTS
-    node
-    worker
+    node    # JavaScript runtime environment
+    worker  # Web worker environment
     CACHE INTERNAL
     "List of supported environments."
 )

139-144: Improve message formatting for better readability.

The current message formatting spans multiple lines with escaped newlines, which could be hard to read in CMake output.

Consider this more readable format:

-    message(
-            "CLP_FFI_JS_BIN_NAME=\"${CLP_FFI_JS_BIN_NAME}\". \
-CMAKE_BUILD_TYPE=\"${CMAKE_BUILD_TYPE}\". \
-Compile options: ${CLP_FFI_JS_COMMON_COMPILE_OPTIONS}. \
-Link options: ${CLP_FFI_JS_LINK_OPTIONS}."
-    )
+    message(STATUS "Building ${CLP_FFI_JS_BIN_NAME}")
+    message(STATUS "  Build type: ${CMAKE_BUILD_TYPE}")
+    message(STATUS "  Compile options: ${CLP_FFI_JS_COMMON_COMPILE_OPTIONS}")
+    message(STATUS "  Link options: ${CLP_FFI_JS_LINK_OPTIONS}")
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between c0f4d68 and 6df29f3.

📒 Files selected for processing (2)
  • CMakeLists.txt (2 hunks)
  • Taskfile.yml (4 hunks)
🔇 Additional comments (3)
Taskfile.yml (2)

11-12: Well-structured environment configuration!

The new variables provide a clean separation of concerns for different JavaScript environments. The array structure allows for easy addition of new environments in the future.


148-148: Verify environment list handling in CMake

The semicolon-separated list format is correct for CMake list variables, but let's verify how it's processed.

✅ Verification successful

Environment list handling in CMake is properly implemented

The semicolon-separated list format is correctly used and processed:

  • Task variable G_CLP_FFI_JS_ENV_NAMES is properly defined with ["node", "worker"]
  • The join operation creates a CMake-compatible list format
  • CMake correctly processes the list in a foreach loop to generate targets for each environment
  • The variable is only used in the main CMakeLists.txt as intended
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify how the environment list is processed in CMake

# Test: Check how the environment list is used in CMakeLists.txt
rg -A 5 'CLP_FFI_JS_SUPPORTED_ENVIRONMENTS' CMakeLists.txt

Length of output: 419


Script:

#!/bin/bash
# Let's verify the task variable and its usage
rg -A 2 'G_CLP_FFI_JS_ENV_NAMES' Taskfile.yml

# Also check if there are any other CMake files that might use this variable
fd -e cmake -e txt -x rg -l 'CLP_FFI_JS_SUPPORTED_ENVIRONMENTS' {}

Length of output: 711

CMakeLists.txt (1)

131-131: Verify TypeScript declaration file generation.

The --emit-tsd flag generates TypeScript declaration files. Please ensure that the generated .d.ts files correctly reflect the API for both environments.

Taskfile.yml Outdated
Comment on lines 123 to 128
- for:
var: "G_CLP_FFI_JS_ENV_NAMES"
cmd: >-
rsync -a
"{{.G_CLP_FFI_JS_BUILD_DIR}}/{{.G_CLP_FFI_JS_TARGET_PREFIX}}-{{.ITEM}}."{d.ts,js,wasm}
"{{.OUTPUT_DIR}}/"
Copy link

@coderabbitai coderabbitai bot Oct 31, 2024

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix the target name construction in rsync command

There's a syntax issue in the rsync command where the target name will have a double hyphen.

Apply this fix:

-          "{{.G_CLP_FFI_JS_BUILD_DIR}}/{{.G_CLP_FFI_JS_TARGET_PREFIX}}-{{.ITEM}}."{d.ts,js,wasm}
+          "{{.G_CLP_FFI_JS_BUILD_DIR}}/{{.G_CLP_FFI_JS_TARGET_PREFIX}}{{.ITEM}}."{d.ts,js,wasm}

The current format would generate paths like "ClpFfiJs--node.js" instead of "ClpFfiJs-node.js".

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
- for:
var: "G_CLP_FFI_JS_ENV_NAMES"
cmd: >-
rsync -a
"{{.G_CLP_FFI_JS_BUILD_DIR}}/{{.G_CLP_FFI_JS_TARGET_PREFIX}}-{{.ITEM}}."{d.ts,js,wasm}
"{{.OUTPUT_DIR}}/"
- for:
var: "G_CLP_FFI_JS_ENV_NAMES"
cmd: >-
rsync -a
"{{.G_CLP_FFI_JS_BUILD_DIR}}/{{.G_CLP_FFI_JS_TARGET_PREFIX}}{{.ITEM}}."{d.ts,js,wasm}
"{{.OUTPUT_DIR}}/"

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

thanks. i have fixed it in the latest commit

Copy link

Choose a reason for hiding this comment

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

@junhaoliao, glad to hear that you've fixed the issue!

(_/)
(•ᴗ•)
(")_(")


If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!

kirkrodrigues
kirkrodrigues previously approved these changes Oct 31, 2024
Copy link
Member

@kirkrodrigues kirkrodrigues left a comment

Choose a reason for hiding this comment

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

Minor nit, but otherwise lgtm.

CMakeLists.txt Show resolved Hide resolved
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (3)
Taskfile.yml (2)

54-58: Effective parallel build configuration for multiple environments.

The build command effectively handles multiple targets using the range operation. Consider adding a comment above the command to document the parallel build behaviour for future maintainers.

Add this comment above line 54:

+      # Build all environment targets in parallel

123-128: Robust file synchronization for multiple environments.

The rsync implementation effectively handles multiple environments. Consider adding error checking for the rsync operation.

Add error checking by modifying the command:

       - for:
           var: "G_CLP_FFI_JS_ENV_NAMES"
         cmd: >-
+          set -e;
           rsync -a
           "{{.G_CLP_FFI_JS_BUILD_DIR}}/{{.G_CLP_FFI_JS_TARGET_PREFIX}}{{.ITEM}}."{d.ts,js,wasm}
           "{{.OUTPUT_DIR}}/"
CMakeLists.txt (1)

140-145: Consider improving message formatting for better readability.

The current message format makes it difficult to read the output. Consider using multiple lines with proper indentation.

-    message(
-            "CLP_FFI_JS_BIN_NAME=\"${CLP_FFI_JS_BIN_NAME}\". \
-CMAKE_BUILD_TYPE=\"${CMAKE_BUILD_TYPE}\". \
-Compile options: ${CLP_FFI_JS_COMMON_COMPILE_OPTIONS}. \
-Link options: ${CLP_FFI_JS_LINK_OPTIONS}."
-    )
+    message(STATUS
+        "\nTarget configuration:"
+        "\n  Binary name: ${CLP_FFI_JS_BIN_NAME}"
+        "\n  Build type: ${CMAKE_BUILD_TYPE}"
+        "\n  Compile options: ${CLP_FFI_JS_COMMON_COMPILE_OPTIONS}"
+        "\n  Link options: ${CLP_FFI_JS_LINK_OPTIONS}"
+    )
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 6df29f3 and 340cca5.

📒 Files selected for processing (2)
  • CMakeLists.txt (2 hunks)
  • Taskfile.yml (4 hunks)
🔇 Additional comments (5)
Taskfile.yml (2)

11-12: Well-structured variable declarations for environment management!

The separation of environment names into an array and the introduction of a target prefix variable promote better organization and maintainability.


148-148: Proper CMake environment configuration.

The environment configuration parameter is well-structured and correctly formatted for CMake list variables.

CMakeLists.txt (3)

63-78: Well-structured implementation of build options!

The consolidation of compile and link options is well-organized, with thoughtful consideration for:

  • Using -fwasm-exceptions for both compilation and linking, which provides better performance compared to JS-emulated exceptions
  • Enabling Link Time Optimization (LTO) for Release builds to improve performance

111-116: Good choice making environments configurable!

The CACHE INTERNAL variable allows for flexible configuration from the Taskfile while maintaining proper encapsulation within CMake.


118-138: Excellent separation of build targets!

The implementation successfully:

  • Creates distinct targets for each environment
  • Applies appropriate compile and link options
  • Generates TypeScript definitions per target
  • Sets the correct environment flag

@junhaoliao junhaoliao merged commit 82079a5 into y-scope:main Nov 1, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants