Skip to content

Comments

CMake: generate chpl-language-server .cls-commands.json#28427

Open
DanilaFe wants to merge 6 commits intochapel-lang:mainfrom
DanilaFe:cmake-generate-cls-commands
Open

CMake: generate chpl-language-server .cls-commands.json#28427
DanilaFe wants to merge 6 commits intochapel-lang:mainfrom
DanilaFe:cmake-generate-cls-commands

Conversation

@DanilaFe
Copy link
Contributor

This enables editors to properly make sense of a project generated with CMake.

Testing

  • my minimal example generates .cls-commands.json when the environment variable is set.

Signed-off-by: Danila Fedorin <daniel.fedorin@hpe.com>
Signed-off-by: Danila Fedorin <daniel.fedorin@hpe.com>
The idea is to make things work with CMake. CMake
explicitly computes the Chapel executable path.
This is great, but `chpl-shim` isn't very good at
handling this, since its whole modus operandi is
to place itself as 'chpl' in the PATH, which CMake
circumvents. To make things work,

* Allow calling code to override 'CHPL_SHIM_REAL_COMPILER_PATH'
  (so that it's not read by chpl-shim from the PATH
  variable, which would violate whatever CMake computed).
* Have CMake invoke the linking command as 'chpl' (no
  explicit path) but provide 'CHPL_SHIM_REAL_COMPILER_PATH'
  (with the explicit path).

This way, CMake invokes `chpl-shim chpl <args>`,
`chpl-shim` places hooks into that call and parses the
arguments (thus generating JSON), and finally it
reads the environment variable to invoke the compiler
CMake intends.

Signed-off-by: Danila Fedorin <daniel.fedorin@hpe.com>
Signed-off-by: Danila Fedorin <daniel.fedorin@hpe.com>
Signed-off-by: Danila Fedorin <daniel.fedorin@hpe.com>
@DanilaFe DanilaFe force-pushed the cmake-generate-cls-commands branch from 1c4c962 to c31e72d Compare February 19, 2026 22:45
mkdir build && cd build
cmake .. -CMAKE_EXPORT_CHPL_COMMANDS=ON
cmake --build .
cd .. && ln -s build/.cls_commands.json .cls_commands.json
Copy link
Member

Choose a reason for hiding this comment

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

This is a bit unfortunate. We should consider adding a flag to CLS to specify the .cls-commands to use

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe, but that's not a flag that can be passed in general since CLS starts up in an arbitrary folder. Not to mention in VSCode, it starts up in potentially a different folder. So as far as I can see:

  • Using the flag with an absolute path is not possible, since that hardcodes CLS to only one project (as opposed to any project your editor opens)
  • Using the flag with a relative path doesn't work since that relative path need no exist

I suppose we could configure .cls-commands.json to allow pointing to a DIFFERENT .cls-commands.json, but isn't that a worse symlink?

FWIW, I use symlinks like this with compile-commands.json and C/C++.

Copy link
Member

Choose a reason for hiding this comment

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

FWIW, I use symlinks like this with compile-commands.json and C/C++.

👍

mkdir build && cd build
cmake .. -CMAKE_EXPORT_CHPL_COMMANDS=ON
cmake --build .
cd .. && ln -s build/.cls_commands.json .cls_commands.json
Copy link
Member

Choose a reason for hiding this comment

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

also note, its cls-commands, not cls_commands

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I knew it looked wrong!

Comment on lines 24 to 36
set(CMAKE_CHPL_LINK_EXECUTABLE "<CMAKE_CHPL_COMPILER> -o <TARGET> <OBJECTS> <FLAGS> <LINK_FLAGS> <LINK_LIBRARIES>")
if(CMAKE_EXPORT_CHPL_COMMANDS)
# Get the directory containing the chpl compiler to find chpl-shim
get_filename_component(CHPL_BIN_DIR "${CMAKE_CHPL_COMPILER}" DIRECTORY)
set(CHPL_SHIM_SCRIPT "${CHPL_BIN_DIR}/chpl-shim")

message(STATUS "CMAKE_EXPORT_CHPL_COMMANDS is ON, checking for chpl-shim at ${CHPL_SHIM_SCRIPT}")

if(EXISTS "${CHPL_SHIM_SCRIPT}")
# Use chpl-shim as a wrapper to generate .cls-commands.json
# Note: chpl-shim expects to be called as 'chpl-shim chpl <args>', not with full path
message(STATUS "Using chpl-shim to generate .cls-commands.json")
set(CMAKE_CHPL_LINK_EXECUTABLE "<CMAKE_COMMAND> -E env CHPL_SHIM_REAL_COMPILER_PATH=${CMAKE_CHPL_COMPILER} ${CHPL_SHIM_SCRIPT} chpl -o <TARGET> <OBJECTS> <FLAGS> <LINK_FLAGS> <LINK_LIBRARIES>")
Copy link
Member

Choose a reason for hiding this comment

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

anyway to reduce the duplication between the two link commands?

Comment on lines +33 to +34
# Use chpl-shim as a wrapper to generate .cls-commands.json
# Note: chpl-shim expects to be called as 'chpl-shim chpl <args>', not with full path
Copy link
Member

Choose a reason for hiding this comment

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

😭

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Feels like that's a hard limitation without dynamic loading magic (whatever bear does)

if(EXISTS "${CHPL_SHIM_SCRIPT}")
# Use chpl-shim as a wrapper to generate .cls-commands.json
# Note: chpl-shim expects to be called as 'chpl-shim chpl <args>', not with full path
message(STATUS "Using chpl-shim to generate .cls-commands.json")
Copy link
Member

Choose a reason for hiding this comment

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

nit, this message will be printed when configuring, not when chpl-shim is actually used

Signed-off-by: Danila Fedorin <daniel.fedorin@hpe.com>
Copy link
Member

@jabraham17 jabraham17 left a comment

Choose a reason for hiding this comment

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

except for the docs typo, looks good!


To enable use of CMake-based projects with the :ref:`language server <readme-chpl-language-server>`,
the CMake integration provides a special variable ``CMAKE_EXPORT_CHPL_COMMANDS``.
When set to ``ON``, CMake will generate a file named ``.cls_commands.json`` in
Copy link
Member

Choose a reason for hiding this comment

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

this needs updates too :)

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