cbuchart
cbuchart

Reputation: 11555

Executing a script to set environment variables before CMake configuration in CLion

I'm setting up a cross-compiling environment with CLion and need to execute a specific configuration script before configuring CMake.

I can do it in the command line as explained in this answer:

source /path/to/the-script
cmake -DCMAKE_C_COMPILER=$CC -DCMAKE_CXX_COMPILER=$CXX ...

Also, I can do it in VS Code through the CMake kits (.vscode/cmake-kits.json):

[
    {
        "name": "ARM",
        "environmentSetupScript": "/path/to/the-script"
    }
]

Now I'd like to do the same in CLion but all that I can do is

  1. Define all the environment variables manually using -D.

  2. Use a CMake toolchain file through -DCMAKE_TOOLCHAIN_FILE, but I don't have a .cmake file but a script. The script may change every time the toolchain is updated, so I'd like to avoid preparing one manually.

So, is there any way to execute the script before launching CMake in CLion?

Upvotes: 3

Views: 2028

Answers (3)

starball
starball

Reputation: 50254

This might be an XY problem. If your script just sets a bunch of environment variables, you can create a CMakePresets.json file and define a configuration preset that sets those environment variables.

  • The official CMake documentation for CMakePresets files is here.
  • The CLion user guide on CMakePresets files is here.

You may want to follow https://intellij-support.jetbrains.com/hc/en-us/community/posts/360003487420-Source-script-before-building and/or https://youtrack.jetbrains.com/issue/CPP-8414.

Upvotes: 1

massimo
massimo

Reputation: 1

I did it like this: In Clion click on Run -> Edit Configurations

Click on + and add Makefile Target. Select the path for the Makefile that will be generated.

Then go to Before Launch and click on + -> Run External Tool. Here click another time on + and add the script you want to execute.

Then you can build and compile your project clicking on the Run arrow.

Upvotes: 0

Piotr Serwa
Piotr Serwa

Reputation: 11

I didn't find a way to source a script. So instead, I run printenv before and after running the source script and then I stored all new/modified env variables in CLion cmake configuration (field Environment).

Upvotes: 1

Related Questions