2

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?

starball
  • 20,030
  • 7
  • 43
  • 238
cbuchart
  • 10,847
  • 9
  • 53
  • 93
  • `but I don't have a .cmake file but a script. The script may change every time the toolchain is updated` so run the script from the toolchain file?? – KamilCuk Apr 20 '22 at 10:03

3 Answers3

1

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

Anton Menshov
  • 2,266
  • 14
  • 34
  • 55
1

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.
starball
  • 20,030
  • 7
  • 43
  • 238
0

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.