# Idiomatic way to handle packages and add\_subdirectory?

**URL:** https://discourse.cmake.org/t/idiomatic-way-to-handle-packages-and-add-subdirectory/8400
**Category:** Usage
**Created:** [June 23, 2023, 6:05pm UTC](https://discourse.cmake.org/t/idiomatic-way-to-handle-packages-and-add-subdirectory/8400 "2023-06-23T18:05:03Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![buildSystemPerson](https://discourse.cmake.org/user_avatar/discourse.cmake.org/buildsystemperson/32/2851_2.png) [@buildSystemPerson](https://discourse.cmake.org/u/buildSystemPerson)
#### Post date: [June 23, 2023, 6:05pm UTC](https://discourse.cmake.org/t/idiomatic-way-to-handle-packages-and-add-subdirectory/8400/1 "2023-06-23T18:05:03Z")

</div>

Currently I work on a project where users want both `add_subdirectory` and `find_package`. We try and support both approaches.

However, it results in having code that looks like this.

```cmake
if (NOT TARGET Vulkan::Headers)
    find_package(VulkanHeaders CONFIG REQUIRED QUIET)
endif()

```

Is there a better way?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [June 25, 2023, 8:08pm UTC](https://discourse.cmake.org/t/idiomatic-way-to-handle-packages-and-add-subdirectory/8400/2 "2023-06-25T20:08:36Z")

</div>

I think it’s better to be more proactive about these kinds of things. Have a variable about whether to use external copies or not and do that. These fallbacks only make weird states when things go as one isn’t expecting and it looks like it’s working when it’s really getting the wrong thing.

```cmake
option(use_external_vulkan "blah" ${use_external_default})
if (use_external_vulkan)
  find_package(VulkanHeaders CONFIG)
  if (NOT VulkanHeaders_FOUND)
    message(FATAL_ERROR
      "External Vulkan requested, but not found: ${VulkanHeaders_NOT_FOUND_MESSAGE}")
  endif ()
else ()
  add_subdirectory(vulkan)
endif ()
# TODO: set up an INTERFACE target to patch over differences

```
