diff --git a/.gitignore b/.gitignore index 1b76f11d..afde9293 100644 --- a/.gitignore +++ b/.gitignore @@ -5,7 +5,11 @@ *.so *.dll *.exe -.vscode +.vscode/ + +# clangd files +compile_commands.json +.cache/ # vim swap files *.sw? diff --git a/README.md b/README.md index e8ad31ee..5f11dafe 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,8 @@ - [Building](#building) - [Building a Libretro core](#building-a-libretro-core) - [Visual Studio Support](#visual-studio-support) + - [Visual Studio Code Support](#visual-studio-code-support) + - [Optional: clangd](#optional-clangd) - [Dependencies](#dependencies) - [Cross compiling for 32 bit on a 64 bit host](#cross-compiling-for-32-bit-on-a-64-bit-host) - [Cross Compiling for Win32](#cross-compiling-for-win32) @@ -109,6 +111,37 @@ cmake .. -DVCPKG_TARGET_TRIPLET=x64-windows-static -DCMAKE_BUILD_TYPE=Debug -G N ninja ``` +## Visual Studio Code Support + +On most platforms, Visual Studio Code should work as-is, as long as the +[CMake Tools extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cmake-tools) +is installed. + +There is a recommended configuration in the `vscode/settings.json` file. To use +it, copy the file to a `.vscode/` folder. + +By default, this will publish builds in the `build-vscode/` directory. In the +`vscode/settings.json` file, there is an alternate configuration for the +`"cmake.buildDirectory"` option that will use different build directories for +different toolchains and build configurations. + +### Optional: clangd + +The [clangd extension](https://marketplace.visualstudio.com/items?itemName=llvm-vs-code-extensions.vscode-clangd) +uses clangd to provide powerful code completion, errors and warnings and +references on click in VS Code. + +With the recommended configuration, the build configuration will generate a +`compile_commands.json` file that can be used with clangd. After configuration, +you can copy that file to the root directory with a command similar to this one: + +```shell +cp build/build-vscode/compile_commands.json . +``` + +Then, select "clangd: Restart language server" from the command palette to get +completion in the IDE. + ## Dependencies If your OS is not supported, you will need the following: diff --git a/cmake-variants.yaml b/cmake-variants.yaml new file mode 100644 index 00000000..aac32666 --- /dev/null +++ b/cmake-variants.yaml @@ -0,0 +1,23 @@ +buildType: + default: debug + choices: + debug: + short: Debug + long: Emit debug information + buildType: Debug + release: + short: Release + long: Optimize generated code + buildType: Release + +linkage: + default: static + choices: + static: + short: Static + long: Create static libraries + linkage: static + shared: + short: Shared + long: Create shared libraries/DLLs + linkage: shared diff --git a/cmake/Set-Toolchain-vcpkg.cmake b/cmake/Set-Toolchain-vcpkg.cmake index 181a7f1a..3b65195e 100644 --- a/cmake/Set-Toolchain-vcpkg.cmake +++ b/cmake/Set-Toolchain-vcpkg.cmake @@ -3,7 +3,49 @@ if(POLICY CMP0012) endif() if(NOT DEFINED VCPKG_TARGET_TRIPLET) - return() + # Check if we are in an MSVC environment. + if($ENV{CXX} MATCHES "cl.exe$") + # Infer the architecture from the LIB folders. + foreach(LIB $ENV{LIB}) + if(${LIB} MATCHES "x64$") + set(VBAM_VCPKG_PLATFORM "x64-windows") + break() + endif() + if(${LIB} MATCHES "x86$") + set(VBAM_VCPKG_PLATFORM "x86-windows") + break() + endif() + if(${LIB} MATCHES "ARM64$") + set(VBAM_VCPKG_PLATFORM "arm64-windows") + break() + endif() + endforeach() + + # If all else fails, try to use a sensible default. + if(NOT DEFINED VBAM_VCPKG_PLATFORM) + set(VBAM_VCPKG_PLATFORM "x64-windows") + endif() + + elseif (NOT DEFINED CMAKE_CXX_COMPILER) + # No way to infer the compiler. + return() + + elseif(${CMAKE_CXX_COMPILER} MATCHES "clang-cl.exe$" OR ${CMAKE_CXX_COMPILER} MATCHES "clang-cl$") + # For stand-alone clang-cl, assume x64. + set(VBAM_VCPKG_PLATFORM "x64-windows") + endif() + + if (NOT DEFINED VBAM_VCPKG_PLATFORM) + # Probably not an MSVC environment. + return() + endif() + + if(DEFINED BUILD_SHARED_LIBS AND NOT ${BUILD_SHARED_LIBS}) + set(VBAM_VCPKG_PLATFORM ${VBAM_VCPKG_PLATFORM}-static) + endif() + + set(VCPKG_TARGET_TRIPLET ${VBAM_VCPKG_PLATFORM} CACHE STRING "Vcpkg target triplet (ex. x86-windows)" FORCE) + message(STATUS "Inferred VCPKG_TARGET_TRIPLET=${VCPKG_TARGET_TRIPLET}") endif() function(vcpkg_seconds) diff --git a/vscode/settings.json b/vscode/settings.json new file mode 100644 index 00000000..893c2f78 --- /dev/null +++ b/vscode/settings.json @@ -0,0 +1,9 @@ +{ + "cmake.generator": "Ninja", + "cmake.configureSettings": { + "CMAKE_EXPORT_COMPILE_COMMANDS": "ON", + }, + "cmake.buildDirectory": "${workspaceRoot}/build-vscode", + // To use separate directories for different configurations: + // "cmake.buildDirectory": "${workspaceRoot}/build/${buildKitTriple}-${buildKitVersion}/${variant:buildType}-${variant:linkage}", +} \ No newline at end of file