diff --git a/06-installer/README.adoc b/06-installer/README.adoc new file mode 100644 index 0000000..0564883 --- /dev/null +++ b/06-installer/README.adoc @@ -0,0 +1,15 @@ += Installers + +:toc: +:toc-placement!: + +toc::[] + +[[intro]] +Introduction +------------ + +CMake has the ability to create installers for multiple platforms using a program +called link:https://cmake.org/Wiki/CMake:CPackPackageGenerators[CPack]. +CPack includes the ability to create Linux RPM, deb and gzip distributions + of both binaries and source code. It also includes teh ability to create NSIS files for Microsoft Windows. diff --git a/06-installer/deb/CMakeLists.txt b/06-installer/deb/CMakeLists.txt new file mode 100644 index 0000000..c935923 --- /dev/null +++ b/06-installer/deb/CMakeLists.txt @@ -0,0 +1,77 @@ +cmake_minimum_required(VERSION 2.6) + +project(cmake_examples_deb) + +include_directories( + ${PROJECT_SOURCE_DIR}/inc + ) + +# set a project version +set (deb_example_VERSION_MAJOR 0) +set (deb_example_VERSION_MINOR 2) +set (deb_example_VERSION_PATCH 2) +set (deb_example_VERSION "${deb_example_VERSION_MAJOR}.${deb_example_VERSION_MINOR}.${deb_example_VERSION_PATCH}") + + +############################################################ +# Create a library +############################################################ + +# Source files to be used in the library +set(library_SOURCES + src/Hello.cpp +) + +#Generate the shared library from the library sources +add_library(cmake_examples_deb SHARED ${library_SOURCES}) + +############################################################ +# Create an executable +############################################################ + +# Source fles for the binary +set(binary_SOURCES + src/main.cpp +) + +# Add an executable with the above sources +add_executable(cmake_examples_deb_bin ${binary_SOURCES}) + +# link the new hello_library target with the hello_binary target +target_link_libraries( cmake_examples_deb_bin + cmake_examples_deb +) + +############################################################ +# Install +############################################################ + +# Binaries +install (TARGETS cmake_examples_deb_bin + DESTINATION bin) + +# Library +# Note: may not work on windows +install (TARGETS cmake_examples_deb + LIBRARY DESTINATION lib) + +# Config +install (FILES cmake-examples.conf + DESTINATION etc) + +############################################################ +# Create DEB +############################################################ + +# Tell CPack to generate a .deb package +set(CPACK_GENERATOR "DEB") + +# Set a Package Maintainer. +# This is required +set(CPACK_DEBIAN_PACKAGE_MAINTAINER "Thom Troy") + +# Set a Package Version +set(CPACK_PACKAGE_VERSION ${deb_example_VERSION}) + +# Include CPack +include(CPack) diff --git a/06-installer/deb/README.adoc b/06-installer/deb/README.adoc new file mode 100644 index 0000000..b7f5953 --- /dev/null +++ b/06-installer/deb/README.adoc @@ -0,0 +1,157 @@ += Creating deb files +:toc: +:toc-placement!: + +toc::[] + +# Introduction + +This example shows how to generate a Linux installers using the link:https://www.debian.org/doc/manuals/debian-faq/ch-pkg_basics.en.html[deb] + format. + +The files in this tutorial are below: + +``` +$ tree +. +├── cmake-examples.conf +├── CMakeLists.txt +├── inc +│   └── Hello.h +└── src + ├── Hello.cpp + └── main.cpp +``` + + * CMakeLists.txt - Contains the CMake commands you wish to run + * cmake-examples.conf - An example configuration file + * inc/Hello.h - The header file to include + * src/Hello.cpp - A source file to compile + * src/main.cpp - The source file with main + +# Concepts + +## CPack Generator + +A CPack Generator can be used by a `make package` target to create an installer. + +In the case of Debian packages you can tell CMake to create a generator using the following: + +[source,cmake] +---- +set(CPACK_GENERATOR "DEB") +---- + +After setting various settings to describe the package you must then tell CMake to +include the CPack generator using + +[source,cmake] +---- +include(CPack) +---- + +Once included all files that would typically be installed using a `make install` target +can now be packaged into a Debian package. + +## Debian Package Settings + +Various settings for the package are exposed by CPack. In this example we set the +following: + +[source,cmake] +---- +# Set a Package Maintainer. +# This is required +set(CPACK_DEBIAN_PACKAGE_MAINTAINER "Thom Troy") + +# Set a Package Version +set(CPACK_PACKAGE_VERSION ${deb_example_VERSION}) +---- + +Which sets the maintainer and version. More debian specific settings are specified below +or at link:https://cmake.org/Wiki/CMake:CPackPackageGenerators#Debian_Generator_specific_settings[the CPack Wiki] + +[cols=",",options="header",] +|======================================================================= +|Variable |Info +|CPACK_DEBIAN_PACKAGE_MAINTAINER |Maintainer information + +|CPACK_PACKAGE_DESCRIPTION_SUMMARY |Package short description + +|CPACK_PACKAGE_DESCRIPTION |Package description + +|CPACK_DEBIAN_PACKAGE_DEPENDS |For advanced users to add custom scripts. + +|CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA |The build directory you are currently in. + +|CPACK_DEBIAN_PACKAGE_SECTION |Package section (see link:http://packages.debian.org/stable/[here]) + +|CPACK_DEBIAN_PACKAGE_VERSION |Package version +|======================================================================= + +# Building the Example + +[source,bash] +---- +$ mkdir build + +$ cd build/ + +$ cmake .. +-- The C compiler identification is GNU 4.8.4 +-- The CXX compiler identification is GNU 4.8.4 +-- 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 +-- 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 +-- Configuring done +-- Generating done +-- Build files have been written to: /home/matrim/workspace/cmake-examples/06-installer/deb/build + +$ make help +The following are some of the valid targets for this Makefile: +... all (the default if no target is provided) +... clean +... depend +... cmake_examples_deb +... cmake_examples_deb_bin +... edit_cache +... install +... install/local +... install/strip +... list_install_components +... package +... package_source +... rebuild_cache +... src/Hello.o +... src/Hello.i +... src/Hello.s +... src/main.o +... src/main.i +... src/main.s + +$ make package +Scanning dependencies of target cmake_examples_deb +[ 50%] Building CXX object CMakeFiles/cmake_examples_deb.dir/src/Hello.cpp.o +Linking CXX shared library libcmake_examples_deb.so +[ 50%] Built target cmake_examples_deb +Scanning dependencies of target cmake_examples_deb_bin +[100%] Building CXX object CMakeFiles/cmake_examples_deb_bin.dir/src/main.cpp.o +Linking CXX executable cmake_examples_deb_bin +[100%] Built target cmake_examples_deb_bin +Run CPack packaging tool... +CPack: Create package using DEB +CPack: Install projects +CPack: - Run preinstall target for: cmake_examples_deb +CPack: - Install project: cmake_examples_deb +CPack: Create package +CPack: - package: /home/matrim/workspace/cmake-examples/06-installer/deb/build/cmake_examples_deb-0.2.2-Linux.deb generated. + +$ ls +CMakeCache.txt cmake_examples_deb-0.2.2-Linux.deb cmake_examples_deb_bin CMakeFiles cmake_install.cmake CPackConfig.cmake _CPack_Packages CPackSourceConfig.cmake install_manifest.txt libcmake_examples_deb.so Makefile + +---- diff --git a/06-installer/deb/cmake-examples.conf b/06-installer/deb/cmake-examples.conf new file mode 100644 index 0000000..d530261 --- /dev/null +++ b/06-installer/deb/cmake-examples.conf @@ -0,0 +1 @@ +# Sample configuration file that could be installed diff --git a/06-installer/deb/inc/Hello.h b/06-installer/deb/inc/Hello.h new file mode 100644 index 0000000..f838400 --- /dev/null +++ b/06-installer/deb/inc/Hello.h @@ -0,0 +1,10 @@ +#ifndef __HELLO_H__ +#define __HELLO_H__ + +class Hello +{ +public: + void print(); +}; + +#endif diff --git a/06-installer/deb/src/Hello.cpp b/06-installer/deb/src/Hello.cpp new file mode 100644 index 0000000..75c152d --- /dev/null +++ b/06-installer/deb/src/Hello.cpp @@ -0,0 +1,8 @@ +#include + +#include "Hello.h" + +void Hello::print() +{ + std::cout << "Hello Install!" << std::endl; +} diff --git a/06-installer/deb/src/main.cpp b/06-installer/deb/src/main.cpp new file mode 100644 index 0000000..62a1520 --- /dev/null +++ b/06-installer/deb/src/main.cpp @@ -0,0 +1,8 @@ +#include "Hello.h" + +int main(int argc, char *argv[]) +{ + Hello hi; + hi.print(); + return 0; +} \ No newline at end of file