Skip to content

Commit

Permalink
CMake: Add basic CMake build system
Browse files Browse the repository at this point in the history
Introduce an initial CMake buildsystem. This build system can build
a fully working apt system without translation or documentation.

The FindBerkelyDB module is from kdelibs, with some small adjustements
to also look in db5 directories.

Initial work on this CMake build system started in 2009, and was
resumed in August 2016.
  • Loading branch information
julian-klode committed Aug 6, 2016
1 parent cc1b834 commit f3de2db
Show file tree
Hide file tree
Showing 14 changed files with 601 additions and 0 deletions.
48 changes: 48 additions & 0 deletions CMake/FindBerkeleyDB.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# - Try to find Berkeley DB
# Once done this will define
#
# BERKELEY_DB_FOUND - system has Berkeley DB
# BERKELEY_DB_INCLUDE_DIRS - the Berkeley DB include directory
# BERKELEY_DB_LIBRARIES - Link these to use Berkeley DB
# BERKELEY_DB_DEFINITIONS - Compiler switches required for using Berkeley DB

# Copyright (c) 2006, Alexander Dymo, <[email protected]>
# Copyright (c) 2016, Julian Andres Klode <[email protected]>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. The name of the author may not be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


find_path(BERKELEY_DB_INCLUDE_DIRS db.h
/usr/include/db5
/usr/local/include/db5
/usr/include/db4
/usr/local/include/db4
)

find_library(BERKELEY_DB_LIBRARIES NAMES db )

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Berkeley "Could not find Berkeley DB >= 4.1" BERKELEY_DB_INCLUDE_DIRS BERKELEY_DB_LIBRARIES)
# show the BERKELEY_DB_INCLUDE_DIRS and BERKELEY_DB_LIBRARIES variables only in the advanced view
mark_as_advanced(BERKELEY_DB_INCLUDE_DIRS BERKELEY_DB_LIBRARIES)
65 changes: 65 additions & 0 deletions CMake/Misc.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
include(CheckCXXCompilerFlag)

# Flatten our header structure
function(flatify target headers)
foreach(header ${headers})
get_filename_component(tgt ${header} NAME)
configure_file(${header} ${target}/${tgt} @ONLY)
endforeach(header ${headers})
endfunction()


function(add_optional_compile_options flags)
foreach(flag ${flags})
check_cxx_compiler_flag(-${flag} have-compiler-flag:-${flag})
if (have-compiler-flag:-${flag})
add_compile_options("-${flag}")
endif()
endforeach()
endfunction()

# Substitute vendor references in a file
function(add_vendor_file)
set(options)
set(oneValueArgs OUTPUT INPUT MODE)
set(multiValueArgs VARIABLES)
cmake_parse_arguments(AVF "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
message(STATUS "Configuring vendor file ${AVF_OUTPUT}")

FILE(READ ${CMAKE_CURRENT_SOURCE_DIR}/${AVF_INPUT} input)
foreach(variable ${AVF_VARIABLES})
execute_process(COMMAND ../vendor/getinfo ${variable} OUTPUT_VARIABLE value OUTPUT_STRIP_TRAILING_WHITESPACE)
string(REPLACE "&${variable};" "${value}" input "${input}")
endforeach()
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/${AVF_OUTPUT} "${input}")

execute_process(COMMAND chmod ${AVF_MODE} ${CMAKE_CURRENT_BINARY_DIR}/${AVF_OUTPUT})
endfunction()

# Add symbolic links to a file
function(add_slaves destination master)
set(slaves "")
foreach(slave ${ARGN})
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${slave}
COMMAND ${CMAKE_COMMAND} -E create_symlink ${master} ${CMAKE_CURRENT_BINARY_DIR}/${slave})
list(APPEND slaves ${CMAKE_CURRENT_BINARY_DIR}/${slave})
endforeach()

STRING(REPLACE "/" "-" master "${master}")
add_custom_target(${master}-slaves ALL DEPENDS ${slaves})
install(FILES ${slaves} DESTINATION ${destination})
endfunction()

# Generates a simple version script versioning everything with current SOVERSION
function(add_version_script target)
get_target_property(soversion ${target} SOVERSION)
set(script "${CMAKE_CURRENT_BINARY_DIR}/${target}.versionscript")
string(REPLACE "-" "" name "${target}_${soversion}")
string(TOUPPER "${name}" name)
add_custom_command(OUTPUT "${script}"
COMMAND echo "${name} {global: *; };" > "${script}"
VERBATIM )
add_custom_target(${target}-versionscript DEPENDS "${script}")
target_link_libraries(${target} PRIVATE -Wl,-version-script="${script}")
add_dependencies(${target} ${target}-versionscript)
endfunction()
30 changes: 30 additions & 0 deletions CMake/apti18n.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// -*- mode: cpp; mode: fold -*-
// $Id: apti18n.h.in,v 1.6 2003/01/11 07:18:18 jgg Exp $
/* Internationalization macros for apt. This header should be included last
in each C file. */

// Set by autoconf
#cmakedefine USE_NLS

#ifdef USE_NLS
// apt will use the gettext implementation of the C library
#include <libintl.h>
#include <locale.h>
# ifdef APT_DOMAIN
# define _(x) dgettext(APT_DOMAIN,x)
# define P_(msg,plural,n) dngettext(APT_DOMAIN,msg,plural,n)
# else
# define _(x) gettext(x)
# define P_(msg,plural,n) ngettext(msg,plural,n)
# endif
# define N_(x) x
#else
// apt will not use any gettext
# define setlocale(a, b)
# define textdomain(a)
# define bindtextdomain(a, b)
# define _(x) x
# define P_(msg,plural,n) (n == 1 ? msg : plural)
# define N_(x) x
# define dgettext(d, m) m
#endif
54 changes: 54 additions & 0 deletions CMake/config.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* Define if your processor stores words with the most significant
byte first (like Motorola and SPARC, unlike Intel and VAX). */
#cmakedefine WORDS_BIGENDIAN

/* Define if we have the timegm() function */
#cmakedefine HAVE_TIMEGM

/* Define if we have the zlib library for gzip */
#cmakedefine HAVE_ZLIB

/* Define if we have the bz2 library for bzip2 */
#cmakedefine HAVE_BZ2

/* Define if we have the lzma library for lzma/xz */
#cmakedefine HAVE_LZMA

/* Define if we have the lz4 library for lz4 */
#cmakedefine HAVE_LZ4

/* These two are used by the statvfs shim for glibc2.0 and bsd */
/* Define if we have sys/vfs.h */
#cmakedefine HAVE_VFS_H
#cmakedefine HAVE_STRUCT_STATFS_F_TYPE

/* Define if we have sys/mount.h */
#cmakedefine HAVE_MOUNT_H

/* Define if we have enabled pthread support */
#cmakedefine HAVE_PTHREAD

/* Check for getresuid() function and similar ones */
#cmakedefine HAVE_GETRESUID
#cmakedefine HAVE_GETRESGID
#cmakedefine HAVE_SETRESUID
#cmakedefine HAVE_SETRESGID

/* Define the arch name string */
#define COMMON_ARCH "${COMMON_ARCH}"

/* The package name string */
#define PACKAGE "${PACKAGE}"

/* The version number string */
#define PACKAGE_VERSION "${PACKAGE_VERSION}"

/* The mail address to reach upstream */
#define PACKAGE_MAIL "[email protected]"

#define APT_8_CLEANER_HEADERS
#define APT_9_CLEANER_HEADERS
#define APT_10_CLEANER_HEADERS

/* unrolling is faster combined with an optimizing compiler */
#define SHA2_UNROLL_TRANSFORM
132 changes: 132 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# Copyright (C) 2009, 2016 Julian Andres Klode <[email protected]>.
# Licensed under the same terms as APT; i.e. GPL 2 or later.

# set minimum version
project(apt)
cmake_minimum_required(VERSION 3.3.0)

option(WITH_DOC "Build documentation." OFF)
option(USE_NLS "Localisation support." ON)

set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/CMake")

# Work around bug in GNUInstallDirs
if (EXISTS "/etc/debian_version")
set(CMAKE_INSTALL_LIBEXECDIR "lib")
endif()

# Include stuff
include(Misc)
include(CheckIncludeFiles)
include(CheckFunctionExists)
include(CheckStructHasMember)
include(GNUInstallDirs)
include(TestBigEndian)
find_package(Threads)
find_package(PkgConfig)

# Set compiler flags
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_VISIBILITY_INLINES_HIDDEN 1)

add_optional_compile_options(Wall)
add_optional_compile_options(Wextra)
add_optional_compile_options(Wcast-align)
add_optional_compile_options(Wlogical-op)
add_optional_compile_options(Wredundant-decls)
add_optional_compile_options(Wmissing-declarations)
add_optional_compile_options(Wunsafe-loop-optimizations)
add_optional_compile_options(Wctor-dtor-privacy)
add_optional_compile_options(Wdisabled-optimization)
add_optional_compile_options(Winit-self)
add_optional_compile_options(Wmissing-include-dirs)
add_optional_compile_options(Wnoexcept)
add_optional_compile_options(Wsign-promo)
add_optional_compile_options(Wundef)

# apt-ftparchive dependencies
find_package(BerkeleyDB REQUIRED)
if (BERKELEY_DB_FOUND)
set(HAVE_BDB 1)
endif()


# apt-transport-https dependencies
pkg_check_modules(CURL libcurl REQUIRED)
if (CURL_FOUND)
set(HAVE_CURL 1)
endif()

# (De)Compressor libraries
find_package(ZLIB REQUIRED)
if (ZLIB_FOUND)
set(HAVE_ZLIB 1)
endif()


find_package(BZip2)
if (BZIP2_FOUND)
set(HAVE_BZ2 1)
endif()

pkg_check_modules(LZMA liblzma)
if (LZMA_FOUND)
set(HAVE_LZMA 1)
endif()

pkg_check_modules(LZ4 liblz4)
if (LZ4_FOUND)
set(HAVE_LZ4 1)
endif()

# Mount()ing and stat()ing and friends

check_function_exists(statvfs HAVE_STATVFS)
if (NOT HAVE_STATVFS)
check_symbol_exists(statfs sys/vfs.h HAVE_VFS_H)
check_symbol_exists(statfs sys/mount.h HAVE_MOUNT_H)
if (NOT HAVE_VFS_H AND NOT HAVE_MOUNT_H)
message(FATAL_ERROR "Can find neither statvfs() nor statfs()")
endif()
configure_file(buildlib/statvfs.h.in ${PROJECT_BINARY_DIR}/include/statvfs.h @ONLY)
endif()

CHECK_STRUCT_HAS_MEMBER("struct statfs" f_type sys/vfs.h HAVE_STRUCT_STATFS_F_TYPE)

# Other checks
check_function_exists(getresuid HAVE_GETRESUID)
check_function_exists(getresgid HAVE_GETRESGID)
check_function_exists(setresuid HAVE_SETRESUID)
check_function_exists(setresgid HAVE_SETRESGID)
check_function_exists(timegm HAVE_TIMEGM)
test_big_endian(WORDS_BIGENDIAN)

if (CMAKE_USE_PTHREADS_INIT)
set(HAVE_PTHREAD 1)
endif()

# Configure some variables like package, version and architecture.
set(PACKAGE "apt")

execute_process(COMMAND dpkg-parsechangelog -SVersion -l${PROJECT_SOURCE_DIR}/debian/changelog
OUTPUT_VARIABLE PACKAGE_VERSION OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process(COMMAND dpkg-architecture -qDEB_HOST_ARCH
OUTPUT_VARIABLE COMMON_ARCH OUTPUT_STRIP_TRAILING_WHITESPACE)

# Configure our configuration headers (config.h and apti18n.h)
configure_file(CMake/config.h.in ${PROJECT_BINARY_DIR}/include/config.h)
configure_file(CMake/apti18n.h.in ${PROJECT_BINARY_DIR}/include/apti18n.h)

# Generic header locations
include_directories(${PROJECT_BINARY_DIR}/include)

# Add our subdirectories
add_subdirectory(vendor)
add_subdirectory(apt-pkg)
add_subdirectory(apt-private)
add_subdirectory(apt-inst)
add_subdirectory(cmdline)
add_subdirectory(dselect)
add_subdirectory(ftparchive)
add_subdirectory(methods)
36 changes: 36 additions & 0 deletions README.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
The Make System
================

To compile this program using cmake you require cmake 3.3 or newer.

Building
--------
The recommended way is to generate a build directory and build in it, e.g.

mkdir build
cd build
cmake .. OR cmake -G Ninja ..
make -j4 OR ninja

You can use either the make or the ninja generator; the ninja stuff is faster,
though. You can also build in-tree:

cmake -G Ninja
ninja

To build a subdirectory; for example, apt-pkg, use one of:

ninja apt-pkg/all
make -C apt-pkg -j4 (or cd apt-pkg && make -j4)

Ninja automatically parallelizes, make needs an explicit -j switch. The travis
system uses the make generator, the packaging as well.

TODO
----

The following features have not been implemented yet:

- documentation
- translation
- unit tests
26 changes: 26 additions & 0 deletions apt-inst/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Include apt-pkg directly, as some files have #include <system.h>
include_directories(${PROJECT_BINARY_DIR}/include/apt-pkg)

# Set the version of the library
set(MAJOR 2.0)
set(MINOR 0)
set(APT_INST_MAJOR ${MAJOR} PARENT_SCOPE)

# Definition of the C++ files used to build the library
file(GLOB_RECURSE library "*.cc")
file(GLOB_RECURSE headers "*.h")

# Create a library using the C++ files
add_library(apt-inst SHARED ${library})

# Link the library and set the SONAME
target_link_libraries(apt-inst PUBLIC apt-pkg ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries(apt-inst PRIVATE ${CMAKE_THREAD_LIBS_INIT})
set_target_properties(apt-inst PROPERTIES VERSION ${MAJOR}.${MINOR})
set_target_properties(apt-inst PROPERTIES SOVERSION ${MAJOR})
add_version_script(apt-inst)

# Install the library and the headers
install(TARGETS apt-inst LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
install(FILES ${headers} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/apt-pkg)
flatify(${PROJECT_BINARY_DIR}/include/apt-pkg/ "${headers}")
Loading

0 comments on commit f3de2db

Please sign in to comment.