Using CMake compile flags in preset for non-external projects only

1.5k Views Asked by At

I am adding a few external projects to my application. For instance, gtest.

In my CMake preset I set the following...

{
    "version": 4,
    "cmakeMinimumRequired": {
      "major": 3,
      "minor": 23,
      "patch": 0
    },
    "configurePresets": [
      {
        "name": "debug",
        "displayName": "debug",
        "binaryDir": "${sourceDir}/build",
        "cacheVariables": {
          "CMAKE_CXX_FLAGS": "/W4 /Wall /EHsc",
          "CMAKE_CXX_STANDARD": "20",
          "CMAKE_CXX_STANDARD_REQUIRED": "YES",
          "CMAKE_CXX_EXTENSIONS": "OFF"
        }
      }
    ]
  }

When I build with the above preset, I get a bunch of warnings from building gtest. I only would like to see the warnings coming from my internal build, not external projects.

In my root CMakeLists.txt I have the following...

cmake_minimum_required(VERSION 3.23.0)

project(ProjectName LANGUAGES C CXX CUDA)
include(CTest)
add_subdirectory(external) # This has a bunch of external dependencies.
add_subdirectory(src) # This builds a normal executable.
add_subdirectory(tests) # This has various unit tests.

Is there a way for me to make sure the flags are only used for my personal project, and nothing external?

I have looked into "https://cmake.org/cmake/help/v3.23/manual/cmake-presets.7.html" but nothing stood out to me.

Thank you

1

There are 1 best solutions below

1
On BEST ANSWER

If you want to localize your settings you should not use globals. Use target_compile_options and other target_* commands instead of setting CMAKE_CXX_* global (cache) variables in your preset.

Alternatively, you can choose not to build external projects as part of your local project build. With that organization you wouldn't have the problem in the first place.