From 4a1a936e64ddc0365d59579ee38e8804e2fa7e21 Mon Sep 17 00:00:00 2001 From: Andrey Astafyev Date: Sat, 5 Oct 2019 08:17:36 +0300 Subject: [PATCH] CMake and pkg-config support --- CMakeLists.txt | 109 +++++++++++++++++++++++++++++++++++++ pkgconfig/whereami.pc.in | 13 +++++ pkgconfig/whereamipp.pc.in | 13 +++++ 3 files changed, 135 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 pkgconfig/whereami.pc.in create mode 100644 pkgconfig/whereamipp.pc.in diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..e59eeec --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,109 @@ +cmake_minimum_required(VERSION 3.1) +project(whereami VERSION 1.0.0 LANGUAGES C CXX) +include(GNUInstallDirs) + +option(BUILD_SHARED_LIBS "Build shared libs" ON) +option(BUILD_STATIC_LIBS "Build static libs" ON) +option(BUILD_EXAMPLES "Build examples" ON) + +find_package(PkgConfig) + +# whereami +set(CMAKE_C_STANDARD 99) +add_library(whereami OBJECT ${CMAKE_SOURCE_DIR}/src/whereami.c) +target_include_directories(whereami + PUBLIC $ + $) +set_target_properties(whereami PROPERTIES POSITION_INDEPENDENT_CODE ON) + +if(BUILD_SHARED_LIBS) + add_library(whereami_shared SHARED $) + set_target_properties(whereami_shared + PROPERTIES VERSION ${PROJECT_VERSION} + SOVERSION ${PROJECT_VERSION_MAJOR} + OUTPUT_NAME whereami + LIBRARY_OUTPUT_DIRECTORY ${CMAKE_INSTALL_LIBDIR}) + install(TARGETS whereami_shared LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) +endif() + +if(BUILD_STATIC_LIBS) + add_library(whereami_static STATIC $) + set_target_properties(whereami_static + PROPERTIES OUTPUT_NAME whereami + ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_INSTALL_LIBDIR}) + install(TARGETS whereami_static ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) +endif() + +if(BUILD_EXAMPLES) + add_executable(executable ${CMAKE_SOURCE_DIR}/example/executable.c + ${CMAKE_SOURCE_DIR}/src/whereami.c) + target_include_directories(executable PUBLIC $) + if(CMAKE_DL_LIBS) + target_link_libraries(executable ${CMAKE_DL_LIBS}) + endif() + + add_library(library SHARED ${CMAKE_SOURCE_DIR}/example/library.c + ${CMAKE_SOURCE_DIR}/src/whereami.c) + target_include_directories(library PUBLIC $) + if(CMAKE_DL_LIBS) + target_link_libraries(library ${CMAKE_DL_LIBS}) + endif() +endif() + +if(PKG_CONFIG_FOUND) + configure_file(${CMAKE_SOURCE_DIR}/pkgconfig/whereami.pc.in ${CMAKE_BINARY_DIR}/whereami.pc) + install(FILES ${CMAKE_BINARY_DIR}/whereami.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig) +endif() + + +# whereamipp +if(CMAKE_CXX_COMPILER) + set(CMAKE_CXX_STANDARD 11) + add_library(whereamipp OBJECT ${CMAKE_SOURCE_DIR}/src/whereami++.cpp) + target_include_directories(whereamipp + PUBLIC $ + $) + set_target_properties(whereamipp PROPERTIES POSITION_INDEPENDENT_CODE ON) + + if(BUILD_SHARED_LIBS) + add_library(whereamipp_shared SHARED $) + set_target_properties(whereamipp_shared + PROPERTIES VERSION ${PROJECT_VERSION} + SOVERSION ${PROJECT_VERSION_MAJOR} + OUTPUT_NAME whereamipp + LIBRARY_OUTPUT_DIRECTORY ${CMAKE_INSTALL_LIBDIR}) + install(TARGETS whereamipp_shared LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) + endif() + + if(BUILD_STATIC_LIBS) + add_library(whereamipp_static STATIC $) + set_target_properties(whereamipp_static + PROPERTIES OUTPUT_NAME whereamipp + ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_INSTALL_LIBDIR}) + install(TARGETS whereamipp_static ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) + endif() + + if(BUILD_EXAMPLES) + add_executable(executable-as-cxx ${CMAKE_SOURCE_DIR}/example/executable.cpp + ${CMAKE_SOURCE_DIR}/src/whereami++.cpp + ${CMAKE_SOURCE_DIR}/src/whereami.c) + target_include_directories(executable-as-cxx PUBLIC $) + if(CMAKE_DL_LIBS) + target_link_libraries(executable-as-cxx ${CMAKE_DL_LIBS}) + endif() + + add_library(library-as-cxx SHARED ${CMAKE_SOURCE_DIR}/example/library.cpp + ${CMAKE_SOURCE_DIR}/src/whereami++.cpp + ${CMAKE_SOURCE_DIR}/src/whereami.c) + target_include_directories(library-as-cxx PUBLIC $) + if(CMAKE_DL_LIBS) + target_link_libraries(library-as-cxx ${CMAKE_DL_LIBS}) + endif() + + endif() + + if(PKG_CONFIG_FOUND) + configure_file(${CMAKE_SOURCE_DIR}/pkgconfig/whereamipp.pc.in ${CMAKE_BINARY_DIR}/whereamipp.pc) + install(FILES ${CMAKE_BINARY_DIR}/whereamipp.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig) + endif() +endif() diff --git a/pkgconfig/whereami.pc.in b/pkgconfig/whereami.pc.in new file mode 100644 index 0000000..c6e02f3 --- /dev/null +++ b/pkgconfig/whereami.pc.in @@ -0,0 +1,13 @@ +prefix=@CMAKE_INSTALL_PREFIX@ +exec_prefix=@CMAKE_INSTALL_PREFIX@ +libdir=@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_LIBDIR@ +includedir=@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_INCLUDEDIR@ + +Name: whereami +Description: Whereami C library +Version: @PROJECT_VERSION@ + +Requires: +Libs: -L@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_LIBDIR@ -lwhereami +Cflags: -I@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_INCLUDEDIR@ + diff --git a/pkgconfig/whereamipp.pc.in b/pkgconfig/whereamipp.pc.in new file mode 100644 index 0000000..b301720 --- /dev/null +++ b/pkgconfig/whereamipp.pc.in @@ -0,0 +1,13 @@ +prefix=@CMAKE_INSTALL_PREFIX@ +exec_prefix=@CMAKE_INSTALL_PREFIX@ +libdir=@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_LIBDIR@ +includedir=@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_INCLUDEDIR@ + +Name: whereamipp +Description: Whereami library C++ bindings +Version: @PROJECT_VERSION@ + +Requires: +Libs: -L@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_LIBDIR@ -lwhereamipp -whereami +Cflags: -I@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_INCLUDEDIR@ +