Skip to content

Commit f2da5ab

Browse files
committed
Add CLion documentation
1 parent 2bd7be8 commit f2da5ab

20 files changed

+190
-17
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ _site/
1010
.bundle/**
1111
!.bundle/config
1212
vendor/
13+
14+
# Ignore IDE directories
15+
.idea/

gettingstarted/CLion.md

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
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+
![Pict8](./resources/CLionPythonDebug8.png)
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+
![Pict9](./resources/CLionPythonDebug9.png)
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+
![Pict10](./resources/CLionPythonDebug10.png)
29+
![Pict11](./resources/CLionPythonDebug11.png)
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+
![Pict1](./resources/CLionPythonDebug1.png)
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+
![Pict2](./resources/CLionPythonDebug2.png)
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+
![Pict1](./resources/CLionPythonDebug6.png)
112+
113+
3. Return to CLion, and start Attach to Process from the run menu:
114+
115+
![Pict1](./resources/CLionPythonDebug3.png)
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+
![Pict1](./resources/CLionPythonDebug4.png)
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+
![Pict1](./resources/CLionPythonDebug5.png)
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+
![Pict1](./resources/CLionPythonDebug6.png)
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+
![Pict1](./resources/CLionPythonDebug7.png)
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.

howtousevscode.md renamed to gettingstarted/VSCode.md

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,29 @@
33

44
A convenient way of working with the FreeCAD source code is to use an IDE or Integrated Development Environment. The FreeCAD repository contains a VSCode configuration which makes it particularly easy to get started with.
55

6-
Get VSCode here https://code.visualstudio.com/download and Git here https://git-scm.com/downloads
6+
Get VSCode here https://code.visualstudio.com/download and Git here https://git-scm.com/downloads
77

8-
After that go to github and create your own fork of the FreeCAD repository by clicking the fork button. Once that's done, clone your fork to your local computer that you will be using for development.
8+
After that go to github and create your own fork of the FreeCAD repository by clicking the fork button. Once that's done, clone your fork to your local computer that you will be using for development.
9+
10+
Note: if you are using VSCode on an Ubuntu based Linux, please avoid using the snap version - it won't work properly with FreeCAD.
911

1012
```
1113
git clone https://github.com/YourUsername/FreeCAD --recurse-submodules
1214
```
1315

1416
Open VSCode and select `File > Open Folder`, then select the folder where you have cloned your fork of the repo.
1517

16-
Be sure to copy the `.vscode` folder inside `/contrib` to the root directory of the repo (`/.vscode`)
18+
Be sure to copy the `.vscode` folder inside `/contrib` to the root directory of the repo (`/.vscode`)
1719

18-
VSCode will probably ask to install the recommended extensions: say `yes`. If it doesn't you'll need to manually install these extensions
20+
VSCode will probably ask to install the recommended extensions: say `yes`. If it doesn't you'll need to manually install these extensions
1921
```
2022
ms-vscode.cpptools-extension-pack
2123
ms-python.python
2224
```
2325

2426
## Select build configuration
2527

26-
<video autoplay muted playsinline loop src="images/configure.mp4" type="video/mp4"></video>
28+
<video autoplay muted playsinline loop src="resources/configure.mp4" type="video/mp4"></video>
2729

2830
First on the bottom left corner, in the status bar, select the build type. Options are:
2931
- debug
@@ -37,7 +39,7 @@ Cmake should automatically configure the project with the selected configuration
3739

3840
## First build
3941

40-
<video autoplay muted playsinline loop src="images/build.mp4" type="video/mp4"></video>
42+
<video autoplay muted playsinline loop src="resources/build.mp4" type="video/mp4"></video>
4143

4244
Press `Ctrl + Shift + B`, or click the build button with the cog icon in the status bar, or run the `CMake: build` task.
4345

@@ -47,26 +49,26 @@ Press `Ctrl + Shift + B`, or click the build button with the cog icon in the sta
4749

4850
## Launching the built executable and debugging
4951

50-
<video autoplay muted playsinline loop src="images/debug.mp4" type="video/mp4"></video>
52+
<video autoplay muted playsinline loop src="resources/debug.mp4" type="video/mp4"></video>
5153

5254
On the left panel go to the debug section and click the little green play button. Alternatively press `F5`. Every time you launch the debugger a build is triggered, to ensure all the latest changes you made to the code are compiled in.
5355

5456
You should see freecad launching and the debugger attaching to the process. If an error pops up the python debugger failed to attach (there's 30 second timeout).
5557
Close FreeCAD and try launching it again. If it still fails try increasing the timeout located in the file `.vscode/scripts/WaitForDebugpy.py`
5658

57-
To debug please refer to the [documentation](https://code.visualstudio.com/docs/editor/debugging#_debug-actions) or search for a tutorial online.
59+
To debug please refer to the [documentation](https://code.visualstudio.com/docs/editor/debugging#_debug-actions) or search for a tutorial online.
5860

59-
**NOTE:** VSCode has an option called 'run without debugging'. This doesn't work, don't use it. If you just want to run the application launch with `F5` and ignore the debugger.
61+
**NOTE:** VSCode has an option called 'run without debugging'. This doesn't work, don't use it. If you just want to run the application launch with `F5` and ignore the debugger.
6062

6163
**NOTE:** There's actually two debuggers: on the top panel you can see a dropdown to select either the c++ debugger or the python one.
6264

6365
**NOTE:** Don't use the restart button. It only restarts the selected debugger, which definitely is not what you want. Instead press the stop button (which stops both debuggers) and launch again.
6466

6567
## Running tests
6668

67-
<video autoplay muted playsinline loop src="images/testing.mp4" type="video/mp4"></video>
69+
<video autoplay muted playsinline loop src="resources/testing.mp4" type="video/mp4"></video>
6870

69-
The IDE supports running FreeCAD tests as well. On the left panel go to the testing section and click the play button. A build takes place and then all the tests are run and results reported.
71+
The IDE supports running FreeCAD tests as well. On the left panel go to the testing section and click the play button. A build takes place and then all the tests are run and results reported.
7072

7173
**NOTE:** This is much slower than running the `Tests_run` executable directly. The video above is sped up.
7274

@@ -78,7 +80,7 @@ Alternatively, there are two vscode tasks that can be used to run the cpp and py
7880

7981
## General features of VSCode
8082

81-
On the left panel you can see the following sections:
83+
On the left panel you can see the following sections:
8284

8385
- File manager: here you can control fils and search them with `Ctrl+F`. Press `F2` to rename the selected file.
8486
- Source control: here you can control git from the graphical user interface.
@@ -101,20 +103,20 @@ General editor useful shortcuts:
101103

102104
Read the documentation for more:
103105

104-
- [basic editing](https://code.visualstudio.com/docs/editor/codebasics)
105-
- [code navigaton](https://code.visualstudio.com/docs/editor/editingevolved)
106+
- [basic editing](https://code.visualstudio.com/docs/editor/codebasics)
107+
- [code navigaton](https://code.visualstudio.com/docs/editor/editingevolved)
106108
- [refactoring](https://code.visualstudio.com/docs/editor/refactoring)
107109

108110
Other useful extensions, recommended but not necessary are
109111
- ```gruntfuggly.todo-tree```: Scans all the files and reports where particular keywords appear. For example `TODO`, `FIXME`, `BUG`...
110112

111-
- ```mhutchie.git-graph```: Helps visualize git commits and branches.
113+
- ```mhutchie.git-graph```: Helps visualize git commits and branches.
112114

113115
- ```donjayamanne.githistory```: Adds two buttons to the right-click menu: `git: view file history` and `git: view line history`
114116

115117

116-
![](images/extensions.png)
118+
![](resources/extensions.png)
117119

118120
- On the very left you can see the `Todo-Tree` extension reporting `TODO`s and `FIXME`
119121
- On the left half you can see the `git graph` extension showing all the commits on different branches. You can click a commit to see exactly what was modified by it.
120-
- On the right half you can see the `git history` of the DocumentObject.cpp file.
122+
- On the right half you can see the `git history` of the DocumentObject.cpp file.

gettingstarted/index.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ that you can run your build system in. For example, on MacOS from the top of a F
7575
- `git branch fixTheThing`
7676
- `git checkout fixTheThing` (or both commands in one go: `git checkout -b fixTheThing`)
7777

78+
## Running and Debugging
79+
80+
- [Visual Studio Code](gettingstarted/VSCode)
81+
- [CLion](gettindstarted/CLion)
82+
7883
## Submitting a PR
7984

8085
The basic process is:
142 KB
Loading
141 KB
Loading
31.3 KB
Loading
162 KB
Loading
352 KB
Loading
213 KB
Loading

0 commit comments

Comments
 (0)