raspberrypi / pico-project-generator

Tool to automatically generate a Pico C SDK Project
BSD 3-Clause "New" or "Revised" License
289 stars 74 forks source link

Project Generator not compatible with SDK version 1.5 on Windows? #68

Open ullibak opened 1 year ago

ullibak commented 1 year ago

Hi I installed the C/C++ SDK by running "pico-setup-windows-x64-standalone.exe" and then downloaded the Pico Project Generator. The Project Generator did not run because tkinter is missing (I posted this as a separate issue on github.com/raspberrypi/pico-setup-windows/issues/12). I then installed a new version of Python manually and changed the python path in "pico-env.cmd" to point to that newly installed python.exe. With this trick, tkinter was present and I was able to run the Project Generator in GUI mode.

However, when trying to create a new project, the error message appears:

_CMake Error at CMakeLists.txt:21 (project): Running

'nmake' '-?'

failed with:

Das System kann die angegebene Datei nicht finden (=file not found)._

As far as I understand, the SDK installer for Windows sets up CMake to use ninja and not nmake (anymore). Is there a possibility to make the Project Generator compatible with the latest version of the SDK installer for Windows?

Thx.

JamesH65 commented 1 year ago

Hi, thanks for the report, I'll try and find the time to look in to this.

JamesH65 commented 1 year ago

@kilograham Hi Graham, do you know the runes for a ninja build on windows to replicate the current "cmake -DCMAKE_BUILD_TYPE=Debug -G "NMake Makefiles" .."

JamesH65 commented 1 year ago

I suspect it is as simple as :

cmake -DCMAKE_BUILD_TYPE=Debug -G Ninja ..

then

ninja in the build folder?

kilograham commented 1 year ago

yes (assuming the installer puts ninja in the path)

ullibak commented 1 year ago

Modified pico_project.py starting from line 1341:

if isWindows:
    # Had a special case report, when using MinGW, need to check if using nmake or mingw32-make.
    if shutil.which("mingw32-make"):
        # Assume MinGW environment
        cmakeCmd = 'cmake -DCMAKE_BUILD_TYPE=Debug -G "MinGW Makefiles" ..'
        makeCmd = 'mingw32-make '
    elif shutil.which("ninja"):
        # When installing SDK version 1.5.0 on windows with installer pico-setup-windows-x64-standalone.exe, ninja is used 
        cmakeCmd = 'cmake -DCMAKE_BUILD_TYPE=Debug -G Ninja ..'
        makeCmd = 'ninja '                 
    else:
        # Everything else assume nmake
        cmakeCmd = 'cmake -DCMAKE_BUILD_TYPE=Debug -G "NMake Makefiles" ..'
        makeCmd = 'nmake '

seems to work fine.

Many Thanks!

ullibak commented 1 year ago

With the modifications described above, compiling was possible, but I ran into problems when trying to debug using VSCode. It turned out that the JSON files generated in the .vscode directory do not match the settings needed for the SDK installed using "pico-setup-windows-x64-standalone.exe". I modified the python script to add a function that generates the JSON files of the .vscode direcory in the examples folder. I am by far not an expert on these settings, so if someone with knowledge could have a look at it and maybe this code could be added in a future version of the pico project generator.

Here is the function:

# Generates the requested project files for Windows SDK 1.5.0 
def generateProjectFiles_Win(projectPath, projectName, sdkPath, projects, debugger):
oldCWD = os.getcwd()

os.chdir(projectPath)

deb = debugger_config_list[debugger]

for p in projects :
    if p == 'vscode':
        # launch.json
        v1 = (  '{\n'
                '  "version": "0.2.0",\n'
                '  "configurations": [\n'
                '    {\n'
                '      "name": "Pico Debug (Cortex-Debug)",\n'
                '      "cwd": "${workspaceFolder}",\n'
                '      "executable": "${command:cmake.launchTargetPath}",\n'
                '      "request": "launch",\n'
                '      "type": "cortex-debug",\n'
                '      "servertype": "openocd",\n'
                '      "gdbPath": "arm-none-eabi-gdb",\n'
                '      "device": "RP2040",\n'
                '      "configFiles": [\n'
                '        "interface/cmsis-dap.cfg",\n'
                '        "target/rp2040.cfg"\n'
                '      ],\n'
                '      "svdFile": "${env:PICO_SDK_PATH}/src/rp2040/hardware_regs/rp2040.svd",\n'
                '      "runToEntryPoint": "main",\n'
                '      "openOCDLaunchCommands": [\n'
                '        "adapter speed 1000"\n'
                '      ]\n'
                '    },\n'
                '    {\n'
                '      "name": "Pico Debug (Cortex-Debug with external OpenOCD)",\n'
                '      "cwd": "${workspaceFolder}",\n'
                '      "executable": "${command:cmake.launchTargetPath}",\n'
                '      "request": "launch",\n'
                '      "type": "cortex-debug",\n'
                '      "servertype": "external",\n'
                '      "gdbTarget": "localhost:3333",\n'
                '      "gdbPath": "arm-none-eabi-gdb",\n'
                '      "device": "RP2040",\n'
                '      "svdFile": "${env:PICO_SDK_PATH}/src/rp2040/hardware_regs/rp2040.svd",\n'
                '      "runToEntryPoint": "main"\n'
                '    },\n'
                '    {\n'
                '      "name": "Pico Debug (C++ Debugger)",\n'
                '      "type": "cppdbg",\n'
                '      "request": "launch",\n'
                '      "cwd": "${workspaceFolder}",\n'
                '      "program": "${command:cmake.launchTargetPath}",\n'
                '      "MIMode": "gdb",\n'
                '      "miDebuggerPath": "arm-none-eabi-gdb",\n'
                '      "miDebuggerServerAddress": "localhost:3333",\n'
                '      "debugServerPath": "openocd",\n'
                '      "debugServerArgs": "-f interface/cmsis-dap.cfg -f target/rp2040.cfg -c \\"adapter speed 1000\\"",\n'
                '      "serverStarted": "Listening on port .* for gdb connections",\n'
                '      "filterStderr": true,\n'
                '      "stopAtEntry": true,\n'
                '      "hardwareBreakpoints": {\n'
                '        "require": true,\n'
                '        "limit": 4\n'
                '      },\n'
                '      "preLaunchTask": "Flash",\n'
                '      "svdPath": "${env:PICO_SDK_PATH}/src/rp2040/hardware_regs/rp2040.svd"\n'
                '    }\n'
                '  ]\n'
                '}\n')

        # c_cpp_properties.json
        c1 = (  '{\n'
                '  "configurations": [\n'
                '    {\n'
                '      "name": "Pico",\n'
                '      "includePath": [\n'
                '        "${workspaceFolder}/**",\n'
                '        "${env:PICO_SDK_PATH}/**"\n'
                '      ],\n'
                '      "defines": [],\n'
                '      "compilerPath": "${env:PICO_INSTALL_PATH}/gcc-arm-none-eabi/bin/arm-none-eabi-gcc.exe",\n'
                '      "cStandard": "c11",\n'
                '      "cppStandard": "c++11",\n'
                '      "intelliSenseMode": "linux-gcc-arm",\n'
                '      "configurationProvider": "ms-vscode.cmake-tools"\n'
                '    }\n'
                '  ],\n'
                '  "version": 4\n'
                '}\n')

        #settings.json
        s1 = (  '{\n'                  
                '    // These settings tweaks to the cmake plugin will ensure\n'
                '    // that you debug using cortex-debug instead of trying to launch\n'
                '    // a Pico binary on the host\n'
                '    "cmake.statusbar.advanced": {\n'
                '        "debug": {\n'
                '            "visibility": "hidden"\n'
                '        },\n'
                '        "launch": {\n'
                '            "visibility": "hidden"\n'
                '        },\n'
                '        "build": {\n'
                '            "visibility": "hidden"\n'
                '        },\n'
                '        "buildTarget": {\n'
                '            "visibility": "hidden"\n'
                '        }\n'
                '    },\n'
                '    "cmake.buildBeforeRun": true,\n'
                '    "cmake.configureOnOpen": true,\n'
                '    "cmake.configureSettings": {\n'
                '      "CMAKE_MODULE_PATH": "${env:PICO_INSTALL_PATH}/pico-sdk-tools"\n'
                '    },\n'
                '    "cmake.generator": "Ninja",\n'
                '    "C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools"\n'
                '}\n')

        #extensions.json
        e1 = (  '{\n'
                '  "recommendations": [\n'
                '    "marus25.cortex-debug",\n'
                '    "ms-vscode.cmake-tools",\n'
                '    "ms-vscode.cpptools",\n'
                '    "ms-vscode.cpptools-extension-pack",\n'
                '    "ms-vscode.vscode-serial-monitor"\n'
                '  ]\n'
                '}\n')                     

        #cmake-kits.json
        k1 = (  '[\n'
                '  {\n'
                '    "name": "Pico ARM GCC",\n'
                '    "description": "Pico SDK Toolchain with GCC arm-none-eabi",\n'
                '    "toolchainFile": "${env:PICO_SDK_PATH}/cmake/preload/toolchains/pico_arm_gcc.cmake"\n'
                '  }\n'
                ']\n')         

        #tasks.json
        t1 = (  '{\n'
                '  "version": "2.0.0",\n'
                '  "tasks": [\n'
                '    {\n'
                '      "label": "Flash",\n'
                '      "type": "shell",\n'
                '      "command": "openocd",\n'
                '      "args": [\n'
                '        "-f",\n'
                '        "interface/cmsis-dap.cfg",\n'
                '        "-f",\n'
                '        "target/rp2040.cfg",\n'
                '        "-c",\n'
                '        "adapter speed 1000; program {${command:cmake.launchTargetPath}} verify reset exit"\n'
                '      ],\n'
                '      "problemMatcher": []\n'
                '    },\n'
                '    {\n'
                '      "label": "Build",\n'
                '      "type": "cmake",\n'
                '      "command": "build",\n'
                '      "problemMatcher": "$gcc",\n'
                '      "group": {\n'
                '        "kind": "build",\n'
                '        "isDefault": true\n'
                '      }\n'
                '    }\n'
                '  ]\n'
                '}\n')

        # Create a build folder, and run our cmake project build from it
        if not os.path.exists(VSCODE_FOLDER):
            os.mkdir(VSCODE_FOLDER)

        os.chdir(VSCODE_FOLDER)

        filename = VSCODE_LAUNCH_FILENAME
        file = open(filename, 'w')
        file.write(v1)
        file.close()

        file = open(VSCODE_C_PROPERTIES_FILENAME, 'w')
        file.write(c1)
        file.close()

        file = open(VSCODE_SETTINGS_FILENAME, 'w')
        file.write(s1)
        file.close()

        file = open(VSCODE_EXTENSIONS_FILENAME, 'w')
        file.write(e1)
        file.close()

        file = open(VSCODE_CMAKEKITS_FILENAME, 'w')
        file.write(k1)
        file.close()

        file = open(VSCODE_TASKS_FILENAME, 'w')
        file.write(t1)
        file.close()

    else :
        print('Unknown project type requested')

os.chdir(oldCWD)

And further down in the code, an if-clause to choose these files if on Windows:

if params['projects']:
        if isWindows:
            generateProjectFiles_Win(projectPath, params['projectName'], params['sdkPath'], params['projects'], params['debugger'])
        else:
            generateProjectFiles(projectPath, params['projectName'], params['sdkPath'], params['projects'], params['debugger'])
JamesH65 commented 1 year ago

Think I need to have a chat with the VSCode dev on this one...!

JamesH65 commented 1 year ago

Hi, I just pushed a possible fix for all this to

https://github.com/raspberrypi/pico-project-generator/tree/update_vscode_json_to_sdk1_5

Testing would be appreciated.

ullibak commented 1 year ago

Tested. Test environment: Windows Sandbox, freshly installed https://github.com/raspberrypi/pico-setup-windows Because tkinter is (for no obvious reasons) still missing in this packet, installed latest Python for Windows: https://www.python.org/downloads/release/python-3113/ and edited "....\Pico SDK v1.5.0\pico-env.cmd" to change the path to the new python.exe:

rem call :AddToPath "%PICO_INSTALL_PATH%\python"
call :AddToPath "(something like: ....\AppData\Local\Programs\Python\Python311\)"

Started project generator in GUI mode, made a test project, opened VSCode, opened the directory of the test project and VSCode started working. Error message appeared: _Failed to parse "c:\pico\Test\Testproject1.vscode\c_cppproperties.json": Unexpected token p in JSON at position 4

I can find no obvious errors in this JSON file, but I am not an expert on VSCode settings...

Klicked on CMake icon in the tool bar on the left and built the projekt. Everything ran smoothly with no error message. Project compiled and all the output files were generated.

JamesH65 commented 1 year ago

Looks to be a backslash/forward slash parsing issue on path names. Will take a look

JamesH65 commented 1 year ago

OK, this was actually a backslash escape issue, I think Windows paths need a double backslash in JSON files. So have added this and pushed to this branch for testing.