|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +--- |
| 4 | + |
| 5 | +# Getting Started |
| 6 | + |
| 7 | +If you don't already have CLion installed, download it, license it, install it per JetBrains instructions. |
| 8 | + |
| 9 | +### Checking out FreeCAD |
| 10 | + |
| 11 | +**Command line:** `git clone https://github.com/FreeCAD/FreeCAD` and then File -> Open or New Project on that directory. |
| 12 | + |
| 13 | +**IDE:** From upper left, choose Project from Version Control. Use https://github.com/FreeCAD/FreeCAD |
| 14 | + |
| 15 | +### Building CMake |
| 16 | + |
| 17 | +CLion should automatically refresh the CMake build for the application. You see results or rerun using the cmake |
| 18 | +selection at the lower right |
| 19 | + |
| 20 | + |
| 21 | +### Building Application |
| 22 | + |
| 23 | +Choose a configuration ( Debug ) and a target ( FreeCADMain ) from the top bar. First time through, use the menus to |
| 24 | +run 'Build All in Debug' from the Build Menu. After that point you can generally just Run or Debug and it will build |
| 25 | +any dependencies for you. Note that if you switch branches you may sometimes need to "Rebuild All in Debug" to force |
| 26 | +a clean full rebuild. |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | +### Running Application |
| 32 | + |
| 33 | +Use the Run or Debug Icons from the top bar shown in the prior image. |
| 34 | + |
| 35 | +## Debugging |
| 36 | + |
| 37 | +*note* This depends on https://github.com/FreeCAD/FreeCAD/pull/16850 getting merged! |
| 38 | + |
| 39 | +While CLion supports both a C++ debugger and a Python debugger, it generally does not support |
| 40 | +embedded Python interpreters, nor debugging both simultaneously. However, there are workaround |
| 41 | +procedures that will allow this. |
| 42 | + |
| 43 | +### Basic C++ concept |
| 44 | + |
| 45 | +CLion invokes an underlying debugger that launches or connects to the program under |
| 46 | +test. You can see this in the "Attach to Process" dialog under the Run menu. After |
| 47 | +choosing a process, you will be asked to attach with GDB or LLDB; CLion uses the |
| 48 | +appropriate API to communicate with the debugger, which uses OS specific mechanisms |
| 49 | +to control the program under test. |
| 50 | + |
| 51 | +### Basic Python concept |
| 52 | + |
| 53 | +The pydevd debugger is run inside of the python interpreter under test. This provides |
| 54 | +a network socket, normally connected _from_ the interpreter under test to a listening |
| 55 | +socket in the controlling debugger UI. This port defaults to 5678 for non-CLion uses. CLion |
| 56 | +generates a random port number, and then passes this into the pydevd initialization code; |
| 57 | +if you run a python program then this is accomplished by directly running a shim with |
| 58 | +parameters that include the port number and the program to be debugged. In an attach |
| 59 | +scenario, CLion _launches a C++ debugger_ to inject the pydevd initialization into the |
| 60 | +running python interpreter and cause it to make the socket connection back to the UI. Note |
| 61 | +that since you generally cannot have more than one c++ debugger attached to a program, |
| 62 | +this mechanism cannot work for a program already under c++ debugging, and since it assumes |
| 63 | +that the c++ program is in fact a python process, it won't work for embedded python either. |
| 64 | + |
| 65 | +### Technique for getting Python testing working |
| 66 | + |
| 67 | +The challenge here is to get CLion to listen on a port for the incoming connection from pydevd, |
| 68 | +and to execute pydevd on that port in the program under test. Since CLion does not support this |
| 69 | +natively, we trick it. To do this we need to have a pydevd installed in the python interpreter in |
| 70 | +FreeCAD. |
| 71 | + |
| 72 | +### Running a basic c++ debug |
| 73 | + |
| 74 | +Simply choose the debug option to start FreeCAD, and you're good to go. |
| 75 | + |
| 76 | +### Setting up for python debugging in CLion |
| 77 | + |
| 78 | +Install pydevd in the interpreter that FreeCAD is using. 'pip3 install pydevd' from the correct |
| 79 | +venv is one way to accomplish this. |
| 80 | + |
| 81 | +Use the settings button in the upper right, the File Menu -> Settings, or the Ctrl-Alt-S shortcut |
| 82 | +to open the settings dialog, expand the Build, Execution, Deployment tree item, and select a Python |
| 83 | +Interpreter: |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | +Switch to the Python Debugger settings, and change the Attach To Process name from python to 'Freecad' |
| 88 | +You may need to change this back if you use the IDE for other applications. |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | +Now there are three approaches here: |
| 93 | +1. Wait for CLion 2024.3 which is supposed to have a static port listener option with a |
| 94 | +configurable port available in the 'Python Debugger' pane of the CMake profile you use. |
| 95 | + [see context](https://youtrack.jetbrains.com/issue/CPP-5797/Cross-debugging-Python-and-C#focus=Comments-27-10655987.0-0) |
| 96 | + and [see planned fix.](https://youtrack.jetbrains.com/issue/PY-21325/Add-an-ability-to-specify-debugger-port) Port should be 5679 if you use the macro below. |
| 97 | +2. Find the attach_pydevd.py file in your CLion installation, and modify it using the |
| 98 | +contrib/clion/attach_pydevd.py.patch in the source code. Then use the macro below. |
| 99 | +3. Use this elaborate work around: |
| 100 | + |
| 101 | +### Running a python or simultaneous debug using the non intrusive procedure |
| 102 | + |
| 103 | +1. You can start your FreeCAD independently, via CLion, or in c++ debug mode under CLion. |
| 104 | + |
| 105 | +2. Regardless of the starting technique, once FreeCAD is up, if there is no code in your FreeCAD supporting |
| 106 | + CLion debugging, then: |
| 107 | + 1. Go to the python console and import pydevd. |
| 108 | + Then go ahead and pretype in the settrace call, leaving the cursor poised to enter a port name but not executing the line. |
| 109 | + 2. Alternatively, go to the Macro -> Attach to remote debugger dialog and switch to the CLion tab. |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | +3. Return to CLion, and start Attach to Process from the run menu: |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | +4. Find the Freecad process, which may be well down at the bottom of the list. It is the configuration |
| 118 | +from earlier that makes Freecad processes appear in the list as Python debuggable. Highlight this entry |
| 119 | +and press the Attach with Python Debugger button. |
| 120 | + |
| 121 | + |
| 122 | + |
| 123 | +5. Now it's time to move FAST. You have about 10 to 15 seconds to complete the following: |
| 124 | +Scroll to the top of the console window and find the (random) port number that CLion has chosen to listen |
| 125 | +on. Ignore everything else: |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | +6. Switch to the FreeCAD window and complete the port number in either the console's settrace line or the attach dialog |
| 130 | +and execute it: |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | +7. Note the appearance of a "Connected to pydev debugger" line in the CLion console. Shortly before or after that |
| 135 | +message, you will also see the timeout of CLion's attempt to use a debugger to trigger an attach. Ignore this |
| 136 | +error. |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | +8. Proceed to debug normally: any existing breakpoints you have either in c++ code or python code will be hit, you |
| 141 | +can step, examine variables, the whole deal. |
| 142 | + |
| 143 | +### Running a python or simultaneous debug using updated CLion or the patched attach_pydevd.py |
| 144 | + |
| 145 | +1. Start FreeCAD running. You can do this from any place for just a python debug; or you can launch a c++ debug |
| 146 | +in CLion for simultaneous debugging. |
| 147 | +2. Run the contrib/clion/GetCLionDebugPort.FCMacro in FreeCAD. The cleanest way to do this is just to add it as |
| 148 | +first parameter of the FreeCAD or FreeCADCmd command line. Editing your FreeCADMain run/debug configuration in |
| 149 | +CLion is the best way to do this - set it once, and python debugging is always there whether you run a c++ debug |
| 150 | +or not, with no effective performance penalty. |
| 151 | + |
| 152 | +#### Caveats |
| 153 | + |
| 154 | +- Sometimes you may get one spurious breakpoint hit in the python debugger. Just hit the continue run button to keep |
| 155 | +going. FreeCAD triggers a python exception during startup, and the default CLion debugger settings are to break on this |
| 156 | +- condition. |
| 157 | +- The python files are copied from the source into the build directory. You must use these build directory versions |
| 158 | +of the python files if you want to set breakpoints. So if you're built to `cmake-build-debug` for instance, then you |
| 159 | +- want `cmake-debug-build/Mod/BIM/Arch.py` and not `src/Mod/BIM/Arch.py`. This also means that you need to be careful |
| 160 | +when editing files - anything in the build directory is essentially temporary and will be overwritten on the next build, |
| 161 | +but if you change something in the source directory then you need to run a build on an appropriate target to copy the |
| 162 | +changed python file to the build directory. CLion does not support specifying mapping directories for this python code, |
| 163 | +and there is no evidence of it changing soon. |
0 commit comments