# Selectively build and pack targets

**URL:** https://discourse.cmake.org/t/selectively-build-and-pack-targets/657
**Category:** Usage
**Tags:** gen:makefiles
**Created:** [February 19, 2020, 2:32pm UTC](https://discourse.cmake.org/t/selectively-build-and-pack-targets/657 "2020-02-19T14:32:13Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![jansende](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/7c8e57/32.png) [@jansende](https://discourse.cmake.org/u/jansende)
#### Post date: [February 19, 2020, 2:32pm UTC](https://discourse.cmake.org/t/selectively-build-and-pack-targets/657/1 "2020-02-19T14:32:13Z")

</div>

Hi,  
at our company we encountered the following problem. If we try to package our software using cpack, all targets in the CMakeLists.txt get build before the actual packaging. This however takes a very long time because unit tests and other services, which are not part of the package, get build as well.  
How can we disable this behavior?

Plus, in any case, which of our executable gets part of the package? Only these with install instructions, or everything?

Greetings  
Jan

---

<div class="post-metadata">

### Author: ![kyle.edwards](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/65b543/32.png) [@kyle.edwards](https://discourse.cmake.org/u/kyle.edwards)
#### Post date: [February 19, 2020, 2:37pm UTC](https://discourse.cmake.org/t/selectively-build-and-pack-targets/657/2 "2020-02-19T14:37:11Z")

</div>

You can run `cpack` rather than `make package` or `ninja package`, which will then skip the build.

Alternatively, you can set `EXCLUDE_FROM_ALL` [target property](https://cmake.org/cmake/help/latest/prop_tgt/EXCLUDE_FROM_ALL.html) or [directory property](https://cmake.org/cmake/help/latest/prop_dir/EXCLUDE_FROM_ALL.html) on the targets you don’t want to build.

---

<div class="post-metadata">

### Author: ![jansende](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/7c8e57/32.png) [@jansende](https://discourse.cmake.org/u/jansende)
#### Post date: [February 19, 2020, 3:02pm UTC](https://discourse.cmake.org/t/selectively-build-and-pack-targets/657/3 "2020-02-19T15:02:38Z")

</div>

Okay. Running cpack alone might be an option. However, I cannot run it on multiple cores, can I?

---

<div class="post-metadata">

### Author: ![kyle.edwards](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/65b543/32.png) [@kyle.edwards](https://discourse.cmake.org/u/kyle.edwards)
#### Post date: [February 19, 2020, 3:06pm UTC](https://discourse.cmake.org/t/selectively-build-and-pack-targets/657/4 "2020-02-19T15:06:13Z")

</div>

No, but you can’t when running `make package` either, so you won’t lose any performance benefit by doing that.

---

<div class="post-metadata">

### Author: ![jansende](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/7c8e57/32.png) [@jansende](https://discourse.cmake.org/u/jansende)
#### Post date: [February 19, 2020, 3:16pm UTC](https://discourse.cmake.org/t/selectively-build-and-pack-targets/657/5 "2020-02-19T15:16:55Z")

</div>

`make package -j` works perfectly fine for me… Anyways. I’m not sure if CPack is doing anything as it doesn’t print progress. Maybe it’s building something, but on one core this might take several hours.  
Any other ideas besides `EXCLUDE_FROM_ALL`?

---

<div class="post-metadata">

### Author: ![kyle.edwards](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/65b543/32.png) [@kyle.edwards](https://discourse.cmake.org/u/kyle.edwards)
#### Post date: [February 19, 2020, 3:22pm UTC](https://discourse.cmake.org/t/selectively-build-and-pack-targets/657/6 "2020-02-19T15:22:19Z")

</div>

`make package -j` is performing the _build_ in parallel. The actual _package_ step can’t be parallelized, because it uses the `cmake_install.cmake` file, which runs serially.

---

<div class="post-metadata">

### Author: ![jansende](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/7c8e57/32.png) [@jansende](https://discourse.cmake.org/u/jansende)
#### Post date: [February 19, 2020, 3:41pm UTC](https://discourse.cmake.org/t/selectively-build-and-pack-targets/657/7 "2020-02-19T15:41:02Z")

</div>

Ah okay. So what I would like then is a parallel _build_, but with only the targets which are needed for the package. For example, unit tests should be skipped. The result should then be _packaged_ for release.

---

<div class="post-metadata">

### Author: ![kyle.edwards](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/65b543/32.png) [@kyle.edwards](https://discourse.cmake.org/u/kyle.edwards)
#### Post date: [February 19, 2020, 3:45pm UTC](https://discourse.cmake.org/t/selectively-build-and-pack-targets/657/8 "2020-02-19T15:45:16Z")

</div>

In that case I would run:

```auto
$ make -j10 <the targets you want>
$ cpack .

```

---

<div class="post-metadata">

### Author: ![erk](https://discourse.cmake.org/user_avatar/discourse.cmake.org/erk/32/114_2.png) [@erk](https://discourse.cmake.org/u/erk)
#### Post date: [February 19, 2020, 5:51pm UTC](https://discourse.cmake.org/t/selectively-build-and-pack-targets/657/9 "2020-02-19T17:51:52Z")

</div>

> [@jansende](#):
>
> For example, unit tests should be skipped

then you may add DISABLE\_TESTS option to your main CMakeLists.txt and do something like:

```cmake
if(DISABLE_TESTS)
   message(STATUS "tests disabled")  
else()
   enable_testing()
endif()

```

Then after that, I don’t know the layout of your project but in my usual layout sources for unit tests are in a separate directory so going in ‘tests’ subdir is only done `if(NOT DISABLE_TESTS)` i.e.

```cmake
if(NOT DISABLE_TESTS)
  add_subdirectory(tests)
endif()

```

---

<div class="post-metadata">

### Author: ![erk](https://discourse.cmake.org/user_avatar/discourse.cmake.org/erk/32/114_2.png) [@erk](https://discourse.cmake.org/u/erk)
#### Post date: [February 19, 2020, 5:56pm UTC](https://discourse.cmake.org/t/selectively-build-and-pack-targets/657/10 "2020-02-19T17:56:47Z")

</div>

> [@jansende](#):
>
> Plus, in any case, which of our executable gets part of the package? Only these with install instructions, or everything?

cpack internally runs install before building the package so if any target is not installed it won’t be packaged either. This is usually the case of unit test executable and any generator tool built & used in the build for example.

---

<div class="post-metadata">

### Author: ![jansende](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/7c8e57/32.png) [@jansende](https://discourse.cmake.org/u/jansende)
#### Post date: [February 25, 2020, 2:56pm UTC](https://discourse.cmake.org/t/selectively-build-and-pack-targets/657/11 "2020-02-25T14:56:16Z")

</div>

This is good to hear. But still annoying that everything gets build.

---

<div class="post-metadata">

### Author: ![jansende](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/7c8e57/32.png) [@jansende](https://discourse.cmake.org/u/jansende)
#### Post date: [February 25, 2020, 2:57pm UTC](https://discourse.cmake.org/t/selectively-build-and-pack-targets/657/12 "2020-02-25T14:57:38Z")

</div>

Yes, but cmake should be able to generate these targets…

---

<div class="post-metadata">

### Author: ![mmatrosov](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mmatrosov/32/3572_2.png) [@mmatrosov](https://discourse.cmake.org/u/mmatrosov)
#### Post date: [June 29, 2023, 10:48am UTC](https://discourse.cmake.org/t/selectively-build-and-pack-targets/657/13 "2023-06-29T10:48:23Z")

</div>

> [@kyle.edwards](#):
>
> `the targets you want`

Sounds good, but can cmake/cpack deduce these targets for me? I mostly call `install` with `TARGETS` option, so these dependencies should be clear for cmake/cpack.

---

<div class="post-metadata">

### Author: ![starball](https://discourse.cmake.org/user_avatar/discourse.cmake.org/starball/32/3437_2.png) [@starball](https://discourse.cmake.org/u/starball)
#### Post date: [June 29, 2023, 10:34pm UTC](https://discourse.cmake.org/t/selectively-build-and-pack-targets/657/14 "2023-06-29T22:34:46Z")

</div>

Related on Stack Overflow: [Build only necessary targets for CPack](https://stackoverflow.com/q/76580952/11107541)
