CLion and mbed Framework attempts to compile twice

When a project is created which uses the mbed Framework and CLion as the IDE, using the “PLATFORMIO_UPLOAD” target compiles the project twice. The first one succeeds, the second one fails.

On Eclipse, it just compiles and uploads once, without an error.

To reproduce:

mkdir mbed-pio && cd mbed-pio
pio init -b nucleo_l152re --ide=clion 

Import the project in CLion and add the test program

#include "mbed.h"
Serial pc(USBTX, USBRX);
int main() {
    while(1) {
        pc.printf("Hello World\n");
        wait(0.5);
    }
}

Using the “upload” target and doing Run->Build gives:

/home/max/Downloads/clion-2017.3/bin/cmake/bin/cmake --build /home/max/Documents/mbed-pio/cmake-build-debug --target PLATFORMIO_UPLOAD -- -j 2
[Thu Jan 18 11:36:46 2018] Processing nucleo_l152re (platform: ststm32; board: nucleo_l152re; framework: mbed)
--------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
Collected 14 compatible libraries
Scanning dependencies...
No dependencies
Compiling .pioenvs/nucleo_l152re/FrameworkMbedCore/drivers/AnalogIn.o
Compiling .pioenvs/nucleo_l152re/FrameworkMbedCore/drivers/BusIn.o
Compiling .pioenvs/nucleo_l152re/FrameworkMbedCore/drivers/BusInOut.o
...
Compiling .pioenvs/nucleo_l152re/FrameworkMbedCore/targets/TARGET_STM/us_ticker_16b.o
Compiling .pioenvs/nucleo_l152re/FrameworkMbedCore/targets/TARGET_STM/us_ticker_32b.o
In file included from /home/max/.platformio/packages/framework-mbed/targets/TARGET_STM/TARGET_STM32L1/spi_device.h:33:0,
from /home/max/.platformio/packages/framework-mbed/targets/TARGET_STM/stm_spi_api.c:42:
/home/max/.platformio/packages/framework-mbed/targets/TARGET_STM/TARGET_STM32L1/device/stm32l1xx_ll_spi.h: In function 'LL_SPI_TransmitData16':
/home/max/.platformio/packages/framework-mbed/targets/TARGET_STM/TARGET_STM32L1/device/stm32l1xx_ll_spi.h:1137:3: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
*((__IO uint16_t *)&SPIx->DR) = TxData;
^
Compiling .pioenvs/nucleo_l152re/src/main.o
Generating LD script .pioenvs/nucleo_l152re/STM32L152XE.ld.link_script.ld
Linking .pioenvs/nucleo_l152re/firmware.elf
Checking program size
text	   data	    bss	    dec	    hex	filename
38428	   2512	   1292	  42232	   a4f8	.pioenvs/nucleo_l152re/firmware.elf
Building .pioenvs/nucleo_l152re/firmware.bin
Looking for upload disk...
Auto-detected: /media/max/NODE_L152RE
Uploading .pioenvs/nucleo_l152re/firmware.bin
Firmware has been successfully uploaded.
(Some boards may require manual hard reset)
========================= [SUCCESS] Took 20.12 seconds =========================
Built target PLATFORMIO_UPLOAD

/home/max/Downloads/clion-2017.3/bin/cmake/bin/cmake --build /home/max/Documents/mbed-pio/cmake-build-debug --target mbed-pio -- -j 2
[ 50%] Building CXX object CMakeFiles/mbed-pio.dir/src/main.cpp.o
<command-line>:0:0: warning: "__UINT_LEAST16_MAX__" redefined [enabled by default]
<built-in>:0:0: note: this is the location of the previous definition
<command-line>:0:0: warning: "__ARM_SIZEOF_WCHAR_T" redefined [enabled by default]
<built-in>:0:0: note: this is the location of the previous definition
<command-line>:0:0: warning: "__UINT8_MAX__" redefined [enabled by default]
<built-in>:0:0: note: this is the location of the previous definition
<command-line>:0:0: warning: "__WINT_MAX__" redefined [enabled by default]
<built-in>:0:0: note: this is the location of the previous definition
<command-line>:0:0: warning: "__SIZE_MAX__" redefined [enabled by default]
<built-in>:0:0: note: this is the location of the previous definition
<command-line>:0:0: warning: "__WCHAR_MAX__" redefined [enabled by default]
[a ton of errors]

/home/max/.platformio/packages/framework-mbed/./drivers/UARTSerial.h:58:51: error: 'MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE' was not declared in this scope
     UARTSerial(PinName tx, PinName rx, int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE);
                                                   ^
/home/max/Documents/mbed-pio/src/main.cpp:17:23: error: call to 'mbed::Serial::Serial(PinName, PinName, const char*, int)' uses the default argument for parameter 4, which is not yet defined
 Serial pc(USBTX, USBRX);
                       ^
CMakeFiles/mbed-pio.dir/build.make:62: recipe for target 'CMakeFiles/mbed-pio.dir/src/main.cpp.o' failed
make[3]: *** [CMakeFiles/mbed-pio.dir/src/main.cpp.o] Error 1
CMakeFiles/Makefile2:395: recipe for target 'CMakeFiles/mbed-pio.dir/all' failed
make[2]: *** [CMakeFiles/mbed-pio.dir/all] Error 2
CMakeFiles/Makefile2:407: recipe for target 'CMakeFiles/mbed-pio.dir/rule' failed
make[1]: *** [CMakeFiles/mbed-pio.dir/rule] Error 2
Makefile:235: recipe for target 'mbed-pio' failed
make: *** [mbed-pio] Error 2

If you do a pio init --ide=eclipse and execute the same upload target, it just compiles once and everything works; I thus presume that this is a CLion specific problem.

Using CLion 2017.3 and platformio version 3.5.1a7.

See Redirecting...
You need to use BUILD button instead of RUN.