nholthaus / units

a compile-time, header-only, dimensional analysis and unit conversion library built on c++14 with no dependencies.
http://nholthaus.github.io/units/
MIT License
938 stars 134 forks source link

Add MAIN_PROJECT check for test option #269

Closed globberwops closed 1 year ago

globberwops commented 3 years ago

When adding units to a CMake project using FetchContent, testing has to be explicitly disabled. This check only enables testing, if units is the main project being built.

The ALIAS target makes sure, that units can be linked as units::units not only from the install tree, but from the build tree as well.

Both of these changes enable the cleaner usage of units with FetchContent.

Before

FetchContent_Declare(
  nholthaus_units
  GIT_REPOSITORY https://github.com/nholthaus/units.git
  GIT_TAG master)
set(BUILD_TESTS OFF)
FetchContent_MakeAvailable(nholthaus_units)
if(NOT TARGET units::units)
  add_library(units::units ALIAS units)
endif()

After

FetchContent_Declare(
  nholthaus_units
  GIT_REPOSITORY https://github.com/nholthaus/units.git
  GIT_TAG master)
FetchContent_MakeAvailable(nholthaus_units)