Initial import

This commit is contained in:
Matteo Settenvini 2019-07-27 18:28:47 +02:00
commit 0a5348b401
31 changed files with 1907 additions and 0 deletions

145
exercises/CMakeLists.txt Normal file
View file

@ -0,0 +1,145 @@
# cmake_minimum_required(VERSION 3.11)
cmake_minimum_required(VERSION 3.11)
## Declare a project named "cmake-exercise"
project(cmake-exercise VERSION 0.90.0)
if(CMAKE_BINARY_DIR STREQUAL CMAKE_SOURCE_DIR)
message(FATAL_ERROR "Don't build in the source directory, pretty please.")
endif()
## Includes
include(GNUInstallDirs)
include(GenerateExportHeader)
include(CMakePackageConfigHelpers)
## Find necessary packages
find_package(OpenMP)
find_package(Threads REQUIRED)
## Artifacts to create
add_library(obj OBJECT)
add_library(static STATIC)
add_library(shared SHARED)
add_library(interface INTERFACE)
add_executable(tests)
## Generate other files
generate_export_header(shared
BASE_NAME DSO
EXPORT_FILE_NAME include/cmake-exercise/config.hh)
## Specify source files
target_sources(obj
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/cmake-exercise/a.hh>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/cmake-exercise/a.hh>
PRIVATE src/a.cc)
target_sources(shared
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/cmake-exercise/b.hh>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/cmake-exercise/b.hh>
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include/cmake-exercise/config.hh>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/cmake-exercise/config.hh>
PRIVATE src/b.cc
$<TARGET_PROPERTY:obj,INTERFACE_SOURCES>
$<TARGET_OBJECTS:obj>)
target_sources(interface
INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/cmake-exercise/c.hh>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/cmake-exercise/c.hh>)
target_sources(tests
PRIVATE test/test.cc)
## Add include folders
target_include_directories(interface
INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
target_include_directories(obj
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
target_include_directories(shared
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
## Set target properties for visibility and PIC
set_property(TARGET obj PROPERTY POSITION_INDEPENDENT_CODE TRUE)
set_property(TARGET obj shared PROPERTY CXX_VISIBILITY_PRESET hidden)
set_property(TARGET obj shared PROPERTY VISIBILITY_INLINES_HIDDEN TRUE)
## Specify C++ version to use
target_compile_features(interface
INTERFACE cxx_std_11)
target_compile_features(obj
INTERFACE cxx_std_11
PRIVATE cxx_std_14)
## Link to other libraries, and inherit their settings
target_link_libraries(obj
PUBLIC Threads::Threads)
target_link_libraries(static
PRIVATE interface
PUBLIC obj)
target_link_libraries(shared
PRIVATE interface obj)
target_link_libraries(tests
PRIVATE static)
if(TARGET OpenMP::OpenMP_CXX)
target_compile_definitions(tests PRIVATE HAVE_OPENMP)
target_link_libraries(tests PRIVATE OpenMP::OpenMP_CXX)
endif()
## Install all needed files for packaging
install(FILES $<TARGET_PROPERTY:shared,INTERFACE_SOURCES>
$<TARGET_PROPERTY:interface,INTERFACE_SOURCES>
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
COMPONENT devel)
install(TARGETS shared
EXPORT cmake-exercise
DESTINATION ${CMAKE_INSTALL_LIBDIR}
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(TARGETS obj interface
EXPORT cmake-exercise
COMPONENT devel
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(TARGETS tests
DESTINATION ${CMAKE_INSTALL_DATADIR}/cmake-exercise/test
COMPONENT test)
## Add a test
enable_testing()
add_test(NAME not-really-a-test
COMMAND tests --ignored-param)
## Generate the export sets to be able to import this project
## in other projects via find_package (the config file is not complete though)
set(CMAKE_INSTALL_CMAKEDIR ${CMAKE_INSTALL_LIBDIR}/cmake/cmake-exercise)
export(EXPORT cmake-exercise
NAMESPACE cmake-exercise::
FILE cmake-exercise-targets.cmake)
install(EXPORT cmake-exercise
NAMESPACE cmake-exercise::
DESTINATION ${CMAKE_INSTALL_CMAKEDIR}
FILE cmake-exercise-targets.cmake
COMPONENT devel)
configure_package_config_file(cmake-exercise-config.cmake.in
cmake-exercise-config.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_CMAKEDIR})
write_basic_package_version_file(cmake-exercise-version.cmake
COMPATIBILITY SameMajorVersion)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/cmake-exercise-config.cmake
${CMAKE_CURRENT_BINARY_DIR}/cmake-exercise-version.cmake
DESTINATION ${CMAKE_INSTALL_CMAKEDIR}
COMPONENT devel)
## Trigger packaging
set(CPACK_PACKAGE_FILE_NAME package)
set(CPACK_GENERATOR TXZ)
set(CPACK_ARCHIVE_COMPONENT_INSTALL TRUE)
include(CPack)

6
exercises/README Normal file
View file

@ -0,0 +1,6 @@
This is a simple exercise to get familiar a bit with CMake.
See the deployment diagram in doc/ to know what is the final result
should look like.
Other than that, follow the comments in CMakeLists.txt

View file

@ -0,0 +1,3 @@
@PACKAGE_INIT@
include(${CMAKE_CURRENT_LIST_DIR}/cmake-exercise-targets.cmake)

Binary file not shown.

After

Width:  |  Height:  |  Size: 183 KiB

View file

@ -0,0 +1,119 @@
@startuml
left to right direction
hide <<Layout>> stereotype
skinparam rectangle<<Layout>> {
borderColor Transparent
backgroundColor Transparent
fontColor Transparent
shadowing false
}
folder "source directory" as srcdir {
folder "include/cmake-exercise" as include_srcdir {
file a.hh
file b.hh
file c.hh
}
folder "src" {
file a.cc
file b.cc
}
folder "test" as test_srcdir {
file test.cc
}
file CMakeLists.txt
}
folder "build directory" as builddir {
rectangle folders_layout <<Layout>> {
folder "src" as src_builddir {
file a.o
file b.o
}
folder "test" as test_builddir {
file test.o
}
folder "include/cmake-exercise" as include_builddir {
file config.hh
}
}
rectangle artifacts_layout <<Layout>> {
artifact "<<static library>>\nlibstatic.a" as static
artifact "<<shared library>>\nlibshared.so" as shared
artifact "<<executable>>\ntest-binary" as test
static -[hidden]r- shared
shared -[hidden]r- test
}
}
srcdir -[hidden]r- builddir
package package.tar.xz {
folder "<prefix>/include/cmake-exercise" {
file a.hh as installed_a.hh
file b.hh as installed_b.hh
file c.hh as installed_c.hh
file config.hh as installed_config.hh
}
folder "<prefix>/share/cmake-exercise/test" {
artifact "<<executable>>\ntest" as installed_test
}
folder "<prefix>/lib" {
artifact "<<shared library>>\nlibshared.so" as installed_shared
}
}
actor CMake
a.hh --> installed_a.hh : <<install>>
b.hh --> installed_b.hh : <<install>>
c.hh --> installed_c.hh : <<install>>
config.hh --> installed_config.hh : <<install>>
test --> installed_test : <<install>>
shared --> installed_shared : <<install>>
file "<<source>>\na.cc" as a.cc
file "<<source>>\nb.cc" as b.cc
file "<<source>>\na.hh" as a.hh
file "<<source>>\nb.hh" as b.hh
file "<<source>>\nc.hh" as c.hh
file "<<source>>\nconfig.hh" as config.hh
file "<<file>>\na.o" as a.o
file "<<file>>\nb.o" as b.o
file "<<file>>\ntest.o" as test.o
file "<<source>>\ntest.cc" as test.cc
CMake -u-> config.hh : <<generate>>
a.cc --> a.o : <<compile>>
b.cc --> b.o : <<compile>>
test.cc --> test.o : <<compile>>
test ..> test.o : <<link>>
test ..> static : <<link>>
static ..> a.o : <<archive>>
shared ..> a.o : <<link>>
shared ..> b.o : <<link>>
b.hh ~r~> a.hh : <<include>>
a.hh ~~> c.hh : <<include>>
b.hh ~~> c.hh : <<include>>
a.hh ~~> config.hh : <<include>>
b.hh ~~> config.hh : <<include>>
a.cc ~~> a.hh : <<include>>
b.cc ~~> b.hh : <<include>>
test.cc ~~> a.hh : <<include>>
@enduml

View file

@ -0,0 +1,16 @@
#pragma once
#include <thread>
class A
{
public:
A ();
A (A&&) = default;
~A ();
A& operator= (A&&) = default;
private:
std::thread _t;
};

View file

@ -0,0 +1,16 @@
#pragma once
#include <cmake-exercise/a.hh>
#include <cmake-exercise/config.hh>
#include <vector>
class B
{
public:
DSO_EXPORT B ();
private:
std::vector<A> _a;
};

View file

@ -0,0 +1,4 @@
#pragma once
template <typename T>
void unused_function (); // nothing to see here

31
exercises/src/a.cc Normal file
View file

@ -0,0 +1,31 @@
#include <cmake-exercise/a.hh>
#include <chrono>
#include <cstdlib>
#include <iostream>
#include <sstream>
A::A ()
: _t ([] ()
{
{
std::ostringstream oss;
oss << "Starting thread " << std::this_thread::get_id () << std::endl;
std::cout << oss.str() << std::flush;
}
std::this_thread::sleep_for (std::chrono::milliseconds(std::rand() % 100 * 10));
{
std::ostringstream oss;
oss << "Thread " << std::this_thread::get_id () << " finished" << std::endl;
std::cout << oss.str() << std::flush;
}
})
{
}
A::~A ()
{
_t.join ();
}

7
exercises/src/b.cc Normal file
View file

@ -0,0 +1,7 @@
#include <cmake-exercise/b.hh>
#include <cmake-exercise/c.hh>
B::B ()
: _a (10)
{
}

19
exercises/test/test.cc Normal file
View file

@ -0,0 +1,19 @@
#include <cmake-exercise/a.hh>
#include <vector>
int
main()
{
// not a real test, heh.
#ifdef HAVE_OPENMP
# pragma omp parallel for
for (int n = 0; n < 20; ++n)
{
A a;
}
#else
std::vector<A> a (20);
#endif
}