-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Convert package to a pure CMake package #285
base: main
Are you sure you want to change the base?
Changes from 2 commits
3025939
380c4e4
c9da89d
61da1e2
40d2950
48f14ce
7b7f62b
2a76e1a
2528dcf
f60d49d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,11 @@ | ||
cmake_minimum_required(VERSION 2.8.3) | ||
cmake_minimum_required(VERSION 2.8.12) | ||
project(serial) | ||
|
||
# Find catkin | ||
find_package(catkin REQUIRED) | ||
|
||
if(APPLE) | ||
find_library(IOKIT_LIBRARY IOKit) | ||
find_library(FOUNDATION_LIBRARY Foundation) | ||
endif() | ||
|
||
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 | ||
) | ||
endif() | ||
|
||
## Sources | ||
set(serial_SRCS | ||
src/serial.cc | ||
|
@@ -48,6 +28,9 @@ endif() | |
|
||
## Add serial library | ||
add_library(${PROJECT_NAME} ${serial_SRCS}) | ||
set_target_properties(${PROJECT_NAME} PROPERTIES | ||
POSITION_INDEPENDENT_CODE ON) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor comment: this make it impossible to compile the library without if (NOT DEFINED CMAKE_POSITION_INDEPENDENT_CODE)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
endif() before the line There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes I agree this shouldn’t be merged as is with this it’s too invasive. I picked this out of the ros2 branch we’ve been using. See comment above. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you build the library as a shared library you don't need this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I picked two commits from @leamas PR #231 (to set the so version) and then set BUILD_SHARED_LIBS ON. Then tested that out of the box it works fine in a colcon workspace...
If I additionally add the suggestion from @traversaro then it seems to work in both cases: @wjwwood any opinion or preference on this? |
||
|
||
if(APPLE) | ||
target_link_libraries(${PROJECT_NAME} ${FOUNDATION_LIBRARY} ${IOKIT_LIBRARY}) | ||
elseif(UNIX) | ||
|
@@ -66,16 +49,26 @@ include_directories(include) | |
|
||
## Install executable | ||
install(TARGETS ${PROJECT_NAME} | ||
ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION} | ||
LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION} | ||
RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION} | ||
ARCHIVE DESTINATION lib | ||
LIBRARY DESTINATION lib | ||
RUNTIME DESTINATION bin | ||
) | ||
|
||
## Install headers | ||
install(FILES include/serial/serial.h include/serial/v8stdint.h | ||
DESTINATION ${CATKIN_GLOBAL_INCLUDE_DESTINATION}/serial) | ||
DESTINATION include/serial) | ||
|
||
## Install CMake config | ||
install(FILES cmake/serialConfig.cmake | ||
DESTINATION share/serial/cmake) | ||
|
||
|
||
## Install package.xml | ||
install(FILES package.xml | ||
DESTINATION share/serial) | ||
Comment on lines
+80
to
+89
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might be a good time to change to exported targets, and also consider putting the headers in a separate subfolder, which we've been doing in ROS 2 for a while now to promote better header isolation in a merged install space. I.e. we'd put the headers in |
||
|
||
## Tests | ||
if(CATKIN_ENABLE_TESTING) | ||
include(CTest) | ||
if(BUILD_TESTING) | ||
add_subdirectory(tests) | ||
endif() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
get_filename_component(SERIAL_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH) | ||
set(SERIAL_INCLUDE_DIRS "${SERIAL_CMAKE_DIR}/../../../include") | ||
find_library(SERIAL_LIBRARIES serial PATHS ${SERIAL_CMAKE_DIR}/../../../lib/serial) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we're adding a config module, we should write a config module that exports a target instead of doing the old school approach of setting some variables that downstream projects have to use. That means we need to create an export set for the library target and install that export set then this file becomes a one liner that will look something like this: include(${CMAKE_CURRENT_LIST_DIR}/serial-targets.cmake) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ChrisThrasher can you point to helpful documentation and examples on how to do this well? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm biased but this installation code I wrote I quite like. It's goes above and beyond to be as robust and idiomatic as possible. You don't need to copy everything it does but it should do a good job laying our the basic steps.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This is not old-school. The normal usage pattern would be something like below, assuming that the serial library lives in a subdirectory
This would just require a one-liner in CMakeLists.txt defining a ALIAS target. It relies on cmake's support for transitive dependencies. Adding such a target would simplify documentation and overall usage a lot. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that's what I'm saying. We're in agreement. The variable-based approach is old school because targets are the modern way of doing things. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This is actually a personal preference... I actually agree on the personal level, but let's face it: the use of ${PROJECT_NAME} is common enough in the cmake community to leave in place. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't mistake commonality for it being a good idea. Lots of bad practices are still popular but that should not be used as an argument in favor of perpetuating bad ideas. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,13 @@ | |
<author email="[email protected]">William Woodall</author> | ||
<author email="[email protected]">John Harrison</author> | ||
|
||
<buildtool_depend>catkin</buildtool_depend> | ||
<buildtool_depend>cmake</buildtool_depend> | ||
|
||
<!-- boost is only needed for test/proof_of_concepts which are not enabled by default --> | ||
<!--test_depend>boost</test_depend--> | ||
<test_depend>gtest</test_depend> | ||
|
||
<export> | ||
<build_type>cmake</build_type> | ||
</export> | ||
</package> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,19 @@ | ||
if(UNIX) | ||
catkin_add_gtest(${PROJECT_NAME}-test unix_serial_tests.cc) | ||
target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME}) | ||
find_package(GTest REQUIRED) | ||
include_directories(${GTEST_INCLUDE_DIRS}) | ||
moriarty marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
add_executable(${PROJECT_NAME}-test unix_serial_tests.cc) | ||
target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME} ${GTEST_LIBRARIES}) | ||
if(NOT APPLE) | ||
target_link_libraries(${PROJECT_NAME}-test util) | ||
endif() | ||
add_test("${PROJECT_NAME}-test-gtest" ${PROJECT_NAME}-test) | ||
|
||
if(NOT APPLE) # these tests are unreliable on macOS | ||
catkin_add_gtest(${PROJECT_NAME}-test-timer unit/unix_timer_tests.cc) | ||
target_link_libraries(${PROJECT_NAME}-test-timer ${PROJECT_NAME}) | ||
add_executable(${PROJECT_NAME}-test-timer unit/unix_timer_tests.cc) | ||
target_link_libraries(${PROJECT_NAME}-test-timer ${PROJECT_NAME} ${GTEST_LIBRARIES}) | ||
add_test("${PROJECT_NAME}-test-timer-gtest" ${PROJECT_NAME}-test-timer) | ||
endif() | ||
# Boost is only required for tests/proof_of_concepts which are not enabled by default | ||
# find_package(Boost REQUIRED) | ||
endif() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It aids in readability and
grep
ability if you simply spell out the variable name.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 to this recommendation as it makes the cmake file much easier to read and understand. One of the hardest parts of cmake is reading the config files.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree here, but I'll wait for @wjwwood to chime in on this PR in general.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am concerned this PR gets too big and goes the way of any of the other open PRs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's fine with me, but it deviates from what it common in the ROS ecosystem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not only the ROS ecosystem, but the whole cmake community I would say. For that reason I think this could be left as-is
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ChrisThrasher wrote
No. This should IMHO be done like in e1dabd8, taking into account whether to include v8stdint.h or not.