From 2d2ddde88dc05931d9593c29c92ff876dd5de677 Mon Sep 17 00:00:00 2001 From: Axel Isouard Date: Thu, 9 Feb 2017 21:13:36 +0100 Subject: [PATCH] Enhance pkg-config generation, state it inside README.md --- CMakeModules/FindLibraries.cmake | 2 +- CMakeModules/Install.cmake | 33 ++++++++++++++++++++++++++ CMakeModules/TargetOsAndCpu.cmake | 9 +++++-- CMakeModules/Templates/LibWebRTC.pc.in | 2 +- README.md | 13 ++++++++++ 5 files changed, 55 insertions(+), 4 deletions(-) diff --git a/CMakeModules/FindLibraries.cmake b/CMakeModules/FindLibraries.cmake index 6e6042f..7ef0f07 100644 --- a/CMakeModules/FindLibraries.cmake +++ b/CMakeModules/FindLibraries.cmake @@ -5,7 +5,7 @@ find_package(Git REQUIRED) find_package(DepotTools REQUIRED) find_package(PythonInterp REQUIRED) -list(APPEND LIBWEBRTC_LIBRARIES webrtc Threads::Threads) +list(APPEND LIBWEBRTC_LIBRARIES webrtc) if (UNIX AND NOT APPLE) find_package(X11 REQUIRED) diff --git a/CMakeModules/Install.cmake b/CMakeModules/Install.cmake index 756810f..d8071ee 100644 --- a/CMakeModules/Install.cmake +++ b/CMakeModules/Install.cmake @@ -34,6 +34,39 @@ install(FILES ${CMAKE_BINARY_DIR}/LibWebRTCConfigVersion.cmake # # Install pkg-config file if (UNIX) + set(prefix "${CMAKE_INSTALL_PREFIX}") + set(exec_prefix "\${prefix}") + set(libdir "${INSTALL_LIB_DIR}") + set(includedir "${INSTALL_INCLUDE_DIR}") + + set(LIBWEBRTC_PC_LIBS "-L${INSTALL_LIB_DIR}" "-lwebrtc") + foreach(LIB_NAME ${LIBWEBRTC_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}) + if (LIB_NAME MATCHES "[\\/]") + get_filename_component(LIB_DIR "${LIB_NAME}" PATH) + get_filename_component(LIB_NAME "${LIB_NAME}" NAME_WE) + string(REGEX REPLACE "^lib(.*)" "-l\\1" LIB_NAME "${LIB_NAME}") + + if (NOT ${LIB_DIR} IN_LIST LIB_DIRS) + list(APPEND LIB_DIRS ${LIB_DIR}) + list(APPEND LIBWEBRTC_PC_LIBS_PRIVATE "-L${LIB_DIR}") + endif (NOT ${LIB_DIR} IN_LIST LIB_DIRS) + + elseif (NOT LIB_NAME MATCHES "^-l") + set(LIB_NAME "-l${LIB_NAME}") + endif () + list(APPEND LIBWEBRTC_PC_LIBS_PRIVATE "${LIB_NAME}") + endforeach(LIB_NAME ${LIBWEBRTC_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}) + + foreach(DEFINITION ${LIBWEBRTC_DEFINITIONS}) + list(APPEND LIBWEBRTC_PC_DEFINITIONS "-D${DEFINITION}") + endforeach(DEFINITION ${LIBWEBRTC_DEFINITIONS}) + + list(REMOVE_ITEM LIBWEBRTC_PC_LIBS_PRIVATE "-lwebrtc") + string(REPLACE ";" " " LIBWEBRTC_PC_DEFINITIONS "${LIBWEBRTC_PC_DEFINITIONS}") + string(REPLACE ";" " " LIBWEBRTC_PC_LIBS "${LIBWEBRTC_PC_LIBS}") + string(REPLACE ";" " " LIBWEBRTC_PC_LIBS_PRIVATE "${LIBWEBRTC_PC_LIBS_PRIVATE}") + string(REPLACE ";" " " LIBWEBRTC_PC_CXXFLAGS "${LIBWEBRTC_REQUIRED_CXX_FLAGS}") + configure_file(${CMAKE_MODULE_PATH}/Templates/LibWebRTC.pc.in ${CMAKE_BINARY_DIR}/LibWebRTC.pc @ONLY) install(FILES ${CMAKE_BINARY_DIR}/LibWebRTC.pc diff --git a/CMakeModules/TargetOsAndCpu.cmake b/CMakeModules/TargetOsAndCpu.cmake index a98eff9..3ab292b 100644 --- a/CMakeModules/TargetOsAndCpu.cmake +++ b/CMakeModules/TargetOsAndCpu.cmake @@ -66,10 +66,15 @@ endif (NOT ${TARGET_CPU} IN_LIST TARGET_CPU_LIST) if (UNIX) if (ARCH_X86) - set(LIBWEBRTC_REQUIRED_CXX_FLAGS "-m32") + list(APPEND LIBWEBRTC_REQUIRED_CXX_FLAGS "-m32") endif (ARCH_X86) - set(LIBWEBRTC_REQUIRED_CXX_FLAGS "${LIBWEBRTC_REQUIRED_CXX_FLAGS} -std=gnu++0x") + list(APPEND LIBWEBRTC_REQUIRED_CXX_FLAGS "-std=gnu++0x") + + if(THREADS_HAVE_PTHREAD_ARG) + list(APPEND LIBWEBRTC_REQUIRED_CXX_FLAGS "-pthread") + endif() + list(APPEND LIBWEBRTC_DEFINITIONS WEBRTC_POSIX _GLIBCXX_USE_CXX11_ABI=0 _CRT_SECURE_NO_WARNINGS) elseif (WIN32) diff --git a/CMakeModules/Templates/LibWebRTC.pc.in b/CMakeModules/Templates/LibWebRTC.pc.in index 464794c..0eb6249 100644 --- a/CMakeModules/Templates/LibWebRTC.pc.in +++ b/CMakeModules/Templates/LibWebRTC.pc.in @@ -8,4 +8,4 @@ Description: Google's native WebRTC implementation shipped into a single library Version: @LIBWEBRTC_VERSION@ Libs: @LIBWEBRTC_PC_LIBS@ Libs.private: @LIBWEBRTC_PC_LIBS_PRIVATE@ -Cflags: -I${includedir} \ No newline at end of file +Cflags: -I${includedir} @LIBWEBRTC_PC_DEFINITIONS@ @LIBWEBRTC_PC_CXXFLAGS@ \ No newline at end of file diff --git a/README.md b/README.md index 0566ae5..035e65b 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,12 @@ add_executable(sample ${SOURCE_FILES}) target_link_libraries(sample ${LIBWEBRTC_LIBRARIES}) ``` +It also produces a `pkg-config` file if you prefer the classic way: + +``` +$ g++ `pkg-config --cflags LibWebRTC` main.cpp -o main `pkg-config --libs LibWebRTC` +``` + ## Status The following table displays the current state of this project, including @@ -133,6 +139,13 @@ include(${LIBWEBRTC_USE_FILE}) target_link_libraries(my-app ${LIBWEBRTC_LIBRARIES}) ``` +A pkg-config file is also provided, you can obtain the required compiler and +linker flags by specifying `LibWebRTC` as the package name. + +``` +$ pkg-config --cflags --libs LibWebRTC +``` + ## Configuration The library will be compiled and usable on the same host's platform and