如何在snapcraft中从创建二进制文件的“零件”中引用创建库的“零件”文件?

时间:2018-06-29 14:02:52

标签: snapcraft

我有一个快照,它将创建一个由多个二进制文件共享的库(在同一快照中)。构造卡扣部件以支持此功能的最佳方法是什么?

下面是我的出发点。

parts:
  libwhatever:
    plugin: cmake
    source: ...
    configflags: [
      ...
    ]
    build-packages:
      - liblog4cxx-dev
      - libboost-system-dev
      - libboost-filesystem-dev
      - libboost-thread-dev

  binwhatever:
    plugin: cmake
    source: binwhatever
    configflags: [
    ]
    build-packages:
      - liblog4cxx-dev
      - libboost-system-dev
      - libboost-filesystem-dev
      - libboost-test-dev
      - libboost-thread-dev
      - libgstreamer1.0-dev
      - #how can I reference libwhatever here?

1 个答案:

答案 0 :(得分:0)

可以做到这一点,但是鉴于工具的当前状态,它并不那么光滑。截至本文发布之日,有几件事情需要考虑:

1)Snap cmake插件不允许您在生成之前定义环境变量(snapcraftctl在运行之前丢弃环境)-除非您完全覆盖它。因此,以下解决方案采用了无需修改每个二进制文件cmake的环境即可进行操作的方法。

2)据我所知,snapcraft.yaml确实没有办法了解“ libwhatever”,因为在构建开始之时它就不存在。因此,snapcraft.yaml中每个二进制文件对libwhatever的所有引用都将被删除。

3)为使以下解决方案起作用,“ libwhatever”必须允许提供安装前缀。这使我们可以将其编译为/ usr而不是/ usr / local或/ opt或任何位置,并且在生成二进制文件时将发现“ libwhatever”。

snapcraft.yaml:

name: project-whatever
version: "6.0.0" 
summary: ""
description: |
  show how to use a common libwhatever among one or more binaries 
  in a snap
grade: devel
confinement: devmode

parts:
  libwhatever:
    plugin: cmake
    source: libwhatever
    configflags: [
      -DCMAKE_BUILD_TYPE=Debug,
      -DCMAKE_INSTALL_PREFIX=/usr
    ]
    build-packages:
      - liblog4cxx-dev
      - libboost-system-dev
      - libboost-filesystem-dev
      - libboost-test-dev
      - libboost-thread-dev
      - libgstreamer1.0-dev
      - libpthread-stubs0-dev
      - libglib2.0-dev
    stage-packages:
      - libc6
      - libgstreamer1.0-0
      - gstreamer1.0-plugins-base-apps
      - liblog4cxx10v5
      - libglib2.0-0

  binwhatever:
    after:
      - libwhatever
    plugin: cmake
    source: binwhatever
    configflags: [
      -DCMAKE_BUILD_TYPE=Debug,
      -DCMAKE_INSTALL_PREFIX=/opt/whatever
      ]
    build-packages:
      - liblog4cxx-dev
      - libboost-system-dev
      - libboost-filesystem-dev
      - libboost-test-dev
      - libboost-thread-dev
      - libboost-program-options-dev
      - libgstreamer1.0-dev
      - libgstreamer-plugins-base1.0-dev
    stage-packages:
      - libc6
      - libgstreamer1.0-0
      - gstreamer1.0-plugins-base-apps
      - liblog4cxx10v5
      - libboost-system1.65.1
      - libboost-filesystem1.65.1
      - libboost-thread1.65.1
      - libboost-program-options1.65.1

用于libwhat的CMakeLists.txt(如您所见,请尊重-DCMAKE_INSTALL_PREFIX的内容)

cmake_minimum_required(VERSION 3.10)

set(VERSION_MAJOR 1)
set(VERSION_MINOR 2)
set(VERSION_PATCH 0)

project(whatever VERSION "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}" DESCRIPTION "whatever")

set(CMAKE_CXX_FLAGS "-std=gnu++11")

include(FindPkgConfig)
include(GNUInstallDirs)

set(REQUIRED_LIBS gstreamer-1.0 liblog4cxx pthread-stubs glib-2.0)
pkg_check_modules(ALL REQUIRED ${REQUIRED_LIBS})
find_package(Boost REQUIRED COMPONENTS system filesystem regex thread)

add_library(whatever-shared SHARED whatever.cpp)

set_target_properties(whatever-shared PROPERTIES VERSION ${PROJECT_VERSION})
set_target_properties(whatever-shared PROPERTIES SOVERSION ${VERSION_MAJOR})
set_target_properties(whatever-shared PROPERTIES PUBLIC_HEADER whatever.h)

target_include_directories(whatever-shared PUBLIC ${ALL_INCLUDE_DIRS} PUBLIC ${Boost_INCLUDE_DIRS})
link_directories(whatever-shared PUBLIC ${ALL_LIBRARY_DIRS} PUBLIC ${Boost_LIBRARY_DIRS})
target_link_libraries(whatever-shared ${ALL_LIBRARIES} ${Boost_LIBRARIES})

function(JOIN OUTPUT GLUE)
  set(_TMP_RESULT "")
  set(_GLUE "") # effective glue is empty at the beginning
  foreach(arg ${ARGN})
    set(_TMP_RESULT "${_TMP_RESULT}${_GLUE}${arg}")
    set(_GLUE "${GLUE}")
  endforeach()
  set(${OUTPUT} "${_TMP_RESULT}" PARENT_SCOPE)
endfunction()

join(PKG_CONFIG_REQUIRED_LIBS_STR " " ${REQUIRED_LIBS})
set(PKG_CONFIG_LDFLAGS)
set(PKG_CONFIG_LDFLAGS_STR "-lboost_system -lboost_thread -lboost_regex -lboost_filesystem")
configure_file(libwhatever.pc.in libwhatever.pc @ONLY)

install(TARGETS whatever-shared
  RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
  LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
  PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})

install(FILES ${CMAKE_BINARY_DIR}/libwhatever.pc
  DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig)

libwhatever.pc.in:

prefix=@CMAKE_INSTALL_PREFIX@
exec_prefix=@CMAKE_INSTALL_PREFIX@
libdir=${exec_prefix}/@CMAKE_INSTALL_LIBDIR@
includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@

Name: @PROJECT_NAME@
Description: @PROJECT_DESCRIPTION@
Version: @PROJECT_VERSION@

Requires: @PKG_CONFIG_REQUIRED_LIBS_STR@
Libs: -L${libdir} -lwhatever @PKG_CONFIG_LDFLAGS_STR@
Cflags: -I${includedir} @PKG_CONFIG_CFLAGS_STR@

现在,每个二进制文件的每个CMakeLists.txt都将能够成功找到libwhatever:

pkg_check_modules(ALL REQUIRED libwhatever gstreamer-video-1.0 gstreamer-app-1.0)