Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Install target generates a clean CMake package (+ Catkin dependency is now optional) #215

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 111 additions & 38 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,41 +1,60 @@
cmake_minimum_required(VERSION 2.8.3)
project(serial)

# Find catkin
find_package(catkin REQUIRED)
# Either this or bump cmake_minimum_required to 3.0+
# Allow us to use Version in project() call : https://cmake.org/cmake/help/v3.0/policy/CMP0048.html#policy:CMP0048
cmake_policy(SET CMP0048 NEW)

if(APPLE)
find_library(IOKIT_LIBRARY IOKit)
find_library(FOUNDATION_LIBRARY Foundation)
endif()
project(serial VERSION 1.2.1)


## Options

# Use Catkin (disabled by default)
option(USE_CATKIN off)
# Build serial sample
option(BUILD_SAMPLE on)

if(UNIX AND NOT APPLE)

# Find catkin
if(USE_CATKIN)
find_package(catkin REQUIRED)

if(UNIX AND NOT APPLE)
# If Linux, add rt and pthread
set(rt_LIBRARIES rt)
set(pthread_LIBRARIES pthread)
catkin_package(
LIBRARIES ${PROJECT_NAME}
INCLUDE_DIRS include
DEPENDS rt pthread
)
else()
# Otherwise normal call
catkin_package(
LIBRARIES ${PROJECT_NAME}
INCLUDE_DIRS include
)
else()
# Otherwise normal call
catkin_package(
LIBRARIES ${PROJECT_NAME}
INCLUDE_DIRS include
)
endif()
endif()



if(APPLE)
find_library(IOKIT_LIBRARY IOKit)
find_library(FOUNDATION_LIBRARY Foundation)
endif()



## Sources
set(serial_SRCS
src/serial.cc
include/serial/serial.h
include/serial/v8stdint.h
)

if(APPLE)
# If OSX
list(APPEND serial_SRCS src/impl/unix.cc)
list(APPEND serial_SRCS src/impl/list_ports/list_ports_osx.cc)
# If OSX
list(APPEND serial_SRCS src/impl/unix.cc)
list(APPEND serial_SRCS src/impl/list_ports/list_ports_osx.cc)
elseif(UNIX)
# If unix
list(APPEND serial_SRCS src/impl/unix.cc)
Expand All @@ -47,34 +66,88 @@ else()
endif()

## Add serial library
add_library(${PROJECT_NAME} ${serial_SRCS})
add_library(${PROJECT_NAME} STATIC ${serial_SRCS})

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why forcing static?
better would be to use -DBUILD_SHARED_LIBS=OFF (see https://cmake.org/cmake/help/v3.0/variable/BUILD_SHARED_LIBS.html)


if(APPLE)
target_link_libraries(${PROJECT_NAME} ${FOUNDATION_LIBRARY} ${IOKIT_LIBRARY})
target_link_libraries(${PROJECT_NAME} ${FOUNDATION_LIBRARY} ${IOKIT_LIBRARY})
elseif(UNIX)
target_link_libraries(${PROJECT_NAME} rt pthread)
target_link_libraries(${PROJECT_NAME} rt pthread)
else()
target_link_libraries(${PROJECT_NAME} setupapi)
target_link_libraries(${PROJECT_NAME} setupapi)
endif()

## Uncomment for example
add_executable(serial_example examples/serial_example.cc)
add_dependencies(serial_example ${PROJECT_NAME})
target_link_libraries(serial_example ${PROJECT_NAME})

## Include headers
include_directories(include)
target_include_directories(${PROJECT_NAME} PUBLIC $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include>)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
target_include_directories(${PROJECT_NAME} PUBLIC $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include>)
target_include_directories(${PROJECT_NAME} PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>)

The use of CMAKE_CURRENT_SOURCE_DIR is problematic when the project is used inside a bigger project via add_subdirectory , for example when using the FetchContent CMake module. Use of CMAKE_CURRENT_SOURCE_DIR should remove any ambiguity.



## Example
if(BUILD_SAMPLE)
add_executable(serial_example examples/serial_example.cc)
add_dependencies(serial_example ${PROJECT_NAME})
target_link_libraries(serial_example ${PROJECT_NAME})
endif()


## Export

# Set postfix names for exporting
set_target_properties(${PROJECT_NAME} PROPERTIES
RELEASE_POSTFIX ""
RELWITHDEBINFO_POSTFIX "-rd"
MINSIZEREL_POSTFIX "-mr"
DEBUG_POSTFIX "-d")


if(USE_CATKIN)
## Install headers
install(FILES include/serial/serial.h include/serial/v8stdint.h
DESTINATION ${CATKIN_GLOBAL_INCLUDE_DESTINATION}/serial)
## Install executable
install(TARGETS ${PROJECT_NAME}
ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
)
else()
set(EXPORT_TARGET_NAME serialTargets)

install(EXPORT ${EXPORT_TARGET_NAME} DESTINATION cmake/)
# Include module with fuction 'write_basic_package_version_file' and 'configure_package_config_file'
include(CMakePackageConfigHelpers)

set(OUTPUT_CMAKE_EXPORT_GENERATED_FILES ${CMAKE_BINARY_DIR}/CMakeExportConfig)
# Configure '<PROJECT-NAME>ConfigVersion.cmake'
write_basic_package_version_file(${OUTPUT_CMAKE_EXPORT_GENERATED_FILES}/${PROJECT_NAME}Config-version.cmake
# <AnyNewerVersion|SameMajorVersion|SameMinorVersion|ExactVersion>
# Careful : SameMinorVersion and ExactVersion were introduced in CMake 3.11
COMPATIBILITY SameMajorVersion
)

# Configure '<PROJECT-NAME>Config.cmake'
configure_package_config_file(
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/${PROJECT_NAME}Config.cmake.in"
"${OUTPUT_CMAKE_EXPORT_GENERATED_FILES}/${PROJECT_NAME}Config.cmake"
INSTALL_DESTINATION "${CMAKE_INSTALL_PREFIX}/cmake"
)

# Install config file generated
install(DIRECTORY ${OUTPUT_CMAKE_EXPORT_GENERATED_FILES}/
DESTINATION cmake/)


## Install headers
install(FILES include/serial/serial.h include/serial/v8stdint.h
DESTINATION ${CMAKE_INSTALL_PREFIX}/include/serial)

## Install executable
install(TARGETS ${PROJECT_NAME} EXPORT ${EXPORT_TARGET_NAME}
INCLUDES DESTINATION $<INSTALL_INTERFACE:include/>
ARCHIVE DESTINATION ${__dest} COMPONENT ${TARGET_NAME}
)
endif()

## Install executable
install(TARGETS ${PROJECT_NAME}
ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
)

## Install headers
install(FILES include/serial/serial.h include/serial/v8stdint.h
DESTINATION ${CATKIN_GLOBAL_INCLUDE_DESTINATION}/serial)

## Tests
if(CATKIN_ENABLE_TESTING)
add_subdirectory(tests)
endif()
endif()
6 changes: 6 additions & 0 deletions cmake/serialConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@PACKAGE_INIT@


include("${CMAKE_CURRENT_LIST_DIR}/@[email protected]")

check_required_components("@PROJECT_NAME@")