introducing-cmake/starting-from-today.tex

235 lines
5.7 KiB
TeX

\begin{frame}
\frametitle{Starting from today}
\begin{center}
\huge Things you can start doing\newline Right Now™
\end{center}
\end{frame}
% --------------------------------------------------
\begin{frame}[containsverbatim]
\frametitle{Use targets in place of variables}
\begin{codebox}[minted options={fontsize=\scriptsize}]{CMake}{}
find_package(GTest REQUIRED)
find_package(Boost REQUIRED COMPONENTS system thread)
find_package(EXPAT REQUIRED) # only vars
add_library(alexandria …)
\end{codebox}
\begin{columns}
\begin{column}{.5\textwidth}
\begin{badcodebox}[equal height group=targets1]{CMake}
target_link_libraries(alexandria
PRIVATE
${BOOST_LIBRARIES}
${EXPAT_LIBRARIES}
${GTEST_MAIN_LIBRARIES})
\end{badcodebox}
\end{column}
\begin{column}{.5\textwidth}
\begin{goodcodebox}[equal height group=targets1]{CMake}
target_link_libraries(alexandria
PRIVATE
Boost::system
Boost::thread
GTest::Main
${EXPAT_LIBRARIES}) # legacy
\end{goodcodebox}
\end{column}
\end{columns}
\end{frame}
% --------------------------------------------------
\begin{frame}[containsverbatim]
\frametitle{Use target-specific commands\footnote{An exception is of
course in toolchain files.}}
Some commands propagate to all subfolders and included files. This
can lead to potential problems where some targets get extra
unintended flags.
\vspace{.3em}
\begin{columns}
\begin{column}{.5\textwidth}
\begin{badcodebox}[equal height group=tgtspecific1]{CMake}
add_definitions(-DDEFINE_SOMETHING=1)
add_compile_options(-fvisibility=hidden)
set(CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS} -fno-exceptions")
\end{badcodebox}
\end{column}
\begin{column}{.5\textwidth}
\begin{goodcodebox}[equal height group=tgtspecific1]{CMake}
target_compile_definitions(tgt PRIVATE
DEFINE_SOMETHING=1)
target_compile_options(tgt PRIVATE
"-fno-tree-vrp")
\end{goodcodebox}
\end{column}
\end{columns}
\end{frame}
% --------------------------------------------------
\begin{frame}[containsverbatim]
\frametitle{Avoid \texttt{target\_include\_directories}}
\begin{codebox}[minted options={fontsize=\scriptsize}]{CMake}{}
find_package(EXPAT REQUIRED) # external, has only vars
find_package(Boost REQUIRED) # external, has targets
add_library(alexandria egypt.cc)
target_link_libraries(alexandria
PUBLIC calliope clio Boost::filesystem
PRIVATE euterpe erato ${EXPAT_LIBRARIES})
\end{codebox}
\vspace{.2em}
\begin{columns}
\begin{column}{.5\textwidth}
\begin{badcodebox}[equal height group=tgtincdir1]{CMake}
target_include_directories(alexandria
PUBLIC ${PROJECT_SOURCE_DIR}/include
$<TARGET_PROPERTY:calliope,#
INTERFACE_INCLUDE_DIRECTORIES>
PRIVATE ${EUTERPE_INCLUDE_DIRS} # ...
${CMAKE_SOURCE_DIR}/calliope/inc
${BOOST_INCLUDE_DIRS}
${EXPAT_INCLUDE_DIRS})
\end{badcodebox}
\end{column}
\begin{column}{.5\textwidth}
\begin{goodcodebox}[equal height group=tgtincdir1]{CMake}
target_include_directories(alexandria
PUBLIC $<BUILD_INTERFACE:#
${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:#
${include_install_rel_dir}>
PRIVATE ${EXPAT_INCLUDE_DIRS})
\end{goodcodebox}
\end{column}
\end{columns}
\end{frame}
% --------------------------------------------------
\begin{frame}[containsverbatim]
\frametitle{Use PRIVATE and PUBLIC}
\begin{columns}
\begin{column}{.5\textwidth}
\begin{badcodebox}[equal height group=privpub1]{CMake}
target_link_libraries(tgt
libA
libB
libC)
target_compile_definitions(tgt
HAVE_THINGIE)
\end{badcodebox}
\end{column}
\begin{column}{.5\textwidth}
\begin{goodcodebox}[equal height group=privpub1]{CMake}
target_link_libraries(tgt
PRIVATE libA
libB
PUBLIC libC)
target_compile_definitions(tgt
PRIVATE HAVE_THINGIE)
\end{goodcodebox}
\end{column}
\end{columns}
\end{frame}
% --------------------------------------------------
\begin{frame}[containsverbatim]
\frametitle{Prefer feature checks}
\begin{columns}
\begin{column}{.5\textwidth}
\begin{badcodebox}[equal height group=feat1]{CMake}
if(APPLE OR IOS OR (UNIX AND NOT ANDROID))
target_compile_definitions(lua PRIVATE
HAVE_POSIX_SPAWN)
endif()
\end{badcodebox}
\end{column}
\begin{column}{.5\textwidth}
\begin{goodcodebox}[equal height group=feat1]{CMake}
check_symbol_exists("posix_spawn" "spawn.h"
has_posix_spawn)
if(has_posix_spawn)
target_compile_definitions(lua PRIVATE
HAVE_POSIX_SPAWN)
endif()
\end{goodcodebox}
\end{column}
\end{columns}
\end{frame}
% --------------------------------------------------
\begin{frame}[containsverbatim]
\frametitle{Make your package relocatable}
\begin{columns}
\begin{column}{.5\textwidth}
\begin{badcodebox}[equal height group=relocatable1]{CMake}
install(FILES "example.conf"
DESTINATION some/strange/folder)
\end{badcodebox}
\begin{badcodebox}[equal height group=relocatable2]{C++}
char buf[2048];
getcwd(buf, 2048);
using boost::filesystem::path;
path rez = path(buf) /
HARDCODED_ABSOLUTE_PREFIX_PATH
/ "some" / "strange" / "folder" /
"example.conf";
\end{badcodebox}
\end{column}
\begin{column}{.5\textwidth}
\begin{goodcodebox}[equal height group=relocatable1]{CMake}
include(GNUInstallDirs)
install(RESOURCES "example.conf"
DESTINATION
${CMAKE_INSTALL_SYSCONFDIR}/appname)
\end{goodcodebox}
\begin{goodcodebox}[equal height group=relocatable2]{C++}
using your_ns::some_enum::ResourceType;
using boost::filesystem::path;
path = your_ns::get_resource_path(
ResourceType::CONFIG,
{ "appname", "example.conf" });
\end{goodcodebox}
\end{column}
\end{columns}
\end{frame}
% --------------------------------------------------