Can I make two separate library packages in one CMakelIsts.txt file?

I am currently creating one library built from two source files. I want to generate both shared library and static library. The static libraries and headers only get loaded when loading a dev version of the package.

set(QW_WEATHER_DEVICES_LIB weatherdevices)

add_library(${QW_WEATHER_DEVICES_LIB} SHARED
  anemometer_adafruit.cpp
  ecowitt_ln90lp.cpp
)

# Also make a static library for dev package
add_library(${QW_WEATHER_DEVICES_LIB}_static STATIC
  anemometer_adafruit.cpp
  ecowitt_ln90lp.cpp
)

install( FILES
    include/anonemometer_adafruit.h
    include/ecowitt_ln90lp.h
  DESTINATION include/qw/devices
  COMPONENT dev
)
# Install static library for dev package
install(
  TARGETS ${QW_WEATHER_DEVICES_LIB}_static
  DESTINATION lib/qw/devices/i2c/lib/lib${QW_WEATHER_DEVICES_LIB}.a
  COMPONENT dev
)
# Install shared library for runtime package
install(
  TARGETS ${QW_WEATHER_DEVICES_LIB}
  DESTINATION lib/qw/devices/lib
  COMPONENT runtime
  )

But after thinking about it these two source files are really mutually exclusive of each other. Essentially, if you wanted one you would not want the other. So, I would like to separate these into two separate libraries with separate install and packaging rules for each.

Can I do that within this same CMakeLists.txt or do I need to move each one into it’s own sub-directory?

I think I can make the separate libraries

set(QW_WEATHER_DEVICES_LIB weatherdevices)

add_library(${QW_WEATHER_DEVICES_LIB} SHARED
  anemometer_adafruit.cpp
)

# Also make a static library for dev package
add_library(${QW_WEATHER_DEVICES_LIB}_static STATIC
  anemometer_adafruit.cpp
)

set(QW_ECOWITT_LN90LP_LIB ecowittlp90lndevice)

add_library(${QW_ECOWITT_LN90LP_LIB} SHARED
  ecowitt_ln90lp.cpp
)

# Also make a static library for dev package
add_library(${QW_ECOWITT_LN90LP_LIB}_static STATIC
  ecowitt_ln90lp.cpp
)

But I am not sure how to get these libraries into separate runtime and dev packages.

Any pointers appreciated.

Thanks

Chris

OK I think I have a solution. I used the COMPONENTS to have two separate packages made. I separated out and created two libraries each with shard and static:

set(QW_WEATHER_DEVICES_LIB weatherdevices)

add_library(${QW_WEATHER_DEVICES_LIB} SHARED
  anemometer_adafruit.cpp
)

# Also make a static library for dev package
add_library(${QW_WEATHER_DEVICES_LIB}_static STATIC
  anemometer_adafruit.cpp
)

set(QW_ECOWITT_LN90LP_DEVICE_LIB ecowittln90lp)

add_library(${QW_ECOWITT_LN90LP_DEVICE_LIB} SHARED
  ecowitt_ln90lp.cpp
)

# Also make a static library for dev package
add_library(${QW_ECOWITT_LN90LP_DEVICE_LIB}_static STATIC
  ecowitt_ln90lp.cpp
)


Then made different install rules with different components:

install( FILES
    include/anemometer_adafruit.h
  DESTINATION include/qw/devices
  COMPONENT weatherdevices_dev
)
# Install static library for dev package
install(
  TARGETS ${QW_WEATHER_DEVICES_LIB}_static
  DESTINATION lib/qw/devices/lib${QW_WEATHER_DEVICES_LIB}.a
  COMPONENT weatherdevices_dev
)
# Install shared library for runtime package
install(
  TARGETS ${QW_WEATHER_DEVICES_LIB}
  DESTINATION lib/qw/devices/lib
  COMPONENT weatherdevices_runtime
  )

install( FILES
    include/ecowitt_ln90lp.h
  DESTINATION include/qw/devices
  COMPONENT ecowittln90lp_dev
)
# Install static library for dev package
install(
  TARGETS ${QW_WEATHER_DEVICES_LIB}_static
  DESTINATION lib/qw/devices/lib${QW_WEATHER_DEVICES_LIB}.a
  COMPONENT ecowittln90lp_dev
)
# Install shared library for runtime package
install(
  TARGETS ${QW_WEATHER_DEVICES_LIB}
  DESTINATION lib/qw/devices/lib
  COMPONENT ecowittln90lp_runtime
  )

I then added these components to the ALL COMPONENTS value in my cpack_debian.cmake file:

set(CPACK_COMPONENTS_ALL
  runtime
  weatherdevices_runtime
  ecowittln90lp_runtime
  dev
  weatherdevices_dev
  ecowittln90lp_dev
)

I then got a different deb package for each one.

[cpack] CPack: Create package
[cpack] CPack: - package: /home/chrisk/Projects/src/qwunits/build/arm64-debug/libqwunits-dev_0.1.1-2_arm64.deb generated.
[cpack] CPack: - package: /home/chrisk/Projects/src/qwunits/build/arm64-debug/qwlibs-0.1.1-Linux-ecowittln90lp_dev.deb generated.
[cpack] CPack: - package: /home/chrisk/Projects/src/qwunits/build/arm64-debug/qwlibs-0.1.1-Linux-ecowittln90lp_runtime.deb generated.
[cpack] CPack: - package: /home/chrisk/Projects/src/qwunits/build/arm64-debug/libqwunits_0.1.1-2_arm64.deb generated.
[cpack] CPack: - package: /home/chrisk/Projects/src/qwunits/build/arm64-debug/qwlibs-0.1.1-Linux-weatherdevices_dev.deb generated.
[cpack] CPack: - package: /home/chrisk/Projects/src/qwunits/build/arm64-debug/qwlibs-0.1.1-Linux-weatherdevices_runtime.deb generated.

It seemed to have the expected files in them.

I’ll have to think if I like that better than giving each one their own directory and leave the COMPONENT for each as runtime and dev like all my other libraries.

Also, one thing I noticed is in order to add both a static and a shared library. In order for them to exist together I had to use a different name. so I appended “_static” to the static library definition. I ended up with a _static.a and a .a file. I would like to figure out how to only end up with a <library.a file. But, that is a different question.

Thanks

Chris