Git commit 1d9b2355c10db4d8825cefe06c73b07d0b4f4373 by Andrew Shark, on behalf of Kristen McWilliam. Committed on 02/12/2023 at 17:16. Pushed by ashark into branch 'master'.
Add "generate-vscode-project-config" option This change adds `generate-vscode-project-config` option for `kdesrc-buildrc` and the corresponding command line flag. When set to `true`, kdesrc-build will create the `.vscode` folder in the module source directory with configuration that makes VSCode to work properly with KDE projects, such as setting the correct build directory & enabling support for C++, CMake, LSP & IntelliSense. The process specifically checks for an existing `.vscode` folder, in case the user already has custom settings present - and only proceeds if it does not exist yet. Closes #84 M +2 -0 completions/zsh/_kdesrc-build M +4 -0 data/kdesrc-buildrc.in A +17 -0 data/vscode/c_cpp_properties.json.in A +9 -0 data/vscode/extensions.json.in A +22 -0 data/vscode/launch.json.in A +15 -0 data/vscode/settings.json.in M +35 -0 doc/index.docbook M +16 -15 modules/ksb/BuildContext.pm M +98 -0 modules/ksb/BuildSystem/KDECMake.pm M +1 -0 modules/ksb/FirstRun.pm https://invent.kde.org/sdk/kdesrc-build/-/commit/1d9b2355c10db4d8825cefe06c73b07d0b4f4373 diff --git a/completions/zsh/_kdesrc-build b/completions/zsh/_kdesrc-build index f9b3d7ec..ca79d62e 100644 --- a/completions/zsh/_kdesrc-build +++ b/completions/zsh/_kdesrc-build @@ -39,5 +39,7 @@ _arguments \ '--rebuild-failures[Only those modules which failed to build on a previous run.]' \ '--force-build[Disable skipping the build process.]' \ '--resume[Resum after a build failure]' \ + '--generate-vscode-project-config[Generate a vscode project config]' \ + '(--generate-vscode-project-config --no-generate-vscode-project-config)'{--generate-vscode-project-config,--no-generate-vscode-project-config}'[Generate a vscode project config]' \ \ '*:: :_kdesrc-build_modules' diff --git a/data/kdesrc-buildrc.in b/data/kdesrc-buildrc.in index d3fb6171..cec49d4f 100644 --- a/data/kdesrc-buildrc.in +++ b/data/kdesrc-buildrc.in @@ -58,6 +58,10 @@ global # Build with LSP support for everything that supports it compile-commands-linking %{compile-commands-linking} compile-commands-export %{compile-commands-export} + + # Generate .vscode config files in project directories + # Enable this if you want to use Visual Studio Code for development + generate-vscode-project-config %{generate-vscode-project-config} end global # With base options set, the remainder of the file is used to define modules to build, in the diff --git a/data/vscode/c_cpp_properties.json.in b/data/vscode/c_cpp_properties.json.in new file mode 100644 index 00000000..92b17fad --- /dev/null +++ b/data/vscode/c_cpp_properties.json.in @@ -0,0 +1,17 @@ +{ + "configurations": [ + { + "name": "Linux", + "includePath": [ + "${workspaceFolder}/**" + ], + "defines": [], + "compilerPath": "/usr/bin/gcc", + "cStandard": "c17", + "cppStandard": "c++17", + "intelliSenseMode": "${default}", + "compileCommands": "${workspaceFolder}/compile_commands.json" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/data/vscode/extensions.json.in b/data/vscode/extensions.json.in new file mode 100644 index 00000000..07ce6504 --- /dev/null +++ b/data/vscode/extensions.json.in @@ -0,0 +1,9 @@ +{ + "recommendations": [ + "ms-vscode.cpptools-extension-pack", // C/C++ debugging + "llvm-vs-code-extensions.vscode-clangd", // C/C++ LSP + "tonka3000.qtvsctools", // Qt + "bbenoist.QML", // QML + "deerawan.vscode-dash", // Docs + ] +} \ No newline at end of file diff --git a/data/vscode/launch.json.in b/data/vscode/launch.json.in new file mode 100644 index 00000000..02ea5709 --- /dev/null +++ b/data/vscode/launch.json.in @@ -0,0 +1,22 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "Debug", + "type": "cppdbg", + "request": "launch", + "program": "${command:cmake.launchTargetPath}", + "args": [], + "stopAtEntry": false, + "cwd": "${workspaceFolder}", + "MIMode": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/data/vscode/settings.json.in b/data/vscode/settings.json.in new file mode 100644 index 00000000..743e6243 --- /dev/null +++ b/data/vscode/settings.json.in @@ -0,0 +1,15 @@ +{ + "cmake.buildDirectory": "$buildDir", + "cmake.environment": { + "CMAKE_PREFIX_PATH": "$installDir:${env:CMAKE_PREFIX_PATH}", + "MANPATH": "$installDir/share/man:${env:MANPATH}", + "PATH": "$installDir/bin:${env:PATH}", + "PKG_CONFIG_PATH": "$installDir/$libDir/pkgconfig:${env:PKG_CONFIG_PATH}", + "PYTHONPATH": "$installDir/$libDir/site-packages:${env:PYTHONPATH}", + "QML2_IMPORT_PATH": "$installDir/$libDir/qml:${env:QML2_IMPORT_PATH}", + "QT_PLUGIN_PATH": "$installDir/$libDir/plugins:${env:QT_PLUGIN_PATH}", + "QT_QUICK_CONTROLS_STYLE_PATH": "$installDir/$libDir/qml/QtQuick/Controls.2/:${env:QT_QUICK_CONTROLS_STYLE_PATH}", + "XDG_DATA_DIRS": "$installDir/share:${env:XDG_DATA_DIRS}", + "XDG_CONFIG_DIRS": "$installDir/etc/xdg:${env:XDG_CONFIG_DIRS}" + } +} \ No newline at end of file diff --git a/doc/index.docbook b/doc/index.docbook index 8fce177a..cd8e9043 100644 --- a/doc/index.docbook +++ b/doc/index.docbook @@ -2541,6 +2541,33 @@ due to changes in the project metadata.</para> </entry> </row> +<row id="conf-generate-vscode-project-config"> +<entry>generate-vscode-project-config</entry> + +<entry> +<simplelist type='horiz' columns='2'> +<member>Type</member><member>Boolean</member> +<member>Default value</member><member>False</member> +</simplelist> + +<para>Module setting overrides global</para> + +<para>Set this option to <userinput>true</userinput> to make +&kdesrc-build; create VS Code project files (.vscode directory) in the module +source directory.</para> + +<para>The .vscode folder will be created in the project source directory, only +if it does not already exist. The configurations will enable the use of LSP, +building, debugging, and running the project from within VS Code.</para> + +<para>The configuration also recommends extensions to install that are useful +for working on most KDE projects.</para> + +<para>You can also use the <link linkend="cmdline-generate-vscode-project-config"> +<option>--generate-vscode-project-config</option></link> command line flag.</para> +</entry> +</row> + <row id="conf-include-dependencies"> <entry>include-dependencies</entry> <entry> @@ -3283,6 +3310,14 @@ Enable colorful output. (This is the default for interactive terminals). </para></listitem> </varlistentry> +<varlistentry id="cmdline-generate-vscode-project-config"> +<term><parameter>--generate-vscode-project-config</parameter></term> +<listitem><para> +Generate a .vscode directory with configurations for building and debugging +in Visual Studio Code. +</para></listitem> +</varlistentry> + <varlistentry id="cmdline-nice"> <term><parameter>--nice=<replaceable>value</replaceable></parameter></term> <listitem><para> diff --git a/modules/ksb/BuildContext.pm b/modules/ksb/BuildContext.pm index f565d17d..c2b8a67c 100644 --- a/modules/ksb/BuildContext.pm +++ b/modules/ksb/BuildContext.pm @@ -124,21 +124,22 @@ my %internalGlobalOptions = ( # ksb::Cmdline to GetOptionsFromArray! This is now checked at runtime so # if you forget the test suite should catch you. our %defaultGlobalFlags = ( - "delete-my-patches" => 0, # Should only be set from cmdline - "delete-my-settings" => 0, # Should only be set from cmdline - "disable-agent-check" => 0, # If true we don't check on ssh-agent - "compile-commands-export" => 1, # 2021-02-06 allow to generate compile_commands.json via cmake, for clangd tooling - "compile-commands-linking" => 0, # 2021-02-06 link generated compile_commands.json back to the source directory - "include-dependencies" => 1, - "install-after-build" => 1, - "install-environment-driver" => 1, # Setup ~/.config/kde-env-*.sh for login scripts - "install-session-driver" => 0, # Above, + ~/.xsession - "purge-old-logs" => 1, - "run-tests" => 0, # 1 = make test, upload = make Experimental - "stop-on-failure" => 1, - "use-clean-install" => 0, - "use-idle-io-priority" => 0, - "use-inactive-modules" => 0, + "delete-my-patches" => 0, # Should only be set from cmdline + "delete-my-settings" => 0, # Should only be set from cmdline + "disable-agent-check" => 0, # If true we don't check on ssh-agent + "compile-commands-export" => 1, # 2021-02-06 allow to generate compile_commands.json via cmake, for clangd tooling + "compile-commands-linking" => 0, # 2021-02-06 link generated compile_commands.json back to the source directory + "generate-vscode-project-config" => 0, + "include-dependencies" => 1, + "install-after-build" => 1, + "install-environment-driver" => 1, # Setup ~/.config/kde-env-*.sh for login scripts + "install-session-driver" => 0, # Above, + ~/.xsession + "purge-old-logs" => 1, + "run-tests" => 0, # 1 = make test, upload = make Experimental + "stop-on-failure" => 1, + "use-clean-install" => 0, + "use-idle-io-priority" => 0, + "use-inactive-modules" => 0, ); # Holds other cmdline-accessible options that aren't simply binary flags. diff --git a/modules/ksb/BuildSystem/KDECMake.pm b/modules/ksb/BuildSystem/KDECMake.pm index 0603acf6..50faa1b8 100644 --- a/modules/ksb/BuildSystem/KDECMake.pm +++ b/modules/ksb/BuildSystem/KDECMake.pm @@ -372,6 +372,12 @@ sub configureInternal my $self = assert_isa(shift, 'ksb::BuildSystem::KDECMake'); my $module = $self->module(); + if ($module->getOption('generate-vscode-project-config')) { + generateVSCodeConfig($module); + } else { + debug ("\tGenerating .vscode directory - disabled for this module"); + } + # Use cmake to create the build directory (sh script return value # semantics). if ($self->_safe_run_cmake()) @@ -391,6 +397,98 @@ sub configureInternal return 1; } +# Generate default config files for VSCode. +# +# This populates the settings VSCode needs to work with most KDE projects, +# such as C++ support, correct build directory, and LSP / IntelliSense. +sub generateVSCodeConfig +{ + if (pretending()) { + pretend ("\tWould have generated .vscode directory"); + return; + } + + my $module = shift; + my $projectName = $module->name(); + my $buildDir = $module->fullpath('build'); + my $srcDir = $module->fullpath('source'); + my $installDir = $module->installationPath(); + my $libDir = $module->getOption('libname'); + my $configDir = "$srcDir/.vscode"; + + if (-e $configDir) { + if (-d $configDir) { + debug ("\tGenerating .vscode directory - skipping as it already exists"); + } elsif (-f $configDir) { + error ("\tGenerating .vscode directory - cannot proceed, file .vscode exists"); + } + return; + } else { + debug ("\tGenerating .vscode directory for $projectName: $configDir"); + } + + mkdir($configDir); + + use FindBin; + my $baseDir = $FindBin::RealBin; + my $dataDir = "$baseDir/data/vscode"; + + # c_cpp_properties.json configures C++, CMake & IntelliSense. + my $cCppPropertiesJson = _readFile("$dataDir/c_cpp_properties.json.in"); + + # settings.json configures the paths for CMake, QML, Qt, etc. + my $settingsJson = _readFile("$dataDir/settings.json.in"); + $settingsJson =~ s/\$buildDir/$buildDir/g; + $settingsJson =~ s/\$installDir/$installDir/g; + $settingsJson =~ s/\$libDir/$libDir/g; + + # extensions.json recommends extensions to install/enable. + my $extensionsJson = _readFile("$dataDir/extensions.json.in"); + + # launch.json configures the run with debugger functionality. + my $launchJson = _readFile("$dataDir/launch.json.in"); + + _writeToFile("$configDir/c_cpp_properties.json", $cCppPropertiesJson); + _writeToFile("$configDir/settings.json", $settingsJson); + _writeToFile("$configDir/extensions.json", $extensionsJson); + _writeToFile("$configDir/launch.json", $launchJson); + + return 1; +} + +# Reads the contents of a file. +# +# Arguments: +# $file_path: The path to the file to read. +# +# Returns: The contents of the file as a string. +sub _readFile +{ + my ($file_path) = @_; + + open my $file, '<', $file_path or warning("\tCouldn't open $file_path: $!"); + my $content = do { local $/; <$file> }; + close $file; + + return $content; +} + +# Writes content to a file. +# +# Arguments: +# $file_path: The path to the file to write to. +# $content: The content to write to the file. +# +# Returns: Nothing. +sub _writeToFile +{ + my ($file_path, $content) = @_; + + open my $file, '>', $file_path or warning("\tCouldn't open $file_path: $!"); + print $file $content or warning("\tCouldn't write to $file_path: $!"); + close $file or warning("\tError closing $file_path: $!"); +} + # Return value style: hashref to build results object (see ksb::BuildSystem::safe_make) sub buildInternal { diff --git a/modules/ksb/FirstRun.pm b/modules/ksb/FirstRun.pm index e8f4ba49..e9f2ffd6 100644 --- a/modules/ksb/FirstRun.pm +++ b/modules/ksb/FirstRun.pm @@ -307,6 +307,7 @@ DONE $sampleRc =~ s/%\{stop-on-failure}/$gl->{"stop-on-failure"}/g; $sampleRc =~ s/%\{compile-commands-linking}/$gl->{"compile-commands-linking"}/g; $sampleRc =~ s/%\{compile-commands-export}/$gl->{"compile-commands-export"}/g; + $sampleRc =~ s/%\{generate-vscode-project-config}/$gl->{"generate-vscode-project-config"}/g; make_path($xdgConfigHome);