From bb61b1683d8aaed113cd25efb96dd9ca19219e74 Mon Sep 17 00:00:00 2001 From: Thom Troy Date: Fri, 19 Aug 2016 22:28:42 +0100 Subject: [PATCH] add imported target example and cmake 3.5.1 docker --- .travis.yml | 2 + 01-basic/K-imported-targets/CMakeLists.txt | 24 ++++++ 01-basic/K-imported-targets/README.adoc | 92 +++++++++++++++++++++ 01-basic/K-imported-targets/main.cpp | 24 ++++++ 01-basic/K-imported-targets/run_test.sh | 12 +++ 01-basic/README.adoc | 1 + dockerfiles/README.adoc | 8 +- dockerfiles/ubuntu16.04-default-cmake-3.5.1 | 23 ++++++ test.sh | 1 + 9 files changed, 185 insertions(+), 2 deletions(-) create mode 100644 01-basic/K-imported-targets/CMakeLists.txt create mode 100644 01-basic/K-imported-targets/README.adoc create mode 100644 01-basic/K-imported-targets/main.cpp create mode 100755 01-basic/K-imported-targets/run_test.sh create mode 100644 dockerfiles/ubuntu16.04-default-cmake-3.5.1 diff --git a/.travis.yml b/.travis.yml index 1214150..cf7620b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,8 +6,10 @@ compiler: - gcc before_install: - docker pull matrim/cmake-examples:3.4.3 +- docker pull matrim/cmake-examples:3.5.1 script: - docker run --rm -v $PWD:/data/code -it matrim/cmake-examples:3.4.3 /data/code/test.sh +- docker run --rm -v $PWD:/data/code -it matrim/cmake-examples:3.5.1 /data/code/test.sh branches: except: - gh-pages diff --git a/01-basic/K-imported-targets/CMakeLists.txt b/01-basic/K-imported-targets/CMakeLists.txt new file mode 100644 index 0000000..0e23d07 --- /dev/null +++ b/01-basic/K-imported-targets/CMakeLists.txt @@ -0,0 +1,24 @@ +cmake_minimum_required(VERSION 3.5) + +# Set the project name +project (imported_targets) + + +# find a boost install with the libraries filesystem and system +find_package(Boost 1.46.1 REQUIRED COMPONENTS filesystem system) + +# check if boost was found +if(Boost_FOUND) + message ("boost found") +else() + message (FATAL_ERROR "Cannot find Boost") +endif() + +# Add an executable +add_executable(imported_targets main.cpp) + +# link against the boost libraries +target_link_libraries( imported_targets + PRIVATE + Boost::filesystem +) diff --git a/01-basic/K-imported-targets/README.adoc b/01-basic/K-imported-targets/README.adoc new file mode 100644 index 0000000..bc6c16c --- /dev/null +++ b/01-basic/K-imported-targets/README.adoc @@ -0,0 +1,92 @@ += Imported Targets +:toc: +:toc-placement!: + +toc::[] + +# Introduction + +As previously mentioned in the link:../H-third-party-library[third party library], newer +versions of CMake alow you to link third party libraries using link:https://cmake.org/cmake/help/v3.6/prop_tgt/IMPORTED.html#prop_tgt:IMPORTED[imported] +ALIAS+ targets. + +The files in this tutorial are below: + +``` +$ tree +. +├── CMakeLists.txt +├── main.cpp +``` + + * link:CMakeLists.txt[] - Contains the CMake commands you wish to run + * link:main.cpp[] - The source file with main + +# Requirements + +This example requires the boost libraries to be installed in a default system location. + +# Concepts + +## Imported Target + +Imported targets are read-only targets that are exported by FindXXX modules. The benefit of imported +targets are that they can also populate include directories and linked libraries. + +To include boost filesystem you can do the following: + +[source,cmake] +---- + target_link_libraries( imported_targets + PRIVATE + Boost::filesystem + ) +---- + +This will automtaically link the Boost::filesystem and Boost::system libraries while also including the +Boost include directories. + +# Building the Example + +[source,bash] +---- +$ mkdir build + +$ cd build/ + +$ cmake .. +-- The C compiler identification is GNU 5.4.0 +-- The CXX compiler identification is GNU 5.4.0 +-- Check for working C compiler: /usr/bin/cc +-- Check for working C compiler: /usr/bin/cc -- works +-- Detecting C compiler ABI info +-- Detecting C compiler ABI info - done +-- Detecting C compile features +-- Detecting C compile features - done +-- Check for working CXX compiler: /usr/bin/c++ +-- Check for working CXX compiler: /usr/bin/c++ -- works +-- Detecting CXX compiler ABI info +-- Detecting CXX compiler ABI info - done +-- Detecting CXX compile features +-- Detecting CXX compile features - done +-- Boost version: 1.58.0 +-- Found the following Boost libraries: +-- filesystem +-- system +boost found +-- Configuring done +-- Generating done +-- Build files have been written to: /data/code/01-basic/K-imported-targets/build + +$ make +Scanning dependencies of target imported_targets +[ 50%] Building CXX object CMakeFiles/imported_targets.dir/main.cpp.o +[100%] Linking CXX executable imported_targets +[100%] Built target imported_targets + + +$ ./imported_targets +Hello Third Party Include! +Path is not relative + + +---- diff --git a/01-basic/K-imported-targets/main.cpp b/01-basic/K-imported-targets/main.cpp new file mode 100644 index 0000000..c7b58af --- /dev/null +++ b/01-basic/K-imported-targets/main.cpp @@ -0,0 +1,24 @@ +#include +#include +#include + +int main(int argc, char *argv[]) +{ + std::cout << "Hello Third Party Include!" << std::endl; + + // use a shared ptr + boost::shared_ptr isp(new int(4)); + + // trivial use of boost filesystem + boost::filesystem::path path = "/usr/share/cmake/modules"; + if(path.is_relative()) + { + std::cout << "Path is relative" << std::endl; + } + else + { + std::cout << "Path is not relative" << std::endl; + } + + return 0; +} diff --git a/01-basic/K-imported-targets/run_test.sh b/01-basic/K-imported-targets/run_test.sh new file mode 100755 index 0000000..e1efb51 --- /dev/null +++ b/01-basic/K-imported-targets/run_test.sh @@ -0,0 +1,12 @@ +#!/bin/bash +# Make sure we have the minimum cmake version +cmake_version=`cmake --version | grep version | cut -d" " -f3` + +[[ "$cmake_version" =~ ([3-9][.][5-9.][.][0-9]) ]] || exit 0 + +echo "correct version of cmake" +mkdir -p build && cd build && cmake .. && make +if [ $? -ne 0 ]; then + echo "Error running example" + exit 1 +fi diff --git a/01-basic/README.adoc b/01-basic/README.adoc index ab6d65d..7ccce5d 100644 --- a/01-basic/README.adoc +++ b/01-basic/README.adoc @@ -15,3 +15,4 @@ The examples included are - link:H-third-party-library[third-party-library]. Shows an example of how to link third party libraries. - link:I-compiling-with-clang[compiling-with-clang]. An example of invoking the clang compiler. - link:J-building-with-ninja[building-with-ninja] - Shows how to generate ninja build files + - link:K-imported-targets[imported-targets] - Shows how to link boost using the new imported targets diff --git a/dockerfiles/README.adoc b/dockerfiles/README.adoc index 2878575..17aaed6 100644 --- a/dockerfiles/README.adoc +++ b/dockerfiles/README.adoc @@ -22,7 +22,7 @@ For example: [source,bash] ---- -$ docker build --rm -f ubuntu14.04-cmake-3.4.3 t matrim/cmake-examples:3.4.3 . +$ docker build --rm -f ubuntu14.04-cmake-3.4.3 -t matrim/cmake-examples:3.4.3 . ---- In this example the tag is created as follows @@ -45,6 +45,10 @@ The images available include the following versions of cmake: $ docker pull docker pull matrim/cmake-examples:3.4.3 +* Ubuntu 16.04 with CMake 3.5.1 + + $ docker pull docker pull matrim/cmake-examples:3.5.1 + # Running When run the images will automatically create a non root user called devuser, with a default command to launch a bash shell in the users home directory. @@ -76,5 +80,5 @@ Below is an example of loading a volume and automatically running all cmake-exam [source,bash] ---- -docker run -e DEV_UID=`id -u` -e DEV_GID=`id -u` -v /checkout/directory:/data/code -it matrim/cmake-examples:3.4.3 /data/code/test.sh +docker run --rm -e DEV_UID=`id -u` -e DEV_GID=`id -u` -v /checkout/directory:/data/code -it matrim/cmake-examples:3.4.3 /data/code/test.sh ---- diff --git a/dockerfiles/ubuntu16.04-default-cmake-3.5.1 b/dockerfiles/ubuntu16.04-default-cmake-3.5.1 new file mode 100644 index 0000000..b99b4a7 --- /dev/null +++ b/dockerfiles/ubuntu16.04-default-cmake-3.5.1 @@ -0,0 +1,23 @@ +# Container for building and testing cmake-examples with default cmake v2.8.12.2 +FROM ubuntu:16.04 +MAINTAINER Thom Troy + +RUN apt-get update && apt-get install -y build-essential \ + sudo \ + cmake \ + libboost-all-dev \ + libprotobuf-dev \ + protobuf-compiler \ + cppcheck \ + clang-3.6 \ + ninja-build \ + wget \ + git \ + && apt-get clean \ + && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* + +ADD setup.sh /setup.sh +RUN chmod +x /setup.sh + +CMD ["/bin/bash"] +ENTRYPOINT ["/setup.sh"] diff --git a/test.sh b/test.sh index 5f97eec..10a3257 100755 --- a/test.sh +++ b/test.sh @@ -21,6 +21,7 @@ dirs=(./01-basic/A-hello-cmake \ ./01-basic/H-third-party-library \ ./01-basic/I-compiling-with-clang \ ./01-basic/J-building-with-ninja \ +./01-basic/K-imported-targets \ ./02-sub-projects/A-basic \ ./03-code-generation/protobuf \ ./03-code-generation/configure-files \