Skip to content

Commit

Permalink
Improve build system
Browse files Browse the repository at this point in the history
Do initial "full" and "standard" build system

I have too little experience with cmake and so for sure other
improvements are possible

I updated also README, github workflow and gitignore

I also improved the debian packaging to make more standard thank to the
"complete" build system

I also did a fix to lib/blksnap/Session.cpp and tools/blksnap/main.cpp
for build failed spotted creating deb packages with all hardening
enabled, I not sure that file permission setted are correct/optimal

Need also to have better build of only part and not all (blksnap-dev
and/or blksnap-tools and/or blksnap-tests) for now is possible with
blksnap-dev and blksnap-tools targets but don't seems optimal

On circleci tests cmake uninstall file was not found even if I not
understand why, manually with only "cmake ." and also generating debian
packages (same of circleci tests) is ok.
I did some try without find a solution so I added an exists check for
don't fail in similar case

Make install and uninstall print wrong text related to tests files don't
installed or uninstalled as not found but checking them was installed
and uninstalled correctly, also for this I didn't found a solution
  • Loading branch information
Fantu committed Dec 27, 2022
1 parent 355767e commit f4b6f0c
Show file tree
Hide file tree
Showing 14 changed files with 140 additions and 228 deletions.
24 changes: 3 additions & 21 deletions .github/workflows/Linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,8 @@ jobs:
cd module
./mk.sh build
- name: Build blksnap-tools
working-directory: tools/blksnap
- name: Build blksnap-dev, blksnap-tools and blksnap-tests
working-directory: .
run: |
mkdir bin
cd bin
cmake ..
make
- name: Build blksnap-dev
working-directory: lib/blksnap
run: |
mkdir bin
cd bin
cmake ..
make
- name: Build blksnap-tests
working-directory: tests/cpp
run: |
mkdir bin
cd bin
cmake ..
cmake .
make
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
!.clang-format
!.circleci
cmake*
CMakeCache.txt
CMakeFiles
Makefile
obj-*-linux-gnu
debian/
/build/
/lib/blksnap/bin/
/tools/blksnap/bin/
Expand Down
17 changes: 17 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# SPDX-License-Identifier: GPL-2.0+

cmake_minimum_required(VERSION 3.5)
project(blksnap)

set(CMAKE_CXX_STANDARD 14)

add_subdirectory(${CMAKE_SOURCE_DIR}/lib/blksnap)
add_subdirectory(${CMAKE_SOURCE_DIR}/tools/blksnap)
add_subdirectory(${CMAKE_SOURCE_DIR}/tests/cpp)

if(EXISTS ${CMAKE_SOURCE_DIR}/cmake/cmake_uninstall.cmake.in)
configure_file(${CMAKE_SOURCE_DIR}/cmake/cmake_uninstall.cmake.in
${CMAKE_SOURCE_DIR}/cmake/cmake_uninstall.cmake @ONLY
)
add_custom_target(uninstall "${CMAKE_COMMAND}" -P "${CMAKE_SOURCE_DIR}/cmake/cmake_uninstall.cmake")
endif()
76 changes: 24 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,24 +81,7 @@ The program allows for execution of individual ioctls of the blksnap module.
The interface of the program may seem inconvenient to the user,
since it is assumed that it will be called by other applications.
### How to build
Installing the necessary deb packages.
``` bash
sudo apt install g++ cmake uuid-dev libboost-program-options-dev libboost-filesystem-dev
```
Or installing the necessary rpm packages.
``` bash
sudo yum install g++ cmake libuuid-devel boost-static libstdc++-static
```
Build.
``` bash
cd ./tools/blksnap
mkdir bin
cd bin
cmake ..
make
```

For deb packages see [how to create dev, tools and tests deb packages](#how-to-create-dev-tools-and-tests-deb-packages)
See [how to build library, tools and tests](#how-to-build-library-tools-and-tests)

## Library
The dynamic C library is not needed to work with blksnap. File
Expand All @@ -107,24 +90,7 @@ But to demonstrate how to call the ioctl, a static c++ library was created.
The library can also help you quickly create a working prototype.
In the project, the library is used for tests.
### How to build
Installing the necessary deb packages.
``` bash
sudo apt install g++ cmake uuid-dev libboost-filesystem-dev
```
Or installing the necessary rpm packages.
``` bash
sudo yum install g++ cmake libuuid-devel boost-static libstdc++-static
```
Build.
``` bash
cd ./lib/blksnap
mkdir bin
cd bin
cmake ..
make
```

For deb packages see [how to create dev, tools and tests deb packages](#how-to-create-dev-tools-and-tests-deb-packages)
See [how to build library, tools and tests](#how-to-build-library-tools-and-tests)

## Tests
The test scripts are written in bash and use the blksnap tool to control
Expand All @@ -133,6 +99,19 @@ the module. To implement complex test algorithms, С++ tests are implemented.
C++ tests use the static library libblksnap.a and it must be compiled to
build С++ tests.
### How to build
See [how to build library, tools and tests](#how-to-build-library-tools-and-tests)

### How to run all usage tests
``` bash
# change working directory to the tests one, for example for debian package is /opt/blksnap/tests
cd /opt/blksnap/tests
# execute all tests script
sudo ./all.sh
# or for logging the output to a file
sudo ./all.sh 2>&1 | tee -a /tmp/blksnap_test_$(date -u '+%Y-%m-%d_%H-%M-%S').log
```

## How to build library, tools and tests
Installing the necessary deb packages.
``` bash
sudo apt install g++ cmake uuid-dev libboost-program-options-dev libboost-filesystem-dev libssl-dev
Expand All @@ -143,26 +122,19 @@ sudo yum install g++ cmake libuuid-devel boost-static libstdc++-static openssl-s
```
Build.
``` bash
cd ./tests/cpp
mkdir bin
cd bin
cmake ..
cmake .
make
```

For deb packages see [how to create dev, tools and tests deb packages](#how-to-create-dev-tools-and-tests-deb-packages)

### How to run all usage tests
``` bash
# change working directory to the tests one, for example for debian package is /opt/blksnap/tests
cd /opt/blksnap/tests
# execute all tests script
sudo ./all.sh
# or for logging the output to a file
sudo ./all.sh 2>&1 | tee -a /tmp/blksnap_test_$(date -u '+%Y-%m-%d_%H-%M-%S').log
Install (but it is recommended to use packages instead, for example the [debian](#how-to-create-dev-tools-and-tests-deb-packages) ones)
```
sudo make install
```
Uninstall (if needed)
```
sudo make uninstall
```

## How to create dev, tools and tests deb packages
### How to create dev, tools and tests deb packages
``` bash
sudo apt install g++ cmake uuid-dev libboost-program-options-dev libboost-filesystem-dev libssl-dev debhelper
cd ./pkg/deb
Expand Down
22 changes: 22 additions & 0 deletions cmake/cmake_uninstall.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
if(NOT EXISTS "@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt")
message(FATAL_ERROR "Cannot find install manifest: \"@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt\"")
endif()

file(READ "@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt" files)
string(REPLACE "\n" ";" files "${files}")
foreach(file ${files})
message(STATUS "Uninstalling \"$ENV{DESTDIR}${file}\"")
if(EXISTS "$ENV{DESTDIR}${file}")
exec_program(
"@CMAKE_COMMAND@" ARGS "-E rm -f \"$ENV{DESTDIR}${file}\""
OUTPUT_VARIABLE rm_out
RETURN_VALUE rm_retval
)
if("${rm_retval}" STREQUAL 0)
else()
message(FATAL_ERROR "Problem when removing \"$ENV{DESTDIR}${file}\"")
endif()
else()
message(STATUS "File \"$ENV{DESTDIR}${file}\" does not exist.")
endif()
endforeach()
20 changes: 10 additions & 10 deletions lib/blksnap/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

cmake_minimum_required(VERSION 3.5)
project(blksnap)
project(blksnap-dev)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libstdc++ -static-libgcc -pthread")
Expand All @@ -30,20 +30,20 @@ if (NOT LIBUUID_LIBRARY)
message(FATAL_ERROR "libuuid not found. please install uuid-dev or libuuid-devel package.")
endif ()

include_directories(${CMAKE_SOURCE_DIR}/../../include)

set(SOURCE_FILES
Blksnap.cpp
Cbt.cpp
Service.cpp
Session.cpp
)

add_library(${PROJECT_NAME} ${SOURCE_FILES})
add_library(${PROJECT_NAME} STATIC ${SOURCE_FILES})
set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME "blksnap")

set_target_properties(${PROJECT_NAME}
PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}../../
#LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}../../
#RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}../../
)
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../include)

install(TARGETS ${PROJECT_NAME} DESTINATION lib)

install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../../include/blksnap
DESTINATION include
FILES_MATCHING PATTERN "*.h")
2 changes: 1 addition & 1 deletion lib/blksnap/Session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ namespace

static void FallocateStorage(const std::string& filename, const off_t filesize)
{
int fd = ::open(filename.c_str(), O_CREAT | O_RDWR | O_EXCL | O_LARGEFILE);
int fd = ::open(filename.c_str(), O_CREAT | O_RDWR | O_EXCL | O_LARGEFILE, 0600);
if (fd < 0)
throw std::system_error(errno, std::generic_category(), "[TBD]Failed to create file for diff storage.");

Expand Down
2 changes: 1 addition & 1 deletion pkg/deb/blksnap/blksnap-tests.install
Original file line number Diff line number Diff line change
@@ -1 +1 @@
opt
opt/blksnap/tests
15 changes: 14 additions & 1 deletion pkg/deb/blksnap/rules
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
#!/usr/bin/make -f
# -*- makefile -*-
include /usr/share/dpkg/pkg-info.mk
include /usr/share/dpkg/default.mk
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1
CFLAGS+=$(CPPFLAGS)
CXXFLAGS+=$(CPPFLAGS)


DEB_CMAKE_EXTRA_FLAGS = \
-DCMAKE_INSTALL_PREFIX=/usr \
-DCMAKE_BUILD_TYPE=RelWithDebInfo

%:
dh $@

override_dh_auto_configure:
dh_auto_configure -- $(DEB_CMAKE_EXTRA_FLAGS)

68 changes: 5 additions & 63 deletions pkg/deb/build-blksnap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,76 +11,18 @@ CURR_DIR=$(pwd)
cd "../../"
ROOT_DIR=$(pwd)

BUILD_DIR="build/pkg"
rm -rf ${BUILD_DIR}
mkdir -p ${BUILD_DIR}
# prepare debian directory
rm -rf ${ROOT_DIR}/debian
cp -r ${CURR_DIR}/blksnap ${ROOT_DIR}/debian

### build library
mkdir -p "lib/blksnap/bin"
cd "lib/blksnap/bin"
cmake ../
make
cd ${ROOT_DIR}

# copy library
mkdir -p ${BUILD_DIR}/usr/lib/
cp lib/libblksnap.a ${BUILD_DIR}/usr/lib/
# copy include
mkdir -p ${BUILD_DIR}/usr/include/blksnap/
cp include/blksnap/* ${BUILD_DIR}/usr/include/blksnap/

### build tools
SOURCE_DIR="tools/blksnap/bin"
TARGET_DIR="usr/sbin"

# build
rm -rf ${SOURCE_DIR}/*
mkdir -p ${SOURCE_DIR}
cd ${SOURCE_DIR}
cmake ../
make
cd ${ROOT_DIR}

# copy binaries
mkdir -p ${BUILD_DIR}/${TARGET_DIR}
cp ${SOURCE_DIR}/blksnap ${BUILD_DIR}/${TARGET_DIR}/
chmod +x ${BUILD_DIR}/${TARGET_DIR}/*

### build tests
SOURCE_DIR="tests"
TARGET_DIR="opt/blksnap/tests"

# build
rm -rf ${SOURCE_DIR}/cpp/bin/*
mkdir -p ${SOURCE_DIR}/cpp/bin/*
cd ${SOURCE_DIR}/cpp/bin
cmake ../
make
cd ${ROOT_DIR}

# copy binaries and scripts
mkdir -p ${BUILD_DIR}/${TARGET_DIR}
for FILE in ${SOURCE_DIR}/*
do
if [ -f "${FILE}" ]
then
cp ${FILE} ${BUILD_DIR}/${TARGET_DIR}/
fi
done
chmod +x ${BUILD_DIR}/${TARGET_DIR}/*


### prepare other package files
cp -r ${CURR_DIR}/blksnap ${BUILD_DIR}/debian

cat > ${BUILD_DIR}/debian/changelog << EOF
cat > ${ROOT_DIR}/debian/changelog << EOF
blksnap (${VERSION}) stable; urgency=low
* Release.
-- Veeam Software Group GmbH <veeam_team@veeam.com> $(date -R)
EOF

cd ${BUILD_DIR}
cd ${ROOT_DIR}

# build backage
dpkg-buildpackage -us -uc -b
Expand Down
2 changes: 1 addition & 1 deletion tests/blksnap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ if [ -f "/usr/bin/blksnap" ] || [ -f "/usr/sbin/blksnap" ]
then
BLKSNAP=blksnap
else
BLKSNAP="$(cd ../; pwd)/tools/blksnap/bin/blksnap"
BLKSNAP="$(cd ../; pwd)/tools/blksnap/blksnap"
fi

ID=""
Expand Down
Loading

0 comments on commit f4b6f0c

Please sign in to comment.