背景介绍:
有一个工程,其目录结构是:
libB
-- libA
-- src
-- packaging
目录libA通过scons编译生成libA.so,目录src中的源代码通过 CMAKE编译生成libB.so, 且libB.so 依赖 libA.so. 另外有个应用程序 appC,依赖 libB.so。请问如何libB如何打包成rpm。libB.rpm
方案一:
1) 先只编译LibA,生成的libA.so在libA/libA.so,然后在libB的CMAKE文件中链接LibA.so, 并用FILES安装LibA.so.
libB.spec:
Name: libB
Version: 0.1.0
Release: 1
License: TO BE / FILLED IN
Group: TO BE / FILLED IN
Source0: %{name}-%{version}.tar.gz
Summary: LibB
BuildRequires: cmake
BuildRequires: scons
%description
LibB
%package devel
Summary: Include Files and Libraries mandatory for Development
Group: TO BE / FILED IN
Requires: %{name} = %{version}-%{release}
%description devel
This package contains all necessary include files and libraries needed
to develop applications that require these.
%prep
%setup -q
%build
pushd .
cd libA
scons --prefix=%{_prefix}
popd
cmake . -DCMAKE_INSTALL_PREFIX=%{_prefix}
make %{?jobs:-j%jobs}
%install
rm -rf %{buildroot}
%make_install
%clean
rm -rf %{buildroot}
%post
%postun
%files
%defattr(-,root,root,-)
%{_libdir}/libA.so
%{_libdir}/LibB.so
%files devel
%defattr(-,root,root,-)
%{_includedir}/*
%{_libdir}/pkgconfig/%{name}.pc
%{_libdir}/pkgconfig/libA.pc
LibB CMAKELists.txt:
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
PROJECT(libB)
ADD_LIBRARY(${PROJECT_NAME} SHARED src/libB.c)
# 寻找libA.so
FIND_LIBRARY(LibA NAME A PATHS ${CMAKE_SOURCE_DIR}/libA DOC "LibA library")
# 链接libA.so
TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${PKGS_LDFLAGS} ${LibA})
# 安装libA.so
INSTALL(FILES ${CMAKE_SOURCE_DIR}/libA/libA.so DESTINATION ${LIBDIR})
# 安装libB.so
INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${LIBDIR})
2)编译appC
编译出错:
appC: nothing provides libA.so needed by libB
原因分析:明明libB.rpm里已经包括了libA.so 和libB.so, 为什么说没有提供libA,so?
我们解开rpm看看:
rpm2cpio libB.rpm | cpio -div
cd usr/lib
ls -l
-rwxr-xr-x 1 root root 1637738 19 18:27 libB.so
-rw-r--r-- 1 root root 1674522 19 18:27 libA.so
libA.so的可执行权限没有了。
原因是 以FILES安装的文件会去除可执行权限
INSTALL(FILES ${CMAKE_SOURCE_DIR}/libA/libA.so DESTINATION ${LIBDIR})
解决方案2:
1)编译安装libA都通过scons。
libB.spec:
Name: libB
Version: 0.1.0
Release: 1
License: TO BE / FILLED IN
Group: TO BE / FILLED IN
Source0: %{name}-%{version}.tar.gz
Summary: LibB
BuildRequires: cmake
BuildRequires: scons
%description
LibB
%package devel
Summary: Include Files and Libraries mandatory for Development
Group: TO BE / FILED IN
Requires: %{name} = %{version}-%{release}
%description devel
This package contains all necessary include files and libraries needed
to develop applications that require these.
%prep
%setup -q
%build
pushd .
cd libA
scons --prefix=%{_prefix}
popd
cmake . -DCMAKE_INSTALL_PREFIX=%{_prefix}
make %{?jobs:-j%jobs}
%install
rm -rf %{buildroot}
pushd .
cd libA
scons --prefix=%{_prefix} --libdir=%{_libdir} --install-sandbox=%{buildroot} install
popd
%make_install
%clean
rm -rf %{buildroot}
%post
%postun
%files
%defattr(-,root,root,-)
%{_libdir}/libA.so
%{_libdir}/LibB.so
%files devel
%defattr(-,root,root,-)
%{_includedir}/*
%{_libdir}/pkgconfig/%{name}.pc
%{_libdir}/pkgconfig/libA.pc
LibB CMAKELists.txt: 去除安装libA.so步骤。
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
PROJECT(libB)
ADD_LIBRARY(${PROJECT_NAME} SHARED src/libB.c)
# 寻找libA.so
FIND_LIBRARY(LibA NAME A PATHS ${CMAKE_SOURCE_DIR}/libA DOC "LibA library")
# 链接libA.so
TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${PKGS_LDFLAGS} ${LibA})
# 安装libA.so
#INSTALL(FILES ${CMAKE_SOURCE_DIR}/libA/libA.so DESTINATION ${LIBDIR})
# 安装libB.so
INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${LIBDIR})
2)编译appC成功。