Created
October 14, 2019 07:04
-
-
Save rene-d/24dba2fc67ffb98ea49bd343256d2960 to your computer and use it in GitHub Desktop.
cmake best practices
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| cmake_minimum_required(VERSION 3.7) | |
| set(CMAKE_EXPORT_COMPILE_COMMANDS ON) | |
| set(CMAKE_VERBOSE_MAKEFILE ON) | |
| string(TOLOWER "${CMAKE_BUILD_TYPE}" cmake_build_type) | |
| message("AAA CMAKE_BUILD_TYPE = ${CMAKE_BUILD_TYPE} ${cmake_build_type}") | |
| if (cmake_build_type STREQUAL "debug") | |
| message("AAA build type is Debug") | |
| elseif(cmake_build_type STREQUAL "release") | |
| message("AAA build type is Release") | |
| elseif(cmake_build_type STREQUAL "minsizerel") | |
| message("AAA build type is MinSizeRel") | |
| elseif(cmake_build_type STREQUAL "relwithdebinfo") | |
| message("AAA build type is RelWithDebInfo") | |
| else() | |
| message("AAA build type is unknown") | |
| endif() | |
| #set(CMAKE_C_STANDARD) | |
| #set(CMAKE_CXX_STANDARD) | |
| # flags utilisés pour tous les fichiers selon le langage | |
| #set(CMAKE_<LANG>_FLAGS) | |
| # flags ajoutés pour tous les fichiers selon le langage par configuration (Debug/Release/MinSizeRel/RelWithDebInfo) | |
| #set(CMAKE_<LANG>_FLAGS_<CONFIG>) | |
| # flags utilisés pour linker shared | |
| #set(CMAKE_SHARED_LINKER_FLAGS) | |
| #set(CMAKE_SHARED_LINKER_FLAGS_<CONFIG>) | |
| # flags utilisés pour linker static | |
| #set(CMAKE_STATIC_LINKER_FLAGS) | |
| #set(CMAKE_STATIC_LINKER_FLAGS_<CONFIG>) | |
| # flags utilisés pour linker les exécutables | |
| #set(CMAKE_EXE_LINKER_FLAGS) | |
| #set(CMAKE_EXE_LINKER_FLAGS_<CONFIG>) | |
| # modification éditable avec cmake gui (ccmake .) ou dans CMakeCache.txt | |
| set(CMAKE_CXX_FLAGS "-DAAA_EDIT" CACHE STRING "editable compile flags" FORCE) | |
| set(CMAKE_CXX_FLAGS "-DAAA_ALWAYS ${CMAKE_CXX_FLAGS}") | |
| set(CMAKE_CXX_FLAGS_DEBUG "-DAAA_DEBUG ${CMAKE_CXX_FLAGS_DEBUG}") | |
| set(CMAKE_CXX_FLAGS_RELEASE "-DAAA_RELEASE ${CMAKE_CXX_FLAGS_RELEASE}") | |
| include(FindPkgConfig) | |
| function(PrintPkg) | |
| #pkg_check_modules(ZZ REQUIRED IMPORTED_TARGET ${ARGN}) | |
| pkg_check_modules(ZZ REQUIRED ${ARGN}) | |
| message(STATUS "PACKAGE: ${ARGN}") | |
| message(STATUS " _FOUND : ${ZZ_FOUND}") | |
| message("") | |
| message(STATUS " _LIBRARIES : ${ZZ_LIBRARIES}") | |
| message(STATUS " _LINK_LIBRARIES : ${ZZ_LINK_LIBRARIES}") | |
| message(STATUS " _LIBRARY_DIRS : ${ZZ_LIBRARY_DIRS}") | |
| message(STATUS " _LDFLAGS : ${ZZ_LDFLAGS}") | |
| message(STATUS " _LDFLAGS_OTHER : ${ZZ_LDFLAGS_OTHER}") | |
| message(STATUS " _INCLUDE_DIRS : ${ZZ_INCLUDE_DIRS}") | |
| message(STATUS " _CFLAGS : ${ZZ_CFLAGS}") | |
| message(STATUS " _CFLAGS_OTHER : ${ZZ_CFLAGS_OTHER}") | |
| message("") | |
| message(STATUS " _STATIC_LIBRARIES : ${ZZ_STATIC_LIBRARIES}") | |
| message(STATUS " _STATIC_LINK_LIBRARIES : ${ZZ_STATIC_LINK_LIBRARIES}") | |
| message(STATUS " _STATIC_LIBRARY_DIRS : ${ZZ_STATIC_LIBRARY_DIRS}") | |
| message(STATUS " _STATIC_LDFLAGS : ${ZZ_STATIC_LDFLAGS}") | |
| message(STATUS " _STATIC_LDFLAGS_OTHER : ${ZZ_STATIC_LDFLAGS_OTHER}") | |
| message(STATUS " _STATIC_INCLUDE_DIRS : ${ZZ_STATIC_INCLUDE_DIRS}") | |
| message(STATUS " _STATIC_CFLAGS : ${ZZ_STATIC_CFLAGS}") | |
| message(STATUS " _STATIC_CFLAGS_OTHER : ${ZZ_STATIC_CFLAGS_OTHER}") | |
| message("") | |
| endfunction() | |
| PrintPkg(glib-2.0) | |
| PrintPkg(jansson) | |
| function (create_lib _prefix _imp_target_global) | |
| if(${_imp_target_global}) | |
| set(_global_opt "GLOBAL") | |
| else() | |
| unset(_global_opt) | |
| endif() | |
| add_library(PkgConfig::${_prefix} INTERFACE IMPORTED ${_global_opt}) | |
| # propriétés liées à la commpilation | |
| # - INCLUDE_DIRS + CFLAGS_OTHER | |
| # - CFLAGS | |
| if(${_prefix}_INCLUDE_DIRS) | |
| set_property(TARGET PkgConfig::${_prefix} PROPERTY | |
| INTERFACE_INCLUDE_DIRECTORIES "${${_prefix}_INCLUDE_DIRS}") | |
| endif() | |
| if(${_prefix}_CFLAGS_OTHER) | |
| set_property(TARGET PkgConfig::${_prefix} PROPERTY | |
| INTERFACE_COMPILE_OPTIONS "${${_prefix}_CFLAGS_OTHER}") | |
| endif() | |
| # propriétés liées à l'édition de lien | |
| # - LDFLAGS | |
| # - LIBRARIES ou LINK_LIBRARIES + LD_FLAGS_OTHER | |
| if(${_prefix}_LINK_LIBRARIES) | |
| set_property(TARGET PkgConfig::${_prefix} PROPERTY | |
| INTERFACE_LINK_LIBRARIES "${${_prefix}_LINK_LIBRARIES}") | |
| elseif(${_prefix}_LIBRARIES) | |
| # nota: INTERFACE_LINK_LIBRARIES est placé en fin de commande | |
| set_property(TARGET PkgConfig::${_prefix} PROPERTY | |
| INTERFACE_LINK_LIBRARIES "${${_prefix}_LIBRARIES}") | |
| # équivalent: target_link_libraries(PkgConfig::${_prefix} INTERFACE ...) | |
| endif() | |
| if(${_prefix}_LDFLAGS_OTHER) | |
| # nota: INTERFACE_LINK_OPTIONS est placé en début de commande | |
| set_property(TARGET PkgConfig::${_prefix} PROPERTY | |
| INTERFACE_LINK_OPTIONS "${${_prefix}_LDFLAGS_OTHER}") | |
| # équivalent: target_link_options(PkgConfig::${_prefix} INTERFACE ...) | |
| endif() | |
| endfunction() | |
| PrintPkg(glib-2.0) | |
| create_lib(ZZ_STATIC ON) | |
| pkg_check_modules(GLIB REQUIRED IMPORTED_TARGET glib-2.0) | |
| add_executable(main main.cpp) | |
| target_link_libraries(main PkgConfig::ZZ_STATIC) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| version: '3' | |
| services: | |
| code: | |
| build: . | |
| volumes: | |
| - .:/code | |
| environment: | |
| COMPOSE_ENV: 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| FROM alpine:3.10 | |
| ARG UID=1000 | |
| COPY repositories /etc/apk/repositories | |
| RUN apk update \ | |
| && apk add --no-cache \ | |
| gcc g++ valgrind gdb pkgconfig cmake make perl bash curl jq \ | |
| musl-dev glib-static glib-dev jansson-dev gtest-dev | |
| RUN curl -sL https://github.com/linux-test-project/lcov/releases/download/v1.14/lcov-1.14.tar.gz | tar -C /tmp -xzf - \ | |
| && cd /tmp/lcov-1.14 \ | |
| && make install PREFIX=/usr \ | |
| && rm -rf /tmp/lcov-1.14 | |
| VOLUME /code | |
| WORKDIR /build | |
| RUN adduser -D -H demo -u ${UID} -g 'demo user' \ | |
| && chown -c demo:demo /build | |
| USER demo |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <glib.h> | |
| #include <iostream> | |
| using namespace std; | |
| int main() | |
| { | |
| std::cout << "coucou" << std::endl; | |
| //std::cout << glib_check_version(GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION) << std::endl; | |
| cout << glib_major_version << endl; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| http://dl-cdn.alpinelinux.org/alpine/v3.10/main | |
| http://dl-cdn.alpinelinux.org/alpine/v3.10/community |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /bin/sh | |
| c() | |
| { | |
| if [ "$1" == "" ]; then | |
| echo "WITHOUT CMAKE_BUILD_TYPE" | |
| (rm -rf * ; cmake /code) 2>&1 | grep -- "AAA" | |
| else | |
| echo "WITH CMAKE_BUILD_TYPE=$1" | |
| (rm -rf * ; cmake /code -DCMAKE_BUILD_TYPE=$1) 2>&1 | grep -- "AAA" | |
| fi | |
| jq '.[0].command' compile_commands.json | |
| } | |
| c $1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment