From 79d88e9af9559365a71c1e62b75f9b1ad6d013aa Mon Sep 17 00:00:00 2001 From: Daniel Koch Date: Wed, 7 Mar 2018 17:43:29 -0700 Subject: [PATCH] Set up CMake package and install --- CMakeLists.txt | 25 +++++++++++++++++---- README.md | 50 ++++++++++++++++++++++++++++++++++------- async_comm-config.cmake | 4 ++++ 3 files changed, 67 insertions(+), 12 deletions(-) create mode 100644 async_comm-config.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index c5a4fe1..cc6dada 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,16 +21,33 @@ add_library(${PROJECT_NAME} target_link_libraries(${PROJECT_NAME} ${Boost_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}) # examples -option(BUILD_EXAMPLES "Build examples" OFF) +option(ASYNC_COMM_BUILD_EXAMPLES "Build examples" OFF) -if(BUILD_EXAMPLES) +if(ASYNC_COMM_BUILD_EXAMPLES) add_executable(udp_hello_world examples/udp_hello_world.cpp) target_link_libraries(udp_hello_world ${PROJECT_NAME}) add_executable(serial_loopback examples/serial_loopback.cpp) target_link_libraries(serial_loopback ${PROJECT_NAME}) -endif(BUILD_EXAMPLES) +endif(ASYNC_COMM_BUILD_EXAMPLES) -# ROS release +# install +set(LIB_DEST lib/${PROJECT_NAME}) +set(INCLUDE_DEST include) + +install(TARGETS ${PROJECT_NAME} + EXPORT ${PROJECT_NAME}-targets + ARCHIVE DESTINATION ${LIB_DEST} +) +install(DIRECTORY include/${PROJECT_NAME} + DESTINATION ${INCLUDE_DEST} + FILES_MATCHING PATTERN "*.h" +) + +# install CMake package configuration +install(EXPORT ${PROJECT_NAME}-targets DESTINATION ${LIB_DEST}) +install(FILES ${PROJECT_NAME}-config.cmake DESTINATION ${LIB_DEST}) + +# install package.xml for ROS release install(FILES package.xml DESTINATION share/${PROJECT_NAME}) diff --git a/README.md b/README.md index 8c69d33..b8757d8 100644 --- a/README.md +++ b/README.md @@ -7,25 +7,59 @@ It uses the [Boost.Asio](http://www.boost.org/doc/libs/master/doc/html/boost_asi ## Including in your project -The following instructions are for a project using Git for version control and CMake for a build system, but should serve as a starting point for other setups. +There are two ways to use the `async_comm` library in your project: -The easiest way to embed the `async_comm` library in your project is as a [Git submodule](https://git-scm.com/docs/gitsubmodules). For example, to put `async_comm` in the `lib/async_comm directory`, run the following from the root of your project: + 1. Build and install the library on your system, then use CMake's `find_package()` functionality + 2. Include the async_comm as a submodule in your project + +### System install + +First, download and install the library: ```bash -git submodule add https://github.com/dpkoch/async_comm.git lib/async_comm +git clone https://github.com/dpkoch/async_comm.git +cd async_comm +mkdir build && cd build/ +cmake .. && make +sudo make install ``` -Then add the following to your `CMakeLists.txt` to build the `async_comm` library and make the headers accessible in your project: +Then, in your project do something like this in your CMakeLists.txt: ```CMake +cmake_minimum_required(VERSION 2.8.11) +project(my_project) + set(CMAKE_CXX_FLAGS "-std=c++11") -add_subdirectory(lib/async_comm) -include_directories(lib/async_comm/include) + +find_package(async_comm REQUIRED) +include_directories(${async_comm_INCLUDE_DIRS}) + +add_executable(my_project src/my_project.cpp) +target_link_libraries(testing ${async_comm_LIBRARIES}) ``` -Then link your executable to the `async_comm` library with something like +### Including as a submodule + +If you don't want to go with the system install option, the next easiest way to embed the `async_comm` library in your project is as a [Git submodule](https://git-scm.com/docs/gitsubmodules). The following instructions are for a project using Git for version control and CMake for a build system, but should serve as a starting point for other setups. + +For example, to put `async_comm` in the `lib/async_comm directory`, run the following from the root of your project: + +```bash +git submodule add https://github.com/dpkoch/async_comm.git lib/async_comm +``` + +Your CMakeLists.txt file would then look something like this: ```CMake +cmake_minimum_required(VERSION 2.8.11) +project(my_project) + +set(CMAKE_CXX_FLAGS "-std=c++11") + +add_subdirectory(lib/async_comm) +include_directories(lib/async_comm/include) + add_executable(my_program src/my_program.cpp) target_link_libraries(my_program async_comm) ``` @@ -60,7 +94,7 @@ where `serial_` is an instance of `async_comm::Serial`. ## Examples -There are two simple examples provided in the repository. To build the examples, run CMake with the `-DBUILD_EXAMPLES=ON` flag. +There are two simple examples provided in the repository. To build the examples, run CMake with the `-DASYNC_COMM_BUILD_EXAMPLES=ON` flag. - `examples/serial_loopback.cpp`: Designed for use with a USB-to-UART adapter with the RX and TX pins connected together (loopback). Sends a series of bytes out and prints them to the console as they are received back. - `examples/udp_hello_world.cpp`: Opens two UDP objects listening on different ports on the local host, and then uses each to send a simple "hello world" message to the other. diff --git a/async_comm-config.cmake b/async_comm-config.cmake new file mode 100644 index 0000000..877c3ba --- /dev/null +++ b/async_comm-config.cmake @@ -0,0 +1,4 @@ +get_filename_component(SELF_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH) +include(${SELF_DIR}/async_comm-targets.cmake) +get_filename_component(aysnc_comm_INCLUDE_DIRS "${SELF_DIR}/../../include/async_comm" ABSOLUTE) +set(async_comm_LIBRARIES async_comm)