CLion integration: ld.exe unrecognized option '--major-image-version'

Platform: Windows 10
Board: Wemos D1 mini
Installed python 3.8, then:
Did following under git bash (msys2) :
$ virtualenv ~/.platformio/penv
$ source ~/.platformio/penv/
$ pip install platformio
$ source ~/.platformio/penv/Scripts/activate

To ensure that CLion/cmake reach platformio binary I also added “~/.platformio/penv/Scripts” to PATH in Windows system


# Created project with platformIO
$ mkdir ~/dev/clion/foobar; cd ~/dev/clion/foobar
$ platformio init --ide clion --board d1_mini
# copied example into src folder
$ cp /tmp/main.cpp src/
# build the project, no errors
$ platformio run  

I now imported the project into CLion and things look ok. All run configurations are there

When I build the project it compiles ok but I get a linker error:
xtensa-lx106-elf/bin/ld.exe: unrecognized option ‘–major-image-version’

Full output in CLion:

====================[ Build | PLATFORMIO_BUILD | d1_mini ]======================
"C:\Program Files\JetBrains\CLion 2019.2\bin\cmake\win\bin\cmake.exe" --build C:\Users\erikb\dev\clion\foobar\cmake-build-d1_mini --target PLATFORMIO_BUILD -- -j 6
Processing d1_mini (platform: espressif8266; board: d1_mini; framework: arduino)
Verbose mode can be enabled via `-v, --verbose` option
PLATFORM: Espressif 8266 2.3.3 > WeMos D1 R2 and mini
HARDWARE: ESP8266 80MHz, 80KB RAM, 4MB Flash
 - framework-arduinoespressif8266 3.20603.200130 (2.6.3) 
 - tool-esptool 1.413.0 (4.13) 
 - tool-esptoolpy 1.20800.0 (2.8.0) 
 - toolchain-xtensa 2.40802.191122 (4.8.2)
LDF: Library Dependency Finder ->
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 29 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
Archiving .pio\build\d1_mini\libFrameworkArduinoVariant.a
Indexing .pio\build\d1_mini\libFrameworkArduinoVariant.a
Archiving .pio\build\d1_mini\libFrameworkArduino.a
Indexing .pio\build\d1_mini\libFrameworkArduino.a
Linking .pio\build\d1_mini\firmware.elf
Retrieving maximum program size .pio\build\d1_mini\firmware.elf
Checking size .pio\build\d1_mini\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [===       ]  32.7% (used 26804 bytes from 81920 bytes)
Flash: [==        ]  24.5% (used 256312 bytes from 1044464 bytes)
========================= [SUCCESS] Took 2.49 seconds =========================

Build finished

====================[ Build | Z_DUMMY_TARGET | d1_mini ]========================
"C:\Program Files\JetBrains\CLion 2019.2\bin\cmake\win\bin\cmake.exe" --build C:\Users\erikb\dev\clion\foobar\cmake-build-d1_mini --target Z_DUMMY_TARGET -- -j 6
[ 50%] Linking CXX executable Z_DUMMY_TARGET.exe
c:/users/erikb/.platformio/packages/toolchain-xtensa/bin/../lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/bin/ld.exe: unrecognized option '--major-image-version'
c:/users/erikb/.platformio/packages/toolchain-xtensa/bin/../lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/bin/ld.exe: use the --help option for usage information
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [CMakeFiles\Z_DUMMY_TARGET.dir\build.make:86: Z_DUMMY_TARGET.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:423: CMakeFiles/Z_DUMMY_TARGET.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:430: CMakeFiles/Z_DUMMY_TARGET.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:273: Z_DUMMY_TARGET] Error 2

Only reference I can find to major-image-version is in link.txt file under cmake-build-d1_mini . The content of the file is:

"C:\Program Files\JetBrains\CLion 2019.2\bin\cmake\win\bin\cmake.exe" -E remove -f CMakeFiles\Z_DUMMY_TARGET.dir/objects.a
C:\mingw-w64\i686-8.1.0-posix-dwarf-rt_v6-rev0\mingw32\bin\ar.exe cr CMakeFiles\Z_DUMMY_TARGET.dir/objects.a @CMakeFiles\Z_DUMMY_TARGET.dir\objects1.rsp
C:\Users\erikb\.platformio\packages\toolchain-xtensa\bin\xtensa-lx106-elf-g++.exe -fno-rtti -std=c++11 -Os -mlongcalls -mtext-section-literals -falign-functions=4 -U__STRICT_ANSI__ -ffunction-sections -fdata-sections -fno-exceptions -Wall   -Wl,--whole-archive CMakeFiles\Z_DUMMY_TARGET.dir/objects.a -Wl,--no-whole-archive  -o Z_DUMMY_TARGET.exe -Wl,--out-implib,libZ_DUMMY_TARGET.dll.a -Wl,--major-image-version,0,--minor-image-version,0 @CMakeFiles\Z_DUMMY_TARGET.dir\linklibs.rsp

The main PlatformIO build has finished (also meaning PIO is working perfectly fine) but CLion has initiated its own cmake based build, which is wrong. Are you only pressing the build button in CLion as it’s shown in the documentation? Which version of CLion are you using?

Ok, my bad. Yes I was pressing the button as instructed in documentation. HOWEVER, the first time I tried to build I did press the CLion “play” button. It then forced me to select an executable (Z_DUMMY_TARGET).
After that mistake pressing the pio build button also triggers the error.

I removed the z_dummy_target from the runconfiguration and then it worked ok with the pio button, the build now completes without invoking the cmake step.

Thanks for your help.