======Programming - Build Systems - cmake - Notes======
[[https://cmake.org/cmake/help/latest/command/if.html|CMake Documentation - if]]
=====Using=====
If you see a CMakeLists.txt in the source tree, and there is no documentation on how to compile, the following ususally works relative to the directory with the CMakeLists.txt:
mkdir build
cd build
cmake ../
=====Adding include dirs when calling cmake=====
If cmake does not find certain include dirs on it's own, and you don't want to start editing cmake config files, you can expand the list of directories to search for include files when running cmake.
cmake -DCMAKE_CXX_FLAGS=-isystem\ /path/to/my/include
Source:[[https://stackoverflow.com/questions/25849571/adding-include-directories-to-cmake-when-calling-it-from-the-command-line|stack overflow - Adding include directories to CMake when calling it from the command line]]
Adding both C++ and C includes:
cmake -DCMAKE_C_FLAGS=-isystem\ /usr/X11R6/include -DCMAKE_CXX_FLAGS=-isystem\ /usr/local/include ../
=====Adding linker flags when calling cmake=====
For libraries: [[https://cmake.org/cmake/help/latest/variable/CMAKE_SHARED_LINKER_FLAGS.html|CMAKE_SHARED_LINKER_FLAGS]] \\
For executables: [[https://cmake.org/cmake/help/latest/variable/CMAKE_EXE_LINKER_FLAGS.html|CMAKE_EXE_LINKER_FLAGS]]
cmake -DCMAKE_SHARED_LINKER_FLAGS="-fstack-protector" -DCMAKE_EXE_LINKER_FLAGS="-fstack-protector" /path/to/source
Source: [[https://stackoverflow.com/questions/24532853/how-can-i-add-linker-flag-for-libraries-with-cmake|stack overlfow - How can I add linker flag for libraries with CMake?]]
Adding /usr/local/lib/ to the search path for the linker:
cmake -DCMAKE_SHARED_LINKER_FLAGS="-L/usr/local/lib" -DCMAKE_EXE_LINKER_FLAGS="-L/usr/local/lib" /path/to/source
=====Adding compiler flags when calling cmake=====
cmake -DCMAKE_C_FLAGS="-lm"
When compiling C++:
cmake -DCMAKE_CXX_FLAGS="-lm"
Source:[[https://stackoverflow.com/questions/11783932/how-do-i-add-a-linker-or-compile-flag-in-a-cmake-file|stack overflow - How do I add a linker or compile flag in a CMake file?]]
=====Setting the compiler to use when calling cmake=====
CC=egcc CXX=eg++ cmake ../
=====Detecting Linux distribution=====
Add to CMakeLists.txt:
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
execute_process(
COMMAND cat /etc/os-release
OUTPUT_VARIABLE OS_RELEASE_OUTPUT
ERROR_VARIABLE OS_RELEASE_ERROR
RESULT_VARIABLE OS_RELEASE_RESULT
)
if(NOT OS_RELEASE_RESULT EQUAL 0)
message(WARNING "Failed to get OS release information: ${OS_RELEASE_ERROR}")
else()
if(OS_RELEASE_OUTPUT MATCHES "ID=alpine")
add_compile_definitions(ALPINE)
endif()
endif()
endif()
Source:[[https://linuxvox.com/blog/cmake-detect-linux/|LinuxVox.com - CMake Detection of Linux: A Comprehensive Guide]]
=====Detecting OpenBSD=====
Add to CMakeLists.txt:
if (BSD STREQUAL "OpenBSD")
include_directories(/usr/local/include)
include_directories(/usr/local/include/opus)
endif()